immutable BTree works
This commit is contained in:
parent
2908b1e44a
commit
1f1edeb23f
1 changed files with 28 additions and 28 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
## to off load pages to a file system or to send it over the wire.
|
## to off load pages to a file system or to send it over the wire.
|
||||||
|
|
||||||
const
|
const
|
||||||
M = 128 # max children per B-tree node = M-1
|
M = 4 # max children per B-tree node = M-1
|
||||||
# (must be even and greater than 2)
|
# (must be even and greater than 2)
|
||||||
Mhalf = M div 2
|
Mhalf = M div 2
|
||||||
|
|
||||||
|
|
@ -106,40 +106,40 @@ proc insert(h: Node, key: Key, val: Val, ht: int): Node =
|
||||||
return if h.m < M: nil else: split(h)
|
return if h.m < M: nil else: split(h)
|
||||||
|
|
||||||
proc insertPs(h: Node, key: Key, val: Val, ht: int): (Node, Node) =
|
proc insertPs(h: Node, key: Key, val: Val, ht: int): (Node, Node) =
|
||||||
#var t = Entry(key: key, val: val, next: nil)
|
|
||||||
var newKey = key
|
|
||||||
var j = 0
|
var j = 0
|
||||||
var hh = Node(m: h.m, isInternal: h.isInternal)
|
var hh = Node(m: h.m, isInternal: h.isInternal)
|
||||||
|
for i in 0 ..< h.m: hh.keys[i] = h.keys[i]
|
||||||
if ht == 0:
|
if ht == 0:
|
||||||
assert(not h.isInternal)
|
assert(not h.isInternal)
|
||||||
while j < h.m:
|
while j < h.m:
|
||||||
if less(key, h.keys[j]): break
|
if less(key, h.keys[j]): break
|
||||||
inc j
|
inc j
|
||||||
for i in countdown(h.m, j+1):
|
for i in countdown(h.m, j+1): shallowCopy(hh.vals[i], h.vals[i-1])
|
||||||
shallowCopy(hh.vals[i], h.vals[i-1])
|
for i in 0 ..< j: shallowCopy(hh.vals[i], h.vals[i])
|
||||||
|
for i in countdown(h.m, j+1): hh.keys[i] = hh.keys[i-1]
|
||||||
hh.vals[j] = val
|
hh.vals[j] = val
|
||||||
|
hh.keys[j] = key
|
||||||
else:
|
else:
|
||||||
assert h.isInternal
|
assert h.isInternal
|
||||||
var newLink: Node = nil
|
# could optimize this copying here:
|
||||||
|
for i in 0 ..< h.m: hh.links[i] = h.links[i]
|
||||||
while j < h.m:
|
while j < h.m:
|
||||||
if j+1 == h.m or less(key, h.keys[j+1]):
|
if j+1 == h.m or less(key, h.keys[j+1]):
|
||||||
let (u, root) = insertPs(h.links[j], key, val, ht-1)
|
let (root, r) = insertPs(h.links[j], key, val, ht-1)
|
||||||
if u == nil: return (u, root)
|
|
||||||
hh.links[j] = root
|
hh.links[j] = root
|
||||||
newKey = u.keys[0]
|
if r == nil:
|
||||||
newLink = u
|
return (hh, nil)
|
||||||
|
else:
|
||||||
inc j
|
inc j
|
||||||
|
for i in countdown(h.m, j+1):
|
||||||
|
hh.links[i] = hh.links[i-1]
|
||||||
|
hh.keys[i] = hh.keys[i-1]
|
||||||
|
hh.links[j] = r
|
||||||
|
hh.keys[j] = r.keys[0]
|
||||||
break
|
break
|
||||||
inc j
|
inc j
|
||||||
for i in countdown(h.m, j+1):
|
|
||||||
hh.links[i] = h.links[i-1]
|
|
||||||
hh.links[j] = newLink
|
|
||||||
|
|
||||||
for i in countdown(h.m, j+1):
|
|
||||||
hh.keys[i] = h.keys[i-1]
|
|
||||||
hh.keys[j] = newKey
|
|
||||||
inc hh.m
|
inc hh.m
|
||||||
return if hh.m < M: (nil, nil) else: (split(hh), hh)
|
return if hh.m < M: (hh, nil) else: (hh, split(hh))
|
||||||
|
|
||||||
proc put(b: var BTree; key: Key; val: Val) =
|
proc put(b: var BTree; key: Key; val: Val) =
|
||||||
let u = insert(b.root, key, val, b.height)
|
let u = insert(b.root, key, val, b.height)
|
||||||
|
|
@ -156,13 +156,11 @@ proc put(b: var BTree; key: Key; val: Val) =
|
||||||
inc b.height
|
inc b.height
|
||||||
|
|
||||||
proc putPs(b: BTree; key: Key; val: Val): BTree =
|
proc putPs(b: BTree; key: Key; val: Val): BTree =
|
||||||
let v = insertPs(b.root, key, val, b.height)
|
let (root, u) = insertPs(b.root, key, val, b.height)
|
||||||
result.n = b.n + 1
|
result.n = b.n + 1
|
||||||
result.height = b.height
|
result.height = b.height
|
||||||
let u = v[0]
|
|
||||||
let root = v[1]
|
|
||||||
if u == nil:
|
if u == nil:
|
||||||
result.root = Node(m: 0, isInternal: false)
|
result.root = root
|
||||||
return
|
return
|
||||||
# need to split root
|
# need to split root
|
||||||
let t = Node(m: 2, isInternal: true)
|
let t = Node(m: 2, isInternal: true)
|
||||||
|
|
@ -231,15 +229,17 @@ proc main =
|
||||||
echo b2.height
|
echo b2.height
|
||||||
|
|
||||||
when true:
|
when true:
|
||||||
|
var b1 = newBTree()
|
||||||
var b2 = newBTree()
|
var b2 = newBTree()
|
||||||
const iters = 10_000
|
const iters = 60_000
|
||||||
for i in 1..iters:
|
for i in 1..iters:
|
||||||
b2 = b2.putPs($i, $(iters - i))
|
b2 = b2.putPs($i, $(iters - i))
|
||||||
|
b1.put($i, $(iters - i))
|
||||||
for i in 1..iters:
|
for i in 1..iters:
|
||||||
let x = b2.get($i)
|
let x = b2.get($i)
|
||||||
if x != $(iters - i):
|
if x != $(iters - i):
|
||||||
echo "got ", x, ", but expected ", iters - i
|
echo i, "th iteration; got ", x, ", but expected ", iters - i
|
||||||
echo b2.n
|
echo b2.n, " = ", b1.n
|
||||||
echo b2.height
|
echo b2.height, " = ", b1.height
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue