-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsafe_trie.go
62 lines (48 loc) · 1.23 KB
/
safe_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
package trie
import "sync"
// safeTrie is safe to use concurrently
type safeTrie struct {
t Trie
mu sync.RWMutex
}
// Safe returns goroutine-safe (thread-safe) version of
// a given trie.
func Safe(trie Trie) Trie {
return &safeTrie{t: trie}
}
// Insert inserts word into the trie and returns
// true if the trie already contains a record.
//
// It is safe to use concurrently.
func (st *safeTrie) Insert(word string) bool {
st.mu.Lock()
defer st.mu.Unlock()
return st.t.Insert(word)
}
// Contains returns true, if the trie contains a word.
//
// It is safe to use concurrently.
func (st *safeTrie) Contains(word string) bool {
st.mu.RLock()
defer st.mu.RUnlock()
return st.t.Contains(word)
}
// StartsWith returns true if there is any word in the trie that starts
// with the given prefix.
func (st *safeTrie) StartsWith(prefix string) bool {
st.mu.RLock()
defer st.mu.RUnlock()
return st.t.StartsWith(prefix)
}
// Finds and returns words by prefix.
func (st *safeTrie) SearchByPrefix(prefix string) []string {
st.mu.RLock()
defer st.mu.RUnlock()
return st.t.SearchByPrefix(prefix)
}
// Size returns the number of keys in the tree.
func (st *safeTrie) Size() int {
st.mu.RLock()
defer st.mu.RUnlock()
return st.t.Size()
}