support iteration over BTrees for the compilation cache
This commit is contained in:
parent
826c1e2d78
commit
9d27079606
1 changed files with 36 additions and 0 deletions
|
|
@ -122,6 +122,39 @@ proc `$`[Key, Val](b: BTree[Key, Val]): string =
|
||||||
result = ""
|
result = ""
|
||||||
toString(b.root, "", result)
|
toString(b.root, "", result)
|
||||||
|
|
||||||
|
proc hasNext*[Key, Val](b: BTree[Key, Val]; index: int): bool =
|
||||||
|
result = index < b.entries
|
||||||
|
|
||||||
|
proc countSubTree[Key, Val](it: Node[Key, Val]): int =
|
||||||
|
if it.isInternal:
|
||||||
|
result = 0
|
||||||
|
for k in 0..<it.entries:
|
||||||
|
inc result, countSubTree(it.links[k])
|
||||||
|
else:
|
||||||
|
result = it.entries
|
||||||
|
|
||||||
|
proc next*[Key, Val](b: BTree[Key, Val]; index: int): (Key, Val, int) =
|
||||||
|
var it = b.root
|
||||||
|
var i = index
|
||||||
|
# navigate to the right leaf:
|
||||||
|
while it.isInternal:
|
||||||
|
var sum = 0
|
||||||
|
for k in 0..<it.entries:
|
||||||
|
let c = countSubTree(it.links[k])
|
||||||
|
inc sum, c
|
||||||
|
if sum > i:
|
||||||
|
it = it.links[k]
|
||||||
|
dec i, (sum - c)
|
||||||
|
break
|
||||||
|
result = (it.keys[i], it.vals[i], index+1)
|
||||||
|
|
||||||
|
iterator pairs*[Key, Val](b: BTree[Key, Val]): (Key, Val) =
|
||||||
|
var i = 0
|
||||||
|
while hasNext(b, i):
|
||||||
|
let (k, v, i2) = next(b, i)
|
||||||
|
i = i2
|
||||||
|
yield (k, v)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
||||||
import random, tables
|
import random, tables
|
||||||
|
|
@ -154,6 +187,9 @@ when isMainModule:
|
||||||
assert st.getOrDefault("www.dell.com") == "143.166.224.230"
|
assert st.getOrDefault("www.dell.com") == "143.166.224.230"
|
||||||
assert(st.entries == 16)
|
assert(st.entries == 16)
|
||||||
|
|
||||||
|
for k, v in st:
|
||||||
|
echo k, ": ", v
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
var b2 = initBTree[string, string]()
|
var b2 = initBTree[string, string]()
|
||||||
const iters = 10_000
|
const iters = 10_000
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue