added btrees.contains
This commit is contained in:
parent
6d19d1eeb2
commit
1ed7751dac
1 changed files with 14 additions and 3 deletions
|
|
@ -26,7 +26,6 @@ type
|
||||||
links: array[M, Node[Key, Val]]
|
links: array[M, Node[Key, Val]]
|
||||||
BTree*[Key, Val] = object
|
BTree*[Key, Val] = object
|
||||||
root: Node[Key, Val]
|
root: Node[Key, Val]
|
||||||
height: int
|
|
||||||
entries: int ## number of key-value pairs
|
entries: int ## number of key-value pairs
|
||||||
|
|
||||||
proc initBTree*[Key, Val](): BTree[Key, Val] =
|
proc initBTree*[Key, Val](): BTree[Key, Val] =
|
||||||
|
|
@ -46,6 +45,18 @@ proc getOrDefault*[Key, Val](b: BTree[Key, Val], key: Key): Val =
|
||||||
for j in 0 ..< x.entries:
|
for j in 0 ..< x.entries:
|
||||||
if eq(key, x.keys[j]): return x.vals[j]
|
if eq(key, x.keys[j]): return x.vals[j]
|
||||||
|
|
||||||
|
proc contains*[Key, Val](b: BTree[Key, Val], key: Key): bool =
|
||||||
|
var x = b.root
|
||||||
|
while x.isInternal:
|
||||||
|
for j in 0 ..< x.entries:
|
||||||
|
if j+1 == x.entries or less(key, x.keys[j+1]):
|
||||||
|
x = x.links[j]
|
||||||
|
break
|
||||||
|
assert(not x.isInternal)
|
||||||
|
for j in 0 ..< x.entries:
|
||||||
|
if eq(key, x.keys[j]): return true
|
||||||
|
return false
|
||||||
|
|
||||||
proc copyHalf[Key, Val](h, result: Node[Key, Val]) =
|
proc copyHalf[Key, Val](h, result: Node[Key, Val]) =
|
||||||
for j in 0 ..< Mhalf:
|
for j in 0 ..< Mhalf:
|
||||||
result.keys[j] = h.keys[Mhalf + j]
|
result.keys[j] = h.keys[Mhalf + j]
|
||||||
|
|
@ -106,7 +117,6 @@ proc add*[Key, Val](b: var BTree[Key, Val]; key: Key; val: Val) =
|
||||||
t.keys[1] = u.keys[0]
|
t.keys[1] = u.keys[0]
|
||||||
t.links[1] = u
|
t.links[1] = u
|
||||||
b.root = t
|
b.root = t
|
||||||
inc b.height
|
|
||||||
|
|
||||||
proc toString[Key, Val](h: Node[Key, Val], indent: string; result: var string) =
|
proc toString[Key, Val](h: Node[Key, Val], indent: string; result: var string) =
|
||||||
if not h.isInternal:
|
if not h.isInternal:
|
||||||
|
|
@ -155,6 +165,8 @@ iterator pairs*[Key, Val](b: BTree[Key, Val]): (Key, Val) =
|
||||||
i = i2
|
i = i2
|
||||||
yield (k, v)
|
yield (k, v)
|
||||||
|
|
||||||
|
proc len*[Key, Val](b: BTree[Key, Val]): int {.inline.} = b.entries
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
|
||||||
import random, tables
|
import random, tables
|
||||||
|
|
@ -200,7 +212,6 @@ when isMainModule:
|
||||||
if x != $(iters - i):
|
if x != $(iters - i):
|
||||||
echo "got ", x, ", but expected ", iters - i
|
echo "got ", x, ", but expected ", iters - i
|
||||||
echo b2.entries
|
echo b2.entries
|
||||||
echo b2.height
|
|
||||||
|
|
||||||
when true:
|
when true:
|
||||||
var b2 = initBTree[int, string]()
|
var b2 = initBTree[int, string]()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue