data-structure-typed 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -9
  3. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  4. package/dist/cjs/types/utils/utils.d.ts +1 -7
  5. package/dist/cjs/utils/utils.d.ts +3 -49
  6. package/dist/cjs/utils/utils.js +13 -82
  7. package/dist/cjs/utils/utils.js.map +1 -1
  8. package/dist/esm/data-structures/binary-tree/binary-tree.js +8 -9
  9. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/esm/types/utils/utils.d.ts +1 -7
  11. package/dist/esm/utils/utils.d.ts +3 -49
  12. package/dist/esm/utils/utils.js +10 -68
  13. package/dist/esm/utils/utils.js.map +1 -1
  14. package/dist/umd/data-structure-typed.js +18 -66
  15. package/dist/umd/data-structure-typed.min.js +2 -2
  16. package/dist/umd/data-structure-typed.min.js.map +1 -1
  17. package/package.json +1 -1
  18. package/src/data-structures/binary-tree/binary-tree.ts +9 -10
  19. package/src/types/utils/utils.ts +1 -6
  20. package/src/utils/utils.ts +11 -83
  21. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  22. package/test/unit/utils/utils.test.ts +35 -2
  23. package/dist/individuals/binary-tree/avl-tree-counter.mjs +0 -4701
  24. package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +0 -4514
  25. package/dist/individuals/binary-tree/avl-tree.mjs +0 -4321
  26. package/dist/individuals/binary-tree/binary-tree.mjs +0 -3097
  27. package/dist/individuals/binary-tree/bst.mjs +0 -3858
  28. package/dist/individuals/binary-tree/red-black-tree.mjs +0 -4391
  29. package/dist/individuals/binary-tree/tree-counter.mjs +0 -4806
  30. package/dist/individuals/binary-tree/tree-multi-map.mjs +0 -4582
  31. package/dist/individuals/graph/directed-graph.mjs +0 -2910
  32. package/dist/individuals/graph/undirected-graph.mjs +0 -2745
  33. package/dist/individuals/hash/hash-map.mjs +0 -1040
  34. package/dist/individuals/heap/heap.mjs +0 -909
  35. package/dist/individuals/heap/max-heap.mjs +0 -671
  36. package/dist/individuals/heap/min-heap.mjs +0 -659
  37. package/dist/individuals/linked-list/doubly-linked-list.mjs +0 -1495
  38. package/dist/individuals/linked-list/singly-linked-list.mjs +0 -1479
  39. package/dist/individuals/priority-queue/max-priority-queue.mjs +0 -768
  40. package/dist/individuals/priority-queue/min-priority-queue.mjs +0 -757
  41. package/dist/individuals/priority-queue/priority-queue.mjs +0 -670
  42. package/dist/individuals/queue/deque.mjs +0 -1262
  43. package/dist/individuals/queue/queue.mjs +0 -1865
  44. package/dist/individuals/stack/stack.mjs +0 -415
  45. package/dist/individuals/trie/trie.mjs +0 -687
