directed-graph-typed 1.54.3 → 2.0.1

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 (70) 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-multi-map.d.ts +12 -8
  6. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
  8. package/dist/data-structures/binary-tree/avl-tree.js +64 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +5 -5
  10. package/dist/data-structures/binary-tree/bst.js +11 -11
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
  13. package/dist/data-structures/graph/abstract-graph.js +16 -16
  14. package/dist/data-structures/hash/hash-map.d.ts +46 -0
  15. package/dist/data-structures/hash/hash-map.js +46 -0
  16. package/dist/data-structures/heap/heap.d.ts +3 -11
  17. package/dist/data-structures/heap/heap.js +0 -10
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  19. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  20. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  21. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  22. package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  23. package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
  24. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  25. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  26. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  27. package/dist/data-structures/queue/deque.d.ts +130 -91
  28. package/dist/data-structures/queue/deque.js +269 -169
  29. package/dist/data-structures/queue/queue.d.ts +131 -40
  30. package/dist/data-structures/queue/queue.js +181 -50
  31. package/dist/data-structures/stack/stack.d.ts +124 -11
  32. package/dist/data-structures/stack/stack.js +121 -10
  33. package/dist/data-structures/trie/trie.d.ts +4 -3
  34. package/dist/data-structures/trie/trie.js +3 -0
  35. package/dist/types/data-structures/base/base.d.ts +9 -4
  36. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  37. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  38. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  39. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  40. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  41. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  42. package/package.json +2 -2
  43. package/src/data-structures/base/iterable-element-base.ts +29 -20
  44. package/src/data-structures/base/linear-base.ts +649 -0
  45. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
  46. package/src/data-structures/binary-tree/avl-tree.ts +64 -0
  47. package/src/data-structures/binary-tree/binary-tree.ts +5 -5
  48. package/src/data-structures/binary-tree/bst.ts +9 -9
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
  50. package/src/data-structures/graph/abstract-graph.ts +16 -16
  51. package/src/data-structures/hash/hash-map.ts +46 -0
  52. package/src/data-structures/heap/heap.ts +3 -14
  53. package/src/data-structures/heap/max-heap.ts +2 -2
  54. package/src/data-structures/heap/min-heap.ts +2 -2
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  56. package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
  57. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  58. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  59. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  60. package/src/data-structures/queue/deque.ts +286 -183
  61. package/src/data-structures/queue/queue.ts +196 -63
  62. package/src/data-structures/stack/stack.ts +124 -18
  63. package/src/data-structures/trie/trie.ts +7 -3
  64. package/src/types/data-structures/base/base.ts +17 -8
  65. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  66. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  67. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  68. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  69. package/src/types/data-structures/queue/deque.ts +2 -3
  70. package/src/types/data-structures/queue/queue.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).
@@ -14,33 +14,71 @@ import { SinglyLinkedList } from '../linked-list';
14
14
  * 5. Data Buffering: Acting as a buffer for data packets in network communication.
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
+ * @example
18
+ * // Sliding Window using Queue
19
+ * const nums = [2, 3, 4, 1, 5];
20
+ * const k = 2;
21
+ * const queue = new Queue<number>();
22
+ *
23
+ * let maxSum = 0;
24
+ * let currentSum = 0;
25
+ *
26
+ * nums.forEach((num, i) => {
27
+ * queue.push(num);
28
+ * currentSum += num;
29
+ *
30
+ * if (queue.length > k) {
31
+ * currentSum -= queue.shift()!;
32
+ * }
33
+ *
34
+ * if (queue.length === k) {
35
+ * maxSum = Math.max(maxSum, currentSum);
36
+ * }
37
+ * });
38
+ *
39
+ * console.log(maxSum); // 7
40
+ * @example
41
+ * // Breadth-First Search (BFS) using Queue
42
+ * const graph: { [key in number]: number[] } = {
43
+ * 1: [2, 3],
44
+ * 2: [4, 5],
45
+ * 3: [],
46
+ * 4: [],
47
+ * 5: []
48
+ * };
49
+ *
50
+ * const queue = new Queue<number>();
51
+ * const visited: number[] = [];
52
+ *
53
+ * queue.push(1);
54
+ *
55
+ * while (!queue.isEmpty()) {
56
+ * const node = queue.shift()!;
57
+ * if (!visited.includes(node)) {
58
+ * visited.push(node);
59
+ * graph[node].forEach(neighbor => queue.push(neighbor));
60
+ * }
61
+ * }
62
+ *
63
+ * console.log(visited); // [1, 2, 3, 4, 5]
17
64
  */
