-
Notifications
You must be signed in to change notification settings - Fork 1
/
skip_list.cpp
388 lines (315 loc) · 12.5 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
Implements the Concurrent Skip list data structure with insert, delete, search and range operations
*/
#include <iostream>
#include <math.h>
#include <limits>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include "skip_list.h"
#define INT_MINI numeric_limits<int>::min()
#define INT_MAXI numeric_limits<int>::max()
static int max_level;
/**
Constructor
*/
SkipList::SkipList(int max_elements, float prob){
max_level = (int) round(log(max_elements) / log(1/prob)) - 1;
head = new Node(INT_MINI, max_level);
tail = new Node(INT_MAXI, max_level);
for (size_t i = 0; i < head->next.size(); i++) {
head->next[i] = tail;
}
}
/**
Finds the predecessors and successors at each level of where a given key exists or might exist.
Updates the references in the vector using pass by reference.
Returns -1 if not the key does not exist.
*/
int SkipList::find(int key, vector<Node*> &predecessors, vector<Node*> &successors) {
int found = -1;
Node *prev = head;
for (int level = max_level; level >= 0; level--){
Node *curr = prev->next[level];
while (key > curr->get_key()){
prev = curr;
curr = prev->next[level];
}
if(found == -1 && key == curr->get_key()){
found = level;
}
predecessors[level] = prev;
successors[level] = curr;
}
return found;
}
/**
Randomly generates a number and increments level if number less than or equal to 0.5
Once more than 0.5, returns the level or available max level.
This decides until which level a new Node is available.
*/
int SkipList::get_random_level() {
int l = 0;
while(static_cast <float> (rand()) / static_cast <float> (RAND_MAX) <= 0.5){
l++;
}
return l > max_level ? max_level : l;
}
/**
Inserts into the Skip list at the appropriate place using locks.
Return if already exists.
*/
bool SkipList::add(int key, string value) {
// Get the level until which the new node must be available
int top_level = get_random_level();
// Initialization of references of the predecessors and successors
vector<Node*> preds(max_level + 1);
vector<Node*> succs(max_level + 1);
for (size_t i = 0; i < preds.size(); i++){
preds[i] = NULL;
succs[i] = NULL;
}
// Keep trying to insert the element into the list. In case predecessors and successors are changed,
// this loop helps to try the insert again
while(true){
// Find the predecessors and successors of where the key must be inserted
int found = find(key, preds, succs);
// If found and marked, wait and continue insert
// If found and unmarked, wait until it is fully_linked and return. No insert needed
// If not found, go ahead with insert
if(found != -1){
Node* node_found = succs[found];
if(!node_found->marked){
while(! node_found->fully_linked){
}
return false;
}
continue;
}
// Store all the Nodes which lock we acquire in a map
// Map used so that we don't try to acquire lock to a Node we have already acquired
// This may happen when we have the same predecessor at different levels
map<Node*, int> locked_nodes;
// Traverse the skip list and try to acquire the lock of predecessor at every level
try{
Node* pred;
Node* succ;
// Used to check if the predecessor and successors are same from when we tried to read them before
bool valid = true;
for (int level = 0; valid && (level <= top_level); level++){
pred = preds[level];
succ = succs[level];
// If not already acquired lock, then acquire the lock
if(!(locked_nodes.count( pred ))){
pred->lock();
locked_nodes.insert(make_pair(pred, 1));
}
// If predecessor marked or if the predecessor and successors change, then abort and try again
valid = !(pred->marked.load(std::memory_order_seq_cst)) && !(succ->marked.load(std::memory_order_seq_cst)) && pred->next[level]==succ;
}
// Conditons are not met, release locks, abort and try again.
if(!valid){
for (auto const& x : locked_nodes){
x.first->unlock();
}
continue;
}
// All conditions satisfied, create the Node and insert it as we have all the required locks
Node* new_node = new Node(key, value, top_level);
// Update the predecessor and successors
for (int level = 0; level <= top_level; level++){
new_node->next[level] = succs[level];
}
for (int level = 0; level <= top_level; level++){
preds[level]->next[level] = new_node;
}
// Mark the node as completely linked.
new_node->fully_linked = true;
// Release lock of all the nodes held once insert is complete
for (auto const& x : locked_nodes){
x.first->unlock();
}
return true;
}catch(const std::exception& e){
// If any exception occurs during the above insert, release locks of the held nodes and try again.
std::cerr << e.what() << '\n';
for (auto const& x : locked_nodes){
x.first->unlock();
}
}
}
}
/**
Performs search to find if a node exists.
Return value if the key found, else return empty
*/
string SkipList::search(int key){
// Finds the predecessor and successors
vector<Node*> preds(max_level + 1);
vector<Node*> succs(max_level + 1);
for (size_t i = 0; i < preds.size(); i++){
preds[i] = NULL;
succs[i] = NULL;
}
int found = find(key, preds, succs);
// If not found return empty.
if(found == -1){
return "";
}
Node *curr = head;
for (int level = max_level; level >= 0; level--){
while (curr->next[level] != NULL && key > curr->next[level]->get_key()){
curr = curr->next[level];
}
}
curr = curr->next[0];
// If found, unmarked and fully linked, then return value. Else return empty.
if ((curr != NULL) && (curr->get_key() == key) && succs[found]->fully_linked && !succs[found]->marked){
return curr->get_value();
}else {
return "";
}
}
/**
Deletes from the Skip list at the appropriate place using locks.
Return if key doesn’t exist in the list.
*/
bool SkipList::remove(int key){
// Initialization
Node* victim = NULL;
bool is_marked = false;
int top_level = -1;
// Initialization of references of the predecessors and successors
vector<Node*> preds(max_level + 1);
vector<Node*> succs(max_level + 1);
for (size_t i = 0; i < preds.size(); i++){
preds[i] = NULL;
succs[i] = NULL;
}
// Keep trying to delete the element from the list. In case predecessors and successors are changed,
// this loop helps to try the delete again
while(true){
// Find the predecessors and successors of where the key to be deleted
int found = find(key, preds, succs);
// If found, select the node to delete. else return
if(found != -1){
victim = succs[found];
}
// If node not found and the node to be deleted is fully linked and not marked return
if(is_marked |
(found != -1 &&
(victim->fully_linked && victim->top_level == found && !(victim->marked))
)
){
// If not marked, the we lock the node and mark the node to delete
if(!is_marked){
top_level = victim->top_level;
victim->lock();
if(victim->marked){
victim->unlock();
return false;
}
victim->marked = true;
is_marked = true;
}
// Store all the Nodes which lock we acquire in a map
// Map used so that we don't try to acquire lock to a Node we have already acquired
// This may happen when we have the same predecessor at different levels
map<Node*, int> locked_nodes;
// Traverse the skip list and try to acquire the lock of predecessor at every level
try{
Node* pred;
//Node* succ;
// Used to check if the predecessors are not marked for delete and if the predecessor next is the node we are trying to delete or if it is changed.
bool valid = true;
for(int level = 0; valid && (level <= top_level); level++){
pred = preds[level];
// If not already acquired lock, then acquire the lock
if(!(locked_nodes.count( pred ))){
pred->lock();
locked_nodes.insert(make_pair(pred, 1));
}
// If predecessor marked or if the predecessor's next has changed, then abort and try again
valid = !(pred->marked) && pred->next[level] == victim;
}
// Conditons are not met, release locks, abort and try again.
if(!valid){
for (auto const& x : locked_nodes){
x.first->unlock();
}
continue;
}
// All conditions satisfied, delete the Node and link them to the successors appropriately
for(int level = top_level; level >= 0; level--){
preds[level]->next[level] = victim->next[level];
}
victim->unlock();
// delete victim;
// Delete is completed, release the locks held.
for (auto const& x : locked_nodes){
x.first->unlock();
}
return true;
}catch(const std::exception& e){
// If any exception occurs during the above delete, release locks of the held nodes and try again.
for (auto const& x : locked_nodes){
x.first->unlock();
}
}
}else{
return false;
}
}
}
/**
Searches for the start_key in the skip list by traversing once we reach a point closer to start_key
reaches to level 0 to find all keys between start_key and end_key. If search exceeds end, then abort
Updates and returns the key value pairs in a map.
*/
map<int, string> SkipList::range(int start_key, int end_key){
map<int, string> range_output;
if(start_key > end_key){
return range_output;
}
Node *curr = head;
for (int level = max_level; level >= 0; level--){
while (curr->next[level] != NULL && start_key > curr->next[level]->get_key()){
if(curr->get_key() >= start_key && curr->get_key() <= end_key){
range_output.insert(make_pair(curr->get_key(), curr->get_value()));
}
curr = curr->next[level];
}
}
while(curr != NULL && end_key >= curr->get_key()){
if(curr->get_key() >= start_key && curr->get_key() <= end_key){
range_output.insert(make_pair(curr->get_key(), curr->get_value()));
}
curr = curr->next[0];
}
return range_output;
}
/**
Display the skip list in readable format
*/
void SkipList::display(){
for (int i = 0; i <= max_level; i++) {
Node *temp = head;
int count = 0;
if(!(temp->get_key() == INT_MINI && temp->next[i]->get_key() == INT_MAXI)){
printf("Level %d ", i);
while (temp != NULL){
printf("%d -> ", temp->get_key());
temp = temp->next[i];
count++;
}
cout<<endl;
}
if(count == 3) break;
}
printf("---------- Display done! ----------\n\n");
}
SkipList::SkipList(){
}
SkipList::~SkipList(){
}