directed-graph-typed 2.0.4 → 2.1.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.
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +612 -879
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +681 -905
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- package/src/utils/utils.ts +152 -86
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.LinkedListQueue = exports.Queue = void 0;
|
|
4
11
|
const linked_list_1 = require("../linked-list");
|
|
5
12
|
const linear_base_1 = require("../base/linear-base");
|
|
6
13
|
/**
|
|
14
|
+
* Array-backed queue with amortized O(1) enqueue/dequeue via an offset pointer and optional auto-compaction.
|
|
15
|
+
* @remarks Time O(1), Space O(1)
|
|
16
|
+
* @template E
|
|
17
|
+
* @template R
|
|
7
18
|
* 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
19
|
* 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).
|
|
9
20
|
* 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
|
|
@@ -20,7 +31,7 @@ const linear_base_1 = require("../base/linear-base");
|
|
|
20
31
|
* let maxSum = 0;
|
|
21
32
|
* let currentSum = 0;
|
|
22
33
|
*
|
|
23
|
-
* nums.forEach(
|
|
34
|
+
* nums.forEach(num => {
|
|
24
35
|
* queue.push(num);
|
|
25
36
|
* currentSum += num;
|
|
26
37
|
*
|
|
@@ -60,6 +71,13 @@ const linear_base_1 = require("../base/linear-base");
|
|
|
60
71
|
* console.log(visited); // [1, 2, 3, 4, 5]
|
|
61
72
|
*/
|
|
62
73
|
class Queue extends linear_base_1.LinearBase {
|
|
74
|
+
/**
|
|
75
|
+
* Create a Queue and optionally bulk-insert elements.
|
|
76
|
+
* @remarks Time O(N), Space O(N)
|
|
77
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
78
|
+
* @param [options] - Options such as toElementFn, maxLen, and autoCompactRatio.
|
|
79
|
+
* @returns New Queue instance.
|
|
80
|
+
*/
|
|
63
81
|
constructor(elements = [], options) {
|
|
64
82
|
super(options);
|
|
65
83
|
this._elements = [];
|
|
@@ -71,63 +89,86 @@ class Queue extends linear_base_1.LinearBase {
|
|
|
71
89
|
}
|
|
72
90
|
this.pushMany(elements);
|
|
73
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the underlying array buffer.
|
|
94
|
+
* @remarks Time O(1), Space O(1)
|
|
95
|
+
* @returns Backing array of elements.
|
|
96
|
+
*/
|
|
74
97
|
get elements() {
|
|
75
98
|
return this._elements;
|
|
76
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Get the current start offset into the array.
|
|
102
|
+
* @remarks Time O(1), Space O(1)
|
|
103
|
+
* @returns Zero-based offset.
|
|
104
|
+
*/
|
|
77
105
|
get offset() {
|
|
78
106
|
return this._offset;
|
|
79
107
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Get the compaction threshold (offset/size).
|
|
110
|
+
* @remarks Time O(1), Space O(1)
|
|
111
|
+
* @returns Auto-compaction ratio in (0,1].
|
|
112
|
+
*/
|
|
83
113
|
get autoCompactRatio() {
|
|
84
114
|
return this._autoCompactRatio;
|
|
85
115
|
}
|
|
86
|
-
|
|
87
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Set the compaction threshold.
|
|
118
|
+
* @remarks Time O(1), Space O(1)
|
|
119
|
+
* @param value - New ratio; compacts when offset/size exceeds this value.
|
|
120
|
+
* @returns void
|
|
121
|
+
*/
|
|
122
|
+
set autoCompactRatio(value) {
|
|
123
|
+
this._autoCompactRatio = value;
|
|
88
124
|
}
|
|
89
125
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
126
|
+
* Get the number of elements currently in the queue.
|
|
127
|
+
* @remarks Time O(1), Space O(1)
|
|
128
|
+
* @returns Current length.
|
|
129
|
+
*/
|
|
130
|
+
get length() {
|
|
131
|
+
return this.elements.length - this._offset;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the first element (front) without removing it.
|
|
135
|
+
* @remarks Time O(1), Space O(1)
|
|
136
|
+
* @returns Front element or undefined.
|
|
96
137
|
*/
|
|
97
138
|
get first() {
|
|
98
|
-
return this.length > 0 ? this.elements[this.
|
|
139
|
+
return this.length > 0 ? this.elements[this._offset] : undefined;
|
|
99
140
|
}
|
|
100
141
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
105
|
-
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
106
|
-
* array is empty, it returns `undefined`.
|
|
142
|
+
* Get the last element (back) without removing it.
|
|
143
|
+
* @remarks Time O(1), Space O(1)
|
|
144
|
+
* @returns Back element or undefined.
|
|
107
145
|
*/
|
|
108
146
|
get last() {
|
|
109
147
|
return this.length > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
110
148
|
}
|
|
111
149
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* @
|
|
117
|
-
* @param {E[]} elements - The "elements" parameter is an array of elements of type E.
|
|
118
|
-
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
119
|
-
* array.
|
|
150
|
+
* Create a queue from an array of elements.
|
|
151
|
+
* @remarks Time O(N), Space O(N)
|
|
152
|
+
* @template E
|
|
153
|
+
* @param elements - Array of elements to enqueue in order.
|
|
154
|
+
* @returns A new queue populated from the array.
|
|
120
155
|
*/
|
|
121
156
|
static fromArray(elements) {
|
|
122
157
|
return new Queue(elements);
|
|
123
158
|
}
|
|
124
159
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
160
|
+
* Check whether the queue is empty.
|
|
161
|
+
* @remarks Time O(1), Space O(1)
|
|
162
|
+
* @returns True if length is 0.
|
|
163
|
+
*/
|
|
164
|
+
isEmpty() {
|
|
165
|
+
return this.length === 0;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Enqueue one element at the back.
|
|
169
|
+
* @remarks Time O(1), Space O(1)
|
|
170
|
+
* @param element - Element to enqueue.
|
|
171
|
+
* @returns True on success.
|
|
131
172
|
*/
|
|
132
173
|
push(element) {
|
|
133
174
|
this.elements.push(element);
|
|
@@ -136,15 +177,10 @@ class Queue extends linear_base_1.LinearBase {
|
|
|
136
177
|
return true;
|
|
137
178
|
}
|
|
138
179
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* transformation function if provided.
|
|
144
|
-
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
145
|
-
* is an iterable containing elements of type `E` or `R`.
|
|
146
|
-
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
147
|
-
* element was successfully pushed into the data structure.
|
|
180
|
+
* Enqueue many elements from an iterable.
|
|
181
|
+
* @remarks Time O(N), Space O(1)
|
|
182
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
183
|
+
* @returns Array of per-element success flags.
|
|
148
184
|
*/
|
|
149
185
|
pushMany(elements) {
|
|
150
186
|
const ans = [];
|
|
@@ -157,300 +193,281 @@ class Queue extends linear_base_1.LinearBase {
|
|
|
157
193
|
return ans;
|
|
158
194
|
}
|
|
159
195
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
164
|
-
* necessary to optimize performance.
|
|
165
|
-
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
196
|
+
* Dequeue one element from the front (amortized via offset).
|
|
197
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
198
|
+
* @returns Removed element or undefined.
|
|
166
199
|
*/
|
|
167
200
|
shift() {
|
|
168
201
|
if (this.length === 0)
|
|
169
202
|
return undefined;
|
|
170
203
|
const first = this.first;
|
|
171
204
|
this._offset += 1;
|
|
172
|
-
if (this.offset / this.elements.length > this.autoCompactRatio)
|
|
205
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
|
|
173
206
|
this.compact();
|
|
174
207
|
return first;
|
|
175
208
|
}
|
|
176
209
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
* @param {E} element - Specify the element to be deleted
|
|
182
|
-
* @return A boolean value indicating whether the element was successfully deleted or not
|
|
210
|
+
* Delete the first occurrence of a specific element.
|
|
211
|
+
* @remarks Time O(N), Space O(1)
|
|
212
|
+
* @param element - Element to remove (strict equality via Object.is).
|
|
213
|
+
* @returns True if an element was removed.
|
|
183
214
|
*/
|
|
184
215
|
delete(element) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
* The deleteAt function deletes the element at a given index.
|
|
193
|
-
* @param {number} index - Determine the index of the element to be deleted
|
|
194
|
-
* @return A boolean value
|
|
195
|
-
*/
|
|
196
|
-
deleteAt(index) {
|
|
197
|
-
const deleted = this.elements[index];
|
|
198
|
-
this.elements.splice(index, 1);
|
|
199
|
-
return deleted;
|
|
216
|
+
for (let i = this._offset; i < this.elements.length; i++) {
|
|
217
|
+
if (Object.is(this.elements[i], element)) {
|
|
218
|
+
this.elements.splice(i, 1);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return false;
|
|
200
223
|
}
|
|
201
224
|
/**
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
* if the index is out of bounds.
|
|
207
|
-
* @param {number} index - The `index` parameter represents the position of the element you want to
|
|
208
|
-
* retrieve from the data structure.
|
|
209
|
-
* @returns The `at` method is returning the element at the specified index adjusted by the offset
|
|
210
|
-
* `_offset`.
|
|
225
|
+
* Get the element at a given logical index.
|
|
226
|
+
* @remarks Time O(1), Space O(1)
|
|
227
|
+
* @param index - Zero-based index from the front.
|
|
228
|
+
* @returns Element or undefined.
|
|
211
229
|
*/
|
|
212
230
|
at(index) {
|
|
213
|
-
|
|
231
|
+
if (index < 0 || index >= this.length)
|
|
232
|
+
return undefined;
|
|
233
|
+
return this._elements[this._offset + index];
|
|
214
234
|
}
|
|
215
235
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
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.
|
|
236
|
+
* Delete the element at a given index.
|
|
237
|
+
* @remarks Time O(N), Space O(1)
|
|
238
|
+
* @param index - Zero-based index from the front.
|
|
239
|
+
* @returns Removed element or undefined.
|
|
223
240
|
*/
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
241
|
+
deleteAt(index) {
|
|
242
|
+
if (index < 0 || index >= this.length)
|
|
243
|
+
return undefined;
|
|
244
|
+
const gi = this._offset + index;
|
|
245
|
+
const [deleted] = this.elements.splice(gi, 1);
|
|
246
|
+
return deleted;
|
|
228
247
|
}
|
|
229
248
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
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).
|
|
249
|
+
* Insert a new element at a given index.
|
|
250
|
+
* @remarks Time O(N), Space O(1)
|
|
251
|
+
* @param index - Zero-based index from the front.
|
|
252
|
+
* @param newElement - Element to insert.
|
|
253
|
+
* @returns True if inserted.
|
|
242
254
|
*/
|
|
243
255
|
addAt(index, newElement) {
|
|
244
256
|
if (index < 0 || index > this.length)
|
|
245
257
|
return false;
|
|
246
|
-
this._elements.splice(this.
|
|
258
|
+
this._elements.splice(this._offset + index, 0, newElement);
|
|
247
259
|
return true;
|
|
248
260
|
}
|
|
249
261
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @
|
|
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).
|
|
262
|
+
* Replace the element at a given index.
|
|
263
|
+
* @remarks Time O(1), Space O(1)
|
|
264
|
+
* @param index - Zero-based index from the front.
|
|
265
|
+
* @param newElement - New element to set.
|
|
266
|
+
* @returns True if updated.
|
|
261
267
|
*/
|
|
262
268
|
setAt(index, newElement) {
|
|
263
|
-
if (index < 0 || index
|
|
269
|
+
if (index < 0 || index >= this.length)
|
|
264
270
|
return false;
|
|
265
|
-
this._elements[this.
|
|
271
|
+
this._elements[this._offset + index] = newElement;
|
|
266
272
|
return true;
|
|
267
273
|
}
|
|
268
274
|
/**
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
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.
|
|
275
|
+
* Reverse the queue in-place by compacting then reversing.
|
|
276
|
+
* @remarks Time O(N), Space O(N)
|
|
277
|
+
* @returns This queue.
|
|
274
278
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
279
|
+
reverse() {
|
|
280
|
+
this._elements = this.elements.slice(this._offset).reverse();
|
|
281
|
+
this._offset = 0;
|
|
282
|
+
return this;
|
|
277
283
|
}
|
|
278
284
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
* The clear function resets the elements array and offset to their initial values.
|
|
285
|
+
* Remove all elements and reset offset.
|
|
286
|
+
* @remarks Time O(1), Space O(1)
|
|
287
|
+
* @returns void
|
|
283
288
|
*/
|
|
284
289
|
clear() {
|
|
285
290
|
this._elements = [];
|
|
286
291
|
this._offset = 0;
|
|
287
292
|
}
|
|
288
293
|
/**
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
293
|
-
* offset to zero.
|
|
294
|
-
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
294
|
+
* Compact storage by discarding consumed head elements.
|
|
295
|
+
* @remarks Time O(N), Space O(N)
|
|
296
|
+
* @returns True when compaction performed.
|
|
295
297
|
*/
|
|
296
298
|
compact() {
|
|
297
|
-
this._elements = this.elements.slice(this.
|
|
299
|
+
this._elements = this.elements.slice(this._offset);
|
|
298
300
|
this._offset = 0;
|
|
299
301
|
return true;
|
|
300
302
|
}
|
|
301
303
|
/**
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
* @
|
|
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.
|
|
304
|
+
* Remove and/or insert elements at a position (array-like).
|
|
305
|
+
* @remarks Time O(N + M), Space O(M)
|
|
306
|
+
* @param start - Start index (clamped to [0, length]).
|
|
307
|
+
* @param [deleteCount] - Number of elements to remove (default 0).
|
|
308
|
+
* @param [items] - Elements to insert after `start`.
|
|
309
|
+
* @returns A new queue containing the removed elements (typed as `this`).
|
|
318
310
|
*/
|
|
319
311
|
splice(start, deleteCount = 0, ...items) {
|
|
320
|
-
const removedQueue = this._createInstance();
|
|
321
312
|
start = Math.max(0, Math.min(start, this.length));
|
|
322
313
|
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
323
|
-
const
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
314
|
+
const gi = this._offset + start;
|
|
315
|
+
const removedArray = this._elements.splice(gi, deleteCount, ...items);
|
|
316
|
+
if (this.elements.length > 0 && this.offset / this.elements.length > this.autoCompactRatio)
|
|
317
|
+
this.compact();
|
|
318
|
+
const removed = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
319
|
+
removed._setAutoCompactRatio(this._autoCompactRatio);
|
|
320
|
+
removed.pushMany(removedArray);
|
|
321
|
+
return removed;
|
|
328
322
|
}
|
|
329
323
|
/**
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
334
|
-
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
324
|
+
* Deep clone this queue and its parameters.
|
|
325
|
+
* @remarks Time O(N), Space O(N)
|
|
326
|
+
* @returns A new queue with the same content and options.
|
|
335
327
|
*/
|
|
336
328
|
clone() {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
*
|
|
345
|
-
* @
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
* queue
|
|
349
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
350
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
351
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
352
|
-
* @returns The `filter` method is returning a new `Queue` object that contains the elements that
|
|
353
|
-
* satisfy the given predicate function.
|
|
329
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
330
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
331
|
+
for (let i = this._offset; i < this.elements.length; i++)
|
|
332
|
+
out.push(this.elements[i]);
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Filter elements into a new queue of the same class.
|
|
337
|
+
* @remarks Time O(N), Space O(N)
|
|
338
|
+
* @param predicate - Predicate (element, index, queue) → boolean to keep element.
|
|
339
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
340
|
+
* @returns A new queue with kept elements.
|
|
354
341
|
*/
|
|
355
342
|
filter(predicate, thisArg) {
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
autoCompactRatio: this._autoCompactRatio,
|
|
359
|
-
maxLen: this._maxLen
|
|
360
|
-
});
|
|
343
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
344
|
+
out._setAutoCompactRatio(this._autoCompactRatio);
|
|
361
345
|
let index = 0;
|
|
362
|
-
for (const
|
|
363
|
-
if (predicate.call(thisArg,
|
|
364
|
-
|
|
365
|
-
}
|
|
346
|
+
for (const v of this) {
|
|
347
|
+
if (predicate.call(thisArg, v, index, this))
|
|
348
|
+
out.push(v);
|
|
366
349
|
index++;
|
|
367
350
|
}
|
|
368
|
-
return
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
*
|
|
375
|
-
* element
|
|
376
|
-
* @param
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
* callback function to each element in the original Queue object.
|
|
387
|
-
*/
|
|
388
|
-
map(callback, toElementFn, thisArg) {
|
|
389
|
-
const newDeque = new Queue([], {
|
|
390
|
-
toElementFn,
|
|
391
|
-
autoCompactRatio: this._autoCompactRatio,
|
|
392
|
-
maxLen: this._maxLen
|
|
351
|
+
return out;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Map each element to a new element in a possibly different-typed queue.
|
|
355
|
+
* @remarks Time O(N), Space O(N)
|
|
356
|
+
* @template EM
|
|
357
|
+
* @template RM
|
|
358
|
+
* @param callback - Mapping function (element, index, queue) → newElement.
|
|
359
|
+
* @param [options] - Options for the output queue (e.g., toElementFn, maxLen, autoCompactRatio).
|
|
360
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
361
|
+
* @returns A new Queue with mapped elements.
|
|
362
|
+
*/
|
|
363
|
+
map(callback, options, thisArg) {
|
|
364
|
+
var _a, _b;
|
|
365
|
+
const out = new this.constructor([], {
|
|
366
|
+
toElementFn: options === null || options === void 0 ? void 0 : options.toElementFn,
|
|
367
|
+
maxLen: (_a = options === null || options === void 0 ? void 0 : options.maxLen) !== null && _a !== void 0 ? _a : this._maxLen,
|
|
368
|
+
autoCompactRatio: (_b = options === null || options === void 0 ? void 0 : options.autoCompactRatio) !== null && _b !== void 0 ? _b : this._autoCompactRatio
|
|
393
369
|
});
|
|
394
370
|
let index = 0;
|
|
395
|
-
for (const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
return newDeque;
|
|
371
|
+
for (const v of this)
|
|
372
|
+
out.push(thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this));
|
|
373
|
+
return out;
|
|
400
374
|
}
|
|
401
375
|
/**
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
376
|
+
* Map each element to a new value of the same type.
|
|
377
|
+
* @remarks Time O(N), Space O(N)
|
|
378
|
+
* @param callback - Mapping function (element, index, queue) → element.
|
|
379
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
380
|
+
* @returns A new queue with mapped elements (same element type).
|
|
406
381
|
*/
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
382
|
+
mapSame(callback, thisArg) {
|
|
383
|
+
var _a;
|
|
384
|
+
const Ctor = this.constructor;
|
|
385
|
+
const out = new Ctor([], {
|
|
386
|
+
toElementFn: this.toElementFn,
|
|
387
|
+
maxLen: this._maxLen,
|
|
388
|
+
autoCompactRatio: this._autoCompactRatio
|
|
389
|
+
});
|
|
390
|
+
(_a = out._setAutoCompactRatio) === null || _a === void 0 ? void 0 : _a.call(out, this._autoCompactRatio);
|
|
391
|
+
let index = 0;
|
|
392
|
+
for (const v of this) {
|
|
393
|
+
const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
394
|
+
out.push(mv);
|
|
410
395
|
}
|
|
396
|
+
return out;
|
|
411
397
|
}
|
|
412
398
|
/**
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* @param
|
|
416
|
-
*
|
|
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.
|
|
399
|
+
* (Protected) Set the internal auto-compaction ratio.
|
|
400
|
+
* @remarks Time O(1), Space O(1)
|
|
401
|
+
* @param value - New ratio to assign.
|
|
402
|
+
* @returns void
|
|
420
403
|
*/
|
|
421
|
-
|
|
422
|
-
|
|
404
|
+
_setAutoCompactRatio(value) {
|
|
405
|
+
this._autoCompactRatio = value;
|
|
423
406
|
}
|
|
424
407
|
/**
|
|
425
|
-
*
|
|
426
|
-
*
|
|
408
|
+
* (Protected) Iterate elements from front to back.
|
|
409
|
+
* @remarks Time O(N), Space O(1)
|
|
410
|
+
* @returns Iterator of E.
|
|
411
|
+
*/
|
|
412
|
+
*_getIterator() {
|
|
413
|
+
for (let i = this._offset; i < this.elements.length; i++)
|
|
414
|
+
yield this.elements[i];
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* (Protected) Iterate elements from back to front.
|
|
418
|
+
* @remarks Time O(N), Space O(1)
|
|
419
|
+
* @returns Iterator of E.
|
|
427
420
|
*/
|
|
428
421
|
*_getReverseIterator() {
|
|
429
422
|
for (let i = this.length - 1; i >= 0; i--) {
|
|
430
|
-
const cur = this.at(i);
|
|
423
|
+
const cur = this.at(i);
|
|
431
424
|
if (cur !== undefined)
|
|
432
425
|
yield cur;
|
|
433
426
|
}
|
|
434
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
430
|
+
* @remarks Time O(1), Space O(1)
|
|
431
|
+
* @param [options] - Options forwarded to the constructor.
|
|
432
|
+
* @returns An empty like-kind queue instance.
|
|
433
|
+
*/
|
|
434
|
+
_createInstance(options) {
|
|
435
|
+
const Ctor = this.constructor;
|
|
436
|
+
return new Ctor([], options);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* (Protected) Create a like-kind queue and seed it from an iterable.
|
|
440
|
+
* @remarks Time O(N), Space O(N)
|
|
441
|
+
* @template EM
|
|
442
|
+
* @template RM
|
|
443
|
+
* @param [elements] - Iterable used to seed the new queue.
|
|
444
|
+
* @param [options] - Options forwarded to the constructor.
|
|
445
|
+
* @returns A like-kind Queue instance.
|
|
446
|
+
*/
|
|
447
|
+
_createLike(elements = [], options) {
|
|
448
|
+
const Ctor = this.constructor;
|
|
449
|
+
return new Ctor(elements, options);
|
|
450
|
+
}
|
|
435
451
|
}
|
|
436
452
|
exports.Queue = Queue;
|
|
437
453
|
/**
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
441
|
-
*
|
|
454
|
+
* Queue implemented over a singly linked list; preserves head/tail operations with linear scans for queries.
|
|
455
|
+
* @remarks Time O(1), Space O(1)
|
|
456
|
+
* @template E
|
|
457
|
+
* @template R
|
|
458
|
+
* @example examples will be generated by unit test
|
|
442
459
|
*/
|
|
443
460
|
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
444
461
|
/**
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
* the current instance.
|
|
449
|
-
* @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
|
|
450
|
-
* values as the original `LinkedListQueue`.
|
|
462
|
+
* Deep clone this linked-list-based queue.
|
|
463
|
+
* @remarks Time O(N), Space O(N)
|
|
464
|
+
* @returns A new queue with the same sequence of elements.
|
|
451
465
|
*/
|
|
452
466
|
clone() {
|
|
453
|
-
|
|
467
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
468
|
+
for (const v of this)
|
|
469
|
+
out.push(v);
|
|
470
|
+
return out;
|
|
454
471
|
}
|
|
455
472
|
}
|
|
456
473
|
exports.LinkedListQueue = LinkedListQueue;
|