forked from JetBrains/kotlin-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove dependency to Kotlin compiler in build tools just because of DFS
(cherry picked from commit 6d9a9e7)
- Loading branch information
1 parent
69b617c
commit 4d1057f
Showing
2 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
build-tools/src/main/kotlin/org/jetbrains/kotlin/utils/DFS.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.jetbrains.kotlin.utils | ||
|
||
import java.util.* | ||
|
||
// Copied from Kotlin org.jetbrains.kotlin.utils.DFS class | ||
@Suppress("MemberVisibilityCanBePrivate", "MemberVisibilityCanBePrivate", "unused") | ||
object DFS { | ||
fun <N, R> dfs(nodes: Collection<N>, neighbors: Neighbors<N>, visited: Visited<N>, handler: NodeHandler<N, R>): R { | ||
for (node in nodes) { | ||
doDfs(node, neighbors, visited, handler) | ||
} | ||
return handler.result() | ||
} | ||
|
||
fun <N, R> dfs( | ||
nodes: Collection<N>, | ||
neighbors: Neighbors<N>, | ||
handler: NodeHandler<N, R> | ||
): R { | ||
return dfs(nodes, neighbors, VisitedWithSet(), handler) | ||
} | ||
|
||
fun <N> ifAny( | ||
nodes: Collection<N>, | ||
neighbors: Neighbors<N>, | ||
predicate: Function1<N, Boolean> | ||
): Boolean { | ||
val result = BooleanArray(1) | ||
return dfs(nodes, neighbors, object : AbstractNodeHandler<N, Boolean?>() { | ||
override fun beforeChildren(current: N): Boolean { | ||
if (predicate.invoke(current)) { | ||
result[0] = true | ||
} | ||
return !result[0] | ||
} | ||
|
||
override fun result(): Boolean { | ||
return result[0] | ||
} | ||
})!! | ||
} | ||
|
||
fun <N, R> dfsFromNode(node: N, neighbors: Neighbors<N>, visited: Visited<N>, handler: NodeHandler<N, R>): R { | ||
doDfs(node, neighbors, visited, handler) | ||
return handler.result() | ||
} | ||
|
||
fun <N> dfsFromNode( | ||
node: N, | ||
neighbors: Neighbors<N>, | ||
visited: Visited<N> | ||
) { | ||
dfsFromNode(node, neighbors, visited, object : AbstractNodeHandler<N, Void?>() { | ||
override fun result(): Void? { | ||
return null | ||
} | ||
}) | ||
} | ||
|
||
fun <N> topologicalOrder(nodes: Iterable<N>, neighbors: Neighbors<N>, visited: Visited<N>): List<N> { | ||
val handler = TopologicalOrder<N>() | ||
for (node in nodes) { | ||
doDfs(node, neighbors, visited, handler) | ||
} | ||
return handler.result() | ||
} | ||
|
||
fun <N> topologicalOrder(nodes: Iterable<N>, neighbors: Neighbors<N>): List<N> { | ||
return topologicalOrder(nodes, neighbors, VisitedWithSet()) | ||
} | ||
|
||
fun <N> doDfs(current: N, neighbors: Neighbors<N>, visited: Visited<N>, handler: NodeHandler<N, *>) { | ||
if (!visited.checkAndMarkVisited(current)) return | ||
if (!handler.beforeChildren(current)) return | ||
for (neighbor in neighbors.getNeighbors(current)) { | ||
doDfs(neighbor, neighbors, visited, handler) | ||
} | ||
handler.afterChildren(current) | ||
} | ||
|
||
interface NodeHandler<N, R> { | ||
fun beforeChildren(current: N): Boolean | ||
fun afterChildren(current: N) | ||
fun result(): R | ||
} | ||
|
||
interface Neighbors<N> { | ||
fun getNeighbors(current: N): Iterable<N> | ||
} | ||
|
||
interface Visited<N> { | ||
fun checkAndMarkVisited(current: N): Boolean | ||
} | ||
|
||
abstract class AbstractNodeHandler<N, R> : NodeHandler<N, R> { | ||
override fun beforeChildren(current: N): Boolean { | ||
return true | ||
} | ||
|
||
override fun afterChildren(current: N) {} | ||
} | ||
|
||
class VisitedWithSet<N> @JvmOverloads constructor(private val visited: MutableSet<N> = HashSet()) : Visited<N> { | ||
override fun checkAndMarkVisited(current: N): Boolean { | ||
return visited.add(current) | ||
} | ||
} | ||
|
||
abstract class CollectingNodeHandler<N, R, C : Iterable<R>> protected constructor(protected val result: C) : AbstractNodeHandler<N, C>() { | ||
override fun result(): C { | ||
return result | ||
} | ||
} | ||
|
||
abstract class NodeHandlerWithListResult<N, R> protected constructor() : CollectingNodeHandler<N, R, LinkedList<R>>(LinkedList<R>()) | ||
class TopologicalOrder<N> : NodeHandlerWithListResult<N, N>() { | ||
override fun afterChildren(current: N) { | ||
result.addFirst(current) | ||
} | ||
} | ||
} |