-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
346 lines (278 loc) · 6.33 KB
/
index.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
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
/**
* OrderedDict is a data structure that preservers order of inserted keys.
* And is a sub-class of a regular object/dictionary.
* Implementation is inspired by python's `OrderedDict` and this particular gist:
* https://gist.github.com/joequery/12332f410a05e6c7c949
*/
function OrderedDict() {
// Root key
this._rk = Symbol("rk");
// Doubly linked list of key-value pairs
this._r = createNode(this._rk, undefined);
// Circular link for linked list nodes
this._r.prev = this._r.next = this._r;
// Key -> Link map
this._m = new Map();
}
/**
* Add a new key-value pair to an ordered dict.
*
* @param {OrderedDictKey} key
* @param {any} value
* @returns {OrderedDict}
*/
OrderedDict.prototype.set = function(key, value) {
if (this._m.has(key)) {
this._m.get(key).value = value;
return this;
}
let node = createNode(key, value);
this._m.set(key, node);
// Inserting node at the end of the linked list
let root = this._r;
let last = root.prev;
[node.prev, node.next] = [last, root];
last.next = node;
root.prev = node;
return this;
};
/**
* Delete a key from an ordered dict.
*
* @param {OrderedDictKey} key
* @returns {boolean}
*/
OrderedDict.prototype.delete = function(key) {
if (!this._m.has(key)) {
return false;
}
// Removing node from the map and connecting node.prev and node.next
let node = this._m.get(key);
let nodePrev = node.prev;
let nodeNext = node.next;
node.prev.next = nodeNext;
node.next.prev = nodePrev;
node.prev = null;
node.next = null;
this._m.delete(key);
return true;
};
/**
* Clear ordered dict.
*
* @returns {undefined}
*/
OrderedDict.prototype.clear = function() {
this._r.prev = this._r.next = this._r;
this._m = new Map();
};
/**
* Retrieve a key from an ordered dict.
*
* @param {OrderedDictKey} key
* @returns {OrderedDictValue|undefined}
*/
OrderedDict.prototype.get = function(key) {
if (!this._m.has(key)) {
return;
}
return this._m.get(key).value;
};
/**
* Check if key exists in an ordered dict.
*
* @param {OrderedDictKey} key
* @returns {boolean}
*/
OrderedDict.prototype.has = function(key) {
return this._m.has(key);
};
/**
* Remove and return last element from an ordered dict.
*
* @returns {undefined|OrderedDictKeyValue}
*/
OrderedDict.prototype.pop = function() {
let node = this._r.prev;
if (node.key === this._rk) {
return;
}
let nodePrev = node.prev;
nodePrev.next = this._r;
this._r.prev = nodePrev;
this._m.delete(node.key);
return [node.key, node.value];
};
/**
* Remove and return first element from an ordered dict.
*
* @returns {undefined|OrderedDictKeyValue}
*/
OrderedDict.prototype.shift = function() {
let node = this._r.next;
if (node.key === this._rk) {
return;
}
let nodeNext = node.next;
nodeNext.prev = this._r;
this._r.next = nodeNext;
this._m.delete(node.key);
return [node.key, node.value];
};
/**
* Move an existing element to the start of an orederd dict.
*
* @param {OrderedDictKey} key
* @returns {boolean}
*/
OrderedDict.prototype.toStart = function(key) {
if (!this._m.has(key)) {
return false;
}
let node = this._m.get(key);
let nodePrev = node.prev;
let nodeNext = node.next;
nodePrev.next = nodeNext;
nodeNext.prev = nodePrev;
let root = this._r;
let first = root.next;
node.prev = root;
node.next = first;
root.next = first.prev = node;
};
/**
* Move an existing element to the end of an ordered dict.
*
* @param {OrderedDictKey} key
* @returns {boolean}
*/
OrderedDict.prototype.toEnd = function(key) {
if (!this._m.has(key)) {
return false;
}
let node = this._m.get(key);
let nodePrev = node.prev;
let nodeNext = node.next;
nodePrev.next = nodeNext;
nodeNext.prev = nodePrev;
let root = this._r;
let last = root.prev;
node.prev = last;
node.next = root;
last.next = root.prev = node;
};
/**
* Create an ordered dict from an array or object.
*
* @param {Array<OrderedDictKeyValue>|Object} data
* @returns {OrderedDict}
*/
OrderedDict.from = function(data) {
let dict = new OrderedDict();
let dataArray =
Object.prototype.toString.call(data) === "[object Object]"
? Object.entries(data)
: Array.isArray(data)
? data
: [];
if (dataArray.length) {
dataArray.forEach(([key, value]) => dict.set(key, value));
}
return dict;
};
OrderedDict.prototype._i = function*() {
let cur = this._r.next;
if (!this._m.size) {
return;
}
while (cur.key !== this._rk) {
yield cur;
cur = cur.next;
}
};
/**
* Returns new Iterator object that contains all keys of an ordered dict.
*
* @returns {Iterator}
*/
OrderedDict.prototype.keys = function*() {
let cur = this._i();
while (true) {
let value = cur.next();
if (value.done) {
return;
}
yield value.value.key;
}
};
/**
* Returns new Iterator object that contains all values of an ordered dict.
*
* @returns {Iterator}
*/
OrderedDict.prototype.values = function*() {
let cur = this._i();
while (true) {
let value = cur.next();
if (value.done) {
return;
}
yield value.value.value;
}
};
/**
* Returns new Iterator object that contains all key-value pairs of an ordered dict.
*
* @returns {Iterator}
*/
OrderedDict.prototype.entries = function*() {
let cur = this._i();
while (true) {
let value = cur.next();
if (value.done) {
return;
}
yield [value.value.key, value.value.value];
}
};
OrderedDict.prototype.toString = function() {
let result = [];
let cur = this._r.next;
if (!this._m.size) {
return "$ empty $";
}
while (cur.key !== this._rk) {
result.push(`(${cur.key}, ${cur.value})`);
cur = cur.next;
}
return "$ -> " + result.join(" -> ") + " -> $";
};
/**
* Creates Linked List Node
*
* @private
* @param {OrderedDictKey} key
* @param {OrderedDictValue} value
* @returns {LinkedListNode}
*/
function createNode(key, value) {
return { key, value, prev: null, next: null };
}
/**
* @private
* @typedef {Object} LinkedListNode
* @property {OrderedDictKey} key
* @property {OrderedDictValue} value
* @property {LinkedListNode|null} prev
* @property {LinkedListNode|null} next
*/
/**
* @typedef {any} OrderedDictKey
*/
/**
* @typedef {any} OrderedDictValue
*/
/**
* @typedef {Array<OrderedDictKey, OrderedDictValue>} OrderedDictKeyValue
*/
module.exports = OrderedDict;