forked from nqd/flat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.go
93 lines (84 loc) · 1.76 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
package flat
import (
"fmt"
"strconv"
)
type TrieNode struct {
children map[string]*TrieNode
sliceDepth int // record depth of slice in case it is slice of slice
value interface{}
}
func (t *TrieNode) Print(delimiter string) {
t.print("", delimiter)
}
func (t *TrieNode) print(parent, delimiter string) {
if t.value != nil {
fmt.Printf("%s: %v\n", parent, t.value)
}
for k, child := range t.children {
if parent != "" {
child.print(parent+delimiter+k, delimiter)
} else {
child.print(k, delimiter)
}
}
}
func (t *TrieNode) insert(parts []string, value interface{}) {
node := t
for i, part := range parts {
if node.children == nil {
node.children = make(map[string]*TrieNode)
}
cnode, ok := node.children[part]
if !ok {
cnode = &TrieNode{}
node.children[part] = cnode
}
node = cnode
if i == len(parts)-1 {
node.value = value
}
}
}
// Start from the bottom to handle slice case
// TODO: support slice of slice when flatten support it
func (t *TrieNode) unflatten() map[string]interface{} {
ret := make(map[string]interface{})
for k, child := range t.children {
ret[k] = child.uf()
}
return ret
}
func (t *TrieNode) uf() interface{} {
if t.value != nil || len(t.children) == 0 {
return t.value
}
isSlice := true
sChildren := make([]*TrieNode, len(t.children))
for k, v := range t.children {
idx, err := strconv.Atoi(k)
if err != nil {
break
}
sChildren[idx] = v
}
for _, v := range sChildren {
if v != nil {
continue
}
isSlice = false
break
}
if isSlice {
ret := make([]interface{}, len(sChildren))
for i, child := range sChildren {
ret[i] = child.uf()
}
return ret
}
ret := make(map[string]interface{})
for k, child := range t.children {
ret[k] = child.uf()
}
return ret
}