Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullinAM committed Feb 26, 2024
1 parent 82e53c2 commit dcefd71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>kt-helper</artifactId>
<groupId>org.vorpal.research</groupId>
<packaging>jar</packaging>
<version>0.1.13</version>
<version>0.1.14</version>

<properties>
<jvm.version>1.8</jvm.version>
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/org/vorpal/research/kthelper/files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package org.vorpal.research.kthelper
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.nio.file.Path

private const val MAX_BYTE_ARRAY_SIZE = 16384

Expand All @@ -22,3 +23,11 @@ val InputStream.asByteArray: ByteArray get() {
}

fun File.write(input: InputStream) = this.writeBytes(input.asByteArray)

fun Path.resolve(vararg names: String): Path {
var current = this
for (name in names) {
current = current.resolve(name)
}
return current
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.vorpal.research.kthelper.graph
import org.vorpal.research.kthelper.KtException
import org.vorpal.research.kthelper.collection.queueOf
import org.vorpal.research.kthelper.collection.stackOf
import java.util.ArrayDeque
import java.util.*

class NoTopologicalSortingException(msg: String) : KtException(msg)

Expand Down Expand Up @@ -81,4 +81,15 @@ class GraphTraversal<T : Graph.Vertex<T>>(private val graph: Graph<T>) {
}

fun topologicalSort(start: T = graph.entry): List<T> = topologicalSort(start) { it }

fun components(): Pair<UInt, Map<T, UInt>> {
var componentNumber = 0U
val components = mutableMapOf<T, UInt>()
for (node in graph.nodes) {
if (node in components) continue
dfs(node) { components[it] = componentNumber }
++componentNumber
}
return (componentNumber + 1U) to components
}
}

0 comments on commit dcefd71

Please sign in to comment.