Skip to content

Commit

Permalink
Add CollectionUtils#poolDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Aug 2, 2023
1 parent 6d53279 commit e35a5d1
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,34 @@ object CollectionUtils {
""".trimIndent())
}

fun <T> poolDependencies(collection: Collection<T>, dependenciesMapper: (T) -> Set<T>): Set<Set<T>> {
val dependencies: Map<T, Set<T>> = collection.associateWithTo(HashMap()) { dependenciesMapper(it).filterTo(HashSet(), collection::contains) }
val dependants: Map<T, Set<T>> = collection.associateWithTo(HashMap()) { collection.filterTo(HashSet()) { candidate -> it in dependencies[candidate]!! } }

val pools = HashSet<HashSet<T>>()
val queue = LinkedList(collection)

while (queue.isNotEmpty()) {
val pool = HashSet<T>()

val unexplored = LinkedList<T>()
unexplored += queue.poll()

while (unexplored.isNotEmpty()) {
val entry = unexplored.poll()
pool += entry

fun explore(entries: Set<T>) =
entries.forEach { if (it !in pool) unexplored += it }

explore(dependencies[entry]!!)
explore(dependants[entry]!!)
}

pools += pool
}

return pools
}

}

0 comments on commit e35a5d1

Please sign in to comment.