Debian Codegraph @

patricia/children.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 "io"
10 "sort"
11 )
12
13 type childList interface {
14 length() int
15 head() *Trie
16 add(child *Trie) childList
17 remove(b byte)
18 replace(b byte, child *Trie)
19 next(b byte) *Trie
20 combinedMask() uint64
21 getChildren() []*Trie
22 walk(prefix *Prefix, visitor VisitorFunc) error
23 print(w io.Writer, indent int)
24 clone() childList
25 total() int
26 }
27
28 type tries []*Trie
29
30 func (t tries) Len() int {
31 return len(t)
32 }
33
34 func (t tries) Less(i, j int) bool {
35 strings := sort.StringSlice{string(t[i].prefix), string(t[j].prefix)}
36 return strings.Less(0, 1)
37 }
38
39 func (t tries) Swap(i, j int) {
40 t[i], t[j] = t[j], t[i]
41 }
42
43 type childContainer struct {
44 char byte
45 node *Trie
46 }
47 type superDenseChildList struct {
48 children []childContainer
49 }
50
51 func newSuperDenseChildList() childList {
52 return &superDenseChildList{
53 make([]childContainer, 0),
54 }
55 }
56
57 func (list *superDenseChildList) length() int {
58 return len(list.children)
59 }
60
61 func (list *superDenseChildList) head() *Trie {
62 if len(list.children) > 0 {
63 return list.children[0].node
64 }
65 return nil
66 }
67
68 func (list *superDenseChildList) add(child *Trie) childList {
69 char := child.prefix[0]
70 list.children = append(list.children, childContainer{
71 char,
72 child,
73 })
74 return list
75 }
76
77 func (list *superDenseChildList) remove(b byte) {
78 children := list.children
79 for i := 0; i < len(children); i++ {
80 if children[i].char == b {
81 // children[i] = children[len(children)-1]
82 // children = children[:len(children)-1]
83 // children = append(children[:i], children[i+1:]...)
84 newChildren := make([]childContainer, len(children)-1)
85 // copy the elements over to avoid "memory leaks"
86 copy(newChildren, children[:i])
87 copy(newChildren[i:], children[i+1:])
88 list.children = newChildren
89
90 // list.children = make([]childContainer, len(children))
91 // copy(list.children, children)
92 // children = nil
93
94 return
95 }
96 }
97 }
98
99 func (list *superDenseChildList) replace(b byte, child *Trie) {
100 children := list.children
101 for i := 0; i < len(list.children); i++ {
102 if children[i].char == b {
103 children[i].node = child
104 return
105 }
106 }
107 }
108
109 func (list *superDenseChildList) next(b byte) *Trie {
110 children := list.children
111 for i := 0; i < len(list.children); i++ {
112 if children[i].char == b {
113 return children[i].node
114 }
115 }
116 return nil
117 }
118
119 func (list superDenseChildList) combinedMask() uint64 {
120 var mask uint64
121 for _, child := range list.children {
122 // fmt.Printf("child = %+v\n", child)
123 mask |= child.node.mask
124 }
125 return mask
126 }
127
128 func (list *superDenseChildList) getChildren() []*Trie {
129 children := make([]*Trie, 0, len(list.children))
130 for _, child := range list.children {
131 children = append(children, child.node)
132 }
133 return children
134 }
135
136 func (list *superDenseChildList) walk(prefix *Prefix, visitor VisitorFunc) error {
137 for _, child := range list.children {
138 node := child.node
139 *prefix = append(*prefix, node.prefix...)
140 if node.item != nil {
141 if err := visitor(*prefix, node.item); err != nil {
142 if err == SkipSubtree {
143 *prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
144 continue
145 }
146 *prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
147 return err
148 }
149 }
150
151 err := node.children.walk(prefix, visitor)
152 *prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
153 if err != nil {
154 return err
155 }
156 }
157
158 return nil
159 }
160
161 func (list *superDenseChildList) print(w io.Writer, indent int) {
162 for _, child := range list.children {
163 child.node.print(w, indent)
164 }
165 }
166
167 func (list *superDenseChildList) clone() childList {
168 clones := make([]childContainer, len(list.children))
169
170 for i := 0; i < len(list.children); i++ {
171 child := list.children[i]
172 clones[i] = childContainer{child.char, child.node.Clone()}
173 }
174
175 return &superDenseChildList{
176 clones,
177 }
178 }
179
180 func (list *superDenseChildList) total() int {
181 return len(list.children)
182 }