18
- export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
65
+ export declare class Queue<E = any, R = any> extends LinearBase<E, R> {
19
66
  constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
20
67
  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
68
  get elements(): E[];
26
69
  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
70
  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;
71
+ get length(): number;
72
+ protected _autoCompactRatio: number;
73
+ get autoCompactRatio(): number;
74
+ set autoCompactRatio(v: number);
37
75
  /**
38
76
  * Time Complexity: O(1)
39
77
  * Space Complexity: O(1)
40
78
  *
41
79
  * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
42
80
  * @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`.
81
+ * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
44
82
  */
45
83
  get first(): E | undefined;
46
84
  /**
@@ -52,18 +90,6 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
52
90
  * array is empty, it returns `undefined`.
53
91
  */
54
92
  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
93
  /**
68
94
  * Time Complexity: O(n)
69
95
  * Space Complexity: O(n)
@@ -122,7 +148,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
122
148
  * @param {number} index - Determine the index of the element to be deleted
123
149
  * @return A boolean value
124
150
  */
125
- deleteAt(index: number): boolean;
151
+ deleteAt(index: number): E | undefined;
126
152
  /**
127
153
  * Time Complexity: O(1)
128
154
  * Space Complexity: O(1)
@@ -135,22 +161,53 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
135
161
  * `_offset`.
136
162
  */
137
163
  at(index: number): E | undefined;
164
+ /**
165
+ * Time Complexity: O(n)
166
+ * Space Complexity: O(1)
167
+ *
168
+ * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
169
+ * offset.
170
+ * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
171
+ * the elements in the array and resetting the offset to 0.
172
+ */
173
+ reverse(): this;
174
+ /**
175
+ * Time Complexity: O(n)
176
+ * Space Complexity: O(1)
177
+ *
178
+ * The function `addAt` inserts a new element at a specified index in an array, returning true if
179
+ * successful and false if the index is out of bounds.
180
+ * @param {number} index - The `index` parameter represents the position at which the `newElement`
181
+ * should be added in the array.
182
+ * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
183
+ * into the array at the specified index.
184
+ * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
185
+ * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
186
+ * than the length of the array).
187
+ */
188
+ addAt(index: number, newElement: E): boolean;
138
189
  /**
139
190
  * Time Complexity: O(1)
140
191
  * Space Complexity: O(1)
141
192
  *
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.
193
+ * The function `setAt` updates an element at a specified index in an array-like data structure.
194
+ * @param {number} index - The `index` parameter is a number that represents the position in the
195
+ * array where the new element will be set.
196
+ * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
197
+ * at the specified index in the array.
198
+ * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
199
+ * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
200
+ * length of the array).
144
201
  */
145
- isEmpty(): boolean;
202
+ setAt(index: number, newElement: E): boolean;
146
203
  /**
147
204
  * Time Complexity: O(1)
148
- * Space Complexity: O(n)
205
+ * Space Complexity: O(1)
149
206
  *
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.
207
+ * The function checks if a data structure is empty by comparing its length to zero.
208
+ * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
152
209
  */
153
- toArray(): E[];
210
+ isEmpty(): boolean;
154
211
  /**
155
212
  * Time Complexity: O(1)
156
213
  * Space Complexity: O(1)
@@ -167,6 +224,25 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
167
224
  * @returns The `compact()` method is returning a boolean value of `true`.
168
225
  */
169
226
  compact(): boolean;
227
+ /**
228
+ * Time Complexity: O(n)
229
+ * Space Complexity: O(n)
230
+ *
231
+ * The function overrides the splice method to remove and insert elements in a queue-like data
232
+ * structure.
233
+ * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
234
+ * to start changing the array. Items will be added or removed starting from this index.
235
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
236
+ * number of elements to remove from the array starting at the specified `start` index. If
237
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
238
+ * elements can still be inserted at
239
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
240
+ * will be added to the array at the specified `start` index. These elements will replace the
241
+ * existing elements starting from the `start` index for the `deleteCount` number of elements.
242
+ * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
243
+ * class as the original object.
244
+ */
245
+ splice(start: number, deleteCount?: number, ...items: E[]): this;
170
246
  /**
171
247
  * Time Complexity: O(n)
172
248
  * Space Complexity: O(n)
@@ -174,7 +250,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
174
250
  * The `clone()` function returns a new Queue object with the same elements as the original Queue.
175
251
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
176
252
  */
177
- clone(): Queue<E, R>;
253
+ clone(): this;
178
254
  /**
179
255
  * Time Complexity: O(n)
180
256
  * Space Complexity: O(n)
@@ -191,7 +267,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
191
267
  * @returns The `filter` method is returning a new `Queue` object that contains the elements that
192
268
  * satisfy the given predicate function.
193
269
  */
194
- filter(predicate: ElementCallback<E, R, boolean, Queue<E, R>>, thisArg?: any): Queue<E, R>;
270
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Queue<E, R>;
195
271
  /**
196
272
  * Time Complexity: O(n)
197
273
  * Space Complexity: O(n)
@@ -210,7 +286,7 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
210
286
  * @returns A new Queue object containing elements of type EM, which are the result of applying the
211
287
  * callback function to each element in the original Queue object.
212
288
  */
213
- map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
289
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
214
290
  /**
215
291
  * Time Complexity: O(n)
216
292
  * Space Complexity: O(n)
@@ -218,6 +294,21 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
218
294
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
219
295
  */
220
296
  protected _getIterator(): IterableIterator<E>;
297
+ /**
298
+ * The function `_createInstance` returns a new instance of the `Queue` class with the specified
299
+ * options.
300
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
301
+ * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
302
+ * allows you to specify settings or properties that can influence how the queue operates.
303
+ * @returns An instance of the `Queue` class with an empty array and the provided options is being
304
+ * returned.
305
+ */
306
+ protected _createInstance(options?: QueueOptions<E, R>): this;
307
+ /**
308
+ * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
309
+ * order.
310
+ */
311
+ protected _getReverseIterator(): IterableIterator<E>;
221
312
  }
