critbits.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a `crit bit tree`:idx: which is an efficient
  10. ## container for a sorted set of strings, or for a sorted mapping of strings. Based on the excellent paper
  11. ## by Adam Langley.
  12. ## (A crit bit tree is a form of `radix tree`:idx: or `patricia trie`:idx:.)
  13. include "system/inclrtl"
  14. type
  15. NodeObj[T] = object {.acyclic.}
  16. byte: int ## byte index of the difference
  17. otherbits: char
  18. case isLeaf: bool
  19. of false: child: array[0..1, ref NodeObj[T]]
  20. of true:
  21. key: string
  22. when T isnot void:
  23. val: T
  24. Node[T] = ref NodeObj[T]
  25. CritBitTree*[T] = object ## The crit bit tree can either be used
  26. ## as a mapping from strings to
  27. ## some type ``T`` or as a set of
  28. ## strings if ``T`` is void.
  29. root: Node[T]
  30. count: int
  31. proc len*[T](c: CritBitTree[T]): int =
  32. ## returns the number of elements in `c` in O(1).
  33. result = c.count
  34. proc rawGet[T](c: CritBitTree[T], key: string): Node[T] =
  35. var it = c.root
  36. while it != nil:
  37. if not it.isLeaf:
  38. let ch = if it.byte < key.len: key[it.byte] else: '\0'
  39. let dir = (1 + (ch.ord or it.otherBits.ord)) shr 8
  40. it = it.child[dir]
  41. else:
  42. return if it.key == key: it else: nil
  43. proc contains*[T](c: CritBitTree[T], key: string): bool {.inline.} =
  44. ## returns true iff `c` contains the given `key`.
  45. result = rawGet(c, key) != nil
  46. proc hasKey*[T](c: CritBitTree[T], key: string): bool {.inline.} =
  47. ## alias for `contains`.
  48. result = rawGet(c, key) != nil
  49. proc rawInsert[T](c: var CritBitTree[T], key: string): Node[T] =
  50. if c.root == nil:
  51. new c.root
  52. c.root.isleaf = true
  53. c.root.key = key
  54. result = c.root
  55. else:
  56. var it = c.root
  57. while not it.isLeaf:
  58. let ch = if it.byte < key.len: key[it.byte] else: '\0'
  59. let dir = (1 + (ch.ord or it.otherBits.ord)) shr 8
  60. it = it.child[dir]
  61. var newOtherBits = 0
  62. var newByte = 0
  63. block blockX:
  64. while newbyte < key.len:
  65. let ch = if newbyte < it.key.len: it.key[newbyte] else: '\0'
  66. if ch != key[newbyte]:
  67. newotherbits = ch.ord xor key[newbyte].ord
  68. break blockX
  69. inc newbyte
  70. if newbyte < it.key.len:
  71. newotherbits = it.key[newbyte].ord
  72. else:
  73. return it
  74. while (newOtherBits and (newOtherBits-1)) != 0:
  75. newOtherBits = newOtherBits and (newOtherBits-1)
  76. newOtherBits = newOtherBits xor 255
  77. let ch = if newByte < it.key.len: it.key[newByte] else: '\0'
  78. let dir = (1 + (ord(ch) or newOtherBits)) shr 8
  79. var inner: Node[T]
  80. new inner
  81. new result
  82. result.isLeaf = true
  83. result.key = key
  84. inner.otherBits = chr(newOtherBits)
  85. inner.byte = newByte
  86. inner.child[1 - dir] = result
  87. var wherep = addr(c.root)
  88. while true:
  89. var p = wherep[]
  90. if p.isLeaf: break
  91. if p.byte > newByte: break
  92. if p.byte == newByte and p.otherBits.ord > newOtherBits: break
  93. let ch = if p.byte < key.len: key[p.byte] else: '\0'
  94. let dir = (1 + (ch.ord or p.otherBits.ord)) shr 8
  95. wherep = addr(p.child[dir])
  96. inner.child[dir] = wherep[]
  97. wherep[] = inner
  98. inc c.count
  99. proc exclImpl[T](c: var CritBitTree[T], key: string) : int =
  100. var p = c.root
  101. var wherep = addr(c.root)
  102. var whereq: ptr Node[T] = nil
  103. if p == nil: return c.count
  104. var dir = 0
  105. var q: Node[T]
  106. while not p.isLeaf:
  107. whereq = wherep
  108. q = p
  109. let ch = if p.byte < key.len: key[p.byte] else: '\0'
  110. dir = (1 + (ch.ord or p.otherBits.ord)) shr 8
  111. wherep = addr(p.child[dir])
  112. p = wherep[]
  113. if p.key == key:
  114. # else: not in tree at all
  115. if whereq == nil:
  116. c.root = nil
  117. else:
  118. whereq[] = q.child[1 - dir]
  119. dec c.count
  120. return c.count
  121. proc excl*[T](c: var CritBitTree[T], key: string) =
  122. ## removes `key` (and its associated value) from the set `c`.
  123. ## If the `key` does not exist, nothing happens.
  124. discard exclImpl(c, key)
  125. proc missingOrExcl*[T](c: var CritBitTree[T], key: string): bool =
  126. ## Returns true iff `c` does not contain the given `key`. If the key
  127. ## does exist, c.excl(key) is performed.
  128. let oldCount = c.count
  129. var n = exclImpl(c, key)
  130. result = c.count == oldCount
  131. proc containsOrIncl*[T](c: var CritBitTree[T], key: string, val: T): bool =
  132. ## returns true iff `c` contains the given `key`. If the key does not exist
  133. ## ``c[key] = val`` is performed.
  134. let oldCount = c.count
  135. var n = rawInsert(c, key)
  136. result = c.count == oldCount
  137. when T isnot void:
  138. if not result: n.val = val
  139. proc containsOrIncl*(c: var CritBitTree[void], key: string): bool =
  140. ## returns true iff `c` contains the given `key`. If the key does not exist
  141. ## it is inserted into `c`.
  142. let oldCount = c.count
  143. var n = rawInsert(c, key)
  144. result = c.count == oldCount
  145. proc inc*(c: var CritBitTree[int]; key: string, val: int = 1) =
  146. ## increments `c[key]` by `val`.
  147. var n = rawInsert(c, key)
  148. inc n.val, val
  149. proc incl*(c: var CritBitTree[void], key: string) =
  150. ## includes `key` in `c`.
  151. discard rawInsert(c, key)
  152. proc incl*[T](c: var CritBitTree[T], key: string, val: T) =
  153. ## inserts `key` with value `val` into `c`.
  154. var n = rawInsert(c, key)
  155. n.val = val
  156. proc `[]=`*[T](c: var CritBitTree[T], key: string, val: T) =
  157. ## puts a (key, value)-pair into `t`.
  158. var n = rawInsert(c, key)
  159. n.val = val
  160. template get[T](c: CritBitTree[T], key: string): T =
  161. let n = rawGet(c, key)
  162. if n == nil:
  163. when compiles($key):
  164. raise newException(KeyError, "key not found: " & $key)
  165. else:
  166. raise newException(KeyError, "key not found")
  167. n.val
  168. proc `[]`*[T](c: CritBitTree[T], key: string): T {.inline, deprecatedGet.} =
  169. ## retrieves the value at ``c[key]``. If `key` is not in `t`, the
  170. ## ``KeyError`` exception is raised. One can check with ``hasKey`` whether
  171. ## the key exists.
  172. get(c, key)
  173. proc `[]`*[T](c: var CritBitTree[T], key: string): var T {.inline,
  174. deprecatedGet.} =
  175. ## retrieves the value at ``c[key]``. The value can be modified.
  176. ## If `key` is not in `t`, the ``KeyError`` exception is raised.
  177. get(c, key)
  178. proc mget*[T](c: var CritBitTree[T], key: string): var T {.inline, deprecated.} =
  179. ## retrieves the value at ``c[key]``. The value can be modified.
  180. ## If `key` is not in `t`, the ``KeyError`` exception is raised.
  181. ## Use ```[]``` instead.
  182. get(c, key)
  183. iterator leaves[T](n: Node[T]): Node[T] =
  184. if n != nil:
  185. # XXX actually we could compute the necessary stack size in advance:
  186. # it's roughly log2(c.count).
  187. var stack = @[n]
  188. while stack.len > 0:
  189. var it = stack.pop
  190. while not it.isLeaf:
  191. stack.add(it.child[1])
  192. it = it.child[0]
  193. assert(it != nil)
  194. yield it
  195. iterator keys*[T](c: CritBitTree[T]): string =
  196. ## yields all keys in lexicographical order.
  197. for x in leaves(c.root): yield x.key
  198. iterator values*[T](c: CritBitTree[T]): T =
  199. ## yields all values of `c` in the lexicographical order of the
  200. ## corresponding keys.
  201. for x in leaves(c.root): yield x.val
  202. iterator mvalues*[T](c: var CritBitTree[T]): var T =
  203. ## yields all values of `c` in the lexicographical order of the
  204. ## corresponding keys. The values can be modified.
  205. for x in leaves(c.root): yield x.val
  206. iterator items*[T](c: CritBitTree[T]): string =
  207. ## yields all keys in lexicographical order.
  208. for x in leaves(c.root): yield x.key
  209. iterator pairs*[T](c: CritBitTree[T]): tuple[key: string, val: T] =
  210. ## yields all (key, value)-pairs of `c`.
  211. for x in leaves(c.root): yield (x.key, x.val)
  212. iterator mpairs*[T](c: var CritBitTree[T]): tuple[key: string, val: var T] =
  213. ## yields all (key, value)-pairs of `c`. The yielded values can be modified.
  214. for x in leaves(c.root): yield (x.key, x.val)
  215. proc allprefixedAux[T](c: CritBitTree[T], key: string; longestMatch: bool): Node[T] =
  216. var p = c.root
  217. var top = p
  218. if p != nil:
  219. while not p.isLeaf:
  220. var q = p
  221. let ch = if p.byte < key.len: key[p.byte] else: '\0'
  222. let dir = (1 + (ch.ord or p.otherBits.ord)) shr 8
  223. p = p.child[dir]
  224. if q.byte < key.len: top = p
  225. if not longestMatch:
  226. for i in 0 ..< key.len:
  227. if p.key[i] != key[i]: return
  228. result = top
  229. iterator itemsWithPrefix*[T](c: CritBitTree[T], prefix: string;
  230. longestMatch=false): string =
  231. ## yields all keys starting with `prefix`. If `longestMatch` is true,
  232. ## the longest match is returned, it doesn't have to be a complete match then.
  233. let top = allprefixedAux(c, prefix, longestMatch)
  234. for x in leaves(top): yield x.key
  235. iterator keysWithPrefix*[T](c: CritBitTree[T], prefix: string;
  236. longestMatch=false): string =
  237. ## yields all keys starting with `prefix`.
  238. let top = allprefixedAux(c, prefix, longestMatch)
  239. for x in leaves(top): yield x.key
  240. iterator valuesWithPrefix*[T](c: CritBitTree[T], prefix: string;
  241. longestMatch=false): T =
  242. ## yields all values of `c` starting with `prefix` of the
  243. ## corresponding keys.
  244. let top = allprefixedAux(c, prefix, longestMatch)
  245. for x in leaves(top): yield x.val
  246. iterator mvaluesWithPrefix*[T](c: var CritBitTree[T], prefix: string;
  247. longestMatch=false): var T =
  248. ## yields all values of `c` starting with `prefix` of the
  249. ## corresponding keys. The values can be modified.
  250. let top = allprefixedAux(c, prefix, longestMatch)
  251. for x in leaves(top): yield x.val
  252. iterator pairsWithPrefix*[T](c: CritBitTree[T],
  253. prefix: string;
  254. longestMatch=false): tuple[key: string, val: T] =
  255. ## yields all (key, value)-pairs of `c` starting with `prefix`.
  256. let top = allprefixedAux(c, prefix, longestMatch)
  257. for x in leaves(top): yield (x.key, x.val)
  258. iterator mpairsWithPrefix*[T](c: var CritBitTree[T],
  259. prefix: string;
  260. longestMatch=false): tuple[key: string, val: var T] =
  261. ## yields all (key, value)-pairs of `c` starting with `prefix`.
  262. ## The yielded values can be modified.
  263. let top = allprefixedAux(c, prefix, longestMatch)
  264. for x in leaves(top): yield (x.key, x.val)
  265. proc `$`*[T](c: CritBitTree[T]): string =
  266. ## turns `c` into a string representation. Example outputs:
  267. ## ``{keyA: value, keyB: value}``, ``{:}``
  268. ## If `T` is void the outputs look like:
  269. ## ``{keyA, keyB}``, ``{}``.
  270. if c.len == 0:
  271. when T is void:
  272. result = "{}"
  273. else:
  274. result = "{:}"
  275. else:
  276. # an educated guess is better than nothing:
  277. when T is void:
  278. const avgItemLen = 8
  279. else:
  280. const avgItemLen = 16
  281. result = newStringOfCap(c.count * avgItemLen)
  282. result.add("{")
  283. when T is void:
  284. for key in keys(c):
  285. if result.len > 1: result.add(", ")
  286. result.addQuoted(key)
  287. else:
  288. for key, val in pairs(c):
  289. if result.len > 1: result.add(", ")
  290. result.addQuoted(key)
  291. result.add(": ")
  292. result.addQuoted(val)
  293. result.add("}")
  294. when isMainModule:
  295. import sequtils
  296. var r: CritBitTree[void]
  297. r.incl "abc"
  298. r.incl "xyz"
  299. r.incl "def"
  300. r.incl "definition"
  301. r.incl "prefix"
  302. r.incl "foo"
  303. doAssert r.contains"def"
  304. r.excl "def"
  305. assert r.missingOrExcl("foo") == false
  306. assert "foo" notin toSeq(r.items)
  307. assert r.missingOrExcl("foo") == true
  308. assert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
  309. assert toSeq(r.itemsWithPrefix("de")) == @["definition"]
  310. var c = CritBitTree[int]()
  311. c.inc("a")
  312. assert c["a"] == 1
  313. c.inc("a", 4)
  314. assert c["a"] == 5
  315. c.inc("a", -5)
  316. assert c["a"] == 0
  317. c.inc("b", 2)
  318. assert c["b"] == 2
  319. c.inc("c", 3)
  320. assert c["c"] == 3
  321. c.inc("a", 1)
  322. assert c["a"] == 1
  323. var cf = CritBitTree[float]()
  324. cf.incl("a", 1.0)
  325. assert cf["a"] == 1.0
  326. cf.incl("b", 2.0)
  327. assert cf["b"] == 2.0
  328. cf.incl("c", 3.0)
  329. assert cf["c"] == 3.0
  330. assert cf.len == 3
  331. cf.excl("c")
  332. assert cf.len == 2