Skip to content

Commit 3525070

Browse files
author
Abduqodiri Qurbonzoda
committed
Add map TrieNode structure tests
1 parent b654c6f commit 3525070

File tree

2 files changed

+472
-0
lines changed

2 files changed

+472
-0
lines changed

core/commonMain/src/implementations/immutableMap/TrieNode.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,33 @@ internal class TrieNode<K, V>(
656656
return this
657657
}
658658

659+
// For testing trie structure
660+
fun accept(visitor: (node: TrieNode<K, V>, shift: Int, hash: Int, dataMap: Int, nodeMap: Int) -> Unit) {
661+
accept(visitor, 0, 0)
662+
}
663+
664+
@UseExperimental(ExperimentalStdlibApi::class)
665+
private fun accept(
666+
visitor: (node: TrieNode<K, V>, shift: Int, hash: Int, dataMap: Int, nodeMap: Int) -> Unit,
667+
hash: Int,
668+
shift: Int
669+
) {
670+
visitor(this, shift, hash, dataMap, nodeMap)
671+
672+
var nodePositions = nodeMap
673+
while (nodePositions != 0) {
674+
val mask = nodePositions.takeLowestOneBit()
675+
// assert(hasNodeAt(mask))
676+
677+
val hashSegment = mask.countTrailingZeroBits()
678+
679+
val childNode = nodeAtIndex(nodeIndex(mask))
680+
childNode.accept(visitor, hash + (hashSegment shl shift), shift + LOG_MAX_BRANCHING_FACTOR)
681+
682+
nodePositions -= mask
683+
}
684+
}
685+
659686
internal companion object {
660687
internal val EMPTY = TrieNode<Nothing, Nothing>(0, 0, emptyArray())
661688
}

0 commit comments

Comments
 (0)