-
Notifications
You must be signed in to change notification settings - Fork 1
/
skiplist.go
174 lines (157 loc) · 3.8 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
package dbengine
import (
"fmt"
"math/rand"
"time"
)
type skipList struct {
head *node
height int // how many levels are there
size int // how many nodes are there
prob float32 // a probability used to determine up to what level should a new node be created
sentinel *node
}
type node struct {
key string
value []byte
forwardNodeAtLevel map[int]*node // tracks the next node of this node at different levels
}
func newSkipList() *skipList {
sentinel := newNode("", []byte{})
return &skipList{
head: sentinel,
height: 1,
size: 0,
prob: 0.25, // use hardcoded probability for now
sentinel: sentinel,
}
}
func newNode(key string, value []byte) *node {
return &node{
key: key,
value: value,
forwardNodeAtLevel: make(map[int]*node),
}
}
func (s *skipList) randomLevel() int {
lvl := 0
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
for {
if r.Float32() <= s.prob {
break
}
lvl++
}
return lvl
}
func (s *skipList) search(key string) *node {
curLevel := s.height - 1
curNode := s.head
// empty skip list OR direct match
if curNode == nil || curNode.key == key {
return curNode
}
for {
nextNode, found := curNode.forwardNodeAtLevel[curLevel]
if !found {
// if it's the last node
if curLevel == 0 {
return nil
}
// if there's no next node, scan down
curLevel--
continue
}
if nextNode.key >= key {
if nextNode.key == key {
return nextNode
}
if curLevel == 0 {
return nil
}
// when next node's key is greater than key to search for
// going down one level
curLevel--
} else {
// scan forward
curNode = nextNode
}
}
}
func (s *skipList) upsert(key string, value []byte) *node {
curNode := s.head
curLevel := s.height - 1
newNode := newNode(key, value)
// tracks the last node we search through at each level, since when we add the new node to those levels
// the anchor nodes will be the one that connects to it
updateAnchors := make([]*node, s.height, s.height)
for {
nextNode, found := curNode.forwardNodeAtLevel[curLevel]
if !found {
updateAnchors[curLevel] = curNode
// if we are at the last node, and the new node so far has the "biggest" key
// insert the new node at the end
if curLevel == 0 {
s.insertNewNode(newNode, updateAnchors)
return newNode
}
// otherwise scan downward
curLevel--
continue
}
if nextNode.key >= key {
if nextNode.key == key {
nextNode.value = value
return nextNode
}
updateAnchors[curLevel] = curNode
// once we are at level 0, insert the new node
if curLevel == 0 {
s.insertNewNode(newNode, updateAnchors)
return newNode
}
// otherwise scan downward
curLevel--
} else {
curNode = nextNode
}
}
}
func (s *skipList) insertNewNode(newNode *node, updateAnchors []*node) {
lvl := s.randomLevel()
// if the generated level is greater than height, create new levels in between for update anchor
if lvl >= s.height {
// grow one level at a time
lvl = s.height + 1
for i := s.height; i <= lvl; i++ {
updateAnchors = append(updateAnchors, s.head)
}
// update height
s.height = lvl + 1
}
// add new node at each level from 0 to generated lvl
for level, anchorNode := range updateAnchors {
if level > lvl {
break
}
oldNext := anchorNode.forwardNodeAtLevel[level]
anchorNode.forwardNodeAtLevel[level] = newNode
if oldNext != nil {
newNode.forwardNodeAtLevel[level] = oldNext
}
}
s.size++
}
func (s *skipList) prettyPrint() {
for lvl := s.height; lvl >= 0; lvl-- {
for curNode := s.head; curNode != nil; curNode = curNode.forwardNodeAtLevel[lvl] {
if curNode != s.sentinel {
fmt.Printf("%s - ", curNode.key)
} else {
fmt.Printf("sentinel - ")
}
}
fmt.Println()
}
}