queue-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
@@ -4,8 +4,8 @@
4
4
  * @class
5
5
  */
6
6
  import type { ElementCallback, QueueOptions } from '../../types';
7
- import { IterableElementBase } from '../base';
8
7
  import { SinglyLinkedList } from '../linked-list';
8
+ import { LinearBase } from '../base/linear-base';
9
9
  /**
10
10
  * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
11
11
  * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
@@ -15,32 +15,23 @@ import { SinglyLinkedList } from '../linked-list';
15
15
  * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
16
16
  * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
17
17
  */
18
- export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
18
+ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
19
19
  constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
20
20
  protected _elements: E[];
21
- /**
22
- * The elements function returns the elements of this set.
23
- * @return An array of the elements in the stack
24
- */
25
21
  get elements(): E[];
26
22
  protected _offset: number;
27
- /**
28
- * The offset function returns the offset of the current page.
29
- * @return The value of the protected variable _offset
30
- */
31
23
  get offset(): number;
32
- /**
33
- * The size function returns the number of elements in an array.
34
- * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
35
- */
36
- get size(): number;
24
+ get length(): number;
25
+ protected _autoCompactRatio: number;
26
+ get autoCompactRatio(): number;
27
+ set autoCompactRatio(v: number);
37
28
  /**
38
29
  * Time Complexity: O(1)
39
30
  * Space Complexity: O(1)
40
31
  *
41
32
  * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
42
33
  * @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
43
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
34
+ * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
44
35
  */
45
36
  get first(): E | undefined;
46
37
  /**
@@ -52,18 +43,6 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
52
43
  * array is empty, it returns `undefined`.
53
44
  */
54
45
  get last(): E | undefined;
55
- protected _autoCompactRatio: number;
56
- /**
57
- * This function returns the value of the autoCompactRatio property.
58
- * @returns The `autoCompactRatio` property of the object, which is a number.
59
- */
60
- get autoCompactRatio(): number;
61
- /**
62
- * The above function sets the autoCompactRatio property to a specified number in TypeScript.
63
- * @param {number} v - The parameter `v` represents the value that will be assigned to the
64
- * `_autoCompactRatio` property.
65
- */
66
- set autoCompactRatio(v: number);
67
46
  /**
68
47
  * Time Complexity: O(n)
69
48
  * Space Complexity: O(n)
@@ -122,7 +101,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
122
101
  * @param {number} index - Determine the index of the element to be deleted
123
102
  * @return A boolean value
124
103
  */
125
- deleteAt(index: number): boolean;
104
+ deleteAt(index: number): E | undefined;
126
105
  /**
127
106
  * Time Complexity: O(1)
128
107
  * Space Complexity: O(1)
@@ -135,22 +114,53 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
135
114
  * `_offset`.
136
115
  */
137
116
  at(index: number): E | undefined;
117
+ /**
118
+ * Time Complexity: O(n)
119
+ * Space Complexity: O(1)
120
+ *
121
+ * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
122
+ * offset.
123
+ * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
124
+ * the elements in the array and resetting the offset to 0.
125
+ */
126
+ reverse(): this;
127
+ /**
128
+ * Time Complexity: O(n)
129
+ * Space Complexity: O(1)
130
+ *
131
+ * The function `addAt` inserts a new element at a specified index in an array, returning true if
132
+ * successful and false if the index is out of bounds.
133
+ * @param {number} index - The `index` parameter represents the position at which the `newElement`
134
+ * should be added in the array.
135
+ * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
136
+ * into the array at the specified index.
137
+ * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
138
+ * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
139
+ * than the length of the array).
140
+ */
141
+ addAt(index: number, newElement: E): boolean;
138
142
  /**
139
143
  * Time Complexity: O(1)
140
144
  * Space Complexity: O(1)
141
145
  *
142
- * The function checks if a data structure is empty by comparing its size to zero.
143
- * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
146
+ * The function `setAt` updates an element at a specified index in an array-like data structure.
147
+ * @param {number} index - The `index` parameter is a number that represents the position in the
148
+ * array where the new element will be set.
149
+ * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
150
+ * at the specified index in the array.
151
+ * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
152
+ * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
153
+ * length of the array).
144
154
  */
