critbits.nim 12 KB

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