-
Notifications
You must be signed in to change notification settings - Fork 2
/
Node.go
92 lines (76 loc) · 2.03 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
package BucketMap
import (
"fmt"
"github.com/g-m-twostay/go-utils/Maps"
"sync"
"sync/atomic"
"unsafe"
)
// node doesn't have to be generic, but removing the type parameter here somehow makes the code run very slow.
type node[K any] struct {
info uint
nx unsafe.Pointer
}
func (cur *node[K]) isRelay() bool {
return cur.info > Maps.MaxArrayLen
}
func (cur *node[K]) Hash() uint {
return Maps.Mask(cur.info)
}
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 *relay[K], nextPtr unsafe.Pointer) bool {
next.Lock()
defer next.Unlock()
next.del = atomic.CompareAndSwapPointer(&cur.nx, nextPtr, next.nx)
return next.del
}
func (cur *node[K]) dangerUnlink(next *node[K]) {
atomic.StorePointer(&cur.nx, next.nx)
}
type value[K any] struct {
node[K]
v unsafe.Pointer
k K
}
func (cur *value[K]) String() string {
return fmt.Sprintf("key: %#v; val: %#v; hash: %d; relay: %t", cur.k, cur.get(), cur.info, cur.isRelay())
}
func (cur *value[K]) set(v unsafe.Pointer) {
atomic.StorePointer(&cur.v, v)
}
func (cur *value[K]) get() unsafe.Pointer {
return atomic.LoadPointer(&cur.v)
}
func (cur *value[K]) swap(v unsafe.Pointer) unsafe.Pointer {
return atomic.SwapPointer(&cur.v, v)
}
// node doesn't have to be generic, but removing the type parameter here somehow makes the code run very slow.
type relay[K any] struct {
node[K]
sync.RWMutex
del bool
}
func (cur *relay[K]) safeLock() bool {
cur.Lock()
return !cur.del
}
func (cur *relay[K]) safeRLock() bool {
cur.RLock()
return !cur.del
}
func (cur *relay[K]) search(k K, at uint, cmp func(K, K) bool) *value[K] {
for left := &cur.node; ; {
if rightB := (*node[K])(left.Next()); rightB == nil || at < rightB.Hash() {
return nil
} else if right := (*value[K])(unsafe.Pointer(rightB)); at == rightB.info && cmp(k, right.k) {
return right
} else {
left = rightB
}
}
}