min-heap-typed 1.50.1 → 1.50.3
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-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -11,14 +11,25 @@ const utils_1 = require("../../utils");
|
|
|
11
11
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
12
12
|
*/
|
|
13
13
|
class Deque extends base_1.IterableElementBase {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
|
16
|
+
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
17
|
+
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
18
|
+
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
19
|
+
* empty deque
|
|
20
|
+
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
|
|
21
|
+
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
|
|
22
|
+
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
23
|
+
* or is not a number
|
|
24
|
+
*/
|
|
14
25
|
constructor(elements = [], options) {
|
|
15
26
|
super();
|
|
27
|
+
this._bucketSize = 1 << 12;
|
|
16
28
|
this._bucketFirst = 0;
|
|
17
29
|
this._firstInBucket = 0;
|
|
18
30
|
this._bucketLast = 0;
|
|
19
31
|
this._lastInBucket = 0;
|
|
20
32
|
this._bucketCount = 0;
|
|
21
|
-
this._bucketSize = 1 << 12;
|
|
22
33
|
this._buckets = [];
|
|
23
34
|
this._size = 0;
|
|
24
35
|
if (options) {
|
|
@@ -50,9 +61,62 @@ class Deque extends base_1.IterableElementBase {
|
|
|
50
61
|
this.push(element);
|
|
51
62
|
}
|
|
52
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* The bucketSize function returns the size of the bucket.
|
|
66
|
+
*
|
|
67
|
+
* @return The size of the bucket
|
|
68
|
+
*/
|
|
69
|
+
get bucketSize() {
|
|
70
|
+
return this._bucketSize;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The function returns the value of the protected variable `_bucketFirst`.
|
|
74
|
+
* @returns The value of the `_bucketFirst` property.
|
|
75
|
+
*/
|
|
76
|
+
get bucketFirst() {
|
|
77
|
+
return this._bucketFirst;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The function returns the value of the protected variable _firstInBucket.
|
|
81
|
+
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
82
|
+
* `number`.
|
|
83
|
+
*/
|
|
84
|
+
get firstInBucket() {
|
|
85
|
+
return this._firstInBucket;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The function returns the value of the protected variable `_bucketLast`.
|
|
89
|
+
* @returns The value of the `_bucketLast` property, which is a number.
|
|
90
|
+
*/
|
|
91
|
+
get bucketLast() {
|
|
92
|
+
return this._bucketLast;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The function returns the value of the protected variable _lastInBucket.
|
|
96
|
+
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
97
|
+
* `number`.
|
|
98
|
+
*/
|
|
99
|
+
get lastInBucket() {
|
|
100
|
+
return this._lastInBucket;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The function returns the number of buckets.
|
|
104
|
+
* @returns The number of buckets.
|
|
105
|
+
*/
|
|
106
|
+
get bucketCount() {
|
|
107
|
+
return this._bucketCount;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The buckets function returns the buckets property of the object.
|
|
111
|
+
* @return The buckets property
|
|
112
|
+
*/
|
|
53
113
|
get buckets() {
|
|
54
114
|
return this._buckets;
|
|
55
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* The size function returns the number of items in the stack.
|
|
118
|
+
* @return The number of values in the set
|
|
119
|
+
*/
|
|
56
120
|
get size() {
|
|
57
121
|
return this._size;
|
|
58
122
|
}
|
|
@@ -66,6 +130,10 @@ class Deque extends base_1.IterableElementBase {
|
|
|
66
130
|
return;
|
|
67
131
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
68
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* The last function returns the last element in the queue.
|
|
135
|
+
* @return The last element in the array
|
|
136
|
+
*/
|
|
69
137
|
get last() {
|
|
70
138
|
if (this.size === 0)
|
|
71
139
|
return;
|
|
@@ -204,13 +272,27 @@ class Deque extends base_1.IterableElementBase {
|
|
|
204
272
|
return element;
|
|
205
273
|
}
|
|
206
274
|
/**
|
|
207
|
-
* Time Complexity: O(1)
|
|
208
|
-
* Space Complexity: O(1)
|
|
275
|
+
* Time Complexity: O(1)
|
|
276
|
+
* Space Complexity: O(1)
|
|
277
|
+
*/
|
|
278
|
+
/**
|
|
279
|
+
* Time Complexity: O(1)
|
|
280
|
+
* Space Complexity: O(1)
|
|
281
|
+
*
|
|
282
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
283
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
209
284
|
*/
|
|
210
285
|
isEmpty() {
|
|
211
286
|
return this.size === 0;
|
|
212
287
|
}
|
|
213
288
|
/**
|
|
289
|
+
* Time Complexity: O(1)
|
|
290
|
+
* Space Complexity: O(1)
|
|
291
|
+
*/
|
|
292
|
+
/**
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
295
|
+
*
|
|
214
296
|
* The clear() function resets the state of the object by initializing all variables to their default
|
|
215
297
|
* values.
|
|
216
298
|
*/
|
|
@@ -226,7 +308,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
226
308
|
*begin() {
|
|
227
309
|
let index = 0;
|
|
228
310
|
while (index < this.size) {
|
|
229
|
-
yield this.
|
|
311
|
+
yield this.at(index);
|
|
230
312
|
index++;
|
|
231
313
|
}
|
|
232
314
|
}
|
|
@@ -237,7 +319,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
237
319
|
*reverseBegin() {
|
|
238
320
|
let index = this.size - 1;
|
|
239
321
|
while (index >= 0) {
|
|
240
|
-
yield this.
|
|
322
|
+
yield this.at(index);
|
|
241
323
|
index--;
|
|
242
324
|
}
|
|
243
325
|
}
|
|
@@ -249,13 +331,13 @@ class Deque extends base_1.IterableElementBase {
|
|
|
249
331
|
* Time Complexity: O(1)
|
|
250
332
|
* Space Complexity: O(1)
|
|
251
333
|
*
|
|
252
|
-
* The `
|
|
334
|
+
* The `at` function retrieves an element at a specified position in an array-like data structure.
|
|
253
335
|
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
254
336
|
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
255
337
|
* range of the data structure.
|
|
256
338
|
* @returns The element at the specified position in the data structure is being returned.
|
|
257
339
|
*/
|
|
258
|
-
|
|
340
|
+
at(pos) {
|
|
259
341
|
(0, utils_1.rangeCheck)(pos, 0, this.size - 1);
|
|
260
342
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
261
343
|
return this._buckets[bucketIndex][indexInBucket];
|
|
@@ -313,9 +395,9 @@ class Deque extends base_1.IterableElementBase {
|
|
|
313
395
|
else {
|
|
314
396
|
const arr = [];
|
|
315
397
|
for (let i = pos; i < this.size; ++i) {
|
|
316
|
-
arr.push(this.
|
|
398
|
+
arr.push(this.at(i));
|
|
317
399
|
}
|
|
318
|
-
this.cut(pos - 1);
|
|
400
|
+
this.cut(pos - 1, true);
|
|
319
401
|
for (let i = 0; i < num; ++i)
|
|
320
402
|
this.push(element);
|
|
321
403
|
for (let i = 0; i < arr.length; ++i)
|
|
@@ -335,26 +417,75 @@ class Deque extends base_1.IterableElementBase {
|
|
|
335
417
|
* updated size.
|
|
336
418
|
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
337
419
|
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
420
|
+
* @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
|
|
338
421
|
* @returns The method is returning the updated size of the data structure.
|
|
339
422
|
*/
|
|
340
|
-
cut(pos) {
|
|
341
|
-
if (
|
|
342
|
-
|
|
343
|
-
|
|
423
|
+
cut(pos, isCutSelf = false) {
|
|
424
|
+
if (isCutSelf) {
|
|
425
|
+
if (pos < 0) {
|
|
426
|
+
this.clear();
|
|
427
|
+
return this;
|
|
428
|
+
}
|
|
429
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
430
|
+
this._bucketLast = bucketIndex;
|
|
431
|
+
this._lastInBucket = indexInBucket;
|
|
432
|
+
this._size = pos + 1;
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
437
|
+
for (let i = 0; i <= pos; i++) {
|
|
438
|
+
newDeque.push(this.at(i));
|
|
439
|
+
}
|
|
440
|
+
return newDeque;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Time Complexity: O(1)
|
|
445
|
+
* Space Complexity: O(1) or O(n)
|
|
446
|
+
*/
|
|
447
|
+
/**
|
|
448
|
+
* Time Complexity: O(1)
|
|
449
|
+
* Space Complexity: O(1) or O(n)
|
|
450
|
+
*
|
|
451
|
+
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new
|
|
452
|
+
* deque with the cut elements.
|
|
453
|
+
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
|
|
454
|
+
* is a number that indicates the index of the element in the Deque where the cut should start.
|
|
455
|
+
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
|
|
456
|
+
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
|
|
457
|
+
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
|
|
458
|
+
* is false, a new De
|
|
459
|
+
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
|
|
460
|
+
* (`newDeque`) depending on the value of the `isCutSelf` parameter.
|
|
461
|
+
*/
|
|
462
|
+
cutRest(pos, isCutSelf = false) {
|
|
463
|
+
if (isCutSelf) {
|
|
464
|
+
if (pos < 0) {
|
|
465
|
+
this.clear();
|
|
466
|
+
return this;
|
|
467
|
+
}
|
|
468
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
469
|
+
this._bucketFirst = bucketIndex;
|
|
470
|
+
this._firstInBucket = indexInBucket;
|
|
471
|
+
this._size = this._size - pos;
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
const newDeque = new Deque([], { bucketSize: this._bucketSize });
|
|
476
|
+
for (let i = pos; i < this.size; i++) {
|
|
477
|
+
newDeque.push(this.at(i));
|
|
478
|
+
}
|
|
479
|
+
return newDeque;
|
|
344
480
|
}
|
|
345
|
-
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
346
|
-
this._bucketLast = bucketIndex;
|
|
347
|
-
this._lastInBucket = indexInBucket;
|
|
348
|
-
this._size = pos + 1;
|
|
349
|
-
return this.size;
|
|
350
481
|
}
|
|
351
482
|
/**
|
|
352
483
|
* Time Complexity: O(n)
|
|
353
|
-
* Space Complexity: O(1)
|
|
484
|
+
* Space Complexity: O(1) or O(n)
|
|
354
485
|
*/
|
|
355
486
|
/**
|
|
356
487
|
* Time Complexity: O(n)
|
|
357
|
-
* Space Complexity: O(1)
|
|
488
|
+
* Space Complexity: O(1) or O(n)
|
|
358
489
|
*
|
|
359
490
|
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
360
491
|
* structure.
|
|
@@ -403,14 +534,14 @@ class Deque extends base_1.IterableElementBase {
|
|
|
403
534
|
let i = 0;
|
|
404
535
|
let index = 0;
|
|
405
536
|
while (i < size) {
|
|
406
|
-
const oldElement = this.
|
|
537
|
+
const oldElement = this.at(i);
|
|
407
538
|
if (oldElement !== element) {
|
|
408
539
|
this.setAt(index, oldElement);
|
|
409
540
|
index += 1;
|
|
410
541
|
}
|
|
411
542
|
i += 1;
|
|
412
543
|
}
|
|
413
|
-
this.cut(index - 1);
|
|
544
|
+
this.cut(index - 1, true);
|
|
414
545
|
return true;
|
|
415
546
|
}
|
|
416
547
|
/**
|
|
@@ -454,15 +585,15 @@ class Deque extends base_1.IterableElementBase {
|
|
|
454
585
|
return this;
|
|
455
586
|
}
|
|
456
587
|
let index = 1;
|
|
457
|
-
let prev = this.
|
|
588
|
+
let prev = this.at(0);
|
|
458
589
|
for (let i = 1; i < this.size; ++i) {
|
|
459
|
-
const cur = this.
|
|
590
|
+
const cur = this.at(i);
|
|
460
591
|
if (cur !== prev) {
|
|
461
592
|
prev = cur;
|
|
462
593
|
this.setAt(index++, cur);
|
|
463
594
|
}
|
|
464
595
|
}
|
|
465
|
-
this.cut(index - 1);
|
|
596
|
+
this.cut(index - 1, true);
|
|
466
597
|
return this;
|
|
467
598
|
}
|
|
468
599
|
/**
|
|
@@ -482,7 +613,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
482
613
|
sort(comparator) {
|
|
483
614
|
const arr = [];
|
|
484
615
|
for (let i = 0; i < this.size; ++i) {
|
|
485
|
-
arr.push(this.
|
|
616
|
+
arr.push(this.at(i));
|
|
486
617
|
}
|
|
487
618
|
arr.sort(comparator);
|
|
488
619
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -526,30 +657,6 @@ class Deque extends base_1.IterableElementBase {
|
|
|
526
657
|
this._bucketLast = newBuckets.length - 1;
|
|
527
658
|
this._buckets = newBuckets;
|
|
528
659
|
}
|
|
529
|
-
/**
|
|
530
|
-
* Time Complexity: O(n)
|
|
531
|
-
* Space Complexity: O(1)
|
|
532
|
-
*/
|
|
533
|
-
/**
|
|
534
|
-
* Time Complexity: O(n)
|
|
535
|
-
* Space Complexity: O(1)
|
|
536
|
-
*
|
|
537
|
-
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
538
|
-
* the callback function returns true, or undefined if no such element is found.
|
|
539
|
-
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
540
|
-
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
541
|
-
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
542
|
-
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
543
|
-
*/
|
|
544
|
-
find(callback) {
|
|
545
|
-
for (let i = 0; i < this.size; ++i) {
|
|
546
|
-
const element = this.getAt(i);
|
|
547
|
-
if (callback(element, i, this)) {
|
|
548
|
-
return element;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
660
|
/**
|
|
554
661
|
* Time Complexity: O(n)
|
|
555
662
|
* Space Complexity: O(1)
|
|
@@ -567,7 +674,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
567
674
|
*/
|
|
568
675
|
indexOf(element) {
|
|
569
676
|
for (let i = 0; i < this.size; ++i) {
|
|
570
|
-
if (this.
|
|
677
|
+
if (this.at(i) === element) {
|
|
571
678
|
return i;
|
|
572
679
|
}
|
|
573
680
|
}
|
|
@@ -587,6 +694,22 @@ class Deque extends base_1.IterableElementBase {
|
|
|
587
694
|
toArray() {
|
|
588
695
|
return [...this];
|
|
589
696
|
}
|
|
697
|
+
/**
|
|
698
|
+
* Time Complexity: O(n)
|
|
699
|
+
* Space Complexity: O(n)
|
|
700
|
+
*/
|
|
701
|
+
/**
|
|
702
|
+
* Time Complexity: O(n)
|
|
703
|
+
* Space Complexity: O(n)
|
|
704
|
+
*
|
|
705
|
+
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
|
|
706
|
+
* bucket size as the original instance.
|
|
707
|
+
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
708
|
+
* elements as the original deque (`this`) and the same bucket size.
|
|
709
|
+
*/
|
|
710
|
+
clone() {
|
|
711
|
+
return new Deque([...this], { bucketSize: this.bucketSize });
|
|
712
|
+
}
|
|
590
713
|
/**
|
|
591
714
|
* Time Complexity: O(n)
|
|
592
715
|
* Space Complexity: O(n)
|
|
@@ -649,8 +772,8 @@ class Deque extends base_1.IterableElementBase {
|
|
|
649
772
|
* Space Complexity: O(n) - Due to potential resizing.
|
|
650
773
|
*/
|
|
651
774
|
/**
|
|
652
|
-
* Time Complexity: O(1)
|
|
653
|
-
* Space Complexity: O(n) -
|
|
775
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
776
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
654
777
|
*
|
|
655
778
|
* The addLast function adds an element to the end of an array.
|
|
656
779
|
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
@@ -660,12 +783,12 @@ class Deque extends base_1.IterableElementBase {
|
|
|
660
783
|
return this.push(element);
|
|
661
784
|
}
|
|
662
785
|
/**
|
|
663
|
-
* Time Complexity: O(1)
|
|
664
|
-
* Space Complexity: O(1)
|
|
786
|
+
* Time Complexity: O(1)
|
|
787
|
+
* Space Complexity: O(1)
|
|
665
788
|
*/
|
|
666
789
|
/**
|
|
667
|
-
* Time Complexity: O(1)
|
|
668
|
-
* Space Complexity: O(1)
|
|
790
|
+
* Time Complexity: O(1)
|
|
791
|
+
* Space Complexity: O(1)
|
|
669
792
|
*
|
|
670
793
|
* The function "pollLast" removes and returns the last element of an array.
|
|
671
794
|
* @returns The last element of the array is being returned.
|
|
@@ -674,8 +797,13 @@ class Deque extends base_1.IterableElementBase {
|
|
|
674
797
|
return this.pop();
|
|
675
798
|
}
|
|
676
799
|
/**
|
|
677
|
-
* Time Complexity: O(1)
|
|
678
|
-
* Space Complexity: O(
|
|
800
|
+
* Time Complexity: O(1)
|
|
801
|
+
* Space Complexity: O(1)
|
|
802
|
+
* /
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* Time Complexity: O(1)
|
|
806
|
+
* Space Complexity: O(1)
|
|
679
807
|
*
|
|
680
808
|
* The "addFirst" function adds an element to the beginning of an array.
|
|
681
809
|
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
@@ -685,8 +813,13 @@ class Deque extends base_1.IterableElementBase {
|
|
|
685
813
|
return this.unshift(element);
|
|
686
814
|
}
|
|
687
815
|
/**
|
|
688
|
-
* Time Complexity: O(1)
|
|
689
|
-
* Space Complexity: O(1)
|
|
816
|
+
* Time Complexity: O(1)
|
|
817
|
+
* Space Complexity: O(1)
|
|
818
|
+
* /
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* Time Complexity: O(1)
|
|
822
|
+
* Space Complexity: O(1)
|
|
690
823
|
*
|
|
691
824
|
* The function "pollFirst" removes and returns the first element of an array.
|
|
692
825
|
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
@@ -696,6 +829,11 @@ class Deque extends base_1.IterableElementBase {
|
|
|
696
829
|
return this.shift();
|
|
697
830
|
}
|
|
698
831
|
/**
|
|
832
|
+
* Time Complexity: O(n)
|
|
833
|
+
* Space Complexity: O(1)
|
|
834
|
+
* /
|
|
835
|
+
|
|
836
|
+
/**
|
|
699
837
|
* Time Complexity: O(n)
|
|
700
838
|
* Space Complexity: O(1)
|
|
701
839
|
*
|
|
@@ -704,7 +842,7 @@ class Deque extends base_1.IterableElementBase {
|
|
|
704
842
|
*/
|
|
705
843
|
*_getIterator() {
|
|
706
844
|
for (let i = 0; i < this.size; ++i) {
|
|
707
|
-
yield this.
|
|
845
|
+
yield this.at(i);
|
|
708
846
|
}
|
|
709
847
|
}
|
|
710
848
|
/**
|