-
Notifications
You must be signed in to change notification settings - Fork 2
/
Node.go
75 lines (61 loc) · 1.57 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
package IntMap
import (
"fmt"
"github.com/g-m-twostay/go-utils/Maps"
"sync/atomic"
"unsafe"
)
type node[K comparable] struct {
k K
info uint
v unsafe.Pointer
nx unsafe.Pointer
}
func (cur *node[K]) Hash() uint {
return Maps.Mask(cur.info)
}
func (cur *node[K]) isRelay() bool {
return cur.info > Maps.MaxArrayLen
}
func (cur *node[K]) lock() *Maps.FlagLock {
return (*Maps.FlagLock)(cur.v)
}
func (cur *node[K]) Next() unsafe.Pointer {
return atomic.LoadPointer(&cur.nx)
}
func (cur *node[K]) dangerLink(oldRight, newRight unsafe.Pointer) bool {
return atomic.CompareAndSwapPointer(&cur.nx, oldRight, newRight)
}
func (cur *node[K]) unlinkRelay(next *node[K], nextPtr unsafe.Pointer) bool {
t := next.lock()
t.Lock()
defer t.Unlock()
t.Del = atomic.CompareAndSwapPointer(&cur.nx, nextPtr, next.nx)
return t.Del
}
func (cur *node[K]) dangerUnlink(next *node[K]) {
atomic.StorePointer(&cur.nx, next.nx)
}
func (cur *node[K]) search(k K, at uint) *node[K] {
for left := cur; ; {
if right := (*node[K])(left.Next()); right == nil || at < right.Hash() {
return nil
} else if at == right.info && k == right.k {
return right
} else {
left = right
}
}
}
func (cur *node[K]) set(v unsafe.Pointer) {
atomic.StorePointer(&cur.v, v)
}
func (cur *node[K]) get() unsafe.Pointer {
return atomic.LoadPointer(&cur.v)
}
func (cur *node[K]) swap(v unsafe.Pointer) unsafe.Pointer {
return atomic.SwapPointer(&cur.v, v)
}
func (cur *node[K]) String() string {
return fmt.Sprintf("key: %#v; val: %#v; info: %d; relay: %t", cur.k, cur.get(), cur.Hash(), cur.isRelay())
}