patricia.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // Copyright (c) 2014 The go-patricia AUTHORS
  2. //
  3. // Use of this source code is governed by The MIT License
  4. // that can be found in the LICENSE file.
  5. package patricia
  6. import (
  7. "errors"
  8. )
  9. //------------------------------------------------------------------------------
  10. // Trie
  11. //------------------------------------------------------------------------------
  12. const (
  13. DefaultMaxPrefixPerNode = 10
  14. DefaultMaxChildrenPerSparseNode = 8
  15. )
  16. type (
  17. Prefix []byte
  18. Item interface{}
  19. VisitorFunc func(prefix Prefix, item Item) error
  20. )
  21. // Trie is a generic patricia trie that allows fast retrieval of items by prefix.
  22. // and other funky stuff.
  23. //
  24. // Trie is not thread-safe.
  25. type Trie struct {
  26. prefix Prefix
  27. item Item
  28. maxPrefixPerNode int
  29. maxChildrenPerSparseNode int
  30. children childList
  31. }
  32. // Public API ------------------------------------------------------------------
  33. type Option func(*Trie)
  34. // Trie constructor.
  35. func NewTrie(options ...Option) *Trie {
  36. trie := &Trie{}
  37. for _, opt := range options {
  38. opt(trie)
  39. }
  40. if trie.maxPrefixPerNode <= 0 {
  41. trie.maxPrefixPerNode = DefaultMaxPrefixPerNode
  42. }
  43. if trie.maxChildrenPerSparseNode <= 0 {
  44. trie.maxChildrenPerSparseNode = DefaultMaxChildrenPerSparseNode
  45. }
  46. trie.children = newSparseChildList(trie.maxChildrenPerSparseNode)
  47. return trie
  48. }
  49. func MaxPrefixPerNode(value int) Option {
  50. return func(trie *Trie) {
  51. trie.maxPrefixPerNode = value
  52. }
  53. }
  54. func MaxChildrenPerSparseNode(value int) Option {
  55. return func(trie *Trie) {
  56. trie.maxChildrenPerSparseNode = value
  57. }
  58. }
  59. // Item returns the item stored in the root of this trie.
  60. func (trie *Trie) Item() Item {
  61. return trie.item
  62. }
  63. // Insert inserts a new item into the trie using the given prefix. Insert does
  64. // not replace existing items. It returns false if an item was already in place.
  65. func (trie *Trie) Insert(key Prefix, item Item) (inserted bool) {
  66. return trie.put(key, item, false)
  67. }
  68. // Set works much like Insert, but it always sets the item, possibly replacing
  69. // the item previously inserted.
  70. func (trie *Trie) Set(key Prefix, item Item) {
  71. trie.put(key, item, true)
  72. }
  73. // Get returns the item located at key.
  74. //
  75. // This method is a bit dangerous, because Get can as well end up in an internal
  76. // node that is not really representing any user-defined value. So when nil is
  77. // a valid value being used, it is not possible to tell if the value was inserted
  78. // into the tree by the user or not. A possible workaround for this is not to use
  79. // nil interface as a valid value, even using zero value of any type is enough
  80. // to prevent this bad behaviour.
  81. func (trie *Trie) Get(key Prefix) (item Item) {
  82. _, node, found, leftover := trie.findSubtree(key)
  83. if !found || len(leftover) != 0 {
  84. return nil
  85. }
  86. return node.item
  87. }
  88. // Match returns what Get(prefix) != nil would return. The same warning as for
  89. // Get applies here as well.
  90. func (trie *Trie) Match(prefix Prefix) (matchedExactly bool) {
  91. return trie.Get(prefix) != nil
  92. }
  93. // MatchSubtree returns true when there is a subtree representing extensions
  94. // to key, that is if there are any keys in the tree which have key as prefix.
  95. func (trie *Trie) MatchSubtree(key Prefix) (matched bool) {
  96. _, _, matched, _ = trie.findSubtree(key)
  97. return
  98. }
  99. // Visit calls visitor on every node containing a non-nil item
  100. // in alphabetical order.
  101. //
  102. // If an error is returned from visitor, the function stops visiting the tree
  103. // and returns that error, unless it is a special error - SkipSubtree. In that
  104. // case Visit skips the subtree represented by the current node and continues
  105. // elsewhere.
  106. func (trie *Trie) Visit(visitor VisitorFunc) error {
  107. return trie.walk(nil, visitor)
  108. }
  109. // VisitSubtree works much like Visit, but it only visits nodes matching prefix.
  110. func (trie *Trie) VisitSubtree(prefix Prefix, visitor VisitorFunc) error {
  111. // Nil prefix not allowed.
  112. if prefix == nil {
  113. panic(ErrNilPrefix)
  114. }
  115. // Empty trie must be handled explicitly.
  116. if trie.prefix == nil {
  117. return nil
  118. }
  119. // Locate the relevant subtree.
  120. _, root, found, leftover := trie.findSubtree(prefix)
  121. if !found {
  122. return nil
  123. }
  124. prefix = append(prefix, leftover...)
  125. // Visit it.
  126. return root.walk(prefix, visitor)
  127. }
  128. // VisitPrefixes visits only nodes that represent prefixes of key.
  129. // To say the obvious, returning SkipSubtree from visitor makes no sense here.
  130. func (trie *Trie) VisitPrefixes(key Prefix, visitor VisitorFunc) error {
  131. // Nil key not allowed.
  132. if key == nil {
  133. panic(ErrNilPrefix)
  134. }
  135. // Empty trie must be handled explicitly.
  136. if trie.prefix == nil {
  137. return nil
  138. }
  139. // Walk the path matching key prefixes.
  140. node := trie
  141. prefix := key
  142. offset := 0
  143. for {
  144. // Compute what part of prefix matches.
  145. common := node.longestCommonPrefixLength(key)
  146. key = key[common:]
  147. offset += common
  148. // Partial match means that there is no subtree matching prefix.
  149. if common < len(node.prefix) {
  150. return nil
  151. }
  152. // Call the visitor.
  153. if item := node.item; item != nil {
  154. if err := visitor(prefix[:offset], item); err != nil {
  155. return err
  156. }
  157. }
  158. if len(key) == 0 {
  159. // This node represents key, we are finished.
  160. return nil
  161. }
  162. // There is some key suffix left, move to the children.
  163. child := node.children.next(key[0])
  164. if child == nil {
  165. // There is nowhere to continue, return.
  166. return nil
  167. }
  168. node = child
  169. }
  170. }
  171. // Delete deletes the item represented by the given prefix.
  172. //
  173. // True is returned if the matching node was found and deleted.
  174. func (trie *Trie) Delete(key Prefix) (deleted bool) {
  175. // Nil prefix not allowed.
  176. if key == nil {
  177. panic(ErrNilPrefix)
  178. }
  179. // Empty trie must be handled explicitly.
  180. if trie.prefix == nil {
  181. return false
  182. }
  183. // Find the relevant node.
  184. parent, node, _, leftover := trie.findSubtree(key)
  185. if len(leftover) != 0 {
  186. return false
  187. }
  188. // If the item is already set to nil, there is nothing to do.
  189. if node.item == nil {
  190. return false
  191. }
  192. // Delete the item.
  193. node.item = nil
  194. // Compact since that might be possible now.
  195. if compacted := node.compact(); compacted != node {
  196. if parent == nil {
  197. *node = *compacted
  198. } else {
  199. parent.children.replace(node.prefix[0], compacted)
  200. *parent = *parent.compact()
  201. }
  202. }
  203. return true
  204. }
  205. // DeleteSubtree finds the subtree exactly matching prefix and deletes it.
  206. //
  207. // True is returned if the subtree was found and deleted.
  208. func (trie *Trie) DeleteSubtree(prefix Prefix) (deleted bool) {
  209. // Nil prefix not allowed.
  210. if prefix == nil {
  211. panic(ErrNilPrefix)
  212. }
  213. // Empty trie must be handled explicitly.
  214. if trie.prefix == nil {
  215. return false
  216. }
  217. // Locate the relevant subtree.
  218. parent, root, found, _ := trie.findSubtree(prefix)
  219. if !found {
  220. return false
  221. }
  222. // If we are in the root of the trie, reset the trie.
  223. if parent == nil {
  224. root.prefix = nil
  225. root.children = newSparseChildList(trie.maxPrefixPerNode)
  226. return true
  227. }
  228. // Otherwise remove the root node from its parent.
  229. parent.children.remove(root)
  230. return true
  231. }
  232. // Internal helper methods -----------------------------------------------------
  233. func (trie *Trie) put(key Prefix, item Item, replace bool) (inserted bool) {
  234. // Nil prefix not allowed.
  235. if key == nil {
  236. panic(ErrNilPrefix)
  237. }
  238. var (
  239. common int
  240. node *Trie = trie
  241. child *Trie
  242. )
  243. if node.prefix == nil {
  244. if len(key) <= trie.maxPrefixPerNode {
  245. node.prefix = key
  246. goto InsertItem
  247. }
  248. node.prefix = key[:trie.maxPrefixPerNode]
  249. key = key[trie.maxPrefixPerNode:]
  250. goto AppendChild
  251. }
  252. for {
  253. // Compute the longest common prefix length.
  254. common = node.longestCommonPrefixLength(key)
  255. key = key[common:]
  256. // Only a part matches, split.
  257. if common < len(node.prefix) {
  258. goto SplitPrefix
  259. }
  260. // common == len(node.prefix) since never (common > len(node.prefix))
  261. // common == len(former key) <-> 0 == len(key)
  262. // -> former key == node.prefix
  263. if len(key) == 0 {
  264. goto InsertItem
  265. }
  266. // Check children for matching prefix.
  267. child = node.children.next(key[0])
  268. if child == nil {
  269. goto AppendChild
  270. }
  271. node = child
  272. }
  273. SplitPrefix:
  274. // Split the prefix if necessary.
  275. child = new(Trie)
  276. *child = *node
  277. *node = *NewTrie()
  278. node.prefix = child.prefix[:common]
  279. child.prefix = child.prefix[common:]
  280. child = child.compact()
  281. node.children = node.children.add(child)
  282. AppendChild:
  283. // Keep appending children until whole prefix is inserted.
  284. // This loop starts with empty node.prefix that needs to be filled.
  285. for len(key) != 0 {
  286. child := NewTrie()
  287. if len(key) <= trie.maxPrefixPerNode {
  288. child.prefix = key
  289. node.children = node.children.add(child)
  290. node = child
  291. goto InsertItem
  292. } else {
  293. child.prefix = key[:trie.maxPrefixPerNode]
  294. key = key[trie.maxPrefixPerNode:]
  295. node.children = node.children.add(child)
  296. node = child
  297. }
  298. }
  299. InsertItem:
  300. // Try to insert the item if possible.
  301. if replace || node.item == nil {
  302. node.item = item
  303. return true
  304. }
  305. return false
  306. }
  307. func (trie *Trie) compact() *Trie {
  308. // Only a node with a single child can be compacted.
  309. if trie.children.length() != 1 {
  310. return trie
  311. }
  312. child := trie.children.head()
  313. // If any item is set, we cannot compact since we want to retain
  314. // the ability to do searching by key. This makes compaction less usable,
  315. // but that simply cannot be avoided.
  316. if trie.item != nil || child.item != nil {
  317. return trie
  318. }
  319. // Make sure the combined prefixes fit into a single node.
  320. if len(trie.prefix)+len(child.prefix) > trie.maxPrefixPerNode {
  321. return trie
  322. }
  323. // Concatenate the prefixes, move the items.
  324. child.prefix = append(trie.prefix, child.prefix...)
  325. if trie.item != nil {
  326. child.item = trie.item
  327. }
  328. return child
  329. }
  330. func (trie *Trie) findSubtree(prefix Prefix) (parent *Trie, root *Trie, found bool, leftover Prefix) {
  331. // Find the subtree matching prefix.
  332. root = trie
  333. for {
  334. // Compute what part of prefix matches.
  335. common := root.longestCommonPrefixLength(prefix)
  336. prefix = prefix[common:]
  337. // We used up the whole prefix, subtree found.
  338. if len(prefix) == 0 {
  339. found = true
  340. leftover = root.prefix[common:]
  341. return
  342. }
  343. // Partial match means that there is no subtree matching prefix.
  344. if common < len(root.prefix) {
  345. leftover = root.prefix[common:]
  346. return
  347. }
  348. // There is some prefix left, move to the children.
  349. child := root.children.next(prefix[0])
  350. if child == nil {
  351. // There is nowhere to continue, there is no subtree matching prefix.
  352. return
  353. }
  354. parent = root
  355. root = child
  356. }
  357. }
  358. func (trie *Trie) walk(actualRootPrefix Prefix, visitor VisitorFunc) error {
  359. var prefix Prefix
  360. // Allocate a bit more space for prefix at the beginning.
  361. if actualRootPrefix == nil {
  362. prefix = make(Prefix, 32+len(trie.prefix))
  363. copy(prefix, trie.prefix)
  364. prefix = prefix[:len(trie.prefix)]
  365. } else {
  366. prefix = make(Prefix, 32+len(actualRootPrefix))
  367. copy(prefix, actualRootPrefix)
  368. prefix = prefix[:len(actualRootPrefix)]
  369. }
  370. // Visit the root first. Not that this works for empty trie as well since
  371. // in that case item == nil && len(children) == 0.
  372. if trie.item != nil {
  373. if err := visitor(prefix, trie.item); err != nil {
  374. if err == SkipSubtree {
  375. return nil
  376. }
  377. return err
  378. }
  379. }
  380. // Then continue to the children.
  381. return trie.children.walk(&prefix, visitor)
  382. }
  383. func (trie *Trie) longestCommonPrefixLength(prefix Prefix) (i int) {
  384. for ; i < len(prefix) && i < len(trie.prefix) && prefix[i] == trie.prefix[i]; i++ {
  385. }
  386. return
  387. }
  388. // Errors ----------------------------------------------------------------------
  389. var (
  390. SkipSubtree = errors.New("Skip this subtree")
  391. ErrNilPrefix = errors.New("Nil prefix passed into a method call")
  392. )