graph-typed 2.0.5 → 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 +598 -869
- 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 +1 -0
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +2 -1
- 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 +664 -893
- 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 +2 -0
- package/src/utils/utils.ts +9 -14
|
@@ -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.Deque = void 0;
|
|
4
11
|
const utils_1 = require("../../utils");
|
|
5
12
|
const linear_base_1 = require("../base/linear-base");
|
|
6
13
|
/**
|
|
14
|
+
* Deque implemented with circular buckets allowing O(1) amortized push/pop at both ends.
|
|
15
|
+
* @remarks Time O(1), Space O(1)
|
|
16
|
+
* @template E
|
|
17
|
+
* @template R
|
|
7
18
|
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
|
8
19
|
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
|
9
20
|
* 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
|
|
@@ -100,18 +111,15 @@ const linear_base_1 = require("../base/linear-base");
|
|
|
100
111
|
*/
|
|
101
112
|
class Deque extends linear_base_1.LinearBase {
|
|
102
113
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @
|
|
105
|
-
* elements
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
|
|
109
|
-
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
|
|
110
|
-
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
111
|
-
* or is not a number
|
|
114
|
+
* Create a Deque and optionally bulk-insert elements.
|
|
115
|
+
* @remarks Time O(N), Space O(N)
|
|
116
|
+
* @param [elements] - Iterable (or iterable-like) of elements/records to insert.
|
|
117
|
+
* @param [options] - Options such as bucketSize, toElementFn, and maxLen.
|
|
118
|
+
* @returns New Deque instance.
|
|
112
119
|
*/
|
|
113
120
|
constructor(elements = [], options) {
|
|
114
121
|
super(options);
|
|
122
|
+
this._equals = Object.is;
|
|
115
123
|
this._bucketSize = 1 << 12;
|
|
116
124
|
this._bucketFirst = 0;
|
|
117
125
|
this._firstInBucket = 0;
|
|
@@ -127,16 +135,10 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
127
135
|
}
|
|
128
136
|
let _size;
|
|
129
137
|
if ('length' in elements) {
|
|
130
|
-
|
|
131
|
-
_size = elements.length();
|
|
132
|
-
else
|
|
133
|
-
_size = elements.length;
|
|
138
|
+
_size = typeof elements.length === 'function' ? elements.length() : elements.length;
|
|
134
139
|
}
|
|
135
140
|
else {
|
|
136
|
-
|
|
137
|
-
_size = elements.size();
|
|
138
|
-
else
|
|
139
|
-
_size = elements.size;
|
|
141
|
+
_size = typeof elements.size === 'function' ? elements.size() : elements.size;
|
|
140
142
|
}
|
|
141
143
|
this._bucketCount = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize) || 1;
|
|
142
144
|
for (let i = 0; i < this._bucketCount; ++i) {
|
|
@@ -147,34 +149,74 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
147
149
|
this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
|
|
148
150
|
this.pushMany(elements);
|
|
149
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Get the current bucket size.
|
|
154
|
+
* @remarks Time O(1), Space O(1)
|
|
155
|
+
* @returns Bucket capacity per bucket.
|
|
156
|
+
*/
|
|
150
157
|
get bucketSize() {
|
|
151
158
|
return this._bucketSize;
|
|
152
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Get the index of the first bucket in use.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
* @returns Zero-based bucket index.
|
|
164
|
+
*/
|
|
153
165
|
get bucketFirst() {
|
|
154
166
|
return this._bucketFirst;
|
|
155
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the index inside the first bucket.
|
|
170
|
+
* @remarks Time O(1), Space O(1)
|
|
171
|
+
* @returns Zero-based index within the first bucket.
|
|
172
|
+
*/
|
|
156
173
|
get firstInBucket() {
|
|
157
174
|
return this._firstInBucket;
|
|
158
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Get the index of the last bucket in use.
|
|
178
|
+
* @remarks Time O(1), Space O(1)
|
|
179
|
+
* @returns Zero-based bucket index.
|
|
180
|
+
*/
|
|
159
181
|
get bucketLast() {
|
|
160
182
|
return this._bucketLast;
|
|
161
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Get the index inside the last bucket.
|
|
186
|
+
* @remarks Time O(1), Space O(1)
|
|
187
|
+
* @returns Zero-based index within the last bucket.
|
|
188
|
+
*/
|
|
162
189
|
get lastInBucket() {
|
|
163
190
|
return this._lastInBucket;
|
|
164
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Get the number of buckets allocated.
|
|
194
|
+
* @remarks Time O(1), Space O(1)
|
|
195
|
+
* @returns Bucket count.
|
|
196
|
+
*/
|
|
165
197
|
get bucketCount() {
|
|
166
198
|
return this._bucketCount;
|
|
167
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Get the internal buckets array.
|
|
202
|
+
* @remarks Time O(1), Space O(1)
|
|
203
|
+
* @returns Array of buckets storing values.
|
|
204
|
+
*/
|
|
168
205
|
get buckets() {
|
|
169
206
|
return this._buckets;
|
|
170
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Get the number of elements in the deque.
|
|
210
|
+
* @remarks Time O(1), Space O(1)
|
|
211
|
+
* @returns Current length.
|
|
212
|
+
*/
|
|
171
213
|
get length() {
|
|
172
214
|
return this._length;
|
|
173
215
|
}
|
|
174
216
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* @returns
|
|
217
|
+
* Get the first element without removing it.
|
|
218
|
+
* @remarks Time O(1), Space O(1)
|
|
219
|
+
* @returns First element or undefined.
|
|
178
220
|
*/
|
|
179
221
|
get first() {
|
|
180
222
|
if (this._length === 0)
|
|
@@ -182,8 +224,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
182
224
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
183
225
|
}
|
|
184
226
|
/**
|
|
185
|
-
*
|
|
186
|
-
* @
|
|
227
|
+
* Get the last element without removing it.
|
|
228
|
+
* @remarks Time O(1), Space O(1)
|
|
229
|
+
* @returns Last element or undefined.
|
|
187
230
|
*/
|
|
188
231
|
get last() {
|
|
189
232
|
if (this._length === 0)
|
|
@@ -191,13 +234,23 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
191
234
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
192
235
|
}
|
|
193
236
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* @param
|
|
199
|
-
*
|
|
200
|
-
* @
|
|
237
|
+
* Create a Deque from an array of elements.
|
|
238
|
+
* @remarks Time O(N), Space O(N)
|
|
239
|
+
* @template E
|
|
240
|
+
* @template R
|
|
241
|
+
* @param this - Constructor (subclass) to instantiate.
|
|
242
|
+
* @param data - Array of elements to insert in order.
|
|
243
|
+
* @param [options] - Options forwarded to the constructor.
|
|
244
|
+
* @returns A new Deque populated from the array.
|
|
245
|
+
*/
|
|
246
|
+
static fromArray(data, options) {
|
|
247
|
+
return new this(data, options);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Append one element at the back.
|
|
251
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
252
|
+
* @param element - Element to append.
|
|
253
|
+
* @returns True when appended.
|
|
201
254
|
*/
|
|
202
255
|
push(element) {
|
|
203
256
|
if (this._length) {
|
|
@@ -222,12 +275,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
222
275
|
return true;
|
|
223
276
|
}
|
|
224
277
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* The `pop()` function removes and returns the last element from a data structure, updating the
|
|
229
|
-
* internal state variables accordingly.
|
|
230
|
-
* @returns The element that was removed from the data structure is being returned.
|
|
278
|
+
* Remove and return the last element.
|
|
279
|
+
* @remarks Time O(1), Space O(1)
|
|
280
|
+
* @returns Removed element or undefined.
|
|
231
281
|
*/
|
|
232
282
|
pop() {
|
|
233
283
|
if (this._length === 0)
|
|
@@ -250,13 +300,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
250
300
|
return element;
|
|
251
301
|
}
|
|
252
302
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* The `shift()` function removes and returns the first element from a data structure, updating the
|
|
257
|
-
* internal state variables accordingly.
|
|
258
|
-
* @returns The element that is being removed from the beginning of the data structure is being
|
|
259
|
-
* returned.
|
|
303
|
+
* Remove and return the first element.
|
|
304
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
305
|
+
* @returns Removed element or undefined.
|
|
260
306
|
*/
|
|
261
307
|
shift() {
|
|
262
308
|
if (this._length === 0)
|
|
@@ -279,14 +325,10 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
279
325
|
return element;
|
|
280
326
|
}
|
|
281
327
|
/**
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
* returns the new size of the structure.
|
|
287
|
-
* @param {E} element - The `element` parameter represents the element that you want to add to the
|
|
288
|
-
* beginning of the data structure.
|
|
289
|
-
* @returns The size of the data structure after the element has been added.
|
|
328
|
+
* Prepend one element at the front.
|
|
329
|
+
* @remarks Time O(1) amortized, Space O(1)
|
|
330
|
+
* @param element - Element to prepend.
|
|
331
|
+
* @returns True when prepended.
|
|
290
332
|
*/
|
|
291
333
|
unshift(element) {
|
|
292
334
|
if (this._length) {
|
|
@@ -311,18 +353,10 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
311
353
|
return true;
|
|
312
354
|
}
|
|
313
355
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
* transformation function if provided.
|
|
319
|
-
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
320
|
-
* parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
|
|
321
|
-
* or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
|
|
322
|
-
* function iterates over each element
|
|
323
|
-
* @returns The `pushMany` function is returning an array of boolean values, where each value
|
|
324
|
-
* represents the result of calling the `push` method on the current object instance with the
|
|
325
|
-
* corresponding element from the input `elements` iterable.
|
|
356
|
+
* Append a sequence of elements.
|
|
357
|
+
* @remarks Time O(N), Space O(1)
|
|
358
|
+
* @param elements - Iterable (or iterable-like) of elements/records.
|
|
359
|
+
* @returns Array of per-element success flags.
|
|
326
360
|
*/
|
|
327
361
|
pushMany(elements) {
|
|
328
362
|
const ans = [];
|
|
@@ -337,17 +371,10 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
337
371
|
return ans;
|
|
338
372
|
}
|
|
339
373
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
* an array, optionally converting them using a provided function.
|
|
345
|
-
* @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
|
|
346
|
-
* parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
|
|
347
|
-
* can be an array or any other iterable data structure that has a known size or length. The function
|
|
348
|
-
* iterates over each element in the `elements` iterable and
|
|
349
|
-
* @returns The `unshiftMany` function returns an array of boolean values indicating whether each
|
|
350
|
-
* element was successfully added to the beginning of the array.
|
|
374
|
+
* Prepend a sequence of elements.
|
|
375
|
+
* @remarks Time O(N), Space O(1)
|
|
376
|
+
* @param [elements] - Iterable (or iterable-like) of elements/records.
|
|
377
|
+
* @returns Array of per-element success flags.
|
|
351
378
|
*/
|
|
352
379
|
unshiftMany(elements = []) {
|
|
353
380
|
const ans = [];
|
|
@@ -362,21 +389,17 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
362
389
|
return ans;
|
|
363
390
|
}
|
|
364
391
|
/**
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
369
|
-
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
392
|
+
* Check whether the deque is empty.
|
|
393
|
+
* @remarks Time O(1), Space O(1)
|
|
394
|
+
* @returns True if length is 0.
|
|
370
395
|
*/
|
|
371
396
|
isEmpty() {
|
|
372
397
|
return this._length === 0;
|
|
373
398
|
}
|
|
374
399
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
379
|
-
* values.
|
|
400
|
+
* Remove all elements and reset structure.
|
|
401
|
+
* @remarks Time O(1), Space O(1)
|
|
402
|
+
* @returns void
|
|
380
403
|
*/
|
|
381
404
|
clear() {
|
|
382
405
|
this._buckets = [new Array(this._bucketSize)];
|
|
@@ -385,29 +408,23 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
385
408
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
386
409
|
}
|
|
387
410
|
/**
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
393
|
-
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
394
|
-
* range of the data structure.
|
|
395
|
-
* @returns The element at the specified position in the data structure is being returned.
|
|
411
|
+
* Get the element at a given position.
|
|
412
|
+
* @remarks Time O(1), Space O(1)
|
|
413
|
+
* @param pos - Zero-based position from the front.
|
|
414
|
+
* @returns Element or undefined.
|
|
396
415
|
*/
|
|
397
416
|
at(pos) {
|
|
398
|
-
|
|
417
|
+
if (pos < 0 || pos >= this._length)
|
|
418
|
+
return undefined;
|
|
399
419
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
400
420
|
return this._buckets[bucketIndex][indexInBucket];
|
|
401
421
|
}
|
|
402
422
|
/**
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* @
|
|
408
|
-
* set. It is of type `number`.
|
|
409
|
-
* @param {E} element - The `element` parameter is the value that you want to set at the specified
|
|
410
|
-
* position in the data structure.
|
|
423
|
+
* Replace the element at a given position.
|
|
424
|
+
* @remarks Time O(1), Space O(1)
|
|
425
|
+
* @param pos - Zero-based position from the front.
|
|
426
|
+
* @param element - New element value.
|
|
427
|
+
* @returns True if updated.
|
|
411
428
|
*/
|
|
412
429
|
setAt(pos, element) {
|
|
413
430
|
(0, utils_1.rangeCheck)(pos, 0, this._length - 1);
|
|
@@ -416,19 +433,12 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
416
433
|
return true;
|
|
417
434
|
}
|
|
418
435
|
/**
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
* @
|
|
425
|
-
* be inserted. It is of type `number`.
|
|
426
|
-
* @param {E} element - The `element` parameter represents the element that you want to insert into
|
|
427
|
-
* the array at the specified position.
|
|
428
|
-
* @param [num=1] - The `num` parameter represents the number of times the `element` should be
|
|
429
|
-
* inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
|
|
430
|
-
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
431
|
-
* @returns The size of the array after the insertion is being returned.
|
|
436
|
+
* Insert repeated copies of an element at a position.
|
|
437
|
+
* @remarks Time O(N), Space O(1)
|
|
438
|
+
* @param pos - Zero-based position from the front.
|
|
439
|
+
* @param element - Element to insert.
|
|
440
|
+
* @param [num] - Number of times to insert (default 1).
|
|
441
|
+
* @returns True if inserted.
|
|
432
442
|
*/
|
|
433
443
|
addAt(pos, element, num = 1) {
|
|
434
444
|
const length = this._length;
|
|
@@ -444,7 +454,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
444
454
|
else {
|
|
445
455
|
const arr = [];
|
|
446
456
|
for (let i = pos; i < this._length; ++i) {
|
|
447
|
-
|
|
457
|
+
const v = this.at(i);
|
|
458
|
+
if (v !== undefined)
|
|
459
|
+
arr.push(v);
|
|
448
460
|
}
|
|
449
461
|
this.cut(pos - 1, true);
|
|
450
462
|
for (let i = 0; i < num; ++i)
|
|
@@ -455,15 +467,11 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
455
467
|
return true;
|
|
456
468
|
}
|
|
457
469
|
/**
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
463
|
-
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
464
|
-
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
465
|
-
* @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
|
|
466
|
-
* @returns The method is returning the updated size of the data structure.
|
|
470
|
+
* Cut the deque to keep items up to index; optionally mutate in-place.
|
|
471
|
+
* @remarks Time O(N), Space O(1)
|
|
472
|
+
* @param pos - Last index to keep.
|
|
473
|
+
* @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
|
|
474
|
+
* @returns This deque if in-place; otherwise a new deque of the prefix.
|
|
467
475
|
*/
|
|
468
476
|
cut(pos, isCutSelf = false) {
|
|
469
477
|
if (isCutSelf) {
|
|
@@ -478,79 +486,56 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
478
486
|
return this;
|
|
479
487
|
}
|
|
480
488
|
else {
|
|
481
|
-
const newDeque = this._createInstance({
|
|
482
|
-
|
|
483
|
-
toElementFn: this._toElementFn,
|
|
484
|
-
maxLen: this._maxLen
|
|
485
|
-
});
|
|
489
|
+
const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
490
|
+
newDeque._setBucketSize(this._bucketSize);
|
|
486
491
|
for (let i = 0; i <= pos; i++) {
|
|
487
|
-
|
|
492
|
+
const v = this.at(i);
|
|
493
|
+
if (v !== undefined)
|
|
494
|
+
newDeque.push(v);
|
|
488
495
|
}
|
|
489
496
|
return newDeque;
|
|
490
497
|
}
|
|
491
498
|
}
|
|
492
499
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
* @
|
|
499
|
-
* to start changing the array. Items will be removed or added starting from this index.
|
|
500
|
-
* @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
|
|
501
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
502
|
-
* `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
|
|
503
|
-
* end of the array (`
|
|
504
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
505
|
-
* will be inserted into the deque at the specified `start` index. These elements will be inserted in
|
|
506
|
-
* place of the elements that are removed based on the `start` and `deleteCount` parameters.
|
|
507
|
-
* @returns The `splice` method is returning the array `deletedElements` which contains the elements
|
|
508
|
-
* that were removed from the Deque during the splice operation.
|
|
500
|
+
* Remove and/or insert elements at a position (array-like behavior).
|
|
501
|
+
* @remarks Time O(N + M), Space O(M)
|
|
502
|
+
* @param start - Start index (clamped to [0, length]).
|
|
503
|
+
* @param [deleteCount] - Number of elements to remove (default: length - start).
|
|
504
|
+
* @param [items] - Elements to insert after `start`.
|
|
505
|
+
* @returns A new deque containing the removed elements (typed as `this`).
|
|
509
506
|
*/
|
|
510
507
|
splice(start, deleteCount = this._length - start, ...items) {
|
|
511
|
-
// Check whether the starting position is legal
|
|
512
508
|
(0, utils_1.rangeCheck)(start, 0, this._length);
|
|
513
|
-
// Adjust the value of deleteCount
|
|
514
509
|
if (deleteCount < 0)
|
|
515
510
|
deleteCount = 0;
|
|
516
511
|
if (start + deleteCount > this._length)
|
|
517
512
|
deleteCount = this._length - start;
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
// Add removed elements to the result
|
|
513
|
+
const removed = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
514
|
+
removed._setBucketSize(this._bucketSize);
|
|
521
515
|
for (let i = 0; i < deleteCount; i++) {
|
|
522
|
-
|
|
516
|
+
const v = this.at(start + i);
|
|
517
|
+
if (v !== undefined)
|
|
518
|
+
removed.push(v);
|
|
523
519
|
}
|
|
524
|
-
|
|
525
|
-
const elementsAfter = [];
|
|
520
|
+
const tail = [];
|
|
526
521
|
for (let i = start + deleteCount; i < this._length; i++) {
|
|
527
|
-
|
|
522
|
+
const v = this.at(i);
|
|
523
|
+
if (v !== undefined)
|
|
524
|
+
tail.push(v);
|
|
528
525
|
}
|
|
529
|
-
// Adjust the length of the current Deque
|
|
530
526
|
this.cut(start - 1, true);
|
|
531
|
-
for (const
|
|
532
|
-
this.push(
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
544
|
-
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new
|
|
545
|
-
* deque with the cut elements.
|
|
546
|
-
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
|
|
547
|
-
* is a number that indicates the index of the element in the Deque where the cut should start.
|
|
548
|
-
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
|
|
549
|
-
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
|
|
550
|
-
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
|
|
551
|
-
* is false, a new De
|
|
552
|
-
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
|
|
553
|
-
* (`newDeque`) depending on the value of the `isCutSelf` parameter.
|
|
527
|
+
for (const it of items)
|
|
528
|
+
this.push(it);
|
|
529
|
+
for (const v of tail)
|
|
530
|
+
this.push(v);
|
|
531
|
+
return removed;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Cut the deque to keep items from index onward; optionally mutate in-place.
|
|
535
|
+
* @remarks Time O(N), Space O(1)
|
|
536
|
+
* @param pos - First index to keep.
|
|
537
|
+
* @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
|
|
538
|
+
* @returns This deque if in-place; otherwise a new deque of the suffix.
|
|
554
539
|
*/
|
|
555
540
|
cutRest(pos, isCutSelf = false) {
|
|
556
541
|
if (isCutSelf) {
|
|
@@ -564,45 +549,36 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
564
549
|
return this;
|
|
565
550
|
}
|
|
566
551
|
else {
|
|
567
|
-
const newDeque = this._createInstance({
|
|
568
|
-
|
|
569
|
-
toElementFn: this._toElementFn,
|
|
570
|
-
maxLen: this._maxLen
|
|
571
|
-
});
|
|
552
|
+
const newDeque = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
553
|
+
newDeque._setBucketSize(this._bucketSize);
|
|
572
554
|
if (pos < 0)
|
|
573
555
|
pos = 0;
|
|
574
556
|
for (let i = pos; i < this._length; i++) {
|
|
575
|
-
|
|
557
|
+
const v = this.at(i);
|
|
558
|
+
if (v !== undefined)
|
|
559
|
+
newDeque.push(v);
|
|
576
560
|
}
|
|
577
561
|
return newDeque;
|
|
578
562
|
}
|
|
579
563
|
}
|
|
580
564
|
/**
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
* structure.
|
|
586
|
-
* @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
|
|
587
|
-
* which an element needs to be deleted from the data structure. It is of type `number` and indicates
|
|
588
|
-
* the index of the element to be deleted.
|
|
589
|
-
* @returns The size of the data structure after the deletion operation is performed.
|
|
565
|
+
* Delete the element at a given position.
|
|
566
|
+
* @remarks Time O(N), Space O(1)
|
|
567
|
+
* @param pos - Zero-based position from the front.
|
|
568
|
+
* @returns Removed element or undefined.
|
|
590
569
|
*/
|
|
591
570
|
deleteAt(pos) {
|
|
592
571
|
(0, utils_1.rangeCheck)(pos, 0, this._length - 1);
|
|
593
572
|
let deleted;
|
|
594
573
|
if (pos === 0) {
|
|
595
|
-
//If it is the first element, use shift() directly
|
|
596
574
|
return this.shift();
|
|
597
575
|
}
|
|
598
576
|
else if (pos === this._length - 1) {
|
|
599
|
-
// If it is the last element, just use pop()
|
|
600
577
|
deleted = this.last;
|
|
601
578
|
this.pop();
|
|
602
579
|
return deleted;
|
|
603
580
|
}
|
|
604
581
|
else {
|
|
605
|
-
// Delete the middle element
|
|
606
582
|
const length = this._length - 1;
|
|
607
583
|
const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
|
|
608
584
|
deleted = this._buckets[targetBucket][targetPointer];
|
|
@@ -611,20 +587,15 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
611
587
|
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
|
|
612
588
|
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
613
589
|
}
|
|
614
|
-
// Remove last duplicate element
|
|
615
590
|
this.pop();
|
|
616
591
|
return deleted;
|
|
617
592
|
}
|
|
618
593
|
}
|
|
619
594
|
/**
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
* structure.
|
|
625
|
-
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
626
|
-
* the data structure.
|
|
627
|
-
* @returns The size of the data structure after the element has been deleted.
|
|
595
|
+
* Delete the first occurrence of a value.
|
|
596
|
+
* @remarks Time O(N), Space O(1)
|
|
597
|
+
* @param element - Element to remove (using the configured equality).
|
|
598
|
+
* @returns True if an element was removed.
|
|
628
599
|
*/
|
|
629
600
|
delete(element) {
|
|
630
601
|
const size = this._length;
|
|
@@ -634,7 +605,7 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
634
605
|
let index = 0;
|
|
635
606
|
while (i < size) {
|
|
636
607
|
const oldElement = this.at(i);
|
|
637
|
-
if (oldElement
|
|
608
|
+
if (!this._equals(oldElement, element)) {
|
|
638
609
|
this.setAt(index, oldElement);
|
|
639
610
|
index += 1;
|
|
640
611
|
}
|
|
@@ -643,50 +614,36 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
643
614
|
this.cut(index - 1, true);
|
|
644
615
|
return true;
|
|
645
616
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
// if (bucketIndex >= this._bucketCount) {
|
|
677
|
-
// bucketIndex = 0;
|
|
678
|
-
// }
|
|
679
|
-
// }
|
|
680
|
-
// return -1;
|
|
681
|
-
// }
|
|
682
|
-
/**
|
|
683
|
-
* Time Complexity: O(n)
|
|
684
|
-
* Space Complexity: O(1)
|
|
685
|
-
*
|
|
686
|
-
* The reverse() function reverses the order of the buckets and the elements within each bucket in a
|
|
687
|
-
* data structure.
|
|
688
|
-
* @returns The reverse() method is returning the object itself (this) after performing the reverse
|
|
689
|
-
* operation on the buckets and updating the relevant properties.
|
|
617
|
+
/**
|
|
618
|
+
* Delete the first element matching a predicate.
|
|
619
|
+
* @remarks Time O(N), Space O(1)
|
|
620
|
+
* @param predicate - Function (value, index, deque) → boolean.
|
|
621
|
+
* @returns True if a match was removed.
|
|
622
|
+
*/
|
|
623
|
+
deleteWhere(predicate) {
|
|
624
|
+
for (let i = 0; i < this._length; i++) {
|
|
625
|
+
const v = this.at(i);
|
|
626
|
+
if (predicate(v, i, this)) {
|
|
627
|
+
this.deleteAt(i);
|
|
628
|
+
return true;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
/**
|
|
634
|
+
* Set the equality comparator used by delete operations.
|
|
635
|
+
* @remarks Time O(1), Space O(1)
|
|
636
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
637
|
+
* @returns This deque.
|
|
638
|
+
*/
|
|
639
|
+
setEquality(equals) {
|
|
640
|
+
this._equals = equals;
|
|
641
|
+
return this;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Reverse the deque by reversing buckets and pointers.
|
|
645
|
+
* @remarks Time O(N), Space O(N)
|
|
646
|
+
* @returns This deque.
|
|
690
647
|
*/
|
|
691
648
|
reverse() {
|
|
692
649
|
this._buckets.reverse().forEach(function (bucket) {
|
|
@@ -700,12 +657,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
700
657
|
return this;
|
|
701
658
|
}
|
|
702
659
|
/**
|
|
703
|
-
*
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
* The `unique()` function removes duplicate elements from an array-like data structure and returns
|
|
707
|
-
* the number of unique elements.
|
|
708
|
-
* @returns The size of the modified array is being returned.
|
|
660
|
+
* Deduplicate consecutive equal elements in-place.
|
|
661
|
+
* @remarks Time O(N), Space O(1)
|
|
662
|
+
* @returns This deque.
|
|
709
663
|
*/
|
|
710
664
|
unique() {
|
|
711
665
|
if (this._length <= 1) {
|
|
@@ -715,7 +669,7 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
715
669
|
let prev = this.at(0);
|
|
716
670
|
for (let i = 1; i < this._length; ++i) {
|
|
717
671
|
const cur = this.at(i);
|
|
718
|
-
if (cur
|
|
672
|
+
if (!this._equals(cur, prev)) {
|
|
719
673
|
prev = cur;
|
|
720
674
|
this.setAt(index++, cur);
|
|
721
675
|
}
|
|
@@ -724,13 +678,9 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
724
678
|
return this;
|
|
725
679
|
}
|
|
726
680
|
/**
|
|
727
|
-
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
731
|
-
* memory usage.
|
|
732
|
-
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
733
|
-
* `this._length` is 0, but it does not return any value.
|
|
681
|
+
* Trim unused buckets to fit exactly the active range.
|
|
682
|
+
* @remarks Time O(N), Space O(1)
|
|
683
|
+
* @returns void
|
|
734
684
|
*/
|
|
735
685
|
shrinkToFit() {
|
|
736
686
|
if (this._length === 0)
|
|
@@ -756,99 +706,98 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
756
706
|
this._buckets = newBuckets;
|
|
757
707
|
}
|
|
758
708
|
/**
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
|
|
763
|
-
* bucket size as the original instance.
|
|
764
|
-
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
765
|
-
* elements as the original deque (`this`) and the same bucket size.
|
|
709
|
+
* Deep clone this deque, preserving options.
|
|
710
|
+
* @remarks Time O(N), Space O(N)
|
|
711
|
+
* @returns A new deque with the same content and options.
|
|
766
712
|
*/
|
|
767
713
|
clone() {
|
|
768
|
-
return
|
|
714
|
+
return this._createLike(this, {
|
|
769
715
|
bucketSize: this.bucketSize,
|
|
770
716
|
toElementFn: this.toElementFn,
|
|
771
717
|
maxLen: this._maxLen
|
|
772
718
|
});
|
|
773
719
|
}
|
|
774
720
|
/**
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
*
|
|
780
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
781
|
-
* the current element being iterated over, the index of the current element, and the deque itself.
|
|
782
|
-
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
783
|
-
* deque or not.
|
|
784
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
785
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
786
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
787
|
-
* @returns The `filter` method is returning a new `Deque` object that contains the elements that
|
|
788
|
-
* satisfy the given predicate function.
|
|
721
|
+
* Filter elements into a new deque of the same class.
|
|
722
|
+
* @remarks Time O(N), Space O(N)
|
|
723
|
+
* @param predicate - Predicate (value, index, deque) → boolean to keep element.
|
|
724
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
725
|
+
* @returns A new deque with kept elements.
|
|
789
726
|
*/
|
|
790
727
|
filter(predicate, thisArg) {
|
|
791
|
-
const
|
|
792
|
-
|
|
793
|
-
toElementFn: this.toElementFn,
|
|
794
|
-
maxLen: this._maxLen
|
|
795
|
-
});
|
|
728
|
+
const out = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
729
|
+
out._setBucketSize(this._bucketSize);
|
|
796
730
|
let index = 0;
|
|
797
731
|
for (const el of this) {
|
|
798
|
-
if (predicate.call(thisArg, el, index, this))
|
|
799
|
-
|
|
800
|
-
}
|
|
732
|
+
if (predicate.call(thisArg, el, index, this))
|
|
733
|
+
out.push(el);
|
|
801
734
|
index++;
|
|
802
735
|
}
|
|
803
|
-
return
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
*
|
|
807
|
-
*
|
|
808
|
-
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
736
|
+
return out;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Map elements into a new deque of the same element type.
|
|
740
|
+
* @remarks Time O(N), Space O(N)
|
|
741
|
+
* @param callback - Mapping function (value, index, deque) → newValue.
|
|
742
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
743
|
+
* @returns A new deque with mapped values.
|
|
744
|
+
*/
|
|
745
|
+
mapSame(callback, thisArg) {
|
|
746
|
+
const out = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
747
|
+
out._setBucketSize(this._bucketSize);
|
|
748
|
+
let index = 0;
|
|
749
|
+
for (const v of this) {
|
|
750
|
+
const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
751
|
+
out.push(mv);
|
|
752
|
+
}
|
|
753
|
+
return out;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Map elements into a new deque (possibly different element type).
|
|
757
|
+
* @remarks Time O(N), Space O(N)
|
|
758
|
+
* @template EM
|
|
759
|
+
* @template RM
|
|
760
|
+
* @param callback - Mapping function (value, index, deque) → newElement.
|
|
761
|
+
* @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
|
|
762
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
763
|
+
* @returns A new Deque with mapped elements.
|
|
764
|
+
*/
|
|
765
|
+
map(callback, options, thisArg) {
|
|
766
|
+
const out = this._createLike([], Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { bucketSize: this._bucketSize, maxLen: this._maxLen }));
|
|
825
767
|
let index = 0;
|
|
826
768
|
for (const el of this) {
|
|
827
|
-
|
|
769
|
+
const mv = thisArg === undefined ? callback(el, index, this) : callback.call(thisArg, el, index, this);
|
|
770
|
+
out.push(mv);
|
|
828
771
|
index++;
|
|
829
772
|
}
|
|
830
|
-
return
|
|
773
|
+
return out;
|
|
831
774
|
}
|
|
832
775
|
/**
|
|
833
|
-
*
|
|
834
|
-
*
|
|
835
|
-
*
|
|
836
|
-
*
|
|
837
|
-
|
|
776
|
+
* (Protected) Set the internal bucket size.
|
|
777
|
+
* @remarks Time O(1), Space O(1)
|
|
778
|
+
* @param size - Bucket capacity to assign.
|
|
779
|
+
* @returns void
|
|
780
|
+
*/
|
|
781
|
+
_setBucketSize(size) {
|
|
782
|
+
this._bucketSize = size;
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* (Protected) Iterate elements from front to back.
|
|
786
|
+
* @remarks Time O(N), Space O(1)
|
|
787
|
+
* @returns Iterator of elements.
|
|
838
788
|
*/
|
|
839
789
|
*_getIterator() {
|
|
840
790
|
for (let i = 0; i < this._length; ++i) {
|
|
841
|
-
|
|
791
|
+
const v = this.at(i);
|
|
792
|
+
if (v !== undefined)
|
|
793
|
+
yield v;
|
|
842
794
|
}
|
|
843
795
|
}
|
|
844
796
|
/**
|
|
845
|
-
*
|
|
846
|
-
*
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
* @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
|
|
850
|
-
* specifies the number of new buckets needed. If not provided, it will default to half of the
|
|
851
|
-
* current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
|
|
797
|
+
* (Protected) Reallocate buckets to make room near the ends.
|
|
798
|
+
* @remarks Time O(N), Space O(N)
|
|
799
|
+
* @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
|
|
800
|
+
* @returns void
|
|
852
801
|
*/
|
|
853
802
|
_reallocate(needBucketNum) {
|
|
854
803
|
const newBuckets = [];
|
|
@@ -872,13 +821,10 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
872
821
|
this._bucketCount = newBuckets.length;
|
|
873
822
|
}
|
|
874
823
|
/**
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
*
|
|
878
|
-
*
|
|
879
|
-
* @param {number} pos - The `pos` parameter represents the position within the data structure. It is
|
|
880
|
-
* a number that indicates the index or position of an element within the structure.
|
|
881
|
-
* @returns an object with two properties: "bucketIndex" and "indexInBucket".
|
|
824
|
+
* (Protected) Translate a logical position to bucket/offset.
|
|
825
|
+
* @remarks Time O(1), Space O(1)
|
|
826
|
+
* @param pos - Zero-based position.
|
|
827
|
+
* @returns An object containing bucketIndex and indexInBucket.
|
|
882
828
|
*/
|
|
883
829
|
_getBucketAndPosition(pos) {
|
|
884
830
|
let bucketIndex;
|
|
@@ -895,23 +841,38 @@ class Deque extends linear_base_1.LinearBase {
|
|
|
895
841
|
return { bucketIndex, indexInBucket };
|
|
896
842
|
}
|
|
897
843
|
/**
|
|
898
|
-
*
|
|
899
|
-
*
|
|
900
|
-
* @param [options] -
|
|
901
|
-
*
|
|
902
|
-
* configuration options when creating a new instance of the `Deque` class.
|
|
903
|
-
* @returns An instance of the `Deque` class with an empty array and the provided options, casted as
|
|
904
|
-
* `this`.
|
|
844
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
845
|
+
* @remarks Time O(1), Space O(1)
|
|
846
|
+
* @param [options] - Options forwarded to the constructor.
|
|
847
|
+
* @returns An empty like-kind deque instance.
|
|
905
848
|
*/
|
|
906
849
|
_createInstance(options) {
|
|
907
|
-
|
|
850
|
+
const Ctor = this.constructor;
|
|
851
|
+
return new Ctor([], options);
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* (Protected) Create a like-kind deque seeded by elements.
|
|
855
|
+
* @remarks Time O(N), Space O(N)
|
|
856
|
+
* @template T
|
|
857
|
+
* @template RR
|
|
858
|
+
* @param [elements] - Iterable used to seed the new deque.
|
|
859
|
+
* @param [options] - Options forwarded to the constructor.
|
|
860
|
+
* @returns A like-kind Deque instance.
|
|
861
|
+
*/
|
|
862
|
+
_createLike(elements = [], options) {
|
|
863
|
+
const Ctor = this.constructor;
|
|
864
|
+
return new Ctor(elements, options);
|
|
908
865
|
}
|
|
909
866
|
/**
|
|
910
|
-
*
|
|
867
|
+
* (Protected) Iterate elements from back to front.
|
|
868
|
+
* @remarks Time O(N), Space O(1)
|
|
869
|
+
* @returns Iterator of elements.
|
|
911
870
|
*/
|
|
912
871
|
*_getReverseIterator() {
|
|
913
872
|
for (let i = this._length - 1; i > -1; i--) {
|
|
914
|
-
|
|
873
|
+
const v = this.at(i);
|
|
874
|
+
if (v !== undefined)
|
|
875
|
+
yield v;
|
|
915
876
|
}
|
|
916
877
|
}
|
|
917
878
|
}
|