mirror of
https://github.com/AdrianKuta/Tree-Data-Structure.git
synced 2026-06-20 03:10:14 +02:00
feat: Kotlin 2.x/K2, version catalog, serialization & coroutines modules, docs
v3.4 modernization (continued): - Migrate to Kotlin 2.x (K2); introduce gradle/libs.versions.toml version catalog; simplify the JS/Wasm/Node and iOS source-set wiring for the K2 hierarchy template. - Apply binary-compatibility-validator and Kover plugins. - New published module tree-structure-serialization: @Serializable TreeNodeDto with toDto()/toTreeNode() round-trip (kotlinx.serialization). - New published module tree-structure-coroutines: asFlow()/pre/post/levelOrderFlow() (kotlinx.coroutines Flow traversal). - Docs: README examples for Sequence/navigation/functional APIs, class-level KDoc (thread-safety/complexity), and a CHANGELOG.md. - Ignore subproject build/ directories. - Bump version to 3.4.0. All JVM tests green (core + both modules).
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.github.adriankuta.datastructure.tree.coroutines
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.TreeNode
|
||||
import com.github.adriankuta.datastructure.tree.asSequence
|
||||
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
|
||||
/**
|
||||
* Emits this node and all of its descendants as a cold [Flow], traversed in the given [order].
|
||||
* Useful for plugging tree traversal into coroutine/Flow pipelines (e.g. in a ViewModel).
|
||||
*/
|
||||
public fun <T> TreeNode<T>.asFlow(
|
||||
order: TreeNodeIterators = TreeNodeIterators.PreOrder,
|
||||
): Flow<TreeNode<T>> = asSequence(order).asFlow()
|
||||
|
||||
/** Pre-order traversal as a cold [Flow]. */
|
||||
public fun <T> TreeNode<T>.preOrderFlow(): Flow<TreeNode<T>> = asFlow(TreeNodeIterators.PreOrder)
|
||||
|
||||
/** Post-order traversal as a cold [Flow]. */
|
||||
public fun <T> TreeNode<T>.postOrderFlow(): Flow<TreeNode<T>> = asFlow(TreeNodeIterators.PostOrder)
|
||||
|
||||
/** Level-order (breadth-first) traversal as a cold [Flow]. */
|
||||
public fun <T> TreeNode<T>.levelOrderFlow(): Flow<TreeNode<T>> = asFlow(TreeNodeIterators.LevelOrder)
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.github.adriankuta.datastructure.tree.coroutines
|
||||
|
||||
import com.github.adriankuta.datastructure.tree.iterators.TreeNodeIterators
|
||||
import com.github.adriankuta.datastructure.tree.tree
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class TreeNodeFlowTest {
|
||||
|
||||
private fun sample() = tree(1) {
|
||||
child(2) {
|
||||
child(4)
|
||||
child(5)
|
||||
}
|
||||
child(3) {
|
||||
child(6)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun preOrderFlowEmitsInPreOrder() = runTest {
|
||||
assertEquals(listOf(1, 2, 4, 5, 3, 6), sample().preOrderFlow().map { it.value }.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun levelOrderFlowEmitsInLevelOrder() = runTest {
|
||||
assertEquals(
|
||||
listOf(1, 2, 3, 4, 5, 6),
|
||||
sample().asFlow(TreeNodeIterators.LevelOrder).map { it.value }.toList(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user