btrees.nim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. var x = b.root
  35. while x.isInternal:
  36. for j in 0..<x.entries:
  37. if j+1 == x.entries or less(key, x.keys[j+1]):
  38. x = x.links[j]
  39. break
  40. assert(not x.isInternal)
  41. for j in 0..<x.entries:
  42. if eq(key, x.keys[j]): return x.vals[j]
  43. proc contains*[Key, Val](b: BTree[Key, Val], key: Key): bool =
  44. var x = b.root
  45. while x.isInternal:
  46. for j in 0..<x.entries:
  47. if j+1 == x.entries or less(key, x.keys[j+1]):
  48. x = x.links[j]
  49. break
  50. assert(not x.isInternal)
  51. for j in 0..<x.entries:
  52. if eq(key, x.keys[j]): return true
  53. return false
  54. proc copyHalf[Key, Val](h, result: Node[Key, Val]) =
  55. for j in 0..<Mhalf:
  56. result.keys[j] = h.keys[Mhalf + j]
  57. if h.isInternal:
  58. for j in 0..<Mhalf:
  59. result.links[j] = h.links[Mhalf + j]
  60. else:
  61. for j in 0..<Mhalf:
  62. when defined(gcArc) or defined(gcOrc):
  63. result.vals[j] = move h.vals[Mhalf + j]
  64. else:
  65. shallowCopy(result.vals[j], h.vals[Mhalf + j])
  66. proc split[Key, Val](h: Node[Key, Val]): Node[Key, Val] =
  67. ## split node in half
  68. result = Node[Key, Val](entries: Mhalf, isInternal: h.isInternal)
  69. h.entries = Mhalf
  70. copyHalf(h, result)
  71. proc insert[Key, Val](h: Node[Key, Val], key: Key, val: Val): Node[Key, Val] =
  72. #var t = Entry(key: key, val: val, next: nil)
  73. var newKey = key
  74. var j = 0
  75. if not h.isInternal:
  76. while j < h.entries:
  77. if eq(key, h.keys[j]):
  78. h.vals[j] = val
  79. return
  80. if less(key, h.keys[j]): break
  81. inc j
  82. for i in countdown(h.entries, j+1):
  83. when defined(gcArc) or defined(gcOrc):
  84. h.vals[i] = move h.vals[i-1]
  85. else:
  86. shallowCopy(h.vals[i], h.vals[i-1])
  87. h.vals[j] = val
  88. else:
  89. var newLink: Node[Key, Val] = nil
  90. while j < h.entries:
  91. if j+1 == h.entries or less(key, h.keys[j+1]):
  92. let u = insert(h.links[j], key, val)
  93. inc j
  94. if u == nil: return nil
  95. newKey = u.keys[0]
  96. newLink = u
  97. break
  98. inc j
  99. for i in countdown(h.entries, j+1):
  100. h.links[i] = h.links[i-1]
  101. h.links[j] = newLink
  102. for i in countdown(h.entries, j+1):
  103. h.keys[i] = h.keys[i-1]
  104. h.keys[j] = newKey
  105. inc h.entries
  106. return if h.entries < M: nil else: split(h)
  107. proc add*[Key, Val](b: var BTree[Key, Val]; key: Key; val: Val) =
  108. let u = insert(b.root, key, val)
  109. inc b.entries
  110. if u == nil: return
  111. # need to split root
  112. let t = Node[Key, Val](entries: 2, isInternal: true)
  113. t.keys[0] = b.root.keys[0]
  114. t.links[0] = b.root
  115. t.keys[1] = u.keys[0]
  116. t.links[1] = u
  117. b.root = t
  118. proc toString[Key, Val](h: Node[Key, Val], indent: string; result: var string) =
  119. if not h.isInternal:
  120. for j in 0..<h.entries:
  121. result.add(indent)
  122. result.add($h.keys[j] & " " & $h.vals[j] & "\n")
  123. else:
  124. for j in 0..<h.entries:
  125. if j > 0: result.add(indent & "(" & $h.keys[j] & ")\n")
  126. toString(h.links[j], indent & " ", result)
  127. proc `$`[Key, Val](b: BTree[Key, Val]): string =
  128. result = ""
  129. toString(b.root, "", result)
  130. proc hasNext*[Key, Val](b: BTree[Key, Val]; index: int): bool = index < b.entries
  131. proc countSubTree[Key, Val](it: Node[Key, Val]): int =
  132. if it.isInternal:
  133. result = 0
  134. for k in 0..<it.entries:
  135. inc result, countSubTree(it.links[k])
  136. else:
  137. result = it.entries
  138. proc next*[Key, Val](b: BTree[Key, Val]; index: int): (Key, Val, int) =
  139. var it = b.root
  140. var i = index
  141. # navigate to the right leaf:
  142. while it.isInternal:
  143. var sum = 0
  144. for k in 0..<it.entries:
  145. let c = countSubTree(it.links[k])
  146. inc sum, c
  147. if sum > i:
  148. it = it.links[k]
  149. dec i, (sum - c)
  150. break
  151. result = (it.keys[i], it.vals[i], index+1)
  152. iterator pairs*[Key, Val](b: BTree[Key, Val]): (Key, Val) =
  153. var i = 0
  154. while hasNext(b, i):
  155. let (k, v, i2) = next(b, i)
  156. i = i2
  157. yield (k, v)
  158. proc len*[Key, Val](b: BTree[Key, Val]): int {.inline.} = b.entries