Skip to content

Commit

Permalink
refactor(model): Use sets instead of lists for DependencyGraphEdge
Browse files Browse the repository at this point in the history
It is not necessary to allow for having multiple edges between any pair
of nodes. So, always operate with sets to reflect that.

Note that order of edges is not important either (even if a set usually
maintains insertion order).

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed May 24, 2024
1 parent 4fb504e commit 4c2db37
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions model/src/main/kotlin/DependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ data class DependencyGraph(
val nodes: List<DependencyGraphNode>? = null,

/**
* A list with the edges of this dependency graph. By traversing the edges, the dependencies of packages can be
* A set with the edges of this dependency graph. By traversing the edges, the dependencies of packages can be
* determined.
*/
val edges: List<DependencyGraphEdge>? = null
val edges: Set<DependencyGraphEdge>? = null
) {
companion object {
/**
Expand Down Expand Up @@ -419,7 +419,7 @@ private fun constructNodeDependenciesFromScopeRoots(roots: SortedSet<DependencyR
*/
private fun constructNodeDependenciesFromGraph(
nodes: List<DependencyGraphNode>,
edges: List<DependencyGraphEdge>
edges: Set<DependencyGraphEdge>
): NodeDependencies {
val mapping = mutableMapOf<DependencyGraphNode, MutableList<DependencyGraphNode>>()

Expand Down
8 changes: 4 additions & 4 deletions model/src/main/kotlin/utils/DependencyGraphBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DependencyGraphBuilder<D>(
)
}

private fun Collection<DependencyGraphEdge>.removeCycles(): List<DependencyGraphEdge> {
private fun Set<DependencyGraphEdge>.removeCycles(): Set<DependencyGraphEdge> {
val edges = toMutableSet()
val edgesToKeep = breakCycles(edges)
val edgesToRemove = edges - edgesToKeep
Expand All @@ -188,7 +188,7 @@ class DependencyGraphBuilder<D>(
logger.warn { "Removing edge '${it.from} -> ${it.to}' to break a cycle." }
}

return filter { it in edgesToKeep }
return filterTo(mutableSetOf()) { it in edgesToKeep }
}

private fun checkReferences() {
Expand Down Expand Up @@ -436,9 +436,9 @@ class DependencyGraphBuilder<D>(
*/
private fun Collection<DependencyReference>.toGraph(
indexMapping: IntArray
): Pair<List<DependencyGraphNode>, List<DependencyGraphEdge>> {
): Pair<List<DependencyGraphNode>, Set<DependencyGraphEdge>> {
val nodes = mutableSetOf<DependencyGraphNode>()
val edges = mutableListOf<DependencyGraphEdge>()
val edges = mutableSetOf<DependencyGraphEdge>()
val nodeIndices = mutableMapOf<NodeKey, Int>()

fun getOrAddNodeIndex(ref: DependencyReference): Int =
Expand Down
2 changes: 1 addition & 1 deletion model/src/test/kotlin/DependencyGraphTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class DependencyGraphTest : WordSpec({
val nodeConfig1 = DependencyGraphNode(2)
val nodeConfig2 = DependencyGraphNode(2, fragment = 1)
val nodes = listOf(nodeLogging, nodeLang, nodeCollections1, nodeCollections2, nodeConfig1, nodeConfig2)
val edges = listOf(
val edges = setOf(
DependencyGraphEdge(3, 0),
DependencyGraphEdge(4, 1),
DependencyGraphEdge(4, 2),
Expand Down

0 comments on commit 4c2db37

Please sign in to comment.