-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip_list.cpp
201 lines (172 loc) · 3.93 KB
/
skip_list.cpp
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
#include <iostream>
constexpr int MAX_LEVEL = 4;
template<class T>
struct Node
{
T Key;
Node** Next;
};
template<class T>
class SkipList
{
public:
SkipList();
~SkipList();
constexpr bool IsEmpty() const;
T* SkipListSearch(const T&);
int ChooseLevel();
void ChoosePowers();
void SkipListInsert(const T&);
private:
Node<T>* root[MAX_LEVEL];
int powers[MAX_LEVEL];
};
template<class T>
SkipList<T>::SkipList()
{
for(int i = 0; i < MAX_LEVEL; i++)
{
root[i] = 0;
}
}
template<class T>
SkipList<T>::~SkipList()
{
}
template<class T>
constexpr bool SkipList<T>::IsEmpty() const
{
return root[0] == 0;
}
template<class T>
void SkipList<T>::ChoosePowers()
{
powers[MAX_LEVEL -1] = (2 << (MAX_LEVEL - 1)) - 1;
for(int i = MAX_LEVEL - 2, j = 0; i >= 0; i--, j++)
{
powers[i] = powers[i + 1] - (2 << j);
}
}
template<class T>
int SkipList<T>::ChooseLevel()
{
int i, r = rand() % powers[MAX_LEVEL - 1] + 1;
for(i = 1; i < MAX_LEVEL; i++)
{
if(r < powers[i])
{
return i - 1; // return a level < the highest level;
}
}
return i - 1; // return the highest level;
}
template<class T>
T* SkipList<T>::SkipListSearch(const T& key)
{
if(isEmpty())
{
return 0;
}
nodePtr previous, current;
int level;
for(level = MAX_LEVEL - 1; level >= 0 && !root[level]; level--) // find the highest non-null level
{
}
previous = current = root[level];
while(true)
{
if(key == current->key) // success if equal;
{
return ¤t->key;
}
else if(key < current->key) // if smaller, go down
{
if(level == 0) // if possible,
{
return 0;
}
else if(current == root[level]) // by one level
{
current = root[--level]; // starting from the predecessor which can be the root
}
else
{
current = *(previous->next + --level);
}
}
else // if greater, go to the next non-null node on the same level
{ // or to a list on a lower level;
previous = current;
if(*(current->next + level) != 0)
{
current = *(current->next + level);
}
else
{
for(level--; level >= 0 && *(current->next + level) == 0; level--)
{
}
if(level >= 0)
{
current = *(current->next + level);
}
else
{
return 0;
}
}
}
}
}
template<class T>
void SkipList<T>::SkipListInsert(const T& key)
{
Node<T>* current[MAX_LEVEL], previous[MAX_LEVEL], newNode;
int level, i;
current[MAX_LEVEL - 1] = root[MAX_LEVEL - 1];
previous[MAX_LEVEL - 1] = 0;
for(level = MAX_LEVEL - 1; level >= 0; level--)
{
while(current[level] && current[level]->key < key) // go to the next
{
previous[level] = current[level]; // if smaller
current[level] = *(current[level]->next + level);
}
if(current[level] && current[level]->key == key) // don't include
{
return; // duplicates
}
if(level > 0) // go one level down
{
if(previous[level] == 0) // if not the lowest
{
current[level - 1] = root[level - 1]; // level, using a link
previous[level - 1] = 0; // either from the root
}
else // or from the predecessor
{
current[level - 1] = *(previous[level]->next + level-1);
previous[level - 1] = previous[level];
}
}
}
level = ChooseLevel(); // generate randomly level for newNode;
newNode = new SkipListNode<T>;
newNode->next = new nodePtr[sizeof(nodePtr) * (level + 1)];
newNode->key = key;
for(i = 0; i < level; i++) // initialize next fields of newNode and reset to newNode
{ // either fields of the root or next fields of newNode's predecessors
*(newNode->next + i) = current[i];
if(previous[i] == 0)
{
root[i] = newNode;
}
else
{
*(previous[i]->next + i) = newNode;
}
}
}
int main()
{
}