222
313
  /**
223
314
  * 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 +325,5 @@ export declare class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<
234
325
  * @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
235
326
  * values as the original `LinkedListQueue`.
236
327
  */
237
- clone(): LinkedListQueue<E, R>;
328
+ clone(): this;
238
329
  }
@@ -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).
@@ -11,8 +11,55 @@ const linked_list_1 = require("../linked-list");
11
11
  * 5. Data Buffering: Acting as a buffer for data packets in network communication.
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
+ * @example
15
+ * // Sliding Window using Queue
16
+ * const nums = [2, 3, 4, 1, 5];
17
+ * const k = 2;
18
+ * const queue = new Queue<number>();
19
+ *
20
+ * let maxSum = 0;
21
+ * let currentSum = 0;
22
+ *
23
+ * nums.forEach((num, i) => {
24
+ * queue.push(num);
25
+ * currentSum += num;
26
+ *
27
+ * if (queue.length > k) {
28
+ * currentSum -= queue.shift()!;
29
+ * }
30
+ *
31
+ * if (queue.length === k) {
32
+ * maxSum = Math.max(maxSum, currentSum);
33
+ * }
34
+ * });
35
+ *
36
+ * console.log(maxSum); // 7
37
+ * @example
38
+ * // Breadth-First Search (BFS) using Queue
39
+ * const graph: { [key in number]: number[] } = {
40
+ * 1: [2, 3],
41
+ * 2: [4, 5],
42
+ * 3: [],
43
+ * 4: [],
44
+ * 5: []
45
+ * };
46
+ *
47
+ * const queue = new Queue<number>();
48
+ * const visited: number[] = [];
49
+ *
50
+ * queue.push(1);
51
+ *
52
+ * while (!queue.isEmpty()) {
53
+ * const node = queue.shift()!;
54
+ * if (!visited.includes(node)) {
55
+ * visited.push(node);
56
+ * graph[node].forEach(neighbor => queue.push(neighbor));
57
+ * }
58
+ * }
59
+ *
60
+ * console.log(visited); // [1, 2, 3, 4, 5]
14
61
  */
