Skip to content

Commit

Permalink
Add predicate APIs to contains and vertexesFor Graph functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Jul 30, 2023
1 parent 8fbcfdf commit 8660a6d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Added

- `Graph.toGraphSortedByEdges: Graph`
- `Graph` data structure

### Changed

Expand Down
10 changes: 7 additions & 3 deletions kotlin-stdlib/api/android/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public final class com/javiersc/kotlin/stdlib/StringsTransformKt {
public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/util/Map, kotlin/jvm/internal/markers/KMappedMarker {
public abstract fun asString ()Ljava/lang/String;
public abstract fun contains (Ljava/lang/Object;)Z
public abstract fun contains (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public abstract fun containsCircularVertexes (Ljava/lang/Object;)Z
public abstract fun doesNotContainsCircularVertexes (Ljava/lang/Object;)Z
public abstract fun getCircularVertexes ()Ljava/util/Map;
Expand All @@ -207,13 +208,14 @@ public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/ut
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;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public abstract fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}

public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
public static fun asString (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Ljava/lang/String;
public static fun contains (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun contains (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static fun containsCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun doesNotContainsCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun getCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Ljava/util/Map;
Expand All @@ -224,8 +226,9 @@ public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
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;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;[Ljava/lang/Object;)Ljava/util/List;
public static synthetic fun vertexesFor$default (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/util/List;
}

public final class com/javiersc/kotlin/stdlib/graph/Graph$Edge {
Expand Down Expand Up @@ -273,6 +276,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
public fun computeIfPresent (Lcom/javiersc/kotlin/stdlib/graph/Graph$Vertex;Ljava/util/function/BiFunction;)Ljava/util/List;
public synthetic fun computeIfPresent (Ljava/lang/Object;Ljava/util/function/BiFunction;)Ljava/lang/Object;
public fun contains (Ljava/lang/Object;)Z
public fun contains (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public fun containsCircularVertexes (Ljava/lang/Object;)Z
public fun containsKey (Lcom/javiersc/kotlin/stdlib/graph/Graph$Vertex;)Z
public final fun containsKey (Ljava/lang/Object;)Z
Expand Down Expand Up @@ -318,7 +322,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
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;
public fun vertexesFor (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}

Expand Down
10 changes: 7 additions & 3 deletions kotlin-stdlib/api/jvm/kotlin-stdlib.api
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public final class com/javiersc/kotlin/stdlib/StringsTransformKt {
public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/util/Map, kotlin/jvm/internal/markers/KMappedMarker {
public abstract fun asString ()Ljava/lang/String;
public abstract fun contains (Ljava/lang/Object;)Z
public abstract fun contains (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public abstract fun containsCircularVertexes (Ljava/lang/Object;)Z
public abstract fun doesNotContainsCircularVertexes (Ljava/lang/Object;)Z
public abstract fun getCircularVertexes ()Ljava/util/Map;
Expand All @@ -213,13 +214,14 @@ public abstract interface class com/javiersc/kotlin/stdlib/graph/Graph : java/ut
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;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public abstract fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}

public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
public static fun asString (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Ljava/lang/String;
public static fun contains (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun contains (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public static fun containsCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun doesNotContainsCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;)Z
public static fun getCircularVertexes (Lcom/javiersc/kotlin/stdlib/graph/Graph;)Ljava/util/Map;
Expand All @@ -230,8 +232,9 @@ public final class com/javiersc/kotlin/stdlib/graph/Graph$DefaultImpls {
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;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public static fun vertexesFor (Lcom/javiersc/kotlin/stdlib/graph/Graph;[Ljava/lang/Object;)Ljava/util/List;
public static synthetic fun vertexesFor$default (Lcom/javiersc/kotlin/stdlib/graph/Graph;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/util/List;
}

public final class com/javiersc/kotlin/stdlib/graph/Graph$Edge {
Expand Down Expand Up @@ -279,6 +282,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
public fun computeIfPresent (Lcom/javiersc/kotlin/stdlib/graph/Graph$Vertex;Ljava/util/function/BiFunction;)Ljava/util/List;
public synthetic fun computeIfPresent (Ljava/lang/Object;Ljava/util/function/BiFunction;)Ljava/lang/Object;
public fun contains (Ljava/lang/Object;)Z
public fun contains (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Z
public fun containsCircularVertexes (Ljava/lang/Object;)Z
public fun containsKey (Lcom/javiersc/kotlin/stdlib/graph/Graph$Vertex;)Z
public final fun containsKey (Ljava/lang/Object;)Z
Expand Down Expand Up @@ -324,7 +328,7 @@ public final class com/javiersc/kotlin/stdlib/graph/MutableGraph : com/javiersc/
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;
public fun vertexesFor (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/util/List;
public fun vertexesFor ([Ljava/lang/Object;)Ljava/util/List;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ public interface Graph<T> : Map<Graph.Vertex<T>, List<Graph.Edge<T>>> {

public fun contains(value: T): Boolean = keys.any { it.value == value }

public fun contains(value: T, predicate: (T) -> Boolean): Boolean =
keys.any { predicate(it.value) }

public fun toGraph(): Graph<T> = this

public fun containsCircularVertexes(value: T): Boolean =
circularVertexes[value]?.isNotEmpty() == true

public fun doesNotContainsCircularVertexes(value: T): Boolean = !containsCircularVertexes(value)

public fun vertexesFor(value: T): List<T> {
public fun vertexesFor(value: T, predicate: (T) -> Boolean = { it == value }): List<T> {
if (containsCircularVertexes(value)) return emptyList()
val vertex: Vertex<T> = keys.find { it.value == value } ?: return emptyList()
val vertex: Vertex<T> = keys.find { predicate(it.value) } ?: return emptyList()
val edges: List<Edge<T>> = this[vertex] ?: return emptyList()
val destinationVertexes: Sequence<Vertex<T>> = edges.asSequence().map(Edge<T>::destination)
val destinations: Sequence<T> = destinationVertexes.map(Vertex<T>::value)
Expand Down

0 comments on commit 8660a6d

Please sign in to comment.