-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist.go
137 lines (114 loc) · 2.86 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
package skiplist
import (
"fmt"
"math/rand"
)
type SkipList struct {
Head *Node
}
type Node struct {
Pointers []*Node
Key int
Val interface{}
}
func New(maxHeight int) *SkipList {
return &SkipList{
Head: &Node{
Pointers: make([]*Node, maxHeight),
},
}
}
func (s *SkipList) Put(key int, val interface{}) *Node {
newNodeHeight := randHeight(len(s.Head.Pointers))
newNode := &Node{
Pointers: make([]*Node, newNodeHeight),
Key: key,
Val: val,
}
node := s.Head
for i := len(s.Head.Pointers); i > 0; i-- {
indexNew := len(newNode.Pointers) - i
indexTest := len(node.Pointers) - i
nodeTest := node.Pointers[indexTest]
for nodeTest != nil && nodeTest.Key <= key {
node = node.Pointers[indexTest]
indexTest = len(node.Pointers) - i
nodeTest = node.Pointers[indexTest]
}
if i <= newNodeHeight {
newNode.Pointers[indexNew] = node.Pointers[indexTest]
node.Pointers[indexTest] = newNode
}
}
return newNode
}
func (s *SkipList) Remove(toRemove *Node) bool {
val := toRemove.Key
removed := false
node := s.Head
for i := len(s.Head.Pointers); i > 0; i-- {
indexTest := len(node.Pointers) - i
nodeTest := node.Pointers[indexTest]
for nodeTest != nil && nodeTest.Key < val {
node = nodeTest
indexTest = len(node.Pointers) - i
nodeTest = node.Pointers[indexTest]
}
// update pointer while handling duplicates
if nodeTest != nil && nodeTest.Key == val {
dupNode := node
dupNodeTest := nodeTest
for dupNodeTest != nil && dupNodeTest.Key == val && dupNodeTest != toRemove {
dupNode = dupNodeTest
indexTest = len(dupNode.Pointers) - i
dupNodeTest = dupNode.Pointers[indexTest]
}
if dupNodeTest == toRemove {
removedNext := len(toRemove.Pointers) - i
dupNode.Pointers[indexTest] = toRemove.Pointers[removedNext]
removed = true
}
}
}
return removed
}
func (s *SkipList) Get(val int) (*Node, bool) {
node := s.Head
for i := len(s.Head.Pointers); i > 0; i-- {
indexTest := len(node.Pointers) - i
nodeTest := node.Pointers[indexTest]
for nodeTest != nil && nodeTest.Key <= val {
node = nodeTest
indexTest = len(node.Pointers) - i
nodeTest = node.Pointers[indexTest]
}
}
return node, node != nil
}
func (s *SkipList) Dump() {
for i := len(s.Head.Pointers) - 1; i >= 0; i-- {
vals := []int{}
for _, v := range s.ToSliceAtHeight(i) {
vals = append(vals, v.Key)
}
fmt.Printf("Height: %d, Count: %d, %v\n", i, len(vals), vals)
}
}
func (s *SkipList) ToSlice() []*Node {
return s.ToSliceAtHeight(0)
}
func (s *SkipList) ToSliceAtHeight(height int) []*Node {
res := []*Node{}
node := s.Head.Pointers[len(s.Head.Pointers)-1-height]
for node != nil {
res = append(res, node)
node = node.Pointers[len(node.Pointers)-1-height]
}
return res
}
func randHeight(maxHeight int) int {
h := 1
for ; rand.Intn(2) == 0 && h < maxHeight; h++ {
}
return h
}