-
Notifications
You must be signed in to change notification settings - Fork 19
/
skipset.go
218 lines (195 loc) · 5.37 KB
/
skipset.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
// Package skipset is a high-performance, scalable, concurrent-safe set based on skip-list.
// In the typical pattern(100000 operations, 90%CONTAINS 9%Add 1%Remove, 8C16T), the skipset
// up to 15x faster than the built-in sync.Map.
//
//go:generate go run gen.go
package skipset
import "math"
// New returns an empty skip set in ascending order.
func New[T ordered]() *OrderedSet[T] {
var t T
h := newOrderedNode(t, maxLevel)
h.flags.SetTrue(fullyLinked)
return &OrderedSet[T]{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewDesc returns an empty skip set in descending order.
func NewDesc[T ordered]() *OrderedSetDesc[T] {
var t T
h := newOrderedNodeDesc(t, maxLevel)
h.flags.SetTrue(fullyLinked)
return &OrderedSetDesc[T]{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewFunc returns an empty skip set in ascending order.
//
// Note that the less function requires a strict weak ordering,
// see https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings,
// or undefined behavior will happen.
func NewFunc[T any](less func(a, b T) bool) *FuncSet[T] {
var t T
h := newFuncNode(t, maxLevel)
h.flags.SetTrue(fullyLinked)
return &FuncSet[T]{
header: h,
highestLevel: defaultHighestLevel,
less: less,
}
}
// NewString returns an empty skip set in ascending order.
func NewString() *StringSet {
h := newStringNode("", maxLevel)
h.flags.SetTrue(fullyLinked)
return &StringSet{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewStringDesc returns an empty skip set in descending order.
func NewStringDesc() *StringSetDesc {
h := newStringNodeDesc("", maxLevel)
h.flags.SetTrue(fullyLinked)
return &StringSetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
func isNaNf32(x float32) bool {
return x != x
}
// NewFloat32 returns an empty skip set in ascending order.
func NewFloat32() *FuncSet[float32] {
return NewFunc(func(a, b float32) bool {
return a < b || (isNaNf32(a) && !isNaNf32(b))
})
}
// NewFloat32Desc returns an empty skip set in descending order.
func NewFloat32Desc() *FuncSet[float32] {
return NewFunc(func(a, b float32) bool {
return a > b || (isNaNf32(a) && !isNaNf32(b))
})
}
// NewFloat64 returns an empty skip set in ascending order.
func NewFloat64() *FuncSet[float64] {
return NewFunc(func(a, b float64) bool {
return a < b || (math.IsNaN(a) && !math.IsNaN(b))
})
}
// NewFloat64Desc returns an empty skip set in descending order.
func NewFloat64Desc() *FuncSet[float64] {
return NewFunc(func(a, b float64) bool {
return a > b || (math.IsNaN(a) && !math.IsNaN(b))
})
}
// NewInt returns an empty skip set in ascending order.
func NewInt() *IntSet {
h := newIntNode(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &IntSet{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewIntDesc returns an empty skip set in descending order.
func NewIntDesc() *IntSetDesc {
h := newIntNodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &IntSetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewInt64 returns an empty skip set in ascending order.
func NewInt64() *Int64Set {
h := newInt64Node(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Int64Set{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewInt64Desc returns an empty skip set in descending order.
func NewInt64Desc() *Int64SetDesc {
h := newInt64NodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Int64SetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewInt32 returns an empty skip set in ascending order.
func NewInt32() *Int32Set {
h := newInt32Node(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Int32Set{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewInt32Desc returns an empty skip set in descending order.
func NewInt32Desc() *Int32SetDesc {
h := newInt32NodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Int32SetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUint64 returns an empty skip set in ascending order.
func NewUint64() *Uint64Set {
h := newUint64Node(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Uint64Set{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUint64Desc returns an empty skip set in descending order.
func NewUint64Desc() *Uint64SetDesc {
h := newUint64NodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Uint64SetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUint32 returns an empty skip set in ascending order.
func NewUint32() *Uint32Set {
h := newUint32Node(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Uint32Set{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUint32Desc returns an empty skip set in descending order.
func NewUint32Desc() *Uint32SetDesc {
h := newUint32NodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &Uint32SetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUint returns an empty skip set in ascending order.
func NewUint() *UintSet {
h := newUintNode(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &UintSet{
header: h,
highestLevel: defaultHighestLevel,
}
}
// NewUintDesc returns an empty skip set in descending order.
func NewUintDesc() *UintSetDesc {
h := newUintNodeDesc(0, maxLevel)
h.flags.SetTrue(fullyLinked)
return &UintSetDesc{
header: h,
highestLevel: defaultHighestLevel,
}
}