-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathSkipList.js
193 lines (169 loc) · 4.16 KB
/
SkipList.js
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
/**
* 跳跃链表(SkipList)
*
* @related
* http://lightthewoods.me/2017/04/25/index-data-structure-besides-books/
* http://lightthewoods.me/2017/04/25/index-data-structure-besides-books/
*/
function makeNode(level, key, value) {
var node = new Array(4 + level);
node[0] = key;
node[1] = value;
return node;
}
function nodesEqual(left, right) {
if ((left === undefined) && right) return false;
if ((right === undefined) && left) return false;
if (left[0] !== right[0]) return false;
if (left[1] !== right[1]) return false;
if (left[2] !== right[2]) return false;
if (left[3] !== right[3]) return false;
return true;
}
var P = 1 / Math.E;
var NIL = makeNode(-1);
class SkipList {
constructor(maxsize) {
this.maxsize = maxsize || 65535;
this.maxlevel = Math.round(Math.log(this.maxsize, 2));
this.level = 0;
this.head = makeNode(this.maxlevel);
this.tail = NIL;
for (var i = 0; i < this.maxlevel; i++)
this.head[i + 3] = NIL;
this._update = new Array(this.maxlevel + 1);
for (i = 0; i < this._update.length; i++)
this._update[i] = this.head;
}
_randomLevel() {
var lvl = 0;
var max = Math.min(this.maxlevel, this.level + 1);
while ((Math.random() < P) && (lvl < max))
lvl++;
return lvl;
}
find(search, reverse) {
var node = reverse ? this.tail : this.head[3];
var idx = reverse ? 2 : 3;
var results = [];
if (search) {
var update = this._update.slice(0);
var found = this._findLess(update, search);
if (!nodesEqual(found[3], NIL))
node = found[3];
}
while (node[0]) {
results.push([node[0], node[1]]);
node = node[idx];
}
return results;
}
findWithCount(search, maxResultsToReturn, reverse) {
var node = reverse ? this.tail : this.head[3];
var idx = reverse ? 2 : 3;
var results = [];
if (search) {
var update = this._update.slice(0);
var found = this._findLess(update, search);
if (!nodesEqual(found[3], NIL))
node = found[3];
}
while (node[0] && (results.length < maxResultsToReturn)) {
results.push([node[0], node[1]]);
node = node[idx];
}
return results;
}
length() {
// more for my curiosity
var node = this.head[3];
var count = 0;
while (node[0]) {
count++;
node = node[3];
}
return count;
}
_findLess(update, search) {
var node = this.head;
for (var i = this.level; i >= 0; i--) {
var key = node[3 + i][0];
while (key && (key < search)) {
node = node[3 + i];
key = node[3 + i] ? node[3 + i][0] : null;
}
update[i] = node;
}
return node;
}
insert(key, value) {
var update = this._update.slice(0);
var node = this._findLess(update, key);
var prev = node;
node = node[3];
if (node[0] === key)
node[1] = value;
else {
var lvl = this._randomLevel();
this.level = Math.max(this.level, lvl);
node = makeNode(lvl, key, value);
node[2] = prev;
for (var i = 0; i <= this.level; i++) {
node[3 + i] = update[i][3 + i];
update[i][3 + i] = node;
}
if (nodesEqual(node[3], NIL))
this.tail = node;
else
node[3][2] = node;
}
}
remove(key) {
var update = this._update.slice(0);
var node = this._findLess(update, key);
node = node[3];
if (node[0] === key) {
node[3][2] = update[0];
for (var i = 0; i <= this.level; i++) {
if (!nodesEqual(update[i][3 + i], node))
break;
update[i][3 + i] = node[3 + i];
}
while ((this.level > 1) && (this.head[3 + this.level].key !== null))
this.level--;
if (nodesEqual(this.tail, node))
this.tail = node[2];
return true;
}
return false; // just to make it explicit
}
match(search) {
var node = this.head;
for (var i = this.level; i >= 0; i--) {
var key = node[3 + i][0];
while (key && (key < search)) {
node = node[3 + i];
key = node[3 + i] ? node[3 + i][0] : null;
}
}
node = node[3];
if (node[0] === search)
return node[1];
return null;
}
}
console.log('SkipList:')
var test = new SkipList(100);
for (let i = 0; i < 8; ++i) {
test.insert(i, i + 1);
}
console.log(test.find(1));
console.log(test.find(2));
console.log(test.find(5));
console.log(test.find(7));
// stack overflow
console.log(test.find(8));
for (let i = 0; i < 9; ++i) {
test.remove(i);
}
console.log('SkipList end')