-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.h
211 lines (147 loc) · 4.08 KB
/
List.h
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
#include <vector>
#include <random>
#include <iostream>
/*
Scarpaci Edoardo Alessandro M-Z 1000001781
*/
template <typename T,int MAXLVL = 32>
class Node_Skip {
T _Key;
Node_Skip* _NextNodes[MAXLVL+1];
//std::vector<Node_Skip*> _NextNodes;
public:
Node_Skip() {
for (int i = 0; i < MAXLVL; i++) {
_NextNodes[i] = nullptr;
}
}
Node_Skip(T& key) :_Key(key){
for (int i = 0; i < MAXLVL; i++) {
_NextNodes[i] = nullptr;
}
}
void setNext(Node_Skip* next , int level) {
if (level + 1 > _NextNodes.size()) {
_NextNodes.resize(level + 1);
}
_NextNodes[level] = next;
}
Node_Skip* getNext(int level) {
if (level > MAXLVL) {
std::cout << "Accessing illegal Zone " << level << " " <<_NextNodes.size();
}
return _NextNodes[level];
}
T getKey() const {
return _Key;
}
};
template <typename T,int MAXLVL = 32>
class SkipList {
private:
using Node = Node_Skip<T, MAXLVL>;
void _inizialize() { //Collega la sentinella testa in ogni livello con la sentinella coda NIL
_Head = new Node();
for (int i = 0; i < MAXLVL; i++)
_Head->setNext(_NIL, i);
}
int _randomLevel() {
int level = 0;
float value = _Distribution(_Generator);
while (value <= _Probability && level < MAXLVL) {
value = _Distribution(_Generator);
level++;
}
return level;
}
Node* _search(T key) {
memset(_PathSearched, 0, sizeof(Node*) * MAXLVL);
Node* currentNode = _Head;
for (int level = _CurrentLvL; level >= 0; level--) {
//Node* nextNode = currentNode->getNext(level);
while (currentNode->getNext(level) != _NIL && currentNode->getNext(level)->getKey() < key) {
currentNode = currentNode->getNext(level);
}
_PathSearched[level] = currentNode;
}
currentNode = currentNode->getNext(0);
return currentNode;
}
public:
SkipList():_Probability(0.5f), _CurrentLvL(0) {
_inizialize();
_Distribution = std::uniform_real_distribution<float>(0.0, 1);
}
SkipList& insert(T key) {
Node* x = _search(key);
int levelOfNode = _randomLevel();
if (levelOfNode > _CurrentLvL) {
for (int i = _CurrentLvL + 1; i <=levelOfNode; i++) {
_PathSearched[i] = _Head;
}
}
_CurrentLvL = levelOfNode;
}
x = new Node(key);
for (int i = 0; i <= levelOfNode; i++) {
x->setNext(_PathSearched[i]->getNext(i), i);
_PathSearched[i]->setNext(x,i);
}
return *this;
}
SkipList& erase(T key) {
Node* nodeToErase = _search(key);
if (nodeToErase->getKey() == key) {
for (int i = 0; i <= _CurrentLvL; i++) {
if (_PathSearched[i]->getNext(i) != nodeToErase)
break;
_PathSearched[i]->setNext(nodeToErase->getNext(i),i);
}
}
delete nodeToErase;
while (_CurrentLvL > 0 && _Head->getNext(_CurrentLvL) == _NIL)
_CurrentLvL--;
return* this;
}
bool search(T key) {
Node* x = _search(key);
return !(x == _NIL || key != x->getKey());
}
T min() {
Node* firstNode = _Head->getNext(0);
if (firstNode != _NIL) return T(firstNode->getKey());
return T();
}
T max() {
Node* currentNode = _Head;
for (int level = _CurrentLvL;level >= 0; level--) {
while (currentNode->getNext(level) != _NIL) {
currentNode = currentNode->getNext(level);
}
}
if (currentNode != _Head) return T(currentNode->getKey());
return T();
}
void printLevels() {
Node* currentNode = nullptr;
for (int i = 0; i <= _CurrentLvL; i++) {
currentNode = _Head->getNext(i);
std::cout << "L" << i;
while (currentNode != _NIL) {
std::cout << " " << currentNode->getKey();
currentNode = currentNode->getNext(i);
}
std::cout << std::endl;
}
}
int currentLevel() const { return _CurrentLvL; }
int maxLvL ()const{ return MAXLVL; }
private:
Node* _Head;
Node* _NIL;
Node* _PathSearched[MAXLVL];
const float _Probability;
int _CurrentLvL;
std::default_random_engine _Generator;
std::uniform_real_distribution<float> _Distribution;
};