145
- isEmpty(): boolean;
155
+ setAt(index: number, newElement: E): boolean;
146
156
  /**
147
157
  * Time Complexity: O(1)
148
- * Space Complexity: O(n)
158
+ * Space Complexity: O(1)
149
159
  *
150
- * The toArray() function returns an array of elements from the current offset to the end of the _elements array.
151
- * @returns An array of type E is being returned.
160
+ * The function checks if a data structure is empty by comparing its length to zero.
161
+ * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
152
162
  */
153
- toArray(): E[];
163
+ isEmpty(): boolean;
154
164
  /**
155
165
  * Time Complexity: O(1)
156
166
  * Space Complexity: O(1)
@@ -167,6 +177,25 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
167
177
  * @returns The `compact()` method is returning a boolean value of `true`.
168
178
  */
169
179
  compact(): boolean;
180
+ /**
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(n)
183
+ *
184
+ * The function overrides the splice method to remove and insert elements in a queue-like data
185
+ * structure.
186
+ * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
187
+ * to start changing the array. Items will be added or removed starting from this index.
188
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
189
+ * number of elements to remove from the array starting at the specified `start` index. If
190
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
191
+ * elements can still be inserted at
192
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
193
+ * will be added to the array at the specified `start` index. These elements will replace the
194
+ * existing elements starting from the `start` index for the `deleteCount` number of elements.
195
+ * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
196
+ * class as the original object.
197
+ */
198
+ splice(start: number, deleteCount?: number, ...items: E[]): this;
170
199
  /**
171
200
  * Time Complexity: O(n)
172
201
  * Space Complexity: O(n)
@@ -174,7 +203,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
174
203
  * The `clone()` function returns a new Queue object with the same elements as the original Queue.
175
204
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
176
205
  */
177
- clone(): Queue<E, R>;
206
+ clone(): this;
178
207
  /**
179
208
  * Time Complexity: O(n)
180
209
  * Space Complexity: O(n)
@@ -191,7 +220,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
191
220
  * @returns The `filter` method is returning a new `Queue` object that contains the elements that
192
221
  * satisfy the given predicate function.
193
222
  */
194
- filter(predicate: ElementCallback<E, R, boolean, Queue<E, R>>, thisArg?: any): Queue<E, R>;
223
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Queue<E, R>;
195
224
  /**
196
225
  * Time Complexity: O(n)
197
226
  * Space Complexity: O(n)
@@ -210,7 +239,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
210
239
  * @returns A new Queue object containing elements of type EM, which are the result of applying the
211
240
  * callback function to each element in the original Queue object.
212
241
  */
213
- map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
242
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
214
243
  /**
215
244
  * Time Complexity: O(n)
216
245
  * Space Complexity: O(n)
@@ -218,6 +247,21 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
218
247
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
219
248
  */
220
249
  protected _getIterator(): IterableIterator<E>;
250
+ /**
251
+ * The function `_createInstance` returns a new instance of the `Queue` class with the specified
252
+ * options.
253
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
254
+ * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
255
+ * allows you to specify settings or properties that can influence how the queue operates.
256
+ * @returns An instance of the `Queue` class with an empty array and the provided options is being
257
+ * returned.
258
+ */
259
+ protected _createInstance(options?: QueueOptions<E, R>): this;
260
+ /**
261
+ * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
262
+ * order.
263
+ */
264
+ protected _getReverseIterator(): IterableIterator<E>;
221
265
  }
222
266
  /**
223
267
  * 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
@@ -234,5 +278,5 @@ export declare class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<
234
278
  * @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
235
279
  * values as the original `LinkedListQueue`.
236
280
  */
237
- clone(): LinkedListQueue<E, R>;
281
+ clone(): this;
238
282
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LinkedListQueue = exports.Queue = void 0;
4
- const base_1 = require("../base");
5
4
  const linked_list_1 = require("../linked-list");
5
+ const linear_base_1 = require("../base/linear-base");
6
6
  /**
7
7
  * 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
8
8
  * 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
@@ -12,7 +12,7 @@ const linked_list_1 = require("../linked-list");
12
12
  * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
13
13
  * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
14
14
  */
