-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathSkipList.js
More file actions
311 lines (286 loc) · 8 KB
/
Copy pathSkipList.js
File metadata and controls
311 lines (286 loc) · 8 KB
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
/**
* Skip List
*
* A skip list is a probabilistic data structure that maintains an ordered set
* of keys and supports search, insertion and deletion in expected O(log n)
* time. It is built from a stack of sorted linked lists where each higher
* level skips over more elements, acting as an "express lane" into the level
* below. Each newly inserted node is promoted to the next level with a fixed
* probability `p` (typically 1/2), giving an expected height of log_{1/p}(n).
*
* Compared to balanced binary search trees, skip lists are simpler to
* implement and require no rotations to maintain balance.
*
* Reference: https://en.wikipedia.org/wiki/Skip_list
*/
const DEFAULT_MAX_LEVEL = 16
const DEFAULT_P = 0.5
class SkipListNode {
/**
* @param {*} key - Ordered key stored at this node, or `null` for the head sentinel.
* @param {*} value - Arbitrary value associated with `key`.
* @param {number} level - Height of the node (number of forward pointers).
*/
constructor(key, value, level) {
this.key = key
this.value = value
this.forward = new Array(level + 1).fill(null)
}
}
class SkipList {
/**
* Create an empty skip list.
*
* @param {object} [options] - Optional tuning parameters.
* @param {number} [options.maxLevel=16] - Maximum number of levels (>= 1).
* @param {number} [options.p=0.5] - Promotion probability in (0, 1).
* @param {() => number} [options.random=Math.random] - RNG returning a value in [0, 1); injectable for deterministic tests.
*
* @example
* const list = new SkipList()
* list.insert(3, 'three')
* list.search(3) // 'three'
*/
constructor(options = {}) {
const {
maxLevel = DEFAULT_MAX_LEVEL,
p = DEFAULT_P,
random = Math.random
} = options
if (!Number.isInteger(maxLevel) || maxLevel < 1) {
throw new RangeError('maxLevel must be a positive integer')
}
if (typeof p !== 'number' || p <= 0 || p >= 1) {
throw new RangeError('p must be a number in the open interval (0, 1)')
}
if (typeof random !== 'function') {
throw new TypeError(
'random must be a function returning a value in [0, 1)'
)
}
this.maxLevel = maxLevel
this.p = p
this.random = random
this.level = 0
this.length = 0
this.head = new SkipListNode(null, null, maxLevel)
}
/**
* Pick a random level in [0, maxLevel] using geometric distribution.
*
* @returns {number} The chosen level for a new node.
* @private
*/
_randomLevel() {
let lvl = 0
while (this.random() < this.p && lvl < this.maxLevel) {
lvl++
}
return lvl
}
/**
* Compare two keys using `<` and `>`. Numbers, strings and any type that
* supports the relational operators consistently can be used as keys.
*
* @param {*} a - Left-hand key.
* @param {*} b - Right-hand key.
* @returns {number} `-1` if `a < b`, `1` if `a > b`, `0` otherwise.
* @private
*/
_compare(a, b) {
if (a < b) return -1
if (a > b) return 1
return 0
}
/**
* Insert a key/value pair. If the key already exists, its value is updated
* in place and the size of the list does not change.
*
* @param {*} key - Key to insert; must be comparable with existing keys.
* @param {*} [value] - Value associated with the key (defaults to the key).
* @returns {SkipList} The list, for chaining.
*
* @example
* const list = new SkipList()
* list.insert(1, 'a').insert(2, 'b')
* list.size() // 2
*/
insert(key, value = key) {
const update = new Array(this.maxLevel + 1).fill(this.head)
let current = this.head
for (let i = this.level; i >= 0; i--) {
while (
current.forward[i] !== null &&
this._compare(current.forward[i].key, key) < 0
) {
current = current.forward[i]
}
update[i] = current
}
current = current.forward[0]
if (current !== null && this._compare(current.key, key) === 0) {
current.value = value
return this
}
const lvl = this._randomLevel()
if (lvl > this.level) {
for (let i = this.level + 1; i <= lvl; i++) {
update[i] = this.head
}
this.level = lvl
}
const node = new SkipListNode(key, value, lvl)
for (let i = 0; i <= lvl; i++) {
node.forward[i] = update[i].forward[i]
update[i].forward[i] = node
}
this.length++
return this
}
/**
* Look up the value associated with a key.
*
* @param {*} key - Key to search for.
* @returns {*} The associated value, or `undefined` if `key` is absent.
*
* @example
* const list = new SkipList()
* list.insert('apple', 1)
* list.search('apple') // 1
* list.search('banana') // undefined
*/
search(key) {
let current = this.head
for (let i = this.level; i >= 0; i--) {
while (
current.forward[i] !== null &&
this._compare(current.forward[i].key, key) < 0
) {
current = current.forward[i]
}
}
current = current.forward[0]
if (current !== null && this._compare(current.key, key) === 0) {
return current.value
}
return undefined
}
/**
* Check whether a key is present.
*
* @param {*} key - Key to test.
* @returns {boolean} `true` if the key is present.
*
* @example
* const list = new SkipList()
* list.insert(7)
* list.has(7) // true
*/
has(key) {
let current = this.head
for (let i = this.level; i >= 0; i--) {
while (
current.forward[i] !== null &&
this._compare(current.forward[i].key, key) < 0
) {
current = current.forward[i]
}
}
current = current.forward[0]
return current !== null && this._compare(current.key, key) === 0
}
/**
* Remove the entry with the given key.
*
* @param {*} key - Key to delete.
* @returns {boolean} `true` if a node was removed, `false` if the key was absent.
*
* @example
* const list = new SkipList()
* list.insert(1).insert(2)
* list.delete(1) // true
* list.delete(1) // false
*/
delete(key) {
const update = new Array(this.maxLevel + 1).fill(this.head)
let current = this.head
for (let i = this.level; i >= 0; i--) {
while (
current.forward[i] !== null &&
this._compare(current.forward[i].key, key) < 0
) {
current = current.forward[i]
}
update[i] = current
}
current = current.forward[0]
if (current === null || this._compare(current.key, key) !== 0) {
return false
}
for (let i = 0; i <= this.level; i++) {
if (update[i].forward[i] !== current) break
update[i].forward[i] = current.forward[i]
}
while (this.level > 0 && this.head.forward[this.level] === null) {
this.level--
}
this.length--
return true
}
/**
* Number of distinct keys in the list.
*
* @returns {number} The current size.
*
* @example
* const list = new SkipList()
* list.insert(1).insert(2).insert(2)
* list.size() // 2
*/
size() {
return this.length
}
/**
* Whether the list contains no keys.
*
* @returns {boolean} `true` when empty.
*/
isEmpty() {
return this.length === 0
}
/**
* Yield `[key, value]` pairs in ascending key order.
*
* @returns {Generator<[*, *]>} Iterator over the entries of the list.
*
* @example
* const list = new SkipList()
* list.insert(2, 'b').insert(1, 'a')
* Array.from(list.entries()) // [[1, 'a'], [2, 'b']]
*/
*entries() {
let current = this.head.forward[0]
while (current !== null) {
yield [current.key, current.value]
current = current.forward[0]
}
}
/**
* Default iterator yielding keys in ascending order.
*
* @returns {Generator<*>} Iterator over keys.
*
* @example
* const list = new SkipList()
* list.insert(3).insert(1).insert(2)
* [...list] // [1, 2, 3]
*/
*[Symbol.iterator]() {
let current = this.head.forward[0]
while (current !== null) {
yield current.key
current = current.forward[0]
}
}
}
export { SkipList, SkipListNode }