data-structure-typed 1.45.3 → 1.46.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/CHANGELOG.md +1 -1
- package/README.md +91 -40
- package/benchmark/report.html +17 -17
- package/benchmark/report.json +160 -142
- package/dist/cjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/cjs/data-structures/hash/hash-map.js +9 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/cjs/data-structures/heap/heap.js +133 -123
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/cjs/data-structures/queue/deque.js +543 -211
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/cjs/types/helpers.d.ts +11 -0
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +3 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +6 -5
- package/dist/mjs/data-structures/hash/hash-map.js +9 -6
- package/dist/mjs/data-structures/heap/heap.d.ts +59 -47
- package/dist/mjs/data-structures/heap/heap.js +133 -123
- package/dist/mjs/data-structures/queue/deque.d.ts +131 -164
- package/dist/mjs/data-structures/queue/deque.js +544 -201
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +0 -9
- package/dist/mjs/types/helpers.d.ts +11 -0
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +1 -0
- package/dist/umd/data-structure-typed.js +684 -328
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +11 -7
- package/src/data-structures/heap/heap.ts +132 -128
- package/src/data-structures/queue/deque.ts +605 -226
- package/src/types/data-structures/hash/hash-map.ts +0 -11
- package/src/types/helpers.ts +15 -0
- package/src/utils/utils.ts +2 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +21 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
- package/test/performance/data-structures/queue/deque.test.ts +24 -13
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -4
- package/test/unit/data-structures/heap/heap.test.ts +2 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
- package/test/unit/data-structures/queue/deque.test.ts +252 -240
- /package/test/performance/data-structures/{comparation.test.ts → comparison.test.ts} +0 -0
|
@@ -93,7 +93,6 @@ var dataStructureTyped = (() => {
|
|
|
93
93
|
AbstractEdge: () => AbstractEdge,
|
|
94
94
|
AbstractGraph: () => AbstractGraph,
|
|
95
95
|
AbstractVertex: () => AbstractVertex,
|
|
96
|
-
ArrayDeque: () => ArrayDeque,
|
|
97
96
|
BST: () => BST,
|
|
98
97
|
BSTNode: () => BSTNode,
|
|
99
98
|
BinaryIndexedTree: () => BinaryIndexedTree,
|
|
@@ -104,6 +103,7 @@ var dataStructureTyped = (() => {
|
|
|
104
103
|
CoordinateMap: () => CoordinateMap,
|
|
105
104
|
CoordinateSet: () => CoordinateSet,
|
|
106
105
|
Deque: () => Deque,
|
|
106
|
+
DequeIterator: () => DequeIterator,
|
|
107
107
|
DirectedEdge: () => DirectedEdge,
|
|
108
108
|
DirectedGraph: () => DirectedGraph,
|
|
109
109
|
DirectedVertex: () => DirectedVertex,
|
|
@@ -156,6 +156,7 @@ var dataStructureTyped = (() => {
|
|
|
156
156
|
UndirectedVertex: () => UndirectedVertex,
|
|
157
157
|
Vector2D: () => Vector2D,
|
|
158
158
|
arrayRemove: () => arrayRemove,
|
|
159
|
+
calcMinUnitsRequired: () => calcMinUnitsRequired,
|
|
159
160
|
getMSB: () => getMSB,
|
|
160
161
|
isObjOrFunc: () => isObjOrFunc,
|
|
161
162
|
isThunk: () => isThunk,
|
|
@@ -565,6 +566,7 @@ var dataStructureTyped = (() => {
|
|
|
565
566
|
const inputType = typeof input;
|
|
566
567
|
return inputType === "object" && input !== null || inputType === "function";
|
|
567
568
|
};
|
|
569
|
+
var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
568
570
|
|
|
569
571
|
// src/types/data-structures/binary-tree/binary-tree.ts
|
|
570
572
|
var IterationType = /* @__PURE__ */ ((IterationType3) => {
|
|
@@ -590,13 +592,6 @@ var dataStructureTyped = (() => {
|
|
|
590
592
|
return RBTNColor2;
|
|
591
593
|
})(RBTNColor || {});
|
|
592
594
|
|
|
593
|
-
// src/types/data-structures/hash/hash-map.ts
|
|
594
|
-
var IterateDirection = /* @__PURE__ */ ((IterateDirection2) => {
|
|
595
|
-
IterateDirection2[IterateDirection2["DEFAULT"] = 0] = "DEFAULT";
|
|
596
|
-
IterateDirection2[IterateDirection2["REVERSE"] = 1] = "REVERSE";
|
|
597
|
-
return IterateDirection2;
|
|
598
|
-
})(IterateDirection || {});
|
|
599
|
-
|
|
600
595
|
// src/types/helpers.ts
|
|
601
596
|
var CP = /* @__PURE__ */ ((CP2) => {
|
|
602
597
|
CP2["lt"] = "lt";
|
|
@@ -604,9 +599,14 @@ var dataStructureTyped = (() => {
|
|
|
604
599
|
CP2["gt"] = "gt";
|
|
605
600
|
return CP2;
|
|
606
601
|
})(CP || {});
|
|
602
|
+
var IterateDirection = /* @__PURE__ */ ((IterateDirection2) => {
|
|
603
|
+
IterateDirection2[IterateDirection2["DEFAULT"] = 0] = "DEFAULT";
|
|
604
|
+
IterateDirection2[IterateDirection2["REVERSE"] = 1] = "REVERSE";
|
|
605
|
+
return IterateDirection2;
|
|
606
|
+
})(IterateDirection || {});
|
|
607
607
|
|
|
608
608
|
// src/data-structures/hash/hash-map.ts
|
|
609
|
-
var HashMapIterator = class {
|
|
609
|
+
var HashMapIterator = class _HashMapIterator {
|
|
610
610
|
/**
|
|
611
611
|
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
612
612
|
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
|
|
@@ -704,15 +704,18 @@ var dataStructureTyped = (() => {
|
|
|
704
704
|
next() {
|
|
705
705
|
return this;
|
|
706
706
|
}
|
|
707
|
+
clone() {
|
|
708
|
+
return new _HashMapIterator(this._node, this._sentinel, this.hashMap, this.iterateDirection);
|
|
709
|
+
}
|
|
707
710
|
};
|
|
708
711
|
var HashMap = class {
|
|
709
712
|
/**
|
|
710
713
|
* The constructor initializes a HashMap object with an optional initial set of key-value pairs.
|
|
711
|
-
* @param
|
|
714
|
+
* @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
|
|
712
715
|
* V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
|
|
713
716
|
* `K` represents the type of the key and `V` represents the
|
|
714
717
|
*/
|
|
715
|
-
constructor(
|
|
718
|
+
constructor(elements = []) {
|
|
716
719
|
__publicField(this, "OBJ_KEY_INDEX", Symbol("OBJ_KEY_INDEX"));
|
|
717
720
|
__publicField(this, "_nodes", []);
|
|
718
721
|
__publicField(this, "_orgMap", {});
|
|
@@ -723,9 +726,9 @@ var dataStructureTyped = (() => {
|
|
|
723
726
|
Object.setPrototypeOf(this._orgMap, null);
|
|
724
727
|
this._sentinel = {};
|
|
725
728
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
726
|
-
|
|
729
|
+
for (const el of elements) {
|
|
727
730
|
this.set(el[0], el[1]);
|
|
728
|
-
}
|
|
731
|
+
}
|
|
729
732
|
}
|
|
730
733
|
get size() {
|
|
731
734
|
return this._size;
|
|
@@ -781,7 +784,7 @@ var dataStructureTyped = (() => {
|
|
|
781
784
|
* @returns The front element of the data structure, represented as a tuple with a key (K) and a
|
|
782
785
|
* value (V).
|
|
783
786
|
*/
|
|
784
|
-
get
|
|
787
|
+
get first() {
|
|
785
788
|
if (this._size === 0)
|
|
786
789
|
return;
|
|
787
790
|
return [this._head.key, this._head.value];
|
|
@@ -794,7 +797,7 @@ var dataStructureTyped = (() => {
|
|
|
794
797
|
* @returns The method is returning an array containing the key-value pair of the tail element in the
|
|
795
798
|
* data structure.
|
|
796
799
|
*/
|
|
797
|
-
get
|
|
800
|
+
get last() {
|
|
798
801
|
if (this._size === 0)
|
|
799
802
|
return;
|
|
800
803
|
return [this._tail.key, this._tail.value];
|
|
@@ -3158,7 +3161,525 @@ var dataStructureTyped = (() => {
|
|
|
3158
3161
|
};
|
|
3159
3162
|
|
|
3160
3163
|
// src/data-structures/queue/deque.ts
|
|
3161
|
-
var
|
|
3164
|
+
var DequeIterator = class _DequeIterator {
|
|
3165
|
+
constructor(index, deque, iterateDirection = 0 /* DEFAULT */) {
|
|
3166
|
+
__publicField(this, "iterateDirection");
|
|
3167
|
+
__publicField(this, "index");
|
|
3168
|
+
__publicField(this, "deque");
|
|
3169
|
+
this.index = index;
|
|
3170
|
+
this.iterateDirection = iterateDirection;
|
|
3171
|
+
if (this.iterateDirection === 0 /* DEFAULT */) {
|
|
3172
|
+
this.prev = function() {
|
|
3173
|
+
if (this.index === 0) {
|
|
3174
|
+
throwRangeError();
|
|
3175
|
+
}
|
|
3176
|
+
this.index -= 1;
|
|
3177
|
+
return this;
|
|
3178
|
+
};
|
|
3179
|
+
this.next = function() {
|
|
3180
|
+
if (this.index === this.deque.size) {
|
|
3181
|
+
throwRangeError();
|
|
3182
|
+
}
|
|
3183
|
+
this.index += 1;
|
|
3184
|
+
return this;
|
|
3185
|
+
};
|
|
3186
|
+
} else {
|
|
3187
|
+
this.prev = function() {
|
|
3188
|
+
if (this.index === this.deque.size - 1) {
|
|
3189
|
+
throwRangeError();
|
|
3190
|
+
}
|
|
3191
|
+
this.index += 1;
|
|
3192
|
+
return this;
|
|
3193
|
+
};
|
|
3194
|
+
this.next = function() {
|
|
3195
|
+
if (this.index === -1) {
|
|
3196
|
+
throwRangeError();
|
|
3197
|
+
}
|
|
3198
|
+
this.index -= 1;
|
|
3199
|
+
return this;
|
|
3200
|
+
};
|
|
3201
|
+
}
|
|
3202
|
+
this.deque = deque;
|
|
3203
|
+
}
|
|
3204
|
+
get current() {
|
|
3205
|
+
return this.deque.getAt(this.index);
|
|
3206
|
+
}
|
|
3207
|
+
set current(newElement) {
|
|
3208
|
+
this.deque.setAt(this.index, newElement);
|
|
3209
|
+
}
|
|
3210
|
+
isAccessible() {
|
|
3211
|
+
return this.index !== this.deque.size;
|
|
3212
|
+
}
|
|
3213
|
+
prev() {
|
|
3214
|
+
return this;
|
|
3215
|
+
}
|
|
3216
|
+
next() {
|
|
3217
|
+
return this;
|
|
3218
|
+
}
|
|
3219
|
+
clone() {
|
|
3220
|
+
return new _DequeIterator(this.index, this.deque, this.iterateDirection);
|
|
3221
|
+
}
|
|
3222
|
+
};
|
|
3223
|
+
var Deque = class _Deque {
|
|
3224
|
+
constructor(elements = [], bucketSize = 1 << 12) {
|
|
3225
|
+
__publicField(this, "_bucketFirst", 0);
|
|
3226
|
+
__publicField(this, "_firstInBucket", 0);
|
|
3227
|
+
__publicField(this, "_bucketLast", 0);
|
|
3228
|
+
__publicField(this, "_lastInBucket", 0);
|
|
3229
|
+
__publicField(this, "_bucketCount", 0);
|
|
3230
|
+
__publicField(this, "_bucketSize");
|
|
3231
|
+
__publicField(this, "_buckets", []);
|
|
3232
|
+
__publicField(this, "_size", 0);
|
|
3233
|
+
let _size;
|
|
3234
|
+
if ("length" in elements) {
|
|
3235
|
+
_size = elements.length;
|
|
3236
|
+
} else {
|
|
3237
|
+
_size = elements.size;
|
|
3238
|
+
}
|
|
3239
|
+
this._bucketSize = bucketSize;
|
|
3240
|
+
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
3241
|
+
for (let i = 0; i < this._bucketCount; ++i) {
|
|
3242
|
+
this._buckets.push(new Array(this._bucketSize));
|
|
3243
|
+
}
|
|
3244
|
+
const needBucketNum = calcMinUnitsRequired(_size, this._bucketSize);
|
|
3245
|
+
this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
|
|
3246
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
|
|
3247
|
+
for (const element of elements) {
|
|
3248
|
+
this.push(element);
|
|
3249
|
+
}
|
|
3250
|
+
}
|
|
3251
|
+
get buckets() {
|
|
3252
|
+
return this._buckets;
|
|
3253
|
+
}
|
|
3254
|
+
get size() {
|
|
3255
|
+
return this._size;
|
|
3256
|
+
}
|
|
3257
|
+
get first() {
|
|
3258
|
+
if (this.size === 0)
|
|
3259
|
+
return;
|
|
3260
|
+
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3261
|
+
}
|
|
3262
|
+
get last() {
|
|
3263
|
+
if (this.size === 0)
|
|
3264
|
+
return;
|
|
3265
|
+
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
3266
|
+
}
|
|
3267
|
+
/**
|
|
3268
|
+
* Time Complexity: Amortized O(1) - Generally constant time, but resizing when the deque is full leads to O(n).
|
|
3269
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
3270
|
+
*/
|
|
3271
|
+
empty() {
|
|
3272
|
+
return this._size === 0;
|
|
3273
|
+
}
|
|
3274
|
+
/**
|
|
3275
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
3276
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
3277
|
+
*/
|
|
3278
|
+
isEmpty() {
|
|
3279
|
+
return this.size === 0;
|
|
3280
|
+
}
|
|
3281
|
+
/**
|
|
3282
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
3283
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
3284
|
+
*/
|
|
3285
|
+
/**
|
|
3286
|
+
* Time Complexity: O(1)
|
|
3287
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
3288
|
+
*
|
|
3289
|
+
* The addLast function adds an element to the end of an array.
|
|
3290
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
3291
|
+
* data structure.
|
|
3292
|
+
*/
|
|
3293
|
+
addLast(element) {
|
|
3294
|
+
this.push(element);
|
|
3295
|
+
}
|
|
3296
|
+
/**
|
|
3297
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
3298
|
+
* Space Complexity: O(1) - In-place operation.
|
|
3299
|
+
*/
|
|
3300
|
+
/**
|
|
3301
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
3302
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
3303
|
+
*
|
|
3304
|
+
* The function "popLast" removes and returns the last element of an array.
|
|
3305
|
+
* @returns The last element of the array is being returned.
|
|
3306
|
+
*/
|
|
3307
|
+
popLast() {
|
|
3308
|
+
return this.pop();
|
|
3309
|
+
}
|
|
3310
|
+
/**
|
|
3311
|
+
* Time Complexity: O(1).
|
|
3312
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
3313
|
+
*
|
|
3314
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
3315
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
3316
|
+
* beginning of the data structure.
|
|
3317
|
+
*/
|
|
3318
|
+
addFirst(element) {
|
|
3319
|
+
this.unshift(element);
|
|
3320
|
+
}
|
|
3321
|
+
/**
|
|
3322
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
3323
|
+
* Space Complexity: O(1) - In-place operation.
|
|
3324
|
+
*
|
|
3325
|
+
* The function "popFirst" removes and returns the first element of an array.
|
|
3326
|
+
* @returns The method `popFirst()` is returning the first element of the array after removing it
|
|
3327
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
3328
|
+
*/
|
|
3329
|
+
popFirst() {
|
|
3330
|
+
return this.shift();
|
|
3331
|
+
}
|
|
3332
|
+
clear() {
|
|
3333
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
3334
|
+
this._bucketCount = 1;
|
|
3335
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
3336
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3337
|
+
}
|
|
3338
|
+
begin() {
|
|
3339
|
+
return new DequeIterator(0, this);
|
|
3340
|
+
}
|
|
3341
|
+
end() {
|
|
3342
|
+
return new DequeIterator(this.size, this);
|
|
3343
|
+
}
|
|
3344
|
+
reverseBegin() {
|
|
3345
|
+
return new DequeIterator(this.size - 1, this, 1 /* REVERSE */);
|
|
3346
|
+
}
|
|
3347
|
+
reverseEnd() {
|
|
3348
|
+
return new DequeIterator(-1, this, 1 /* REVERSE */);
|
|
3349
|
+
}
|
|
3350
|
+
push(element) {
|
|
3351
|
+
if (this.size) {
|
|
3352
|
+
if (this._lastInBucket < this._bucketSize - 1) {
|
|
3353
|
+
this._lastInBucket += 1;
|
|
3354
|
+
} else if (this._bucketLast < this._bucketCount - 1) {
|
|
3355
|
+
this._bucketLast += 1;
|
|
3356
|
+
this._lastInBucket = 0;
|
|
3357
|
+
} else {
|
|
3358
|
+
this._bucketLast = 0;
|
|
3359
|
+
this._lastInBucket = 0;
|
|
3360
|
+
}
|
|
3361
|
+
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket)
|
|
3362
|
+
this._reallocate();
|
|
3363
|
+
}
|
|
3364
|
+
this._size += 1;
|
|
3365
|
+
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
3366
|
+
return this.size;
|
|
3367
|
+
}
|
|
3368
|
+
pop() {
|
|
3369
|
+
if (this.size === 0)
|
|
3370
|
+
return;
|
|
3371
|
+
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
3372
|
+
if (this.size !== 1) {
|
|
3373
|
+
if (this._lastInBucket > 0) {
|
|
3374
|
+
this._lastInBucket -= 1;
|
|
3375
|
+
} else if (this._bucketLast > 0) {
|
|
3376
|
+
this._bucketLast -= 1;
|
|
3377
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
3378
|
+
} else {
|
|
3379
|
+
this._bucketLast = this._bucketCount - 1;
|
|
3380
|
+
this._lastInBucket = this._bucketSize - 1;
|
|
3381
|
+
}
|
|
3382
|
+
}
|
|
3383
|
+
this._size -= 1;
|
|
3384
|
+
return element;
|
|
3385
|
+
}
|
|
3386
|
+
unshift(element) {
|
|
3387
|
+
if (this.size) {
|
|
3388
|
+
if (this._firstInBucket > 0) {
|
|
3389
|
+
this._firstInBucket -= 1;
|
|
3390
|
+
} else if (this._bucketFirst > 0) {
|
|
3391
|
+
this._bucketFirst -= 1;
|
|
3392
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
3393
|
+
} else {
|
|
3394
|
+
this._bucketFirst = this._bucketCount - 1;
|
|
3395
|
+
this._firstInBucket = this._bucketSize - 1;
|
|
3396
|
+
}
|
|
3397
|
+
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket)
|
|
3398
|
+
this._reallocate();
|
|
3399
|
+
}
|
|
3400
|
+
this._size += 1;
|
|
3401
|
+
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
3402
|
+
return this.size;
|
|
3403
|
+
}
|
|
3404
|
+
shift() {
|
|
3405
|
+
if (this.size === 0)
|
|
3406
|
+
return;
|
|
3407
|
+
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3408
|
+
if (this.size !== 1) {
|
|
3409
|
+
if (this._firstInBucket < this._bucketSize - 1) {
|
|
3410
|
+
this._firstInBucket += 1;
|
|
3411
|
+
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
3412
|
+
this._bucketFirst += 1;
|
|
3413
|
+
this._firstInBucket = 0;
|
|
3414
|
+
} else {
|
|
3415
|
+
this._bucketFirst = 0;
|
|
3416
|
+
this._firstInBucket = 0;
|
|
3417
|
+
}
|
|
3418
|
+
}
|
|
3419
|
+
this._size -= 1;
|
|
3420
|
+
return element;
|
|
3421
|
+
}
|
|
3422
|
+
getAt(pos) {
|
|
3423
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
3424
|
+
const {
|
|
3425
|
+
bucketIndex,
|
|
3426
|
+
indexInBucket
|
|
3427
|
+
} = this._getBucketAndPosition(pos);
|
|
3428
|
+
return this._buckets[bucketIndex][indexInBucket];
|
|
3429
|
+
}
|
|
3430
|
+
setAt(pos, element) {
|
|
3431
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
3432
|
+
const {
|
|
3433
|
+
bucketIndex,
|
|
3434
|
+
indexInBucket
|
|
3435
|
+
} = this._getBucketAndPosition(pos);
|
|
3436
|
+
this._buckets[bucketIndex][indexInBucket] = element;
|
|
3437
|
+
}
|
|
3438
|
+
insertAt(pos, element, num = 1) {
|
|
3439
|
+
const length = this.size;
|
|
3440
|
+
rangeCheck(pos, 0, length);
|
|
3441
|
+
if (pos === 0) {
|
|
3442
|
+
while (num--)
|
|
3443
|
+
this.unshift(element);
|
|
3444
|
+
} else if (pos === this.size) {
|
|
3445
|
+
while (num--)
|
|
3446
|
+
this.push(element);
|
|
3447
|
+
} else {
|
|
3448
|
+
const arr = [];
|
|
3449
|
+
for (let i = pos; i < this.size; ++i) {
|
|
3450
|
+
arr.push(this.getAt(i));
|
|
3451
|
+
}
|
|
3452
|
+
this.cut(pos - 1);
|
|
3453
|
+
for (let i = 0; i < num; ++i)
|
|
3454
|
+
this.push(element);
|
|
3455
|
+
for (let i = 0; i < arr.length; ++i)
|
|
3456
|
+
this.push(arr[i]);
|
|
3457
|
+
}
|
|
3458
|
+
return this.size;
|
|
3459
|
+
}
|
|
3460
|
+
cut(pos) {
|
|
3461
|
+
if (pos < 0) {
|
|
3462
|
+
this.clear();
|
|
3463
|
+
return 0;
|
|
3464
|
+
}
|
|
3465
|
+
const {
|
|
3466
|
+
bucketIndex,
|
|
3467
|
+
indexInBucket
|
|
3468
|
+
} = this._getBucketAndPosition(pos);
|
|
3469
|
+
this._bucketLast = bucketIndex;
|
|
3470
|
+
this._lastInBucket = indexInBucket;
|
|
3471
|
+
this._size = pos + 1;
|
|
3472
|
+
return this.size;
|
|
3473
|
+
}
|
|
3474
|
+
deleteAt(pos) {
|
|
3475
|
+
rangeCheck(pos, 0, this.size - 1);
|
|
3476
|
+
if (pos === 0)
|
|
3477
|
+
this.shift();
|
|
3478
|
+
else if (pos === this.size - 1)
|
|
3479
|
+
this.pop();
|
|
3480
|
+
else {
|
|
3481
|
+
const length = this.size - 1;
|
|
3482
|
+
let {
|
|
3483
|
+
bucketIndex: curBucket,
|
|
3484
|
+
indexInBucket: curPointer
|
|
3485
|
+
} = this._getBucketAndPosition(pos);
|
|
3486
|
+
for (let i = pos; i < length; ++i) {
|
|
3487
|
+
const {
|
|
3488
|
+
bucketIndex: nextBucket,
|
|
3489
|
+
indexInBucket: nextPointer
|
|
3490
|
+
} = this._getBucketAndPosition(pos + 1);
|
|
3491
|
+
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
3492
|
+
curBucket = nextBucket;
|
|
3493
|
+
curPointer = nextPointer;
|
|
3494
|
+
}
|
|
3495
|
+
this.pop();
|
|
3496
|
+
}
|
|
3497
|
+
return this.size;
|
|
3498
|
+
}
|
|
3499
|
+
delete(element) {
|
|
3500
|
+
const size = this.size;
|
|
3501
|
+
if (size === 0)
|
|
3502
|
+
return 0;
|
|
3503
|
+
let i = 0;
|
|
3504
|
+
let index = 0;
|
|
3505
|
+
while (i < size) {
|
|
3506
|
+
const oldElement = this.getAt(i);
|
|
3507
|
+
if (oldElement !== element) {
|
|
3508
|
+
this.setAt(index, oldElement);
|
|
3509
|
+
index += 1;
|
|
3510
|
+
}
|
|
3511
|
+
i += 1;
|
|
3512
|
+
}
|
|
3513
|
+
this.cut(index - 1);
|
|
3514
|
+
return this.size;
|
|
3515
|
+
}
|
|
3516
|
+
deleteByIterator(iter) {
|
|
3517
|
+
const index = iter.index;
|
|
3518
|
+
this.deleteAt(index);
|
|
3519
|
+
iter = iter.next();
|
|
3520
|
+
return iter;
|
|
3521
|
+
}
|
|
3522
|
+
findIterator(element) {
|
|
3523
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3524
|
+
if (this.getAt(i) === element) {
|
|
3525
|
+
return new DequeIterator(i, this);
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
return this.end();
|
|
3529
|
+
}
|
|
3530
|
+
reverse() {
|
|
3531
|
+
this._buckets.reverse().forEach(function(bucket) {
|
|
3532
|
+
bucket.reverse();
|
|
3533
|
+
});
|
|
3534
|
+
const { _bucketFirst, _bucketLast, _firstInBucket, _lastInBucket } = this;
|
|
3535
|
+
this._bucketFirst = this._bucketCount - _bucketLast - 1;
|
|
3536
|
+
this._bucketLast = this._bucketCount - _bucketFirst - 1;
|
|
3537
|
+
this._firstInBucket = this._bucketSize - _lastInBucket - 1;
|
|
3538
|
+
this._lastInBucket = this._bucketSize - _firstInBucket - 1;
|
|
3539
|
+
return this;
|
|
3540
|
+
}
|
|
3541
|
+
unique() {
|
|
3542
|
+
if (this.size <= 1) {
|
|
3543
|
+
return this.size;
|
|
3544
|
+
}
|
|
3545
|
+
let index = 1;
|
|
3546
|
+
let prev = this.getAt(0);
|
|
3547
|
+
for (let i = 1; i < this.size; ++i) {
|
|
3548
|
+
const cur = this.getAt(i);
|
|
3549
|
+
if (cur !== prev) {
|
|
3550
|
+
prev = cur;
|
|
3551
|
+
this.setAt(index++, cur);
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
this.cut(index - 1);
|
|
3555
|
+
return this.size;
|
|
3556
|
+
}
|
|
3557
|
+
sort(comparator) {
|
|
3558
|
+
const arr = [];
|
|
3559
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3560
|
+
arr.push(this.getAt(i));
|
|
3561
|
+
}
|
|
3562
|
+
arr.sort(comparator);
|
|
3563
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3564
|
+
this.setAt(i, arr[i]);
|
|
3565
|
+
}
|
|
3566
|
+
return this;
|
|
3567
|
+
}
|
|
3568
|
+
shrinkToFit() {
|
|
3569
|
+
if (this.size === 0)
|
|
3570
|
+
return;
|
|
3571
|
+
const newBuckets = [];
|
|
3572
|
+
if (this._bucketFirst === this._bucketLast)
|
|
3573
|
+
return;
|
|
3574
|
+
else if (this._bucketFirst < this._bucketLast) {
|
|
3575
|
+
for (let i = this._bucketFirst; i <= this._bucketLast; ++i) {
|
|
3576
|
+
newBuckets.push(this._buckets[i]);
|
|
3577
|
+
}
|
|
3578
|
+
} else {
|
|
3579
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
3580
|
+
newBuckets.push(this._buckets[i]);
|
|
3581
|
+
}
|
|
3582
|
+
for (let i = 0; i <= this._bucketLast; ++i) {
|
|
3583
|
+
newBuckets.push(this._buckets[i]);
|
|
3584
|
+
}
|
|
3585
|
+
}
|
|
3586
|
+
this._bucketFirst = 0;
|
|
3587
|
+
this._bucketLast = newBuckets.length - 1;
|
|
3588
|
+
this._buckets = newBuckets;
|
|
3589
|
+
}
|
|
3590
|
+
forEach(callback) {
|
|
3591
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3592
|
+
callback(this.getAt(i), i, this);
|
|
3593
|
+
}
|
|
3594
|
+
}
|
|
3595
|
+
find(callback) {
|
|
3596
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3597
|
+
const element = this.getAt(i);
|
|
3598
|
+
if (callback(element, i, this)) {
|
|
3599
|
+
return element;
|
|
3600
|
+
}
|
|
3601
|
+
}
|
|
3602
|
+
return void 0;
|
|
3603
|
+
}
|
|
3604
|
+
toArray() {
|
|
3605
|
+
const arr = [];
|
|
3606
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3607
|
+
arr.push(this.getAt(i));
|
|
3608
|
+
}
|
|
3609
|
+
return arr;
|
|
3610
|
+
}
|
|
3611
|
+
map(callback) {
|
|
3612
|
+
const newDeque = new _Deque([], this._bucketSize);
|
|
3613
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3614
|
+
newDeque.push(callback(this.getAt(i), i, this));
|
|
3615
|
+
}
|
|
3616
|
+
return newDeque;
|
|
3617
|
+
}
|
|
3618
|
+
filter(predicate) {
|
|
3619
|
+
const newDeque = new _Deque([], this._bucketSize);
|
|
3620
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3621
|
+
const element = this.getAt(i);
|
|
3622
|
+
if (predicate(element, i, this)) {
|
|
3623
|
+
newDeque.push(element);
|
|
3624
|
+
}
|
|
3625
|
+
}
|
|
3626
|
+
return newDeque;
|
|
3627
|
+
}
|
|
3628
|
+
reduce(callback, initialValue) {
|
|
3629
|
+
let accumulator = initialValue;
|
|
3630
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3631
|
+
accumulator = callback(accumulator, this.getAt(i), i, this);
|
|
3632
|
+
}
|
|
3633
|
+
return accumulator;
|
|
3634
|
+
}
|
|
3635
|
+
indexOf(element) {
|
|
3636
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3637
|
+
if (this.getAt(i) === element) {
|
|
3638
|
+
return i;
|
|
3639
|
+
}
|
|
3640
|
+
}
|
|
3641
|
+
return -1;
|
|
3642
|
+
}
|
|
3643
|
+
*[Symbol.iterator]() {
|
|
3644
|
+
for (let i = 0; i < this.size; ++i) {
|
|
3645
|
+
yield this.getAt(i);
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
3648
|
+
_reallocate(needBucketNum) {
|
|
3649
|
+
const newBuckets = [];
|
|
3650
|
+
const addBucketNum = needBucketNum || this._bucketCount >> 1 || 1;
|
|
3651
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
3652
|
+
newBuckets[i] = new Array(this._bucketSize);
|
|
3653
|
+
}
|
|
3654
|
+
for (let i = this._bucketFirst; i < this._bucketCount; ++i) {
|
|
3655
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
3656
|
+
}
|
|
3657
|
+
for (let i = 0; i < this._bucketLast; ++i) {
|
|
3658
|
+
newBuckets[newBuckets.length] = this._buckets[i];
|
|
3659
|
+
}
|
|
3660
|
+
newBuckets[newBuckets.length] = [...this._buckets[this._bucketLast]];
|
|
3661
|
+
this._bucketFirst = addBucketNum;
|
|
3662
|
+
this._bucketLast = newBuckets.length - 1;
|
|
3663
|
+
for (let i = 0; i < addBucketNum; ++i) {
|
|
3664
|
+
newBuckets[newBuckets.length] = new Array(this._bucketSize);
|
|
3665
|
+
}
|
|
3666
|
+
this._buckets = newBuckets;
|
|
3667
|
+
this._bucketCount = newBuckets.length;
|
|
3668
|
+
}
|
|
3669
|
+
_getBucketAndPosition(pos) {
|
|
3670
|
+
let bucketIndex;
|
|
3671
|
+
let indexInBucket;
|
|
3672
|
+
const overallIndex = this._firstInBucket + pos;
|
|
3673
|
+
bucketIndex = this._bucketFirst + Math.floor(overallIndex / this._bucketSize);
|
|
3674
|
+
if (bucketIndex >= this._bucketCount) {
|
|
3675
|
+
bucketIndex -= this._bucketCount;
|
|
3676
|
+
}
|
|
3677
|
+
indexInBucket = (overallIndex + 1) % this._bucketSize - 1;
|
|
3678
|
+
if (indexInBucket < 0) {
|
|
3679
|
+
indexInBucket = this._bucketSize - 1;
|
|
3680
|
+
}
|
|
3681
|
+
return { bucketIndex, indexInBucket };
|
|
3682
|
+
}
|
|
3162
3683
|
};
|
|
3163
3684
|
var ObjectDeque = class {
|
|
3164
3685
|
constructor(capacity) {
|
|
@@ -3193,11 +3714,11 @@ var dataStructureTyped = (() => {
|
|
|
3193
3714
|
* Time Complexity: O(1)
|
|
3194
3715
|
* Space Complexity: O(1)
|
|
3195
3716
|
*
|
|
3196
|
-
* The "addFirst" function adds
|
|
3197
|
-
* @param {E}
|
|
3717
|
+
* The "addFirst" function adds an element to the beginning of an array-like data structure.
|
|
3718
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the beginning of the data
|
|
3198
3719
|
* structure.
|
|
3199
3720
|
*/
|
|
3200
|
-
addFirst(
|
|
3721
|
+
addFirst(element) {
|
|
3201
3722
|
if (this.size === 0) {
|
|
3202
3723
|
const mid = Math.floor(this.capacity / 2);
|
|
3203
3724
|
this._first = mid;
|
|
@@ -3205,7 +3726,7 @@ var dataStructureTyped = (() => {
|
|
|
3205
3726
|
} else {
|
|
3206
3727
|
this._first--;
|
|
3207
3728
|
}
|
|
3208
|
-
this.nodes[this.first] =
|
|
3729
|
+
this.nodes[this.first] = element;
|
|
3209
3730
|
this._size++;
|
|
3210
3731
|
}
|
|
3211
3732
|
/**
|
|
@@ -3216,10 +3737,10 @@ var dataStructureTyped = (() => {
|
|
|
3216
3737
|
* Time Complexity: O(1)
|
|
3217
3738
|
* Space Complexity: O(1)
|
|
3218
3739
|
*
|
|
3219
|
-
* The addLast function adds
|
|
3220
|
-
* @param {E}
|
|
3740
|
+
* The addLast function adds an element to the end of an array-like data structure.
|
|
3741
|
+
* @param {E} element - The `element` parameter represents the element that you want to add to the end of the data structure.
|
|
3221
3742
|
*/
|
|
3222
|
-
addLast(
|
|
3743
|
+
addLast(element) {
|
|
3223
3744
|
if (this.size === 0) {
|
|
3224
3745
|
const mid = Math.floor(this.capacity / 2);
|
|
3225
3746
|
this._first = mid;
|
|
@@ -3227,7 +3748,7 @@ var dataStructureTyped = (() => {
|
|
|
3227
3748
|
} else {
|
|
3228
3749
|
this._last++;
|
|
3229
3750
|
}
|
|
3230
|
-
this.nodes[this.last] =
|
|
3751
|
+
this.nodes[this.last] = element;
|
|
3231
3752
|
this._size++;
|
|
3232
3753
|
}
|
|
3233
3754
|
/**
|
|
@@ -3239,16 +3760,16 @@ var dataStructureTyped = (() => {
|
|
|
3239
3760
|
* Space Complexity: O(1)
|
|
3240
3761
|
*
|
|
3241
3762
|
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
3242
|
-
* @returns The
|
|
3763
|
+
* @returns The element of the first element in the data structure.
|
|
3243
3764
|
*/
|
|
3244
3765
|
popFirst() {
|
|
3245
3766
|
if (!this.size)
|
|
3246
3767
|
return;
|
|
3247
|
-
const
|
|
3768
|
+
const element = this.getFirst();
|
|
3248
3769
|
delete this.nodes[this.first];
|
|
3249
3770
|
this._first++;
|
|
3250
3771
|
this._size--;
|
|
3251
|
-
return
|
|
3772
|
+
return element;
|
|
3252
3773
|
}
|
|
3253
3774
|
/**
|
|
3254
3775
|
* Time Complexity: O(1)
|
|
@@ -3274,16 +3795,16 @@ var dataStructureTyped = (() => {
|
|
|
3274
3795
|
* Space Complexity: O(1)
|
|
3275
3796
|
*
|
|
3276
3797
|
* The `popLast()` function removes and returns the last element in a data structure.
|
|
3277
|
-
* @returns The
|
|
3798
|
+
* @returns The element that was removed from the data structure.
|
|
3278
3799
|
*/
|
|
3279
3800
|
popLast() {
|
|
3280
3801
|
if (!this.size)
|
|
3281
3802
|
return;
|
|
3282
|
-
const
|
|
3803
|
+
const element = this.getLast();
|
|
3283
3804
|
delete this.nodes[this.last];
|
|
3284
3805
|
this._last--;
|
|
3285
3806
|
this._size--;
|
|
3286
|
-
return
|
|
3807
|
+
return element;
|
|
3287
3808
|
}
|
|
3288
3809
|
/**
|
|
3289
3810
|
* Time Complexity: O(1)
|
|
@@ -3312,217 +3833,33 @@ var dataStructureTyped = (() => {
|
|
|
3312
3833
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
3313
3834
|
* retrieve from the array.
|
|
3314
3835
|
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
3315
|
-
* index, `
|
|
3836
|
+
* index, `undefined` is returned.
|
|
3316
3837
|
*/
|
|
3317
3838
|
get(index) {
|
|
3318
|
-
return this.nodes[this.first + index] ||
|
|
3839
|
+
return this.nodes[this.first + index] || void 0;
|
|
3319
3840
|
}
|
|
3320
3841
|
/**
|
|
3321
3842
|
* The function checks if the size of a data structure is less than or equal to zero.
|
|
3322
|
-
* @returns The method is returning a boolean
|
|
3843
|
+
* @returns The method is returning a boolean element indicating whether the size of the object is less than or equal to 0.
|
|
3323
3844
|
*/
|
|
3324
3845
|
isEmpty() {
|
|
3325
3846
|
return this.size <= 0;
|
|
3326
3847
|
}
|
|
3327
3848
|
};
|
|
3328
|
-
var ArrayDeque = class {
|
|
3329
|
-
constructor() {
|
|
3330
|
-
__publicField(this, "_nodes", []);
|
|
3331
|
-
}
|
|
3332
|
-
get nodes() {
|
|
3333
|
-
return this._nodes;
|
|
3334
|
-
}
|
|
3335
|
-
get size() {
|
|
3336
|
-
return this.nodes.length;
|
|
3337
|
-
}
|
|
3338
|
-
/**
|
|
3339
|
-
* Time Complexity: O(1)
|
|
3340
|
-
* Space Complexity: O(1)
|
|
3341
|
-
*/
|
|
3342
|
-
/**
|
|
3343
|
-
* Time Complexity: O(1)
|
|
3344
|
-
* Space Complexity: O(1)
|
|
3345
|
-
*
|
|
3346
|
-
* The function "addLast" adds a value to the end of an array.
|
|
3347
|
-
* @param {E} value - The value parameter represents the value that you want to add to the end of the array.
|
|
3348
|
-
* @returns The return value is the new length of the array after the value has been added.
|
|
3349
|
-
*/
|
|
3350
|
-
addLast(value) {
|
|
3351
|
-
return this.nodes.push(value);
|
|
3352
|
-
}
|
|
3353
|
-
/**
|
|
3354
|
-
* Time Complexity: O(1)
|
|
3355
|
-
* Space Complexity: O(1)
|
|
3356
|
-
*/
|
|
3357
|
-
/**
|
|
3358
|
-
* Time Complexity: O(1)
|
|
3359
|
-
* Space Complexity: O(1)
|
|
3360
|
-
*
|
|
3361
|
-
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
3362
|
-
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
3363
|
-
*/
|
|
3364
|
-
popLast() {
|
|
3365
|
-
var _a;
|
|
3366
|
-
return (_a = this.nodes.pop()) != null ? _a : null;
|
|
3367
|
-
}
|
|
3368
|
-
/**
|
|
3369
|
-
* Time Complexity: O(n)
|
|
3370
|
-
* Space Complexity: O(1)
|
|
3371
|
-
*/
|
|
3372
|
-
/**
|
|
3373
|
-
* Time Complexity: O(n)
|
|
3374
|
-
* Space Complexity: O(1)
|
|
3375
|
-
*
|
|
3376
|
-
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
3377
|
-
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
3378
|
-
* empty.
|
|
3379
|
-
*/
|
|
3380
|
-
popFirst() {
|
|
3381
|
-
var _a;
|
|
3382
|
-
return (_a = this.nodes.shift()) != null ? _a : null;
|
|
3383
|
-
}
|
|
3384
|
-
/**
|
|
3385
|
-
* Time Complexity: O(n)
|
|
3386
|
-
* Space Complexity: O(1)
|
|
3387
|
-
*/
|
|
3388
|
-
/**
|
|
3389
|
-
* Time Complexity: O(n)
|
|
3390
|
-
* Space Complexity: O(1)
|
|
3391
|
-
*
|
|
3392
|
-
* The function "addFirst" adds a value to the beginning of an array.
|
|
3393
|
-
* @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
3394
|
-
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
3395
|
-
* `value` at the beginning.
|
|
3396
|
-
*/
|
|
3397
|
-
addFirst(value) {
|
|
3398
|
-
return this.nodes.unshift(value);
|
|
3399
|
-
}
|
|
3400
|
-
/**
|
|
3401
|
-
* Time Complexity: O(1)
|
|
3402
|
-
* Space Complexity: O(1)
|
|
3403
|
-
*/
|
|
3404
|
-
/**
|
|
3405
|
-
* Time Complexity: O(1)
|
|
3406
|
-
* Space Complexity: O(1)
|
|
3407
|
-
*
|
|
3408
|
-
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
3409
|
-
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
3410
|
-
* empty, it will return `null`.
|
|
3411
|
-
*/
|
|
3412
|
-
getFirst() {
|
|
3413
|
-
var _a;
|
|
3414
|
-
return (_a = this.nodes[0]) != null ? _a : null;
|
|
3415
|
-
}
|
|
3416
|
-
/**
|
|
3417
|
-
* Time Complexity: O(1)
|
|
3418
|
-
* Space Complexity: O(1)
|
|
3419
|
-
*/
|
|
3420
|
-
/**
|
|
3421
|
-
* Time Complexity: O(1)
|
|
3422
|
-
* Space Complexity: O(1)
|
|
3423
|
-
*
|
|
3424
|
-
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
3425
|
-
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
3426
|
-
*/
|
|
3427
|
-
getLast() {
|
|
3428
|
-
var _a;
|
|
3429
|
-
return (_a = this.nodes[this.nodes.length - 1]) != null ? _a : null;
|
|
3430
|
-
}
|
|
3431
|
-
/**
|
|
3432
|
-
* Time Complexity: O(1)
|
|
3433
|
-
* Space Complexity: O(1)
|
|
3434
|
-
*/
|
|
3435
|
-
/**
|
|
3436
|
-
* Time Complexity: O(1)
|
|
3437
|
-
* Space Complexity: O(1)
|
|
3438
|
-
*
|
|
3439
|
-
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
3440
|
-
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
3441
|
-
* retrieve from the array.
|
|
3442
|
-
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
3443
|
-
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
3444
|
-
*/
|
|
3445
|
-
get(index) {
|
|
3446
|
-
var _a;
|
|
3447
|
-
return (_a = this.nodes[index]) != null ? _a : null;
|
|
3448
|
-
}
|
|
3449
|
-
/**
|
|
3450
|
-
* Time Complexity: O(1)
|
|
3451
|
-
* Space Complexity: O(1)
|
|
3452
|
-
*/
|
|
3453
|
-
/**
|
|
3454
|
-
* Time Complexity: O(1)
|
|
3455
|
-
* Space Complexity: O(1)
|
|
3456
|
-
*
|
|
3457
|
-
* The set function assigns a value to a specific index in an array.
|
|
3458
|
-
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
3459
|
-
* that you want to set a new value for.
|
|
3460
|
-
* @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
3461
|
-
* _nodes array.
|
|
3462
|
-
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
3463
|
-
*/
|
|
3464
|
-
set(index, value) {
|
|
3465
|
-
return this.nodes[index] = value;
|
|
3466
|
-
}
|
|
3467
|
-
/**
|
|
3468
|
-
* Time Complexity: O(n)
|
|
3469
|
-
* Space Complexity: O(1)
|
|
3470
|
-
*/
|
|
3471
|
-
/**
|
|
3472
|
-
* Time Complexity: O(n)
|
|
3473
|
-
* Space Complexity: O(1)
|
|
3474
|
-
*
|
|
3475
|
-
* The insert function adds a value at a specified index in an array.
|
|
3476
|
-
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
3477
|
-
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
3478
|
-
* from 0, so the first element of the array has an index of 0, the second element has
|
|
3479
|
-
* @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
3480
|
-
* index.
|
|
3481
|
-
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
3482
|
-
* are being removed, an empty array will be returned.
|
|
3483
|
-
*/
|
|
3484
|
-
insert(index, value) {
|
|
3485
|
-
return this.nodes.splice(index, 0, value);
|
|
3486
|
-
}
|
|
3487
|
-
/**
|
|
3488
|
-
* Time Complexity: O(n)
|
|
3489
|
-
* Space Complexity: O(1)
|
|
3490
|
-
*/
|
|
3491
|
-
/**
|
|
3492
|
-
* Time Complexity: O(n)
|
|
3493
|
-
* Space Complexity: O(1)
|
|
3494
|
-
*
|
|
3495
|
-
* The delete function removes an element from an array at a specified index.
|
|
3496
|
-
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
3497
|
-
* is a number that represents the index of the element to be removed.
|
|
3498
|
-
* @returns The method is returning an array containing the removed element.
|
|
3499
|
-
*/
|
|
3500
|
-
delete(index) {
|
|
3501
|
-
return this.nodes.splice(index, 1);
|
|
3502
|
-
}
|
|
3503
|
-
/**
|
|
3504
|
-
* The function checks if an array called "_nodes" is empty.
|
|
3505
|
-
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
3506
|
-
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
3507
|
-
*/
|
|
3508
|
-
isEmpty() {
|
|
3509
|
-
return this.nodes.length === 0;
|
|
3510
|
-
}
|
|
3511
|
-
};
|
|
3512
3849
|
|
|
3513
3850
|
// src/data-structures/heap/heap.ts
|
|
3514
3851
|
var Heap = class _Heap {
|
|
3515
3852
|
constructor(options) {
|
|
3516
|
-
__publicField(this, "
|
|
3853
|
+
__publicField(this, "_elements", []);
|
|
3517
3854
|
__publicField(this, "_comparator");
|
|
3518
3855
|
this._comparator = options.comparator;
|
|
3519
|
-
if (options.
|
|
3520
|
-
this.
|
|
3856
|
+
if (options.elements && options.elements.length > 0) {
|
|
3857
|
+
this._elements = options.elements;
|
|
3521
3858
|
this.fix();
|
|
3522
3859
|
}
|
|
3523
3860
|
}
|
|
3524
|
-
get
|
|
3525
|
-
return this.
|
|
3861
|
+
get elements() {
|
|
3862
|
+
return this._elements;
|
|
3526
3863
|
}
|
|
3527
3864
|
get comparator() {
|
|
3528
3865
|
return this._comparator;
|
|
@@ -3531,7 +3868,7 @@ var dataStructureTyped = (() => {
|
|
|
3531
3868
|
* Get the size (number of elements) of the heap.
|
|
3532
3869
|
*/
|
|
3533
3870
|
get size() {
|
|
3534
|
-
return this.
|
|
3871
|
+
return this.elements.length;
|
|
3535
3872
|
}
|
|
3536
3873
|
/**
|
|
3537
3874
|
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
@@ -3539,10 +3876,10 @@ var dataStructureTyped = (() => {
|
|
|
3539
3876
|
*/
|
|
3540
3877
|
get leaf() {
|
|
3541
3878
|
var _a;
|
|
3542
|
-
return (_a = this.
|
|
3879
|
+
return (_a = this.elements[this.size - 1]) != null ? _a : void 0;
|
|
3543
3880
|
}
|
|
3544
3881
|
/**
|
|
3545
|
-
* Static method that creates a binary heap from an array of
|
|
3882
|
+
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
3546
3883
|
* @returns A new Heap instance.
|
|
3547
3884
|
* @param options
|
|
3548
3885
|
*/
|
|
@@ -3550,11 +3887,11 @@ var dataStructureTyped = (() => {
|
|
|
3550
3887
|
return new _Heap(options);
|
|
3551
3888
|
}
|
|
3552
3889
|
/**
|
|
3553
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3890
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3554
3891
|
* Space Complexity: O(1)
|
|
3555
3892
|
*/
|
|
3556
3893
|
/**
|
|
3557
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3894
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3558
3895
|
* Space Complexity: O(1)
|
|
3559
3896
|
*
|
|
3560
3897
|
* Insert an element into the heap and maintain the heap properties.
|
|
@@ -3564,50 +3901,49 @@ var dataStructureTyped = (() => {
|
|
|
3564
3901
|
return this.push(element);
|
|
3565
3902
|
}
|
|
3566
3903
|
/**
|
|
3567
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3904
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3568
3905
|
* Space Complexity: O(1)
|
|
3569
3906
|
*/
|
|
3570
3907
|
/**
|
|
3571
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3908
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3572
3909
|
* Space Complexity: O(1)
|
|
3573
3910
|
*
|
|
3574
3911
|
* Insert an element into the heap and maintain the heap properties.
|
|
3575
3912
|
* @param element - The element to be inserted.
|
|
3576
3913
|
*/
|
|
3577
3914
|
push(element) {
|
|
3578
|
-
this.
|
|
3579
|
-
this.
|
|
3915
|
+
this._elements.push(element);
|
|
3916
|
+
this._bubbleUp(this.elements.length - 1);
|
|
3580
3917
|
return this;
|
|
3581
3918
|
}
|
|
3582
3919
|
/**
|
|
3583
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3920
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3584
3921
|
* Space Complexity: O(1)
|
|
3585
3922
|
*/
|
|
3586
3923
|
/**
|
|
3587
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3924
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3588
3925
|
* Space Complexity: O(1)
|
|
3589
3926
|
*
|
|
3590
3927
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
3591
3928
|
* @returns The top element or undefined if the heap is empty.
|
|
3592
3929
|
*/
|
|
3593
3930
|
poll() {
|
|
3594
|
-
if (this.
|
|
3595
|
-
return
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3931
|
+
if (this.elements.length === 0)
|
|
3932
|
+
return;
|
|
3933
|
+
const value = this.elements[0];
|
|
3934
|
+
const last = this.elements.pop();
|
|
3935
|
+
if (this.elements.length) {
|
|
3936
|
+
this.elements[0] = last;
|
|
3937
|
+
this._sinkDown(0, this.elements.length >> 1);
|
|
3599
3938
|
}
|
|
3600
|
-
|
|
3601
|
-
this.nodes[0] = this.nodes.pop();
|
|
3602
|
-
this.sinkDown(0);
|
|
3603
|
-
return topValue;
|
|
3939
|
+
return value;
|
|
3604
3940
|
}
|
|
3605
3941
|
/**
|
|
3606
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3942
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3607
3943
|
* Space Complexity: O(1)
|
|
3608
3944
|
*/
|
|
3609
3945
|
/**
|
|
3610
|
-
* Time Complexity: O(log n), where n is the number of
|
|
3946
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3611
3947
|
* Space Complexity: O(1)
|
|
3612
3948
|
*
|
|
3613
3949
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -3621,10 +3957,7 @@ var dataStructureTyped = (() => {
|
|
|
3621
3957
|
* @returns The top element or undefined if the heap is empty.
|
|
3622
3958
|
*/
|
|
3623
3959
|
peek() {
|
|
3624
|
-
|
|
3625
|
-
return void 0;
|
|
3626
|
-
}
|
|
3627
|
-
return this.nodes[0];
|
|
3960
|
+
return this.elements[0];
|
|
3628
3961
|
}
|
|
3629
3962
|
/**
|
|
3630
3963
|
* Check if the heap is empty.
|
|
@@ -3634,32 +3967,32 @@ var dataStructureTyped = (() => {
|
|
|
3634
3967
|
return this.size === 0;
|
|
3635
3968
|
}
|
|
3636
3969
|
/**
|
|
3637
|
-
* Reset the
|
|
3970
|
+
* Reset the elements of the heap. Make the elements empty.
|
|
3638
3971
|
*/
|
|
3639
3972
|
clear() {
|
|
3640
|
-
this.
|
|
3973
|
+
this._elements = [];
|
|
3641
3974
|
}
|
|
3642
3975
|
/**
|
|
3643
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
3976
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
3644
3977
|
* Space Complexity: O(n)
|
|
3645
3978
|
*/
|
|
3646
3979
|
/**
|
|
3647
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
3980
|
+
* Time Complexity: O(n), where n is the number of elements in the elements array.
|
|
3648
3981
|
* Space Complexity: O(n)
|
|
3649
3982
|
*
|
|
3650
|
-
* Clear and add
|
|
3651
|
-
* @param
|
|
3983
|
+
* Clear and add elements of the heap
|
|
3984
|
+
* @param elements
|
|
3652
3985
|
*/
|
|
3653
|
-
refill(
|
|
3654
|
-
this.
|
|
3986
|
+
refill(elements) {
|
|
3987
|
+
this._elements = elements;
|
|
3655
3988
|
this.fix();
|
|
3656
3989
|
}
|
|
3657
3990
|
/**
|
|
3658
|
-
* Time Complexity: O(n), where n is the number of
|
|
3991
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
3659
3992
|
* Space Complexity: O(1)
|
|
3660
3993
|
*/
|
|
3661
3994
|
/**
|
|
3662
|
-
* Time Complexity: O(n), where n is the number of
|
|
3995
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
3663
3996
|
* Space Complexity: O(1)
|
|
3664
3997
|
*
|
|
3665
3998
|
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
@@ -3667,14 +4000,44 @@ var dataStructureTyped = (() => {
|
|
|
3667
4000
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
3668
4001
|
*/
|
|
3669
4002
|
has(element) {
|
|
3670
|
-
return this.
|
|
4003
|
+
return this.elements.includes(element);
|
|
3671
4004
|
}
|
|
3672
4005
|
/**
|
|
3673
|
-
* Time Complexity: O(n), where n is the number of
|
|
4006
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
4007
|
+
* Space Complexity: O(1)
|
|
4008
|
+
*/
|
|
4009
|
+
/**
|
|
4010
|
+
* Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
|
|
4011
|
+
* Space Complexity: O(1)
|
|
4012
|
+
*
|
|
4013
|
+
* The `delete` function removes an element from an array-like data structure, maintaining the order
|
|
4014
|
+
* and structure of the remaining elements.
|
|
4015
|
+
* @param {E} element - The `element` parameter represents the element that you want to delete from
|
|
4016
|
+
* the array `this.elements`.
|
|
4017
|
+
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
4018
|
+
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
4019
|
+
*/
|
|
4020
|
+
delete(element) {
|
|
4021
|
+
const index = this.elements.indexOf(element);
|
|
4022
|
+
if (index < 0)
|
|
4023
|
+
return false;
|
|
4024
|
+
if (index === 0) {
|
|
4025
|
+
this.pop();
|
|
4026
|
+
} else if (index === this.elements.length - 1) {
|
|
4027
|
+
this.elements.pop();
|
|
4028
|
+
} else {
|
|
4029
|
+
this.elements.splice(index, 1, this.elements.pop());
|
|
4030
|
+
this._bubbleUp(index);
|
|
4031
|
+
this._sinkDown(index, this.elements.length >> 1);
|
|
4032
|
+
}
|
|
4033
|
+
return true;
|
|
4034
|
+
}
|
|
4035
|
+
/**
|
|
4036
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
3674
4037
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
3675
4038
|
*/
|
|
3676
4039
|
/**
|
|
3677
|
-
* Time Complexity: O(n), where n is the number of
|
|
4040
|
+
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
3678
4041
|
* Space Complexity: O(h), where h is the height of the heap.
|
|
3679
4042
|
*
|
|
3680
4043
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
@@ -3687,16 +4050,16 @@ var dataStructureTyped = (() => {
|
|
|
3687
4050
|
if (index < this.size) {
|
|
3688
4051
|
if (order === "in") {
|
|
3689
4052
|
dfsHelper(2 * index + 1);
|
|
3690
|
-
result.push(this.
|
|
4053
|
+
result.push(this.elements[index]);
|
|
3691
4054
|
dfsHelper(2 * index + 2);
|
|
3692
4055
|
} else if (order === "pre") {
|
|
3693
|
-
result.push(this.
|
|
4056
|
+
result.push(this.elements[index]);
|
|
3694
4057
|
dfsHelper(2 * index + 1);
|
|
3695
4058
|
dfsHelper(2 * index + 2);
|
|
3696
4059
|
} else if (order === "post") {
|
|
3697
4060
|
dfsHelper(2 * index + 1);
|
|
3698
4061
|
dfsHelper(2 * index + 2);
|
|
3699
|
-
result.push(this.
|
|
4062
|
+
result.push(this.elements[index]);
|
|
3700
4063
|
}
|
|
3701
4064
|
}
|
|
3702
4065
|
};
|
|
@@ -3715,14 +4078,7 @@ var dataStructureTyped = (() => {
|
|
|
3715
4078
|
* @returns An array containing the elements of the heap.
|
|
3716
4079
|
*/
|
|
3717
4080
|
toArray() {
|
|
3718
|
-
return [...this.
|
|
3719
|
-
}
|
|
3720
|
-
/**
|
|
3721
|
-
* Time Complexity: O(1)
|
|
3722
|
-
* Space Complexity: O(1)
|
|
3723
|
-
*/
|
|
3724
|
-
getNodes() {
|
|
3725
|
-
return this.nodes;
|
|
4081
|
+
return [...this.elements];
|
|
3726
4082
|
}
|
|
3727
4083
|
/**
|
|
3728
4084
|
* Time Complexity: O(n)
|
|
@@ -3737,7 +4093,7 @@ var dataStructureTyped = (() => {
|
|
|
3737
4093
|
*/
|
|
3738
4094
|
clone() {
|
|
3739
4095
|
const clonedHeap = new _Heap({ comparator: this.comparator });
|
|
3740
|
-
clonedHeap.
|
|
4096
|
+
clonedHeap._elements = [...this.elements];
|
|
3741
4097
|
return clonedHeap;
|
|
3742
4098
|
}
|
|
3743
4099
|
/**
|
|
@@ -3766,23 +4122,14 @@ var dataStructureTyped = (() => {
|
|
|
3766
4122
|
* Space Complexity: O(1)
|
|
3767
4123
|
*/
|
|
3768
4124
|
/**
|
|
3769
|
-
* Time Complexity: O(
|
|
4125
|
+
* Time Complexity: O(n)
|
|
3770
4126
|
* Space Complexity: O(1)
|
|
3771
4127
|
*
|
|
3772
|
-
*
|
|
3773
|
-
* @param index - The index of the newly added element.
|
|
4128
|
+
* Fix the entire heap to maintain heap properties.
|
|
3774
4129
|
*/
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
const parent = index - 1 >> 1;
|
|
3779
|
-
const parentItem = this.nodes[parent];
|
|
3780
|
-
if (this.comparator(parentItem, item) <= 0)
|
|
3781
|
-
break;
|
|
3782
|
-
this.nodes[index] = parentItem;
|
|
3783
|
-
index = parent;
|
|
3784
|
-
}
|
|
3785
|
-
this.nodes[index] = item;
|
|
4130
|
+
fix() {
|
|
4131
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
4132
|
+
this._sinkDown(i, this.elements.length >> 1);
|
|
3786
4133
|
}
|
|
3787
4134
|
/**
|
|
3788
4135
|
* Time Complexity: O(log n)
|
|
@@ -3792,40 +4139,49 @@ var dataStructureTyped = (() => {
|
|
|
3792
4139
|
* Time Complexity: O(log n)
|
|
3793
4140
|
* Space Complexity: O(1)
|
|
3794
4141
|
*
|
|
3795
|
-
*
|
|
3796
|
-
* @param index - The index
|
|
4142
|
+
* Float operation to maintain heap properties after adding an element.
|
|
4143
|
+
* @param index - The index of the newly added element.
|
|
3797
4144
|
*/
|
|
3798
|
-
|
|
3799
|
-
const
|
|
3800
|
-
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
targetIndex = rightChildIndex;
|
|
3808
|
-
}
|
|
3809
|
-
if (targetIndex !== index) {
|
|
3810
|
-
const temp = this.nodes[index];
|
|
3811
|
-
this.nodes[index] = this.nodes[targetIndex];
|
|
3812
|
-
this.nodes[targetIndex] = temp;
|
|
3813
|
-
this.sinkDown(targetIndex);
|
|
4145
|
+
_bubbleUp(index) {
|
|
4146
|
+
const element = this.elements[index];
|
|
4147
|
+
while (index > 0) {
|
|
4148
|
+
const parent = index - 1 >> 1;
|
|
4149
|
+
const parentItem = this.elements[parent];
|
|
4150
|
+
if (this._comparator(parentItem, element) <= 0)
|
|
4151
|
+
break;
|
|
4152
|
+
this.elements[index] = parentItem;
|
|
4153
|
+
index = parent;
|
|
3814
4154
|
}
|
|
4155
|
+
this.elements[index] = element;
|
|
3815
4156
|
}
|
|
3816
4157
|
/**
|
|
3817
4158
|
* Time Complexity: O(n)
|
|
3818
4159
|
* Space Complexity: O(1)
|
|
3819
4160
|
*/
|
|
3820
4161
|
/**
|
|
3821
|
-
* Time Complexity: O(n)
|
|
4162
|
+
* Time Complexity: O(log n)
|
|
3822
4163
|
* Space Complexity: O(1)
|
|
3823
4164
|
*
|
|
3824
|
-
*
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
4165
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
4166
|
+
* @param index - The index from which to start sinking.
|
|
4167
|
+
* @param halfLength
|
|
4168
|
+
*/
|
|
4169
|
+
_sinkDown(index, halfLength) {
|
|
4170
|
+
const element = this.elements[index];
|
|
4171
|
+
while (index < halfLength) {
|
|
4172
|
+
let left = index << 1 | 1;
|
|
4173
|
+
const right = left + 1;
|
|
4174
|
+
let minItem = this.elements[left];
|
|
4175
|
+
if (right < this.elements.length && this._comparator(minItem, this.elements[right]) > 0) {
|
|
4176
|
+
left = right;
|
|
4177
|
+
minItem = this.elements[right];
|
|
4178
|
+
}
|
|
4179
|
+
if (this._comparator(minItem, element) >= 0)
|
|
4180
|
+
break;
|
|
4181
|
+
this.elements[index] = minItem;
|
|
4182
|
+
index = left;
|
|
4183
|
+
}
|
|
4184
|
+
this.elements[index] = element;
|
|
3829
4185
|
}
|
|
3830
4186
|
};
|
|
3831
4187
|
var FibonacciHeapNode = class {
|
|
@@ -3929,22 +4285,22 @@ var dataStructureTyped = (() => {
|
|
|
3929
4285
|
return this.min ? this.min.element : void 0;
|
|
3930
4286
|
}
|
|
3931
4287
|
/**
|
|
3932
|
-
* Time Complexity: O(n), where n is the number of
|
|
4288
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
3933
4289
|
* Space Complexity: O(1)
|
|
3934
4290
|
*/
|
|
3935
4291
|
/**
|
|
3936
|
-
* Time Complexity: O(n), where n is the number of
|
|
4292
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
3937
4293
|
* Space Complexity: O(1)
|
|
3938
4294
|
*
|
|
3939
4295
|
* Get the size (number of elements) of the heap.
|
|
3940
4296
|
* @param {FibonacciHeapNode<E>} head - The head of the linked list.
|
|
3941
4297
|
* @protected
|
|
3942
|
-
* @returns FibonacciHeapNode<E>[] - An array containing the
|
|
4298
|
+
* @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
|
|
3943
4299
|
*/
|
|
3944
4300
|
consumeLinkedList(head) {
|
|
3945
|
-
const
|
|
4301
|
+
const elements = [];
|
|
3946
4302
|
if (!head)
|
|
3947
|
-
return
|
|
4303
|
+
return elements;
|
|
3948
4304
|
let node = head;
|
|
3949
4305
|
let flag = false;
|
|
3950
4306
|
while (true) {
|
|
@@ -3953,11 +4309,11 @@ var dataStructureTyped = (() => {
|
|
|
3953
4309
|
else if (node === head)
|
|
3954
4310
|
flag = true;
|
|
3955
4311
|
if (node) {
|
|
3956
|
-
|
|
4312
|
+
elements.push(node);
|
|
3957
4313
|
node = node.right;
|
|
3958
4314
|
}
|
|
3959
4315
|
}
|
|
3960
|
-
return
|
|
4316
|
+
return elements;
|
|
3961
4317
|
}
|
|
3962
4318
|
/**
|
|
3963
4319
|
* Time Complexity: O(1)
|
|
@@ -3977,11 +4333,11 @@ var dataStructureTyped = (() => {
|
|
|
3977
4333
|
}
|
|
3978
4334
|
}
|
|
3979
4335
|
/**
|
|
3980
|
-
* Time Complexity: O(log n), where n is the number of
|
|
4336
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3981
4337
|
* Space Complexity: O(1)
|
|
3982
4338
|
*/
|
|
3983
4339
|
/**
|
|
3984
|
-
* Time Complexity: O(log n), where n is the number of
|
|
4340
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3985
4341
|
* Space Complexity: O(1)
|
|
3986
4342
|
*
|
|
3987
4343
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -3991,11 +4347,11 @@ var dataStructureTyped = (() => {
|
|
|
3991
4347
|
return this.pop();
|
|
3992
4348
|
}
|
|
3993
4349
|
/**
|
|
3994
|
-
* Time Complexity: O(log n), where n is the number of
|
|
4350
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3995
4351
|
* Space Complexity: O(1)
|
|
3996
4352
|
*/
|
|
3997
4353
|
/**
|
|
3998
|
-
* Time Complexity: O(log n), where n is the number of
|
|
4354
|
+
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
3999
4355
|
* Space Complexity: O(1)
|
|
4000
4356
|
*
|
|
4001
4357
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -4006,8 +4362,8 @@ var dataStructureTyped = (() => {
|
|
|
4006
4362
|
return void 0;
|
|
4007
4363
|
const z = this.min;
|
|
4008
4364
|
if (z.child) {
|
|
4009
|
-
const
|
|
4010
|
-
for (const node of
|
|
4365
|
+
const elements = this.consumeLinkedList(z.child);
|
|
4366
|
+
for (const node of elements) {
|
|
4011
4367
|
this.mergeWithRoot(node);
|
|
4012
4368
|
node.parent = void 0;
|
|
4013
4369
|
}
|
|
@@ -4138,11 +4494,11 @@ var dataStructureTyped = (() => {
|
|
|
4138
4494
|
y.parent = x;
|
|
4139
4495
|
}
|
|
4140
4496
|
/**
|
|
4141
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
4497
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
4142
4498
|
* Space Complexity: O(n)
|
|
4143
4499
|
*/
|
|
4144
4500
|
/**
|
|
4145
|
-
* Time Complexity: O(n log n), where n is the number of
|
|
4501
|
+
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
4146
4502
|
* Space Complexity: O(n)
|
|
4147
4503
|
*
|
|
4148
4504
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
@@ -4150,9 +4506,9 @@ var dataStructureTyped = (() => {
|
|
|
4150
4506
|
*/
|
|
4151
4507
|
consolidate() {
|
|
4152
4508
|
const A = new Array(this.size);
|
|
4153
|
-
const
|
|
4509
|
+
const elements = this.consumeLinkedList(this.root);
|
|
4154
4510
|
let x, y, d, t;
|
|
4155
|
-
for (const node of
|
|
4511
|
+
for (const node of elements) {
|
|
4156
4512
|
x = node;
|
|
4157
4513
|
d = x.degree;
|
|
4158
4514
|
while (A[d]) {
|