forked from sandia-minimega/minimega
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.go
180 lines (140 loc) · 3.56 KB
/
trie.go
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
// Copyright 2015-2021 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government retains certain
// rights in this software.
package ranges
import (
"fmt"
"sort"
"strconv"
"unicode"
)
type trieNode struct {
Children map[rune]*trieNode
Terminal bool
}
func newTrie() *trieNode {
return &trieNode{
Children: make(map[rune]*trieNode),
}
}
// Add a string to the trie
func (t *trieNode) Add(s string) {
t.add([]rune(s))
}
// Add a slice of runes to the trie
func (t *trieNode) add(s []rune) {
if len(s) == 0 {
t.Terminal = true
return
}
if _, ok := t.Children[s[0]]; !ok {
t.Children[s[0]] = newTrie()
}
t.Children[s[0]].add(s[1:])
}
// Strings returns the unique strings stored in the trie
func (t *trieNode) Strings() []string {
res := []string{}
if t.Terminal {
res = append(res, "")
}
for k, child := range t.Children {
for _, suffix := range child.Strings() {
res = append(res, fmt.Sprintf("%c%s", k, suffix))
}
}
return res
}
// AlphaPrefixes find alphabetical prefixes among the unique strings stored in
// the trie.
func (t *trieNode) AlphaPrefixes() []string {
var numericChild bool
res := []string{}
for k, child := range t.Children {
if unicode.IsLetter(k) {
for _, suffix := range child.AlphaPrefixes() {
res = append(res, fmt.Sprintf("%c%s", k, suffix))
}
} else {
numericChild = true
}
}
if t.Terminal || numericChild {
res = append(res, "")
}
return res
}
// Flatten returns a compact representation of the trie with numbers replaced
// with range notations
func (t *trieNode) Flatten() []string {
var res []string
if t.Terminal {
res = append(res, "")
}
children := t.sortedChildren()
// children that are numbers and their descendants that are non-numbers
nums := map[int]*trieNode{}
for _, k := range children {
// record numeric children for later
if unicode.IsNumber(k) {
for i, child := range t.Children[k].flattenNums(k) {
v, _ := strconv.Atoi(i)
nums[v] = child
}
continue
}
// append non-numeric children immediately
for _, suffix := range t.Children[k].Flatten() {
res = append(res, fmt.Sprintf("%c%s", k, suffix))
}
}
// suffixes and the ints that they correspond to
vals := map[string][]int{}
for i, descendant := range nums {
for _, suffix := range descendant.Flatten() {
vals[suffix] = append(vals[suffix], i)
}
}
// again, sort for determinism
suffixes := []string{}
for suffix := range vals {
suffixes = append(suffixes, suffix)
}
sort.Strings(suffixes)
// combine each group with the same suffix
for _, suffix := range suffixes {
res = append(res, unsplitInts(vals[suffix])+suffix)
}
return res
}
// sortedChildren returns the children rune in sorted order
func (t *trieNode) sortedChildren() []rune {
// runes of children to sort
var res []rune
// flatten all the children that are numbers first
for k := range t.Children {
res = append(res, k)
}
// sort child runes so that there's some determinism on ordering
sort.Slice(res, func(i, j int) bool {
return res[i] < res[j]
})
return res
}
// flattenNums converts trieNodes from a given point into ints and returns
// pointers to the children. If r is not a number, returns an empty map.
func (t *trieNode) flattenNums(r rune) map[string]*trieNode {
res := map[string]*trieNode{}
for k, child := range t.Children {
if !unicode.IsNumber(k) {
continue
}
for k, v := range child.flattenNums(k) {
res[string(r)+k] = v
}
}
if len(res) == 0 {
res[string(r)] = t
}
return res
}