-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.go
More file actions
40 lines (31 loc) · 715 Bytes
/
tree.go
File metadata and controls
40 lines (31 loc) · 715 Bytes
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
package coalescent
import (
"sync/atomic"
"unsafe"
"github.com/hashicorp/go-immutable-radix"
)
var emptyTree = iradix.New()
type Tree struct {
ptr unsafe.Pointer // *iradix.Tree
}
func (t *Tree) Clone() Tree {
return Tree{ptr: atomic.LoadPointer(&t.ptr)}
}
func (t Tree) Get() *iradix.Tree {
return (*iradix.Tree)(t.ptr)
}
func (t Tree) GetOrNew() *iradix.Tree {
if tree := t.Get(); tree != nil {
return tree
}
return emptyTree
}
func (t *Tree) Load() *iradix.Tree {
return (*iradix.Tree)(atomic.LoadPointer(&t.ptr))
}
func (t *Tree) Store(tree *iradix.Tree) {
if tree == nil || tree.Len() == 0 {
tree = emptyTree // to reduce garbage
}
atomic.StorePointer(&t.ptr, unsafe.Pointer(tree))
}