Debian Codegraph @

patricia/patricia.go

raw symbols SCIP
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
6 package patricia
7
8 import (
9 "bytes"
10 "errors"
11 "fmt"
12 "io"
13 "strings"
14 )
15
16 //------------------------------------------------------------------------------
17 // Trie
18 //------------------------------------------------------------------------------
19
20 const (
21 defaultMaxPrefixPerNode = 10
22 )
23
24 var (
25 maxPrefixPerNode = defaultMaxPrefixPerNode
26 )
27
28 type (
29 // Prefix is the type of node prefixes
30 Prefix []byte
31 // Item is just interface{}
32 Item interface{}
33 // VisitorFunc is the type of functions passed to visit function
34 VisitorFunc func(prefix Prefix, item Item) error
35 // FuzzyVisitorFunc additionaly returns how many characters were skipped which can be sorted on
36 FuzzyVisitorFunc func(prefix Prefix, item Item, skipped int) error
37 )
38
39 // Trie is a generic patricia trie that allows fast retrieval of items by prefix.
40 // and other funky stuff.
41 //
42 // Trie is not thread-safe.
43 type Trie struct {
44 prefix Prefix
45 item Item
46 mask uint64
47
48 children childList
49 }
50
51 // Public API ------------------------------------------------------------------
52
53 // NewTrie constructs a new trie.
54 func NewTrie() *Trie {
55 trie := &Trie{}
56
57 trie.children = newSuperDenseChildList()
58
59 trie.mask = 0
60 return trie
61 }
62
63 // SetMaxPrefixPerNode sets the maximum length of a prefix before it is split into two nodes
64 func SetMaxPrefixPerNode(value int) {
65 maxPrefixPerNode = value
66 }
67
68 // Clone makes a copy of an existing trie.
69 // Items stored in both tries become shared, obviously.
70 func (trie *Trie) Clone() *Trie {
71 return &Trie{
72 prefix: append(Prefix(nil), trie.prefix...),
73 item: trie.item,
74 children: trie.children.clone(),
75 }
76 }
77
78 // Item returns the item stored in the root of this trie.
79 func (trie *Trie) Item() Item {
80 return trie.item
81 }
82
83 // Insert inserts a new item into the trie using the given prefix. Insert does
84 // not replace existing items. It returns false if an item was already in place.
85 func (trie *Trie) Insert(key Prefix, item Item) (inserted bool) {
86 return trie.put(key, item, false)
87 }
88
89 // Set works much like Insert, but it always sets the item, possibly replacing
90 // the item previously inserted.
91 func (trie *Trie) Set(key Prefix, item Item) {
92 trie.put(key, item, true)
93 }
94
95 // Get returns the item located at key.
96 //
97 // This method is a bit dangerous, because Get can as well end up in an internal
98 // node that is not really representing any user-defined value. So when nil is
99 // a valid value being used, it is not possible to tell if the value was inserted
100 // into the tree by the user or not. A possible workaround for this is not to use
101 // nil interface as a valid value, even using zero value of any type is enough
102 // to prevent this bad behaviour.
103 func (trie *Trie) Get(key Prefix) (item Item) {
104 _, node, found, leftover := trie.findSubtree(key)
105 if !found || len(leftover) != 0 {
106 return nil
107 }
108 return node.item
109 }
110
111 // Match returns what Get(prefix) != nil would return. The same warning as for
112 // Get applies here as well.
113 func (trie *Trie) Match(prefix Prefix) (matchedExactly bool) {
114 return trie.Get(prefix) != nil
115 }
116
117 // MatchSubtree returns true when there is a subtree representing extensions
118 // to key, that is if there are any keys in the tree which have key as prefix.
119 func (trie *Trie) MatchSubtree(key Prefix) (matched bool) {
120 _, _, matched, _ = trie.findSubtree(key)
121 return
122 }
123
124 // Visit calls visitor on every node containing a non-nil item
125 // in alphabetical order.
126 //
127 // If an error is returned from visitor, the function stops visiting the tree
128 // and returns that error, unless it is a special error - SkipSubtree. In that
129 // case Visit skips the subtree represented by the current node and continues
130 // elsewhere.
131 func (trie *Trie) Visit(visitor VisitorFunc) error {
132 return trie.walk(nil, visitor)
133 }
134
135 func (trie *Trie) size() int {
136 n := 0
137
138 err := trie.walk(nil, func(prefix Prefix, item Item) error {
139 n++
140 return nil
141 })
142
143 if err != nil {
144 panic(err)
145 }
146
147 return n
148 }
149
150 func (trie *Trie) total() int {
151 return 1 + trie.children.total()
152 }
153
154 // VisitSubtree works much like Visit, but it only visits nodes matching prefix.
155 func (trie *Trie) VisitSubtree(prefix Prefix, visitor VisitorFunc) error {
156 // Nil prefix not allowed.
157 if prefix == nil {
158 panic(ErrNilPrefix)
159 }
160
161 // Empty trie must be handled explicitly.
162 if trie.prefix == nil {
163 return nil
164 }
165
166 // Locate the relevant subtree.
167 _, root, found, leftover := trie.findSubtree(prefix)
168 if !found {
169 return nil
170 }
171 prefix = append(prefix, leftover...)
172
173 // Visit it.
174 return root.walk(prefix, visitor)
175 }
176
177 type potentialSubtree struct {
178 idx int
179 skipped int
180 prefix Prefix
181 node *Trie
182 }
183
184 // VisitFuzzy visits every node that is succesfully matched via fuzzy matching
185 func (trie *Trie) VisitFuzzy(partial Prefix, caseInsensitive bool, visitor FuzzyVisitorFunc) error {
186 if len(partial) == 0 {
187 return trie.VisitPrefixes(partial, caseInsensitive, func(prefix Prefix, item Item) error {
188 return visitor(prefix, item, 0)
189 })
190 }
191
192 var (
193 m uint64
194 cmp uint64
195 i int
196 p potentialSubtree
197 )
198
199 potential := []potentialSubtree{potentialSubtree{node: trie, prefix: Prefix(""), idx: 0}}
200 for l := len(potential); l > 0; l = len(potential) {
201 i = l - 1
202 p = potential[i]
203
204 potential = potential[:i]
205 m = makePrefixMask(partial[p.idx:])
206
207 if caseInsensitive {
208 cmp = caseInsensitiveMask(p.node.mask)
209 } else {
210 cmp = p.node.mask
211 }
212
213 if (cmp & m) != m {
214 continue
215 }
216
217 matchCount, skipped := fuzzyMatchCount(p.node.prefix,
218 partial[p.idx:], p.idx, caseInsensitive)
219 p.idx += matchCount
220 if p.idx != 0 {
221 p.skipped += skipped
222 }
223
224 if p.idx == len(partial) {
225 fullPrefix := append(p.prefix, p.node.prefix...)
226
227 err := p.node.walk(Prefix(""), func(prefix Prefix, item Item) error {
228 key := make([]byte, len(fullPrefix), len(fullPrefix)+len(prefix))
229 copy(key, fullPrefix)
230 key = append(key, prefix...)
231
232 err := visitor(key, item, p.skipped)
233 if err != nil {
234 return err
235 }
236
237 return nil
238 })
239 if err != nil {
240 return err
241 }
242
243 continue
244 }
245
246 for _, c := range p.node.children.getChildren() {
247 if c != nil {
248 newPrefix := make(Prefix, len(p.prefix), len(p.prefix)+len(p.node.prefix))
249 copy(newPrefix, p.prefix)
250 newPrefix = append(newPrefix, p.node.prefix...)
251 potential = append(potential, potentialSubtree{
252 node: c,
253 prefix: newPrefix,
254 idx: p.idx,
255 skipped: p.skipped,
256 })
257 } else {
258 fmt.Println("warning, child isn il")
259 }
260 }
261 }
262
263 return nil
264 }
265
266 func fuzzyMatchCount(prefix, query Prefix, idx int, caseInsensitive bool) (count, skipped int) {
267 for i := 0; i < len(prefix); i++ {
268 var match bool
269
270 if caseInsensitive {
271 match = matchCaseInsensitive(prefix[i], query[count])
272 } else {
273 match = prefix[i] == query[count]
274 }
275
276 if !match {
277 if count+idx > 0 {
278 skipped++
279 }
280 continue
281 }
282
283 count++
284 if count >= len(query) {
285 return
286 }
287 }
288 return
289 }
290
291 // VisitSubstring takes a substring and visits all the nodes that whos prefix contains this substring
292 func (trie *Trie) VisitSubstring(substring Prefix, caseInsensitive bool, visitor VisitorFunc) error {
293 if len(substring) == 0 {
294 return trie.VisitSubtree(substring, visitor)
295 }
296
297 var (
298 m uint64
299 cmp uint64
300 i int
301 p potentialSubtree
302 suffixLen int
303 maxSuffixLen = len(substring) - 1
304 )
305
306 potential := []potentialSubtree{potentialSubtree{node: trie, prefix: nil}}
307 for l := len(potential); l > 0; l = len(potential) {
308 i = l - 1
309 p = potential[i]
310
311 potential = potential[:i]
312
313 if len(p.prefix) < maxSuffixLen {
314 suffixLen = len(p.prefix)
315 } else {
316 suffixLen = maxSuffixLen
317 }
318
319 searchBytes := append(p.prefix[len(p.prefix)-suffixLen:], p.node.prefix...)
320
321 contains := false
322
323 if caseInsensitive {
324 contains = bytes.Contains(bytes.ToUpper(searchBytes), bytes.ToUpper(substring))
325 } else {
326 contains = bytes.Contains(searchBytes, substring)
327 }
328
329 if contains {
330 fullPrefix := append(p.prefix, p.node.prefix...)
331 err := p.node.walk(Prefix(""), func(prefix Prefix, item Item) error {
332 key := make([]byte, len(fullPrefix), len(fullPrefix)+len(prefix))
333 copy(key, fullPrefix)
334 key = append(key, prefix...)
335 copy(key, append(fullPrefix, prefix...))
336
337 err := visitor(key, item)
338 if err != nil {
339 return err
340 }
341
342 return nil
343 })
344 if err != nil {
345 return err
346 }
347 }
348
349 newPrefix := make(Prefix, len(p.prefix), len(p.prefix)+len(p.node.prefix))
350 copy(newPrefix, p.prefix)
351 newPrefix = append(newPrefix, p.node.prefix...)
352
353 overLap := overlapLength(newPrefix, substring, caseInsensitive)
354 m = makePrefixMask(substring[overLap:])
355
356 for _, c := range p.node.children.getChildren() {
357 if caseInsensitive {
358 cmp = caseInsensitiveMask(c.mask)
359 } else {
360 cmp = c.mask
361 }
362 if c != nil && (cmp&m == m) {
363 potential = append(potential, potentialSubtree{
364 node: c,
365 prefix: newPrefix,
366 })
367 }
368 }
369 }
370
371 return nil
372 }
373
374 func overlapLength(prefix, query Prefix, caseInsensitive bool) int {
375 startLength := len(query) - 1
376 if len(prefix) < startLength {
377 startLength = len(prefix)
378 }
379 for i := startLength; i > 0; i-- {
380 suffix := prefix[len(prefix)-i:]
381 queryPrefix := query[:i]
382 if caseInsensitive {
383 if bytes.EqualFold(suffix, queryPrefix) {
384 return i
385 }
386 } else if bytes.Equal(suffix, queryPrefix) {
387 return i
388 }
389 }
390
391 return 0
392 }
393
394 // VisitPrefixes visits only nodes that represent prefixes of key.
395 // To say the obvious, returning SkipSubtree from visitor makes no sense here.
396 func (trie *Trie) VisitPrefixes(key Prefix, caseInsensitive bool, visitor VisitorFunc) error {
397 // Nil key not allowed.
398 if key == nil {
399 panic(ErrNilPrefix)
400 }
401
402 // Empty trie must be handled explicitly.
403 if trie.prefix == nil {
404 return nil
405 }
406
407 // Walk the path matching key prefixes.
408 node := trie
409 prefix := key
410 offset := 0
411 for {
412 // Compute what part of prefix matches.
413 common := node.longestCommonPrefixLength(key, caseInsensitive)
414 key = key[common:]
415 offset += common
416
417 // Partial match means that there is no subtree matching prefix.
418 if common < len(node.prefix) {
419 return nil
420 }
421
422 // Call the visitor.
423 if item := node.item; item != nil {
424 if err := visitor(prefix[:offset], item); err != nil {
425 return err
426 }
427 }
428
429 if len(key) == 0 {
430 // This node represents key, we are finished.
431 return nil
432 }
433
434 // There is some key suffix left, move to the children.
435 child := node.children.next(key[0])
436 if child == nil {
437 // There is nowhere to continue, return.
438 return nil
439 }
440
441 node = child
442 }
443 }
444
445 // Delete deletes the item represented by the given prefix.
446 //
447 // True is returned if the matching node was found and deleted.
448 func (trie *Trie) Delete(key Prefix) (deleted bool) {
449 // Nil prefix not allowed.
450 if key == nil {
451 panic(ErrNilPrefix)
452 }
453
454 // Empty trie must be handled explicitly.
455 if trie.prefix == nil {
456 return false
457 }
458
459 // Find the relevant node.
460 path, found, _ := trie.findSubtreePath(key)
461 if !found {
462 return false
463 }
464
465 node := path[len(path)-1]
466 var parent *Trie
467 if len(path) != 1 {
468 parent = path[len(path)-2]
469 }
470
471 // If the item is already set to nil, there is nothing to do.
472 if node.item == nil {
473 return false
474 }
475
476 // Delete the item.
477 node.item = nil
478
479 // Initialise i before goto.
480 // Will be used later in a loop.
481 i := len(path) - 1
482
483 // In case there are some child nodes, we cannot drop the whole subtree.
484 // We can try to compact nodes, though.
485 if node.children.length() != 0 {
486 goto Compact
487 }
488
489 // In case we are at the root, just reset it and we are done.
490 if parent == nil {
491 node.reset()
492 return true
493 }
494
495 // We can drop a subtree.
496 // Find the first ancestor that has its value set or it has 2 or more child nodes.
497 // That will be the node where to drop the subtree at.
498 for ; i >= 0; i-- {
499 if current := path[i]; current.item != nil || current.children.length() >= 2 {
500 break
501 }
502 }
503
504 // Handle the case when there is no such node.
505 // In other words, we can reset the whole tree.
506 if i == -1 {
507 path[0].reset()
508 return true
509 }
510
511 // We can just remove the subtree here.
512 node = path[i]
513 if i == 0 {
514 parent = nil
515 } else {
516 parent = path[i-1]
517 }
518 // i+1 is always a valid index since i is never pointing to the last node.
519 // The loop above skips at least the last node since we are sure that the item
520 // is set to nil and it has no children, othewise we would be compacting instead.
521 node.children.remove(path[i+1].prefix[0])
522
523 // lastly, the bitmasks of all of the parent nodes have to be updated again, since
524 // a child node of all of them has bin removed
525 for ; i >= 0; i-- {
526 n := path[i]
527 n.mask = n.children.combinedMask()
528 }
529
530 Compact:
531 // The node is set to the first non-empty ancestor,
532 // so try to compact since that might be possible now.
533 if compacted := node.compact(); compacted != node {
534 if parent == nil {
535 *node = *compacted
536 } else {
537 parent.children.replace(node.prefix[0], compacted)
538 *parent = *parent.compact()
539 }
540 }
541
542 return true
543 }
544
545 // DeleteSubtree finds the subtree exactly matching prefix and deletes it.
546 //
547 // True is returned if the subtree was found and deleted.
548 func (trie *Trie) DeleteSubtree(prefix Prefix) (deleted bool) {
549 // Nil prefix not allowed.
550 if prefix == nil {
551 panic(ErrNilPrefix)
552 }
553
554 // Empty trie must be handled explicitly.
555 if trie.prefix == nil {
556 return false
557 }
558
559 // Locate the relevant subtree.
560 parent, root, found, _ := trie.findSubtree(prefix)
561 path, _, _ := trie.findSubtreePath(prefix)
562 if !found {
563 return false
564 }
565
566 // If we are in the root of the trie, reset the trie.
567 if parent == nil {
568 root.reset()
569 return true
570 }
571
572 // Otherwise remove the root node from its parent.
573 parent.children.remove(root.prefix[0])
574
575 // update masks
576 parent.mask = parent.children.combinedMask()
577 for i := len(path) - 1; i >= 0; i-- {
578 n := path[i]
579 n.mask = n.children.combinedMask()
580 }
581
582 return true
583 }
584
585 // Internal helper methods -----------------------------------------------------
586
587 func (trie *Trie) empty() bool {
588 return trie.item == nil && trie.children.length() == 0
589 }
590
591 func (trie *Trie) reset() {
592 trie.prefix = nil
593 trie.children = newSuperDenseChildList()
594 }
595
596 func makePrefixMask(key Prefix) uint64 {
597 var mask uint64
598 for _, b := range key {
599 if b >= '0' && b <= '9' {
600 // 0-9 bits: 0-9
601 b -= 48
602 } else if b >= 'A' && b <= 'Z' {
603 // A-Z bits: 10-35
604 b -= 55
605 } else if b >= 'a' && b <= 'z' {
606 // a-z bits: 36-61
607 b -= 61
608 } else if b == '.' {
609 b = 62
610 } else if b == '-' {
611 b = 63
612 } else {
613 continue
614 }
615 mask |= uint64(1) << uint64(b)
616 }
617 return mask
618 }
619
620 const upperBits = 0xFFFFFFC00
621 const lowerBits = 0x3FFFFFF000000000
622
623 func caseInsensitiveMask(mask uint64) uint64 {
624 mask |= (mask & upperBits) << uint64(26)
625 mask |= (mask & lowerBits) >> uint64(26)
626 return mask
627 }
628
629 var charmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-"
630
631 func (trie *Trie) put(key Prefix, item Item, replace bool) (inserted bool) {
632 // Nil prefix not allowed.
633 if key == nil {
634 panic(ErrNilPrefix)
635 }
636
637 var (
638 common int
639 node = trie
640 child *Trie
641 mask uint64
642 )
643
644 mask = makePrefixMask(key)
645
646 if node.prefix == nil {
647 node.mask |= mask
648 if len(key) <= maxPrefixPerNode {
649 node.prefix = key
650 goto InsertItem
651 }
652 node.prefix = key[:maxPrefixPerNode]
653 key = key[maxPrefixPerNode:]
654 mask = makePrefixMask(key)
655 goto AppendChild
656 }
657
658 for {
659 // Compute the longest common prefix length.
660 common = node.longestCommonPrefixLength(key, false)
661 key = key[common:]
662
663 // Only a part matches, split.
664 if common < len(node.prefix) {
665 goto SplitPrefix
666 }
667
668 // common == len(node.prefix) since never (common > len(node.prefix))
669 // common == len(former key) <-> 0 == len(key)
670 // -> former key == node.prefix
671 if len(key) == 0 {
672 goto InsertItem
673 }
674
675 node.mask |= mask
676 // Check children for matching prefix.
677 child = node.children.next(key[0])
678 if child == nil {
679 goto AppendChild
680 }
681 node = child
682 }
683
684 SplitPrefix:
685 // Split the prefix if necessary.
686 child = new(Trie)
687 *child = *node
688 *node = *NewTrie()
689 node.prefix = child.prefix[:common]
690 child.prefix = child.prefix[common:]
691 child = child.compact()
692 node.children = node.children.add(child)
693 node.mask = child.mask
694 node.mask |= mask
695 mask = makePrefixMask(key)
696
697 AppendChild:
698 // Keep appending children until whole prefix is inserted.
699 // This loop starts with empty node.prefix that needs to be filled.
700 for len(key) != 0 {
701 child := NewTrie()
702 child.mask = mask
703 if len(key) <= maxPrefixPerNode {
704 child.prefix = key
705 node.children = node.children.add(child)
706 node = child
707 goto InsertItem
708 } else {
709 child.prefix = key[:maxPrefixPerNode]
710 key = key[maxPrefixPerNode:]
711 mask = makePrefixMask(key)
712 node.children = node.children.add(child)
713 node = child
714 }
715 }
716
717 InsertItem:
718 // Try to insert the item if possible.
719 if replace || node.item == nil {
720 node.item = item
721 return true
722 }
723 return false
724 }
725
726 func (trie *Trie) compact() *Trie {
727 // Only a node with a single child can be compacted.
728 if trie.children.length() != 1 {
729 return trie
730 }
731
732 child := trie.children.head()
733
734 // If any item is set, we cannot compact since we want to retain
735 // the ability to do searching by key. This makes compaction less usable,
736 // but that simply cannot be avoided.
737 if trie.item != nil || child.item != nil {
738 return trie
739 }
740
741 // Make sure the combined prefixes fit into a single node.
742 if len(trie.prefix)+len(child.prefix) > maxPrefixPerNode {
743 return trie
744 }
745
746 // Concatenate the prefixes, move the items.
747 child.prefix = append(trie.prefix, child.prefix...)
748 child.mask = trie.mask
749 if trie.item != nil {
750 child.item = trie.item
751 }
752
753 return child
754 }
755
756 func (trie *Trie) findSubtree(prefix Prefix) (parent *Trie, root *Trie, found bool, leftover Prefix) {
757 // Find the subtree matching prefix.
758 root = trie
759 for {
760 // Compute what part of prefix matches.
761 common := root.longestCommonPrefixLength(prefix, false)
762 prefix = prefix[common:]
763
764 // We used up the whole prefix, subtree found.
765 if len(prefix) == 0 {
766 found = true
767 leftover = root.prefix[common:]
768 return
769 }
770
771 // Partial match means that there is no subtree matching prefix.
772 if common < len(root.prefix) {
773 leftover = root.prefix[common:]
774 return
775 }
776
777 // There is some prefix left, move to the children.
778 child := root.children.next(prefix[0])
779 if child == nil {
780 // There is nowhere to continue, there is no subtree matching prefix.
781 return
782 }
783
784 parent = root
785 root = child
786 }
787 }
788
789 func (trie *Trie) findSubtreePath(prefix Prefix) (path []*Trie, found bool, leftover Prefix) {
790 // Find the subtree matching prefix.
791 root := trie
792 var subtreePath []*Trie
793 for {
794 // Append the current root to the path.
795 subtreePath = append(subtreePath, root)
796
797 // Compute what part of prefix matches.
798 common := root.longestCommonPrefixLength(prefix, false)
799 prefix = prefix[common:]
800
801 // We used up the whole prefix, subtree found.
802 if len(prefix) == 0 {
803 path = subtreePath
804 found = true
805 leftover = root.prefix[common:]
806 return
807 }
808
809 // Partial match means that there is no subtree matching prefix.
810 if common < len(root.prefix) {
811 leftover = root.prefix[common:]
812 return
813 }
814
815 // There is some prefix left, move to the children.
816 child := root.children.next(prefix[0])
817 if child == nil {
818 // There is nowhere to continue, there is no subtree matching prefix.
819 return
820 }
821
822 root = child
823 }
824 }
825
826 func (trie *Trie) walk(actualRootPrefix Prefix, visitor VisitorFunc) error {
827 var prefix Prefix
828 // Allocate a bit more space for prefix at the beginning.
829 if actualRootPrefix == nil {
830 prefix = make(Prefix, 32+len(trie.prefix))
831 copy(prefix, trie.prefix)
832 prefix = prefix[:len(trie.prefix)]
833 } else {
834 prefix = make(Prefix, 32+len(actualRootPrefix))
835 copy(prefix, actualRootPrefix)
836 prefix = prefix[:len(actualRootPrefix)]
837 }
838
839 // Visit the root first. Not that this works for empty trie as well since
840 // in that case item == nil && len(children) == 0.
841 if trie.item != nil {
842 if err := visitor(prefix, trie.item); err != nil {
843 if err == SkipSubtree {
844 return nil
845 }
846 return err
847 }
848 }
849
850 // Then continue to the children.
851 return trie.children.walk(&prefix, visitor)
852 }
853
854 func (trie *Trie) longestCommonPrefixLength(prefix Prefix, caseInsensitive bool) (i int) {
855 for ; i < len(prefix) && i < len(trie.prefix); i++ {
856 p := prefix[i]
857 t := trie.prefix[i]
858
859 if caseInsensitive {
860 if !(matchCaseInsensitive(t, p)) {
861 break
862 }
863 } else {
864 if p != t {
865 break
866 }
867 }
868 }
869 return
870 }
871
872 func matchCaseInsensitive(a byte, b byte) bool {
873 return a == b+32 || b == a+32 || a == b
874 }
875
876 func (trie *Trie) dump() string {
877 writer := &bytes.Buffer{}
878 trie.print(writer, 0)
879 return writer.String()
880 }
881
882 func (trie *Trie) print(writer io.Writer, indent int) {
883 fmt.Fprintf(writer, "%s%s %v\n", strings.Repeat(" ", indent), string(trie.prefix), trie.item)
884 trie.children.print(writer, indent+2)
885 }
886
887 // Errors ----------------------------------------------------------------------
888
889 var (
890 SkipSubtree = errors.New("Skip this subtree")
891 ErrNilPrefix = errors.New("Nil prefix passed into a method call")
892 )