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