-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrie.go
113 lines (93 loc) · 2.19 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
package trie
import (
"fmt"
"sync"
)
// Tree contains Trie structure.
type Tree struct {
mu sync.Mutex
children map[byte]*node
}
// New returns new Tree.
func New() *Tree {
return &Tree{
children: make(map[byte]*node),
}
}
// GetPrefixMembers returns a slice of prefix members. Error if the prefix doesn't exist.
func (t *Tree) GetPrefixMembers(path string) (mm []Member, err error) {
// check whether the prefix exists
if !t.PrefixExists(path) {
err = fmt.Errorf("prefix %s doesn't exist", path)
return
}
// manage mutex
t.mu.Lock()
defer t.mu.Unlock()
// get prefix node
// get base node first
key := path[0]
baseNode, baseNodeExists := t.children[key]
if !baseNodeExists {
err = fmt.Errorf("base node %s doesn't exist", string(key))
return
}
// return data if the node is the last one requested
if len(path) == 1 {
mm = baseNode.getAllMembers()
return
}
// going deeper
var n *node
n, err = baseNode.getChildNodeByPath(path[1:len(path)])
if err != nil {
return
}
mm = n.getAllMembers()
return
}
// PrefixExists tells whether the requested prefix exists in the tree.
func (t *Tree) PrefixExists(path string) bool {
// skip if no path provided
if path == "" {
return false
}
// manage mutex
t.mu.Lock()
defer t.mu.Unlock()
// check the base node for prefix
key := path[0]
_, baseNodeExists := t.children[key]
if !baseNodeExists {
return false
}
// return true if the node is the last one requested
if len(path) == 1 {
return true
}
// lookup path children
return t.children[key].lookupPathChildren(path[1:len(path)])
}
// Add adds a new record to the tree.
func (t *Tree) Add(path string, data interface{}) {
// skip if no path provided
if path == "" {
return
}
// manage mutex
t.mu.Lock()
defer t.mu.Unlock()
// make base node if doesn't exist
key := path[0]
_, baseNodeExists := t.children[key]
if !baseNodeExists {
t.children[key] = newEmptyNode(string(key))
}
// if the base node is the last one requested, adding the data right into it
if len(path) == 1 {
t.children[key].data = append(t.children[key].data, data)
return
}
// creating chain children
t.children[key].createPathChildren(string(key), path[1:len(path)], data)
}