-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist.go
192 lines (169 loc) · 3.33 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
package skiplist
import (
"errors"
"fmt"
"math/rand"
"sync"
"time"
)
type node struct {
key int
value int
forwards []*node
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func newNode(key, value int, level int) *node {
return &node{
key: key,
value: value,
forwards: make([]*node, level+1),
}
}
type SkipList struct {
mu sync.RWMutex
head *node
max_level int
}
var (
MaxLevel int = 64
P float64 = 0.5
)
var (
ErrKeyNotFound = errors.New("key not found")
)
func New() *SkipList {
return &SkipList{
head: newNode(0, 0, MaxLevel+1),
max_level: 0,
}
}
func (sk *SkipList) Insert(key, value int) {
sk.mu.Lock()
defer sk.mu.Unlock()
lvl := randomLevel()
if lvl >= sk.max_level {
sk.max_level = lvl + 1
}
var updates = make([]*node, lvl+1)
now := sk.head
for l := sk.max_level; l >= 1; l-- {
for now.forwards[l] != nil && now.forwards[l].key < key {
now = now.forwards[l]
}
if l <= lvl {
updates[l] = now
}
}
// [now] -> [next]
next := now.forwards[1]
if next != nil && next.key == key {
next.value = value
} else {
newNode := newNode(key, value, lvl)
for u := lvl; u >= 1; u-- {
newNode.forwards[u] = updates[u].forwards[u]
updates[u].forwards[u] = newNode
}
}
}
func (sk *SkipList) Update(key, value int) error {
sk.mu.Lock()
defer sk.mu.Unlock()
now := sk.head.forwards[sk.max_level]
for l := sk.max_level; l >= 1; l-- {
for now.forwards != nil && now.forwards[l].key < key {
now = now.forwards[l]
}
}
// [now] -> [next]
next := now.forwards[1]
if next.key == key {
next.value = value
return nil
}
return ErrKeyNotFound
}
func (sk *SkipList) Search(key int) (value int, err error) {
sk.mu.RLock()
defer sk.mu.RUnlock()
now := sk.head
for l := sk.max_level; l >= 1; l-- {
for now.forwards[l] != nil && now.forwards[l].key < key {
now = now.forwards[l]
}
}
// [now] -> [next]
next := now.forwards[1]
if next != nil && next.key == key {
return next.value, nil
}
return 0, ErrKeyNotFound
}
func randomLevel() int {
lvl := 1
for lvl < MaxLevel && rand.Float64() < P {
lvl += 1
}
return lvl
}
func print(sk *SkipList) {
head := sk.head
for head != nil {
fmt.Printf("level: %d [%d,%d]\n", len(head.forwards), head.key, head.value)
head = head.forwards[1]
}
}
func printSimulate(sk *SkipList) {
fmt.Println("print skip list :")
defer fmt.Println("done")
var matrix [][]string = make([][]string, sk.max_level+1)
var length int
{
head := sk.head.forwards[1]
for head != nil {
length++
head = head.forwards[1]
}
for i := range matrix {
matrix[i] = make([]string, length+1)
}
}
length = 0
{
head := sk.head
for head != nil {
for h := 0; h <= sk.max_level; h++ {
var format = "%2d "
var v string
if h == 0 {
v = fmt.Sprintf(format, head.key)
} else if h < len(head.forwards) {
v = " | "
} else if h == len(head.forwards)+1 {
v = fmt.Sprintf(format, head.value)
} else if h == len(head.forwards) {
v = " = "
} else {
v = " "
}
matrix[h][length] = v
}
head = head.forwards[1]
length++
}
}
{
length := sk.max_level + 1
for i := 0; i < length/2; i++ {
matrix[i], matrix[length-1-i] = matrix[length-1-i], matrix[i]
}
}
for _, row := range matrix {
for _, b := range row {
fmt.Printf("%s", b)
}
fmt.Printf("\n")
}
}