generated from wesen/wesen-go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie-node.go
210 lines (176 loc) · 4.75 KB
/
trie-node.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
210
package repositories
import (
"github.com/go-go-golems/glazed/pkg/cmds"
"github.com/go-go-golems/glazed/pkg/cmds/alias"
"github.com/rs/zerolog/log"
"sort"
)
type TrieNode struct {
Children map[string]*TrieNode
Commands []cmds.Command
}
type RenderNode struct {
Name string
Command cmds.Command
Children []*RenderNode
}
// NewTrieNode creates a new trie node.
func NewTrieNode(commands []cmds.Command, aliases []*alias.CommandAlias) *TrieNode {
return &TrieNode{
Children: make(map[string]*TrieNode),
Commands: commands,
}
}
// Remove removes a command from the trie.
func (t *TrieNode) Remove(prefix []string) []cmds.Command {
if len(prefix) == 0 {
commands := t.CollectCommands(prefix, true)
t.Commands = make([]cmds.Command, 0)
t.Children = make(map[string]*TrieNode)
return commands
}
removedCommands := make([]cmds.Command, 0)
// try to get parent node
path := prefix[:len(prefix)-1]
parentNode := t.findNode(path, false)
name := prefix[len(prefix)-1]
if parentNode == nil {
log.Debug().Msgf("parent node not found for %s", name)
return []cmds.Command{}
}
childNode, ok := parentNode.Children[name]
if ok {
// remove the node
commands := childNode.CollectCommands([]string{}, true)
removedCommands = append(removedCommands, commands...)
delete(parentNode.Children, name)
}
// check if this is an actual command or alias
for i, c := range parentNode.Commands {
if c.Description().Name == name {
removedCommands = append(removedCommands, c)
parentNode.Commands = append(parentNode.Commands[:i], parentNode.Commands[i+1:]...)
}
}
return removedCommands
}
// InsertCommand inserts a command in the trie, replacing it if it already exists.
func (t *TrieNode) InsertCommand(prefix []string, command cmds.Command) {
node := t.findNode(prefix, true)
// check if the command is already in the trie
for i, c := range node.Commands {
if c.Description().Name == command.Description().Name {
node.Commands[i] = command
return
}
}
node.Commands = append(node.Commands, command)
}
// findNode finds the node corresponding to the given prefix, creating it if it doesn't exist.
func (t *TrieNode) findNode(prefix []string, createNewNodes bool) *TrieNode {
node := t
for _, p := range prefix {
if _, ok := node.Children[p]; !ok {
if !createNewNodes {
log.Debug().Msgf("node %s not found", p)
return nil
}
node.Children[p] = NewTrieNode([]cmds.Command{}, []*alias.CommandAlias{})
}
node = node.Children[p]
}
return node
}
func (t *TrieNode) FindNode(prefix []string) *TrieNode {
return t.findNode(prefix, false)
}
func (t *TrieNode) FindCommand(path []string) (cmds.Command, bool) {
if len(path) == 0 {
return nil, false
}
parentPath := path[:len(path)-1]
commandName := path[len(path)-1]
node := t.findNode(parentPath, false)
if node == nil {
return nil, false
}
for _, c := range node.Commands {
if c.Description().Name == commandName {
return c, true
}
}
return nil, false
}
// CollectCommands collects all commands and aliases under the given prefix.
func (t *TrieNode) CollectCommands(prefix []string, recurse bool) []cmds.Command {
ret := make([]cmds.Command, 0)
// Check if the prefix identifies a single command.
if len(prefix) > 0 {
// try to get parent node
path := prefix[:len(prefix)-1]
parentNode := t.findNode(path, false)
name := prefix[len(prefix)-1]
if parentNode != nil {
for _, c := range parentNode.Commands {
if c.Description().Name == name {
ret = append(ret, c)
break
}
}
}
if !recurse {
return ret
}
}
node := t.findNode(prefix, false)
if node == nil {
return ret
}
if !recurse {
return node.Commands
}
// recurse into node to collect all commands and aliases
for _, child := range node.Children {
c := child.CollectCommands([]string{}, true)
ret = append(ret, c...)
}
// add commands and aliases from current node
ret = append(ret, node.Commands...)
return ret
}
func (r *TrieNode) ToRenderNode() *RenderNode {
ret := &RenderNode{
Name: "",
Command: nil,
Children: nil,
}
childrenMap := make(map[string]*RenderNode)
for _, c := range r.Commands {
childrenMap[c.Description().Name] = &RenderNode{
Name: c.Description().Name,
Command: c,
Children: nil,
}
}
for k, v := range r.Children {
existingNode, ok := childrenMap[k]
newNode := v.ToRenderNode()
newNode.Name = k
if ok {
// merge command
newNode.Command = existingNode.Command
childrenMap[k] = newNode
} else {
childrenMap[k] = newNode
}
}
ret.Children = make([]*RenderNode, 0, len(childrenMap))
for _, v := range childrenMap {
ret.Children = append(ret.Children, v)
}
// sort by name
sort.Slice(ret.Children, func(i, j int) bool {
return ret.Children[i].Name < ret.Children[j].Name
})
return ret
}