-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist.go
240 lines (214 loc) · 4.27 KB
/
skiplist.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package goskiplist
import (
"errors"
"math/rand"
"sync"
"sync/atomic"
"time"
)
var (
ErrNotFound = errors.New("not found")
)
const (
DefaultSkipStepSize = 4
DefaultSkipLevel = 16
MaxSkipLevel = 64
)
func init() {
rand.Seed(time.Now().Unix())
}
type Key interface {
~string |
~int | ~int64 | ~int32 | ~int16 | ~int8 |
~uint | ~uint64 | ~uint32 | ~uint16 | ~uint8 |
~float64 | ~float32
}
func NewSkipList[K Key, V any](stepSize, level int) *SkipList[K, V] {
if stepSize < 2 {
stepSize = DefaultSkipStepSize
}
if level < 0 || level > MaxSkipLevel {
level = DefaultSkipLevel
}
sl := SkipList[K, V]{
stepSize: stepSize,
level: level,
lock: &sync.RWMutex{},
head: &Elem[K, V]{
next: make([]*Elem[K, V], level+1),
},
}
return &sl
}
type SkipList[K Key, V any] struct {
stepSize int
level int
lock *sync.RWMutex
length int32
head *Elem[K, V] // must not be nil
}
type Elem[K Key, V any] struct {
next []*Elem[K, V] // skip index list, level0, level1, level2...
// kv
k K // read only
v *V
}
//================================================================
// SkipList
// StepSize return the stepSize of list
func (s *SkipList[K, V]) StepSize() int {
return s.stepSize
}
// Level return the level of list
func (s *SkipList[K, V]) Level() int {
return s.level
}
// Length return the length of list
func (s *SkipList[K, V]) Length() int32 {
return atomic.LoadInt32(&s.length)
}
// randomLevel
func (s *SkipList[K, V]) randomLevel() int {
l := 0
for l < s.level && rand.Intn(s.stepSize) == 0 {
l++
}
return l
}
// Put elem into list
func (s *SkipList[K, V]) Put(k K, v *V) error {
lv := s.randomLevel()
e := Elem[K, V]{
next: make([]*Elem[K, V], lv+1),
k: k,
v: v,
}
pres := make([]*Elem[K, V], lv+1)
s.lock.Lock()
defer s.lock.Unlock()
el := s.head
for i := s.level; i > -1; i-- {
for el.next[i] != nil && e.k >= el.next[i].k {
el = el.next[i]
if e.k == el.k { // just replace
el.v = e.v
return nil
}
}
if i > lv {
continue
}
pres[i] = el
}
for i := lv; i > -1; i-- {
e.next[i] = pres[i].next[i]
pres[i].next[i] = &e
}
atomic.AddInt32(&s.length, 1)
return nil
}
// Find elem from list
func (s *SkipList[K, V]) Find(k K) (*V, error) {
s.lock.RLock()
defer s.lock.RUnlock()
el := s.head
for i := s.level; i > -1; i-- {
for el.next[i] != nil && k >= el.next[i].k {
el = el.next[i]
if k == el.k {
return el.v, nil
}
}
}
return nil, ErrNotFound
}
// FindMin find the min elem from list
func (s *SkipList[K, V]) FindMin() (*V, error) {
s.lock.RLock()
defer s.lock.RUnlock()
if s.head.next[0] == nil {
return nil, ErrNotFound
}
return s.head.next[0].v, nil
}
// FindMax find the max elem from list
func (s *SkipList[K, V]) FindMax() (*V, error) {
s.lock.RLock()
defer s.lock.RUnlock()
el := s.head
for i := s.level; i > -1; i-- {
for el.next[i] != nil {
el = el.next[i]
}
}
if el == s.head {
return nil, ErrNotFound
}
return el.v, nil
}
// Pop elem from list
func (s *SkipList[K, V]) Pop(k K) (*V, error) {
s.lock.Lock()
defer s.lock.Unlock()
el := s.head
var (
has bool
v *V
)
for i := s.level; i > -1; i-- {
for el.next[i] != nil && k >= el.next[i].k {
if k == el.next[i].k {
has = true
v = el.next[i].v
el.next[i] = el.next[i].next[i]
break
}
el = el.next[i]
}
}
if has {
atomic.AddInt32(&s.length, -1)
return v, nil
}
return nil, ErrNotFound
}
// PopMin pop the min elem from list
func (s *SkipList[K, V]) PopMin() (*V, error) {
s.lock.Lock()
defer s.lock.Unlock()
if s.head.next[0] == nil {
return nil, ErrNotFound
}
el := s.head.next[0]
for i := len(el.next) - 1; i > -1; i-- {
s.head.next[i] = el.next[i]
}
atomic.AddInt32(&s.length, -1)
return el.v, nil
}
// PopMax pop the max elem from list
func (s *SkipList[K, V]) PopMax() (*V, error) {
pres := make([]*Elem[K, V], s.level+1)
s.lock.Lock()
defer s.lock.Unlock()
el := s.head
var pre *Elem[K, V]
for i := s.level; i > -1; i-- {
for el.next[i] != nil {
pre = el
el = el.next[i]
}
for pre != nil && pre.next[i].k < el.k {
pre = pre.next[i]
}
pres[i] = pre
}
if el == s.head {
return nil, ErrNotFound
}
for i := len(el.next) - 1; i > -1; i-- {
pres[i].next[i] = nil
}
atomic.AddInt32(&s.length, -1)
return el.v, nil
}