Skip to content

Commit

Permalink
Add Graph.toGraphSortedByEdges: Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Jul 29, 2023
1 parent 2298799 commit 8fbcfdf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- `Graph.toGraphSortedByEdges: Graph`

### Changed

### Deprecated
Expand Down
3 changes: 3 additions & 0 deletions kotlin-stdlib/api/android/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/ut
public abstract fun renderer (Lkotlin/jvm/functions/Function1;)V
public abstract fun setRenderer (Lkotlin/jvm/functions/Function1;)V
public abstract fun toGraph ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public abstract fun toGraphSortedByEdges ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public abstract fun vertexesFor (Ljava/lang/Object;)Ljava/util/List;
public abstract fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}
Expand All @@ -222,6 +223,7 @@ public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
public static fun getHasMissingVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Z
public static fun renderer (Lcom/javiersc/kotlin/stdlib/graph/Graph;Lkotlin/jvm/functions/Function1;)V
public static fun toGraph (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Lcom/javiersc/kotlin/stdlib/graph/Graph;
public static fun toGraphSortedByEdges (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Lcom/javiersc/kotlin/stdlib/graph/Graph;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Ljava/util/List;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;[Ljava/lang/Object;)Ljava/util/List;
}
Expand Down Expand Up @@ -313,6 +315,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
public fun setRenderer (Lkotlin/jvm/functions/Function1;)V
public final fun size ()I
public fun toGraph ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public fun toGraphSortedByEdges ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public fun toString ()Ljava/lang/String;
public final fun values ()Ljava/util/Collection;
public fun vertexesFor (Ljava/lang/Object;)Ljava/util/List;
Expand Down
3 changes: 3 additions & 0 deletions kotlin-stdlib/api/jvm/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/ut
public abstract fun renderer (Lkotlin/jvm/functions/Function1;)V
public abstract fun setRenderer (Lkotlin/jvm/functions/Function1;)V
public abstract fun toGraph ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public abstract fun toGraphSortedByEdges ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public abstract fun vertexesFor (Ljava/lang/Object;)Ljava/util/List;
public abstract fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}
Expand All @@ -228,6 +229,7 @@ public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
public static fun getHasMissingVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Z
public static fun renderer (Lcom/javiersc/kotlin/stdlib/graph/Graph;Lkotlin/jvm/functions/Function1;)V
public static fun toGraph (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Lcom/javiersc/kotlin/stdlib/graph/Graph;
public static fun toGraphSortedByEdges (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Lcom/javiersc/kotlin/stdlib/graph/Graph;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Ljava/util/List;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;[Ljava/lang/Object;)Ljava/util/List;
}
Expand Down Expand Up @@ -319,6 +321,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
public fun setRenderer (Lkotlin/jvm/functions/Function1;)V
public final fun size ()I
public fun toGraph ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public fun toGraphSortedByEdges ()Lcom/javiersc/kotlin/stdlib/graph/Graph;
public fun toString ()Ljava/lang/String;
public final fun values ()Ljava/util/Collection;
public fun vertexesFor (Ljava/lang/Object;)Ljava/util/List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ public interface Graph<T> : Map<Graph.Vertex<T>, List<Graph.Edge<T>>> {

public fun vertexesFor(vararg values: T): List<T> = values.flatMap(::vertexesFor)

public fun toGraphSortedByEdges(): Graph<T> = buildGraph {
val sortedGraph: MutableGraph<T> = this
val graph: Graph<T> = this@Graph
val remainingMap: MutableMap<Vertex<T>, List<Edge<T>>> = graph.toMutableMap()
while (remainingMap.isNotEmpty()) {
val vertexesToBeRemoved: MutableList<Vertex<T>> = mutableListOf()
for ((vertex: Vertex<T>, edges: List<Edge<T>>) in remainingMap) {
if (edges.isEmpty()) {
sortedGraph.addVertex(vertex.value)
vertexesToBeRemoved.add(vertex)
println()
}
for (edge: Edge<T> in edges) {
if (edge.destination.value in sortedGraph.keys.map(Vertex<T>::value)) {
sortedGraph.addVertex(edge.source.value)
sortedGraph.addEdge(edge.source.value, edge.destination.value)
vertexesToBeRemoved.add(edge.source)
println()
}
}
}
for (vertexToBeRemoved: Vertex<T> in vertexesToBeRemoved) {
remainingMap.remove(vertexToBeRemoved)
}
vertexesToBeRemoved.clear()
}
}

public fun renderer(block: Any?.() -> String) {
this.renderer = block
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,37 @@ class GraphTest {
assertTrue { graph.vertexesFor(Qux) == listOf(Quux) }
assertTrue { graph.vertexesFor(Quux) == emptyList<A>() }
}

@Test
fun vertexes_sorted_by_edges() {
val graph: Graph<A> = buildGraph {
addVertex(Bar)
addVertex(Foo)
addVertex(Baz)
addEdge(Bar, Baz)
addEdge(Foo, Bar)
}

assertTrue {
val actual: String = graph.toString()
val expect: String = buildString {
appendLine("Bar -> [Baz]")
appendLine("Foo -> [Bar]")
appendLine("Baz -> []")
}
expect == actual
}

assertTrue {
val actual: String = graph.toGraphSortedByEdges().toString()
val expect: String = buildString {
appendLine("Baz -> []")
appendLine("Bar -> [Baz]")
appendLine("Foo -> [Bar]")
}
expect == actual
}
}
}

private object Foo : A()
Expand Down

0 comments on commit 8fbcfdf

Please sign in to comment.