-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
145 lines (127 loc) · 3.14 KB
/
node.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package go_sknn
import (
"sync"
"github.com/golang/geo/s2"
)
type Node[T any] struct {
cellID s2.CellID
values []*Value[T]
children []*Node[T]
isLeaveNode bool
parent *Node[T]
childMutex sync.RWMutex
valuesMutex sync.RWMutex
}
func (n *Node[T]) ValuesCount() []int {
result := make([]int, 0)
for _, child := range n.children {
result = append(result, child.ValuesCount()...)
}
if len(n.values) > 0 {
result = append(result, len(n.values))
}
return result
}
func (n *Node[T]) GetOrCreateChild(key s2.CellID, isLeaveNode bool) *Node[T] {
n.childMutex.RLock()
for _, child := range n.children {
if child.cellID == key {
n.childMutex.RUnlock()
return child
}
}
n.childMutex.RUnlock()
n.childMutex.Lock()
defer n.childMutex.Unlock()
for _, child := range n.children {
if child.cellID == key {
return child
}
}
child := &Node[T]{
cellID: key,
values: []*Value[T]{},
children: make([]*Node[T], 0, 1),
isLeaveNode: isLeaveNode,
parent: n,
childMutex: sync.RWMutex{},
valuesMutex: sync.RWMutex{},
}
n.children = append(n.children, child)
return child
}
func (n *Node[T]) AddChildrenToQueue(point s2.Point, addFunction func(*Node[T], float64)) {
n.childMutex.RLock()
defer n.childMutex.RUnlock()
for _, child := range n.children {
addFunction(child, float64(s2.CellFromCellID(child.cellID).Distance(point)))
}
}
func (n *Node[T]) AddChildrenToQueueInterface(point s2.Point, addFunction func(interface{}, float64)) {
n.childMutex.RLock()
defer n.childMutex.RUnlock()
for _, child := range n.children {
addFunction(child, float64(s2.CellFromCellID(child.cellID).Distance(point)))
}
}
func (n *Node[T]) AddValuesToQueue(point s2.Point, addFunction func(interface{}, float64)) {
n.valuesMutex.RLock()
defer n.valuesMutex.RUnlock()
for _, value := range n.values {
addFunction(value, float64(s2.CellFromCellID(value.cell).Distance(point)))
}
}
func (n *Node[T]) FilerValues(callback func(*Value[T]) bool) bool {
n.valuesMutex.RLock()
defer n.valuesMutex.RUnlock()
for _, value := range n.values {
if callback(value) {
return true
}
}
return false
}
func (n *Node[T]) SetValue(key string, value T, cell s2.CellID) {
n.valuesMutex.Lock()
defer n.valuesMutex.Unlock()
n.values = append(n.values, &Value[T]{key: key, value: value, cell: cell})
}
func (n *Node[T]) IsLeaveNode() bool {
return n.isLeaveNode
}
func (n *Node[T]) RemoveValue(key string) bool {
n.valuesMutex.Lock()
defer n.valuesMutex.Unlock()
foundIndex := -1
for i := range n.values {
if n.values[i].key == key {
foundIndex = i
break
}
}
if foundIndex != -1 {
n.values[foundIndex] = n.values[len(n.values)-1]
n.values = n.values[:len(n.values)-1]
}
return len(n.values) == 0
}
func (n *Node[T]) RemoveChild(id s2.CellID) {
for i, child := range n.children {
if child.cellID == id {
n.children = append(n.children[:i], n.children[i+1:]...)
return
}
}
}
func (n *Node[T]) Prune() bool {
for _, child := range n.children {
if child.isLeaveNode {
return len(n.values) == 0
}
empty := child.Prune()
if empty {
n.RemoveChild(child.cellID)
}
}
return len(n.children) == 0
}