Skip to content

Commit

Permalink
Add map TrieNode structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Abduqodiri Qurbonzoda committed Oct 9, 2019
1 parent 3eb424c commit d14c192
Show file tree
Hide file tree
Showing 2 changed files with 472 additions and 0 deletions.
27 changes: 27 additions & 0 deletions core/commonMain/src/implementations/immutableMap/TrieNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,33 @@ internal class TrieNode<K, V>(
return this
}

// For testing trie structure
internal fun accept(visitor: (node: TrieNode<K, V>, shift: Int, hash: Int, dataMap: Int, nodeMap: Int) -> Unit) {
accept(visitor, 0, 0)
}

@UseExperimental(ExperimentalStdlibApi::class)
private fun accept(
visitor: (node: TrieNode<K, V>, shift: Int, hash: Int, dataMap: Int, nodeMap: Int) -> Unit,
hash: Int,
shift: Int
) {
visitor(this, shift, hash, dataMap, nodeMap)

var nodePositions = nodeMap
while (nodePositions != 0) {
val mask = nodePositions.takeLowestOneBit()
// assert(hasNodeAt(mask))

val hashSegment = mask.countTrailingZeroBits()

val childNode = nodeAtIndex(nodeIndex(mask))
childNode.accept(visitor, hash + (hashSegment shl shift), shift + LOG_MAX_BRANCHING_FACTOR)

nodePositions -= mask
}
}

internal companion object {
internal val EMPTY = TrieNode<Nothing, Nothing>(0, 0, emptyArray())
}
Expand Down
Loading

0 comments on commit d14c192

Please sign in to comment.