15
- class Queue extends base_1.IterableElementBase {
15
+ class Queue extends linear_base_1.LinearBase {
16
16
  constructor(elements = [], options) {
17
17
  super(options);
18
18
  this._elements = [];
@@ -24,37 +24,31 @@ class Queue extends base_1.IterableElementBase {
24
24
  }
25
25
  this.pushMany(elements);
26
26
  }
27
- /**
28
- * The elements function returns the elements of this set.
29
- * @return An array of the elements in the stack
30
- */
31
27
  get elements() {
32
28
  return this._elements;
33
29
  }
34
- /**
35
- * The offset function returns the offset of the current page.
36
- * @return The value of the protected variable _offset
37
- */
38
30
  get offset() {
39
31
  return this._offset;
40
32
  }
41
- /**
42
- * The size function returns the number of elements in an array.
43
- * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
44
- */
45
- get size() {
33
+ get length() {
46
34
  return this.elements.length - this.offset;
47
35
  }
36
+ get autoCompactRatio() {
37
+ return this._autoCompactRatio;
38
+ }
39
+ set autoCompactRatio(v) {
40
+ this._autoCompactRatio = v;
41
+ }
48
42
  /**
49
43
  * Time Complexity: O(1)
50
44
  * Space Complexity: O(1)
51
45
  *
52
46
  * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
53
47
  * @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
54
- * the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
48
+ * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
55
49
  */
56
50
  get first() {
57
- return this.size > 0 ? this.elements[this.offset] : undefined;
51
+ return this.length > 0 ? this.elements[this.offset] : undefined;
58
52
  }
59
53
  /**
60
54
  * Time Complexity: O(1)
@@ -65,22 +59,7 @@ class Queue extends base_1.IterableElementBase {
65
59
  * array is empty, it returns `undefined`.
66
60
  */
67
61
  get last() {
68
- return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
69
- }
70
- /**
71
- * This function returns the value of the autoCompactRatio property.
72
- * @returns The `autoCompactRatio` property of the object, which is a number.
73
- */
74
- get autoCompactRatio() {
75
- return this._autoCompactRatio;
76
- }
77
- /**
78
- * The above function sets the autoCompactRatio property to a specified number in TypeScript.
79
- * @param {number} v - The parameter `v` represents the value that will be assigned to the
80
- * `_autoCompactRatio` property.
81
- */
82
- set autoCompactRatio(v) {
83
- this._autoCompactRatio = v;
62
+ return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
84
63
  }
85
64
  /**
86
65
  * Time Complexity: O(n)
@@ -105,6 +84,8 @@ class Queue extends base_1.IterableElementBase {
105
84
  */
106
85
  push(element) {
107
86
  this.elements.push(element);
87
+ if (this._maxLen > 0 && this.length > this._maxLen)
88
+ this.shift();
108
89
  return true;
109
90
  }
110
91
  /**
@@ -137,7 +118,7 @@ class Queue extends base_1.IterableElementBase {
137
118
  * @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
138
119
  */
139
120
  shift() {
140
- if (this.size === 0)
121
+ if (this.length === 0)
141
122
  return undefined;
142
123
  const first = this.first;
143
124
  this._offset += 1;
@@ -155,7 +136,7 @@ class Queue extends base_1.IterableElementBase {
155
136
  */
156
137
  delete(element) {
157
138
  const index = this.elements.indexOf(element);
158
- return this.deleteAt(index);
139
+ return !!this.deleteAt(index);
159
140
  }
160
141
  /**
161
142
  * Time Complexity: O(n)
@@ -166,8 +147,9 @@ class Queue extends base_1.IterableElementBase {
166
147
  * @return A boolean value
167
148
  */
168
149
  deleteAt(index) {
169
- const spliced = this.elements.splice(index, 1);
170
- return spliced.length === 1;
150
+ const deleted = this.elements[index];
151
+ this.elements.splice(index, 1);
152
+ return deleted;
171
153
  }
172
154
  /**
173
155
  * Time Complexity: O(1)
@@ -183,25 +165,68 @@ class Queue extends base_1.IterableElementBase {
183
165
  at(index) {
184
166
  return this.elements[index + this._offset];
185
167
  }
168
+ /**
169
+ * Time Complexity: O(n)
170
+ * Space Complexity: O(1)
171
+ *
172
+ * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
173
+ * offset.
174
+ * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
175
+ * the elements in the array and resetting the offset to 0.
176
+ */
177
+ reverse() {
178
+ this._elements = this.elements.slice(this.offset).reverse();
179
+ this._offset = 0;
180
+ return this;
181
+ }
182
+ /**
183
+ * Time Complexity: O(n)
184
+ * Space Complexity: O(1)
185
+ *
186
+ * The function `addAt` inserts a new element at a specified index in an array, returning true if
187
+ * successful and false if the index is out of bounds.
188
+ * @param {number} index - The `index` parameter represents the position at which the `newElement`
189
+ * should be added in the array.
190
+ * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
191
+ * into the array at the specified index.
192
+ * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
193
+ * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
194
+ * than the length of the array).
195
+ */
196
+ addAt(index, newElement) {
197
+ if (index < 0 || index > this.length)
198
+ return false;
199
+ this._elements.splice(this.offset + index, 0, newElement);
200
+ return true;
201
+ }
186
202
  /**
187
203
  * Time Complexity: O(1)
188
204
  * Space Complexity: O(1)
189
205
  *
190
- * The function checks if a data structure is empty by comparing its size to zero.
191
- * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
206
+ * The function `setAt` updates an element at a specified index in an array-like data structure.
207
+ * @param {number} index - The `index` parameter is a number that represents the position in the
208
+ * array where the new element will be set.
209
+ * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
210
+ * at the specified index in the array.
211
+ * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
212
+ * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
213
+ * length of the array).
192
214
  */
193
- isEmpty() {
194
- return this.size === 0;
215
+ setAt(index, newElement) {
216
+ if (index < 0 || index > this.length)
217
+ return false;
218
+ this._elements[this.offset + index] = newElement;
219
+ return true;
195
220
  }
196
221
  /**
197
222
  * Time Complexity: O(1)
198
- * Space Complexity: O(n)
223
+ * Space Complexity: O(1)
199
224
  *
200
- * The toArray() function returns an array of elements from the current offset to the end of the _elements array.
201
- * @returns An array of type E is being returned.
225
+ * The function checks if a data structure is empty by comparing its length to zero.
226
+ * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
202
227
  */
203
- toArray() {
204
- return this.elements.slice(this.offset);
228
+ isEmpty() {
229
+ return this.length === 0;
205
230
  }
206
231
  /**
207
232
  * Time Complexity: O(1)
@@ -226,6 +251,34 @@ class Queue extends base_1.IterableElementBase {
226
251
  this._offset = 0;
227
252
  return true;
228
253
  }
254
+ /**
255
+ * Time Complexity: O(n)
256
+ * Space Complexity: O(n)
257
+ *
258
+ * The function overrides the splice method to remove and insert elements in a queue-like data
259
+ * structure.
260
+ * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
261
+ * to start changing the array. Items will be added or removed starting from this index.
262
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
263
+ * number of elements to remove from the array starting at the specified `start` index. If
264
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
265
+ * elements can still be inserted at
266
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
267
+ * will be added to the array at the specified `start` index. These elements will replace the
268
+ * existing elements starting from the `start` index for the `deleteCount` number of elements.
269
+ * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
270
+ * class as the original object.
271
+ */
272
+ splice(start, deleteCount = 0, ...items) {
273
+ const removedQueue = this._createInstance();
274
+ start = Math.max(0, Math.min(start, this.length));
275
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
276
+ const globalStartIndex = this.offset + start;
277
+ const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
278
+ removedQueue.pushMany(removedElements);
279
+ this.compact();
280
+ return removedQueue;
281
+ }
229
282
  /**
230
283
  * Time Complexity: O(n)
231
284
  * Space Complexity: O(n)
@@ -234,7 +287,7 @@ class Queue extends base_1.IterableElementBase {
234
287
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
235
288
  */
236
289
  clone() {
237
- return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
290
+ return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen });
238
291
  }
239
292
  /**
240
293
  * Time Complexity: O(n)
@@ -253,7 +306,11 @@ class Queue extends base_1.IterableElementBase {
253
306
  * satisfy the given predicate function.
254
307
  */
255
308
  filter(predicate, thisArg) {
256
- const newDeque = new Queue([], { toElementFn: this.toElementFn });
309
+ const newDeque = this._createInstance({
310
+ toElementFn: this._toElementFn,
311
+ autoCompactRatio: this._autoCompactRatio,
312
+ maxLen: this._maxLen
313
+ });
257
314
  let index = 0;
258
315
  for (const el of this) {
259
316
  if (predicate.call(thisArg, el, index, this)) {
@@ -282,7 +339,11 @@ class Queue extends base_1.IterableElementBase {
282
339
  * callback function to each element in the original Queue object.
283
340
  */
284
341
  map(callback, toElementFn, thisArg) {
285
- const newDeque = new Queue([], { toElementFn });
342
+ const newDeque = new Queue([], {
343
+ toElementFn,
344
+ autoCompactRatio: this._autoCompactRatio,
345
+ maxLen: this._maxLen
346
+ });
286
347
  let index = 0;
287
348
  for (const el of this) {
288
349
  newDeque.push(callback.call(thisArg, el, index, this));
@@ -301,6 +362,29 @@ class Queue extends base_1.IterableElementBase {
301
362
  yield item;
302
363
  }
303
364
  }
365
+ /**
366
+ * The function `_createInstance` returns a new instance of the `Queue` class with the specified
367
+ * options.
368
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
369
+ * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
370
+ * allows you to specify settings or properties that can influence how the queue operates.
371
+ * @returns An instance of the `Queue` class with an empty array and the provided options is being
372
+ * returned.
373
+ */
374
+ _createInstance(options) {
375
+ return new Queue([], options);
376
+ }
377
+ /**
378
+ * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
379
+ * order.
380
+ */
381
+ *_getReverseIterator() {
382
+ for (let i = this.length - 1; i >= 0; i--) {
383
+ const cur = this.at(i); // `at()` handles the offset.
384
+ if (cur !== undefined)
385
+ yield cur;
386
+ }
387
+ }
304
388
  }
305
389
  exports.Queue = Queue;
306
390
  /**
@@ -319,7 +403,7 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
319
403
  * values as the original `LinkedListQueue`.
320
404
  */
321
405
  clone() {
322
- return new LinkedListQueue(this, { toElementFn: this.toElementFn });
406
+ return new LinkedListQueue(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
323
407
  }
324
408
  }
325
409
  exports.LinkedListQueue = LinkedListQueue;
@@ -15,7 +15,7 @@ import { IterableElementBase } from '../base';
15
15
  * 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
16
16
  * 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
17
17
  */
18
- export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E, R>> {
18
+ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R> {
19
19
  constructor(elements?: Iterable<E> | Iterable<R>, options?: StackOptions<E, R>);
20
20
  protected _elements: E[];
21
21
  /**
@@ -102,14 +102,6 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
102
102
  * @returns An array of type E.
103
103
  */
104
104
  deleteAt(index: number): boolean;
105
- /**
106
- * Time Complexity: O(n)
107
- * Space Complexity: O(n)
108
- *
109
- * The toArray function returns a copy of the elements in an array.
110
- * @returns An array of type E.
111
- */
112
- toArray(): E[];
113
105
  /**
114
106
  * Time Complexity: O(1)
115
107
  * Space Complexity: O(1)
@@ -141,7 +133,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
141
133
  * @returns The `filter` method is returning a new `Stack` object that contains the elements that
142
134
  * satisfy the given predicate function.
143
135
  */
144
- filter(predicate: ElementCallback<E, R, boolean, Stack<E, R>>, thisArg?: any): Stack<E, R>;
136
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Stack<E, R>;
145
137
  /**
146
138
  * Time Complexity: O(n)
147
139
  * Space Complexity: O(n)
@@ -159,7 +151,7 @@ export declare class Stack<E = any, R = any> extends IterableElementBase<E, R, S
159
151
  * value of
160
152
  * @returns a new Stack object with elements of type EM and raw elements of type RM.
161
153
  */
162
- map<EM, RM>(callback: ElementCallback<E, R, EM, Stack<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
154
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Stack<EM, RM>;
163
155
  /**
164
156
  * Time Complexity: O(n)
165
157
  * Space Complexity: O(n)
@@ -136,16 +136,6 @@ class Stack extends base_1.IterableElementBase {
136
136
  const spliced = this.elements.splice(index, 1);
137
137
  return spliced.length === 1;
138
138
  }
139
- /**
140
- * Time Complexity: O(n)
141
- * Space Complexity: O(n)
142
- *
143
- * The toArray function returns a copy of the elements in an array.
144
- * @returns An array of type E.
145
- */
146
- toArray() {
147
- return this.elements.slice();
148
- }
149
139
  /**
150
140
  * Time Complexity: O(1)
151
141
  * Space Complexity: O(1)
@@ -143,7 +143,7 @@ export declare class TrieNode {
143
143
  * const subnet = ip.split('.').slice(0, 3).join('.');
144
144
  * console.log(ipRoutingTable.hasPrefix(subnet)); // true
145
145
  */
146
- export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
146
+ export declare class Trie<R = any> extends IterableElementBase<string, R> {
147
147
  /**
148
148
  * The constructor initializes a Trie data structure with optional options and words provided as
149
149
  * input.
@@ -311,7 +311,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
311
311
  * specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
312
312
  * @returns The `filter` method is returning an array of strings (`string[]`).
313
313
  */
314
- filter(predicate: ElementCallback<string, R, boolean, Trie<R>>, thisArg?: any): Trie<R>;
314
+ filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): Trie<R>;
315
315
  /**
316
316
  * Time Complexity: O(n)
317
317
  * Space Complexity: O(n)
@@ -330,7 +330,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
330
330
  * value of
331
331
  * @returns a new Trie object.
332
332
  */
333
- map<RM>(callback: ElementCallback<string, R, string, Trie<R>>, toElementFn?: (rawElement: RM) => string, thisArg?: any): Trie<RM>;
333
+ map<RM>(callback: ElementCallback<string, R, string>, toElementFn?: (rawElement: RM) => string, thisArg?: any): Trie<RM>;
334
334
  /**
335
335
  * Time Complexity: O(n)
336
336
  * Space Complexity: O(n)
@@ -339,6 +339,7 @@ export declare class Trie<R = any> extends IterableElementBase<string, R, Trie<R
339
339
  * trie data structure and yields all the paths to the end nodes.
340
340
  */
341
341
  protected _getIterator(): IterableIterator<string>;
342
+ protected get _total(): number;
342
343
  /**
343
344
  * Time Complexity: O(l), where l is the length of the input string.
344
345
  * Space Complexity: O(1) - Constant space.
@@ -573,6 +573,9 @@ class Trie extends base_1.IterableElementBase {
573
573
  }
574
574
  yield* _dfs(this.root, '');
575
575
  }
576
+ get _total() {
577
+ return this._size;
578
+ }
576
579
  /**
577
580
  * Time Complexity: O(l), where l is the length of the input string.
578
581
  * Space Complexity: O(1) - Constant space.
@@ -1,8 +1,13 @@
1
1
  import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
2
- export type EntryCallback<K, V, R> = (key: K, value: V, index: number, container: IterableEntryBase<K, V>) => R;
3
- export type ElementCallback<E, R, RT, C> = (element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
4
- export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, container: IterableEntryBase<K, V>) => R;
5
- export type ReduceElementCallback<E, R, RT, C> = (accumulator: RT, element: E, index: number, container: IterableElementBase<E, R, C>) => RT;
2
+ import { LinearBase } from '../../../data-structures/base/linear-base';
3
+ export type EntryCallback<K, V, R> = (key: K, value: V, index: number, original: IterableEntryBase<K, V>) => R;
4
+ export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
5
+ export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
6
+ export type ReduceElementCallback<E, R, RT = E> = (accumulator: RT, element: E, index: number, original: IterableElementBase<E, R>) => RT;
7
+ export type ReduceLinearCallback<E, RT = E> = (accumulator: RT, element: E, index: number, original: LinearBase<E>) => RT;
6
8
  export type IterableElementBaseOptions<E, R> = {
7
9
  toElementFn?: (rawElement: R) => E;
8
10
  };
11
+ export type LinearBaseOptions<E, R> = IterableElementBaseOptions<E, R> & {
12
+ maxLen?: number;
13
+ };
@@ -1,2 +1,2 @@
1
1
  import type { AVLTreeOptions } from './avl-tree';
2
- export type AVLTreeMultiMapOptions<K, V, R> = Omit<AVLTreeOptions<K, V, R>, 'isMapMode'> & {};
2
+ export type AVLTreeMultiMapOptions<K, V, R> = AVLTreeOptions<K, V, R> & {};