forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.js
More file actions
452 lines (452 loc) · 14.1 KB
/
Copy pathLinkedList.js
File metadata and controls
452 lines (452 loc) · 14.1 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const LinkedListItem_1 = require("./LinkedListItem");
/**
* Implements a linked list structure
* @typeparam T Type of values within this LinkedList
*/
class LinkedList {
/**
* @param values Values to be added upfront into list
*/
constructor(values) {
/**
* Current length of this LinkedList.
* Note that this does not work anymore if you for some reason add your own LinkedListItems to LinkedList by hand
*/
this.length = 0;
/**
* Given to own LinkedListItem's for following jobs regarding an unlink:
* - If item is first item, set the next item as first item
* - If item is last item, set the previous item as last item
* - Decrease length
* @param item Item that has been unlinked
*/
this.unlinkCleanup = (item) => {
if (this.first === item) {
this.first = this.first.behind;
}
if (this.last === item) {
this.last = this.last.before;
}
this.length--;
};
if (values) {
if (values instanceof LinkedList)
values = values.values();
for (const value of values) {
this.push(value);
}
}
}
/**
* Clears this LinkedList.
* The default complexity is O(1), because it only removes links to the first and last item and resets the length.
* Note that if any LinkedListItem is still referenced outside the LinkedList, their before and behind fields might
* still reference the chain, not freeing space.
* You can set the unchain parameter to true, so every item in the linked list will be unchained,
* meaning all references to before and behind items will be removed.
* This increases complexity to O(n), but removes accidental outside references to the full chain.
* @param unchain If `true`, remove link info from every item. Changes complexity to O(n)!
*/
clear(unchain = false) {
if (unchain) {
while (this.first) {
this.first.unlink(true);
}
}
this.first = this.last = undefined;
this.length = 0;
}
/**
* As Array#every() given callback is called for every element until one call returns falsy or all elements had been processed
* @returns `false` if there was a falsy response from the callback, `true` if all elements have been processed "falselesly"
* @see Array#every
*/
every(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
for (const item of this.keys()) {
if (!callback(item.value, item, this)) {
return false;
}
}
return true;
}
/**
* Filters values into a new LinkedList
* @param callback decides wether given element should be part of new LinkedList
* @see Array#filter
*/
filter(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
const newList = new LinkedList();
for (const [item, value] of this) {
if (callback(value, item, this)) {
newList.push(value);
}
}
return newList;
}
/**
* Returns value for which given callback returns truthy
* @param callback runs for every value in LinkedList. If it returns truthy, current value is returned.
* @see Array#find
*/
find(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
for (const [item, value] of this) {
if (callback(value, item, this)) {
return value;
}
}
}
/**
* Returns the LinkedListItem for which given callback returns truthy
* @param callback runs for every LinkedListItem in LinkedList. If it returns truthy, current LinkedListItem is returned.
* @see Array#findIndex
*/
findItem(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
for (const [item, value] of this) {
if (callback(value, item, this)) {
return item;
}
}
}
/**
* Iterates this LinkedList's items and values
* @param callback Gets every value in LinkedList once with corresponding LinkedListItem and LinkedList
* @param thisArg If given, callback will be bound here
* @see Array#forEach
*/
forEach(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
for (const [item, value] of this) {
callback(value, item, this);
}
}
/**
* Checks if value can be found within LinkedList, starting from fromIndex, if given.
* @param value value to be found in this
* @param fromIndex Starting index. Supports negative values for which `this.size - 1 + fromIndex` will be used as starting point.
* @returns true if value could be found in LinkedList (respecting fromIndex), false otherwhise
* @see Array#includes
*/
includes(value, fromIndex = 0) {
let current = this.getItemByIndex(fromIndex);
while (current) {
if (current.value === value) {
return true;
}
current = current.behind;
}
return false;
}
/**
* Searches forward for given value and returns the first corresponding LinkedListItem found
* @param searchedValue Value to be found
* @param fromIndex Index to start from
* @see Array#indexOf
*/
itemOf(searchedValue, fromIndex = 0) {
let current = this.getItemByIndex(fromIndex);
while (current) {
if (current.value === searchedValue) {
return current;
}
current = current.behind;
}
return;
}
/**
* Searches backwards for given value and returns the first corresponding LinkedListItem found
* @param searchedValue Value to be found
* @param fromIndex Index to start from
* @see Array#indexOf
*/
lastItemOf(searchedValue, fromIndex = -1) {
let current = this.getItemByIndex(fromIndex);
while (current) {
if (current.value === searchedValue) {
return current;
}
current = current.before;
}
return;
}
/**
* Creates a new LinkedList with each of its itesm representing the output of the callback with each item in current LinkedList.
* @param callback Gets value, LinkedListeItem and LinkedList. The response will be used as value in the new LinkedList
* @param thisArg If given, callback is bound to thisArg
* @see Array#map
*/
map(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
const newList = new LinkedList();
for (const [item, value] of this) {
newList.push(callback(value, item, this));
}
return newList;
}
reduce(callback, initialValue) {
let current = this.first;
if (!current) {
if (!initialValue) {
throw new TypeError("Empty accumulator on empty LinkedList is not allowed.");
}
return initialValue;
}
if (initialValue === undefined) {
initialValue = current.value;
if (!current.behind) {
return initialValue;
}
current = current.behind;
}
do {
initialValue = callback(initialValue, current.value, current, this);
current = current.behind;
} while (current);
return initialValue;
}
reduceRight(callback, initialValue) {
let current = this.last;
if (!current) {
if (!initialValue) {
throw new TypeError("Empty accumulator on empty LinkedList is not allowed.");
}
return initialValue;
}
// let accumulator: V | T;
if (initialValue === undefined) {
initialValue = current.value;
if (!current.before) {
return initialValue;
}
current = current.before;
}
do {
initialValue = callback(initialValue, current.value, current, this);
current = current.before;
} while (current);
return initialValue;
}
/**
* Runs callback for every entry and returns true immediately if call of callback returns truthy.
* @param callback called for every element. If response is truthy, iteration
* @param thisArg If set, callback is bound to this
* @returns `true` once a callback call returns truthy, `false` if none returned truthy.
*/
some(callback, thisArg) {
if (thisArg) {
callback = callback.bind(thisArg);
}
for (const [item, value] of this) {
if (callback(value, item, this)) {
return true;
}
}
return false;
}
/**
* Joins values within this by given separator. Uses Array#join directly.
* @param separator separator to be used
* @see Array#join
*/
join(separator) {
return [...this.values()].join(separator);
}
/**
* Concats given values and returns a new LinkedList with all given values.
* If LinkedList's are given, they will be spread.
* @param others Other values or lists to be concat'ed together
* @see Array#concat
*/
concat(...others) {
const newList = new LinkedList(this);
for (const other of others) {
if (other instanceof LinkedList) {
newList.push(...other.values());
}
else {
newList.push(other);
}
}
return newList;
}
/**
* Removes the last LinkedListItem and returns its inner value
*/
pop() {
if (!this.last) {
return;
}
const item = this.last;
item.unlink();
return item.value;
}
/**
* Adds given values on the end of this LinkedList
* @param values Values to be added
*/
push(...values) {
for (const value of values) {
const item = new LinkedListItem_1.LinkedListItem(value, this.unlinkCleanup);
if (!this.first || !this.last) {
this.first = this.last = item;
}
else {
this.last.insertBehind(item);
this.last = item;
}
this.length++;
}
return this.length;
}
/**
* Adds given values to the beginning of this LinkedList
* @param values Values to be added
*/
unshift(...values) {
for (const value of values) {
const item = new LinkedListItem_1.LinkedListItem(value, this.unlinkCleanup);
if (!this.last || !this.first) {
this.first = this.last = item;
}
else {
item.insertBehind(this.first);
this.first = item;
}
this.length++;
}
return this.length;
}
/**
* Removes first occurrence of value found.
* @param value value to remove from LinkedList
*/
remove(value) {
for (const item of this.keys()) {
if (item.value === value) {
item.unlink();
return true;
}
}
return false;
}
/**
* Removes every occurrance of value within this.
* @param value value to remove from LinkedList
*/
removeAllOccurrences(value) {
let foundSomethingToDelete = false;
for (const item of this.keys()) {
if (item.value === value) {
item.unlink();
foundSomethingToDelete = true;
}
}
return foundSomethingToDelete;
}
/**
* Returns and removes first element from LinkedList
*/
shift() {
if (!this.first) {
return;
}
const item = this.first;
item.unlink();
return item.value;
}
/**
* Returns LinkedListItem and value for every entry of this LinkedList
*/
*[Symbol.iterator]() {
let current = this.first;
if (!current) {
return;
}
do {
yield [current, current.value];
current = current.behind;
} while (current);
}
/**
* Returns LinkedListItem and value for every entry of this LinkedList
* @see LinkedList#Symbol.iterator
*/
entries() {
return this[Symbol.iterator]();
}
/**
* Iterates the LinkedListItem's of this LinkedList
*/
*keys() {
let current = this.first;
if (!current) {
return;
}
do {
yield current;
current = current.behind;
} while (current);
}
/**
* Returns a value for every entry of this LinkedList
*/
*values() {
let current = this.first;
if (!current) {
return;
}
do {
yield current.value;
current = current.behind;
} while (current);
}
/**
* Returns the item by given index.
* Supports negative values and will return the item at `LinkedList.size - 1 + index` in that case.
* @param index Index of item to get from list
*/
getItemByIndex(index) {
if (index === undefined) {
throw new Error("index must be a number!");
}
if (!this.first) {
return;
}
let current;
if (index > 0) {
current = this.first;
while (current && index--) {
current = current.behind;
}
}
else if (index < 0) {
current = this.last;
while (current && ++index) {
current = current.before;
}
}
else {
return this.first;
}
return current;
}
}
exports.LinkedList = LinkedList;
//# sourceMappingURL=LinkedList.js.map