15
- class Queue extends base_1.IterableElementBase {
62
+ class Queue extends linear_base_1.LinearBase {
16
63
  constructor(elements = [], options) {
17
64
  super(options);
18
65
  this._elements = [];
@@ -24,37 +71,31 @@ class Queue extends base_1.IterableElementBase {
24
71
  }
25
72
  this.pushMany(elements);
26
73
  }
27
- /**
28
- * The elements function returns the elements of this set.
29
- * @return An array of the elements in the stack
30
- */
31
74
  get elements() {
32
75
  return this._elements;
33
76
  }
34
- /**
35
- * The offset function returns the offset of the current page.
36
- * @return The value of the protected variable _offset
37
- */
38
77
  get offset() {
39
78
  return this._offset;
40
79
  }
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() {
80
+ get length() {
46
81
  return this.elements.length - this.offset;
47
82
  }
83
+ get autoCompactRatio() {
84
+ return this._autoCompactRatio;
85
+ }
86
+ set autoCompactRatio(v) {
87
+ this._autoCompactRatio = v;
88
+ }
48
89
  /**
49
90
  * Time Complexity: O(1)
50
91
  * Space Complexity: O(1)
51
92
  *
52
93
  * The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
53
94
  * @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`.
95
+ * the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
55
96
  */
56
97
  get first() {
57
- return this.size > 0 ? this.elements[this.offset] : undefined;
98
+ return this.length > 0 ? this.elements[this.offset] : undefined;
58
99
  }
59
100
  /**
60
101
  * Time Complexity: O(1)
@@ -65,22 +106,7 @@ class Queue extends base_1.IterableElementBase {
65
106
  * array is empty, it returns `undefined`.
66
107
  */
67
108
  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;
109
+ return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
84
110
  }
85
111
  /**
86
112
  * Time Complexity: O(n)
@@ -105,6 +131,8 @@ class Queue extends base_1.IterableElementBase {
105
131
  */
106
132
  push(element) {
107
133
  this.elements.push(element);
134
+ if (this._maxLen > 0 && this.length > this._maxLen)
135
+ this.shift();
108
136
  return true;
109
137
  }
110
138
  /**
@@ -137,7 +165,7 @@ class Queue extends base_1.IterableElementBase {
137
165
  * @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
138
166
  */
139
167
  shift() {
140
- if (this.size === 0)
168
+ if (this.length === 0)
141
169
  return undefined;
142
170
  const first = this.first;
143
171
  this._offset += 1;
@@ -155,7 +183,7 @@ class Queue extends base_1.IterableElementBase {
155
183
  */
156
184
  delete(element) {
157
185
  const index = this.elements.indexOf(element);
158
- return this.deleteAt(index);
186
+ return !!this.deleteAt(index);
159
187
  }
160
188
  /**
161
189
  * Time Complexity: O(n)
@@ -166,8 +194,9 @@ class Queue extends base_1.IterableElementBase {
166
194
  * @return A boolean value
167
195
  */
168
196
  deleteAt(index) {
169
- const spliced = this.elements.splice(index, 1);
170
- return spliced.length === 1;
197
+ const deleted = this.elements[index];
198
+ this.elements.splice(index, 1);
199
+ return deleted;
171
200
  }
172
201
  /**
173
202
  * Time Complexity: O(1)
@@ -183,25 +212,68 @@ class Queue extends base_1.IterableElementBase {
183
212
  at(index) {
184
213
  return this.elements[index + this._offset];
185
214
  }
215
+ /**
216
+ * Time Complexity: O(n)
217
+ * Space Complexity: O(1)
218
+ *
219
+ * The `reverse` function in TypeScript reverses the elements of an array starting from a specified
220
+ * offset.
221
+ * @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
222
+ * the elements in the array and resetting the offset to 0.
223
+ */
224
+ reverse() {
225
+ this._elements = this.elements.slice(this.offset).reverse();
226
+ this._offset = 0;
227
+ return this;
228
+ }
229
+ /**
230
+ * Time Complexity: O(n)
231
+ * Space Complexity: O(1)
232
+ *
233
+ * The function `addAt` inserts a new element at a specified index in an array, returning true if
234
+ * successful and false if the index is out of bounds.
235
+ * @param {number} index - The `index` parameter represents the position at which the `newElement`
236
+ * should be added in the array.
237
+ * @param {E} newElement - The `newElement` parameter represents the element that you want to insert
238
+ * into the array at the specified index.
239
+ * @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
240
+ * added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
241
+ * than the length of the array).
242
+ */
243
+ addAt(index, newElement) {
244
+ if (index < 0 || index > this.length)
245
+ return false;
246
+ this._elements.splice(this.offset + index, 0, newElement);
247
+ return true;
248
+ }
186
249
  /**
187
250
  * Time Complexity: O(1)
188
251
  * Space Complexity: O(1)
189
252
  *
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.
253
+ * The function `setAt` updates an element at a specified index in an array-like data structure.
254
+ * @param {number} index - The `index` parameter is a number that represents the position in the
255
+ * array where the new element will be set.
256
+ * @param {E} newElement - The `newElement` parameter represents the new value that you want to set
257
+ * at the specified index in the array.
258
+ * @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
259
+ * at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
260
+ * length of the array).
192
261
  */
193
- isEmpty() {
194
- return this.size === 0;
262
+ setAt(index, newElement) {
263
+ if (index < 0 || index > this.length)
264
+ return false;
265
+ this._elements[this.offset + index] = newElement;
266
+ return true;
195
267
  }
196
268
  /**
197
269
  * Time Complexity: O(1)
198
- * Space Complexity: O(n)
270
+ * Space Complexity: O(1)
199
271
  *
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.
272
+ * The function checks if a data structure is empty by comparing its length to zero.
273
+ * @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
202
274
  */
203
- toArray() {
204
- return this.elements.slice(this.offset);
275
+ isEmpty() {
276
+ return this.length === 0;
205
277
  }
206
278
  /**
207
279
  * Time Complexity: O(1)
@@ -226,6 +298,34 @@ class Queue extends base_1.IterableElementBase {
226
298
  this._offset = 0;
227
299
  return true;
228
300
  }
301
+ /**
302
+ * Time Complexity: O(n)
303
+ * Space Complexity: O(n)
304
+ *
305
+ * The function overrides the splice method to remove and insert elements in a queue-like data
306
+ * structure.
307
+ * @param {number} start - The `start` parameter in the `splice` method specifies the index at which
308
+ * to start changing the array. Items will be added or removed starting from this index.
309
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
310
+ * number of elements to remove from the array starting at the specified `start` index. If
311
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
312
+ * elements can still be inserted at
313
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
314
+ * will be added to the array at the specified `start` index. These elements will replace the
315
+ * existing elements starting from the `start` index for the `deleteCount` number of elements.
316
+ * @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
317
+ * class as the original object.
318
+ */
319
+ splice(start, deleteCount = 0, ...items) {
320
+ const removedQueue = this._createInstance();
321
+ start = Math.max(0, Math.min(start, this.length));
322
+ deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
323
+ const globalStartIndex = this.offset + start;
324
+ const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
325
+ removedQueue.pushMany(removedElements);
326
+ this.compact();
327
+ return removedQueue;
328
+ }
229
329
  /**
230
330
  * Time Complexity: O(n)
231
331
  * Space Complexity: O(n)
@@ -234,7 +334,7 @@ class Queue extends base_1.IterableElementBase {
234
334
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
235
335
  */
236
336
  clone() {
237
- return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
337
+ return new Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen });
238
338
  }
239
339
  /**
240
340
  * Time Complexity: O(n)
@@ -253,7 +353,11 @@ class Queue extends base_1.IterableElementBase {
253
353
  * satisfy the given predicate function.
254
354
  */
255
355
  filter(predicate, thisArg) {
256
- const newDeque = new Queue([], { toElementFn: this.toElementFn });
356
+ const newDeque = this._createInstance({
357
+ toElementFn: this._toElementFn,
358
+ autoCompactRatio: this._autoCompactRatio,
359
+ maxLen: this._maxLen
360
+ });
257
361
  let index = 0;
258
362
  for (const el of this) {
259
363
  if (predicate.call(thisArg, el, index, this)) {
@@ -282,7 +386,11 @@ class Queue extends base_1.IterableElementBase {
282
386
  * callback function to each element in the original Queue object.
283
387
  */
284
388
  map(callback, toElementFn, thisArg) {
285
- const newDeque = new Queue([], { toElementFn });
389
+ const newDeque = new Queue([], {
390
+ toElementFn,
391
+ autoCompactRatio: this._autoCompactRatio,
392
+ maxLen: this._maxLen
393
+ });
286
394
  let index = 0;
287
395
  for (const el of this) {
288
396
  newDeque.push(callback.call(thisArg, el, index, this));
@@ -301,6 +409,29 @@ class Queue extends base_1.IterableElementBase {
301
409
  yield item;
302
410
  }
303
411
  }
412
+ /**
413
+ * The function `_createInstance` returns a new instance of the `Queue` class with the specified
414
+ * options.
415
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
416
+ * `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
417
+ * allows you to specify settings or properties that can influence how the queue operates.
418
+ * @returns An instance of the `Queue` class with an empty array and the provided options is being
419
+ * returned.
420
+ */
421
+ _createInstance(options) {
422
+ return new Queue([], options);
423
+ }
424
+ /**
425
+ * The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
426
+ * order.
427
+ */
428
+ *_getReverseIterator() {
429
+ for (let i = this.length - 1; i >= 0; i--) {
430
+ const cur = this.at(i); // `at()` handles the offset.
431
+ if (cur !== undefined)
432
+ yield cur;
433
+ }
434
+ }
304
435
  }
305
436
  exports.Queue = Queue;
306
437
  /**
@@ -319,7 +450,7 @@ class LinkedListQueue extends linked_list_1.SinglyLinkedList {
319
450
  * values as the original `LinkedListQueue`.
320
451
  */
321
452
  clone() {
322
- return new LinkedListQueue(this, { toElementFn: this.toElementFn });
453
+ return new LinkedListQueue(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
323
454
  }
324
455
  }
325
456
  exports.LinkedListQueue = LinkedListQueue;