btrees.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## BTree implementation with few features, but good enough for the
  10. ## Nim compiler's needs.
  11. when defined(nimPreviewSlimSystem):
  12. import std/assertions
  13. const
  14. M = 512 # max children per B-tree node = M-1
  15. # (must be even and greater than 2)
  16. Mhalf = M div 2
  17. type
  18. Node[Key, Val] {.acyclic.} = ref object
  19. entries: int
  20. keys: array[M, Key]
  21. case isInternal: bool
  22. of false:
  23. vals: array[M, Val]
  24. of true:
  25. links: array[M, Node[Key, Val]]
  26. BTree*[Key, Val] = object
  27. root: Node[Key, Val]
  28. entries: int ## number of key-value pairs
  29. proc initBTree*[Key, Val](): BTree[Key, Val] =
  30. BTree[Key, Val](root: Node[Key, Val](entries: 0, isInternal: false))
  31. template less(a, b): bool = cmp(a, b) < 0
  32. template eq(a, b): bool = cmp(a, b) == 0
  33. proc getOrDefault*[Key, Val](b: BTree[Key, Val], key: Key): Val =
  34. result = default(Val)
  35. var x = b.root
  36. while x.isInternal:
  37. for j in 0..<x.entries:
  38. if j+1 == x.entries or less(key, x.keys[j+1]):
  39. x = x.links[j]
  40. break
  41. assert(not x.isInternal)
  42. for j in 0..<x.entries:
  43. if eq(key, x.keys[j]): return x.vals[j]
  44. proc contains*[Key, Val](b: BTree[Key, Val], key: Key): bool =
  45. var x = b.root
  46. while x.isInternal:
  47. for j in 0..<x.entries:
  48. if j+1 == x.entries or less(key, x.keys[j+1]):
  49. x = x.links[j]
  50. break
  51. assert(not x.isInternal)
  52. for j in 0..<x.entries:
  53. if eq(key, x.keys[j]): return true
  54. return false
  55. proc copyHalf[Key, Val](h, result: Node[Key, Val]) =
  56. for j in 0..<Mhalf:
  57. result.keys[j] = h.keys[Mhalf + j]
  58. if h.isInternal:
  59. for j in 0..<Mhalf:
  60. result.links[j] = h.links[Mhalf + j]
  61. else:
  62. for j in 0..<Mhalf:
  63. when defined(gcArc) or defined(gcOrc) or defined(gcAtomicArc):
  64. result.vals[j] = move h.vals[Mhalf + j]
  65. else:
  66. shallowCopy(result.vals[j], h.vals[Mhalf + j])
  67. proc split[Key, Val](h: Node[Key, Val]): Node[Key, Val] =
  68. ## split node in half
  69. result = Node[Key, Val](entries: Mhalf, isInternal: h.isInternal)
  70. h.entries = Mhalf
  71. copyHalf(h, result)
  72. proc insert[Key, Val](h: Node[Key, Val], key: Key, val: Val): Node[Key, Val] =
  73. #var t = Entry(key: key, val: val, next: nil)
  74. var newKey = key
  75. var j = 0
  76. if not h.isInternal:
  77. while j < h.entries:
  78. if eq(key, h.keys[j]):
  79. h.vals[j] = val
  80. return
  81. if less(key, h.keys[j]): break
  82. inc j
  83. for i in countdown(h.entries, j+1):
  84. when defined(gcArc) or defined(gcOrc) or defined(gcAtomicArc):
  85. h.vals[i] = move h.vals[i-1]
  86. else:
  87. shallowCopy(h.vals[i], h.vals[i-1])
  88. h.vals[j] = val
  89. else:
  90. var newLink: Node[Key, Val] = nil
  91. while j < h.entries:
  92. if j+1 == h.entries or less(key, h.keys[j+1]):
  93. let u = insert(h.links[j], key, val)
  94. inc j
  95. if u == nil: return nil
  96. newKey = u.keys[0]
  97. newLink = u
  98. break
  99. inc j
  100. for i in countdown(h.entries, j+1):
  101. h.links[i] = h.links[i-1]
  102. h.links[j] = newLink
  103. for i in countdown(h.entries, j+1):
  104. h.keys[i] = h.keys[i-1]
  105. h.keys[j] = newKey
  106. inc h.entries
  107. return if h.entries < M: nil else: split(h)
  108. proc add*[Key, Val](b: var BTree[Key, Val]; key: Key; val: Val) =
  109. let u = insert(b.root, key, val)
  110. inc b.entries
  111. if u == nil: return
  112. # need to split root
  113. let t = Node[Key, Val](entries: 2, isInternal: true)
  114. t.keys[0] = b.root.keys[0]
  115. t.links[0] = b.root
  116. t.keys[1] = u.keys[0]
  117. t.links[1] = u
  118. b.root = t
  119. proc toString[Key, Val](h: Node[Key, Val], indent: string; result: var string) =
  120. if not h.isInternal:
  121. for j in 0..<h.entries:
  122. result.add(indent)
  123. result.add($h.keys[j] & " " & $h.vals[j] & "\n")
  124. else:
  125. for j in 0..<h.entries:
  126. if j > 0: result.add(indent & "(" & $h.keys[j] & ")\n")
  127. toString(h.links[j], indent & " ", result)
  128. proc `$`[Key, Val](b: BTree[Key, Val]): string =
  129. result = ""
  130. toString(b.root, "", result)
  131. proc hasNext*[Key, Val](b: BTree[Key, Val]; index: int): bool = index < b.entries
  132. proc countSubTree[Key, Val](it: Node[Key, Val]): int =
  133. if it.isInternal:
  134. result = 0
  135. for k in 0..<it.entries:
  136. inc result, countSubTree(it.links[k])
  137. else:
  138. result = it.entries
  139. proc next*[Key, Val](b: BTree[Key, Val]; index: int): (Key, Val, int) =
  140. var it = b.root
  141. var i = index
  142. # navigate to the right leaf:
  143. while it.isInternal:
  144. var sum = 0
  145. for k in 0..<it.entries:
  146. let c = countSubTree(it.links[k])
  147. inc sum, c
  148. if sum > i:
  149. it = it.links[k]
  150. dec i, (sum - c)
  151. break
  152. result = (it.keys[i], it.vals[i], index+1)
  153. iterator pairs*[Key, Val](b: BTree[Key, Val]): (Key, Val) =
  154. var i = 0
  155. while hasNext(b, i):
  156. let (k, v, i2) = next(b, i)
  157. i = i2
  158. yield (k, v)
  159. proc len*[Key, Val](b: BTree[Key, Val]): int {.inline.} = b.entries