-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree_node.go
220 lines (186 loc) · 3.81 KB
/
rbtree_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// source: https://github.com/liyue201/gostl/blob/master/utils/iterator/iterator.go
package odmap
import (
"cmp"
"sync/atomic"
)
type KVisitor[K cmp.Ordered, V any] func(key K, value V) bool
// Color defines node color type
type Color bool
// Define node 's colors
const (
RED = false
BLACK = true
)
// Entry is a tree entry
type Entry[K cmp.Ordered, V any] struct {
expunged *V
parent *Entry[K, V]
left *Entry[K, V]
right *Entry[K, V]
color Color
key K
value *atomic.Pointer[V]
}
// Key returns node's key
func (n *Entry[K, V]) Key() K {
return n.key
}
// Value returns node's value
func (n *Entry[K, V]) Value() V {
p := n.value.Load()
if p == nil {
return empty[V]()
}
return *p
}
func (n *Entry[K, V]) load() (V, bool) {
p := n.value.Load()
if p == nil || p == n.expunged {
return empty[V](), false
}
return *p, true
}
func (n *Entry[K, V]) tryCompareAndSwap(old, new V) bool {
p := n.value.Load()
if p == nil || p == n.expunged || any(*p) != any(old) {
return false
}
nc := new
for {
if n.value.CompareAndSwap(p, &nc) {
return true
}
p = n.value.Load()
if p == nil || p == n.expunged || any(*p) != any(old) {
return false
}
}
}
func (n *Entry[K, V]) unexpungeLocked() bool {
return n.value.CompareAndSwap(n.expunged, nil)
}
func (n *Entry[K, V]) swapLocked(i *V) *V {
return n.value.Swap(i)
}
func (n *Entry[K, V]) tryLoadOrStore(i V) (V, bool, bool) {
p := n.value.Load()
if p == n.expunged {
return empty[V](), false, false
}
if p != nil {
return *p, true, true
}
ic := i
for {
if n.value.CompareAndSwap(nil, &ic) {
return i, false, true
}
p = n.value.Load()
if p == n.expunged {
return empty[V](), false, false
}
if p != nil {
return *p, true, true
}
}
}
func (n *Entry[K, V]) delete() (V, bool) {
for {
p := n.value.Load()
if p == nil || p == n.expunged {
return empty[V](), false
}
if n.value.CompareAndSwap(p, nil) {
return *p, true
}
}
}
func (n *Entry[K, V]) trySwap(i *V) (*V, bool) {
for {
p := n.value.Load()
if p == n.expunged {
return nil, false
}
if n.value.CompareAndSwap(p, i) {
return p, true
}
}
}
func (n *Entry[K, V]) tryExpungeLocked() bool {
p := n.value.Load()
for p == nil {
if n.value.CompareAndSwap(nil, n.expunged) {
return true
}
p = n.value.Load()
}
return p == n.expunged
}
// ---- iterator ----
// Next returns the Entry's successor as an iterator.
func (n *Entry[K, V]) Next() *Entry[K, V] {
return successor(n)
}
// Prev returns the Entry's predecessor as an iterator.
func (n *Entry[K, V]) Prev() *Entry[K, V] {
return presuccessor(n)
}
// successor returns the successor of the Entry
func successor[K cmp.Ordered, V any](x *Entry[K, V]) *Entry[K, V] {
if x.right != nil {
return minimum(x.right)
}
y := x.parent
for y != nil && x == y.right {
x = y
y = x.parent
}
return y
}
// presuccessor returns the presuccessor of the Entry
func presuccessor[K cmp.Ordered, V any](x *Entry[K, V]) *Entry[K, V] {
if x.left != nil {
return maximum(x.left)
}
if x.parent != nil {
if x.parent.right == x {
return x.parent
}
for x.parent != nil && x.parent.left == x {
x = x.parent
}
return x.parent
}
return nil
}
// minimum finds the minimum Entry of subtree n.
func minimum[K cmp.Ordered, V any](n *Entry[K, V]) *Entry[K, V] {
for n.left != nil {
n = n.left
}
return n
}
// maximum finds the maximum Entry of subtree n.
func maximum[K cmp.Ordered, V any](n *Entry[K, V]) *Entry[K, V] {
for n.right != nil {
n = n.right
}
return n
}
// getColor returns the node's color
func getColor[K cmp.Ordered, V any](n *Entry[K, V]) Color {
if n == nil {
return BLACK
}
return n.color
}
func empty[V any]() V {
var e V
return e
}
func newPointerValue[V any](val V) *atomic.Pointer[V] {
p := &atomic.Pointer[V]{}
p.Store(&val)
return p
}