data-structure-typed 1.54.3 → 2.0.1
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/benchmark/report.html +26 -2
- package/benchmark/report.json +292 -42
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
- package/dist/cjs/data-structures/base/linear-base.js +553 -0
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +16 -16
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js +46 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
- package/dist/cjs/data-structures/heap/heap.js +0 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
- package/dist/cjs/data-structures/queue/deque.js +269 -169
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +131 -40
- package/dist/cjs/data-structures/queue/queue.js +181 -50
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -11
- package/dist/cjs/data-structures/stack/stack.js +121 -10
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
- package/dist/cjs/data-structures/trie/trie.js +3 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
- package/dist/esm/data-structures/base/linear-base.js +549 -0
- package/dist/esm/data-structures/base/linear-base.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +50 -36
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +62 -0
- package/dist/esm/data-structures/binary-tree/binary-tree.js +67 -5
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/esm/data-structures/binary-tree/bst.js +14 -12
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +1 -7
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +210 -39
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +16 -16
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/esm/data-structures/hash/hash-map.js +46 -0
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +3 -11
- package/dist/esm/data-structures/heap/heap.js +0 -10
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +283 -170
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +130 -91
- package/dist/esm/data-structures/queue/deque.js +269 -169
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +131 -40
- package/dist/esm/data-structures/queue/queue.js +182 -51
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -11
- package/dist/esm/data-structures/stack/stack.js +121 -10
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +4 -3
- package/dist/esm/data-structures/trie/trie.js +3 -0
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/types/data-structures/base/base.d.ts +9 -4
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +4701 -0
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +4514 -0
- package/dist/individuals/binary-tree/avl-tree.mjs +4321 -0
- package/dist/individuals/binary-tree/binary-tree.mjs +3097 -0
- package/dist/individuals/binary-tree/bst.mjs +3858 -0
- package/dist/individuals/binary-tree/red-black-tree.mjs +4391 -0
- package/dist/individuals/binary-tree/tree-counter.mjs +4806 -0
- package/dist/individuals/binary-tree/tree-multi-map.mjs +4582 -0
- package/dist/individuals/graph/directed-graph.mjs +2910 -0
- package/dist/individuals/graph/undirected-graph.mjs +2745 -0
- package/dist/individuals/hash/hash-map.mjs +1040 -0
- package/dist/individuals/heap/heap.mjs +909 -0
- package/dist/individuals/heap/max-heap.mjs +671 -0
- package/dist/individuals/heap/min-heap.mjs +659 -0
- package/dist/individuals/linked-list/doubly-linked-list.mjs +1495 -0
- package/dist/individuals/linked-list/singly-linked-list.mjs +1479 -0
- package/dist/individuals/priority-queue/max-priority-queue.mjs +768 -0
- package/dist/individuals/priority-queue/min-priority-queue.mjs +757 -0
- package/dist/individuals/priority-queue/priority-queue.mjs +670 -0
- package/dist/individuals/queue/deque.mjs +1262 -0
- package/dist/individuals/queue/queue.mjs +1865 -0
- package/dist/individuals/stack/stack.mjs +415 -0
- package/dist/individuals/trie/trie.mjs +687 -0
- package/dist/umd/data-structure-typed.js +1260 -649
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +168 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +15 -14
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +165 -7
- package/test/unit/data-structures/graph/directed-graph.test.ts +37 -37
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +135 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +183 -12
- package/test/unit/data-structures/queue/deque.test.ts +241 -60
- package/test/unit/data-structures/queue/queue.test.ts +332 -19
- package/test/unit/data-structures/stack/stack.test.ts +165 -0
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
|
@@ -422,13 +422,6 @@ var dataStructureTyped = (() => {
|
|
|
422
422
|
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
423
423
|
}
|
|
424
424
|
}
|
|
425
|
-
/**
|
|
426
|
-
* The function returns the _toElementFn property, which is a function that converts a raw element to
|
|
427
|
-
* a specific type.
|
|
428
|
-
* @returns The function `get toElementFn()` is returning either a function that takes a raw element
|
|
429
|
-
* `rawElement` of type `R` and returns an element `E`, or `undefined` if no function is assigned to
|
|
430
|
-
* `_toElementFn`.
|
|
431
|
-
*/
|
|
432
425
|
get toElementFn() {
|
|
433
426
|
return this._toElementFn;
|
|
434
427
|
}
|
|
@@ -526,7 +519,7 @@ var dataStructureTyped = (() => {
|
|
|
526
519
|
*
|
|
527
520
|
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
528
521
|
* element that satisfies the provided callback function.
|
|
529
|
-
* @param
|
|
522
|
+
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
530
523
|
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
531
524
|
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
532
525
|
* current element matches the desired condition.
|
|
@@ -536,10 +529,10 @@ var dataStructureTyped = (() => {
|
|
|
536
529
|
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
537
530
|
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
538
531
|
*/
|
|
539
|
-
find(
|
|
532
|
+
find(predicate, thisArg) {
|
|
540
533
|
let index = 0;
|
|
541
534
|
for (const item of this) {
|
|
542
|
-
if (
|
|
535
|
+
if (predicate.call(thisArg, item, index++, this)) return item;
|
|
543
536
|
}
|
|
544
537
|
return;
|
|
545
538
|
}
|
|
@@ -573,13 +566,23 @@ var dataStructureTyped = (() => {
|
|
|
573
566
|
* all the elements in the array and applying the callback function to each element.
|
|
574
567
|
*/
|
|
575
568
|
reduce(callbackfn, initialValue) {
|
|
576
|
-
let accumulator = initialValue;
|
|
569
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
577
570
|
let index = 0;
|
|
578
571
|
for (const item of this) {
|
|
579
572
|
accumulator = callbackfn(accumulator, item, index++, this);
|
|
580
573
|
}
|
|
581
574
|
return accumulator;
|
|
582
575
|
}
|
|
576
|
+
/**
|
|
577
|
+
* Time Complexity: O(n)
|
|
578
|
+
* Space Complexity: O(n)
|
|
579
|
+
*
|
|
580
|
+
* The `toArray` function converts a linked list into an array.
|
|
581
|
+
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
582
|
+
*/
|
|
583
|
+
toArray() {
|
|
584
|
+
return [...this];
|
|
585
|
+
}
|
|
583
586
|
/**
|
|
584
587
|
* Time Complexity: O(n)
|
|
585
588
|
* Space Complexity: O(n)
|
|
@@ -1500,97 +1503,566 @@ var dataStructureTyped = (() => {
|
|
|
1500
1503
|
}
|
|
1501
1504
|
};
|
|
1502
1505
|
|
|
1503
|
-
// src/data-structures/
|
|
1504
|
-
var
|
|
1505
|
-
/**
|
|
1506
|
-
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
1507
|
-
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
1508
|
-
* will be stored in the node of a linked list.
|
|
1509
|
-
*/
|
|
1506
|
+
// src/data-structures/base/linear-base.ts
|
|
1507
|
+
var LinkedListNode = class {
|
|
1510
1508
|
constructor(value) {
|
|
1511
1509
|
__publicField(this, "_value");
|
|
1512
1510
|
__publicField(this, "_next");
|
|
1513
1511
|
this._value = value;
|
|
1514
1512
|
this._next = void 0;
|
|
1515
1513
|
}
|
|
1516
|
-
/**
|
|
1517
|
-
* The function returns the value of a protected variable.
|
|
1518
|
-
* @returns The value of the variable `_value` is being returned.
|
|
1519
|
-
*/
|
|
1520
1514
|
get value() {
|
|
1521
1515
|
return this._value;
|
|
1522
1516
|
}
|
|
1523
|
-
/**
|
|
1524
|
-
* The above function sets the value of a variable.
|
|
1525
|
-
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
1526
|
-
*/
|
|
1527
1517
|
set value(value) {
|
|
1528
1518
|
this._value = value;
|
|
1529
1519
|
}
|
|
1530
|
-
/**
|
|
1531
|
-
* The `next` function returns the next node in a singly linked list.
|
|
1532
|
-
* @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>`
|
|
1533
|
-
* object or `undefined`.
|
|
1534
|
-
*/
|
|
1535
1520
|
get next() {
|
|
1536
1521
|
return this._next;
|
|
1537
1522
|
}
|
|
1523
|
+
set next(value) {
|
|
1524
|
+
this._next = value;
|
|
1525
|
+
}
|
|
1526
|
+
};
|
|
1527
|
+
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
1528
|
+
/**
|
|
1529
|
+
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
1530
|
+
* if provided.
|
|
1531
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
1532
|
+
* constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
|
|
1533
|
+
* object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
|
|
1534
|
+
*/
|
|
1535
|
+
constructor(options) {
|
|
1536
|
+
super(options);
|
|
1537
|
+
__publicField(this, "_maxLen", -1);
|
|
1538
|
+
if (options) {
|
|
1539
|
+
const { maxLen } = options;
|
|
1540
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
get maxLen() {
|
|
1544
|
+
return this._maxLen;
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* Time Complexity: O(n)
|
|
1548
|
+
* Space Complexity: O(1)
|
|
1549
|
+
*
|
|
1550
|
+
* The function indexOf searches for a specified element starting from a given index in an array-like
|
|
1551
|
+
* object and returns the index of the first occurrence, or -1 if not found.
|
|
1552
|
+
* @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
|
|
1553
|
+
* element that you want to find within the array. The function will search for this element starting
|
|
1554
|
+
* from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
|
|
1555
|
+
* within the
|
|
1556
|
+
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
|
|
1557
|
+
* index at which to start searching for the `searchElement` within the array. If provided, the
|
|
1558
|
+
* search will begin at this index and continue to the end of the array. If `fromIndex` is not
|
|
1559
|
+
* specified, the default
|
|
1560
|
+
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
1561
|
+
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
1562
|
+
*/
|
|
1563
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
1564
|
+
if (this.length === 0) return -1;
|
|
1565
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
1566
|
+
if (fromIndex < 0) fromIndex = 0;
|
|
1567
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
1568
|
+
const element = this.at(i);
|
|
1569
|
+
if (element === searchElement) return i;
|
|
1570
|
+
}
|
|
1571
|
+
return -1;
|
|
1572
|
+
}
|
|
1538
1573
|
/**
|
|
1539
|
-
*
|
|
1540
|
-
*
|
|
1541
|
-
*
|
|
1542
|
-
* `
|
|
1574
|
+
* Time Complexity: O(n)
|
|
1575
|
+
* Space Complexity: O(1)
|
|
1576
|
+
*
|
|
1577
|
+
* The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
|
|
1578
|
+
* element in an array.
|
|
1579
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
|
|
1580
|
+
* last index of within the array. The `lastIndexOf` method will search the array starting from the
|
|
1581
|
+
* `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
|
|
1582
|
+
* of the
|
|
1583
|
+
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
1584
|
+
* index at which to start searching for the `searchElement` in the array. By default, it starts
|
|
1585
|
+
* searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
|
|
1586
|
+
* provided
|
|
1587
|
+
* @returns The last index of the `searchElement` in the array is being returned. If the
|
|
1588
|
+
* `searchElement` is not found in the array, -1 is returned.
|
|
1589
|
+
*/
|
|
1590
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
1591
|
+
if (this.length === 0) return -1;
|
|
1592
|
+
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
1593
|
+
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
1594
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
1595
|
+
const element = this.at(i);
|
|
1596
|
+
if (element === searchElement) return i;
|
|
1597
|
+
}
|
|
1598
|
+
return -1;
|
|
1599
|
+
}
|
|
1600
|
+
/**
|
|
1601
|
+
* Time Complexity: O(n)
|
|
1602
|
+
* Space Complexity: O(1)
|
|
1603
|
+
*
|
|
1604
|
+
* The `findIndex` function iterates over an array and returns the index of the first element that
|
|
1605
|
+
* satisfies the provided predicate function.
|
|
1606
|
+
* @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
|
|
1607
|
+
* that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
|
|
1608
|
+
* value indicating whether the current element satisfies the condition being checked for.
|
|
1609
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
|
|
1610
|
+
* parameter that specifies the value to use as `this` when executing the `predicate` function. If
|
|
1611
|
+
* provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
|
|
1612
|
+
* @returns The `findIndex` method is returning the index of the first element in the array that
|
|
1613
|
+
* satisfies the provided predicate function. If no such element is found, it returns -1.
|
|
1614
|
+
*/
|
|
1615
|
+
findIndex(predicate, thisArg) {
|
|
1616
|
+
for (let i = 0; i < this.length; i++) {
|
|
1617
|
+
const item = this.at(i);
|
|
1618
|
+
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
1619
|
+
}
|
|
1620
|
+
return -1;
|
|
1621
|
+
}
|
|
1622
|
+
/**
|
|
1623
|
+
* Time Complexity: O(n + m)
|
|
1624
|
+
* Space Complexity: O(n + m)
|
|
1625
|
+
*
|
|
1626
|
+
* The `concat` function in TypeScript concatenates multiple items into a new list, handling both
|
|
1627
|
+
* individual elements and instances of `LinearBase`.
|
|
1628
|
+
* @param {(E | this)[]} items - The `concat` method takes in an array of items, where
|
|
1629
|
+
* each item can be either of type `E` or an instance of `LinearBase<E, R>`.
|
|
1630
|
+
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
1631
|
+
* items passed as arguments concatenated to it.
|
|
1632
|
+
*/
|
|
1633
|
+
concat(...items) {
|
|
1634
|
+
const newList = this.clone();
|
|
1635
|
+
for (const item of items) {
|
|
1636
|
+
if (item instanceof _LinearBase) {
|
|
1637
|
+
newList.pushMany(item);
|
|
1638
|
+
} else {
|
|
1639
|
+
newList.push(item);
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
return newList;
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Time Complexity: O(n log n)
|
|
1646
|
+
* Space Complexity: O(n)
|
|
1647
|
+
*
|
|
1648
|
+
* The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
|
|
1649
|
+
* function.
|
|
1650
|
+
* @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
|
|
1651
|
+
* two elements `a` and `b` as input and returns a number indicating their relative order. If the
|
|
1652
|
+
* returned value is negative, `a` comes before `b`. If the returned value is positive, `
|
|
1653
|
+
* @returns The `sort` method is returning the instance of the object on which it is called (this),
|
|
1654
|
+
* after sorting the elements based on the provided comparison function (compareFn).
|
|
1655
|
+
*/
|
|
1656
|
+
sort(compareFn) {
|
|
1657
|
+
const arr = this.toArray();
|
|
1658
|
+
arr.sort(compareFn);
|
|
1659
|
+
this.clear();
|
|
1660
|
+
for (const item of arr) this.push(item);
|
|
1661
|
+
return this;
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* Time Complexity: O(n + m)
|
|
1665
|
+
* Space Complexity: O(m)
|
|
1666
|
+
*
|
|
1667
|
+
* The `splice` function in TypeScript removes elements from an array and optionally inserts new
|
|
1668
|
+
* elements at the specified index.
|
|
1669
|
+
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
1670
|
+
* to start modifying the array. If `start` is a negative number, it will count from the end of the
|
|
1671
|
+
* array.
|
|
1672
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
1673
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
1674
|
+
* `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
|
|
1675
|
+
* at the `start`
|
|
1676
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
1677
|
+
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
1678
|
+
* and you can pass multiple elements separated by commas. The `splice` method will insert these
|
|
1679
|
+
* items into the array at the
|
|
1680
|
+
* @returns The `splice` method returns a list of elements that were removed from the original list
|
|
1681
|
+
* during the operation.
|
|
1682
|
+
*/
|
|
1683
|
+
splice(start, deleteCount = 0, ...items) {
|
|
1684
|
+
const removedList = this._createInstance();
|
|
1685
|
+
start = start < 0 ? this.length + start : start;
|
|
1686
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
1687
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
1688
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
1689
|
+
const removed = this.deleteAt(start);
|
|
1690
|
+
if (removed !== void 0) {
|
|
1691
|
+
removedList.push(removed);
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
for (let i = 0; i < items.length; i++) {
|
|
1695
|
+
this.addAt(start + i, items[i]);
|
|
1696
|
+
}
|
|
1697
|
+
return removedList;
|
|
1698
|
+
}
|
|
1699
|
+
/**
|
|
1700
|
+
* Time Complexity: O(n)
|
|
1701
|
+
* Space Complexity: O(1)
|
|
1702
|
+
*
|
|
1703
|
+
* The `join` function in TypeScript returns a string by joining the elements of an array with a
|
|
1704
|
+
* specified separator.
|
|
1705
|
+
* @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
|
|
1706
|
+
* or characters that will be used to separate each element when joining them into a single string.
|
|
1707
|
+
* By default, the separator is set to a comma (`,`), but you can provide a different separator if
|
|
1708
|
+
* needed.
|
|
1709
|
+
* @returns The `join` method is being returned, which takes an optional `separator` parameter
|
|
1710
|
+
* (defaulting to a comma) and returns a string created by joining all elements of the array after
|
|
1711
|
+
* converting it to an array.
|
|
1712
|
+
*/
|
|
1713
|
+
join(separator = ",") {
|
|
1714
|
+
return this.toArray().join(separator);
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Time Complexity: O(n)
|
|
1718
|
+
* Space Complexity: O(n)
|
|
1719
|
+
*
|
|
1720
|
+
* The function `toReversedArray` takes an array and returns a new array with its elements in reverse
|
|
1721
|
+
* order.
|
|
1722
|
+
* @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
|
|
1723
|
+
* order.
|
|
1724
|
+
*/
|
|
1725
|
+
toReversedArray() {
|
|
1726
|
+
const array = [];
|
|
1727
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1728
|
+
array.push(this.at(i));
|
|
1729
|
+
}
|
|
1730
|
+
return array;
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Time Complexity: O(n)
|
|
1734
|
+
* Space Complexity: O(1)
|
|
1735
|
+
*
|
|
1736
|
+
* The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
|
|
1737
|
+
* callback function to each element, accumulating a single result.
|
|
1738
|
+
* @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
|
|
1739
|
+
* be called on each element in the array from right to left. It takes four arguments:
|
|
1740
|
+
* @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
|
|
1741
|
+
* optional parameter that specifies the initial value of the accumulator. If provided, the
|
|
1742
|
+
* `accumulator` will start with this initial value before iterating over the elements of the array.
|
|
1743
|
+
* If `initialValue` is not provided, the accumulator will
|
|
1744
|
+
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
1745
|
+
* callback function to each element in the array from right to left.
|
|
1543
1746
|
*/
|
|
1747
|
+
reduceRight(callbackfn, initialValue) {
|
|
1748
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
1749
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
1750
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
1751
|
+
}
|
|
1752
|
+
return accumulator;
|
|
1753
|
+
}
|
|
1754
|
+
/**
|
|
1755
|
+
* Time Complexity: O(m)
|
|
1756
|
+
* Space Complexity: O(m)
|
|
1757
|
+
*
|
|
1758
|
+
* The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
|
|
1759
|
+
* the original instance based on the specified start and end indices.
|
|
1760
|
+
* @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
|
|
1761
|
+
* which to begin extracting elements from an array-like object. If no `start` parameter is provided,
|
|
1762
|
+
* the default value is 0, meaning the extraction will start from the beginning of the array.
|
|
1763
|
+
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
1764
|
+
* end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
|
|
1765
|
+
* array (i.e., `this.length`).
|
|
1766
|
+
* @returns The `slice` method is returning a new instance of the object with elements sliced from
|
|
1767
|
+
* the specified start index (default is 0) to the specified end index (default is the length of the
|
|
1768
|
+
* object).
|
|
1769
|
+
*/
|
|
1770
|
+
slice(start = 0, end = this.length) {
|
|
1771
|
+
start = start < 0 ? this.length + start : start;
|
|
1772
|
+
end = end < 0 ? this.length + end : end;
|
|
1773
|
+
const newList = this._createInstance();
|
|
1774
|
+
for (let i = start; i < end; i++) {
|
|
1775
|
+
newList.push(this.at(i));
|
|
1776
|
+
}
|
|
1777
|
+
return newList;
|
|
1778
|
+
}
|
|
1779
|
+
/**
|
|
1780
|
+
* Time Complexity: O(n)
|
|
1781
|
+
* Space Complexity: O(1)
|
|
1782
|
+
*
|
|
1783
|
+
* The `fill` function in TypeScript fills a specified range in an array-like object with a given
|
|
1784
|
+
* value.
|
|
1785
|
+
* @param {E} value - The `value` parameter in the `fill` method represents the element that will be
|
|
1786
|
+
* used to fill the specified range in the array.
|
|
1787
|
+
* @param [start=0] - The `start` parameter specifies the index at which to start filling the array
|
|
1788
|
+
* with the specified value. If not provided, it defaults to 0, indicating the beginning of the
|
|
1789
|
+
* array.
|
|
1790
|
+
* @param end - The `end` parameter in the `fill` function represents the index at which the filling
|
|
1791
|
+
* of values should stop. It specifies the end of the range within the array where the `value` should
|
|
1792
|
+
* be filled.
|
|
1793
|
+
* @returns The `fill` method is returning the modified object (`this`) after filling the specified
|
|
1794
|
+
* range with the provided value.
|
|
1795
|
+
*/
|
|
1796
|
+
fill(value, start = 0, end = this.length) {
|
|
1797
|
+
start = start < 0 ? this.length + start : start;
|
|
1798
|
+
end = end < 0 ? this.length + end : end;
|
|
1799
|
+
if (start < 0) start = 0;
|
|
1800
|
+
if (end > this.length) end = this.length;
|
|
1801
|
+
if (start >= end) return this;
|
|
1802
|
+
for (let i = start; i < end; i++) {
|
|
1803
|
+
this.setAt(i, value);
|
|
1804
|
+
}
|
|
1805
|
+
return this;
|
|
1806
|
+
}
|
|
1807
|
+
};
|
|
1808
|
+
var LinearLinkedBase = class extends LinearBase {
|
|
1809
|
+
/**
|
|
1810
|
+
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
1811
|
+
* if provided and valid.
|
|
1812
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
1813
|
+
* constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
|
|
1814
|
+
* `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
|
|
1815
|
+
*/
|
|
1816
|
+
constructor(options) {
|
|
1817
|
+
super(options);
|
|
1818
|
+
if (options) {
|
|
1819
|
+
const { maxLen } = options;
|
|
1820
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Time Complexity: O(n)
|
|
1825
|
+
* Space Complexity: O(1)
|
|
1826
|
+
*
|
|
1827
|
+
* The function overrides the indexOf method to improve performance by searching for an element in a
|
|
1828
|
+
* custom array implementation starting from a specified index.
|
|
1829
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
1830
|
+
* within the array. The `indexOf` method will return the index of the first occurrence of this
|
|
1831
|
+
* element within the array.
|
|
1832
|
+
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
1833
|
+
* index in the array at which to start the search for the `searchElement`. If provided, the search
|
|
1834
|
+
* will begin at the specified index and continue to the end of the array. If not provided, the
|
|
1835
|
+
* search will start at index
|
|
1836
|
+
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
1837
|
+
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
1838
|
+
*/
|
|
1839
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
1840
|
+
const iterator = this._getIterator();
|
|
1841
|
+
let current = iterator.next();
|
|
1842
|
+
let index = 0;
|
|
1843
|
+
while (index < fromIndex) {
|
|
1844
|
+
current = iterator.next();
|
|
1845
|
+
index++;
|
|
1846
|
+
}
|
|
1847
|
+
while (!current.done) {
|
|
1848
|
+
if (current.value === searchElement) return index;
|
|
1849
|
+
current = iterator.next();
|
|
1850
|
+
index++;
|
|
1851
|
+
}
|
|
1852
|
+
return -1;
|
|
1853
|
+
}
|
|
1854
|
+
/**
|
|
1855
|
+
* Time Complexity: O(n)
|
|
1856
|
+
* Space Complexity: O(1)
|
|
1857
|
+
*
|
|
1858
|
+
* The function overrides the lastIndexOf method in TypeScript to improve performance by searching
|
|
1859
|
+
* for an element in reverse order starting from a specified index.
|
|
1860
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find
|
|
1861
|
+
* within the array. The `lastIndexOf` method searches the array for this element starting from the
|
|
1862
|
+
* end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
|
|
1863
|
+
* occurrence of the element
|
|
1864
|
+
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
1865
|
+
* index at which to start searching for the `searchElement` in the array. If provided, the search
|
|
1866
|
+
* will begin at this index and move towards the beginning of the array. If not provided, the search
|
|
1867
|
+
* will start at the
|
|
1868
|
+
* @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
|
|
1869
|
+
* from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
|
|
1870
|
+
* reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
|
|
1871
|
+
* occurrence of the `searchElement` if found, or -1 if not found.
|
|
1872
|
+
*/
|
|
1873
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
1874
|
+
const iterator = this._getReverseIterator();
|
|
1875
|
+
let current = iterator.next();
|
|
1876
|
+
let index = this.length - 1;
|
|
1877
|
+
while (index > fromIndex) {
|
|
1878
|
+
current = iterator.next();
|
|
1879
|
+
index--;
|
|
1880
|
+
}
|
|
1881
|
+
while (!current.done) {
|
|
1882
|
+
if (current.value === searchElement) return index;
|
|
1883
|
+
current = iterator.next();
|
|
1884
|
+
index--;
|
|
1885
|
+
}
|
|
1886
|
+
return -1;
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* Time Complexity: O(n + m)
|
|
1890
|
+
* Space Complexity: O(n + m)
|
|
1891
|
+
*
|
|
1892
|
+
* The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
|
|
1893
|
+
* list, handling both individual elements and instances of `LinearBase`.
|
|
1894
|
+
* @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
|
|
1895
|
+
* number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
|
|
1896
|
+
* current list and returns a new list with the concatenated items.
|
|
1897
|
+
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
1898
|
+
* items passed as arguments concatenated to it.
|
|
1899
|
+
*/
|
|
1900
|
+
concat(...items) {
|
|
1901
|
+
const newList = this.clone();
|
|
1902
|
+
for (const item of items) {
|
|
1903
|
+
if (item instanceof LinearBase) {
|
|
1904
|
+
newList.pushMany(item);
|
|
1905
|
+
} else {
|
|
1906
|
+
newList.push(item);
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
return newList;
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* Time Complexity: O(m)
|
|
1913
|
+
* Space Complexity: O(m)
|
|
1914
|
+
*
|
|
1915
|
+
* The `slice` method is overridden to improve performance by creating a new instance and iterating
|
|
1916
|
+
* through the array to extract a subset based on the specified start and end indices.
|
|
1917
|
+
* @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
|
|
1918
|
+
* which to begin extracting elements from the array. If no `start` parameter is provided, the
|
|
1919
|
+
* default value is 0, indicating that extraction should start from the beginning of the array.
|
|
1920
|
+
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
1921
|
+
* end the slicing of the array. If not provided, it defaults to the length of the array.
|
|
1922
|
+
* @returns The `slice` method is returning a new instance of the array implementation with elements
|
|
1923
|
+
* sliced from the original array based on the `start` and `end` parameters.
|
|
1924
|
+
*/
|
|
1925
|
+
slice(start = 0, end = this.length) {
|
|
1926
|
+
start = start < 0 ? this.length + start : start;
|
|
1927
|
+
end = end < 0 ? this.length + end : end;
|
|
1928
|
+
const newList = this._createInstance();
|
|
1929
|
+
const iterator = this._getIterator();
|
|
1930
|
+
let current = iterator.next();
|
|
1931
|
+
let c = 0;
|
|
1932
|
+
while (c < start) {
|
|
1933
|
+
current = iterator.next();
|
|
1934
|
+
c++;
|
|
1935
|
+
}
|
|
1936
|
+
for (let i = start; i < end; i++) {
|
|
1937
|
+
newList.push(current.value);
|
|
1938
|
+
current = iterator.next();
|
|
1939
|
+
}
|
|
1940
|
+
return newList;
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Time Complexity: O(n + m)
|
|
1944
|
+
* Space Complexity: O(m)
|
|
1945
|
+
*
|
|
1946
|
+
* The function overrides the splice method to handle deletion and insertion of elements in a data
|
|
1947
|
+
* structure while returning the removed elements.
|
|
1948
|
+
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
1949
|
+
* to start modifying the array.
|
|
1950
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
1951
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
1952
|
+
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
1953
|
+
* elements can still be inserted at
|
|
1954
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
1955
|
+
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
1956
|
+
* and there can be multiple elements passed as arguments to be inserted into the array.
|
|
1957
|
+
* @returns The `splice` method is returning a new instance of the data structure that was modified
|
|
1958
|
+
* by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
|
|
1959
|
+
* elements provided in the `items` array.
|
|
1960
|
+
*/
|
|
1961
|
+
splice(start, deleteCount = 0, ...items) {
|
|
1962
|
+
const removedList = this._createInstance();
|
|
1963
|
+
start = start < 0 ? this.length + start : start;
|
|
1964
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
1965
|
+
deleteCount = Math.max(0, deleteCount);
|
|
1966
|
+
let currentIndex = 0;
|
|
1967
|
+
let currentNode = void 0;
|
|
1968
|
+
let previousNode = void 0;
|
|
1969
|
+
const iterator = this._getNodeIterator();
|
|
1970
|
+
for (const node of iterator) {
|
|
1971
|
+
if (currentIndex === start) {
|
|
1972
|
+
currentNode = node;
|
|
1973
|
+
break;
|
|
1974
|
+
}
|
|
1975
|
+
previousNode = node;
|
|
1976
|
+
currentIndex++;
|
|
1977
|
+
}
|
|
1978
|
+
for (let i = 0; i < deleteCount && currentNode; i++) {
|
|
1979
|
+
removedList.push(currentNode.value);
|
|
1980
|
+
const nextNode = currentNode.next;
|
|
1981
|
+
this.delete(currentNode);
|
|
1982
|
+
currentNode = nextNode;
|
|
1983
|
+
}
|
|
1984
|
+
for (let i = 0; i < items.length; i++) {
|
|
1985
|
+
if (previousNode) {
|
|
1986
|
+
this.addAfter(previousNode, items[i]);
|
|
1987
|
+
previousNode = previousNode.next;
|
|
1988
|
+
} else {
|
|
1989
|
+
this.addAt(0, items[i]);
|
|
1990
|
+
previousNode = this._getNodeIterator().next().value;
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
return removedList;
|
|
1994
|
+
}
|
|
1995
|
+
/**
|
|
1996
|
+
* Time Complexity: O(n)
|
|
1997
|
+
* Space Complexity: O(1)
|
|
1998
|
+
*
|
|
1999
|
+
* The function `reduceRight` iterates over an array in reverse order and applies a callback function
|
|
2000
|
+
* to each element, accumulating a single result.
|
|
2001
|
+
* @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
|
|
2002
|
+
* of the array from right to left. It takes four arguments:
|
|
2003
|
+
* @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
|
|
2004
|
+
* initial accumulator value in the reduce operation. If provided, the reduce operation starts with
|
|
2005
|
+
* this initial value and iterates over the elements of the array, applying the callback function to
|
|
2006
|
+
* each element and the current accumulator value. If `initial
|
|
2007
|
+
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
2008
|
+
* callback function to each element in the array from right to left.
|
|
2009
|
+
*/
|
|
2010
|
+
reduceRight(callbackfn, initialValue) {
|
|
2011
|
+
let accumulator = initialValue != null ? initialValue : 0;
|
|
2012
|
+
let index = this.length - 1;
|
|
2013
|
+
for (const item of this._getReverseIterator()) {
|
|
2014
|
+
accumulator = callbackfn(accumulator, item, index--, this);
|
|
2015
|
+
}
|
|
2016
|
+
return accumulator;
|
|
2017
|
+
}
|
|
2018
|
+
};
|
|
2019
|
+
|
|
2020
|
+
// src/data-structures/linked-list/singly-linked-list.ts
|
|
2021
|
+
var SinglyLinkedListNode = class extends LinkedListNode {
|
|
2022
|
+
/**
|
|
2023
|
+
* The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
|
|
2024
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
2025
|
+
* will be stored in the node of a linked list.
|
|
2026
|
+
*/
|
|
2027
|
+
constructor(value) {
|
|
2028
|
+
super(value);
|
|
2029
|
+
__publicField(this, "_next");
|
|
2030
|
+
this._value = value;
|
|
2031
|
+
this._next = void 0;
|
|
2032
|
+
}
|
|
2033
|
+
get next() {
|
|
2034
|
+
return this._next;
|
|
2035
|
+
}
|
|
1544
2036
|
set next(value) {
|
|
1545
2037
|
this._next = value;
|
|
1546
2038
|
}
|
|
1547
2039
|
};
|
|
1548
|
-
var SinglyLinkedList = class _SinglyLinkedList extends
|
|
2040
|
+
var SinglyLinkedList = class _SinglyLinkedList extends LinearLinkedBase {
|
|
1549
2041
|
constructor(elements = [], options) {
|
|
1550
2042
|
super(options);
|
|
1551
2043
|
__publicField(this, "_head");
|
|
1552
2044
|
__publicField(this, "_tail");
|
|
1553
|
-
__publicField(this, "
|
|
2045
|
+
__publicField(this, "_length", 0);
|
|
2046
|
+
if (options) {
|
|
2047
|
+
}
|
|
1554
2048
|
this.pushMany(elements);
|
|
1555
2049
|
}
|
|
1556
|
-
/**
|
|
1557
|
-
* The `head` function returns the first node of a singly linked list.
|
|
1558
|
-
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
1559
|
-
*/
|
|
1560
2050
|
get head() {
|
|
1561
2051
|
return this._head;
|
|
1562
2052
|
}
|
|
1563
|
-
/**
|
|
1564
|
-
* The `tail` function returns the last node of a singly linked list.
|
|
1565
|
-
* @returns The method is returning either a SinglyLinkedListNode object or undefined.
|
|
1566
|
-
*/
|
|
1567
2053
|
get tail() {
|
|
1568
2054
|
return this._tail;
|
|
1569
2055
|
}
|
|
1570
|
-
/**
|
|
1571
|
-
* The above function returns the value of the first element in a linked list, or undefined if the
|
|
1572
|
-
* list is empty.
|
|
1573
|
-
* @returns The value of the first node in the linked list, or undefined if the linked list is empty.
|
|
1574
|
-
*/
|
|
1575
2056
|
get first() {
|
|
1576
2057
|
var _a;
|
|
1577
2058
|
return (_a = this.head) == null ? void 0 : _a.value;
|
|
1578
2059
|
}
|
|
1579
|
-
/**
|
|
1580
|
-
* The function returns the value of the last element in a linked list, or undefined if the list is
|
|
1581
|
-
* empty.
|
|
1582
|
-
* @returns The value of the last node in the linked list, or undefined if the linked list is empty.
|
|
1583
|
-
*/
|
|
1584
2060
|
get last() {
|
|
1585
2061
|
var _a;
|
|
1586
2062
|
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
1587
2063
|
}
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
* @returns The size of the object, which is a number.
|
|
1591
|
-
*/
|
|
1592
|
-
get size() {
|
|
1593
|
-
return this._size;
|
|
2064
|
+
get length() {
|
|
2065
|
+
return this._length;
|
|
1594
2066
|
}
|
|
1595
2067
|
/**
|
|
1596
2068
|
* Time Complexity: O(n)
|
|
@@ -1626,7 +2098,8 @@ var dataStructureTyped = (() => {
|
|
|
1626
2098
|
this.tail.next = newNode;
|
|
1627
2099
|
this._tail = newNode;
|
|
1628
2100
|
}
|
|
1629
|
-
this.
|
|
2101
|
+
this._length++;
|
|
2102
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
1630
2103
|
return true;
|
|
1631
2104
|
}
|
|
1632
2105
|
/**
|
|
@@ -1643,7 +2116,7 @@ var dataStructureTyped = (() => {
|
|
|
1643
2116
|
const value2 = this.head.value;
|
|
1644
2117
|
this._head = void 0;
|
|
1645
2118
|
this._tail = void 0;
|
|
1646
|
-
this.
|
|
2119
|
+
this._length--;
|
|
1647
2120
|
return value2;
|
|
1648
2121
|
}
|
|
1649
2122
|
let current = this.head;
|
|
@@ -1653,7 +2126,7 @@ var dataStructureTyped = (() => {
|
|
|
1653
2126
|
const value = this.tail.value;
|
|
1654
2127
|
current.next = void 0;
|
|
1655
2128
|
this._tail = current;
|
|
1656
|
-
this.
|
|
2129
|
+
this._length--;
|
|
1657
2130
|
return value;
|
|
1658
2131
|
}
|
|
1659
2132
|
/**
|
|
@@ -1667,7 +2140,7 @@ var dataStructureTyped = (() => {
|
|
|
1667
2140
|
if (!this.head) return void 0;
|
|
1668
2141
|
const removedNode = this.head;
|
|
1669
2142
|
this._head = this.head.next;
|
|
1670
|
-
this.
|
|
2143
|
+
this._length--;
|
|
1671
2144
|
return removedNode.value;
|
|
1672
2145
|
}
|
|
1673
2146
|
/**
|
|
@@ -1690,7 +2163,7 @@ var dataStructureTyped = (() => {
|
|
|
1690
2163
|
newNode.next = this.head;
|
|
1691
2164
|
this._head = newNode;
|
|
1692
2165
|
}
|
|
1693
|
-
this.
|
|
2166
|
+
this._length++;
|
|
1694
2167
|
return true;
|
|
1695
2168
|
}
|
|
1696
2169
|
/**
|
|
@@ -1772,7 +2245,7 @@ var dataStructureTyped = (() => {
|
|
|
1772
2245
|
* `undefined` if the index is out of bounds.
|
|
1773
2246
|
*/
|
|
1774
2247
|
at(index) {
|
|
1775
|
-
if (index < 0 || index >= this.
|
|
2248
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
1776
2249
|
let current = this.head;
|
|
1777
2250
|
for (let i = 0; i < index; i++) {
|
|
1778
2251
|
current = current.next;
|
|
@@ -1823,20 +2296,23 @@ var dataStructureTyped = (() => {
|
|
|
1823
2296
|
* bounds.
|
|
1824
2297
|
*/
|
|
1825
2298
|
deleteAt(index) {
|
|
1826
|
-
if (index < 0 || index >= this.
|
|
2299
|
+
if (index < 0 || index >= this._length) return;
|
|
2300
|
+
let deleted;
|
|
1827
2301
|
if (index === 0) {
|
|
2302
|
+
deleted = this.first;
|
|
1828
2303
|
this.shift();
|
|
1829
|
-
return
|
|
2304
|
+
return deleted;
|
|
1830
2305
|
}
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
2306
|
+
const targetNode = this.getNodeAt(index);
|
|
2307
|
+
const prevNode = this._getPrevNode(targetNode);
|
|
2308
|
+
if (prevNode && targetNode) {
|
|
2309
|
+
deleted = targetNode.value;
|
|
2310
|
+
prevNode.next = targetNode.next;
|
|
2311
|
+
if (targetNode === this.tail) this._tail = prevNode;
|
|
2312
|
+
this._length--;
|
|
2313
|
+
return deleted;
|
|
1834
2314
|
}
|
|
1835
|
-
|
|
1836
|
-
const removedNode = prevNode.next;
|
|
1837
|
-
prevNode.next = removedNode.next;
|
|
1838
|
-
this._size--;
|
|
1839
|
-
return true;
|
|
2315
|
+
return;
|
|
1840
2316
|
}
|
|
1841
2317
|
/**
|
|
1842
2318
|
* Time Complexity: O(n)
|
|
@@ -1849,34 +2325,19 @@ var dataStructureTyped = (() => {
|
|
|
1849
2325
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
1850
2326
|
*/
|
|
1851
2327
|
delete(elementOrNode) {
|
|
1852
|
-
if (elementOrNode === void 0) return false;
|
|
1853
|
-
|
|
1854
|
-
if (
|
|
1855
|
-
|
|
2328
|
+
if (elementOrNode === void 0 || !this.head) return false;
|
|
2329
|
+
const node = this.isNode(elementOrNode) ? elementOrNode : this.getNode(elementOrNode);
|
|
2330
|
+
if (!node) return false;
|
|
2331
|
+
const prevNode = this._getPrevNode(node);
|
|
2332
|
+
if (!prevNode) {
|
|
2333
|
+
this._head = node.next;
|
|
2334
|
+
if (node === this.tail) this._tail = void 0;
|
|
1856
2335
|
} else {
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
let current = this.head, prev = void 0;
|
|
1860
|
-
while (current) {
|
|
1861
|
-
if (current.value === value) {
|
|
1862
|
-
if (prev === void 0) {
|
|
1863
|
-
this._head = current.next;
|
|
1864
|
-
if (current === this.tail) {
|
|
1865
|
-
this._tail = void 0;
|
|
1866
|
-
}
|
|
1867
|
-
} else {
|
|
1868
|
-
prev.next = current.next;
|
|
1869
|
-
if (current === this.tail) {
|
|
1870
|
-
this._tail = prev;
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
this._size--;
|
|
1874
|
-
return true;
|
|
1875
|
-
}
|
|
1876
|
-
prev = current;
|
|
1877
|
-
current = current.next;
|
|
2336
|
+
prevNode.next = node.next;
|
|
2337
|
+
if (node === this.tail) this._tail = prevNode;
|
|
1878
2338
|
}
|
|
1879
|
-
|
|
2339
|
+
this._length--;
|
|
2340
|
+
return true;
|
|
1880
2341
|
}
|
|
1881
2342
|
/**
|
|
1882
2343
|
* Time Complexity: O(n)
|
|
@@ -1894,12 +2355,12 @@ var dataStructureTyped = (() => {
|
|
|
1894
2355
|
* successfully added at the specified index, and `false` if the index is out of bounds.
|
|
1895
2356
|
*/
|
|
1896
2357
|
addAt(index, newElementOrNode) {
|
|
1897
|
-
if (index < 0 || index > this.
|
|
2358
|
+
if (index < 0 || index > this._length) return false;
|
|
1898
2359
|
if (index === 0) {
|
|
1899
2360
|
this.unshift(newElementOrNode);
|
|
1900
2361
|
return true;
|
|
1901
2362
|
}
|
|
1902
|
-
if (index === this.
|
|
2363
|
+
if (index === this._length) {
|
|
1903
2364
|
this.push(newElementOrNode);
|
|
1904
2365
|
return true;
|
|
1905
2366
|
}
|
|
@@ -1907,9 +2368,31 @@ var dataStructureTyped = (() => {
|
|
|
1907
2368
|
const prevNode = this.getNodeAt(index - 1);
|
|
1908
2369
|
newNode.next = prevNode.next;
|
|
1909
2370
|
prevNode.next = newNode;
|
|
1910
|
-
this.
|
|
2371
|
+
this._length++;
|
|
1911
2372
|
return true;
|
|
1912
2373
|
}
|
|
2374
|
+
/**
|
|
2375
|
+
* Time Complexity: O(n)
|
|
2376
|
+
* Space Complexity: O(1)
|
|
2377
|
+
*
|
|
2378
|
+
* The function setAt(index, value) updates the value at a specified index in a data structure if the
|
|
2379
|
+
* index exists.
|
|
2380
|
+
* @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
|
|
2381
|
+
* data structure where you want to set a new value.
|
|
2382
|
+
* @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
|
|
2383
|
+
* want to set at the specified index in the data structure.
|
|
2384
|
+
* @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
|
|
2385
|
+
* is successfully updated, and `false` if the index is out of bounds (i.e., the node at that index
|
|
2386
|
+
* does not exist).
|
|
2387
|
+
*/
|
|
2388
|
+
setAt(index, value) {
|
|
2389
|
+
const node = this.getNodeAt(index);
|
|
2390
|
+
if (node) {
|
|
2391
|
+
node.value = value;
|
|
2392
|
+
return true;
|
|
2393
|
+
}
|
|
2394
|
+
return false;
|
|
2395
|
+
}
|
|
1913
2396
|
/**
|
|
1914
2397
|
* Time Complexity: O(1)
|
|
1915
2398
|
* Space Complexity: O(1)
|
|
@@ -1919,7 +2402,7 @@ var dataStructureTyped = (() => {
|
|
|
1919
2402
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
1920
2403
|
*/
|
|
1921
2404
|
isEmpty() {
|
|
1922
|
-
return this.
|
|
2405
|
+
return this._length === 0;
|
|
1923
2406
|
}
|
|
1924
2407
|
/**
|
|
1925
2408
|
* Time Complexity: O(1)
|
|
@@ -1930,23 +2413,7 @@ var dataStructureTyped = (() => {
|
|
|
1930
2413
|
clear() {
|
|
1931
2414
|
this._head = void 0;
|
|
1932
2415
|
this._tail = void 0;
|
|
1933
|
-
this.
|
|
1934
|
-
}
|
|
1935
|
-
/**
|
|
1936
|
-
* Time Complexity: O(n)
|
|
1937
|
-
* Space Complexity: O(n)
|
|
1938
|
-
*
|
|
1939
|
-
* The `toArray` function converts a linked list into an array.
|
|
1940
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
1941
|
-
*/
|
|
1942
|
-
toArray() {
|
|
1943
|
-
const array = [];
|
|
1944
|
-
let current = this.head;
|
|
1945
|
-
while (current) {
|
|
1946
|
-
array.push(current.value);
|
|
1947
|
-
current = current.next;
|
|
1948
|
-
}
|
|
1949
|
-
return array;
|
|
2416
|
+
this._length = 0;
|
|
1950
2417
|
}
|
|
1951
2418
|
/**
|
|
1952
2419
|
* Time Complexity: O(n)
|
|
@@ -1969,32 +2436,6 @@ var dataStructureTyped = (() => {
|
|
|
1969
2436
|
[this._head, this._tail] = [this.tail, this.head];
|
|
1970
2437
|
return this;
|
|
1971
2438
|
}
|
|
1972
|
-
/**
|
|
1973
|
-
* Time Complexity: O(n)
|
|
1974
|
-
* Space Complexity: O(1)
|
|
1975
|
-
*
|
|
1976
|
-
* The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
|
|
1977
|
-
* list and returns its index if found.
|
|
1978
|
-
* @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1979
|
-
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
|
|
1980
|
-
* of the following types:
|
|
1981
|
-
* @returns The `indexOf` method returns the index of the first occurrence of the element that
|
|
1982
|
-
* matches the provided predicate in the singly linked list. If no matching element is found, it
|
|
1983
|
-
* returns -1.
|
|
1984
|
-
*/
|
|
1985
|
-
indexOf(elementNodeOrPredicate) {
|
|
1986
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1987
|
-
let index = 0;
|
|
1988
|
-
let current = this.head;
|
|
1989
|
-
while (current) {
|
|
1990
|
-
if (predicate(current)) {
|
|
1991
|
-
return index;
|
|
1992
|
-
}
|
|
1993
|
-
index++;
|
|
1994
|
-
current = current.next;
|
|
1995
|
-
}
|
|
1996
|
-
return -1;
|
|
1997
|
-
}
|
|
1998
2439
|
/**
|
|
1999
2440
|
* Time Complexity: O(n)
|
|
2000
2441
|
* Space Complexity: O(1)
|
|
@@ -2010,6 +2451,7 @@ var dataStructureTyped = (() => {
|
|
|
2010
2451
|
*/
|
|
2011
2452
|
getNode(elementNodeOrPredicate) {
|
|
2012
2453
|
if (elementNodeOrPredicate === void 0) return;
|
|
2454
|
+
if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
2013
2455
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2014
2456
|
let current = this.head;
|
|
2015
2457
|
while (current) {
|
|
@@ -2037,29 +2479,18 @@ var dataStructureTyped = (() => {
|
|
|
2037
2479
|
* unsuccessful.
|
|
2038
2480
|
*/
|
|
2039
2481
|
addBefore(existingElementOrNode, newElementOrNode) {
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2482
|
+
const existingNode = this.getNode(existingElementOrNode);
|
|
2483
|
+
if (!existingNode) return false;
|
|
2484
|
+
const prevNode = this._getPrevNode(existingNode);
|
|
2485
|
+
const newNode = this._ensureNode(newElementOrNode);
|
|
2486
|
+
if (!prevNode) {
|
|
2487
|
+
this.unshift(newNode);
|
|
2044
2488
|
} else {
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
this.unshift(newElementOrNode);
|
|
2049
|
-
return true;
|
|
2050
|
-
}
|
|
2051
|
-
let current = this.head;
|
|
2052
|
-
while (current.next) {
|
|
2053
|
-
if (current.next.value === existingValue) {
|
|
2054
|
-
const newNode = this._ensureNode(newElementOrNode);
|
|
2055
|
-
newNode.next = current.next;
|
|
2056
|
-
current.next = newNode;
|
|
2057
|
-
this._size++;
|
|
2058
|
-
return true;
|
|
2059
|
-
}
|
|
2060
|
-
current = current.next;
|
|
2489
|
+
prevNode.next = newNode;
|
|
2490
|
+
newNode.next = existingNode;
|
|
2491
|
+
this._length++;
|
|
2061
2492
|
}
|
|
2062
|
-
return
|
|
2493
|
+
return true;
|
|
2063
2494
|
}
|
|
2064
2495
|
/**
|
|
2065
2496
|
* Time Complexity: O(n)
|
|
@@ -2086,11 +2517,67 @@ var dataStructureTyped = (() => {
|
|
|
2086
2517
|
if (existingNode === this.tail) {
|
|
2087
2518
|
this._tail = newNode;
|
|
2088
2519
|
}
|
|
2089
|
-
this.
|
|
2520
|
+
this._length++;
|
|
2090
2521
|
return true;
|
|
2091
2522
|
}
|
|
2092
2523
|
return false;
|
|
2093
2524
|
}
|
|
2525
|
+
/**
|
|
2526
|
+
* Time Complexity: O(n)
|
|
2527
|
+
* Space Complexity: O(1)
|
|
2528
|
+
*
|
|
2529
|
+
* The function `splice` in TypeScript overrides the default behavior to remove and insert elements
|
|
2530
|
+
* in a singly linked list while handling boundary cases.
|
|
2531
|
+
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
2532
|
+
* to start modifying the list. It specifies the position where elements will be added or removed.
|
|
2533
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
2534
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
2535
|
+
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
2536
|
+
* elements can still be inserted at
|
|
2537
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements to be
|
|
2538
|
+
* inserted into the list at the specified `start` index. These elements will be inserted in place of
|
|
2539
|
+
* the elements that are removed from the list. The `splice` method allows you to add new elements to
|
|
2540
|
+
* the list while
|
|
2541
|
+
* @returns The `splice` method is returning a `SinglyLinkedList` containing the elements that were
|
|
2542
|
+
* removed from the original list during the splice operation.
|
|
2543
|
+
*/
|
|
2544
|
+
splice(start, deleteCount = 0, ...items) {
|
|
2545
|
+
const removedList = this._createInstance({ toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
2546
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
2547
|
+
deleteCount = Math.max(0, deleteCount);
|
|
2548
|
+
const prevNode = start === 0 ? void 0 : this.getNodeAt(start - 1);
|
|
2549
|
+
const startNode = prevNode ? prevNode.next : this.head;
|
|
2550
|
+
let current = startNode;
|
|
2551
|
+
for (let i = 0; i < deleteCount && current; i++) {
|
|
2552
|
+
removedList.push(current.value);
|
|
2553
|
+
current = current.next;
|
|
2554
|
+
}
|
|
2555
|
+
const nextNode = current;
|
|
2556
|
+
let lastInsertedNode = void 0;
|
|
2557
|
+
for (const item of items) {
|
|
2558
|
+
const newNode = this._ensureNode(item);
|
|
2559
|
+
if (!lastInsertedNode) {
|
|
2560
|
+
if (prevNode) {
|
|
2561
|
+
prevNode.next = newNode;
|
|
2562
|
+
} else {
|
|
2563
|
+
this._head = newNode;
|
|
2564
|
+
}
|
|
2565
|
+
} else {
|
|
2566
|
+
lastInsertedNode.next = newNode;
|
|
2567
|
+
}
|
|
2568
|
+
lastInsertedNode = newNode;
|
|
2569
|
+
}
|
|
2570
|
+
if (lastInsertedNode) {
|
|
2571
|
+
lastInsertedNode.next = nextNode;
|
|
2572
|
+
} else if (prevNode) {
|
|
2573
|
+
prevNode.next = nextNode;
|
|
2574
|
+
}
|
|
2575
|
+
if (!nextNode) {
|
|
2576
|
+
this._tail = lastInsertedNode || prevNode;
|
|
2577
|
+
}
|
|
2578
|
+
this._length += items.length - removedList.length;
|
|
2579
|
+
return removedList;
|
|
2580
|
+
}
|
|
2094
2581
|
/**
|
|
2095
2582
|
* Time Complexity: O(n)
|
|
2096
2583
|
* Space Complexity: O(1)
|
|
@@ -2124,7 +2611,7 @@ var dataStructureTyped = (() => {
|
|
|
2124
2611
|
* is a clone of the original list.
|
|
2125
2612
|
*/
|
|
2126
2613
|
clone() {
|
|
2127
|
-
return new _SinglyLinkedList(this, { toElementFn: this.toElementFn });
|
|
2614
|
+
return new _SinglyLinkedList(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2128
2615
|
}
|
|
2129
2616
|
/**
|
|
2130
2617
|
* Time Complexity: O(n)
|
|
@@ -2144,7 +2631,7 @@ var dataStructureTyped = (() => {
|
|
|
2144
2631
|
* elements that pass the filter condition specified by the `callback` function.
|
|
2145
2632
|
*/
|
|
2146
2633
|
filter(callback, thisArg) {
|
|
2147
|
-
const filteredList =
|
|
2634
|
+
const filteredList = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2148
2635
|
let index = 0;
|
|
2149
2636
|
for (const current of this) {
|
|
2150
2637
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -2175,7 +2662,7 @@ var dataStructureTyped = (() => {
|
|
|
2175
2662
|
* @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
|
|
2176
2663
|
*/
|
|
2177
2664
|
map(callback, toElementFn, thisArg) {
|
|
2178
|
-
const mappedList = new _SinglyLinkedList([], { toElementFn });
|
|
2665
|
+
const mappedList = new _SinglyLinkedList([], { toElementFn, maxLen: this._maxLen });
|
|
2179
2666
|
let index = 0;
|
|
2180
2667
|
for (const current of this) {
|
|
2181
2668
|
mappedList.push(callback.call(thisArg, current, index, this));
|
|
@@ -2183,6 +2670,19 @@ var dataStructureTyped = (() => {
|
|
|
2183
2670
|
}
|
|
2184
2671
|
return mappedList;
|
|
2185
2672
|
}
|
|
2673
|
+
/**
|
|
2674
|
+
* The function `_createInstance` returns a new instance of `SinglyLinkedList` with the specified
|
|
2675
|
+
* options.
|
|
2676
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
2677
|
+
* `SinglyLinkedListOptions<E, R>`, which is used to configure the behavior of the `SinglyLinkedList`
|
|
2678
|
+
* instance being created. It is an optional parameter, meaning it can be omitted when calling the
|
|
2679
|
+
* method.
|
|
2680
|
+
* @returns An instance of the `SinglyLinkedList` class with an empty array and the provided options
|
|
2681
|
+
* is being returned.
|
|
2682
|
+
*/
|
|
2683
|
+
_createInstance(options) {
|
|
2684
|
+
return new _SinglyLinkedList([], options);
|
|
2685
|
+
}
|
|
2186
2686
|
/**
|
|
2187
2687
|
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
2188
2688
|
*/
|
|
@@ -2193,6 +2693,33 @@ var dataStructureTyped = (() => {
|
|
|
2193
2693
|
current = current.next;
|
|
2194
2694
|
}
|
|
2195
2695
|
}
|
|
2696
|
+
/**
|
|
2697
|
+
* The function returns an iterator that iterates over the elements of a collection in reverse order.
|
|
2698
|
+
*/
|
|
2699
|
+
*_getReverseIterator() {
|
|
2700
|
+
const reversedArr = [...this].reverse();
|
|
2701
|
+
for (const item of reversedArr) {
|
|
2702
|
+
yield item;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
/**
|
|
2706
|
+
* The function `_getNodeIterator` returns an iterator that iterates over the nodes of a singly
|
|
2707
|
+
* linked list.
|
|
2708
|
+
*/
|
|
2709
|
+
*_getNodeIterator() {
|
|
2710
|
+
let current = this.head;
|
|
2711
|
+
while (current) {
|
|
2712
|
+
yield current;
|
|
2713
|
+
current = current.next;
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
// protected *_getReverseNodeIterator(): IterableIterator<SinglyLinkedListNode<E>> {
|
|
2717
|
+
// const reversedArr = [...this._getNodeIterator()].reverse();
|
|
2718
|
+
//
|
|
2719
|
+
// for (const item of reversedArr) {
|
|
2720
|
+
// yield item;
|
|
2721
|
+
// }
|
|
2722
|
+
// }
|
|
2196
2723
|
/**
|
|
2197
2724
|
* The _isPredicate function in TypeScript checks if the input is a function that takes a
|
|
2198
2725
|
* SinglyLinkedListNode as an argument and returns a boolean.
|
|
@@ -2232,73 +2759,54 @@ var dataStructureTyped = (() => {
|
|
|
2232
2759
|
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
2233
2760
|
return (node) => node.value === elementNodeOrPredicate;
|
|
2234
2761
|
}
|
|
2762
|
+
/**
|
|
2763
|
+
* The function `_getPrevNode` returns the node before a given node in a singly linked list.
|
|
2764
|
+
* @param node - The `node` parameter in the `_getPrevNode` method is a reference to a node in a
|
|
2765
|
+
* singly linked list. The method is used to find the node that comes before the given node in the
|
|
2766
|
+
* linked list.
|
|
2767
|
+
* @returns The `_getPrevNode` method returns either the previous node of the input node in a singly
|
|
2768
|
+
* linked list or `undefined` if the input node is the head of the list or if the input node is not
|
|
2769
|
+
* found in the list.
|
|
2770
|
+
*/
|
|
2771
|
+
_getPrevNode(node) {
|
|
2772
|
+
if (!this.head || this.head === node) return void 0;
|
|
2773
|
+
let current = this.head;
|
|
2774
|
+
while (current.next && current.next !== node) {
|
|
2775
|
+
current = current.next;
|
|
2776
|
+
}
|
|
2777
|
+
return current.next === node ? current : void 0;
|
|
2778
|
+
}
|
|
2235
2779
|
};
|
|
2236
2780
|
|
|
2237
2781
|
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
2238
|
-
var DoublyLinkedListNode = class {
|
|
2782
|
+
var DoublyLinkedListNode = class extends LinkedListNode {
|
|
2239
2783
|
/**
|
|
2240
2784
|
* The constructor function initializes the value, next, and previous properties of an object.
|
|
2241
2785
|
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
2242
2786
|
* is defined as a generic type "E".
|
|
2243
2787
|
*/
|
|
2244
2788
|
constructor(value) {
|
|
2245
|
-
|
|
2789
|
+
super(value);
|
|
2246
2790
|
__publicField(this, "_next");
|
|
2247
2791
|
__publicField(this, "_prev");
|
|
2248
2792
|
this._value = value;
|
|
2249
2793
|
this._next = void 0;
|
|
2250
2794
|
this._prev = void 0;
|
|
2251
2795
|
}
|
|
2252
|
-
/**
|
|
2253
|
-
* The function returns the value of a protected variable.
|
|
2254
|
-
* @returns The value of the variable `_value` is being returned.
|
|
2255
|
-
*/
|
|
2256
|
-
get value() {
|
|
2257
|
-
return this._value;
|
|
2258
|
-
}
|
|
2259
|
-
/**
|
|
2260
|
-
* The above function sets the value of a variable.
|
|
2261
|
-
* @param {E} value - The parameter "value" is of type E, which means it can be any type.
|
|
2262
|
-
*/
|
|
2263
|
-
set value(value) {
|
|
2264
|
-
this._value = value;
|
|
2265
|
-
}
|
|
2266
|
-
/**
|
|
2267
|
-
* The "next" function returns the next node in a doubly linked list.
|
|
2268
|
-
* @returns The `next` property is being returned. It can be either a `DoublyLinkedListNode<E>`
|
|
2269
|
-
* object or `undefined`.
|
|
2270
|
-
*/
|
|
2271
2796
|
get next() {
|
|
2272
2797
|
return this._next;
|
|
2273
2798
|
}
|
|
2274
|
-
/**
|
|
2275
|
-
* The "next" property of a DoublyLinkedListNode is set to the provided value.
|
|
2276
|
-
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
2277
|
-
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
2278
|
-
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
2279
|
-
*/
|
|
2280
2799
|
set next(value) {
|
|
2281
2800
|
this._next = value;
|
|
2282
2801
|
}
|
|
2283
|
-
/**
|
|
2284
|
-
* The `prev` function returns the previous node in a doubly linked list.
|
|
2285
|
-
* @returns The `prev` property of the `DoublyLinkedListNode` class is being returned. It can either
|
|
2286
|
-
* be a `DoublyLinkedListNode` object or `undefined`.
|
|
2287
|
-
*/
|
|
2288
2802
|
get prev() {
|
|
2289
2803
|
return this._prev;
|
|
2290
2804
|
}
|
|
2291
|
-
/**
|
|
2292
|
-
* The function sets the previous node of a doubly linked list node.
|
|
2293
|
-
* @param {DoublyLinkedListNode<E> | undefined} value - The `value` parameter is of type
|
|
2294
|
-
* `DoublyLinkedListNode<E> | undefined`. This means that it can accept either a
|
|
2295
|
-
* `DoublyLinkedListNode` object or `undefined` as its value.
|
|
2296
|
-
*/
|
|
2297
2805
|
set prev(value) {
|
|
2298
2806
|
this._prev = value;
|
|
2299
2807
|
}
|
|
2300
2808
|
};
|
|
2301
|
-
var DoublyLinkedList = class _DoublyLinkedList extends
|
|
2809
|
+
var DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
2302
2810
|
/**
|
|
2303
2811
|
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
2304
2812
|
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
@@ -2313,33 +2821,24 @@ var dataStructureTyped = (() => {
|
|
|
2313
2821
|
super(options);
|
|
2314
2822
|
__publicField(this, "_head");
|
|
2315
2823
|
__publicField(this, "_tail");
|
|
2316
|
-
__publicField(this, "
|
|
2824
|
+
__publicField(this, "_length");
|
|
2317
2825
|
this._head = void 0;
|
|
2318
2826
|
this._tail = void 0;
|
|
2319
|
-
this.
|
|
2827
|
+
this._length = 0;
|
|
2828
|
+
if (options) {
|
|
2829
|
+
const { maxLen } = options;
|
|
2830
|
+
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
2831
|
+
}
|
|
2320
2832
|
this.pushMany(elements);
|
|
2321
2833
|
}
|
|
2322
|
-
/**
|
|
2323
|
-
* The `head` function returns the first node of a doubly linked list.
|
|
2324
|
-
* @returns The method `getHead()` returns either a `DoublyLinkedListNode<E>` object or `undefined`.
|
|
2325
|
-
*/
|
|
2326
2834
|
get head() {
|
|
2327
2835
|
return this._head;
|
|
2328
2836
|
}
|
|
2329
|
-
/**
|
|
2330
|
-
* The `tail` function returns the last node of a doubly linked list.
|
|
2331
|
-
* @returns The `get tail()` method is returning either a `DoublyLinkedListNode<E>` object or
|
|
2332
|
-
* `undefined`.
|
|
2333
|
-
*/
|
|
2334
2837
|
get tail() {
|
|
2335
2838
|
return this._tail;
|
|
2336
2839
|
}
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
* @returns The size of the object, which is a number.
|
|
2340
|
-
*/
|
|
2341
|
-
get size() {
|
|
2342
|
-
return this._size;
|
|
2840
|
+
get length() {
|
|
2841
|
+
return this._length;
|
|
2343
2842
|
}
|
|
2344
2843
|
/**
|
|
2345
2844
|
* Time Complexity: O(1)
|
|
@@ -2411,7 +2910,8 @@ var dataStructureTyped = (() => {
|
|
|
2411
2910
|
this.tail.next = newNode;
|
|
2412
2911
|
this._tail = newNode;
|
|
2413
2912
|
}
|
|
2414
|
-
this.
|
|
2913
|
+
this._length++;
|
|
2914
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
2415
2915
|
return true;
|
|
2416
2916
|
}
|
|
2417
2917
|
/**
|
|
@@ -2431,7 +2931,7 @@ var dataStructureTyped = (() => {
|
|
|
2431
2931
|
this._tail = removedNode.prev;
|
|
2432
2932
|
this.tail.next = void 0;
|
|
2433
2933
|
}
|
|
2434
|
-
this.
|
|
2934
|
+
this._length--;
|
|
2435
2935
|
return removedNode.value;
|
|
2436
2936
|
}
|
|
2437
2937
|
/**
|
|
@@ -2451,7 +2951,7 @@ var dataStructureTyped = (() => {
|
|
|
2451
2951
|
this._head = removedNode.next;
|
|
2452
2952
|
this.head.prev = void 0;
|
|
2453
2953
|
}
|
|
2454
|
-
this.
|
|
2954
|
+
this._length--;
|
|
2455
2955
|
return removedNode.value;
|
|
2456
2956
|
}
|
|
2457
2957
|
/**
|
|
@@ -2474,7 +2974,8 @@ var dataStructureTyped = (() => {
|
|
|
2474
2974
|
this.head.prev = newNode;
|
|
2475
2975
|
this._head = newNode;
|
|
2476
2976
|
}
|
|
2477
|
-
this.
|
|
2977
|
+
this._length++;
|
|
2978
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
|
|
2478
2979
|
return true;
|
|
2479
2980
|
}
|
|
2480
2981
|
/**
|
|
@@ -2537,7 +3038,7 @@ var dataStructureTyped = (() => {
|
|
|
2537
3038
|
* or the linked list is empty, it will return undefined.
|
|
2538
3039
|
*/
|
|
2539
3040
|
at(index) {
|
|
2540
|
-
if (index < 0 || index >= this.
|
|
3041
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
2541
3042
|
let current = this.head;
|
|
2542
3043
|
for (let i = 0; i < index; i++) {
|
|
2543
3044
|
current = current.next;
|
|
@@ -2556,7 +3057,7 @@ var dataStructureTyped = (() => {
|
|
|
2556
3057
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
2557
3058
|
*/
|
|
2558
3059
|
getNodeAt(index) {
|
|
2559
|
-
if (index < 0 || index >= this.
|
|
3060
|
+
if (index < 0 || index >= this._length) return void 0;
|
|
2560
3061
|
let current = this.head;
|
|
2561
3062
|
for (let i = 0; i < index; i++) {
|
|
2562
3063
|
current = current.next;
|
|
@@ -2583,6 +3084,7 @@ var dataStructureTyped = (() => {
|
|
|
2583
3084
|
*/
|
|
2584
3085
|
getNode(elementNodeOrPredicate) {
|
|
2585
3086
|
if (elementNodeOrPredicate === void 0) return;
|
|
3087
|
+
if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
2586
3088
|
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2587
3089
|
let current = this.head;
|
|
2588
3090
|
while (current) {
|
|
@@ -2605,15 +3107,15 @@ var dataStructureTyped = (() => {
|
|
|
2605
3107
|
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
2606
3108
|
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
2607
3109
|
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
2608
|
-
* or greater than the
|
|
3110
|
+
* or greater than the length of the list).
|
|
2609
3111
|
*/
|
|
2610
3112
|
addAt(index, newElementOrNode) {
|
|
2611
|
-
if (index < 0 || index > this.
|
|
3113
|
+
if (index < 0 || index > this._length) return false;
|
|
2612
3114
|
if (index === 0) {
|
|
2613
3115
|
this.unshift(newElementOrNode);
|
|
2614
3116
|
return true;
|
|
2615
3117
|
}
|
|
2616
|
-
if (index === this.
|
|
3118
|
+
if (index === this._length) {
|
|
2617
3119
|
this.push(newElementOrNode);
|
|
2618
3120
|
return true;
|
|
2619
3121
|
}
|
|
@@ -2624,7 +3126,7 @@ var dataStructureTyped = (() => {
|
|
|
2624
3126
|
newNode.next = nextNode;
|
|
2625
3127
|
prevNode.next = newNode;
|
|
2626
3128
|
nextNode.prev = newNode;
|
|
2627
|
-
this.
|
|
3129
|
+
this._length++;
|
|
2628
3130
|
return true;
|
|
2629
3131
|
}
|
|
2630
3132
|
/**
|
|
@@ -2655,7 +3157,7 @@ var dataStructureTyped = (() => {
|
|
|
2655
3157
|
if (existingNode === this.head) {
|
|
2656
3158
|
this._head = newNode;
|
|
2657
3159
|
}
|
|
2658
|
-
this.
|
|
3160
|
+
this._length++;
|
|
2659
3161
|
return true;
|
|
2660
3162
|
}
|
|
2661
3163
|
return false;
|
|
@@ -2689,7 +3191,28 @@ var dataStructureTyped = (() => {
|
|
|
2689
3191
|
if (existingNode === this.tail) {
|
|
2690
3192
|
this._tail = newNode;
|
|
2691
3193
|
}
|
|
2692
|
-
this.
|
|
3194
|
+
this._length++;
|
|
3195
|
+
return true;
|
|
3196
|
+
}
|
|
3197
|
+
return false;
|
|
3198
|
+
}
|
|
3199
|
+
/**
|
|
3200
|
+
* Time Complexity: O(n)
|
|
3201
|
+
* Space Complexity: O(1)
|
|
3202
|
+
*
|
|
3203
|
+
* The function `setAt` updates the value at a specified index in a data structure if the index
|
|
3204
|
+
* exists.
|
|
3205
|
+
* @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
|
|
3206
|
+
* data structure where you want to set a new value.
|
|
3207
|
+
* @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
|
|
3208
|
+
* want to set at the specified index in the data structure.
|
|
3209
|
+
* @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
|
|
3210
|
+
* is successfully updated, and `false` if the index is out of bounds.
|
|
3211
|
+
*/
|
|
3212
|
+
setAt(index, value) {
|
|
3213
|
+
const node = this.getNodeAt(index);
|
|
3214
|
+
if (node) {
|
|
3215
|
+
node.value = value;
|
|
2693
3216
|
return true;
|
|
2694
3217
|
}
|
|
2695
3218
|
return false;
|
|
@@ -2705,22 +3228,25 @@ var dataStructureTyped = (() => {
|
|
|
2705
3228
|
* bounds.
|
|
2706
3229
|
*/
|
|
2707
3230
|
deleteAt(index) {
|
|
2708
|
-
if (index < 0 || index >= this.
|
|
3231
|
+
if (index < 0 || index >= this._length) return;
|
|
3232
|
+
let deleted;
|
|
2709
3233
|
if (index === 0) {
|
|
3234
|
+
deleted = this.first;
|
|
2710
3235
|
this.shift();
|
|
2711
|
-
return
|
|
3236
|
+
return deleted;
|
|
2712
3237
|
}
|
|
2713
|
-
if (index === this.
|
|
3238
|
+
if (index === this._length - 1) {
|
|
3239
|
+
deleted = this.last;
|
|
2714
3240
|
this.pop();
|
|
2715
|
-
return
|
|
3241
|
+
return deleted;
|
|
2716
3242
|
}
|
|
2717
3243
|
const removedNode = this.getNodeAt(index);
|
|
2718
3244
|
const prevNode = removedNode.prev;
|
|
2719
3245
|
const nextNode = removedNode.next;
|
|
2720
3246
|
prevNode.next = nextNode;
|
|
2721
3247
|
nextNode.prev = prevNode;
|
|
2722
|
-
this.
|
|
2723
|
-
return
|
|
3248
|
+
this._length--;
|
|
3249
|
+
return removedNode == null ? void 0 : removedNode.value;
|
|
2724
3250
|
}
|
|
2725
3251
|
/**
|
|
2726
3252
|
* Time Complexity: O(1) or O(n)
|
|
@@ -2747,7 +3273,7 @@ var dataStructureTyped = (() => {
|
|
|
2747
3273
|
const nextNode = node.next;
|
|
2748
3274
|
if (prevNode) prevNode.next = nextNode;
|
|
2749
3275
|
if (nextNode) nextNode.prev = prevNode;
|
|
2750
|
-
this.
|
|
3276
|
+
this._length--;
|
|
2751
3277
|
}
|
|
2752
3278
|
return true;
|
|
2753
3279
|
}
|
|
@@ -2757,46 +3283,22 @@ var dataStructureTyped = (() => {
|
|
|
2757
3283
|
* Time Complexity: O(1)
|
|
2758
3284
|
* Space Complexity: O(1)
|
|
2759
3285
|
*
|
|
2760
|
-
* The function checks if a variable has a
|
|
3286
|
+
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
2761
3287
|
* @returns A boolean value is being returned.
|
|
2762
3288
|
*/
|
|
2763
3289
|
isEmpty() {
|
|
2764
|
-
return this.
|
|
3290
|
+
return this._length === 0;
|
|
2765
3291
|
}
|
|
2766
3292
|
/**
|
|
2767
3293
|
* Time Complexity: O(1)
|
|
2768
3294
|
* Space Complexity: O(1)
|
|
2769
3295
|
*
|
|
2770
|
-
* The `clear` function resets the linked list by setting the head, tail, and
|
|
3296
|
+
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
2771
3297
|
*/
|
|
2772
3298
|
clear() {
|
|
2773
3299
|
this._head = void 0;
|
|
2774
3300
|
this._tail = void 0;
|
|
2775
|
-
this.
|
|
2776
|
-
}
|
|
2777
|
-
/**
|
|
2778
|
-
* Time Complexity: O(n)
|
|
2779
|
-
* Space Complexity: O(1)
|
|
2780
|
-
*
|
|
2781
|
-
* This function finds the index of a specified element, node, or predicate in a doubly linked list.
|
|
2782
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
2783
|
-
* elementNodeOrPredicate - The `indexOf` method takes in a parameter `elementNodeOrPredicate`, which
|
|
2784
|
-
* can be one of the following:
|
|
2785
|
-
* @returns The `indexOf` method returns the index of the element in the doubly linked list that
|
|
2786
|
-
* matches the provided element, node, or predicate. If no match is found, it returns -1.
|
|
2787
|
-
*/
|
|
2788
|
-
indexOf(elementNodeOrPredicate) {
|
|
2789
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
2790
|
-
let index = 0;
|
|
2791
|
-
let current = this.head;
|
|
2792
|
-
while (current) {
|
|
2793
|
-
if (predicate(current)) {
|
|
2794
|
-
return index;
|
|
2795
|
-
}
|
|
2796
|
-
index++;
|
|
2797
|
-
current = current.next;
|
|
2798
|
-
}
|
|
2799
|
-
return -1;
|
|
3301
|
+
this._length = 0;
|
|
2800
3302
|
}
|
|
2801
3303
|
/**
|
|
2802
3304
|
* Time Complexity: O(n)
|
|
@@ -2857,38 +3359,6 @@ var dataStructureTyped = (() => {
|
|
|
2857
3359
|
}
|
|
2858
3360
|
return this;
|
|
2859
3361
|
}
|
|
2860
|
-
/**
|
|
2861
|
-
* Time Complexity: O(n)
|
|
2862
|
-
* Space Complexity: O(n)
|
|
2863
|
-
*
|
|
2864
|
-
* The `toArray` function converts a linked list into an array.
|
|
2865
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
2866
|
-
*/
|
|
2867
|
-
toArray() {
|
|
2868
|
-
const array = [];
|
|
2869
|
-
let current = this.head;
|
|
2870
|
-
while (current) {
|
|
2871
|
-
array.push(current.value);
|
|
2872
|
-
current = current.next;
|
|
2873
|
-
}
|
|
2874
|
-
return array;
|
|
2875
|
-
}
|
|
2876
|
-
/**
|
|
2877
|
-
* Time Complexity: O(n)
|
|
2878
|
-
* Space Complexity: O(n)
|
|
2879
|
-
*
|
|
2880
|
-
* The `toReversedArray` function converts a doubly linked list into an array in reverse order.
|
|
2881
|
-
* @returns The `toReversedArray()` function returns an array of type `E[]`.
|
|
2882
|
-
*/
|
|
2883
|
-
toReversedArray() {
|
|
2884
|
-
const array = [];
|
|
2885
|
-
let current = this.tail;
|
|
2886
|
-
while (current) {
|
|
2887
|
-
array.push(current.value);
|
|
2888
|
-
current = current.prev;
|
|
2889
|
-
}
|
|
2890
|
-
return array;
|
|
2891
|
-
}
|
|
2892
3362
|
/**
|
|
2893
3363
|
* Time Complexity: O(n)
|
|
2894
3364
|
* Space Complexity: O(n)
|
|
@@ -2899,7 +3369,7 @@ var dataStructureTyped = (() => {
|
|
|
2899
3369
|
* is a copy of the original list.
|
|
2900
3370
|
*/
|
|
2901
3371
|
clone() {
|
|
2902
|
-
return new _DoublyLinkedList(this);
|
|
3372
|
+
return new _DoublyLinkedList(this, { toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
2903
3373
|
}
|
|
2904
3374
|
/**
|
|
2905
3375
|
* Time Complexity: O(n)
|
|
@@ -2919,7 +3389,7 @@ var dataStructureTyped = (() => {
|
|
|
2919
3389
|
* elements that pass the filter condition specified by the `callback` function.
|
|
2920
3390
|
*/
|
|
2921
3391
|
filter(callback, thisArg) {
|
|
2922
|
-
const filteredList =
|
|
3392
|
+
const filteredList = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
2923
3393
|
let index = 0;
|
|
2924
3394
|
for (const current of this) {
|
|
2925
3395
|
if (callback.call(thisArg, current, index, this)) {
|
|
@@ -2950,7 +3420,7 @@ var dataStructureTyped = (() => {
|
|
|
2950
3420
|
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
2951
3421
|
*/
|
|
2952
3422
|
map(callback, toElementFn, thisArg) {
|
|
2953
|
-
const mappedList = new _DoublyLinkedList([], { toElementFn });
|
|
3423
|
+
const mappedList = new _DoublyLinkedList([], { toElementFn, maxLen: this._maxLen });
|
|
2954
3424
|
let index = 0;
|
|
2955
3425
|
for (const current of this) {
|
|
2956
3426
|
mappedList.push(callback.call(thisArg, current, index, this));
|
|
@@ -2991,6 +3461,35 @@ var dataStructureTyped = (() => {
|
|
|
2991
3461
|
current = current.next;
|
|
2992
3462
|
}
|
|
2993
3463
|
}
|
|
3464
|
+
/**
|
|
3465
|
+
* The function returns an iterator that iterates over the elements of a data structure in reverse
|
|
3466
|
+
* order.
|
|
3467
|
+
*/
|
|
3468
|
+
*_getReverseIterator() {
|
|
3469
|
+
let current = this.tail;
|
|
3470
|
+
while (current) {
|
|
3471
|
+
yield current.value;
|
|
3472
|
+
current = current.prev;
|
|
3473
|
+
}
|
|
3474
|
+
}
|
|
3475
|
+
/**
|
|
3476
|
+
* The function returns an iterator that iterates over the nodes of a doubly linked list starting
|
|
3477
|
+
* from the head.
|
|
3478
|
+
*/
|
|
3479
|
+
*_getNodeIterator() {
|
|
3480
|
+
let current = this.head;
|
|
3481
|
+
while (current) {
|
|
3482
|
+
yield current;
|
|
3483
|
+
current = current.next;
|
|
3484
|
+
}
|
|
3485
|
+
}
|
|
3486
|
+
// protected *_getReverseNodeIterator(): IterableIterator<DoublyLinkedListNode<E>> {
|
|
3487
|
+
// const reversedArr = [...this._getNodeIterator()].reverse();
|
|
3488
|
+
//
|
|
3489
|
+
// for (const item of reversedArr) {
|
|
3490
|
+
// yield item;
|
|
3491
|
+
// }
|
|
3492
|
+
// }
|
|
2994
3493
|
/**
|
|
2995
3494
|
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
2996
3495
|
* as an argument and returns a boolean.
|
|
@@ -3030,6 +3529,30 @@ var dataStructureTyped = (() => {
|
|
|
3030
3529
|
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
3031
3530
|
return (node) => node.value === elementNodeOrPredicate;
|
|
3032
3531
|
}
|
|
3532
|
+
/**
|
|
3533
|
+
* The function `_createInstance` returns a new instance of `DoublyLinkedList` with the specified
|
|
3534
|
+
* options.
|
|
3535
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
3536
|
+
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
3537
|
+
* configuration options when creating a new instance of the `DoublyLinkedList` class.
|
|
3538
|
+
* @returns An instance of the `DoublyLinkedList` class with an empty array and the provided options
|
|
3539
|
+
* is being returned, cast as the current class type.
|
|
3540
|
+
*/
|
|
3541
|
+
_createInstance(options) {
|
|
3542
|
+
return new _DoublyLinkedList([], options);
|
|
3543
|
+
}
|
|
3544
|
+
/**
|
|
3545
|
+
* The function `_getPrevNode` returns the previous node of a given node in a doubly linked list.
|
|
3546
|
+
* @param node - The parameter `node` in the `_getPrevNode` method is of type
|
|
3547
|
+
* `DoublyLinkedListNode<E>`, which represents a node in a doubly linked list containing an element
|
|
3548
|
+
* of type `E`.
|
|
3549
|
+
* @returns The `_getPrevNode` method is returning the previous node of the input `node` in a doubly
|
|
3550
|
+
* linked list. If the input node has a previous node, it will return that node. Otherwise, it will
|
|
3551
|
+
* return `undefined`.
|
|
3552
|
+
*/
|
|
3553
|
+
_getPrevNode(node) {
|
|
3554
|
+
return node.prev;
|
|
3555
|
+
}
|
|
3033
3556
|
};
|
|
3034
3557
|
|
|
3035
3558
|
// src/data-structures/linked-list/skip-linked-list.ts
|
|
@@ -3394,16 +3917,6 @@ var dataStructureTyped = (() => {
|
|
|
3394
3917
|
const spliced = this.elements.splice(index, 1);
|
|
3395
3918
|
return spliced.length === 1;
|
|
3396
3919
|
}
|
|
3397
|
-
/**
|
|
3398
|
-
* Time Complexity: O(n)
|
|
3399
|
-
* Space Complexity: O(n)
|
|
3400
|
-
*
|
|
3401
|
-
* The toArray function returns a copy of the elements in an array.
|
|
3402
|
-
* @returns An array of type E.
|
|
3403
|
-
*/
|
|
3404
|
-
toArray() {
|
|
3405
|
-
return this.elements.slice();
|
|
3406
|
-
}
|
|
3407
3920
|
/**
|
|
3408
3921
|
* Time Complexity: O(1)
|
|
3409
3922
|
* Space Complexity: O(1)
|
|
@@ -3491,7 +4004,7 @@ var dataStructureTyped = (() => {
|
|
|
3491
4004
|
};
|
|
3492
4005
|
|
|
3493
4006
|
// src/data-structures/queue/queue.ts
|
|
3494
|
-
var Queue = class _Queue extends
|
|
4007
|
+
var Queue = class _Queue extends LinearBase {
|
|
3495
4008
|
constructor(elements = [], options) {
|
|
3496
4009
|
super(options);
|
|
3497
4010
|
__publicField(this, "_elements", []);
|
|
@@ -3503,37 +4016,31 @@ var dataStructureTyped = (() => {
|
|
|
3503
4016
|
}
|
|
3504
4017
|
this.pushMany(elements);
|
|
3505
4018
|
}
|
|
3506
|
-
/**
|
|
3507
|
-
* The elements function returns the elements of this set.
|
|
3508
|
-
* @return An array of the elements in the stack
|
|
3509
|
-
*/
|
|
3510
4019
|
get elements() {
|
|
3511
4020
|
return this._elements;
|
|
3512
4021
|
}
|
|
3513
|
-
/**
|
|
3514
|
-
* The offset function returns the offset of the current page.
|
|
3515
|
-
* @return The value of the protected variable _offset
|
|
3516
|
-
*/
|
|
3517
4022
|
get offset() {
|
|
3518
4023
|
return this._offset;
|
|
3519
4024
|
}
|
|
3520
|
-
|
|
3521
|
-
* The size function returns the number of elements in an array.
|
|
3522
|
-
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
3523
|
-
*/
|
|
3524
|
-
get size() {
|
|
4025
|
+
get length() {
|
|
3525
4026
|
return this.elements.length - this.offset;
|
|
3526
4027
|
}
|
|
4028
|
+
get autoCompactRatio() {
|
|
4029
|
+
return this._autoCompactRatio;
|
|
4030
|
+
}
|
|
4031
|
+
set autoCompactRatio(v) {
|
|
4032
|
+
this._autoCompactRatio = v;
|
|
4033
|
+
}
|
|
3527
4034
|
/**
|
|
3528
4035
|
* Time Complexity: O(1)
|
|
3529
4036
|
* Space Complexity: O(1)
|
|
3530
4037
|
*
|
|
3531
4038
|
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
3532
4039
|
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
3533
|
-
* the `_offset` index. If the data structure is empty (
|
|
4040
|
+
* the `_offset` index. If the data structure is empty (length is 0), it returns `undefined`.
|
|
3534
4041
|
*/
|
|
3535
4042
|
get first() {
|
|
3536
|
-
return this.
|
|
4043
|
+
return this.length > 0 ? this.elements[this.offset] : void 0;
|
|
3537
4044
|
}
|
|
3538
4045
|
/**
|
|
3539
4046
|
* Time Complexity: O(1)
|
|
@@ -3544,22 +4051,7 @@ var dataStructureTyped = (() => {
|
|
|
3544
4051
|
* array is empty, it returns `undefined`.
|
|
3545
4052
|
*/
|
|
3546
4053
|
get last() {
|
|
3547
|
-
return this.
|
|
3548
|
-
}
|
|
3549
|
-
/**
|
|
3550
|
-
* This function returns the value of the autoCompactRatio property.
|
|
3551
|
-
* @returns The `autoCompactRatio` property of the object, which is a number.
|
|
3552
|
-
*/
|
|
3553
|
-
get autoCompactRatio() {
|
|
3554
|
-
return this._autoCompactRatio;
|
|
3555
|
-
}
|
|
3556
|
-
/**
|
|
3557
|
-
* The above function sets the autoCompactRatio property to a specified number in TypeScript.
|
|
3558
|
-
* @param {number} v - The parameter `v` represents the value that will be assigned to the
|
|
3559
|
-
* `_autoCompactRatio` property.
|
|
3560
|
-
*/
|
|
3561
|
-
set autoCompactRatio(v) {
|
|
3562
|
-
this._autoCompactRatio = v;
|
|
4054
|
+
return this.length > 0 ? this.elements[this.elements.length - 1] : void 0;
|
|
3563
4055
|
}
|
|
3564
4056
|
/**
|
|
3565
4057
|
* Time Complexity: O(n)
|
|
@@ -3584,6 +4076,7 @@ var dataStructureTyped = (() => {
|
|
|
3584
4076
|
*/
|
|
3585
4077
|
push(element) {
|
|
3586
4078
|
this.elements.push(element);
|
|
4079
|
+
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
3587
4080
|
return true;
|
|
3588
4081
|
}
|
|
3589
4082
|
/**
|
|
@@ -3614,7 +4107,7 @@ var dataStructureTyped = (() => {
|
|
|
3614
4107
|
* @returns The function `shift()` returns either the first element in the queue or `undefined` if the queue is empty.
|
|
3615
4108
|
*/
|
|
3616
4109
|
shift() {
|
|
3617
|
-
if (this.
|
|
4110
|
+
if (this.length === 0) return void 0;
|
|
3618
4111
|
const first = this.first;
|
|
3619
4112
|
this._offset += 1;
|
|
3620
4113
|
if (this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
@@ -3630,7 +4123,7 @@ var dataStructureTyped = (() => {
|
|
|
3630
4123
|
*/
|
|
3631
4124
|
delete(element) {
|
|
3632
4125
|
const index = this.elements.indexOf(element);
|
|
3633
|
-
return this.deleteAt(index);
|
|
4126
|
+
return !!this.deleteAt(index);
|
|
3634
4127
|
}
|
|
3635
4128
|
/**
|
|
3636
4129
|
* Time Complexity: O(n)
|
|
@@ -3641,8 +4134,9 @@ var dataStructureTyped = (() => {
|
|
|
3641
4134
|
* @return A boolean value
|
|
3642
4135
|
*/
|
|
3643
4136
|
deleteAt(index) {
|
|
3644
|
-
const
|
|
3645
|
-
|
|
4137
|
+
const deleted = this.elements[index];
|
|
4138
|
+
this.elements.splice(index, 1);
|
|
4139
|
+
return deleted;
|
|
3646
4140
|
}
|
|
3647
4141
|
/**
|
|
3648
4142
|
* Time Complexity: O(1)
|
|
@@ -3658,25 +4152,66 @@ var dataStructureTyped = (() => {
|
|
|
3658
4152
|
at(index) {
|
|
3659
4153
|
return this.elements[index + this._offset];
|
|
3660
4154
|
}
|
|
4155
|
+
/**
|
|
4156
|
+
* Time Complexity: O(n)
|
|
4157
|
+
* Space Complexity: O(1)
|
|
4158
|
+
*
|
|
4159
|
+
* The `reverse` function in TypeScript reverses the elements of an array starting from a specified
|
|
4160
|
+
* offset.
|
|
4161
|
+
* @returns The `reverse()` method is returning the modified object itself (`this`) after reversing
|
|
4162
|
+
* the elements in the array and resetting the offset to 0.
|
|
4163
|
+
*/
|
|
4164
|
+
reverse() {
|
|
4165
|
+
this._elements = this.elements.slice(this.offset).reverse();
|
|
4166
|
+
this._offset = 0;
|
|
4167
|
+
return this;
|
|
4168
|
+
}
|
|
4169
|
+
/**
|
|
4170
|
+
* Time Complexity: O(n)
|
|
4171
|
+
* Space Complexity: O(1)
|
|
4172
|
+
*
|
|
4173
|
+
* The function `addAt` inserts a new element at a specified index in an array, returning true if
|
|
4174
|
+
* successful and false if the index is out of bounds.
|
|
4175
|
+
* @param {number} index - The `index` parameter represents the position at which the `newElement`
|
|
4176
|
+
* should be added in the array.
|
|
4177
|
+
* @param {E} newElement - The `newElement` parameter represents the element that you want to insert
|
|
4178
|
+
* into the array at the specified index.
|
|
4179
|
+
* @returns The `addAt` method returns a boolean value - `true` if the new element was successfully
|
|
4180
|
+
* added at the specified index, and `false` if the index is out of bounds (less than 0 or greater
|
|
4181
|
+
* than the length of the array).
|
|
4182
|
+
*/
|
|
4183
|
+
addAt(index, newElement) {
|
|
4184
|
+
if (index < 0 || index > this.length) return false;
|
|
4185
|
+
this._elements.splice(this.offset + index, 0, newElement);
|
|
4186
|
+
return true;
|
|
4187
|
+
}
|
|
3661
4188
|
/**
|
|
3662
4189
|
* Time Complexity: O(1)
|
|
3663
4190
|
* Space Complexity: O(1)
|
|
3664
4191
|
*
|
|
3665
|
-
* The function
|
|
3666
|
-
* @
|
|
4192
|
+
* The function `setAt` updates an element at a specified index in an array-like data structure.
|
|
4193
|
+
* @param {number} index - The `index` parameter is a number that represents the position in the
|
|
4194
|
+
* array where the new element will be set.
|
|
4195
|
+
* @param {E} newElement - The `newElement` parameter represents the new value that you want to set
|
|
4196
|
+
* at the specified index in the array.
|
|
4197
|
+
* @returns The `setAt` method returns a boolean value - `true` if the element was successfully set
|
|
4198
|
+
* at the specified index, and `false` if the index is out of bounds (less than 0 or greater than the
|
|
4199
|
+
* length of the array).
|
|
3667
4200
|
*/
|
|
3668
|
-
|
|
3669
|
-
|
|
4201
|
+
setAt(index, newElement) {
|
|
4202
|
+
if (index < 0 || index > this.length) return false;
|
|
4203
|
+
this._elements[this.offset + index] = newElement;
|
|
4204
|
+
return true;
|
|
3670
4205
|
}
|
|
3671
4206
|
/**
|
|
3672
4207
|
* Time Complexity: O(1)
|
|
3673
|
-
* Space Complexity: O(
|
|
4208
|
+
* Space Complexity: O(1)
|
|
3674
4209
|
*
|
|
3675
|
-
* The
|
|
3676
|
-
* @returns
|
|
4210
|
+
* The function checks if a data structure is empty by comparing its length to zero.
|
|
4211
|
+
* @returns {boolean} A boolean value indicating whether the length of the object is 0 or not.
|
|
3677
4212
|
*/
|
|
3678
|
-
|
|
3679
|
-
return this.
|
|
4213
|
+
isEmpty() {
|
|
4214
|
+
return this.length === 0;
|
|
3680
4215
|
}
|
|
3681
4216
|
/**
|
|
3682
4217
|
* Time Complexity: O(1)
|
|
@@ -3701,6 +4236,34 @@ var dataStructureTyped = (() => {
|
|
|
3701
4236
|
this._offset = 0;
|
|
3702
4237
|
return true;
|
|
3703
4238
|
}
|
|
4239
|
+
/**
|
|
4240
|
+
* Time Complexity: O(n)
|
|
4241
|
+
* Space Complexity: O(n)
|
|
4242
|
+
*
|
|
4243
|
+
* The function overrides the splice method to remove and insert elements in a queue-like data
|
|
4244
|
+
* structure.
|
|
4245
|
+
* @param {number} start - The `start` parameter in the `splice` method specifies the index at which
|
|
4246
|
+
* to start changing the array. Items will be added or removed starting from this index.
|
|
4247
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
4248
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
4249
|
+
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
4250
|
+
* elements can still be inserted at
|
|
4251
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
4252
|
+
* will be added to the array at the specified `start` index. These elements will replace the
|
|
4253
|
+
* existing elements starting from the `start` index for the `deleteCount` number of elements.
|
|
4254
|
+
* @returns The `splice` method is returning the `removedQueue`, which is an instance of the same
|
|
4255
|
+
* class as the original object.
|
|
4256
|
+
*/
|
|
4257
|
+
splice(start, deleteCount = 0, ...items) {
|
|
4258
|
+
const removedQueue = this._createInstance();
|
|
4259
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
4260
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
4261
|
+
const globalStartIndex = this.offset + start;
|
|
4262
|
+
const removedElements = this._elements.splice(globalStartIndex, deleteCount, ...items);
|
|
4263
|
+
removedQueue.pushMany(removedElements);
|
|
4264
|
+
this.compact();
|
|
4265
|
+
return removedQueue;
|
|
4266
|
+
}
|
|
3704
4267
|
/**
|
|
3705
4268
|
* Time Complexity: O(n)
|
|
3706
4269
|
* Space Complexity: O(n)
|
|
@@ -3709,7 +4272,7 @@ var dataStructureTyped = (() => {
|
|
|
3709
4272
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
3710
4273
|
*/
|
|
3711
4274
|
clone() {
|
|
3712
|
-
return new _Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn });
|
|
4275
|
+
return new _Queue(this.elements.slice(this.offset), { toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
3713
4276
|
}
|
|
3714
4277
|
/**
|
|
3715
4278
|
* Time Complexity: O(n)
|
|
@@ -3728,7 +4291,11 @@ var dataStructureTyped = (() => {
|
|
|
3728
4291
|
* satisfy the given predicate function.
|
|
3729
4292
|
*/
|
|
3730
4293
|
filter(predicate, thisArg) {
|
|
3731
|
-
const newDeque =
|
|
4294
|
+
const newDeque = this._createInstance({
|
|
4295
|
+
toElementFn: this._toElementFn,
|
|
4296
|
+
autoCompactRatio: this._autoCompactRatio,
|
|
4297
|
+
maxLen: this._maxLen
|
|
4298
|
+
});
|
|
3732
4299
|
let index = 0;
|
|
3733
4300
|
for (const el of this) {
|
|
3734
4301
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -3757,7 +4324,11 @@ var dataStructureTyped = (() => {
|
|
|
3757
4324
|
* callback function to each element in the original Queue object.
|
|
3758
4325
|
*/
|
|
3759
4326
|
map(callback, toElementFn, thisArg) {
|
|
3760
|
-
const newDeque = new _Queue([], {
|
|
4327
|
+
const newDeque = new _Queue([], {
|
|
4328
|
+
toElementFn,
|
|
4329
|
+
autoCompactRatio: this._autoCompactRatio,
|
|
4330
|
+
maxLen: this._maxLen
|
|
4331
|
+
});
|
|
3761
4332
|
let index = 0;
|
|
3762
4333
|
for (const el of this) {
|
|
3763
4334
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -3776,6 +4347,28 @@ var dataStructureTyped = (() => {
|
|
|
3776
4347
|
yield item;
|
|
3777
4348
|
}
|
|
3778
4349
|
}
|
|
4350
|
+
/**
|
|
4351
|
+
* The function `_createInstance` returns a new instance of the `Queue` class with the specified
|
|
4352
|
+
* options.
|
|
4353
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
4354
|
+
* `QueueOptions<E, R>`, which is used to configure the behavior of the queue being created. It
|
|
4355
|
+
* allows you to specify settings or properties that can influence how the queue operates.
|
|
4356
|
+
* @returns An instance of the `Queue` class with an empty array and the provided options is being
|
|
4357
|
+
* returned.
|
|
4358
|
+
*/
|
|
4359
|
+
_createInstance(options) {
|
|
4360
|
+
return new _Queue([], options);
|
|
4361
|
+
}
|
|
4362
|
+
/**
|
|
4363
|
+
* The function `_getReverseIterator` returns an iterator that iterates over elements in reverse
|
|
4364
|
+
* order.
|
|
4365
|
+
*/
|
|
4366
|
+
*_getReverseIterator() {
|
|
4367
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
4368
|
+
const cur = this.at(i);
|
|
4369
|
+
if (cur !== void 0) yield cur;
|
|
4370
|
+
}
|
|
4371
|
+
}
|
|
3779
4372
|
};
|
|
3780
4373
|
var LinkedListQueue = class _LinkedListQueue extends SinglyLinkedList {
|
|
3781
4374
|
/**
|
|
@@ -3787,12 +4380,12 @@ var dataStructureTyped = (() => {
|
|
|
3787
4380
|
* values as the original `LinkedListQueue`.
|
|
3788
4381
|
*/
|
|
3789
4382
|
clone() {
|
|
3790
|
-
return new _LinkedListQueue(this, { toElementFn: this.toElementFn });
|
|
4383
|
+
return new _LinkedListQueue(this, { toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
3791
4384
|
}
|
|
3792
4385
|
};
|
|
3793
4386
|
|
|
3794
4387
|
// src/data-structures/queue/deque.ts
|
|
3795
|
-
var Deque = class _Deque extends
|
|
4388
|
+
var Deque = class _Deque extends LinearBase {
|
|
3796
4389
|
/**
|
|
3797
4390
|
* The constructor initializes a Deque object with optional iterable of elements and options.
|
|
3798
4391
|
* @param elements - An iterable object (such as an array or a Set) that contains the initial
|
|
@@ -3807,18 +4400,16 @@ var dataStructureTyped = (() => {
|
|
|
3807
4400
|
constructor(elements = [], options) {
|
|
3808
4401
|
super(options);
|
|
3809
4402
|
__publicField(this, "_bucketSize", 1 << 12);
|
|
3810
|
-
__publicField(this, "_maxLen", -1);
|
|
3811
4403
|
__publicField(this, "_bucketFirst", 0);
|
|
3812
4404
|
__publicField(this, "_firstInBucket", 0);
|
|
3813
4405
|
__publicField(this, "_bucketLast", 0);
|
|
3814
4406
|
__publicField(this, "_lastInBucket", 0);
|
|
3815
4407
|
__publicField(this, "_bucketCount", 0);
|
|
3816
4408
|
__publicField(this, "_buckets", []);
|
|
3817
|
-
__publicField(this, "
|
|
4409
|
+
__publicField(this, "_length", 0);
|
|
3818
4410
|
if (options) {
|
|
3819
|
-
const { bucketSize
|
|
4411
|
+
const { bucketSize } = options;
|
|
3820
4412
|
if (typeof bucketSize === "number") this._bucketSize = bucketSize;
|
|
3821
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
3822
4413
|
}
|
|
3823
4414
|
let _size;
|
|
3824
4415
|
if ("length" in elements) {
|
|
@@ -3837,72 +4428,29 @@ var dataStructureTyped = (() => {
|
|
|
3837
4428
|
this._firstInBucket = this._lastInBucket = this._bucketSize - _size % this._bucketSize >> 1;
|
|
3838
4429
|
this.pushMany(elements);
|
|
3839
4430
|
}
|
|
3840
|
-
/**
|
|
3841
|
-
* The bucketSize function returns the size of the bucket.
|
|
3842
|
-
*
|
|
3843
|
-
* @return The size of the bucket
|
|
3844
|
-
*/
|
|
3845
4431
|
get bucketSize() {
|
|
3846
4432
|
return this._bucketSize;
|
|
3847
4433
|
}
|
|
3848
|
-
/**
|
|
3849
|
-
* The maxLen function returns the max length of the deque.
|
|
3850
|
-
*
|
|
3851
|
-
* @return The max length of the deque
|
|
3852
|
-
*/
|
|
3853
|
-
get maxLen() {
|
|
3854
|
-
return this._maxLen;
|
|
3855
|
-
}
|
|
3856
|
-
/**
|
|
3857
|
-
* The function returns the value of the protected variable `_bucketFirst`.
|
|
3858
|
-
* @returns The value of the `_bucketFirst` property.
|
|
3859
|
-
*/
|
|
3860
4434
|
get bucketFirst() {
|
|
3861
4435
|
return this._bucketFirst;
|
|
3862
4436
|
}
|
|
3863
|
-
/**
|
|
3864
|
-
* The function returns the value of the protected variable _firstInBucket.
|
|
3865
|
-
* @returns The method is returning the value of the variable `_firstInBucket`, which is of type
|
|
3866
|
-
* `number`.
|
|
3867
|
-
*/
|
|
3868
4437
|
get firstInBucket() {
|
|
3869
4438
|
return this._firstInBucket;
|
|
3870
4439
|
}
|
|
3871
|
-
/**
|
|
3872
|
-
* The function returns the value of the protected variable `_bucketLast`.
|
|
3873
|
-
* @returns The value of the `_bucketLast` property, which is a number.
|
|
3874
|
-
*/
|
|
3875
4440
|
get bucketLast() {
|
|
3876
4441
|
return this._bucketLast;
|
|
3877
4442
|
}
|
|
3878
|
-
/**
|
|
3879
|
-
* The function returns the value of the protected variable _lastInBucket.
|
|
3880
|
-
* @returns The method is returning the value of the variable `_lastInBucket`, which is of type
|
|
3881
|
-
* `number`.
|
|
3882
|
-
*/
|
|
3883
4443
|
get lastInBucket() {
|
|
3884
4444
|
return this._lastInBucket;
|
|
3885
4445
|
}
|
|
3886
|
-
/**
|
|
3887
|
-
* The function returns the number of buckets.
|
|
3888
|
-
* @returns The number of buckets.
|
|
3889
|
-
*/
|
|
3890
4446
|
get bucketCount() {
|
|
3891
4447
|
return this._bucketCount;
|
|
3892
4448
|
}
|
|
3893
|
-
/**
|
|
3894
|
-
* The buckets function returns the buckets property of the object.
|
|
3895
|
-
* @return The buckets property
|
|
3896
|
-
*/
|
|
3897
4449
|
get buckets() {
|
|
3898
4450
|
return this._buckets;
|
|
3899
4451
|
}
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
* @return The number of values in the set
|
|
3903
|
-
*/
|
|
3904
|
-
get size() {
|
|
3905
|
-
return this._size;
|
|
4452
|
+
get length() {
|
|
4453
|
+
return this._length;
|
|
3906
4454
|
}
|
|
3907
4455
|
/**
|
|
3908
4456
|
* The function returns the first element in a collection if it exists, otherwise it returns
|
|
@@ -3910,7 +4458,7 @@ var dataStructureTyped = (() => {
|
|
|
3910
4458
|
* @returns The first element of the collection, of type E, is being returned.
|
|
3911
4459
|
*/
|
|
3912
4460
|
get first() {
|
|
3913
|
-
if (this.
|
|
4461
|
+
if (this._length === 0) return;
|
|
3914
4462
|
return this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3915
4463
|
}
|
|
3916
4464
|
/**
|
|
@@ -3918,7 +4466,7 @@ var dataStructureTyped = (() => {
|
|
|
3918
4466
|
* @return The last element in the array
|
|
3919
4467
|
*/
|
|
3920
4468
|
get last() {
|
|
3921
|
-
if (this.
|
|
4469
|
+
if (this._length === 0) return;
|
|
3922
4470
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
3923
4471
|
}
|
|
3924
4472
|
/**
|
|
@@ -3931,7 +4479,7 @@ var dataStructureTyped = (() => {
|
|
|
3931
4479
|
* @returns The size of the data structure after the element has been pushed.
|
|
3932
4480
|
*/
|
|
3933
4481
|
push(element) {
|
|
3934
|
-
if (this.
|
|
4482
|
+
if (this._length) {
|
|
3935
4483
|
if (this._lastInBucket < this._bucketSize - 1) {
|
|
3936
4484
|
this._lastInBucket += 1;
|
|
3937
4485
|
} else if (this._bucketLast < this._bucketCount - 1) {
|
|
@@ -3943,9 +4491,9 @@ var dataStructureTyped = (() => {
|
|
|
3943
4491
|
}
|
|
3944
4492
|
if (this._bucketLast === this._bucketFirst && this._lastInBucket === this._firstInBucket) this._reallocate();
|
|
3945
4493
|
}
|
|
3946
|
-
this.
|
|
4494
|
+
this._length += 1;
|
|
3947
4495
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
3948
|
-
if (this._maxLen > 0 && this.
|
|
4496
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.shift();
|
|
3949
4497
|
return true;
|
|
3950
4498
|
}
|
|
3951
4499
|
/**
|
|
@@ -3957,9 +4505,9 @@ var dataStructureTyped = (() => {
|
|
|
3957
4505
|
* @returns The element that was removed from the data structure is being returned.
|
|
3958
4506
|
*/
|
|
3959
4507
|
pop() {
|
|
3960
|
-
if (this.
|
|
4508
|
+
if (this._length === 0) return;
|
|
3961
4509
|
const element = this._buckets[this._bucketLast][this._lastInBucket];
|
|
3962
|
-
if (this.
|
|
4510
|
+
if (this._length !== 1) {
|
|
3963
4511
|
if (this._lastInBucket > 0) {
|
|
3964
4512
|
this._lastInBucket -= 1;
|
|
3965
4513
|
} else if (this._bucketLast > 0) {
|
|
@@ -3970,7 +4518,7 @@ var dataStructureTyped = (() => {
|
|
|
3970
4518
|
this._lastInBucket = this._bucketSize - 1;
|
|
3971
4519
|
}
|
|
3972
4520
|
}
|
|
3973
|
-
this.
|
|
4521
|
+
this._length -= 1;
|
|
3974
4522
|
return element;
|
|
3975
4523
|
}
|
|
3976
4524
|
/**
|
|
@@ -3983,9 +4531,9 @@ var dataStructureTyped = (() => {
|
|
|
3983
4531
|
* returned.
|
|
3984
4532
|
*/
|
|
3985
4533
|
shift() {
|
|
3986
|
-
if (this.
|
|
4534
|
+
if (this._length === 0) return;
|
|
3987
4535
|
const element = this._buckets[this._bucketFirst][this._firstInBucket];
|
|
3988
|
-
if (this.
|
|
4536
|
+
if (this._length !== 1) {
|
|
3989
4537
|
if (this._firstInBucket < this._bucketSize - 1) {
|
|
3990
4538
|
this._firstInBucket += 1;
|
|
3991
4539
|
} else if (this._bucketFirst < this._bucketCount - 1) {
|
|
@@ -3996,7 +4544,7 @@ var dataStructureTyped = (() => {
|
|
|
3996
4544
|
this._firstInBucket = 0;
|
|
3997
4545
|
}
|
|
3998
4546
|
}
|
|
3999
|
-
this.
|
|
4547
|
+
this._length -= 1;
|
|
4000
4548
|
return element;
|
|
4001
4549
|
}
|
|
4002
4550
|
/**
|
|
@@ -4010,7 +4558,7 @@ var dataStructureTyped = (() => {
|
|
|
4010
4558
|
* @returns The size of the data structure after the element has been added.
|
|
4011
4559
|
*/
|
|
4012
4560
|
unshift(element) {
|
|
4013
|
-
if (this.
|
|
4561
|
+
if (this._length) {
|
|
4014
4562
|
if (this._firstInBucket > 0) {
|
|
4015
4563
|
this._firstInBucket -= 1;
|
|
4016
4564
|
} else if (this._bucketFirst > 0) {
|
|
@@ -4022,9 +4570,9 @@ var dataStructureTyped = (() => {
|
|
|
4022
4570
|
}
|
|
4023
4571
|
if (this._bucketFirst === this._bucketLast && this._firstInBucket === this._lastInBucket) this._reallocate();
|
|
4024
4572
|
}
|
|
4025
|
-
this.
|
|
4573
|
+
this._length += 1;
|
|
4026
4574
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
4027
|
-
if (this._maxLen > 0 && this.
|
|
4575
|
+
if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
|
|
4028
4576
|
return true;
|
|
4029
4577
|
}
|
|
4030
4578
|
/**
|
|
@@ -4084,7 +4632,7 @@ var dataStructureTyped = (() => {
|
|
|
4084
4632
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
4085
4633
|
*/
|
|
4086
4634
|
isEmpty() {
|
|
4087
|
-
return this.
|
|
4635
|
+
return this._length === 0;
|
|
4088
4636
|
}
|
|
4089
4637
|
/**
|
|
4090
4638
|
* Time Complexity: O(1)
|
|
@@ -4096,30 +4644,9 @@ var dataStructureTyped = (() => {
|
|
|
4096
4644
|
clear() {
|
|
4097
4645
|
this._buckets = [new Array(this._bucketSize)];
|
|
4098
4646
|
this._bucketCount = 1;
|
|
4099
|
-
this._bucketFirst = this._bucketLast = this.
|
|
4647
|
+
this._bucketFirst = this._bucketLast = this._length = 0;
|
|
4100
4648
|
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
4101
4649
|
}
|
|
4102
|
-
/**
|
|
4103
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
4104
|
-
*/
|
|
4105
|
-
*begin() {
|
|
4106
|
-
let index = 0;
|
|
4107
|
-
while (index < this._size) {
|
|
4108
|
-
yield this.at(index);
|
|
4109
|
-
index++;
|
|
4110
|
-
}
|
|
4111
|
-
}
|
|
4112
|
-
/**
|
|
4113
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
4114
|
-
* the last element.
|
|
4115
|
-
*/
|
|
4116
|
-
*reverseBegin() {
|
|
4117
|
-
let index = this._size - 1;
|
|
4118
|
-
while (index >= 0) {
|
|
4119
|
-
yield this.at(index);
|
|
4120
|
-
index--;
|
|
4121
|
-
}
|
|
4122
|
-
}
|
|
4123
4650
|
/**
|
|
4124
4651
|
* Time Complexity: O(1)
|
|
4125
4652
|
* Space Complexity: O(1)
|
|
@@ -4131,7 +4658,7 @@ var dataStructureTyped = (() => {
|
|
|
4131
4658
|
* @returns The element at the specified position in the data structure is being returned.
|
|
4132
4659
|
*/
|
|
4133
4660
|
at(pos) {
|
|
4134
|
-
rangeCheck(pos, 0, this.
|
|
4661
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
4135
4662
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4136
4663
|
return this._buckets[bucketIndex][indexInBucket];
|
|
4137
4664
|
}
|
|
@@ -4146,7 +4673,7 @@ var dataStructureTyped = (() => {
|
|
|
4146
4673
|
* position in the data structure.
|
|
4147
4674
|
*/
|
|
4148
4675
|
setAt(pos, element) {
|
|
4149
|
-
rangeCheck(pos, 0, this.
|
|
4676
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
4150
4677
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4151
4678
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
4152
4679
|
return true;
|
|
@@ -4167,15 +4694,15 @@ var dataStructureTyped = (() => {
|
|
|
4167
4694
|
* @returns The size of the array after the insertion is being returned.
|
|
4168
4695
|
*/
|
|
4169
4696
|
addAt(pos, element, num = 1) {
|
|
4170
|
-
const length = this.
|
|
4697
|
+
const length = this._length;
|
|
4171
4698
|
rangeCheck(pos, 0, length);
|
|
4172
4699
|
if (pos === 0) {
|
|
4173
4700
|
while (num--) this.unshift(element);
|
|
4174
|
-
} else if (pos === this.
|
|
4701
|
+
} else if (pos === this._length) {
|
|
4175
4702
|
while (num--) this.push(element);
|
|
4176
4703
|
} else {
|
|
4177
4704
|
const arr = [];
|
|
4178
|
-
for (let i = pos; i < this.
|
|
4705
|
+
for (let i = pos; i < this._length; ++i) {
|
|
4179
4706
|
arr.push(this.at(i));
|
|
4180
4707
|
}
|
|
4181
4708
|
this.cut(pos - 1, true);
|
|
@@ -4204,16 +4731,59 @@ var dataStructureTyped = (() => {
|
|
|
4204
4731
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4205
4732
|
this._bucketLast = bucketIndex;
|
|
4206
4733
|
this._lastInBucket = indexInBucket;
|
|
4207
|
-
this.
|
|
4734
|
+
this._length = pos + 1;
|
|
4208
4735
|
return this;
|
|
4209
4736
|
} else {
|
|
4210
|
-
const newDeque =
|
|
4737
|
+
const newDeque = this._createInstance({
|
|
4738
|
+
bucketSize: this._bucketSize,
|
|
4739
|
+
toElementFn: this._toElementFn,
|
|
4740
|
+
maxLen: this._maxLen
|
|
4741
|
+
});
|
|
4211
4742
|
for (let i = 0; i <= pos; i++) {
|
|
4212
4743
|
newDeque.push(this.at(i));
|
|
4213
4744
|
}
|
|
4214
4745
|
return newDeque;
|
|
4215
4746
|
}
|
|
4216
4747
|
}
|
|
4748
|
+
/**
|
|
4749
|
+
* Time Complexity: O(n)
|
|
4750
|
+
* Space Complexity: O(1)
|
|
4751
|
+
*
|
|
4752
|
+
* The `splice` function in TypeScript overrides the default behavior to remove and insert elements
|
|
4753
|
+
* in a Deque data structure while ensuring the starting position and delete count are within bounds.
|
|
4754
|
+
* @param {number} start - The `start` parameter in the `splice` method represents the index at which
|
|
4755
|
+
* to start changing the array. Items will be removed or added starting from this index.
|
|
4756
|
+
* @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
|
|
4757
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
4758
|
+
* `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
|
|
4759
|
+
* end of the array (`
|
|
4760
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
4761
|
+
* will be inserted into the deque at the specified `start` index. These elements will be inserted in
|
|
4762
|
+
* place of the elements that are removed based on the `start` and `deleteCount` parameters.
|
|
4763
|
+
* @returns The `splice` method is returning the array `deletedElements` which contains the elements
|
|
4764
|
+
* that were removed from the Deque during the splice operation.
|
|
4765
|
+
*/
|
|
4766
|
+
splice(start, deleteCount = this._length - start, ...items) {
|
|
4767
|
+
rangeCheck(start, 0, this._length);
|
|
4768
|
+
if (deleteCount < 0) deleteCount = 0;
|
|
4769
|
+
if (start + deleteCount > this._length) deleteCount = this._length - start;
|
|
4770
|
+
const deletedElements = this._createInstance();
|
|
4771
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
4772
|
+
deletedElements.push(this.at(start + i));
|
|
4773
|
+
}
|
|
4774
|
+
const elementsAfter = [];
|
|
4775
|
+
for (let i = start + deleteCount; i < this._length; i++) {
|
|
4776
|
+
elementsAfter.push(this.at(i));
|
|
4777
|
+
}
|
|
4778
|
+
this.cut(start - 1, true);
|
|
4779
|
+
for (const item of items) {
|
|
4780
|
+
this.push(item);
|
|
4781
|
+
}
|
|
4782
|
+
for (const element of elementsAfter) {
|
|
4783
|
+
this.push(element);
|
|
4784
|
+
}
|
|
4785
|
+
return deletedElements;
|
|
4786
|
+
}
|
|
4217
4787
|
/**
|
|
4218
4788
|
* Time Complexity: O(1)
|
|
4219
4789
|
* Space Complexity: O(1) or O(n)
|
|
@@ -4237,12 +4807,16 @@ var dataStructureTyped = (() => {
|
|
|
4237
4807
|
const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
|
|
4238
4808
|
this._bucketFirst = bucketIndex;
|
|
4239
4809
|
this._firstInBucket = indexInBucket;
|
|
4240
|
-
this.
|
|
4810
|
+
this._length = this._length - pos;
|
|
4241
4811
|
return this;
|
|
4242
4812
|
} else {
|
|
4243
|
-
const newDeque =
|
|
4813
|
+
const newDeque = this._createInstance({
|
|
4814
|
+
bucketSize: this._bucketSize,
|
|
4815
|
+
toElementFn: this._toElementFn,
|
|
4816
|
+
maxLen: this._maxLen
|
|
4817
|
+
});
|
|
4244
4818
|
if (pos < 0) pos = 0;
|
|
4245
|
-
for (let i = pos; i < this.
|
|
4819
|
+
for (let i = pos; i < this._length; i++) {
|
|
4246
4820
|
newDeque.push(this.at(i));
|
|
4247
4821
|
}
|
|
4248
4822
|
return newDeque;
|
|
@@ -4260,21 +4834,26 @@ var dataStructureTyped = (() => {
|
|
|
4260
4834
|
* @returns The size of the data structure after the deletion operation is performed.
|
|
4261
4835
|
*/
|
|
4262
4836
|
deleteAt(pos) {
|
|
4263
|
-
rangeCheck(pos, 0, this.
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4837
|
+
rangeCheck(pos, 0, this._length - 1);
|
|
4838
|
+
let deleted;
|
|
4839
|
+
if (pos === 0) {
|
|
4840
|
+
return this.shift();
|
|
4841
|
+
} else if (pos === this._length - 1) {
|
|
4842
|
+
deleted = this.last;
|
|
4843
|
+
this.pop();
|
|
4844
|
+
return deleted;
|
|
4845
|
+
} else {
|
|
4846
|
+
const length = this._length - 1;
|
|
4847
|
+
const { bucketIndex: targetBucket, indexInBucket: targetPointer } = this._getBucketAndPosition(pos);
|
|
4848
|
+
deleted = this._buckets[targetBucket][targetPointer];
|
|
4849
|
+
for (let i = pos; i < length; i++) {
|
|
4850
|
+
const { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(i);
|
|
4851
|
+
const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(i + 1);
|
|
4271
4852
|
this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
|
|
4272
|
-
curBucket = nextBucket;
|
|
4273
|
-
curPointer = nextPointer;
|
|
4274
4853
|
}
|
|
4275
4854
|
this.pop();
|
|
4855
|
+
return deleted;
|
|
4276
4856
|
}
|
|
4277
|
-
return true;
|
|
4278
4857
|
}
|
|
4279
4858
|
/**
|
|
4280
4859
|
* Time Complexity: O(n)
|
|
@@ -4287,7 +4866,7 @@ var dataStructureTyped = (() => {
|
|
|
4287
4866
|
* @returns The size of the data structure after the element has been deleted.
|
|
4288
4867
|
*/
|
|
4289
4868
|
delete(element) {
|
|
4290
|
-
const size = this.
|
|
4869
|
+
const size = this._length;
|
|
4291
4870
|
if (size === 0) return false;
|
|
4292
4871
|
let i = 0;
|
|
4293
4872
|
let index = 0;
|
|
@@ -4302,6 +4881,42 @@ var dataStructureTyped = (() => {
|
|
|
4302
4881
|
this.cut(index - 1, true);
|
|
4303
4882
|
return true;
|
|
4304
4883
|
}
|
|
4884
|
+
// /**
|
|
4885
|
+
// * Time Complexity: O(n)
|
|
4886
|
+
// * Space Complexity: O(1)
|
|
4887
|
+
// *
|
|
4888
|
+
// * This function overrides the indexOf method to search for an element within a custom data
|
|
4889
|
+
// * structure.
|
|
4890
|
+
// * @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
4891
|
+
// * within the data structure. The `indexOf` method will return the index of the first occurrence of
|
|
4892
|
+
// * this element within the data structure.
|
|
4893
|
+
// * @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
4894
|
+
// * index at which to start searching for the `searchElement` within the data structure. If provided,
|
|
4895
|
+
// * the search will begin at this index instead of the beginning of the data structure.
|
|
4896
|
+
// * @returns The indexOf method is returning the index of the searchElement if it is found in the data
|
|
4897
|
+
// * structure, or -1 if the searchElement is not found.
|
|
4898
|
+
// */
|
|
4899
|
+
// override indexOf(searchElement: E, fromIndex: number = 0): number {
|
|
4900
|
+
// let index = fromIndex;
|
|
4901
|
+
// let bucketIndex = this._bucketFirst;
|
|
4902
|
+
// let indexInBucket = this._firstInBucket + fromIndex;
|
|
4903
|
+
//
|
|
4904
|
+
// for (let i = 0; i < this._length; i++) {
|
|
4905
|
+
// if (this._buckets[bucketIndex][indexInBucket] === searchElement) {
|
|
4906
|
+
// return index;
|
|
4907
|
+
// }
|
|
4908
|
+
// index++;
|
|
4909
|
+
// indexInBucket++;
|
|
4910
|
+
// if (indexInBucket >= this._bucketSize) {
|
|
4911
|
+
// bucketIndex++;
|
|
4912
|
+
// indexInBucket = 0;
|
|
4913
|
+
// }
|
|
4914
|
+
// if (bucketIndex >= this._bucketCount) {
|
|
4915
|
+
// bucketIndex = 0;
|
|
4916
|
+
// }
|
|
4917
|
+
// }
|
|
4918
|
+
// return -1;
|
|
4919
|
+
// }
|
|
4305
4920
|
/**
|
|
4306
4921
|
* Time Complexity: O(n)
|
|
4307
4922
|
* Space Complexity: O(1)
|
|
@@ -4331,12 +4946,12 @@ var dataStructureTyped = (() => {
|
|
|
4331
4946
|
* @returns The size of the modified array is being returned.
|
|
4332
4947
|
*/
|
|
4333
4948
|
unique() {
|
|
4334
|
-
if (this.
|
|
4949
|
+
if (this._length <= 1) {
|
|
4335
4950
|
return this;
|
|
4336
4951
|
}
|
|
4337
4952
|
let index = 1;
|
|
4338
4953
|
let prev = this.at(0);
|
|
4339
|
-
for (let i = 1; i < this.
|
|
4954
|
+
for (let i = 1; i < this._length; ++i) {
|
|
4340
4955
|
const cur = this.at(i);
|
|
4341
4956
|
if (cur !== prev) {
|
|
4342
4957
|
prev = cur;
|
|
@@ -4346,27 +4961,6 @@ var dataStructureTyped = (() => {
|
|
|
4346
4961
|
this.cut(index - 1, true);
|
|
4347
4962
|
return this;
|
|
4348
4963
|
}
|
|
4349
|
-
/**
|
|
4350
|
-
* Time Complexity: O(n log n)
|
|
4351
|
-
* Space Complexity: O(n)
|
|
4352
|
-
*
|
|
4353
|
-
* The `sort` function sorts the elements in a data structure using a provided comparator function.
|
|
4354
|
-
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
4355
|
-
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
4356
|
-
* the elements in the sorted array.
|
|
4357
|
-
* @returns Deque<E>
|
|
4358
|
-
*/
|
|
4359
|
-
sort(comparator) {
|
|
4360
|
-
const arr = [];
|
|
4361
|
-
for (let i = 0; i < this._size; ++i) {
|
|
4362
|
-
arr.push(this.at(i));
|
|
4363
|
-
}
|
|
4364
|
-
arr.sort(comparator);
|
|
4365
|
-
for (let i = 0; i < this._size; ++i) {
|
|
4366
|
-
this.setAt(i, arr[i]);
|
|
4367
|
-
}
|
|
4368
|
-
return this;
|
|
4369
|
-
}
|
|
4370
4964
|
/**
|
|
4371
4965
|
* Time Complexity: O(n)
|
|
4372
4966
|
* Space Complexity: O(n)
|
|
@@ -4374,10 +4968,10 @@ var dataStructureTyped = (() => {
|
|
|
4374
4968
|
* The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
|
|
4375
4969
|
* memory usage.
|
|
4376
4970
|
* @returns Nothing is being returned. The function is using the `return` statement to exit early if
|
|
4377
|
-
* `this.
|
|
4971
|
+
* `this._length` is 0, but it does not return any value.
|
|
4378
4972
|
*/
|
|
4379
4973
|
shrinkToFit() {
|
|
4380
|
-
if (this.
|
|
4974
|
+
if (this._length === 0) return;
|
|
4381
4975
|
const newBuckets = [];
|
|
4382
4976
|
if (this._bucketFirst === this._bucketLast) return;
|
|
4383
4977
|
else if (this._bucketFirst < this._bucketLast) {
|
|
@@ -4396,35 +4990,6 @@ var dataStructureTyped = (() => {
|
|
|
4396
4990
|
this._bucketLast = newBuckets.length - 1;
|
|
4397
4991
|
this._buckets = newBuckets;
|
|
4398
4992
|
}
|
|
4399
|
-
/**
|
|
4400
|
-
* Time Complexity: O(n)
|
|
4401
|
-
* Space Complexity: O(1)
|
|
4402
|
-
*
|
|
4403
|
-
* The function "indexOf" returns the index of the first occurrence of a given element in an array,
|
|
4404
|
-
* or -1 if the element is not found.
|
|
4405
|
-
* @param {E} element - The "element" parameter represents the element that you want to find the
|
|
4406
|
-
* index of in the data structure.
|
|
4407
|
-
* @returns The indexOf function returns the index of the first occurrence of the specified element
|
|
4408
|
-
* in the data structure. If the element is not found, it returns -1.
|
|
4409
|
-
*/
|
|
4410
|
-
indexOf(element) {
|
|
4411
|
-
for (let i = 0; i < this._size; ++i) {
|
|
4412
|
-
if (this.at(i) === element) {
|
|
4413
|
-
return i;
|
|
4414
|
-
}
|
|
4415
|
-
}
|
|
4416
|
-
return -1;
|
|
4417
|
-
}
|
|
4418
|
-
/**
|
|
4419
|
-
* Time Complexity: O(n)
|
|
4420
|
-
* Space Complexity: O(n)
|
|
4421
|
-
*
|
|
4422
|
-
* The `toArray` function converts the elements of a data structure into an array.
|
|
4423
|
-
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
4424
|
-
*/
|
|
4425
|
-
toArray() {
|
|
4426
|
-
return [...this];
|
|
4427
|
-
}
|
|
4428
4993
|
/**
|
|
4429
4994
|
* Time Complexity: O(n)
|
|
4430
4995
|
* Space Complexity: O(n)
|
|
@@ -4435,7 +5000,11 @@ var dataStructureTyped = (() => {
|
|
|
4435
5000
|
* elements as the original deque (`this`) and the same bucket size.
|
|
4436
5001
|
*/
|
|
4437
5002
|
clone() {
|
|
4438
|
-
return new _Deque(this, {
|
|
5003
|
+
return new _Deque(this, {
|
|
5004
|
+
bucketSize: this.bucketSize,
|
|
5005
|
+
toElementFn: this.toElementFn,
|
|
5006
|
+
maxLen: this._maxLen
|
|
5007
|
+
});
|
|
4439
5008
|
}
|
|
4440
5009
|
/**
|
|
4441
5010
|
* Time Complexity: O(n)
|
|
@@ -4454,7 +5023,11 @@ var dataStructureTyped = (() => {
|
|
|
4454
5023
|
* satisfy the given predicate function.
|
|
4455
5024
|
*/
|
|
4456
5025
|
filter(predicate, thisArg) {
|
|
4457
|
-
const newDeque =
|
|
5026
|
+
const newDeque = this._createInstance({
|
|
5027
|
+
bucketSize: this._bucketSize,
|
|
5028
|
+
toElementFn: this.toElementFn,
|
|
5029
|
+
maxLen: this._maxLen
|
|
5030
|
+
});
|
|
4458
5031
|
let index = 0;
|
|
4459
5032
|
for (const el of this) {
|
|
4460
5033
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -4483,7 +5056,7 @@ var dataStructureTyped = (() => {
|
|
|
4483
5056
|
* @returns a new Deque object with elements of type EM and raw elements of type RM.
|
|
4484
5057
|
*/
|
|
4485
5058
|
map(callback, toElementFn, thisArg) {
|
|
4486
|
-
const newDeque = new _Deque([], { bucketSize: this._bucketSize, toElementFn });
|
|
5059
|
+
const newDeque = new _Deque([], { bucketSize: this._bucketSize, toElementFn, maxLen: this._maxLen });
|
|
4487
5060
|
let index = 0;
|
|
4488
5061
|
for (const el of this) {
|
|
4489
5062
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -4499,7 +5072,7 @@ var dataStructureTyped = (() => {
|
|
|
4499
5072
|
* object to be iterated over using a for...of loop.
|
|
4500
5073
|
*/
|
|
4501
5074
|
*_getIterator() {
|
|
4502
|
-
for (let i = 0; i < this.
|
|
5075
|
+
for (let i = 0; i < this._length; ++i) {
|
|
4503
5076
|
yield this.at(i);
|
|
4504
5077
|
}
|
|
4505
5078
|
}
|
|
@@ -4556,6 +5129,26 @@ var dataStructureTyped = (() => {
|
|
|
4556
5129
|
}
|
|
4557
5130
|
return { bucketIndex, indexInBucket };
|
|
4558
5131
|
}
|
|
5132
|
+
/**
|
|
5133
|
+
* The function `_createInstance` returns a new instance of the `Deque` class with the specified
|
|
5134
|
+
* options.
|
|
5135
|
+
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
5136
|
+
* `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
|
|
5137
|
+
* configuration options when creating a new instance of the `Deque` class.
|
|
5138
|
+
* @returns An instance of the `Deque` class with an empty array and the provided options, casted as
|
|
5139
|
+
* `this`.
|
|
5140
|
+
*/
|
|
5141
|
+
_createInstance(options) {
|
|
5142
|
+
return new _Deque([], options);
|
|
5143
|
+
}
|
|
5144
|
+
/**
|
|
5145
|
+
* This function returns an iterator that iterates over elements in reverse order.
|
|
5146
|
+
*/
|
|
5147
|
+
*_getReverseIterator() {
|
|
5148
|
+
for (let i = this._length - 1; i > -1; i--) {
|
|
5149
|
+
yield this.at(i);
|
|
5150
|
+
}
|
|
5151
|
+
}
|
|
4559
5152
|
};
|
|
4560
5153
|
|
|
4561
5154
|
// src/data-structures/heap/heap.ts
|
|
@@ -4779,16 +5372,6 @@ var dataStructureTyped = (() => {
|
|
|
4779
5372
|
_dfs(0);
|
|
4780
5373
|
return result;
|
|
4781
5374
|
}
|
|
4782
|
-
/**
|
|
4783
|
-
* Time Complexity: O(n)
|
|
4784
|
-
* Space Complexity: O(n)
|
|
4785
|
-
*
|
|
4786
|
-
* Convert the heap to an array.
|
|
4787
|
-
* @returns An array containing the elements of the heap.
|
|
4788
|
-
*/
|
|
4789
|
-
toArray() {
|
|
4790
|
-
return [...this.elements];
|
|
4791
|
-
}
|
|
4792
5375
|
/**
|
|
4793
5376
|
* Time Complexity: O(n)
|
|
4794
5377
|
* Space Complexity: O(n)
|
|
@@ -5673,7 +6256,7 @@ var dataStructureTyped = (() => {
|
|
|
5673
6256
|
if (isWeight === void 0) isWeight = false;
|
|
5674
6257
|
if (isWeight) {
|
|
5675
6258
|
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
5676
|
-
let min =
|
|
6259
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
5677
6260
|
for (const path of allPaths) {
|
|
5678
6261
|
min = Math.min(this.getPathSumWeight(path), min);
|
|
5679
6262
|
}
|
|
@@ -5688,8 +6271,8 @@ var dataStructureTyped = (() => {
|
|
|
5688
6271
|
const queue = new Queue([vertex1]);
|
|
5689
6272
|
visited.set(vertex1, true);
|
|
5690
6273
|
let cost = 0;
|
|
5691
|
-
while (queue.
|
|
5692
|
-
for (let i = 0; i < queue.
|
|
6274
|
+
while (queue.length > 0) {
|
|
6275
|
+
for (let i = 0; i < queue.length; i++) {
|
|
5693
6276
|
const cur = queue.shift();
|
|
5694
6277
|
if (cur === vertex2) {
|
|
5695
6278
|
return cost;
|
|
@@ -5734,7 +6317,7 @@ var dataStructureTyped = (() => {
|
|
|
5734
6317
|
if (isWeight) {
|
|
5735
6318
|
if (isDFS) {
|
|
5736
6319
|
const allPaths = this.getAllPathsBetween(v1, v2, 1e4);
|
|
5737
|
-
let min =
|
|
6320
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
5738
6321
|
let minIndex = -1;
|
|
5739
6322
|
let index = 0;
|
|
5740
6323
|
for (const path of allPaths) {
|
|
@@ -5794,7 +6377,7 @@ var dataStructureTyped = (() => {
|
|
|
5794
6377
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
5795
6378
|
*/
|
|
5796
6379
|
dijkstraWithoutHeap(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
5797
|
-
let minDist =
|
|
6380
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
5798
6381
|
let minDest = void 0;
|
|
5799
6382
|
let minPath = [];
|
|
5800
6383
|
const paths = [];
|
|
@@ -5809,12 +6392,12 @@ var dataStructureTyped = (() => {
|
|
|
5809
6392
|
}
|
|
5810
6393
|
for (const vertex of vertexMap) {
|
|
5811
6394
|
const vertexOrKey = vertex[1];
|
|
5812
|
-
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey,
|
|
6395
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
5813
6396
|
}
|
|
5814
6397
|
distMap.set(srcVertex, 0);
|
|
5815
6398
|
preMap.set(srcVertex, void 0);
|
|
5816
6399
|
const getMinOfNoSeen = () => {
|
|
5817
|
-
let min =
|
|
6400
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
5818
6401
|
let minV = void 0;
|
|
5819
6402
|
for (const [key, value] of distMap) {
|
|
5820
6403
|
if (!seen.has(key)) {
|
|
@@ -5848,7 +6431,7 @@ var dataStructureTyped = (() => {
|
|
|
5848
6431
|
seen.add(cur);
|
|
5849
6432
|
if (destVertex && destVertex === cur) {
|
|
5850
6433
|
if (getMinDist) {
|
|
5851
|
-
minDist = distMap.get(destVertex) ||
|
|
6434
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
5852
6435
|
}
|
|
5853
6436
|
if (genPaths) {
|
|
5854
6437
|
getPaths(destVertex);
|
|
@@ -5907,7 +6490,7 @@ var dataStructureTyped = (() => {
|
|
|
5907
6490
|
*/
|
|
5908
6491
|
dijkstra(src, dest = void 0, getMinDist = false, genPaths = false) {
|
|
5909
6492
|
var _a;
|
|
5910
|
-
let minDist =
|
|
6493
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
5911
6494
|
let minDest = void 0;
|
|
5912
6495
|
let minPath = [];
|
|
5913
6496
|
const paths = [];
|
|
@@ -5920,7 +6503,7 @@ var dataStructureTyped = (() => {
|
|
|
5920
6503
|
if (!srcVertex) return void 0;
|
|
5921
6504
|
for (const vertex of vertexMap) {
|
|
5922
6505
|
const vertexOrKey = vertex[1];
|
|
5923
|
-
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey,
|
|
6506
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
5924
6507
|
}
|
|
5925
6508
|
const heap = new Heap([], { comparator: (a, b) => a.key - b.key });
|
|
5926
6509
|
heap.add({ key: 0, value: srcVertex });
|
|
@@ -5951,7 +6534,7 @@ var dataStructureTyped = (() => {
|
|
|
5951
6534
|
seen.add(cur);
|
|
5952
6535
|
if (destVertex && destVertex === cur) {
|
|
5953
6536
|
if (getMinDist) {
|
|
5954
|
-
minDist = distMap.get(destVertex) ||
|
|
6537
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
5955
6538
|
}
|
|
5956
6539
|
if (genPaths) {
|
|
5957
6540
|
getPaths(destVertex);
|
|
@@ -6017,7 +6600,7 @@ var dataStructureTyped = (() => {
|
|
|
6017
6600
|
const paths = [];
|
|
6018
6601
|
const distMap = /* @__PURE__ */ new Map();
|
|
6019
6602
|
const preMap = /* @__PURE__ */ new Map();
|
|
6020
|
-
let min =
|
|
6603
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
6021
6604
|
let minPath = [];
|
|
6022
6605
|
let hasNegativeCycle;
|
|
6023
6606
|
if (scanNegativeCycle) hasNegativeCycle = false;
|
|
@@ -6027,7 +6610,7 @@ var dataStructureTyped = (() => {
|
|
|
6027
6610
|
const edgeMap = this.edgeSet();
|
|
6028
6611
|
const numOfEdges = edgeMap.length;
|
|
6029
6612
|
this._vertexMap.forEach((vertex) => {
|
|
6030
|
-
distMap.set(vertex,
|
|
6613
|
+
distMap.set(vertex, Number.MAX_SAFE_INTEGER);
|
|
6031
6614
|
});
|
|
6032
6615
|
distMap.set(srcVertex, 0);
|
|
6033
6616
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
@@ -6039,7 +6622,7 @@ var dataStructureTyped = (() => {
|
|
|
6039
6622
|
const sWeight = distMap.get(s);
|
|
6040
6623
|
const dWeight = distMap.get(d);
|
|
6041
6624
|
if (sWeight !== void 0 && dWeight !== void 0) {
|
|
6042
|
-
if (distMap.get(s) !==
|
|
6625
|
+
if (distMap.get(s) !== Number.MAX_SAFE_INTEGER && sWeight + weight < dWeight) {
|
|
6043
6626
|
distMap.set(d, sWeight + weight);
|
|
6044
6627
|
if (genPath) preMap.set(d, s);
|
|
6045
6628
|
}
|
|
@@ -6081,7 +6664,7 @@ var dataStructureTyped = (() => {
|
|
|
6081
6664
|
const weight = edgeMap[j].weight;
|
|
6082
6665
|
const sWeight = distMap.get(s);
|
|
6083
6666
|
if (sWeight) {
|
|
6084
|
-
if (sWeight !==
|
|
6667
|
+
if (sWeight !== Number.MAX_SAFE_INTEGER && sWeight + weight < sWeight) hasNegativeCycle = true;
|
|
6085
6668
|
}
|
|
6086
6669
|
}
|
|
6087
6670
|
}
|
|
@@ -6129,7 +6712,7 @@ var dataStructureTyped = (() => {
|
|
|
6129
6712
|
}
|
|
6130
6713
|
for (let i = 0; i < n; i++) {
|
|
6131
6714
|
for (let j = 0; j < n; j++) {
|
|
6132
|
-
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) == null ? void 0 : _a.weight) ||
|
|
6715
|
+
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) == null ? void 0 : _a.weight) || Number.MAX_SAFE_INTEGER;
|
|
6133
6716
|
}
|
|
6134
6717
|
}
|
|
6135
6718
|
for (let k = 0; k < n; k++) {
|
|
@@ -7741,7 +8324,7 @@ var dataStructureTyped = (() => {
|
|
|
7741
8324
|
}
|
|
7742
8325
|
const queue = new Queue([this._root]);
|
|
7743
8326
|
let potentialParent;
|
|
7744
|
-
while (queue.
|
|
8327
|
+
while (queue.length > 0) {
|
|
7745
8328
|
const cur = queue.shift();
|
|
7746
8329
|
if (!cur) continue;
|
|
7747
8330
|
if (!this._isDuplicate) {
|
|
@@ -8495,7 +9078,7 @@ var dataStructureTyped = (() => {
|
|
|
8495
9078
|
startNode
|
|
8496
9079
|
]);
|
|
8497
9080
|
const dfs = (level) => {
|
|
8498
|
-
if (queue.
|
|
9081
|
+
if (queue.length === 0) return;
|
|
8499
9082
|
const current = queue.shift();
|
|
8500
9083
|
ans.push(callback(current));
|
|
8501
9084
|
if (includeNull) {
|
|
@@ -8510,8 +9093,8 @@ var dataStructureTyped = (() => {
|
|
|
8510
9093
|
dfs(0);
|
|
8511
9094
|
} else {
|
|
8512
9095
|
const queue = new Queue([startNode]);
|
|
8513
|
-
while (queue.
|
|
8514
|
-
const levelSize = queue.
|
|
9096
|
+
while (queue.length > 0) {
|
|
9097
|
+
const levelSize = queue.length;
|
|
8515
9098
|
for (let i = 0; i < levelSize; i++) {
|
|
8516
9099
|
const current = queue.shift();
|
|
8517
9100
|
ans.push(callback(current));
|
|
@@ -8561,7 +9144,7 @@ var dataStructureTyped = (() => {
|
|
|
8561
9144
|
dfs(startNode);
|
|
8562
9145
|
} else {
|
|
8563
9146
|
const queue = new Queue([startNode]);
|
|
8564
|
-
while (queue.
|
|
9147
|
+
while (queue.length > 0) {
|
|
8565
9148
|
const cur = queue.shift();
|
|
8566
9149
|
if (this.isRealNode(cur)) {
|
|
8567
9150
|
if (this.isLeaf(cur)) {
|
|
@@ -9595,22 +10178,20 @@ var dataStructureTyped = (() => {
|
|
|
9595
10178
|
return 0;
|
|
9596
10179
|
});
|
|
9597
10180
|
const _dfs = (arr) => {
|
|
9598
|
-
var _a;
|
|
9599
10181
|
if (arr.length === 0) return;
|
|
9600
10182
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
9601
|
-
|
|
10183
|
+
const { key, value } = arr[mid];
|
|
9602
10184
|
const { orgIndex } = arr[mid];
|
|
9603
10185
|
if (this.isRaw(key)) {
|
|
9604
10186
|
const entry = this._toEntryFn(key);
|
|
9605
|
-
|
|
9606
|
-
|
|
10187
|
+
inserted[orgIndex] = this.add(entry);
|
|
10188
|
+
} else {
|
|
10189
|
+
inserted[orgIndex] = this.add(key, value);
|
|
9607
10190
|
}
|
|
9608
|
-
inserted[orgIndex] = this.add(key, value);
|
|
9609
10191
|
_dfs(arr.slice(0, mid));
|
|
9610
10192
|
_dfs(arr.slice(mid + 1));
|
|
9611
10193
|
};
|
|
9612
10194
|
const _iterate = () => {
|
|
9613
|
-
var _a;
|
|
9614
10195
|
const n = sorted.length;
|
|
9615
10196
|
const stack = [[0, n - 1]];
|
|
9616
10197
|
while (stack.length > 0) {
|
|
@@ -9619,14 +10200,14 @@ var dataStructureTyped = (() => {
|
|
|
9619
10200
|
const [l, r] = popped;
|
|
9620
10201
|
if (l <= r) {
|
|
9621
10202
|
const m = l + Math.floor((r - l) / 2);
|
|
9622
|
-
|
|
10203
|
+
const { key, value } = sorted[m];
|
|
9623
10204
|
const { orgIndex } = sorted[m];
|
|
9624
10205
|
if (this.isRaw(key)) {
|
|
9625
10206
|
const entry = this._toEntryFn(key);
|
|
9626
|
-
|
|
9627
|
-
|
|
10207
|
+
inserted[orgIndex] = this.add(entry);
|
|
10208
|
+
} else {
|
|
10209
|
+
inserted[orgIndex] = this.add(key, value);
|
|
9628
10210
|
}
|
|
9629
|
-
inserted[orgIndex] = this.add(key, value);
|
|
9630
10211
|
stack.push([m + 1, r]);
|
|
9631
10212
|
stack.push([l, m - 1]);
|
|
9632
10213
|
}
|
|
@@ -9886,7 +10467,7 @@ var dataStructureTyped = (() => {
|
|
|
9886
10467
|
return ans;
|
|
9887
10468
|
} else {
|
|
9888
10469
|
const queue = new Queue([this._root]);
|
|
9889
|
-
while (queue.
|
|
10470
|
+
while (queue.length > 0) {
|
|
9890
10471
|
const cur = queue.shift();
|
|
9891
10472
|
if (this.isRealNode(cur)) {
|
|
9892
10473
|
const compared = this._compare(cur.key, targetKey);
|
|
@@ -11715,63 +12296,77 @@ var dataStructureTyped = (() => {
|
|
|
11715
12296
|
iterationType: this.iterationType,
|
|
11716
12297
|
specifyComparable: this._specifyComparable,
|
|
11717
12298
|
toEntryFn: this._toEntryFn,
|
|
11718
|
-
isReverse: this._isReverse
|
|
12299
|
+
isReverse: this._isReverse,
|
|
12300
|
+
isMapMode: this._isMapMode
|
|
11719
12301
|
}, options));
|
|
11720
12302
|
}
|
|
11721
12303
|
/**
|
|
11722
12304
|
* Time Complexity: O(1)
|
|
11723
12305
|
* Space Complexity: O(1)
|
|
11724
12306
|
*
|
|
11725
|
-
* The
|
|
11726
|
-
* specified key and
|
|
11727
|
-
* @param {K} key - The `key` parameter
|
|
11728
|
-
*
|
|
11729
|
-
* @
|
|
11730
|
-
*
|
|
12307
|
+
* The `createNode` function in TypeScript overrides the default implementation to create a new
|
|
12308
|
+
* AVLTreeMultiMapNode with a specified key and value array.
|
|
12309
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
12310
|
+
* AVLTreeMultiMap.
|
|
12311
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
12312
|
+
* values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
|
|
12313
|
+
* calling the method, an empty array `[]` is used as the default value.
|
|
12314
|
+
* @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
|
|
12315
|
+
* AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
|
|
12316
|
+
* array is used.
|
|
11731
12317
|
*/
|
|
11732
|
-
createNode(key) {
|
|
11733
|
-
return new AVLTreeMultiMapNode(key, []);
|
|
12318
|
+
createNode(key, value = []) {
|
|
12319
|
+
return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
11734
12320
|
}
|
|
11735
12321
|
/**
|
|
11736
12322
|
* Time Complexity: O(log n)
|
|
11737
12323
|
* Space Complexity: O(log n)
|
|
11738
12324
|
*
|
|
11739
|
-
* The function `add` in TypeScript overrides the superclass method to add key-value pairs
|
|
11740
|
-
*
|
|
11741
|
-
* @param
|
|
11742
|
-
*
|
|
11743
|
-
*
|
|
11744
|
-
* `values`
|
|
11745
|
-
*
|
|
11746
|
-
*
|
|
11747
|
-
*
|
|
11748
|
-
*
|
|
11749
|
-
* addition operation was successful or not.
|
|
12325
|
+
* The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
|
|
12326
|
+
* to an AVLTreeMultiMap, handling different input types and scenarios.
|
|
12327
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
12328
|
+
* be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
|
|
12329
|
+
* can be a single key value, a node of the AVLTree
|
|
12330
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
12331
|
+
* that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
|
|
12332
|
+
* specific key.
|
|
12333
|
+
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
12334
|
+
* successful or not.
|
|
11750
12335
|
*/
|
|
11751
12336
|
add(keyNodeOrEntry, value) {
|
|
11752
12337
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
11753
12338
|
const _commonAdd = (key, values) => {
|
|
11754
12339
|
if (key === void 0 || key === null) return false;
|
|
11755
|
-
const
|
|
11756
|
-
|
|
11757
|
-
|
|
11758
|
-
return true;
|
|
11759
|
-
}
|
|
11760
|
-
const existingNode = this.getNode(key);
|
|
11761
|
-
if (this.isRealNode(existingNode)) {
|
|
11762
|
-
if (existingValues === void 0) {
|
|
11763
|
-
super.add(key, values);
|
|
11764
|
-
return true;
|
|
11765
|
-
}
|
|
11766
|
-
if (values !== void 0) {
|
|
12340
|
+
const _addToValues = () => {
|
|
12341
|
+
const existingValues = this.get(key);
|
|
12342
|
+
if (existingValues !== void 0 && values !== void 0) {
|
|
11767
12343
|
for (const value2 of values) existingValues.push(value2);
|
|
11768
12344
|
return true;
|
|
12345
|
+
}
|
|
12346
|
+
return false;
|
|
12347
|
+
};
|
|
12348
|
+
const _addByNode = () => {
|
|
12349
|
+
const existingNode = this.getNode(key);
|
|
12350
|
+
if (this.isRealNode(existingNode)) {
|
|
12351
|
+
const existingValues = this.get(existingNode);
|
|
12352
|
+
if (existingValues === void 0) {
|
|
12353
|
+
super.add(key, values);
|
|
12354
|
+
return true;
|
|
12355
|
+
}
|
|
12356
|
+
if (values !== void 0) {
|
|
12357
|
+
for (const value2 of values) existingValues.push(value2);
|
|
12358
|
+
return true;
|
|
12359
|
+
} else {
|
|
12360
|
+
return false;
|
|
12361
|
+
}
|
|
11769
12362
|
} else {
|
|
11770
|
-
return
|
|
12363
|
+
return super.add(key, values);
|
|
11771
12364
|
}
|
|
11772
|
-
}
|
|
11773
|
-
|
|
12365
|
+
};
|
|
12366
|
+
if (this._isMapMode) {
|
|
12367
|
+
return _addByNode() || _addToValues();
|
|
11774
12368
|
}
|
|
12369
|
+
return _addToValues() || _addByNode();
|
|
11775
12370
|
};
|
|
11776
12371
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11777
12372
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -11871,7 +12466,7 @@ var dataStructureTyped = (() => {
|
|
|
11871
12466
|
* additional options for configuring the TreeMultiMap instance.
|
|
11872
12467
|
*/
|
|
11873
12468
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
11874
|
-
super([],
|
|
12469
|
+
super([], __spreadValues({}, options));
|
|
11875
12470
|
if (keysNodesEntriesOrRaws) {
|
|
11876
12471
|
this.addMany(keysNodesEntriesOrRaws);
|
|
11877
12472
|
}
|
|
@@ -11894,35 +12489,38 @@ var dataStructureTyped = (() => {
|
|
|
11894
12489
|
iterationType: this.iterationType,
|
|
11895
12490
|
specifyComparable: this._specifyComparable,
|
|
11896
12491
|
toEntryFn: this._toEntryFn,
|
|
11897
|
-
isReverse: this._isReverse
|
|
12492
|
+
isReverse: this._isReverse,
|
|
12493
|
+
isMapMode: this._isMapMode
|
|
11898
12494
|
}, options));
|
|
11899
12495
|
}
|
|
11900
12496
|
/**
|
|
11901
12497
|
* Time Complexity: O(1)
|
|
11902
12498
|
* Space Complexity: O(1)
|
|
11903
12499
|
*
|
|
11904
|
-
* The function `createNode` overrides the
|
|
11905
|
-
*
|
|
11906
|
-
* @param {K} key - The `key` parameter
|
|
11907
|
-
*
|
|
11908
|
-
* @
|
|
11909
|
-
*
|
|
12500
|
+
* The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
|
|
12501
|
+
* and value array.
|
|
12502
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
12503
|
+
* `TreeMultiMap`.
|
|
12504
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
12505
|
+
* values associated with a specific key in the TreeMultiMap data structure.
|
|
12506
|
+
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
|
|
12507
|
+
* value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
|
|
12508
|
+
* value is used.
|
|
11910
12509
|
*/
|
|
11911
|
-
createNode(key) {
|
|
11912
|
-
return new TreeMultiMapNode(key, []);
|
|
12510
|
+
createNode(key, value = []) {
|
|
12511
|
+
return new TreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
11913
12512
|
}
|
|
11914
12513
|
/**
|
|
11915
12514
|
* Time Complexity: O(log n)
|
|
11916
12515
|
* Space Complexity: O(log n)
|
|
11917
12516
|
*
|
|
11918
|
-
* The function
|
|
11919
|
-
*
|
|
11920
|
-
* @param
|
|
11921
|
-
*
|
|
11922
|
-
*
|
|
11923
|
-
* @param {V} [
|
|
11924
|
-
* you want to add to the TreeMultiMap.
|
|
11925
|
-
* be added to the existing list of values associated with that key. If the key is not present,
|
|
12517
|
+
* The function overrides the add method to handle different types of input for a TreeMultiMap data
|
|
12518
|
+
* structure.
|
|
12519
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
12520
|
+
* be added to the TreeMultiMap. It can be of type `K`, which is the key type of the TreeMultiMap, or
|
|
12521
|
+
* it can be a TreeMultiMapNode containing the key and its
|
|
12522
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
12523
|
+
* that you want to add to the TreeMultiMap. It can contain one or more values of type `V`.
|
|
11926
12524
|
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
11927
12525
|
* successful or not.
|
|
11928
12526
|
*/
|
|
@@ -11930,26 +12528,36 @@ var dataStructureTyped = (() => {
|
|
|
11930
12528
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
11931
12529
|
const _commonAdd = (key, values) => {
|
|
11932
12530
|
if (key === void 0 || key === null) return false;
|
|
11933
|
-
const
|
|
11934
|
-
|
|
11935
|
-
|
|
11936
|
-
return true;
|
|
11937
|
-
}
|
|
11938
|
-
const existingNode = this.getNode(key);
|
|
11939
|
-
if (this.isRealNode(existingNode)) {
|
|
11940
|
-
if (existingValues === void 0) {
|
|
11941
|
-
super.add(key, values);
|
|
11942
|
-
return true;
|
|
11943
|
-
}
|
|
11944
|
-
if (values !== void 0) {
|
|
12531
|
+
const _addToValues = () => {
|
|
12532
|
+
const existingValues = this.get(key);
|
|
12533
|
+
if (existingValues !== void 0 && values !== void 0) {
|
|
11945
12534
|
for (const value2 of values) existingValues.push(value2);
|
|
11946
12535
|
return true;
|
|
12536
|
+
}
|
|
12537
|
+
return false;
|
|
12538
|
+
};
|
|
12539
|
+
const _addByNode = () => {
|
|
12540
|
+
const existingNode = this.getNode(key);
|
|
12541
|
+
if (this.isRealNode(existingNode)) {
|
|
12542
|
+
const existingValues = this.get(existingNode);
|
|
12543
|
+
if (existingValues === void 0) {
|
|
12544
|
+
super.add(key, values);
|
|
12545
|
+
return true;
|
|
12546
|
+
}
|
|
12547
|
+
if (values !== void 0) {
|
|
12548
|
+
for (const value2 of values) existingValues.push(value2);
|
|
12549
|
+
return true;
|
|
12550
|
+
} else {
|
|
12551
|
+
return false;
|
|
12552
|
+
}
|
|
11947
12553
|
} else {
|
|
11948
|
-
return
|
|
12554
|
+
return super.add(key, values);
|
|
11949
12555
|
}
|
|
11950
|
-
}
|
|
11951
|
-
|
|
12556
|
+
};
|
|
12557
|
+
if (this._isMapMode) {
|
|
12558
|
+
return _addByNode() || _addToValues();
|
|
11952
12559
|
}
|
|
12560
|
+
return _addToValues() || _addByNode();
|
|
11953
12561
|
};
|
|
11954
12562
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
11955
12563
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -14050,6 +14658,9 @@ var dataStructureTyped = (() => {
|
|
|
14050
14658
|
}
|
|
14051
14659
|
yield* __yieldStar(_dfs(this.root, ""));
|
|
14052
14660
|
}
|
|
14661
|
+
get _total() {
|
|
14662
|
+
return this._size;
|
|
14663
|
+
}
|
|
14053
14664
|
/**
|
|
14054
14665
|
* Time Complexity: O(l), where l is the length of the input string.
|
|
14055
14666
|
* Space Complexity: O(1) - Constant space.
|