-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupbyaffix.go
209 lines (180 loc) · 5.08 KB
/
groupbyaffix.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package compiler
import (
"sort"
"strings"
)
type trieNode[Assoc any] struct {
c byte
children map[byte]*trieNode[Assoc]
assocs []Assoc
isWord bool
}
func addToTrie[Assoc any](pool *[]trieNode[Assoc], root *trieNode[Assoc], s string, assoc Assoc) {
getNode := func() *trieNode[Assoc] {
if cap(*pool) <= len(*pool) {
*pool = make([]trieNode[Assoc], 0, cap(*pool)*2)
}
*pool = append(*pool, trieNode[Assoc]{})
return &((*pool)[len(*pool)-1])
}
if len(s) == 0 {
root.isWord = true
}
for i := range s {
b := s[i]
if root.children == nil {
root.children = make(map[byte]*trieNode[Assoc])
}
child, ok := root.children[b]
if !ok {
child = getNode()
child.c = b
root.children[b] = child
}
child.isWord = child.isWord || (i+1 == len(s))
root = child
}
root.assocs = append(root.assocs, assoc)
}
func words[Assoc any](node *trieNode[Assoc]) []Assoc {
accum := make([]Assoc, 0)
wordsHelper(node, &accum)
return accum
}
func wordsHelper[Assoc any](node *trieNode[Assoc], accum *[]Assoc) {
if node.isWord {
*accum = append(*accum, node.assocs...)
}
deterministicChildIter(node, func(c *trieNode[Assoc]) {
wordsHelper(c, accum)
})
}
func stoppingPoints[Assoc any](node *trieNode[Assoc]) [][]Assoc {
accum := make([][]Assoc, 0)
stoppingPointsHelper(node, node, &accum)
// Anything associated with the empty prefix should be added to every group
if node.isWord {
if len(accum) == 0 {
accum = append(accum, make([]Assoc, 0))
}
for i := range accum {
accum[i] = append(accum[i], node.assocs...)
}
}
return accum
}
func stoppingPointsHelper[Assoc any](root *trieNode[Assoc], node *trieNode[Assoc], accum *[][]Assoc) {
if node.isWord && node != root {
*accum = append(*accum, words(node))
return
}
deterministicChildIter(node, func(c *trieNode[Assoc]) {
stoppingPointsHelper(root, c, accum)
})
}
type deterministicChildIterPr[Assoc any] struct {
b byte
n *trieNode[Assoc]
}
// Iterating deterministically through the trie is useful for testing, and
// should not have a huge performance impact.
func deterministicChildIter[Assoc any](node *trieNode[Assoc], f func(*trieNode[Assoc])) {
cs := make([]deterministicChildIterPr[Assoc], 0, 256)
for i, c := range node.children {
cs = append(cs, deterministicChildIterPr[Assoc]{i, c})
}
sort.Slice(cs, func(i, j int) bool {
return cs[i].b < cs[j].b
})
for _, pr := range cs {
f(pr.n)
}
}
func getConstishPrefix(r *RouteInfo, parents []*RouteInfo) string {
var pref strings.Builder
lastWasSlash := false
for _, p := range parents {
writeByteToAffix(&pref, &lastWasSlash, '/')
writeStringToAffix(&pref, &lastWasSlash, p.constishPrefix)
if !allConst(p) {
return removeTrailingSlash(pref.String())
}
}
writeByteToAffix(&pref, &lastWasSlash, '/')
writeStringToAffix(&pref, &lastWasSlash, r.constishPrefix)
return removeTrailingSlash(pref.String())
}
// split list of routes into non-overlapping groups based on prefix
func groupByConstishPrefix(rwps []RouteWithParents) [][]RouteWithParents {
return groupByX(rwps, getConstishPrefix)
}
func getConstishSuffix(r *RouteInfo, parents []*RouteInfo) string {
var suff strings.Builder
lastWashSlash := false
// This is not unicode correct, but we don't care for this purpose.
for i := len(r.constishSuffix) - 1; i >= 0; i-- {
writeByteToAffix(&suff, &lastWashSlash, r.constishSuffix[i])
}
if allConst(r) {
writeByteToAffix(&suff, &lastWashSlash, '/')
for i := len(parents) - 1; i >= 0; i-- {
p := parents[i]
// This is not unicode correct, but we don't care for this purpose
for i := len(p.constishSuffix) - 1; i >= 0; i-- {
writeByteToAffix(&suff, &lastWashSlash, p.constishSuffix[i])
}
writeByteToAffix(&suff, &lastWashSlash, '/')
if !allConst(p) {
break
}
}
}
return removeTrailingSlash(suff.String())
}
func removeTrailingSlash(str string) string {
if len(str) > 1 /* deliberately not zero */ && str[len(str)-1] == '/' {
return str[:len(str)-1]
}
return str
}
// split list of routes into non-overlapping groups based on suffix
func groupByConstishSuffix(rwps []RouteWithParents) [][]RouteWithParents {
return groupByX(rwps, getConstishSuffix)
}
func allConst(r *RouteInfo) bool {
for _, elem := range r.elems {
if !(elem.kind == slash || elem.kind == constant) {
return false
}
}
return true
}
func writeByteToAffix(sb *strings.Builder, lastWasSlash *bool, b byte) {
if b == '/' && (*lastWasSlash || sb.Len() == 0) {
return
}
sb.WriteByte(b)
*lastWasSlash = (b == '/')
}
func writeStringToAffix(sb *strings.Builder, lastWasSlash *bool, s string) {
for i := range s {
b := s[i]
if b == '/' && (*lastWasSlash || sb.Len() == 0) {
return
}
sb.WriteByte(b)
*lastWasSlash = (b == '/')
}
}
func groupByX(rwps []RouteWithParents, getGroupString func(*RouteInfo, []*RouteInfo) string) [][]RouteWithParents {
var trie trieNode[RouteWithParents]
pool := make([]trieNode[RouteWithParents], 0)
for _, rwp := range rwps {
if !rwp.Route.terminal {
continue
}
groupString := getGroupString(rwp.Route, rwp.Parents)
addToTrie(&pool, &trie, groupString, rwp)
}
return stoppingPoints(&trie)
}