doubly-linked-list-typed 1.54.2 → 2.0.0

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 (85) hide show
  1. package/README.md +7 -7
  2. package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
  3. package/dist/data-structures/base/iterable-element-base.js +14 -11
  4. package/dist/data-structures/base/linear-base.d.ts +277 -0
  5. package/dist/data-structures/base/linear-base.js +552 -0
  6. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  7. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  10. package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
  11. package/dist/data-structures/binary-tree/avl-tree.js +76 -8
  12. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  13. package/dist/data-structures/binary-tree/binary-tree.js +244 -149
  14. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  15. package/dist/data-structures/binary-tree/bst.js +89 -133
  16. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  17. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  18. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  19. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  20. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  21. package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
  22. package/dist/data-structures/graph/abstract-graph.js +2 -2
  23. package/dist/data-structures/heap/heap.d.ts +3 -11
  24. package/dist/data-structures/heap/heap.js +0 -10
  25. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  26. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  30. package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  32. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  33. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  34. package/dist/data-structures/queue/deque.d.ts +130 -91
  35. package/dist/data-structures/queue/deque.js +269 -169
  36. package/dist/data-structures/queue/queue.d.ts +84 -40
  37. package/dist/data-structures/queue/queue.js +134 -50
  38. package/dist/data-structures/stack/stack.d.ts +3 -11
  39. package/dist/data-structures/stack/stack.js +0 -10
  40. package/dist/data-structures/trie/trie.d.ts +4 -3
  41. package/dist/data-structures/trie/trie.js +3 -0
  42. package/dist/types/data-structures/base/base.d.ts +9 -4
  43. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  44. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  45. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  46. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  47. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  48. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  49. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  50. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  51. package/dist/utils/utils.d.ts +2 -2
  52. package/package.json +2 -2
  53. package/src/data-structures/base/iterable-element-base.ts +29 -20
  54. package/src/data-structures/base/linear-base.ts +649 -0
  55. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  56. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  57. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  58. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  59. package/src/data-structures/binary-tree/bst.ts +150 -152
  60. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  61. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  62. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  63. package/src/data-structures/graph/abstract-graph.ts +2 -2
  64. package/src/data-structures/heap/heap.ts +3 -14
  65. package/src/data-structures/heap/max-heap.ts +2 -2
  66. package/src/data-structures/heap/min-heap.ts +2 -2
  67. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  68. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  69. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  70. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  71. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  72. package/src/data-structures/queue/deque.ts +286 -183
  73. package/src/data-structures/queue/queue.ts +149 -63
  74. package/src/data-structures/stack/stack.ts +3 -18
  75. package/src/data-structures/trie/trie.ts +7 -3
  76. package/src/types/data-structures/base/base.ts +17 -8
  77. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  78. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  79. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  80. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  81. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  82. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  83. package/src/types/data-structures/queue/deque.ts +2 -3
  84. package/src/types/data-structures/queue/queue.ts +2 -2
  85. package/src/utils/utils.ts +2 -2
