-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipList.hpp
85 lines (65 loc) · 1.89 KB
/
SkipList.hpp
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
#ifndef SKIPLIST_HPP_
#define SKIPLIST_HPP_
#include <iostream>
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include "Key.hpp"
#include "DataStructure.hpp"
using namespace std;
class SkipListNode;
class SkipListNode: public Key {
public:
SkipListNode(const Key& key, int height):Key(key),m_links(height) {
assert (height>0);
int i;
for (i=0; i<height; i++) {
m_links[i] = NULL;
}
}
virtual ~SkipListNode() {}
//retuns the hight of this node
unsigned int height() {
return m_links.size();
};
//returns pointer to the next node at a given level
SkipListNode* nextAtLevel(unsigned int level) {
assert (level >=0 && level < m_links.size());
return m_links[level];
};
//setup the pointer at the next node at a given level
void setNextAtLevel(unsigned int level, SkipListNode* next) {
assert (level >=0 && level < m_links.size());
m_links[level] = next;
}
private:
SkipListNode() {}
vector<SkipListNode*> m_links;
};
class SkipList : public DataStructure {
public:
SkipList() {}
SkipList(int maxHeight) {
init(maxHeight);
}
virtual ~SkipList();
void init(int maxHeight);
// ADD FUNCTIONS
int add(const Key& key, bool verbose=false);
unsigned int randHeight();
int add(SkipListNode* target, SkipListNode* newNode, unsigned int level);
// FIND FUNCTIONS
int find(const Key &key, bool verbose=false);
SkipListNode* find(SkipListNode* target, const Key& key, unsigned int level);
//DEL FUNCTIONS
int del(const Key& key, bool verbose= false);
SkipListNode* del(SkipListNode* target, const Key& key, unsigned int level);
//DUMP FUNCTION
void dump(char sep = '\n');
int elements;
private:
SkipListNode* m_head;
unsigned int m_maxHeight;
};
#endif /*SKIPLIST_HPP_*/