-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradix_tree.go
77 lines (63 loc) · 1.3 KB
/
radix_tree.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
// Copyright 2016 David Lavieri. All rights reserved.
// Use of this source code is governed by a MIT License
// License that can be found in the LICENSE file.
package goradix
import (
"errors"
"sync"
)
// ErrNoMatchFound self-explanatory
var ErrNoMatchFound = errors.New("no match found")
// Radix Radix
type Radix struct {
Path []byte
nodes []*Radix
master bool
value interface{}
leaf, key bool
mu *sync.RWMutex
ts bool
}
// New return a Radix Tree
// ts bool - Thread Safe
func New(ts bool) *Radix {
return &Radix{master: true, mu: &sync.RWMutex{}, ts: ts}
}
// ----------------------- Basic ------------------------ //
// Set a value to the Radix Tree node
func (r *Radix) set(v interface{}) {
r.value = v
}
// Get a value from Radix Tree node
func (r *Radix) getNonBlocking() interface{} {
return r.value
}
// Get a value from Radix Tree node
func (r *Radix) get() interface{} {
r.mu.RLock()
v := r.value
r.mu.RUnlock()
return v
}
// ----------------------- Locks ------------------------ //
// in order to make thread safety optional
func (r *Radix) lock() {
if r.ts {
r.mu.Lock()
}
}
func (r *Radix) unlock() {
if r.ts {
r.mu.Unlock()
}
}
func (r *Radix) rLock() {
if r.ts {
r.mu.RLock()
}
}
func (r *Radix) rUnlock() {
if r.ts {
r.mu.RUnlock()
}
}