@@ -0,0 +1,649 @@
1
+ import { ElementCallback, LinearBaseOptions, ReduceLinearCallback } from '../../types';
2
+ import { IterableElementBase } from './iterable-element-base';
3
+
4
+ export class LinkedListNode<E = any> {
5
+ constructor(value: E) {
6
+ this._value = value;
7
+ this._next = undefined;
8
+ }
9
+
10
+ protected _value: E;
11
+
12
+ get value(): E {
13
+ return this._value;
14
+ }
15
+
16
+ set value(value: E) {
17
+ this._value = value;
18
+ }
19
+
20
+ protected _next: LinkedListNode<E> | undefined;
21
+
22
+ get next(): LinkedListNode<E> | undefined {
23
+ return this._next;
24
+ }
25
+
26
+ set next(value: LinkedListNode<E> | undefined) {
27
+ this._next = value;
28
+ }
29
+ }
30
+
31
+ export abstract class LinearBase<
32
+ E,
33
+ R = any,
34
+ NODE extends LinkedListNode<E> = LinkedListNode<E>
35
+ > extends IterableElementBase<E, R> {
36
+ /**
37
+ * The constructor initializes the LinearBase class with optional options, setting the maximum length
38
+ * if provided.
39
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
40
+ * constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
41
+ * object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
42
+ */
43
+ protected constructor(options?: LinearBaseOptions<E, R>) {
44
+ super(options);
45
+ if (options) {
46
+ const { maxLen } = options;
47
+ if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
48
+ }
49
+ }
50
+
51
+ abstract get length(): number;
52
+
53
+ protected _maxLen: number = -1;
54
+
55
+ get maxLen() {
56
+ return this._maxLen;
57
+ }
58
+
59
+ /**
60
+ * Time Complexity: O(n)
61
+ * Space Complexity: O(1)
62
+ *
63
+ * The function indexOf searches for a specified element starting from a given index in an array-like
64
+ * object and returns the index of the first occurrence, or -1 if not found.
65
+ * @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
66
+ * element that you want to find within the array. The function will search for this element starting
67
+ * from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
68
+ * within the
69
+ * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
70
+ * index at which to start searching for the `searchElement` within the array. If provided, the
71
+ * search will begin at this index and continue to the end of the array. If `fromIndex` is not
72
+ * specified, the default
73
+ * @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
74
+ * array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
75
+ */
76
+ indexOf(searchElement: E, fromIndex: number = 0): number {
77
+ // Boundary checks and adjustments
78
+ if (this.length === 0) return -1;
79
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
80
+ if (fromIndex < 0) fromIndex = 0;
81
+
82
+ // Iterating from the specified index to the end
83
+ for (let i = fromIndex; i < this.length; i++) {
84
+ const element = this.at(i);
85
+ if (element === searchElement) return i;
86
+ }
87
+
88
+ return -1; // Not found
89
+ }
90
+
91
+ /**
92
+ * Time Complexity: O(n)
93
+ * Space Complexity: O(1)
94
+ *
95
+ * The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
96
+ * element in an array.
97
+ * @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
98
+ * last index of within the array. The `lastIndexOf` method will search the array starting from the
99
+ * `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
100
+ * of the
101
+ * @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
102
+ * index at which to start searching for the `searchElement` in the array. By default, it starts
103
+ * searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
104
+ * provided
105
+ * @returns The last index of the `searchElement` in the array is being returned. If the
106
+ * `searchElement` is not found in the array, -1 is returned.
107
+ */
108
+ lastIndexOf(searchElement: E, fromIndex: number = this.length - 1): number {
109
+ if (this.length === 0) return -1;
110
+ if (fromIndex >= this.length) fromIndex = this.length - 1;
111
+ if (fromIndex < 0) fromIndex = this.length + fromIndex;
112
+
113
+ for (let i = fromIndex; i >= 0; i--) {
114
+ const element = this.at(i);
115
+ if (element === searchElement) return i;
116
+ }
117
+
118
+ return -1;
119
+ }
120
+
121
+ /**
122
+ * Time Complexity: O(n)
123
+ * Space Complexity: O(1)
124
+ *
125
+ * The `findIndex` function iterates over an array and returns the index of the first element that
126
+ * satisfies the provided predicate function.
127
+ * @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
128
+ * that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
129
+ * value indicating whether the current element satisfies the condition being checked for.
130
+ * @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
131
+ * parameter that specifies the value to use as `this` when executing the `predicate` function. If
132
+ * provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
133
+ * @returns The `findIndex` method is returning the index of the first element in the array that
134
+ * satisfies the provided predicate function. If no such element is found, it returns -1.
135
+ */
136
+ findIndex(predicate: ElementCallback<E, R, boolean>, thisArg?: any): number {
137
+ for (let i = 0; i < this.length; i++) {
138
+ const item = this.at(i);
139
+ if (item !== undefined && predicate.call(thisArg, item, i, this)) return i;
140
+ }
141
+ return -1;
142
+ }
143
+
144
+ concat(...items: this[]): this;
145
+
146
+ /**
147
+ * Time Complexity: O(n + m)
148
+ * Space Complexity: O(n + m)
149
+ *
150
+ * The `concat` function in TypeScript concatenates multiple items into a new list, handling both
151
+ * individual elements and instances of `LinearBase`.
152
+ * @param {(E | this)[]} items - The `concat` method takes in an array of items, where
153
+ * each item can be either of type `E` or an instance of `LinearBase<E, R>`.
154
+ * @returns The `concat` method is returning a new instance of the class that it belongs to, with the
155
+ * items passed as arguments concatenated to it.
156
+ */
157
+ concat(...items: (E | this)[]): this {
158
+ const newList = this.clone();
159
+
160
+ for (const item of items) {
161
+ if (item instanceof LinearBase) {
162
+ newList.pushMany(item);
163
+ } else {
164
+ newList.push(item);
165
+ }
166
+ }
167
+
168
+ return newList;
169
+ }
170
+
171
+ /**
172
+ * Time Complexity: O(n log n)
173
+ * Space Complexity: O(n)
174
+ *
175
+ * The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
176
+ * function.
177
+ * @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
178
+ * two elements `a` and `b` as input and returns a number indicating their relative order. If the
179
+ * returned value is negative, `a` comes before `b`. If the returned value is positive, `
180
+ * @returns The `sort` method is returning the instance of the object on which it is called (this),
181
+ * after sorting the elements based on the provided comparison function (compareFn).
182
+ */
183
+ sort(compareFn?: (a: E, b: E) => number): this {
184
+ const arr = this.toArray();
185
+ arr.sort(compareFn);
186
+ this.clear();
187
+ for (const item of arr) this.push(item);
188
+ return this;
189
+ }
190
+
191
+ /**
192
+ * Time Complexity: O(n + m)
193
+ * Space Complexity: O(m)
194
+ *
195
+ * The `splice` function in TypeScript removes elements from an array and optionally inserts new
196
+ * elements at the specified index.
197
+ * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
198
+ * to start modifying the array. If `start` is a negative number, it will count from the end of the
199
+ * array.
200
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
201
+ * number of elements to remove from the array starting at the specified `start` index. If
202
+ * `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
203
+ * at the `start`
204
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
205
+ * will be inserted into the array at the specified `start` index. These elements can be of any type
206
+ * and you can pass multiple elements separated by commas. The `splice` method will insert these
207
+ * items into the array at the
208
+ * @returns The `splice` method returns a list of elements that were removed from the original list
209
+ * during the operation.
210
+ */
211
+ splice(start: number, deleteCount: number = 0, ...items: E[]): this {
212
+ const removedList = this._createInstance();
213
+
214
+ // Handling negative indexes and bounds
215
+ start = start < 0 ? this.length + start : start;
216
+ start = Math.max(0, Math.min(start, this.length));
217
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
218
+
219
+ // Delete elements
220
+ for (let i = 0; i < deleteCount; i++) {
221
+ const removed = this.deleteAt(start); // Always delete the start position
222
+ if (removed !== undefined) {
223
+ removedList.push(removed); // Add removed elements to the returned list
224
+ }
225
+ }
226
+
227
+ // Insert new element
228
+ for (let i = 0; i < items.length; i++) {
229
+ this.addAt(start + i, items[i]); // Insert new elements one by one at the current position
230
+ }
231
+
232
+ return removedList; // Returns a list of removed elements
233
+ }
234
+
235
+ /**
236
+ * Time Complexity: O(n)
237
+ * Space Complexity: O(1)
238
+ *
239
+ * The `join` function in TypeScript returns a string by joining the elements of an array with a
240
+ * specified separator.
241
+ * @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
242
+ * or characters that will be used to separate each element when joining them into a single string.
243
+ * By default, the separator is set to a comma (`,`), but you can provide a different separator if
244
+ * needed.
245
+ * @returns The `join` method is being returned, which takes an optional `separator` parameter
246
+ * (defaulting to a comma) and returns a string created by joining all elements of the array after
247
+ * converting it to an array.
248
+ */
249
+ join(separator: string = ','): string {
250
+ return this.toArray().join(separator);
251
+ }
252
+
253
+ /**
254
+ * Time Complexity: O(n)
255
+ * Space Complexity: O(n)
256
+ *
257
+ * The function `toReversedArray` takes an array and returns a new array with its elements in reverse
258
+ * order.
259
+ * @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
260
+ * order.
261
+ */
262
+ toReversedArray(): E[] {
263
+ const array: E[] = [];
264
+ for (let i = this.length - 1; i >= 0; i--) {
265
+ array.push(this.at(i)!);
266
+ }
267
+ return array;
268
+ }
269
+
270
+ reduceRight(callbackfn: ReduceLinearCallback<E>): E;
271
+
272
+ reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
273
+
274
+ reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
275
+
276
+ /**
277
+ * Time Complexity: O(n)
278
+ * Space Complexity: O(1)
279
+ *
280
+ * The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
281
+ * callback function to each element, accumulating a single result.
282
+ * @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
283
+ * be called on each element in the array from right to left. It takes four arguments:
284
+ * @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
285
+ * optional parameter that specifies the initial value of the accumulator. If provided, the
286
+ * `accumulator` will start with this initial value before iterating over the elements of the array.
287
+ * If `initialValue` is not provided, the accumulator will
288
+ * @returns The `reduceRight` method is returning the final accumulated value after applying the
289
+ * callback function to each element in the array from right to left.
290
+ */
291
+ reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
292
+ let accumulator = initialValue ?? (0 as U);
293
+ for (let i = this.length - 1; i >= 0; i--) {
294
+ accumulator = callbackfn(accumulator, this.at(i)!, i, this);
295
+ }
296
+ return accumulator;
297
+ }
298
+
299
+ /**
300
+ * Time Complexity: O(m)
301
+ * Space Complexity: O(m)
302
+ *
303
+ * The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
304
+ * the original instance based on the specified start and end indices.
305
+ * @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
306
+ * which to begin extracting elements from an array-like object. If no `start` parameter is provided,
307
+ * the default value is 0, meaning the extraction will start from the beginning of the array.
308
+ * @param {number} end - The `end` parameter in the `slice` method represents the index at which to
309
+ * end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
310
+ * array (i.e., `this.length`).
311
+ * @returns The `slice` method is returning a new instance of the object with elements sliced from
312
+ * the specified start index (default is 0) to the specified end index (default is the length of the
313
+ * object).
314
+ */
315
+ slice(start: number = 0, end: number = this.length): this {
316
+ start = start < 0 ? this.length + start : start;
317
+ end = end < 0 ? this.length + end : end;
318
+
319
+ const newList = this._createInstance();
320
+ for (let i = start; i < end; i++) {
321
+ newList.push(this.at(i)!);
322
+ }
323
+ return newList;
324
+ }
325
+
326
+ /**
327
+ * Time Complexity: O(n)
328
+ * Space Complexity: O(1)
329
+ *
330
+ * The `fill` function in TypeScript fills a specified range in an array-like object with a given
331
+ * value.
332
+ * @param {E} value - The `value` parameter in the `fill` method represents the element that will be
333
+ * used to fill the specified range in the array.
334
+ * @param [start=0] - The `start` parameter specifies the index at which to start filling the array
335
+ * with the specified value. If not provided, it defaults to 0, indicating the beginning of the
336
+ * array.
337
+ * @param end - The `end` parameter in the `fill` function represents the index at which the filling
338
+ * of values should stop. It specifies the end of the range within the array where the `value` should
339
+ * be filled.
340
+ * @returns The `fill` method is returning the modified object (`this`) after filling the specified
341
+ * range with the provided value.
342
+ */
343
+ fill(value: E, start = 0, end = this.length): this {
344
+ // Handling negative indexes
345
+ start = start < 0 ? this.length + start : start;
346
+ end = end < 0 ? this.length + end : end;
347
+
348
+ // Boundary processing
349
+ if (start < 0) start = 0;
350
+ if (end > this.length) end = this.length;
351
+ if (start >= end) return this;
352
+
353
+ // Iterate through the specified range and fill in the values
354
+ for (let i = start; i < end; i++) {
355
+ this.setAt(i, value);
356
+ }
357
+
358
+ return this;
359
+ }
360
+
361
+ abstract setAt(index: number, value: E): boolean;
362
+
363
+ abstract override clone(): this;
364
+
365
+ abstract reverse(): this;
366
+
367
+ abstract push(elementOrNode: E | NODE): boolean;
368
+
369
+ abstract pushMany(elements: Iterable<E> | Iterable<R> | Iterable<NODE>): boolean[];
370
+
371
+ abstract delete(elementOrNode: E | NODE | undefined): boolean;
372
+
373
+ abstract at(index: number): E | undefined;
374
+
375
+ abstract deleteAt(pos: number): E | undefined;
376
+
377
+ abstract addAt(index: number, newElementOrNode: E | NODE): boolean;
378
+
379
+ protected abstract _createInstance(options?: LinearBaseOptions<E, R>): this;
380
+
381
+ protected abstract _getReverseIterator(...args: any[]): IterableIterator<E>;
382
+ }
383
+
384
+ export abstract class LinearLinkedBase<
385
+ E,
386
+ R = any,
387
+ NODE extends LinkedListNode<E> = LinkedListNode<E>
388
+ > extends LinearBase<E, R, NODE> {
389
+ /**
390
+ * The constructor initializes the LinearBase class with optional options, setting the maximum length
391
+ * if provided and valid.
392
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
393
+ * constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
394
+ * `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
395
+ */
396
+ protected constructor(options?: LinearBaseOptions<E, R>) {
397
+ super(options);
398
+ if (options) {
399
+ const { maxLen } = options;
400
+ if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Time Complexity: O(n)
406
+ * Space Complexity: O(1)
407
+ *
408
+ * The function overrides the indexOf method to improve performance by searching for an element in a
409
+ * custom array implementation starting from a specified index.
410
+ * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
411
+ * within the array. The `indexOf` method will return the index of the first occurrence of this
412
+ * element within the array.
413
+ * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
414
+ * index in the array at which to start the search for the `searchElement`. If provided, the search
415
+ * will begin at the specified index and continue to the end of the array. If not provided, the
416
+ * search will start at index
417
+ * @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
418
+ * array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
419
+ */
420
+ override indexOf(searchElement: E, fromIndex: number = 0): number {
421
+ // In order to improve performance, it is best to override this method in the subclass of the array implementation
422
+ const iterator = this._getIterator();
423
+ let current = iterator.next();
424
+
425
+ let index = 0;
426
+ while (index < fromIndex) {
427
+ current = iterator.next();
428
+ index++;
429
+ }
430
+
431
+ while (!current.done) {
432
+ if (current.value === searchElement) return index;
433
+ current = iterator.next();
434
+ index++;
435
+ }
436
+
437
+ return -1;
438
+ }
439
+
440
+ /**
441
+ * Time Complexity: O(n)
442
+ * Space Complexity: O(1)
443
+ *
444
+ * The function overrides the lastIndexOf method in TypeScript to improve performance by searching
445
+ * for an element in reverse order starting from a specified index.
446
+ * @param {E} searchElement - The `searchElement` parameter is the element that you want to find
447
+ * within the array. The `lastIndexOf` method searches the array for this element starting from the
448
+ * end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
449
+ * occurrence of the element
450
+ * @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
451
+ * index at which to start searching for the `searchElement` in the array. If provided, the search
452
+ * will begin at this index and move towards the beginning of the array. If not provided, the search
453
+ * will start at the
454
+ * @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
455
+ * from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
456
+ * reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
457
+ * occurrence of the `searchElement` if found, or -1 if not found.
458
+ */
459
+ override lastIndexOf(searchElement: E, fromIndex: number = this.length - 1): number {
460
+ // In order to improve performance, it is best to override this method in the subclass of the array implementation
461
+ const iterator = this._getReverseIterator();
462
+ let current = iterator.next();
463
+
464
+ let index = this.length - 1;
465
+ while (index > fromIndex) {
466
+ current = iterator.next();
467
+ index--;
468
+ }
469
+
470
+ while (!current.done) {
471
+ if (current.value === searchElement) return index;
472
+ current = iterator.next();
473
+ index--;
474
+ }
475
+
476
+ return -1;
477
+ }
478
+
479
+ override concat(...items: LinearBase<E, R>[]): this;
480
+
481
+ /**
482
+ * Time Complexity: O(n + m)
483
+ * Space Complexity: O(n + m)
484
+ *
485
+ * The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
486
+ * list, handling both individual elements and instances of `LinearBase`.
487
+ * @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
488
+ * number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
489
+ * current list and returns a new list with the concatenated items.
490
+ * @returns The `concat` method is returning a new instance of the class that it belongs to, with the
491
+ * items passed as arguments concatenated to it.
492
+ */
493
+ override concat(...items: (E | LinearBase<E, R>)[]): this {
494
+ const newList = this.clone();
495
+
496
+ for (const item of items) {
497
+ if (item instanceof LinearBase) {
498
+ newList.pushMany(item);
499
+ } else {
500
+ newList.push(item);
501
+ }
502
+ }
503
+
504
+ return newList;
505
+ }
506
+
507
+ /**
508
+ * Time Complexity: O(m)
509
+ * Space Complexity: O(m)
510
+ *
511
+ * The `slice` method is overridden to improve performance by creating a new instance and iterating
512
+ * through the array to extract a subset based on the specified start and end indices.
513
+ * @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
514
+ * which to begin extracting elements from the array. If no `start` parameter is provided, the
515
+ * default value is 0, indicating that extraction should start from the beginning of the array.
516
+ * @param {number} end - The `end` parameter in the `slice` method represents the index at which to
517
+ * end the slicing of the array. If not provided, it defaults to the length of the array.
518
+ * @returns The `slice` method is returning a new instance of the array implementation with elements
519
+ * sliced from the original array based on the `start` and `end` parameters.
520
+ */
521
+ override slice(start: number = 0, end: number = this.length): this {
522
+ // In order to improve performance, it is best to override this method in the subclass of the array implementation
523
+ start = start < 0 ? this.length + start : start;
524
+ end = end < 0 ? this.length + end : end;
525
+
526
+ const newList = this._createInstance();
527
+ const iterator = this._getIterator();
528
+ let current = iterator.next();
529
+ let c = 0;
530
+ while (c < start) {
531
+ current = iterator.next();
532
+ c++;
533
+ }
534
+ for (let i = start; i < end; i++) {
535
+ newList.push(current.value);
536
+ current = iterator.next();
537
+ }
538
+
539
+ return newList;
540
+ }
541
+
542
+ /**
543
+ * Time Complexity: O(n + m)
544
+ * Space Complexity: O(m)
545
+ *
546
+ * The function overrides the splice method to handle deletion and insertion of elements in a data
547
+ * structure while returning the removed elements.
548
+ * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
549
+ * to start modifying the array.
550
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
551
+ * number of elements to remove from the array starting at the specified `start` index. If
552
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
553
+ * elements can still be inserted at
554
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
555
+ * will be inserted into the array at the specified `start` index. These elements can be of any type
556
+ * and there can be multiple elements passed as arguments to be inserted into the array.
557
+ * @returns The `splice` method is returning a new instance of the data structure that was modified
558
+ * by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
559
+ * elements provided in the `items` array.
560
+ */
561
+ override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
562
+ const removedList = this._createInstance(); // Used to store deleted elements
563
+
564
+ // Handling negative indexes
565
+ start = start < 0 ? this.length + start : start;
566
+ start = Math.max(0, Math.min(start, this.length)); // Correct start range
567
+ deleteCount = Math.max(0, deleteCount); // Make sure deleteCount is non-negative
568
+
569
+ let currentIndex = 0;
570
+ let currentNode: NODE | undefined = undefined;
571
+ let previousNode: NODE | undefined = undefined;
572
+
573
+ // Find the starting point using an iterator
574
+ const iterator = this._getNodeIterator();
575
+ for (const node of iterator) {
576
+ if (currentIndex === start) {
577
+ currentNode = node; // Find the starting node
578
+ break;
579
+ }
580
+ previousNode = node; // Update the previous node
581
+ currentIndex++;
582
+ }
583
+
584
+ // Delete nodes
585
+ for (let i = 0; i < deleteCount && currentNode; i++) {
586
+ removedList.push(currentNode.value); // Store the deleted value in removedList
587
+ const nextNode = currentNode.next; // Save next node
588
+ this.delete(currentNode); // Delete current node
589
+ currentNode = nextNode as NODE;
590
+ }
591
+
592
+ // Insert new value
593
+ for (let i = 0; i < items.length; i++) {
594
+ if (previousNode) {
595
+ this.addAfter(previousNode, items[i]); // Insert after previousNode
596
+ previousNode = previousNode.next as NODE; // Move to newly inserted node
597
+ } else {
598
+ this.addAt(0, items[i]); // Insert at the head of the linked list
599
+ previousNode = this._getNodeIterator().next().value; // Update the head node to be the first inserted node
600
+ }
601
+ }
602
+
603
+ return removedList;
604
+ }
605
+
606
+ override reduceRight(callbackfn: ReduceLinearCallback<E>): E;
607
+
608
+ override reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
609
+
610
+ override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
611
+
612
+ /**
613
+ * Time Complexity: O(n)
614
+ * Space Complexity: O(1)
615
+ *
616
+ * The function `reduceRight` iterates over an array in reverse order and applies a callback function
617
+ * to each element, accumulating a single result.
618
+ * @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
619
+ * of the array from right to left. It takes four arguments:
620
+ * @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
621
+ * initial accumulator value in the reduce operation. If provided, the reduce operation starts with
622
+ * this initial value and iterates over the elements of the array, applying the callback function to
623
+ * each element and the current accumulator value. If `initial
624
+ * @returns The `reduceRight` method is returning the final accumulated value after applying the
625
+ * callback function to each element in the array from right to left.
626
+ */
627
+ override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
628
+ let accumulator = initialValue ?? (0 as U);
629
+ let index = this.length - 1;
630
+ for (const item of this._getReverseIterator()) {
631
+ accumulator = callbackfn(accumulator, item, index--, this);
632
+ }
633
+ return accumulator;
634
+ }
635
+
636
+ abstract override delete(elementOrNode: E | NODE | undefined): boolean;
637
+
638
+ abstract addBefore(existingElementOrNode: E | NODE, newElementOrNode: E | NODE): boolean;
639
+
640
+ abstract addAfter(existingElementOrNode: E | NODE, newElementOrNode: E | NODE): boolean;
641
+
642
+ abstract getNodeAt(index: number): NODE | undefined;
643
+
644
+ protected abstract _getNodeIterator(...args: any[]): IterableIterator<NODE>;
645
+
646
+ // protected abstract _getReverseNodeIterator(...args: any[]): IterableIterator<NODE>;
647
+
648
+ protected abstract _getPrevNode(node: NODE): NODE | undefined;
649
+ }