summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/ozeidan/fuzzy-patricia.v3/patricia/children.go
blob: b1e4e63c485e60ad819d5e7f39639abd6383f388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2014 The go-patricia AUTHORS
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.

package patricia

import (
	"io"
	"sort"
)

type childList interface {
	length() int
	head() *Trie
	add(child *Trie) childList
	remove(b byte)
	replace(b byte, child *Trie)
	next(b byte) *Trie
	combinedMask() uint64
	getChildren() []*Trie
	walk(prefix *Prefix, visitor VisitorFunc) error
	print(w io.Writer, indent int)
	clone() childList
	total() int
}

type tries []*Trie

func (t tries) Len() int {
	return len(t)
}

func (t tries) Less(i, j int) bool {
	strings := sort.StringSlice{string(t[i].prefix), string(t[j].prefix)}
	return strings.Less(0, 1)
}

func (t tries) Swap(i, j int) {
	t[i], t[j] = t[j], t[i]
}

type childContainer struct {
	char byte
	node *Trie
}
type superDenseChildList struct {
	children []childContainer
}

func newSuperDenseChildList() childList {
	return &superDenseChildList{
		make([]childContainer, 0),
	}
}

func (list *superDenseChildList) length() int {
	return len(list.children)
}

func (list *superDenseChildList) head() *Trie {
	if len(list.children) > 0 {
		return list.children[0].node
	}
	return nil
}

func (list *superDenseChildList) add(child *Trie) childList {
	char := child.prefix[0]
	list.children = append(list.children, childContainer{
		char,
		child,
	})
	return list
}

func (list *superDenseChildList) remove(b byte) {
	children := list.children
	for i := 0; i < len(children); i++ {
		if children[i].char == b {
			// children[i] = children[len(children)-1]
			// children = children[:len(children)-1]
			// children = append(children[:i], children[i+1:]...)
			newChildren := make([]childContainer, len(children)-1)
			// copy the elements over to avoid "memory leaks"
			copy(newChildren, children[:i])
			copy(newChildren[i:], children[i+1:])
			list.children = newChildren

			// list.children = make([]childContainer, len(children))
			// copy(list.children, children)
			// children = nil

			return
		}
	}
}

func (list *superDenseChildList) replace(b byte, child *Trie) {
	children := list.children
	for i := 0; i < len(list.children); i++ {
		if children[i].char == b {
			children[i].node = child
			return
		}
	}
}

func (list *superDenseChildList) next(b byte) *Trie {
	children := list.children
	for i := 0; i < len(list.children); i++ {
		if children[i].char == b {
			return children[i].node
		}
	}
	return nil
}

func (list superDenseChildList) combinedMask() uint64 {
	var mask uint64
	for _, child := range list.children {
		// fmt.Printf("child = %+v\n", child)
		mask |= child.node.mask
	}
	return mask
}

func (list *superDenseChildList) getChildren() []*Trie {
	children := make([]*Trie, 0, len(list.children))
	for _, child := range list.children {
		children = append(children, child.node)
	}
	return children
}

func (list *superDenseChildList) walk(prefix *Prefix, visitor VisitorFunc) error {
	for _, child := range list.children {
		node := child.node
		*prefix = append(*prefix, node.prefix...)
		if node.item != nil {
			if err := visitor(*prefix, node.item); err != nil {
				if err == SkipSubtree {
					*prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
					continue
				}
				*prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
				return err
			}
		}

		err := node.children.walk(prefix, visitor)
		*prefix = (*prefix)[:len(*prefix)-len(node.prefix)]
		if err != nil {
			return err
		}
	}

	return nil
}

func (list *superDenseChildList) print(w io.Writer, indent int) {
	for _, child := range list.children {
		child.node.print(w, indent)
	}
}

func (list *superDenseChildList) clone() childList {
	clones := make([]childContainer, len(list.children))

	for i := 0; i < len(list.children); i++ {
		child := list.children[i]
		clones[i] = childContainer{child.char, child.node.Clone()}
	}

	return &superDenseChildList{
		clones,
	}
}

func (list *superDenseChildList) total() int {
	return len(list.children)
}