node32.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Package numtree implements radix tree data structure for 32 and 64-bit unsigned integets. Copy-on-write is used for any tree modification so old root doesn't see any change happens with tree.
  2. package numtree
  3. import (
  4. "fmt"
  5. "math/bits"
  6. )
  7. // Key32BitSize is an alias for bitsize of 32-bit radix tree's key.
  8. const Key32BitSize = 32
  9. var (
  10. masks32 = []uint32{
  11. 0x00000000, 0x80000000, 0xc0000000, 0xe0000000,
  12. 0xf0000000, 0xf8000000, 0xfc000000, 0xfe000000,
  13. 0xff000000, 0xff800000, 0xffc00000, 0xffe00000,
  14. 0xfff00000, 0xfff80000, 0xfffc0000, 0xfffe0000,
  15. 0xffff0000, 0xffff8000, 0xffffc000, 0xffffe000,
  16. 0xfffff000, 0xfffff800, 0xfffffc00, 0xfffffe00,
  17. 0xffffff00, 0xffffff80, 0xffffffc0, 0xffffffe0,
  18. 0xfffffff0, 0xfffffff8, 0xfffffffc, 0xfffffffe,
  19. 0xffffffff}
  20. )
  21. // Node32 is an element of radix tree with 32-bit unsigned integer as a key.
  22. type Node32 struct {
  23. // Key stores key for current node.
  24. Key uint32
  25. // Bits is a number of significant bits in Key.
  26. Bits uint8
  27. // Leaf indicates if the node is leaf node and contains any data in Value.
  28. Leaf bool
  29. // Value contains data associated with key.
  30. Value interface{}
  31. chld [2]*Node32
  32. }
  33. // Dot dumps tree to Graphviz .dot format
  34. func (n *Node32) Dot() string {
  35. body := ""
  36. // Iterate all nodes using breadth-first search algorithm.
  37. i := 0
  38. queue := []*Node32{n}
  39. for len(queue) > 0 {
  40. c := queue[0]
  41. body += fmt.Sprintf("N%d %s\n", i, c.dotString())
  42. if c != nil && (c.chld[0] != nil || c.chld[1] != nil) {
  43. // Children for current node if any always go to the end of the queue
  44. // so we can know their indices using current queue length.
  45. body += fmt.Sprintf("N%d -> { N%d N%d }\n", i, i+len(queue), i+len(queue)+1)
  46. queue = append(append(queue, c.chld[0]), c.chld[1])
  47. }
  48. queue = queue[1:]
  49. i++
  50. }
  51. return "digraph d {\n" + body + "}\n"
  52. }
  53. // Insert puts new leaf to radix tree and returns pointer to new root. The method uses copy on write strategy so old root doesn't see the change.
  54. func (n *Node32) Insert(key uint32, bits int, value interface{}) *Node32 {
  55. // Adjust bits.
  56. if bits < 0 {
  57. bits = 0
  58. } else if bits > Key32BitSize {
  59. bits = Key32BitSize
  60. }
  61. return n.insert(newNode32(key, uint8(bits), true, value))
  62. }
  63. // InplaceInsert puts new leaf to radix tree (or replaces value in existing one). The method inserts data directly to current tree so make sure you have exclusive access to it.
  64. func (n *Node32) InplaceInsert(key uint32, bits int, value interface{}) *Node32 {
  65. // Adjust bits.
  66. if bits < 0 {
  67. bits = 0
  68. } else if bits > Key32BitSize {
  69. bits = Key32BitSize
  70. }
  71. return n.inplaceInsert(key, uint8(bits), value)
  72. }
  73. // Enumerate returns channel which is populated by nodes with data in order of their keys.
  74. func (n *Node32) Enumerate() chan *Node32 {
  75. ch := make(chan *Node32)
  76. go func() {
  77. defer close(ch)
  78. // If tree is empty -
  79. if n == nil {
  80. // return nothing.
  81. return
  82. }
  83. n.enumerate(ch)
  84. }()
  85. return ch
  86. }
  87. // Match locates node which key is equal to or "contains" the key passed as argument.
  88. func (n *Node32) Match(key uint32, bits int) (interface{}, bool) {
  89. // If tree is empty -
  90. if n == nil {
  91. // report nothing.
  92. return n, false
  93. }
  94. // Adjust bits.
  95. if bits < 0 {
  96. bits = 0
  97. } else if bits > Key32BitSize {
  98. bits = Key32BitSize
  99. }
  100. r := n.match(key, uint8(bits))
  101. if r == nil {
  102. return nil, false
  103. }
  104. return r.Value, true
  105. }
  106. // ExactMatch locates node which exactly matches given key.
  107. func (n *Node32) ExactMatch(key uint32, bits int) (interface{}, bool) {
  108. // If tree is empty -
  109. if n == nil {
  110. // report nothing.
  111. return n, false
  112. }
  113. // Adjust bits.
  114. if bits < 0 {
  115. bits = 0
  116. } else if bits > Key32BitSize {
  117. bits = Key32BitSize
  118. }
  119. r := n.exactMatch(key, uint8(bits))
  120. if r == nil {
  121. return nil, false
  122. }
  123. return r.Value, true
  124. }
  125. // Delete removes subtree which is contained by given key. The method uses copy on write strategy.
  126. func (n *Node32) Delete(key uint32, bits int) (*Node32, bool) {
  127. // If tree is empty -
  128. if n == nil {
  129. // report nothing.
  130. return n, false
  131. }
  132. // Adjust bits.
  133. if bits < 0 {
  134. bits = 0
  135. } else if bits > Key32BitSize {
  136. bits = Key32BitSize
  137. }
  138. return n.del(key, uint8(bits))
  139. }
  140. func (n *Node32) dotString() string {
  141. if n == nil {
  142. return "[label=\"nil\"]"
  143. }
  144. if n.Leaf {
  145. v := fmt.Sprintf("%q", fmt.Sprintf("%#v", n.Value))
  146. return fmt.Sprintf("[label=\"k: %08x, b: %d, v: \\\"%s\\\"\"]", n.Key, n.Bits, v[1:len(v)-1])
  147. }
  148. return fmt.Sprintf("[label=\"k: %08x, b: %d\"]", n.Key, n.Bits)
  149. }
  150. func (n *Node32) insert(c *Node32) *Node32 {
  151. if n == nil {
  152. return c
  153. }
  154. // Find number of common most significant bits (NCSB):
  155. // 1. xor operation puts zeroes at common bits;
  156. // 2. or masks put ones so that zeroes can't go after smaller number of significant bits (NSB)
  157. // 3. count of leading zeroes gives number of common bits
  158. bits := uint8(bits.LeadingZeros32((n.Key ^ c.Key) | ^masks32[n.Bits] | ^masks32[c.Bits]))
  159. // There are three cases possible:
  160. // - NCSB less than number of significant bits (NSB) of current tree node:
  161. if bits < n.Bits {
  162. // (branch for current tree node is determined by a bit right after the last common bit)
  163. branch := (n.Key >> (Key32BitSize - 1 - bits)) & 1
  164. // - NCSB equals to NSB of candidate node:
  165. if bits == c.Bits {
  166. // make new root from the candidate and put current node to one of its branch;
  167. c.chld[branch] = n
  168. return c
  169. }
  170. // - NCSB less than NSB of candidate node (it can't be greater because bits after NSB don't count):
  171. // make new root (non-leaf node)
  172. m := newNode32(c.Key&masks32[bits], bits, false, nil)
  173. // with current tree node at one of branches
  174. m.chld[branch] = n
  175. // and the candidate at the other.
  176. m.chld[1-branch] = c
  177. return m
  178. }
  179. // - keys are equal (NCSB not less than NSB of current tree node and both numbers are equal):
  180. if c.Bits == n.Bits {
  181. // replace current node with the candidate.
  182. c.chld = n.chld
  183. return c
  184. }
  185. // - current tree node contains candidate node:
  186. // make new root as a copy of current tree node;
  187. m := newNode32(n.Key, n.Bits, n.Leaf, n.Value)
  188. m.chld = n.chld
  189. // (branch for the candidate is determined by a bit right after the last common bit)
  190. branch := (c.Key >> (Key32BitSize - 1 - bits)) & 1
  191. // insert it to correct branch.
  192. m.chld[branch] = m.chld[branch].insert(c)
  193. return m
  194. }
  195. func (n *Node32) inplaceInsert(key uint32, sbits uint8, value interface{}) *Node32 {
  196. var (
  197. p *Node32
  198. branch uint32
  199. )
  200. r := n
  201. for n != nil {
  202. cbits := uint8(bits.LeadingZeros32((n.Key ^ key) | ^masks32[n.Bits] | ^masks32[sbits]))
  203. if cbits < n.Bits {
  204. pBranch := branch
  205. branch = (n.Key >> (Key32BitSize - 1 - cbits)) & 1
  206. var m *Node32
  207. if cbits == sbits {
  208. m = newNode32(key, sbits, true, value)
  209. m.chld[branch] = n
  210. } else {
  211. m = newNode32(key&masks32[cbits], cbits, false, nil)
  212. m.chld[1-branch] = newNode32(key, sbits, true, value)
  213. }
  214. m.chld[branch] = n
  215. if p == nil {
  216. r = m
  217. } else {
  218. p.chld[pBranch] = m
  219. }
  220. return r
  221. }
  222. if sbits == n.Bits {
  223. n.Key = key
  224. n.Leaf = true
  225. n.Value = value
  226. return r
  227. }
  228. p = n
  229. branch = (key >> (Key32BitSize - 1 - cbits)) & 1
  230. n = n.chld[branch]
  231. }
  232. n = newNode32(key, sbits, true, value)
  233. if p == nil {
  234. return n
  235. }
  236. p.chld[branch] = n
  237. return r
  238. }
  239. func (n *Node32) enumerate(ch chan *Node32) {
  240. // Implemented by depth-first search.
  241. if n.Leaf {
  242. ch <- n
  243. }
  244. if n.chld[0] != nil {
  245. n.chld[0].enumerate(ch)
  246. }
  247. if n.chld[1] != nil {
  248. n.chld[1].enumerate(ch)
  249. }
  250. }
  251. func (n *Node32) match(key uint32, bits uint8) *Node32 {
  252. // If can't be contained in current root node -
  253. if n.Bits > bits {
  254. // report nothing.
  255. return nil
  256. }
  257. // If NSB of current tree node is the same as key has -
  258. if n.Bits == bits {
  259. // return current node only if it contains data (leaf node) and masked keys are equal.
  260. if n.Leaf && (n.Key^key)&masks32[n.Bits] == 0 {
  261. return n
  262. }
  263. return nil
  264. }
  265. // If key can be contained by current tree node -
  266. if (n.Key^key)&masks32[n.Bits] != 0 {
  267. // but it isn't report nothing.
  268. return nil
  269. }
  270. // Otherwise jump to branch by key bit right after NSB of current tree node
  271. c := n.chld[(key>>(Key32BitSize-1-n.Bits))&1]
  272. if c != nil {
  273. // and check if child on the branch has anything.
  274. r := c.match(key, bits)
  275. if r != nil {
  276. return r
  277. }
  278. }
  279. // If nothing matches check if current node contains any data.
  280. if n.Leaf {
  281. return n
  282. }
  283. return nil
  284. }
  285. func (n *Node32) exactMatch(key uint32, bits uint8) *Node32 {
  286. // If can't be contained in current root node -
  287. if n.Bits > bits {
  288. // report nothing.
  289. return nil
  290. }
  291. // If NSB of current tree node is the same as key has -
  292. if n.Bits == bits {
  293. // return current node only if it contains data (leaf node) and masked keys are equal.
  294. if n.Leaf && (n.Key^key)&masks32[n.Bits] == 0 {
  295. return n
  296. }
  297. return nil
  298. }
  299. // If key can be contained by current tree node -
  300. if (n.Key^key)&masks32[n.Bits] != 0 {
  301. // but it isn't report nothing.
  302. return nil
  303. }
  304. // Otherwise jump to branch by key bit right after NSB of current tree node
  305. c := n.chld[(key>>(Key32BitSize-1-n.Bits))&1]
  306. if c != nil {
  307. // and check if child on the branch has anything.
  308. r := c.exactMatch(key, bits)
  309. if r != nil {
  310. return r
  311. }
  312. }
  313. return nil
  314. }
  315. func (n *Node32) del(key uint32, bits uint8) (*Node32, bool) {
  316. // If key can contain current tree node -
  317. if bits <= n.Bits {
  318. // report empty new tree and put deletion mark if it contains indeed.
  319. if (n.Key^key)&masks32[bits] == 0 {
  320. return nil, true
  321. }
  322. return n, false
  323. }
  324. // If key can be contained by current tree node -
  325. if (n.Key^key)&masks32[n.Bits] != 0 {
  326. // but it isn't report nothing.
  327. return n, false
  328. }
  329. // Otherwise jump to branch by key bit right after NSB of current tree node
  330. branch := (key >> (Key32BitSize - 1 - n.Bits)) & 1
  331. c := n.chld[branch]
  332. if c == nil {
  333. // report nothing if the branch is empty.
  334. return n, false
  335. }
  336. // Try to remove from subtree
  337. c, ok := c.del(key, bits)
  338. if !ok {
  339. // and report nothing if nothing has been deleted.
  340. return n, false
  341. }
  342. // If child of non-leaf node has been completely deleted -
  343. if c == nil && !n.Leaf {
  344. // drop the node.
  345. return n.chld[1-branch], true
  346. }
  347. // If deletion happens inside the branch then copy current node.
  348. m := newNode32(n.Key, n.Bits, n.Leaf, n.Value)
  349. m.chld = n.chld
  350. // Replace changed child with new one and return new root with deletion mark set.
  351. m.chld[branch] = c
  352. return m, true
  353. }
  354. func newNode32(key uint32, bits uint8, leaf bool, value interface{}) *Node32 {
  355. return &Node32{
  356. Key: key,
  357. Bits: bits,
  358. Leaf: leaf,
  359. Value: value}
  360. }