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
|
@@ -17,13 +17,17 @@ import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|
|
17
17
|
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
|
18
18
|
*/
|
|
19
19
|
export class Deque<E> extends IterableElementBase<E> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes a Deque object with an optional iterable of elements and options.
|
|
22
|
+
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
23
|
+
* elements to be added to the deque. It can also be an object with a `length` or `size` property
|
|
24
|
+
* that represents the number of elements in the iterable object. If no elements are provided, an
|
|
25
|
+
* empty deque
|
|
26
|
+
* @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
|
|
27
|
+
* configuration options for the deque. In this code, it is used to set the `bucketSize` option,
|
|
28
|
+
* which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
|
|
29
|
+
* or is not a number
|
|
30
|
+
*/
|
|
27
31
|
constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) {
|
|
28
32
|
super();
|
|
29
33
|
|
|
@@ -54,14 +58,85 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
54
58
|
}
|
|
55
59
|
}
|
|
56
60
|
|
|
61
|
+
protected _bucketSize: number = 1 << 12;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The bucketSize function returns the size of the bucket.
|
|
65
|
+
*
|
|
66
|
+
* @return The size of the bucket
|
|
67
|
+
*/
|
|
68
|
+
get bucketSize() {
|
|
69
|
+
return this._bucketSize;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected _bucketFirst = 0;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The function returns the value of the protected variable `_bucketFirst`.
|
|
76
|
+
* @returns The value of the `_bucketFirst` property.
|
|
77
|
+
*/
|
|
78
|
+
get bucketFirst(): number {
|
|
79
|
+
return this._bucketFirst;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
protected _firstInBucket = 0;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The function returns the value of the protected variable _firstInBucket.
|
|
86
|
+
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
87
|
+
* `number`.
|
|
88
|
+
*/
|
|
89
|
+
get firstInBucket(): number {
|
|
90
|
+
return this._firstInBucket;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected _bucketLast = 0;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The function returns the value of the protected variable `_bucketLast`.
|
|
97
|
+
* @returns The value of the `_bucketLast` property, which is a number.
|
|
98
|
+
*/
|
|
99
|
+
get bucketLast(): number {
|
|
100
|
+
return this._bucketLast;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected _lastInBucket = 0;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The function returns the value of the protected variable _lastInBucket.
|
|
107
|
+
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
108
|
+
* `number`.
|
|
109
|
+
*/
|
|
110
|
+
get lastInBucket(): number {
|
|
111
|
+
return this._lastInBucket;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected _bucketCount = 0;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The function returns the number of buckets.
|
|
118
|
+
* @returns The number of buckets.
|
|
119
|
+
*/
|
|
120
|
+
get bucketCount(): number {
|
|
121
|
+
return this._bucketCount;
|
|
122
|
+
}
|
|
123
|
+
|
|
57
124
|
protected _buckets: E[][] = [];
|
|
58
125
|
|
|
126
|
+
/**
|
|
127
|
+
* The buckets function returns the buckets property of the object.
|
|
128
|
+
* @return The buckets property
|
|
129
|
+
*/
|
|
59
130
|
get buckets() {
|
|
60
131
|
return this._buckets;
|
|
61
132
|
}
|
|
62
133
|
|
|
63
134
|
protected _size = 0;
|
|
64
135
|
|
|
136
|
+
/**
|
|
137
|
+
* The size function returns the number of items in the stack.
|
|
138
|
+
* @return The number of values in the set
|
|
139
|
+
*/
|
|
65
140
|
get size() {
|
|
66
141
|
return this._size;
|
|
67
142
|
}
|
|
@@ -76,6 +151,10 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
76
151
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
77
152
|
}
|
|
78
153
|
|
|
154
|
+
/**
|
|
155
|
+
* The last function returns the last element in the queue.
|
|
156
|
+
* @return The last element in the array
|
|
157
|
+
*/
|
|
79
158
|
get last(): E | undefined {
|
|
80
159
|
if (this.size === 0) return;
|
|
81
160
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
@@ -210,15 +289,30 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
210
289
|
}
|
|
211
290
|
|
|
212
291
|
/**
|
|
213
|
-
* Time Complexity: O(1)
|
|
214
|
-
* Space Complexity: O(1)
|
|
292
|
+
* Time Complexity: O(1)
|
|
293
|
+
* Space Complexity: O(1)
|
|
215
294
|
*/
|
|
216
295
|
|
|
296
|
+
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
300
|
+
* The function checks if the size of an object is equal to zero and returns a boolean value.
|
|
301
|
+
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
302
|
+
*/
|
|
217
303
|
isEmpty(): boolean {
|
|
218
304
|
return this.size === 0;
|
|
219
305
|
}
|
|
220
306
|
|
|
221
307
|
/**
|
|
308
|
+
* Time Complexity: O(1)
|
|
309
|
+
* Space Complexity: O(1)
|
|
310
|
+
*/
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Time Complexity: O(1)
|
|
314
|
+
* Space Complexity: O(1)
|
|
315
|
+
*
|
|
222
316
|
* The clear() function resets the state of the object by initializing all variables to their default
|
|
223
317
|
* values.
|
|
224
318
|
*/
|
|
@@ -235,7 +329,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
235
329
|
* begin(): Generator<E> {
|
|
236
330
|
let index = 0;
|
|
237
331
|
while (index < this.size) {
|
|
238
|
-
yield this.
|
|
332
|
+
yield this.at(index);
|
|
239
333
|
index++;
|
|
240
334
|
}
|
|
241
335
|
}
|
|
@@ -247,7 +341,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
247
341
|
* reverseBegin(): Generator<E> {
|
|
248
342
|
let index = this.size - 1;
|
|
249
343
|
while (index >= 0) {
|
|
250
|
-
yield this.
|
|
344
|
+
yield this.at(index);
|
|
251
345
|
index--;
|
|
252
346
|
}
|
|
253
347
|
}
|
|
@@ -261,13 +355,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
261
355
|
* Time Complexity: O(1)
|
|
262
356
|
* Space Complexity: O(1)
|
|
263
357
|
*
|
|
264
|
-
* The `
|
|
358
|
+
* The `at` function retrieves an element at a specified position in an array-like data structure.
|
|
265
359
|
* @param {number} pos - The `pos` parameter represents the position of the element that you want to
|
|
266
360
|
* retrieve from the data structure. It is of type `number` and should be a valid index within the
|
|
267
361
|
* range of the data structure.
|
|
268
362
|
* @returns The element at the specified position in the data structure is being returned.
|
|
269
363
|
*/
|
|
270
|
-
|
|
364
|
+
at(pos: number): E {
|
|
271
365
|
rangeCheck(pos, 0, this.size - 1);
|
|
272
366
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
273
367
|
return this._buckets[bucketIndex][indexInBucket]!;
|
|
@@ -325,9 +419,9 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
325
419
|
} else {
|
|
326
420
|
const arr: E[] = [];
|
|
327
421
|
for (let i = pos; i < this.size; ++i) {
|
|
328
|
-
arr.push(this.
|
|
422
|
+
arr.push(this.at(i));
|
|
329
423
|
}
|
|
330
|
-
this.cut(pos - 1);
|
|
424
|
+
this.cut(pos - 1, true);
|
|
331
425
|
for (let i = 0; i < num; ++i) this.push(element);
|
|
332
426
|
for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
|
|
333
427
|
}
|
|
@@ -347,28 +441,81 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
347
441
|
* updated size.
|
|
348
442
|
* @param {number} pos - The `pos` parameter represents the position at which the string should be
|
|
349
443
|
* cut. It is a number that indicates the index of the character where the cut should be made.
|
|
444
|
+
* @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
|
|
350
445
|
* @returns The method is returning the updated size of the data structure.
|
|
351
446
|
*/
|
|
352
|
-
cut(pos: number):
|
|
353
|
-
if (
|
|
354
|
-
|
|
355
|
-
|
|
447
|
+
cut(pos: number, isCutSelf = false): Deque<E> {
|
|
448
|
+
if (isCutSelf) {
|
|
449
|
+
if (pos < 0) {
|
|
450
|
+
this.clear();
|
|
451
|
+
return this;
|
|
452
|
+
}
|
|
453
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
454
|
+
this._bucketLast = bucketIndex;
|
|
455
|
+
this._lastInBucket = indexInBucket;
|
|
456
|
+
this._size = pos + 1;
|
|
457
|
+
return this;
|
|
458
|
+
} else {
|
|
459
|
+
const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
|
|
460
|
+
|
|
461
|
+
for (let i = 0; i <= pos; i++) {
|
|
462
|
+
newDeque.push(this.at(i));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return newDeque;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Time Complexity: O(1)
|
|
471
|
+
* Space Complexity: O(1) or O(n)
|
|
472
|
+
*/
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Time Complexity: O(1)
|
|
476
|
+
* Space Complexity: O(1) or O(n)
|
|
477
|
+
*
|
|
478
|
+
* The `cutRest` function cuts the elements from a specified position in a deque and returns a new
|
|
479
|
+
* deque with the cut elements.
|
|
480
|
+
* @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
|
|
481
|
+
* is a number that indicates the index of the element in the Deque where the cut should start.
|
|
482
|
+
* @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
|
|
483
|
+
* Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
|
|
484
|
+
* Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
|
|
485
|
+
* is false, a new De
|
|
486
|
+
* @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
|
|
487
|
+
* (`newDeque`) depending on the value of the `isCutSelf` parameter.
|
|
488
|
+
*/
|
|
489
|
+
cutRest(pos: number, isCutSelf = false): Deque<E> {
|
|
490
|
+
if (isCutSelf) {
|
|
491
|
+
if (pos < 0) {
|
|
492
|
+
this.clear();
|
|
493
|
+
return this;
|
|
494
|
+
}
|
|
495
|
+
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
496
|
+
this._bucketFirst = bucketIndex;
|
|
497
|
+
this._firstInBucket = indexInBucket;
|
|
498
|
+
this._size = this._size - pos;
|
|
499
|
+
return this;
|
|
500
|
+
} else {
|
|
501
|
+
const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
|
|
502
|
+
|
|
503
|
+
for (let i = pos; i < this.size; i++) {
|
|
504
|
+
newDeque.push(this.at(i));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return newDeque;
|
|
356
508
|
}
|
|
357
|
-
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
358
|
-
this._bucketLast = bucketIndex;
|
|
359
|
-
this._lastInBucket = indexInBucket;
|
|
360
|
-
this._size = pos + 1;
|
|
361
|
-
return this.size;
|
|
362
509
|
}
|
|
363
510
|
|
|
364
511
|
/**
|
|
365
512
|
* Time Complexity: O(n)
|
|
366
|
-
* Space Complexity: O(1)
|
|
513
|
+
* Space Complexity: O(1) or O(n)
|
|
367
514
|
*/
|
|
368
515
|
|
|
369
516
|
/**
|
|
370
517
|
* Time Complexity: O(n)
|
|
371
|
-
* Space Complexity: O(1)
|
|
518
|
+
* Space Complexity: O(1) or O(n)
|
|
372
519
|
*
|
|
373
520
|
* The `deleteAt` function removes an element at a specified position in an array-like data
|
|
374
521
|
* structure.
|
|
@@ -416,14 +563,14 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
416
563
|
let i = 0;
|
|
417
564
|
let index = 0;
|
|
418
565
|
while (i < size) {
|
|
419
|
-
const oldElement = this.
|
|
566
|
+
const oldElement = this.at(i);
|
|
420
567
|
if (oldElement !== element) {
|
|
421
568
|
this.setAt(index, oldElement!);
|
|
422
569
|
index += 1;
|
|
423
570
|
}
|
|
424
571
|
i += 1;
|
|
425
572
|
}
|
|
426
|
-
this.cut(index - 1);
|
|
573
|
+
this.cut(index - 1, true);
|
|
427
574
|
return true;
|
|
428
575
|
}
|
|
429
576
|
|
|
@@ -471,15 +618,15 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
471
618
|
return this;
|
|
472
619
|
}
|
|
473
620
|
let index = 1;
|
|
474
|
-
let prev = this.
|
|
621
|
+
let prev = this.at(0);
|
|
475
622
|
for (let i = 1; i < this.size; ++i) {
|
|
476
|
-
const cur = this.
|
|
623
|
+
const cur = this.at(i);
|
|
477
624
|
if (cur !== prev) {
|
|
478
625
|
prev = cur;
|
|
479
626
|
this.setAt(index++, cur);
|
|
480
627
|
}
|
|
481
628
|
}
|
|
482
|
-
this.cut(index - 1);
|
|
629
|
+
this.cut(index - 1, true);
|
|
483
630
|
return this;
|
|
484
631
|
}
|
|
485
632
|
|
|
@@ -501,7 +648,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
501
648
|
sort(comparator?: (x: E, y: E) => number): this {
|
|
502
649
|
const arr: E[] = [];
|
|
503
650
|
for (let i = 0; i < this.size; ++i) {
|
|
504
|
-
arr.push(this.
|
|
651
|
+
arr.push(this.at(i));
|
|
505
652
|
}
|
|
506
653
|
arr.sort(comparator);
|
|
507
654
|
for (let i = 0; i < this.size; ++i) {
|
|
@@ -545,32 +692,6 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
545
692
|
this._buckets = newBuckets;
|
|
546
693
|
}
|
|
547
694
|
|
|
548
|
-
/**
|
|
549
|
-
* Time Complexity: O(n)
|
|
550
|
-
* Space Complexity: O(1)
|
|
551
|
-
*/
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Time Complexity: O(n)
|
|
555
|
-
* Space Complexity: O(1)
|
|
556
|
-
*
|
|
557
|
-
* The `find` function iterates over the elements in a deque and returns the first element for which
|
|
558
|
-
* the callback function returns true, or undefined if no such element is found.
|
|
559
|
-
* @param callback - A function that takes three parameters: element, index, and deque. It should
|
|
560
|
-
* return a boolean value indicating whether the element satisfies a certain condition.
|
|
561
|
-
* @returns The method `find` returns the first element in the deque that satisfies the condition
|
|
562
|
-
* specified by the callback function. If no element satisfies the condition, it returns `undefined`.
|
|
563
|
-
*/
|
|
564
|
-
find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
|
|
565
|
-
for (let i = 0; i < this.size; ++i) {
|
|
566
|
-
const element = this.getAt(i);
|
|
567
|
-
if (callback(element, i, this)) {
|
|
568
|
-
return element;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
695
|
/**
|
|
575
696
|
* Time Complexity: O(n)
|
|
576
697
|
* Space Complexity: O(1)
|
|
@@ -589,7 +710,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
589
710
|
*/
|
|
590
711
|
indexOf(element: E): number {
|
|
591
712
|
for (let i = 0; i < this.size; ++i) {
|
|
592
|
-
if (this.
|
|
713
|
+
if (this.at(i) === element) {
|
|
593
714
|
return i;
|
|
594
715
|
}
|
|
595
716
|
}
|
|
@@ -616,6 +737,25 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
616
737
|
* Time Complexity: O(n)
|
|
617
738
|
* Space Complexity: O(n)
|
|
618
739
|
*/
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Time Complexity: O(n)
|
|
743
|
+
* Space Complexity: O(n)
|
|
744
|
+
*
|
|
745
|
+
* The `clone()` function returns a new instance of the `Deque` class with the same elements and
|
|
746
|
+
* bucket size as the original instance.
|
|
747
|
+
* @returns The `clone()` method is returning a new instance of the `Deque` class with the same
|
|
748
|
+
* elements as the original deque (`this`) and the same bucket size.
|
|
749
|
+
*/
|
|
750
|
+
clone(): Deque<E> {
|
|
751
|
+
return new Deque<E>([...this], { bucketSize: this.bucketSize });
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Time Complexity: O(n)
|
|
756
|
+
* Space Complexity: O(n)
|
|
757
|
+
*/
|
|
758
|
+
|
|
619
759
|
/**
|
|
620
760
|
* Time Complexity: O(n)
|
|
621
761
|
* Space Complexity: O(n)
|
|
@@ -677,8 +817,8 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
677
817
|
*/
|
|
678
818
|
|
|
679
819
|
/**
|
|
680
|
-
* Time Complexity: O(1)
|
|
681
|
-
* Space Complexity: O(n) -
|
|
820
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
821
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
682
822
|
*
|
|
683
823
|
* The addLast function adds an element to the end of an array.
|
|
684
824
|
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
@@ -689,13 +829,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
689
829
|
}
|
|
690
830
|
|
|
691
831
|
/**
|
|
692
|
-
* Time Complexity: O(1)
|
|
693
|
-
* Space Complexity: O(1)
|
|
832
|
+
* Time Complexity: O(1)
|
|
833
|
+
* Space Complexity: O(1)
|
|
694
834
|
*/
|
|
695
835
|
|
|
696
836
|
/**
|
|
697
|
-
* Time Complexity: O(1)
|
|
698
|
-
* Space Complexity: O(1)
|
|
837
|
+
* Time Complexity: O(1)
|
|
838
|
+
* Space Complexity: O(1)
|
|
699
839
|
*
|
|
700
840
|
* The function "pollLast" removes and returns the last element of an array.
|
|
701
841
|
* @returns The last element of the array is being returned.
|
|
@@ -705,8 +845,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
705
845
|
}
|
|
706
846
|
|
|
707
847
|
/**
|
|
708
|
-
* Time Complexity: O(1)
|
|
709
|
-
* Space Complexity: O(
|
|
848
|
+
* Time Complexity: O(1)
|
|
849
|
+
* Space Complexity: O(1)
|
|
850
|
+
* /
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Time Complexity: O(1)
|
|
854
|
+
* Space Complexity: O(1)
|
|
710
855
|
*
|
|
711
856
|
* The "addFirst" function adds an element to the beginning of an array.
|
|
712
857
|
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
@@ -717,8 +862,13 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
717
862
|
}
|
|
718
863
|
|
|
719
864
|
/**
|
|
720
|
-
* Time Complexity: O(1)
|
|
721
|
-
* Space Complexity: O(1)
|
|
865
|
+
* Time Complexity: O(1)
|
|
866
|
+
* Space Complexity: O(1)
|
|
867
|
+
* /
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Time Complexity: O(1)
|
|
871
|
+
* Space Complexity: O(1)
|
|
722
872
|
*
|
|
723
873
|
* The function "pollFirst" removes and returns the first element of an array.
|
|
724
874
|
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
@@ -729,6 +879,11 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
729
879
|
}
|
|
730
880
|
|
|
731
881
|
/**
|
|
882
|
+
* Time Complexity: O(n)
|
|
883
|
+
* Space Complexity: O(1)
|
|
884
|
+
* /
|
|
885
|
+
|
|
886
|
+
/**
|
|
732
887
|
* Time Complexity: O(n)
|
|
733
888
|
* Space Complexity: O(1)
|
|
734
889
|
*
|
|
@@ -737,7 +892,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
737
892
|
*/
|
|
738
893
|
protected* _getIterator(): IterableIterator<E> {
|
|
739
894
|
for (let i = 0; i < this.size; ++i) {
|
|
740
|
-
yield this.
|
|
895
|
+
yield this.at(i);
|
|
741
896
|
}
|
|
742
897
|
}
|
|
743
898
|
|