-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
dataStructures.js
417 lines (339 loc) · 9.84 KB
/
dataStructures.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Derived from
// https://github.com/wooorm/linked-list/blob/d2390fe1cab9f780cfd34fa31c8fa8ede4ad674d/index.js
export const TYPE_TEXT = 'text';
// Creates a new `Iterator` for looping over the `List`.
class Iterator {
constructor(item) {
this.item = item;
}
// Move the `Iterator` to the next item.
next() {
this.value = this.item;
this.done = !this.item;
this.item = this.item ? this.item.next : undefined;
return this;
}
}
// Creates a new `Item`:
// An item is a bit like DOM node: It knows only about its "parent" (`list`),
// the item before it (`prev`), and the item after it (`next`).
export class LLNode {
// Prepends the given item *before* the item operated on.
prepend(item) {
const list = this.list;
if (!item || !item.append || !item.prepend || !item.detach) {
throw new Error(
'An argument without append, prepend, or detach methods was given to `Item#prepend`.',
);
}
// If self is detached, return false.
if (!list) {
return false;
}
if (this === item) {
return false;
}
// Detach the prependee.
const transient = this.list === item.list;
item.detach(transient);
// If self has a previous item...
if (this.prev) {
item.prev = this.prev;
this.prev.next = item;
}
// Connect the prependee.
item.next = this;
item.list = list;
// Set the previous item of self to the prependee.
this.prev = item;
// If self is the first item in the parent list, link the lists first item to
// the prependee.
if (this === list.head) {
list.head = item;
}
// If the the parent list has no last item, link the lists last item to self.
if (!list.tail) {
list.tail = this;
}
list.length++;
if (!transient) {
item._addToIndex();
}
return item;
}
// Appends the given item *after* the item operated on.
append(item) {
const list = this.list;
if (!item || !item.append || !item.prepend || !item.detach) {
throw new Error(
'An argument without append, prepend, or detach methods was given to `Item#append`.',
);
}
if (!list) {
return false;
}
if (this === item) {
return false;
}
// Detach the appendee.
const transient = this.list === item.list;
item.detach(transient);
// If self has a next item...
if (this.next) {
item.next = this.next;
this.next.prev = item;
}
// Connect the appendee.
item.prev = this;
item.list = list;
// Set the next item of self to the appendee.
this.next = item;
// If the the parent list has no last item or if self is the parent lists last
// item, link the lists last item to the appendee.
if (this === list.tail || !list.tail) {
list.tail = item;
}
list.length++;
if (!transient) {
item._addToIndex();
}
return item;
}
// Detaches the item operated on from its parent list.
detach(transient) {
const list = this.list;
if (!list) {
return this;
}
if (!transient) {
this._removeFromIndex();
}
// If self is the last item in the parent list, link the lists last item to
// the previous item.
if (list.tail === this) {
list.tail = this.prev;
}
// If self is the first item in the parent list, link the lists first item to
// the next item.
if (list.head === this) {
list.head = this.next;
}
// If both the last and first items in the parent list are the same, remove
// the link to the last item.
if (list.tail === list.head) {
list.tail = null;
}
// If a previous item exists, link its next item to selfs next item.
if (this.prev) {
this.prev.next = this.next;
}
// If a next item exists, link its previous item to selfs previous item.
if (this.next) {
this.next.prev = this.prev;
}
// Remove links from self to both the next and previous items, and to the
// parent list.
this.prev = this.next = this.list = null;
list.length--;
return this;
}
nextCyclic() {
return this.next || this.list.head;
}
prevCyclic() {
return this.prev || this.list.last();
}
_addToIndex() {
const hash = this._hash();
if (hash === undefined || hash === null) {
return;
}
if (this.type === TYPE_TEXT) {
this.list.bytes += this.text.length;
}
let entries = this.list.invertedIndex[hash];
if (!entries) {
entries = [];
this.list.invertedIndex[hash] = entries;
}
entries.push(this.id);
this.list.idsToItems[this.id] = this;
}
_removeFromIndex() {
const hash = this._hash();
if (hash === undefined || hash === null) {
return;
}
if (this.type === TYPE_TEXT) {
this.list.bytes -= this.text.length;
}
const entries = this.list.invertedIndex[hash];
if (entries.length === 1) {
delete this.list.invertedIndex[hash];
} else {
entries.splice(entries.indexOf(this.id), 1);
}
delete this.list.idsToItems[this.id];
}
_hash() {
if (this.type === TYPE_TEXT) {
return _hashText(this.text);
} else {
return null;
}
}
}
LLNode.prototype.next = LLNode.prototype.prev = LLNode.prototype.list = null;
// Creates a new List: A linked list is a bit like an Array, but knows nothing
// about how many items are in it, and knows only about its first (`head`) and
// last (`tail`) items.
// Each item (e.g. `head`, `tail`, &c.) knows which item comes before or after
// it (its more like the implementation of the DOM in JavaScript).
export class LinkedList {
// Creates a new list from the arguments (each a list item) passed in.
static of(...items) {
return appendAll(new this(), items);
}
// Creates a new list from the given array-like object (each a list item) passed
// in.
static from(items) {
return appendAll(new this(), items);
}
constructor(...items) {
appendAll(this, items);
this.idsToItems = {};
this.invertedIndex = {};
/** Note: this isn't an accurate count because of UTF encoding and other JS mumbo jumbo. */
this.bytes = 0;
}
// Returns the list's items as an array.
// This does *not* detach the items.
toArray() {
let item = this.head;
const result = [];
while (item) {
result.push(item);
item = item.next;
}
return result;
}
// Prepends the given item to the list.
// `item` will be the new first item (`head`).
prepend(item) {
if (!item) {
return false;
}
if (!item.append || !item.prepend || !item.detach) {
throw new Error(
'An argument without append, prepend, or detach methods was given to `List#prepend`.',
);
}
if (this.head) {
return this.head.prepend(item);
}
item.detach();
item.list = this;
this.head = item;
this.length++;
item._addToIndex();
return item;
}
// Appends the given item to the list.
// `item` will be the new last item (`tail`) if the list had a first item, and
// its first item (`head`) otherwise.
append(item) {
if (!item) {
return false;
}
if (!item.append || !item.prepend || !item.detach) {
throw new Error(
'An argument without append, prepend, or detach methods was given to `List#append`.',
);
}
// If self has a last item, defer appending to the last items append method,
// and return the result.
if (this.tail) {
return this.tail.append(item);
}
// If self has a first item, defer appending to the first items append method,
// and return the result.
if (this.head) {
return this.head.append(item);
}
// ...otherwise, there is no `tail` or `head` item yet.
item.detach();
item.list = this;
this.head = item;
this.length++;
item._addToIndex();
return item;
}
last() {
return this.tail || this.head;
}
findById(id) {
return this.idsToItems[id];
}
findTextItem(text) {
const entries = this.invertedIndex[_hashText(text)];
if (!entries) {
return null;
}
for (let i = entries.length - 1; i >= 0; i--) {
const item = this.idsToItems[entries[i]];
if (item.type === TYPE_TEXT && item.text === text) {
return item;
}
}
return null;
}
// Creates an iterator from the list.
[Symbol.iterator]() {
return new Iterator(this.head);
}
}
LinkedList.prototype.length = 0;
LinkedList.prototype.tail = LinkedList.prototype.head = null;
// Creates a new list from the items passed in.
export function appendAll(list, items) {
let index;
let item;
let iterator;
if (!items) {
return list;
}
if (items[Symbol.iterator]) {
iterator = items[Symbol.iterator]();
item = {};
while (!item.done) {
item = iterator.next();
list.append(item && item.value);
}
} else {
index = -1;
while (++index < items.length) {
list.append(items[index]);
}
}
return list;
}
function _hashText(text) {
// The goal of this hash function is to be extremely fast while minimizing collisions. To do
// this, we make an assumption about our data. If users copy text, the guess is that there is
// a very low likelihood of collisions when the text is very long. For example, why would
// someone copy two different pieces of text that are exactly 29047 characters long? However, for
// smaller pieces of text, it's very easy to get length collisions. For example, I can copy "the"
// and "123" to cause a collision. Thus, our hash function returns the string length for longer
// strings while using an ok-ish hash for short strings.
if (text.length > 500) {
return text.length;
}
// Copied from https://stackoverflow.com/a/7616484/4548500
let hash = 0;
for (let i = 0; i < text.length; i++) {
let chr = text.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to integer
}
return hash;
}