@@ -1,1479 +0,0 @@
1
- // src/data-structures/base/iterable-element-base.ts
2
- var IterableElementBase = class {
3
- /**
4
- * The protected constructor initializes the options for the IterableElementBase class, including the
5
- * toElementFn function.
6
- * @param [options] - An optional object that contains the following properties:
7
- */
8
- constructor(options) {
9
- if (options) {
10
- const { toElementFn } = options;
11
- if (typeof toElementFn === "function") this._toElementFn = toElementFn;
12
- else if (toElementFn) throw new TypeError("toElementFn must be a function type");
13
- }
14
- }
15
- _toElementFn;
16
- get toElementFn() {
17
- return this._toElementFn;
18
- }
19
- /**
20
- * Time Complexity: O(n)
21
- * Space Complexity: O(1)
22
- *
23
- * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
24
- * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
25
- * allows the function to accept any number of arguments as an array. In this case, the `args`
26
- * parameter is used to pass any number of arguments to the `_getIterator` method.
27
- */
28
- *[Symbol.iterator](...args) {
29
- yield* this._getIterator(...args);
30
- }
31
- /**
32
- * Time Complexity: O(n)
33
- * Space Complexity: O(n)
34
- *
35
- * The function returns an iterator that yields all the values in the object.
36
- */
37
- *values() {
38
- for (const item of this) {
39
- yield item;
40
- }
41
- }
42
- /**
43
- * Time Complexity: O(n)
44
- * Space Complexity: O(1)
45
- *
46
- * The `every` function checks if every element in the array satisfies a given predicate.
47
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
48
- * the current element being processed, its index, and the array it belongs to. It should return a
49
- * boolean value indicating whether the element satisfies a certain condition or not.
50
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
51
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
52
- * passed as the `this` value to the `predicate` function. If `thisArg` is
53
- * @returns The `every` method is returning a boolean value. It returns `true` if every element in
54
- * the array satisfies the provided predicate function, and `false` otherwise.
55
- */
56
- every(predicate, thisArg) {
57
- let index = 0;
58
- for (const item of this) {
59
- if (!predicate.call(thisArg, item, index++, this)) {
60
- return false;
61
- }
62
- }
63
- return true;
64
- }
65
- /**
66
- * Time Complexity: O(n)
67
- * Space Complexity: O(1)
68
- *
69
- * The "some" function checks if at least one element in a collection satisfies a given predicate.
70
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
71
- * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
72
- * element satisfies the condition.
73
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
74
- * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
75
- * it will be passed as the `this` value to the `predicate` function. If `thisArg
76
- * @returns a boolean value. It returns true if the predicate function returns true for any element
77
- * in the collection, and false otherwise.
78
- */
79
- some(predicate, thisArg) {
80
- let index = 0;
81
- for (const item of this) {
82
- if (predicate.call(thisArg, item, index++, this)) {
83
- return true;
84
- }
85
- }
86
- return false;
87
- }
88
- /**
89
- * Time Complexity: O(n)
90
- * Space Complexity: O(1)
91
- *
92
- * The `forEach` function iterates over each element in an array-like object and calls a callback
93
- * function for each element.
94
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
95
- * the array. It takes three arguments: the current element being processed, the index of the current
96
- * element, and the array that forEach was called upon.
97
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
98
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
99
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
100
- */
101
- forEach(callbackfn, thisArg) {
102
- let index = 0;
103
- for (const item of this) {
104
- callbackfn.call(thisArg, item, index++, this);
105
- }
106
- }
107
- /**
108
- * Time Complexity: O(n)
109
- * Space Complexity: O(1)
110
- *
111
- * The `find` function iterates over the elements of an array-like object and returns the first
112
- * element that satisfies the provided callback function.
113
- * @param predicate - The predicate parameter is a function that will be called for each element in
114
- * the array. It takes three arguments: the current element being processed, the index of the current
115
- * element, and the array itself. The function should return a boolean value indicating whether the
116
- * current element matches the desired condition.
117
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
118
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
119
- * be passed as the `this` value to the `callbackfn` function. If `thisArg
120
- * @returns The `find` method returns the first element in the array that satisfies the provided
121
- * callback function. If no element satisfies the callback function, `undefined` is returned.
122
- */
123
- find(predicate, thisArg) {
124
- let index = 0;
125
- for (const item of this) {
126
- if (predicate.call(thisArg, item, index++, this)) return item;
127
- }
128
- return;
129
- }
130
- /**
131
- * Time Complexity: O(n)
132
- * Space Complexity: O(1)
133
- *
134
- * The function checks if a given element exists in a collection.
135
- * @param {E} element - The parameter "element" is of type E, which means it can be any type. It
136
- * represents the element that we want to check for existence in the collection.
137
- * @returns a boolean value. It returns true if the element is found in the collection, and false
138
- * otherwise.
139
- */
140
- has(element) {
141
- for (const ele of this) {
142
- if (ele === element) return true;
143
- }
144
- return false;
145
- }
146
- /**
147
- * Time Complexity: O(n)
148
- * Space Complexity: O(1)
149
- *
150
- * The `reduce` function iterates over the elements of an array-like object and applies a callback
151
- * function to reduce them into a single value.
152
- * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
153
- * the array. It takes four arguments:
154
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
155
- * is the value that the accumulator starts with before the reduction operation begins.
156
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
157
- * all the elements in the array and applying the callback function to each element.
158
- */
159
- reduce(callbackfn, initialValue) {
160
- let accumulator = initialValue ?? 0;
161
- let index = 0;
162
- for (const item of this) {
163
- accumulator = callbackfn(accumulator, item, index++, this);
164
- }
165
- return accumulator;
166
- }
167
- /**
168
- * Time Complexity: O(n)
169
- * Space Complexity: O(n)
170
- *
171
- * The `toArray` function converts a linked list into an array.
172
- * @returns The `toArray()` method is returning an array of type `E[]`.
173
- */
174
- toArray() {
175
- return [...this];
176
- }
177
- /**
178
- * Time Complexity: O(n)
179
- * Space Complexity: O(n)
180
- *
181
- * The print function logs the elements of an array to the console.
182
- */
183
- toVisual() {
184
- return [...this];
185
- }
186
- /**
187
- * Time Complexity: O(n)
188
- * Space Complexity: O(n)
189
- *
190
- * The print function logs the elements of an array to the console.
191
- */
192
- print() {
193
- console.log(this.toVisual());
194
- }
195
- };
196
-
197
- // src/data-structures/base/linear-base.ts
198
- var LinkedListNode = class {
199
- constructor(value) {
200
- this._value = value;
201
- this._next = void 0;
202
- }
203
- _value;
204
- get value() {
205
- return this._value;
206
- }
207
- set value(value) {
208
- this._value = value;
209
- }
210
- _next;
211
- get next() {
212
- return this._next;
213
- }
214
- set next(value) {
215
- this._next = value;
216
- }
217
- };
218
- var LinearBase = class _LinearBase extends IterableElementBase {
219
- /**
220
- * The constructor initializes the LinearBase class with optional options, setting the maximum length
221
- * if provided.
222
- * @param [options] - The `options` parameter is an optional object that can be passed to the
223
- * constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
224
- * object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
225
- */
226
- constructor(options) {
227
- super(options);
228
- if (options) {
229
- const { maxLen } = options;
230
- if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
231
- }
232
- }
233
- _maxLen = -1;
234
- get maxLen() {
235
- return this._maxLen;
236
- }
237
- /**
238
- * Time Complexity: O(n)
239
- * Space Complexity: O(1)
240
- *
241
- * The function indexOf searches for a specified element starting from a given index in an array-like
242
- * object and returns the index of the first occurrence, or -1 if not found.
243
- * @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
244
- * element that you want to find within the array. The function will search for this element starting
245
- * from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
246
- * within the
247
- * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
248
- * index at which to start searching for the `searchElement` within the array. If provided, the
249
- * search will begin at this index and continue to the end of the array. If `fromIndex` is not
250
- * specified, the default
251
- * @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
252
- * array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
253
- */
254
- indexOf(searchElement, fromIndex = 0) {
255
- if (this.length === 0) return -1;
256
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
257
- if (fromIndex < 0) fromIndex = 0;
258
- for (let i = fromIndex; i < this.length; i++) {
259
- const element = this.at(i);
260
- if (element === searchElement) return i;
261
- }
262
- return -1;
263
- }
264
- /**
265
- * Time Complexity: O(n)
266
- * Space Complexity: O(1)
267
- *
268
- * The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
269
- * element in an array.
270
- * @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
271
- * last index of within the array. The `lastIndexOf` method will search the array starting from the
272
- * `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
273
- * of the
274
- * @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
275
- * index at which to start searching for the `searchElement` in the array. By default, it starts
276
- * searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
277
- * provided
278
- * @returns The last index of the `searchElement` in the array is being returned. If the
279
- * `searchElement` is not found in the array, -1 is returned.
280
- */
281
- lastIndexOf(searchElement, fromIndex = this.length - 1) {
282
- if (this.length === 0) return -1;
283
- if (fromIndex >= this.length) fromIndex = this.length - 1;
284
- if (fromIndex < 0) fromIndex = this.length + fromIndex;
285
- for (let i = fromIndex; i >= 0; i--) {
286
- const element = this.at(i);
287
- if (element === searchElement) return i;
288
- }
289
- return -1;
290
- }
291
- /**
292
- * Time Complexity: O(n)
293
- * Space Complexity: O(1)
294
- *
295
- * The `findIndex` function iterates over an array and returns the index of the first element that
296
- * satisfies the provided predicate function.
297
- * @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
298
- * that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
299
- * value indicating whether the current element satisfies the condition being checked for.
300
- * @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
301
- * parameter that specifies the value to use as `this` when executing the `predicate` function. If
302
- * provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
303
- * @returns The `findIndex` method is returning the index of the first element in the array that
304
- * satisfies the provided predicate function. If no such element is found, it returns -1.
305
- */
306
- findIndex(predicate, thisArg) {
307
- for (let i = 0; i < this.length; i++) {
308
- const item = this.at(i);
309
- if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
310
- }
311
- return -1;
312
- }
313
- /**
314
- * Time Complexity: O(n + m)
315
- * Space Complexity: O(n + m)
316
- *
317
- * The `concat` function in TypeScript concatenates multiple items into a new list, handling both
318
- * individual elements and instances of `LinearBase`.
319
- * @param {(E | this)[]} items - The `concat` method takes in an array of items, where
320
- * each item can be either of type `E` or an instance of `LinearBase<E, R>`.
321
- * @returns The `concat` method is returning a new instance of the class that it belongs to, with the
322
- * items passed as arguments concatenated to it.
323
- */
324
- concat(...items) {
325
- const newList = this.clone();
326
- for (const item of items) {
327
- if (item instanceof _LinearBase) {
328
- newList.pushMany(item);
329
- } else {
330
- newList.push(item);
331
- }
332
- }
333
- return newList;
334
- }
335
- /**
336
- * Time Complexity: O(n log n)
337
- * Space Complexity: O(n)
338
- *
339
- * The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
340
- * function.
341
- * @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
342
- * two elements `a` and `b` as input and returns a number indicating their relative order. If the
343
- * returned value is negative, `a` comes before `b`. If the returned value is positive, `
344
- * @returns The `sort` method is returning the instance of the object on which it is called (this),
345
- * after sorting the elements based on the provided comparison function (compareFn).
346
- */
347
- sort(compareFn) {
348
- const arr = this.toArray();
349
- arr.sort(compareFn);
350
- this.clear();
351
- for (const item of arr) this.push(item);
352
- return this;
353
- }
354
- /**
355
- * Time Complexity: O(n + m)
356
- * Space Complexity: O(m)
357
- *
358
- * The `splice` function in TypeScript removes elements from an array and optionally inserts new
359
- * elements at the specified index.
360
- * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
361
- * to start modifying the array. If `start` is a negative number, it will count from the end of the
362
- * array.
363
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
364
- * number of elements to remove from the array starting at the specified `start` index. If
365
- * `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
366
- * at the `start`
367
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
368
- * will be inserted into the array at the specified `start` index. These elements can be of any type
369
- * and you can pass multiple elements separated by commas. The `splice` method will insert these
370
- * items into the array at the
371
- * @returns The `splice` method returns a list of elements that were removed from the original list
372
- * during the operation.
373
- */
374
- splice(start, deleteCount = 0, ...items) {
375
- const removedList = this._createInstance();
376
- start = start < 0 ? this.length + start : start;
377
- start = Math.max(0, Math.min(start, this.length));
378
- deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
379
- for (let i = 0; i < deleteCount; i++) {
380
- const removed = this.deleteAt(start);
381
- if (removed !== void 0) {
382
- removedList.push(removed);
383
- }
384
- }
385
- for (let i = 0; i < items.length; i++) {
386
- this.addAt(start + i, items[i]);
387
- }
388
- return removedList;
389
- }
390
- /**
391
- * Time Complexity: O(n)
392
- * Space Complexity: O(1)
393
- *
394
- * The `join` function in TypeScript returns a string by joining the elements of an array with a
395
- * specified separator.
396
- * @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
397
- * or characters that will be used to separate each element when joining them into a single string.
398
- * By default, the separator is set to a comma (`,`), but you can provide a different separator if
399
- * needed.
400
- * @returns The `join` method is being returned, which takes an optional `separator` parameter
401
- * (defaulting to a comma) and returns a string created by joining all elements of the array after
402
- * converting it to an array.
403
- */
404
- join(separator = ",") {
405
- return this.toArray().join(separator);
406
- }
407
- /**
408
- * Time Complexity: O(n)
409
- * Space Complexity: O(n)
410
- *
411
- * The function `toReversedArray` takes an array and returns a new array with its elements in reverse
412
- * order.
413
- * @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
414
- * order.
415
- */
416
- toReversedArray() {
417
- const array = [];
418
- for (let i = this.length - 1; i >= 0; i--) {
419
- array.push(this.at(i));
420
- }
421
- return array;
422
- }
423
- /**
424
- * Time Complexity: O(n)
425
- * Space Complexity: O(1)
426
- *
427
- * The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
428
- * callback function to each element, accumulating a single result.
429
- * @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
430
- * be called on each element in the array from right to left. It takes four arguments:
431
- * @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
432
- * optional parameter that specifies the initial value of the accumulator. If provided, the
433
- * `accumulator` will start with this initial value before iterating over the elements of the array.
434
- * If `initialValue` is not provided, the accumulator will
435
- * @returns The `reduceRight` method is returning the final accumulated value after applying the
436
- * callback function to each element in the array from right to left.
437
- */
438
- reduceRight(callbackfn, initialValue) {
439
- let accumulator = initialValue ?? 0;
440
- for (let i = this.length - 1; i >= 0; i--) {
441
- accumulator = callbackfn(accumulator, this.at(i), i, this);
442
- }
443
- return accumulator;
444
- }
445
- /**
446
- * Time Complexity: O(m)
447
- * Space Complexity: O(m)
448
- *
449
- * The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
450
- * the original instance based on the specified start and end indices.
451
- * @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
452
- * which to begin extracting elements from an array-like object. If no `start` parameter is provided,
453
- * the default value is 0, meaning the extraction will start from the beginning of the array.
454
- * @param {number} end - The `end` parameter in the `slice` method represents the index at which to
455
- * end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
456
- * array (i.e., `this.length`).
457
- * @returns The `slice` method is returning a new instance of the object with elements sliced from
458
- * the specified start index (default is 0) to the specified end index (default is the length of the
459
- * object).
460
- */
461
- slice(start = 0, end = this.length) {
462
- start = start < 0 ? this.length + start : start;
463
- end = end < 0 ? this.length + end : end;
464
- const newList = this._createInstance();
465
- for (let i = start; i < end; i++) {
466
- newList.push(this.at(i));
467
- }
468
- return newList;
469
- }
470
- /**
471
- * Time Complexity: O(n)
472
- * Space Complexity: O(1)
473
- *
474
- * The `fill` function in TypeScript fills a specified range in an array-like object with a given
475
- * value.
476
- * @param {E} value - The `value` parameter in the `fill` method represents the element that will be
477
- * used to fill the specified range in the array.
478
- * @param [start=0] - The `start` parameter specifies the index at which to start filling the array
479
- * with the specified value. If not provided, it defaults to 0, indicating the beginning of the
480
- * array.
481
- * @param end - The `end` parameter in the `fill` function represents the index at which the filling
482
- * of values should stop. It specifies the end of the range within the array where the `value` should
483
- * be filled.
484
- * @returns The `fill` method is returning the modified object (`this`) after filling the specified
485
- * range with the provided value.
486
- */
487
- fill(value, start = 0, end = this.length) {
488
- start = start < 0 ? this.length + start : start;
489
- end = end < 0 ? this.length + end : end;
490
- if (start < 0) start = 0;
491
- if (end > this.length) end = this.length;
492
- if (start >= end) return this;
493
- for (let i = start; i < end; i++) {
494
- this.setAt(i, value);
495
- }
496
- return this;
497
- }
498
- };
499
- var LinearLinkedBase = class extends LinearBase {
500
- /**
501
- * The constructor initializes the LinearBase class with optional options, setting the maximum length
502
- * if provided and valid.
503
- * @param [options] - The `options` parameter is an optional object that can be passed to the
504
- * constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
505
- * `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
506
- */
507
- constructor(options) {
508
- super(options);
509
- if (options) {
510
- const { maxLen } = options;
511
- if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
512
- }
513
- }
514
- /**
515
- * Time Complexity: O(n)
516
- * Space Complexity: O(1)
517
- *
518
- * The function overrides the indexOf method to improve performance by searching for an element in a
519
- * custom array implementation starting from a specified index.
520
- * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
521
- * within the array. The `indexOf` method will return the index of the first occurrence of this
522
- * element within the array.
523
- * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
524
- * index in the array at which to start the search for the `searchElement`. If provided, the search
525
- * will begin at the specified index and continue to the end of the array. If not provided, the
526
- * search will start at index
527
- * @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
528
- * array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
529
- */
530
- indexOf(searchElement, fromIndex = 0) {
531
- const iterator = this._getIterator();
532
- let current = iterator.next();
533
- let index = 0;
534
- while (index < fromIndex) {
535
- current = iterator.next();
536
- index++;
537
- }
538
- while (!current.done) {
539
- if (current.value === searchElement) return index;
540
- current = iterator.next();
541
- index++;
542
- }
543
- return -1;
544
- }
545
- /**
546
- * Time Complexity: O(n)
547
- * Space Complexity: O(1)
548
- *
549
- * The function overrides the lastIndexOf method in TypeScript to improve performance by searching
550
- * for an element in reverse order starting from a specified index.
551
- * @param {E} searchElement - The `searchElement` parameter is the element that you want to find
552
- * within the array. The `lastIndexOf` method searches the array for this element starting from the
553
- * end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
554
- * occurrence of the element
555
- * @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
556
- * index at which to start searching for the `searchElement` in the array. If provided, the search
557
- * will begin at this index and move towards the beginning of the array. If not provided, the search
558
- * will start at the
559
- * @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
560
- * from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
561
- * reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
562
- * occurrence of the `searchElement` if found, or -1 if not found.
563
- */
564
- lastIndexOf(searchElement, fromIndex = this.length - 1) {
565
- const iterator = this._getReverseIterator();
566
- let current = iterator.next();
567
- let index = this.length - 1;
568
- while (index > fromIndex) {
569
- current = iterator.next();
570
- index--;
571
- }
572
- while (!current.done) {
573
- if (current.value === searchElement) return index;
574
- current = iterator.next();
575
- index--;
576
- }
577
- return -1;
578
- }
579
- /**
580
- * Time Complexity: O(n + m)
581
- * Space Complexity: O(n + m)
582
- *
583
- * The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
584
- * list, handling both individual elements and instances of `LinearBase`.
585
- * @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
586
- * number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
587
- * current list and returns a new list with the concatenated items.
588
- * @returns The `concat` method is returning a new instance of the class that it belongs to, with the
589
- * items passed as arguments concatenated to it.
590
- */
591
- concat(...items) {
592
- const newList = this.clone();
593
- for (const item of items) {
594
- if (item instanceof LinearBase) {
595
- newList.pushMany(item);
596
- } else {
597
- newList.push(item);
598
- }
599
- }
600
- return newList;
601
- }
602
- /**
603
- * Time Complexity: O(m)
604
- * Space Complexity: O(m)
605
- *
606
- * The `slice` method is overridden to improve performance by creating a new instance and iterating
607
- * through the array to extract a subset based on the specified start and end indices.
608
- * @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
609
- * which to begin extracting elements from the array. If no `start` parameter is provided, the
610
- * default value is 0, indicating that extraction should start from the beginning of the array.
611
- * @param {number} end - The `end` parameter in the `slice` method represents the index at which to
612
- * end the slicing of the array. If not provided, it defaults to the length of the array.
613
- * @returns The `slice` method is returning a new instance of the array implementation with elements
614
- * sliced from the original array based on the `start` and `end` parameters.
615
- */
616
- slice(start = 0, end = this.length) {
617
- start = start < 0 ? this.length + start : start;
618
- end = end < 0 ? this.length + end : end;
619
- const newList = this._createInstance();
620
- const iterator = this._getIterator();
621
- let current = iterator.next();
622
- let c = 0;
623
- while (c < start) {
624
- current = iterator.next();
625
- c++;
626
- }
627
- for (let i = start; i < end; i++) {
628
- newList.push(current.value);
629
- current = iterator.next();
630
- }
631
- return newList;
632
- }
633
- /**
634
- * Time Complexity: O(n + m)
635
- * Space Complexity: O(m)
636
- *
637
- * The function overrides the splice method to handle deletion and insertion of elements in a data
638
- * structure while returning the removed elements.
639
- * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
640
- * to start modifying the array.
641
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
642
- * number of elements to remove from the array starting at the specified `start` index. If
643
- * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
644
- * elements can still be inserted at
645
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
646
- * will be inserted into the array at the specified `start` index. These elements can be of any type
647
- * and there can be multiple elements passed as arguments to be inserted into the array.
648
- * @returns The `splice` method is returning a new instance of the data structure that was modified
649
- * by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
650
- * elements provided in the `items` array.
651
- */
652
- splice(start, deleteCount = 0, ...items) {
653
- const removedList = this._createInstance();
654
- start = start < 0 ? this.length + start : start;
655
- start = Math.max(0, Math.min(start, this.length));
656
- deleteCount = Math.max(0, deleteCount);
657
- let currentIndex = 0;
658
- let currentNode = void 0;
659
- let previousNode = void 0;
660
- const iterator = this._getNodeIterator();
661
- for (const node of iterator) {
662
- if (currentIndex === start) {
663
- currentNode = node;
664
- break;
665
- }
666
- previousNode = node;
667
- currentIndex++;
668
- }
669
- for (let i = 0; i < deleteCount && currentNode; i++) {
670
- removedList.push(currentNode.value);
671
- const nextNode = currentNode.next;
672
- this.delete(currentNode);
673
- currentNode = nextNode;
674
- }
675
- for (let i = 0; i < items.length; i++) {
676
- if (previousNode) {
677
- this.addAfter(previousNode, items[i]);
678
- previousNode = previousNode.next;
679
- } else {
680
- this.addAt(0, items[i]);
681
- previousNode = this._getNodeIterator().next().value;
682
- }
683
- }
684
- return removedList;
685
- }
686
- /**
687
- * Time Complexity: O(n)
688
- * Space Complexity: O(1)
689
- *
690
- * The function `reduceRight` iterates over an array in reverse order and applies a callback function
691
- * to each element, accumulating a single result.
692
- * @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
693
- * of the array from right to left. It takes four arguments:
694
- * @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
695
- * initial accumulator value in the reduce operation. If provided, the reduce operation starts with
696
- * this initial value and iterates over the elements of the array, applying the callback function to
697
- * each element and the current accumulator value. If `initial
698
- * @returns The `reduceRight` method is returning the final accumulated value after applying the
699
- * callback function to each element in the array from right to left.
700
- */
701
- reduceRight(callbackfn, initialValue) {
702
- let accumulator = initialValue ?? 0;
703
- let index = this.length - 1;
704
- for (const item of this._getReverseIterator()) {
705
- accumulator = callbackfn(accumulator, item, index--, this);
706
- }
707
- return accumulator;
708
- }
709
- };
710
-
711
- // src/data-structures/linked-list/singly-linked-list.ts
712
- var SinglyLinkedListNode = class extends LinkedListNode {
713
- /**
714
- * The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
715
- * @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
716
- * will be stored in the node of a linked list.
717
- */
718
- constructor(value) {
719
- super(value);
720
- this._value = value;
721
- this._next = void 0;
722
- }
723
- _next;
724
- get next() {
725
- return this._next;
726
- }
727
- set next(value) {
728
- this._next = value;
729
- }
730
- };
731
- var SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
732
- constructor(elements = [], options) {
733
- super(options);
734
- if (options) {
735
- }
736
- this.pushMany(elements);
737
- }
738
- _head;
739
- get head() {
740
- return this._head;
741
- }
742
- _tail;
743
- get tail() {
744
- return this._tail;
745
- }
746
- get first() {
747
- return this.head?.value;
748
- }
749
- get last() {
750
- return this.tail?.value;
751
- }
752
- _length = 0;
753
- get length() {
754
- return this._length;
755
- }
756
- /**
757
- * Time Complexity: O(n)
758
- * Space Complexity: O(n)
759
- *
760
- * The `fromArray` function creates a new SinglyLinkedList instance and populates it with the elements from the given
761
- * array.
762
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
763
- * @returns The `fromArray` function returns a `SinglyLinkedList` object.
764
- */
765
- static fromArray(data) {
766
- const singlyLinkedList = new _SinglyLinkedList();
767
- for (const item of data) {
768
- singlyLinkedList.push(item);
769
- }
770
- return singlyLinkedList;
771
- }
772
- /**
773
- * Time Complexity: O(1)
774
- * Space Complexity: O(1)
775
- *
776
- * The `push` function adds a new element or node to the end of a singly linked list.
777
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
778
- * method can accept either an element of type `E` or a `SinglyLinkedListNode<E>` object.
779
- * @returns The `push` method is returning a boolean value, specifically `true`.
780
- */
781
- push(elementOrNode) {
782
- const newNode = this._ensureNode(elementOrNode);
783
- if (!this.head) {
784
- this._head = newNode;
785
- this._tail = newNode;
786
- } else {
787
- this.tail.next = newNode;
788
- this._tail = newNode;
789
- }
790
- this._length++;
791
- if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
792
- return true;
793
- }
794
- /**
795
- * Time Complexity: O(n)
796
- * Space Complexity: O(1)
797
- *
798
- * The `pop` function removes and returns the value of the last element in a linked list.
799
- * @returns The method is returning the value of the element that is being popped from the end of the
800
- * list.
801
- */
802
- pop() {
803
- if (!this.head) return void 0;
804
- if (this.head === this.tail) {
805
- const value2 = this.head.value;
806
- this._head = void 0;
807
- this._tail = void 0;
808
- this._length--;
809
- return value2;
810
- }
811
- let current = this.head;
812
- while (current.next !== this.tail) {
813
- current = current.next;
814
- }
815
- const value = this.tail.value;
816
- current.next = void 0;
817
- this._tail = current;
818
- this._length--;
819
- return value;
820
- }
821
- /**
822
- * Time Complexity: O(1)
823
- * Space Complexity: O(1)
824
- *
825
- * The `shift()` function removes and returns the value of the first element in a linked list.
826
- * @returns The value of the removed node.
827
- */
828
- shift() {
829
- if (!this.head) return void 0;
830
- const removedNode = this.head;
831
- this._head = this.head.next;
832
- this._length--;
833
- return removedNode.value;
834
- }
835
- /**
836
- * Time Complexity: O(1)
837
- * Space Complexity: O(1)
838
- *
839
- * The unshift function adds a new element or node to the beginning of a singly linked list in
840
- * TypeScript.
841
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
842
- * `unshift` method can be either an element of type `E` or a `SinglyLinkedListNode` containing an
843
- * element of type `E`.
844
- * @returns The `unshift` method is returning a boolean value, specifically `true`.
845
- */
846
- unshift(elementOrNode) {
847
- const newNode = this._ensureNode(elementOrNode);
848
- if (!this.head) {
849
- this._head = newNode;
850
- this._tail = newNode;
851
- } else {
852
- newNode.next = this.head;
853
- this._head = newNode;
854
- }
855
- this._length++;
856
- return true;
857
- }
858
- /**
859
- * Time Complexity: O(k)
860
- * Space Complexity: O(k)
861
- *
862
- * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
863
- * transformation function if provided.
864
- * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
865
- * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
866
- * or `SinglyLinkedListNode<E>`.
867
- * @returns The `pushMany` function returns an array of boolean values indicating whether each
868
- * element was successfully pushed into the data structure.
869
- */
870
- pushMany(elements) {
871
- const ans = [];
872
- for (const el of elements) {
873
- if (this.toElementFn) {
874
- ans.push(this.push(this.toElementFn(el)));
875
- continue;
876
- }
877
- ans.push(this.push(el));
878
- }
879
- return ans;
880
- }
881
- /**
882
- * Time Complexity: O(k)
883
- * Space Complexity: O(k)
884
- *
885
- * The function `unshiftMany` iterates over elements and adds them to a data structure, optionally
886
- * converting them using a provided function.
887
- * @param {Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>} elements - The `elements`
888
- * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
889
- * `R`, or `SinglyLinkedListNode<E>`. The function iterates over each element in the iterable and
890
- * performs an `unshift` operation on the linked list for each
891
- * @returns The `unshiftMany` function is returning an array of boolean values, where each value
892
- * represents the result of calling the `unshift` method on the current instance of the class.
893
- */
894
- unshiftMany(elements) {
895
- const ans = [];
896
- for (const el of elements) {
897
- if (this.toElementFn) {
898
- ans.push(this.unshift(this.toElementFn(el)));
899
- continue;
900
- }
901
- ans.push(this.unshift(el));
902
- }
903
- return ans;
904
- }
905
- /**
906
- * Time Complexity: O(n)
907
- * Space Complexity: O(1)
908
- *
909
- * This function searches for a specific element in a singly linked list based on a given node or
910
- * predicate.
911
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
912
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `get` method can be one of
913
- * the following types:
914
- * @returns The `get` method returns the value of the first node in the singly linked list that
915
- * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
916
- */
917
- search(elementNodeOrPredicate) {
918
- const predicate = this._ensurePredicate(elementNodeOrPredicate);
919
- let current = this.head;
920
- while (current) {
921
- if (predicate(current)) return current.value;
922
- current = current.next;
923
- }
924
- return void 0;
925
- }
926
- /**
927
- * Time Complexity: O(n)
928
- * Space Complexity: O(1)
929
- *
930
- * The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range.
931
- * @param {number} index - The index parameter is a number that represents the position of the element we want to
932
- * retrieve from the list.
933
- * @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or
934
- * `undefined` if the index is out of bounds.
935
- */
936
- at(index) {
937
- if (index < 0 || index >= this._length) return void 0;
938
- let current = this.head;
939
- for (let i = 0; i < index; i++) {
940
- current = current.next;
941
- }
942
- return current.value;
943
- }
944
- /**
945
- * Time Complexity: O(1)
946
- * Space Complexity: O(1)
947
- *
948
- * The function `isNode` in TypeScript checks if the input is an instance of `SinglyLinkedListNode`.
949
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
950
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can be
951
- * one of the following types:
952
- * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
953
- * instance of `SinglyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
954
- * parameter is a `SinglyLinkedListNode<E>`. If it is not an instance of `SinglyLinkedListNode<E>`,
955
- * the function returns `false`.
956
- */
957
- isNode(elementNodeOrPredicate) {
958
- return elementNodeOrPredicate instanceof SinglyLinkedListNode;
959
- }
960
- /**
961
- * Time Complexity: O(n)
962
- * Space Complexity: O(1)
963
- *
964
- * The function `getNodeAt` returns the node at a given index in a singly linked list.
965
- * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
966
- * retrieve from the linked list. It indicates the zero-based index of the node we want to access.
967
- * @returns The method `getNodeAt(index: number)` returns a `SinglyLinkedListNode<E>` object if the node at the
968
- * specified index exists, or `undefined` if the index is out of bounds.
969
- */
970
- getNodeAt(index) {
971
- let current = this.head;
972
- for (let i = 0; i < index; i++) {
973
- current = current.next;
974
- }
975
- return current;
976
- }
977
- /**
978
- * Time Complexity: O(n)
979
- * Space Complexity: O(1)
980
- *
981
- * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
982
- * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
983
- * data structure. It is of type number.
984
- * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
985
- * bounds.
986
- */
987
- deleteAt(index) {
988
- if (index < 0 || index >= this._length) return;
989
- let deleted;
990
- if (index === 0) {
991
- deleted = this.first;
992
- this.shift();
993
- return deleted;
994
- }
995
- const targetNode = this.getNodeAt(index);
996
- const prevNode = this._getPrevNode(targetNode);
997
- if (prevNode && targetNode) {
998
- deleted = targetNode.value;
999
- prevNode.next = targetNode.next;
1000
- if (targetNode === this.tail) this._tail = prevNode;
1001
- this._length--;
1002
- return deleted;
1003
- }
1004
- return;
1005
- }
1006
- /**
1007
- * Time Complexity: O(n)
1008
- * Space Complexity: O(1)
1009
- *
1010
- * The delete function removes a node with a specific value from a singly linked list.
1011
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can accept either a value of type `E`
1012
- * or a `SinglyLinkedListNode<E>` object.
1013
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
1014
- * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
1015
- */
1016
- delete(elementOrNode) {
1017
- if (elementOrNode === void 0 || !this.head) return false;
1018
- const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
1019
- if (!node) return false;
1020
- const prevNode = this._getPrevNode(node);
1021
- if (!prevNode) {
1022
- this._head = node.next;
1023
- if (node === this.tail) this._tail = void 0;
1024
- } else {
1025
- prevNode.next = node.next;
1026
- if (node === this.tail) this._tail = prevNode;
1027
- }
1028
- this._length--;
1029
- return true;
1030
- }
1031
- /**
1032
- * Time Complexity: O(n)
1033
- * Space Complexity: O(1)
1034
- *
1035
- * The `addAt` function inserts a new element or node at a specified index in a singly linked list.
1036
- * @param {number} index - The `index` parameter represents the position at which you want to add a
1037
- * new element or node in the linked list. It is a number that indicates the index where the new
1038
- * element or node should be inserted.
1039
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1040
- * `addAt` method can be either a value of type `E` or a `SinglyLinkedListNode<E>` object. This
1041
- * parameter represents the element or node that you want to add to the linked list at the specified
1042
- * index.
1043
- * @returns The `addAt` method returns a boolean value - `true` if the element or node was
1044
- * successfully added at the specified index, and `false` if the index is out of bounds.
1045
- */
1046
- addAt(index, newElementOrNode) {
1047
- if (index < 0 || index > this._length) return false;
1048
- if (index === 0) {
1049
- this.unshift(newElementOrNode);
1050
- return true;
1051
- }
1052
- if (index === this._length) {
1053
- this.push(newElementOrNode);
1054
- return true;
1055
- }
1056
- const newNode = this._ensureNode(newElementOrNode);
1057
- const prevNode = this.getNodeAt(index - 1);
1058
- newNode.next = prevNode.next;
1059
- prevNode.next = newNode;
1060
- this._length++;
1061
- return true;
1062
- }
1063
- /**
1064
- * Time Complexity: O(n)
1065
- * Space Complexity: O(1)
1066
- *
1067
- * The function setAt(index, value) updates the value at a specified index in a data structure if the
1068
- * index exists.
1069
- * @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
1070
- * data structure where you want to set a new value.
1071
- * @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
1072
- * want to set at the specified index in the data structure.
1073
- * @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
1074
- * is successfully updated, and `false` if the index is out of bounds (i.e., the node at that index
1075
- * does not exist).
1076
- */
1077
- setAt(index, value) {
1078
- const node = this.getNodeAt(index);
1079
- if (node) {
1080
- node.value = value;
1081
- return true;
1082
- }
1083
- return false;
1084
- }
1085
- /**
1086
- * Time Complexity: O(1)
1087
- * Space Complexity: O(1)
1088
- *
1089
- * The function checks if the length of a data structure is equal to zero and returns a boolean value indicating
1090
- * whether it is empty or not.
1091
- * @returns A boolean value indicating whether the length of the object is equal to 0.
1092
- */
1093
- isEmpty() {
1094
- return this._length === 0;
1095
- }
1096
- /**
1097
- * Time Complexity: O(1)
1098
- * Space Complexity: O(1)
1099
- *
1100
- * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
1101
- */
1102
- clear() {
1103
- this._head = void 0;
1104
- this._tail = void 0;
1105
- this._length = 0;
1106
- }
1107
- /**
1108
- * Time Complexity: O(n)
1109
- * Space Complexity: O(1)
1110
- *
1111
- * The `reverse` function reverses the order of the nodes in a singly linked list.
1112
- * @returns The reverse() method does not return anything. It has a return type of void.
1113
- */
1114
- reverse() {
1115
- if (!this.head || this.head === this.tail) return this;
1116
- let prev = void 0;
1117
- let current = this.head;
1118
- let next = void 0;
1119
- while (current) {
1120
- next = current.next;
1121
- current.next = prev;
1122
- prev = current;
1123
- current = next;
1124
- }
1125
- [this._head, this._tail] = [this.tail, this.head];
1126
- return this;
1127
- }
1128
- /**
1129
- * Time Complexity: O(n)
1130
- * Space Complexity: O(1)
1131
- *
1132
- * The function `getNode` in TypeScript searches for a node in a singly linked list based on a given
1133
- * element, node, or predicate.
1134
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean) | undefined} elementNodeOrPredicate
1135
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getNode` method can be one
1136
- * of the following types:
1137
- * @returns The `getNode` method returns either a `SinglyLinkedListNode<E>` if a matching node is
1138
- * found based on the provided predicate, or it returns `undefined` if no matching node is found or
1139
- * if the input parameter is `undefined`.
1140
- */
1141
- getNode(elementNodeOrPredicate) {
1142
- if (elementNodeOrPredicate === void 0) return;
1143
- if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
1144
- const predicate = this._ensurePredicate(elementNodeOrPredicate);
1145
- let current = this.head;
1146
- while (current) {
1147
- if (predicate(current)) {
1148
- return current;
1149
- }
1150
- current = current.next;
1151
- }
1152
- return void 0;
1153
- }
1154
- /**
1155
- * Time Complexity: O(n)
1156
- * Space Complexity: O(1)
1157
- *
1158
- * The function `addBefore` in TypeScript adds a new element or node before an existing element or
1159
- * node in a singly linked list.
1160
- * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
1161
- * element or node in the linked list before which you want to add a new element or node.
1162
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1163
- * `addBefore` method represents the element or node that you want to insert before the existing
1164
- * element or node in the linked list. This new element can be of type `E` or a
1165
- * `SinglyLinkedListNode<E>`.
1166
- * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
1167
- * successfully added before the existing element or node, and `false` if the operation was
1168
- * unsuccessful.
1169
- */
1170
- addBefore(existingElementOrNode, newElementOrNode) {
1171
- const existingNode = this.getNode(existingElementOrNode);
1172
- if (!existingNode) return false;
1173
- const prevNode = this._getPrevNode(existingNode);
1174
- const newNode = this._ensureNode(newElementOrNode);
1175
- if (!prevNode) {
1176
- this.unshift(newNode);
1177
- } else {
1178
- prevNode.next = newNode;
1179
- newNode.next = existingNode;
1180
- this._length++;
1181
- }
1182
- return true;
1183
- }
1184
- /**
1185
- * Time Complexity: O(n)
1186
- * Space Complexity: O(1)
1187
- *
1188
- * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
1189
- * in a singly linked list.
1190
- * @param {E | SinglyLinkedListNode<E>} existingElementOrNode - existingElementOrNode can be either
1191
- * an element of type E or a SinglyLinkedListNode of type E.
1192
- * @param {E | SinglyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
1193
- * `addAfter` method represents the element or node that you want to add after the existing element
1194
- * or node in a singly linked list. This parameter can be either the value of the new element or a
1195
- * reference to a `SinglyLinkedListNode` containing
1196
- * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
1197
- * successfully added after the existing element or node, and `false` if the existing element or node
1198
- * was not found.
1199
- */
1200
- addAfter(existingElementOrNode, newElementOrNode) {
1201
- const existingNode = this.getNode(existingElementOrNode);
1202
- if (existingNode) {
1203
- const newNode = this._ensureNode(newElementOrNode);
1204
- newNode.next = existingNode.next;
1205
- existingNode.next = newNode;
1206
- if (existingNode === this.tail) {
1207
- this._tail = newNode;
1208
- }
1209
- this._length++;
1210
- return true;
1211
- }
1212
- return false;
1213
- }
1214
- /**
1215
- * Time Complexity: O(n)
1216
- * Space Complexity: O(1)
1217
- *
1218
- * The function `splice` in TypeScript overrides the default behavior to remove and insert elements
1219
- * in a singly linked list while handling boundary cases.
1220
- * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
1221
- * to start modifying the list. It specifies the position where elements will be added or removed.
1222
- * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
1223
- * number of elements to remove from the array starting at the specified `start` index. If
1224
- * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
1225
- * elements can still be inserted at
1226
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements to be
1227
- * inserted into the list at the specified `start` index. These elements will be inserted in place of
1228
- * the elements that are removed from the list. The `splice` method allows you to add new elements to
1229
- * the list while
1230
- * @returns The `splice` method is returning a `SinglyLinkedList` containing the elements that were
1231
- * removed from the original list during the splice operation.
1232
- */
1233
- splice(start, deleteCount = 0, ...items) {
1234
- const removedList = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
1235
- start = Math.max(0, Math.min(start, this.length));
1236
- deleteCount = Math.max(0, deleteCount);
1237
- const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
1238
- const startNode = prevNode ? prevNode.next : this.head;
1239
- let current = startNode;
1240
- for (let i = 0; i < deleteCount && current; i++) {
1241
- removedList.push(current.value);
1242
- current = current.next;
1243
- }
1244
- const nextNode = current;
1245
- let lastInsertedNode = void 0;
1246
- for (const item of items) {
1247
- const newNode = this._ensureNode(item);
1248
- if (!lastInsertedNode) {
1249
- if (prevNode) {
1250
- prevNode.next = newNode;
1251
- } else {
1252
- this._head = newNode;
1253
- }
1254
- } else {
1255
- lastInsertedNode.next = newNode;
1256
- }
1257
- lastInsertedNode = newNode;
1258
- }
1259
- if (lastInsertedNode) {
1260
- lastInsertedNode.next = nextNode;
1261
- } else if (prevNode) {
1262
- prevNode.next = nextNode;
1263
- }
1264
- if (!nextNode) {
1265
- this._tail = lastInsertedNode || prevNode;
1266
- }
1267
- this._length += items.length - removedList.length;
1268
- return removedList;
1269
- }
1270
- /**
1271
- * Time Complexity: O(n)
1272
- * Space Complexity: O(1)
1273
- *
1274
- * The function `countOccurrences` iterates through a singly linked list and counts the occurrences
1275
- * of a specified element or nodes that satisfy a given predicate.
1276
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementOrNode
1277
- * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
1278
- * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
1279
- * node, or predicate function in the singly linked list.
1280
- */
1281
- countOccurrences(elementOrNode) {
1282
- const predicate = this._ensurePredicate(elementOrNode);
1283
- let count = 0;
1284
- let current = this.head;
1285
- while (current) {
1286
- if (predicate(current)) {
1287
- count++;
1288
- }
1289
- current = current.next;
1290
- }
1291
- return count;
1292
- }
1293
- /**
1294
- * Time Complexity: O(n)
1295
- * Space Complexity: O(n)
1296
- *
1297
- * The `clone` function returns a new instance of the `SinglyLinkedList` class with the same values
1298
- * as the original list.
1299
- * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
1300
- * is a clone of the original list.
1301
- */
1302
- clone() {
1303
- return new _SinglyLinkedList(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
1304
- }
1305
- /**
1306
- * Time Complexity: O(n)
1307
- * Space Complexity: O(n)
1308
- *
1309
- * The `filter` function creates a new SinglyLinkedList by iterating over the elements of the current
1310
- * list and applying a callback function to each element to determine if it should be included in the
1311
- * filtered list.
1312
- * @param callback - The callback parameter is a function that will be called for each element in the
1313
- * list. It takes three arguments: the current element, the index of the current element, and the
1314
- * list itself. The callback function should return a boolean value indicating whether the current
1315
- * element should be included in the filtered list or not
1316
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
1317
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
1318
- * passed as the `this` value to the `callback` function. If `thisArg` is
1319
- * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
1320
- * elements that pass the filter condition specified by the `callback` function.
1321
- */
1322
- filter(callback, thisArg) {
1323
- const filteredList = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
1324
- let index = 0;
1325
- for (const current of this) {
1326
- if (callback.call(thisArg, current, index, this)) {
1327
- filteredList.push(current);
1328
- }
1329
- index++;
1330
- }
1331
- return filteredList;
1332
- }
1333
- /**
1334
- * Time Complexity: O(n)
1335
- * Space Complexity: O(n)
1336
- *
1337
- * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
1338
- * of applying the callback to each element in the original list.
1339
- * @param callback - The `callback` parameter is a function that will be called for each element in
1340
- * the original list. It takes three arguments: `current` (the current element being processed),
1341
- * `index` (the index of the current element), and `this` (the original list). It should return a
1342
- * value
1343
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
1344
- * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
1345
- * input and returns the converted element. If this parameter is not provided, the raw element will
1346
- * be used as is.
1347
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
1348
- * specify the value of `this` within the callback function. It is used to set the context or scope
1349
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
1350
- * value of
1351
- * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
1352
- */
1353
- map(callback, toElementFn, thisArg) {
1354
- const mappedList = new _SinglyLinkedList([], { toElementFn, maxLen: this._maxLen });
1355
- let index = 0;
1356
- for (const current of this) {
1357
- mappedList.push(callback.call(thisArg, current, index, this));
1358
- index++;
1359
- }
1360
- return mappedList;
1361
- }
1362
- /**
1363
- * The function `_createInstance` returns a new instance of `SinglyLinkedList` with the specified
1364
- * options.
1365
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
1366
- * `SinglyLinkedListOptions<E, R>`, which is used to configure the behavior of the `SinglyLinkedList`
1367
- * instance being created. It is an optional parameter, meaning it can be omitted when calling the
1368
- * method.
1369
- * @returns An instance of the `SinglyLinkedList` class with an empty array and the provided options
1370
- * is being returned.
1371
- */
1372
- _createInstance(options) {
1373
- return new _SinglyLinkedList([], options);
1374
- }
1375
- /**
1376
- * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
1377
- */
1378
- *_getIterator() {
1379
- let current = this.head;
1380
- while (current) {
1381
- yield current.value;
1382
- current = current.next;
1383
- }
1384
- }
1385
- /**
1386
- * The function returns an iterator that iterates over the elements of a collection in reverse order.
1387
- */
1388
- *_getReverseIterator() {
1389
- const reversedArr = [...this].reverse();
1390
- for (const item of reversedArr) {
1391
- yield item;
1392
- }
1393
- }
1394
- /**
1395
- * The function `_getNodeIterator` returns an iterator that iterates over the nodes of a singly
1396
- * linked list.
1397
- */
1398
- *_getNodeIterator() {
1399
- let current = this.head;
1400
- while (current) {
1401
- yield current;
1402
- current = current.next;
1403
- }
1404
- }
1405
- // protected *_getReverseNodeIterator(): IterableIterator<SinglyLinkedListNode<E>> {
1406
- // const reversedArr = [...this._getNodeIterator()].reverse();
1407
- //
1408
- // for (const item of reversedArr) {
1409
- // yield item;
1410
- // }
1411
- // }
1412
- /**
1413
- * The _isPredicate function in TypeScript checks if the input is a function that takes a
1414
- * SinglyLinkedListNode as an argument and returns a boolean.
1415
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1416
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
1417
- * @returns The _isPredicate method is returning a boolean value based on whether the
1418
- * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
1419
- * function, the method will return true, indicating that it is a predicate function. If it is not a
1420
- * function, the method will return false.
1421
- */
1422
- _isPredicate(elementNodeOrPredicate) {
1423
- return typeof elementNodeOrPredicate === "function";
1424
- }
1425
- /**
1426
- * The function `_ensureNode` ensures that the input is a valid node and returns it, creating a new
1427
- * node if necessary.
1428
- * @param {E | SinglyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
1429
- * an element of type `E` or a `SinglyLinkedListNode` containing an element of type `E`.
1430
- * @returns A SinglyLinkedListNode<E> object is being returned.
1431
- */
1432
- _ensureNode(elementOrNode) {
1433
- if (this.isNode(elementOrNode)) return elementOrNode;
1434
- return new SinglyLinkedListNode(elementOrNode);
1435
- }
1436
- /**
1437
- * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
1438
- * function, or a value to compare with the node's value.
1439
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
1440
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following types:
1441
- * @returns A function is being returned. If the input `elementNodeOrPredicate` is already a node, a
1442
- * function is returned that checks if a given node is equal to the input node. If the input is a
1443
- * predicate function, it is returned as is. If the input is neither a node nor a predicate function,
1444
- * a function is returned that checks if a given node's value is equal to the input
1445
- */
1446
- _ensurePredicate(elementNodeOrPredicate) {
1447
- if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
1448
- if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
1449
- return (node) => node.value === elementNodeOrPredicate;
1450
- }
1451
- /**
1452
- * The function `_getPrevNode` returns the node before a given node in a singly linked list.
1453
- * @param node - The `node` parameter in the `_getPrevNode` method is a reference to a node in a
1454
- * singly linked list. The method is used to find the node that comes before the given node in the
1455
- * linked list.
1456
- * @returns The `_getPrevNode` method returns either the previous node of the input node in a singly
1457
- * linked list or `undefined` if the input node is the head of the list or if the input node is not
1458
- * found in the list.
1459
- */
1460
- _getPrevNode(node) {
1461
- if (!this.head || this.head === node) return void 0;
1462
- let current = this.head;
1463
- while (current.next && current.next !== node) {
1464
- current = current.next;
1465
- }
1466
- return current.next === node ? current : void 0;
1467
- }
1468
- };
1469
- export {
1470
- SinglyLinkedList,
1471
- SinglyLinkedListNode
1472
- };
1473
- /**
1474
- * data-structure-typed
1475
- *
1476
- * @author Pablo Zeng
1477
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1478
- * @license MIT License
1479
- */