data-structure-typed 1.49.1 → 1.49.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +126 -129
- 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 +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +13 -14
- package/dist/cjs/data-structures/queue/queue.js +15 -18
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +87 -93
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +125 -128
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +13 -14
- package/dist/mjs/data-structures/queue/queue.js +15 -18
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +338 -370
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +138 -142
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +19 -23
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
|
@@ -616,6 +616,20 @@ var dataStructureTyped = (() => {
|
|
|
616
616
|
}
|
|
617
617
|
return accumulator;
|
|
618
618
|
}
|
|
619
|
+
hasValue(value) {
|
|
620
|
+
for (const [, elementValue] of this) {
|
|
621
|
+
if (elementValue === value)
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Time Complexity: O(n)
|
|
628
|
+
* Space Complexity: O(n)
|
|
629
|
+
*/
|
|
630
|
+
print() {
|
|
631
|
+
console.log([...this]);
|
|
632
|
+
}
|
|
619
633
|
};
|
|
620
634
|
var IterableElementBase = class {
|
|
621
635
|
/**
|
|
@@ -751,6 +765,13 @@ var dataStructureTyped = (() => {
|
|
|
751
765
|
}
|
|
752
766
|
return accumulator;
|
|
753
767
|
}
|
|
768
|
+
/**
|
|
769
|
+
* Time Complexity: O(n)
|
|
770
|
+
* Space Complexity: O(n)
|
|
771
|
+
*/
|
|
772
|
+
print() {
|
|
773
|
+
console.log([...this]);
|
|
774
|
+
}
|
|
754
775
|
};
|
|
755
776
|
|
|
756
777
|
// src/utils/utils.ts
|
|
@@ -886,6 +907,7 @@ var dataStructureTyped = (() => {
|
|
|
886
907
|
}
|
|
887
908
|
this._store[strKey] = { key, value };
|
|
888
909
|
}
|
|
910
|
+
return true;
|
|
889
911
|
}
|
|
890
912
|
/**
|
|
891
913
|
* The function "setMany" sets multiple key-value pairs in a map.
|
|
@@ -893,8 +915,10 @@ var dataStructureTyped = (() => {
|
|
|
893
915
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
894
916
|
*/
|
|
895
917
|
setMany(elements) {
|
|
918
|
+
const results = [];
|
|
896
919
|
for (const [key, value] of elements)
|
|
897
|
-
this.set(key, value);
|
|
920
|
+
results.push(this.set(key, value));
|
|
921
|
+
return results;
|
|
898
922
|
}
|
|
899
923
|
/**
|
|
900
924
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
@@ -1009,6 +1033,9 @@ var dataStructureTyped = (() => {
|
|
|
1009
1033
|
print() {
|
|
1010
1034
|
console.log([...this.entries()]);
|
|
1011
1035
|
}
|
|
1036
|
+
put(key, value) {
|
|
1037
|
+
return this.set(key, value);
|
|
1038
|
+
}
|
|
1012
1039
|
/**
|
|
1013
1040
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
1014
1041
|
* object map.
|
|
@@ -1160,7 +1187,7 @@ var dataStructureTyped = (() => {
|
|
|
1160
1187
|
this._sentinel.prev = node;
|
|
1161
1188
|
this._size++;
|
|
1162
1189
|
}
|
|
1163
|
-
return
|
|
1190
|
+
return true;
|
|
1164
1191
|
}
|
|
1165
1192
|
has(key) {
|
|
1166
1193
|
if (isWeakKey(key)) {
|
|
@@ -1171,18 +1198,13 @@ var dataStructureTyped = (() => {
|
|
|
1171
1198
|
return hash in this._noObjMap;
|
|
1172
1199
|
}
|
|
1173
1200
|
}
|
|
1174
|
-
hasValue(value) {
|
|
1175
|
-
for (const [, elementValue] of this) {
|
|
1176
|
-
if (elementValue === value)
|
|
1177
|
-
return true;
|
|
1178
|
-
}
|
|
1179
|
-
return false;
|
|
1180
|
-
}
|
|
1181
1201
|
setMany(entries) {
|
|
1202
|
+
const results = [];
|
|
1182
1203
|
for (const entry of entries) {
|
|
1183
1204
|
const [key, value] = entry;
|
|
1184
|
-
this.set(key, value);
|
|
1205
|
+
results.push(this.set(key, value));
|
|
1185
1206
|
}
|
|
1207
|
+
return results;
|
|
1186
1208
|
}
|
|
1187
1209
|
/**
|
|
1188
1210
|
* Time Complexity: O(1)
|
|
@@ -1225,7 +1247,7 @@ var dataStructureTyped = (() => {
|
|
|
1225
1247
|
while (index--) {
|
|
1226
1248
|
node = node.next;
|
|
1227
1249
|
}
|
|
1228
|
-
return
|
|
1250
|
+
return node.value;
|
|
1229
1251
|
}
|
|
1230
1252
|
/**
|
|
1231
1253
|
* Time Complexity: O(1)
|
|
@@ -1272,8 +1294,7 @@ var dataStructureTyped = (() => {
|
|
|
1272
1294
|
while (index--) {
|
|
1273
1295
|
node = node.next;
|
|
1274
1296
|
}
|
|
1275
|
-
this._deleteNode(node);
|
|
1276
|
-
return this._size;
|
|
1297
|
+
return this._deleteNode(node);
|
|
1277
1298
|
}
|
|
1278
1299
|
/**
|
|
1279
1300
|
* Time Complexity: O(1)
|
|
@@ -1366,8 +1387,8 @@ var dataStructureTyped = (() => {
|
|
|
1366
1387
|
}
|
|
1367
1388
|
return mappedMap;
|
|
1368
1389
|
}
|
|
1369
|
-
|
|
1370
|
-
|
|
1390
|
+
put(key, value) {
|
|
1391
|
+
return this.set(key, value);
|
|
1371
1392
|
}
|
|
1372
1393
|
/**
|
|
1373
1394
|
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
@@ -1403,6 +1424,7 @@ var dataStructureTyped = (() => {
|
|
|
1403
1424
|
this._tail = prev;
|
|
1404
1425
|
}
|
|
1405
1426
|
this._size -= 1;
|
|
1427
|
+
return true;
|
|
1406
1428
|
}
|
|
1407
1429
|
};
|
|
1408
1430
|
|
|
@@ -1428,10 +1450,10 @@ var dataStructureTyped = (() => {
|
|
|
1428
1450
|
super();
|
|
1429
1451
|
__publicField(this, "_head");
|
|
1430
1452
|
__publicField(this, "_tail");
|
|
1431
|
-
__publicField(this, "
|
|
1453
|
+
__publicField(this, "_size");
|
|
1432
1454
|
this._head = void 0;
|
|
1433
1455
|
this._tail = void 0;
|
|
1434
|
-
this.
|
|
1456
|
+
this._size = 0;
|
|
1435
1457
|
if (elements) {
|
|
1436
1458
|
for (const el of elements)
|
|
1437
1459
|
this.push(el);
|
|
@@ -1443,8 +1465,8 @@ var dataStructureTyped = (() => {
|
|
|
1443
1465
|
get tail() {
|
|
1444
1466
|
return this._tail;
|
|
1445
1467
|
}
|
|
1446
|
-
get
|
|
1447
|
-
return this.
|
|
1468
|
+
get size() {
|
|
1469
|
+
return this._size;
|
|
1448
1470
|
}
|
|
1449
1471
|
/**
|
|
1450
1472
|
* Time Complexity: O(n) - Linear time, where n is the length of the input array, as it performs a loop to push each element into the linked list.
|
|
@@ -1487,7 +1509,8 @@ var dataStructureTyped = (() => {
|
|
|
1487
1509
|
this.tail.next = newNode;
|
|
1488
1510
|
this._tail = newNode;
|
|
1489
1511
|
}
|
|
1490
|
-
this.
|
|
1512
|
+
this._size++;
|
|
1513
|
+
return true;
|
|
1491
1514
|
}
|
|
1492
1515
|
/**
|
|
1493
1516
|
* Time Complexity: O(1) - Constant time, as it involves basic pointer adjustments.
|
|
@@ -1502,7 +1525,7 @@ var dataStructureTyped = (() => {
|
|
|
1502
1525
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
1503
1526
|
*/
|
|
1504
1527
|
addLast(value) {
|
|
1505
|
-
this.push(value);
|
|
1528
|
+
return this.push(value);
|
|
1506
1529
|
}
|
|
1507
1530
|
/**
|
|
1508
1531
|
* Time Complexity: O(n) - Linear time in the worst case, as it may need to traverse the list to find the last element.
|
|
@@ -1524,7 +1547,7 @@ var dataStructureTyped = (() => {
|
|
|
1524
1547
|
const value2 = this.head.value;
|
|
1525
1548
|
this._head = void 0;
|
|
1526
1549
|
this._tail = void 0;
|
|
1527
|
-
this.
|
|
1550
|
+
this._size--;
|
|
1528
1551
|
return value2;
|
|
1529
1552
|
}
|
|
1530
1553
|
let current = this.head;
|
|
@@ -1534,7 +1557,7 @@ var dataStructureTyped = (() => {
|
|
|
1534
1557
|
const value = this.tail.value;
|
|
1535
1558
|
current.next = void 0;
|
|
1536
1559
|
this._tail = current;
|
|
1537
|
-
this.
|
|
1560
|
+
this._size--;
|
|
1538
1561
|
return value;
|
|
1539
1562
|
}
|
|
1540
1563
|
/**
|
|
@@ -1569,7 +1592,7 @@ var dataStructureTyped = (() => {
|
|
|
1569
1592
|
return void 0;
|
|
1570
1593
|
const removedNode = this.head;
|
|
1571
1594
|
this._head = this.head.next;
|
|
1572
|
-
this.
|
|
1595
|
+
this._size--;
|
|
1573
1596
|
return removedNode.value;
|
|
1574
1597
|
}
|
|
1575
1598
|
/**
|
|
@@ -1607,7 +1630,8 @@ var dataStructureTyped = (() => {
|
|
|
1607
1630
|
newNode.next = this.head;
|
|
1608
1631
|
this._head = newNode;
|
|
1609
1632
|
}
|
|
1610
|
-
this.
|
|
1633
|
+
this._size++;
|
|
1634
|
+
return true;
|
|
1611
1635
|
}
|
|
1612
1636
|
/**
|
|
1613
1637
|
* Time Complexity: O(1) - Constant time, as it involves adjusting pointers at the head.
|
|
@@ -1622,7 +1646,7 @@ var dataStructureTyped = (() => {
|
|
|
1622
1646
|
* linked list.
|
|
1623
1647
|
*/
|
|
1624
1648
|
addFirst(value) {
|
|
1625
|
-
this.unshift(value);
|
|
1649
|
+
return this.unshift(value);
|
|
1626
1650
|
}
|
|
1627
1651
|
/**
|
|
1628
1652
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
@@ -1639,7 +1663,7 @@ var dataStructureTyped = (() => {
|
|
|
1639
1663
|
* `undefined` if the index is out of bounds.
|
|
1640
1664
|
*/
|
|
1641
1665
|
getAt(index) {
|
|
1642
|
-
if (index < 0 || index >= this.
|
|
1666
|
+
if (index < 0 || index >= this.size)
|
|
1643
1667
|
return void 0;
|
|
1644
1668
|
let current = this.head;
|
|
1645
1669
|
for (let i = 0; i < index; i++) {
|
|
@@ -1683,17 +1707,21 @@ var dataStructureTyped = (() => {
|
|
|
1683
1707
|
* bounds.
|
|
1684
1708
|
*/
|
|
1685
1709
|
deleteAt(index) {
|
|
1686
|
-
if (index < 0 || index >= this.
|
|
1687
|
-
return
|
|
1688
|
-
if (index === 0)
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1710
|
+
if (index < 0 || index >= this.size)
|
|
1711
|
+
return false;
|
|
1712
|
+
if (index === 0) {
|
|
1713
|
+
this.shift();
|
|
1714
|
+
return true;
|
|
1715
|
+
}
|
|
1716
|
+
if (index === this.size - 1) {
|
|
1717
|
+
this.pop();
|
|
1718
|
+
return true;
|
|
1719
|
+
}
|
|
1692
1720
|
const prevNode = this.getNodeAt(index - 1);
|
|
1693
1721
|
const removedNode = prevNode.next;
|
|
1694
1722
|
prevNode.next = removedNode.next;
|
|
1695
|
-
this.
|
|
1696
|
-
return
|
|
1723
|
+
this._size--;
|
|
1724
|
+
return true;
|
|
1697
1725
|
}
|
|
1698
1726
|
/**
|
|
1699
1727
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
@@ -1732,7 +1760,7 @@ var dataStructureTyped = (() => {
|
|
|
1732
1760
|
this._tail = prev;
|
|
1733
1761
|
}
|
|
1734
1762
|
}
|
|
1735
|
-
this.
|
|
1763
|
+
this._size--;
|
|
1736
1764
|
return true;
|
|
1737
1765
|
}
|
|
1738
1766
|
prev = current;
|
|
@@ -1748,7 +1776,7 @@ var dataStructureTyped = (() => {
|
|
|
1748
1776
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
1749
1777
|
* Space Complexity: O(1) - Constant space.
|
|
1750
1778
|
*
|
|
1751
|
-
* The `
|
|
1779
|
+
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
1752
1780
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
1753
1781
|
* linked list. It is of type number.
|
|
1754
1782
|
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
@@ -1756,14 +1784,14 @@ var dataStructureTyped = (() => {
|
|
|
1756
1784
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
1757
1785
|
* if the index is out of bounds.
|
|
1758
1786
|
*/
|
|
1759
|
-
|
|
1760
|
-
if (index < 0 || index > this.
|
|
1787
|
+
addAt(index, value) {
|
|
1788
|
+
if (index < 0 || index > this.size)
|
|
1761
1789
|
return false;
|
|
1762
1790
|
if (index === 0) {
|
|
1763
1791
|
this.unshift(value);
|
|
1764
1792
|
return true;
|
|
1765
1793
|
}
|
|
1766
|
-
if (index === this.
|
|
1794
|
+
if (index === this.size) {
|
|
1767
1795
|
this.push(value);
|
|
1768
1796
|
return true;
|
|
1769
1797
|
}
|
|
@@ -1771,7 +1799,7 @@ var dataStructureTyped = (() => {
|
|
|
1771
1799
|
const prevNode = this.getNodeAt(index - 1);
|
|
1772
1800
|
newNode.next = prevNode.next;
|
|
1773
1801
|
prevNode.next = newNode;
|
|
1774
|
-
this.
|
|
1802
|
+
this._size++;
|
|
1775
1803
|
return true;
|
|
1776
1804
|
}
|
|
1777
1805
|
/**
|
|
@@ -1780,7 +1808,7 @@ var dataStructureTyped = (() => {
|
|
|
1780
1808
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
1781
1809
|
*/
|
|
1782
1810
|
isEmpty() {
|
|
1783
|
-
return this.
|
|
1811
|
+
return this.size === 0;
|
|
1784
1812
|
}
|
|
1785
1813
|
/**
|
|
1786
1814
|
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
@@ -1788,7 +1816,7 @@ var dataStructureTyped = (() => {
|
|
|
1788
1816
|
clear() {
|
|
1789
1817
|
this._head = void 0;
|
|
1790
1818
|
this._tail = void 0;
|
|
1791
|
-
this.
|
|
1819
|
+
this._size = 0;
|
|
1792
1820
|
}
|
|
1793
1821
|
/**
|
|
1794
1822
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to traverse the entire list to convert it to an array.
|
|
@@ -1823,7 +1851,7 @@ var dataStructureTyped = (() => {
|
|
|
1823
1851
|
*/
|
|
1824
1852
|
reverse() {
|
|
1825
1853
|
if (!this.head || this.head === this.tail)
|
|
1826
|
-
return;
|
|
1854
|
+
return this;
|
|
1827
1855
|
let prev = void 0;
|
|
1828
1856
|
let current = this.head;
|
|
1829
1857
|
let next = void 0;
|
|
@@ -1834,6 +1862,7 @@ var dataStructureTyped = (() => {
|
|
|
1834
1862
|
current = next;
|
|
1835
1863
|
}
|
|
1836
1864
|
[this._head, this._tail] = [this.tail, this.head];
|
|
1865
|
+
return this;
|
|
1837
1866
|
}
|
|
1838
1867
|
/**
|
|
1839
1868
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
@@ -1916,14 +1945,14 @@ var dataStructureTyped = (() => {
|
|
|
1916
1945
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1917
1946
|
* Space Complexity: O(1) - Constant space.
|
|
1918
1947
|
*
|
|
1919
|
-
* The `
|
|
1948
|
+
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
1920
1949
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
1921
1950
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
1922
1951
|
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
1923
|
-
* @returns The method `
|
|
1952
|
+
* @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
1924
1953
|
* inserted before the existing value, and `false` otherwise.
|
|
1925
1954
|
*/
|
|
1926
|
-
|
|
1955
|
+
addBefore(existingValueOrNode, newValue) {
|
|
1927
1956
|
if (!this.head)
|
|
1928
1957
|
return false;
|
|
1929
1958
|
let existingValue;
|
|
@@ -1942,7 +1971,7 @@ var dataStructureTyped = (() => {
|
|
|
1942
1971
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
1943
1972
|
newNode.next = current.next;
|
|
1944
1973
|
current.next = newNode;
|
|
1945
|
-
this.
|
|
1974
|
+
this._size++;
|
|
1946
1975
|
return true;
|
|
1947
1976
|
}
|
|
1948
1977
|
current = current.next;
|
|
@@ -1957,14 +1986,14 @@ var dataStructureTyped = (() => {
|
|
|
1957
1986
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
1958
1987
|
* Space Complexity: O(1) - Constant space.
|
|
1959
1988
|
*
|
|
1960
|
-
* The `
|
|
1989
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
1961
1990
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
1962
1991
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
1963
1992
|
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
1964
1993
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
1965
1994
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
1966
1995
|
*/
|
|
1967
|
-
|
|
1996
|
+
addAfter(existingValueOrNode, newValue) {
|
|
1968
1997
|
let existingNode;
|
|
1969
1998
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
1970
1999
|
existingNode = existingValueOrNode;
|
|
@@ -1978,7 +2007,7 @@ var dataStructureTyped = (() => {
|
|
|
1978
2007
|
if (existingNode === this.tail) {
|
|
1979
2008
|
this._tail = newNode;
|
|
1980
2009
|
}
|
|
1981
|
-
this.
|
|
2010
|
+
this._size++;
|
|
1982
2011
|
return true;
|
|
1983
2012
|
}
|
|
1984
2013
|
return false;
|
|
@@ -2065,13 +2094,6 @@ var dataStructureTyped = (() => {
|
|
|
2065
2094
|
}
|
|
2066
2095
|
return mappedList;
|
|
2067
2096
|
}
|
|
2068
|
-
/**
|
|
2069
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2070
|
-
* Space Complexity: O(n)
|
|
2071
|
-
*/
|
|
2072
|
-
print() {
|
|
2073
|
-
console.log([...this]);
|
|
2074
|
-
}
|
|
2075
2097
|
*_getIterator() {
|
|
2076
2098
|
let current = this.head;
|
|
2077
2099
|
while (current) {
|
|
@@ -2099,16 +2121,16 @@ var dataStructureTyped = (() => {
|
|
|
2099
2121
|
};
|
|
2100
2122
|
var DoublyLinkedList = class _DoublyLinkedList extends IterableElementBase {
|
|
2101
2123
|
/**
|
|
2102
|
-
* The constructor initializes the linked list with an empty head, tail, and
|
|
2124
|
+
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
2103
2125
|
*/
|
|
2104
2126
|
constructor(elements) {
|
|
2105
2127
|
super();
|
|
2106
2128
|
__publicField(this, "_head");
|
|
2107
2129
|
__publicField(this, "_tail");
|
|
2108
|
-
__publicField(this, "
|
|
2130
|
+
__publicField(this, "_size");
|
|
2109
2131
|
this._head = void 0;
|
|
2110
2132
|
this._tail = void 0;
|
|
2111
|
-
this.
|
|
2133
|
+
this._size = 0;
|
|
2112
2134
|
if (elements) {
|
|
2113
2135
|
for (const el of elements) {
|
|
2114
2136
|
this.push(el);
|
|
@@ -2121,18 +2143,15 @@ var dataStructureTyped = (() => {
|
|
|
2121
2143
|
get tail() {
|
|
2122
2144
|
return this._tail;
|
|
2123
2145
|
}
|
|
2124
|
-
get length() {
|
|
2125
|
-
return this._length;
|
|
2126
|
-
}
|
|
2127
2146
|
get size() {
|
|
2128
|
-
return this.
|
|
2147
|
+
return this._size;
|
|
2129
2148
|
}
|
|
2130
2149
|
/**
|
|
2131
|
-
* Time Complexity: O(n), where n is the
|
|
2150
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
2132
2151
|
* Space Complexity: O(n)
|
|
2133
2152
|
*/
|
|
2134
2153
|
/**
|
|
2135
|
-
* Time Complexity: O(n), where n is the
|
|
2154
|
+
* Time Complexity: O(n), where n is the size of the input array.
|
|
2136
2155
|
* Space Complexity: O(n)
|
|
2137
2156
|
*
|
|
2138
2157
|
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
@@ -2168,21 +2187,8 @@ var dataStructureTyped = (() => {
|
|
|
2168
2187
|
this.tail.next = newNode;
|
|
2169
2188
|
this._tail = newNode;
|
|
2170
2189
|
}
|
|
2171
|
-
this.
|
|
2172
|
-
|
|
2173
|
-
/**
|
|
2174
|
-
* Time Complexity: O(1)
|
|
2175
|
-
* Space Complexity: O(1)
|
|
2176
|
-
*/
|
|
2177
|
-
/**
|
|
2178
|
-
* Time Complexity: O(1)
|
|
2179
|
-
* Space Complexity: O(1)
|
|
2180
|
-
*
|
|
2181
|
-
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
2182
|
-
* @param {E} value - The value to be added to the linked list.
|
|
2183
|
-
*/
|
|
2184
|
-
addLast(value) {
|
|
2185
|
-
this.push(value);
|
|
2190
|
+
this._size++;
|
|
2191
|
+
return true;
|
|
2186
2192
|
}
|
|
2187
2193
|
/**
|
|
2188
2194
|
* Time Complexity: O(1)
|
|
@@ -2207,24 +2213,9 @@ var dataStructureTyped = (() => {
|
|
|
2207
2213
|
this._tail = removedNode.prev;
|
|
2208
2214
|
this.tail.next = void 0;
|
|
2209
2215
|
}
|
|
2210
|
-
this.
|
|
2216
|
+
this._size--;
|
|
2211
2217
|
return removedNode.value;
|
|
2212
2218
|
}
|
|
2213
|
-
/**
|
|
2214
|
-
* Time Complexity: O(1)
|
|
2215
|
-
* Space Complexity: O(1)
|
|
2216
|
-
*/
|
|
2217
|
-
/**
|
|
2218
|
-
* Time Complexity: O(1)
|
|
2219
|
-
* Space Complexity: O(1)
|
|
2220
|
-
*
|
|
2221
|
-
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
2222
|
-
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
2223
|
-
* list is empty, it returns undefined.
|
|
2224
|
-
*/
|
|
2225
|
-
pollLast() {
|
|
2226
|
-
return this.pop();
|
|
2227
|
-
}
|
|
2228
2219
|
/**
|
|
2229
2220
|
* Time Complexity: O(1)
|
|
2230
2221
|
* Space Complexity: O(1)
|
|
@@ -2248,24 +2239,9 @@ var dataStructureTyped = (() => {
|
|
|
2248
2239
|
this._head = removedNode.next;
|
|
2249
2240
|
this.head.prev = void 0;
|
|
2250
2241
|
}
|
|
2251
|
-
this.
|
|
2242
|
+
this._size--;
|
|
2252
2243
|
return removedNode.value;
|
|
2253
2244
|
}
|
|
2254
|
-
/**
|
|
2255
|
-
* Time Complexity: O(1)
|
|
2256
|
-
* Space Complexity: O(1)
|
|
2257
|
-
*/
|
|
2258
|
-
/**
|
|
2259
|
-
* Time Complexity: O(1)
|
|
2260
|
-
* Space Complexity: O(1)
|
|
2261
|
-
*
|
|
2262
|
-
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
2263
|
-
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
2264
|
-
* list.
|
|
2265
|
-
*/
|
|
2266
|
-
pollFirst() {
|
|
2267
|
-
return this.shift();
|
|
2268
|
-
}
|
|
2269
2245
|
/**
|
|
2270
2246
|
* Time Complexity: O(1)
|
|
2271
2247
|
* Space Complexity: O(1)
|
|
@@ -2288,52 +2264,8 @@ var dataStructureTyped = (() => {
|
|
|
2288
2264
|
this.head.prev = newNode;
|
|
2289
2265
|
this._head = newNode;
|
|
2290
2266
|
}
|
|
2291
|
-
this.
|
|
2292
|
-
|
|
2293
|
-
/**
|
|
2294
|
-
* Time Complexity: O(1)
|
|
2295
|
-
* Space Complexity: O(1)
|
|
2296
|
-
*/
|
|
2297
|
-
/**
|
|
2298
|
-
* Time Complexity: O(1)
|
|
2299
|
-
* Space Complexity: O(1)
|
|
2300
|
-
*
|
|
2301
|
-
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
2302
|
-
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
2303
|
-
* doubly linked list.
|
|
2304
|
-
*/
|
|
2305
|
-
addFirst(value) {
|
|
2306
|
-
this.unshift(value);
|
|
2307
|
-
}
|
|
2308
|
-
/**
|
|
2309
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2310
|
-
* Space Complexity: O(1)
|
|
2311
|
-
*/
|
|
2312
|
-
/**
|
|
2313
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2314
|
-
* Space Complexity: O(1)
|
|
2315
|
-
*
|
|
2316
|
-
* The `getFirst` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
2317
|
-
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
2318
|
-
*/
|
|
2319
|
-
getFirst() {
|
|
2320
|
-
var _a;
|
|
2321
|
-
return (_a = this.head) == null ? void 0 : _a.value;
|
|
2322
|
-
}
|
|
2323
|
-
/**
|
|
2324
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2325
|
-
* Space Complexity: O(1)
|
|
2326
|
-
*/
|
|
2327
|
-
/**
|
|
2328
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2329
|
-
* Space Complexity: O(1)
|
|
2330
|
-
*
|
|
2331
|
-
* The `getLast` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
2332
|
-
* @returns The method `getLast()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
2333
|
-
*/
|
|
2334
|
-
getLast() {
|
|
2335
|
-
var _a;
|
|
2336
|
-
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
2267
|
+
this._size++;
|
|
2268
|
+
return true;
|
|
2337
2269
|
}
|
|
2338
2270
|
/**
|
|
2339
2271
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2350,7 +2282,7 @@ var dataStructureTyped = (() => {
|
|
|
2350
2282
|
* or the linked list is empty, it will return undefined.
|
|
2351
2283
|
*/
|
|
2352
2284
|
getAt(index) {
|
|
2353
|
-
if (index < 0 || index >= this.
|
|
2285
|
+
if (index < 0 || index >= this.size)
|
|
2354
2286
|
return void 0;
|
|
2355
2287
|
let current = this.head;
|
|
2356
2288
|
for (let i = 0; i < index; i++) {
|
|
@@ -2374,7 +2306,7 @@ var dataStructureTyped = (() => {
|
|
|
2374
2306
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
2375
2307
|
*/
|
|
2376
2308
|
getNodeAt(index) {
|
|
2377
|
-
if (index < 0 || index >= this.
|
|
2309
|
+
if (index < 0 || index >= this.size)
|
|
2378
2310
|
return void 0;
|
|
2379
2311
|
let current = this.head;
|
|
2380
2312
|
for (let i = 0; i < index; i++) {
|
|
@@ -2422,14 +2354,14 @@ var dataStructureTyped = (() => {
|
|
|
2422
2354
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
2423
2355
|
* if the index is out of bounds.
|
|
2424
2356
|
*/
|
|
2425
|
-
|
|
2426
|
-
if (index < 0 || index > this.
|
|
2357
|
+
addAt(index, value) {
|
|
2358
|
+
if (index < 0 || index > this.size)
|
|
2427
2359
|
return false;
|
|
2428
2360
|
if (index === 0) {
|
|
2429
2361
|
this.unshift(value);
|
|
2430
2362
|
return true;
|
|
2431
2363
|
}
|
|
2432
|
-
if (index === this.
|
|
2364
|
+
if (index === this.size) {
|
|
2433
2365
|
this.push(value);
|
|
2434
2366
|
return true;
|
|
2435
2367
|
}
|
|
@@ -2440,7 +2372,7 @@ var dataStructureTyped = (() => {
|
|
|
2440
2372
|
newNode.next = nextNode;
|
|
2441
2373
|
prevNode.next = newNode;
|
|
2442
2374
|
nextNode.prev = newNode;
|
|
2443
|
-
this.
|
|
2375
|
+
this._size++;
|
|
2444
2376
|
return true;
|
|
2445
2377
|
}
|
|
2446
2378
|
/**
|
|
@@ -2451,7 +2383,7 @@ var dataStructureTyped = (() => {
|
|
|
2451
2383
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2452
2384
|
* Space Complexity: O(1)
|
|
2453
2385
|
*
|
|
2454
|
-
* The `
|
|
2386
|
+
* The `addBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
2455
2387
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
2456
2388
|
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
2457
2389
|
* itself.
|
|
@@ -2460,7 +2392,7 @@ var dataStructureTyped = (() => {
|
|
|
2460
2392
|
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
2461
2393
|
* insertion fails.
|
|
2462
2394
|
*/
|
|
2463
|
-
|
|
2395
|
+
addBefore(existingValueOrNode, newValue) {
|
|
2464
2396
|
let existingNode;
|
|
2465
2397
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
2466
2398
|
existingNode = existingValueOrNode;
|
|
@@ -2478,7 +2410,7 @@ var dataStructureTyped = (() => {
|
|
|
2478
2410
|
if (existingNode === this.head) {
|
|
2479
2411
|
this._head = newNode;
|
|
2480
2412
|
}
|
|
2481
|
-
this.
|
|
2413
|
+
this._size++;
|
|
2482
2414
|
return true;
|
|
2483
2415
|
}
|
|
2484
2416
|
return false;
|
|
@@ -2491,7 +2423,7 @@ var dataStructureTyped = (() => {
|
|
|
2491
2423
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2492
2424
|
* Space Complexity: O(1)
|
|
2493
2425
|
*
|
|
2494
|
-
* The `
|
|
2426
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
2495
2427
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
2496
2428
|
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
2497
2429
|
* itself.
|
|
@@ -2499,7 +2431,7 @@ var dataStructureTyped = (() => {
|
|
|
2499
2431
|
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
|
|
2500
2432
|
* existing value or node is not found in the doubly linked list.
|
|
2501
2433
|
*/
|
|
2502
|
-
|
|
2434
|
+
addAfter(existingValueOrNode, newValue) {
|
|
2503
2435
|
let existingNode;
|
|
2504
2436
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
2505
2437
|
existingNode = existingValueOrNode;
|
|
@@ -2517,7 +2449,7 @@ var dataStructureTyped = (() => {
|
|
|
2517
2449
|
if (existingNode === this.tail) {
|
|
2518
2450
|
this._tail = newNode;
|
|
2519
2451
|
}
|
|
2520
|
-
this.
|
|
2452
|
+
this._size++;
|
|
2521
2453
|
return true;
|
|
2522
2454
|
}
|
|
2523
2455
|
return false;
|
|
@@ -2537,19 +2469,23 @@ var dataStructureTyped = (() => {
|
|
|
2537
2469
|
* bounds.
|
|
2538
2470
|
*/
|
|
2539
2471
|
deleteAt(index) {
|
|
2540
|
-
if (index < 0 || index >= this.
|
|
2541
|
-
return
|
|
2542
|
-
if (index === 0)
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2472
|
+
if (index < 0 || index >= this.size)
|
|
2473
|
+
return false;
|
|
2474
|
+
if (index === 0) {
|
|
2475
|
+
this.shift();
|
|
2476
|
+
return true;
|
|
2477
|
+
}
|
|
2478
|
+
if (index === this.size - 1) {
|
|
2479
|
+
this.pop();
|
|
2480
|
+
return true;
|
|
2481
|
+
}
|
|
2546
2482
|
const removedNode = this.getNodeAt(index);
|
|
2547
2483
|
const prevNode = removedNode.prev;
|
|
2548
2484
|
const nextNode = removedNode.next;
|
|
2549
2485
|
prevNode.next = nextNode;
|
|
2550
2486
|
nextNode.prev = prevNode;
|
|
2551
|
-
this.
|
|
2552
|
-
return
|
|
2487
|
+
this._size--;
|
|
2488
|
+
return true;
|
|
2553
2489
|
}
|
|
2554
2490
|
/**
|
|
2555
2491
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2582,26 +2518,26 @@ var dataStructureTyped = (() => {
|
|
|
2582
2518
|
const nextNode = node.next;
|
|
2583
2519
|
prevNode.next = nextNode;
|
|
2584
2520
|
nextNode.prev = prevNode;
|
|
2585
|
-
this.
|
|
2521
|
+
this._size--;
|
|
2586
2522
|
}
|
|
2587
2523
|
return true;
|
|
2588
2524
|
}
|
|
2589
2525
|
return false;
|
|
2590
2526
|
}
|
|
2591
2527
|
/**
|
|
2592
|
-
* The function checks if a variable has a
|
|
2528
|
+
* The function checks if a variable has a size greater than zero and returns a boolean value.
|
|
2593
2529
|
* @returns A boolean value is being returned.
|
|
2594
2530
|
*/
|
|
2595
2531
|
isEmpty() {
|
|
2596
|
-
return this.
|
|
2532
|
+
return this.size === 0;
|
|
2597
2533
|
}
|
|
2598
2534
|
/**
|
|
2599
|
-
* The `clear` function resets the linked list by setting the head, tail, and
|
|
2535
|
+
* The `clear` function resets the linked list by setting the head, tail, and size to undefined and 0 respectively.
|
|
2600
2536
|
*/
|
|
2601
2537
|
clear() {
|
|
2602
2538
|
this._head = void 0;
|
|
2603
2539
|
this._tail = void 0;
|
|
2604
|
-
this.
|
|
2540
|
+
this._size = 0;
|
|
2605
2541
|
}
|
|
2606
2542
|
/**
|
|
2607
2543
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2696,6 +2632,7 @@ var dataStructureTyped = (() => {
|
|
|
2696
2632
|
[current.prev, current.next] = [current.next, current.prev];
|
|
2697
2633
|
current = next;
|
|
2698
2634
|
}
|
|
2635
|
+
return this;
|
|
2699
2636
|
}
|
|
2700
2637
|
/**
|
|
2701
2638
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
@@ -2799,12 +2736,94 @@ var dataStructureTyped = (() => {
|
|
|
2799
2736
|
}
|
|
2800
2737
|
return mappedList;
|
|
2801
2738
|
}
|
|
2739
|
+
/**
|
|
2740
|
+
* Time Complexity: O(1)
|
|
2741
|
+
* Space Complexity: O(1)
|
|
2742
|
+
*/
|
|
2743
|
+
/**
|
|
2744
|
+
* Time Complexity: O(1)
|
|
2745
|
+
* Space Complexity: O(1)
|
|
2746
|
+
*
|
|
2747
|
+
* The addLast function adds a new node with the given value to the end of the doubly linked list.
|
|
2748
|
+
* @param {E} value - The value to be added to the linked list.
|
|
2749
|
+
*/
|
|
2750
|
+
addLast(value) {
|
|
2751
|
+
return this.push(value);
|
|
2752
|
+
}
|
|
2753
|
+
/**
|
|
2754
|
+
* Time Complexity: O(1)
|
|
2755
|
+
* Space Complexity: O(1)
|
|
2756
|
+
*/
|
|
2757
|
+
/**
|
|
2758
|
+
* Time Complexity: O(1)
|
|
2759
|
+
* Space Complexity: O(1)
|
|
2760
|
+
*
|
|
2761
|
+
* The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
2762
|
+
* @returns The method is returning the value of the removed node (removedNode.value) if the list is not empty. If the
|
|
2763
|
+
* list is empty, it returns undefined.
|
|
2764
|
+
*/
|
|
2765
|
+
pollLast() {
|
|
2766
|
+
return this.pop();
|
|
2767
|
+
}
|
|
2768
|
+
/**
|
|
2769
|
+
* Time Complexity: O(1)
|
|
2770
|
+
* Space Complexity: O(1)
|
|
2771
|
+
*/
|
|
2772
|
+
/**
|
|
2773
|
+
* Time Complexity: O(1)
|
|
2774
|
+
* Space Complexity: O(1)
|
|
2775
|
+
*
|
|
2776
|
+
* The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
2777
|
+
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
2778
|
+
* list.
|
|
2779
|
+
*/
|
|
2780
|
+
pollFirst() {
|
|
2781
|
+
return this.shift();
|
|
2782
|
+
}
|
|
2783
|
+
/**
|
|
2784
|
+
* Time Complexity: O(1)
|
|
2785
|
+
* Space Complexity: O(1)
|
|
2786
|
+
*/
|
|
2787
|
+
/**
|
|
2788
|
+
* Time Complexity: O(1)
|
|
2789
|
+
* Space Complexity: O(1)
|
|
2790
|
+
*
|
|
2791
|
+
* The addFirst function adds a new node with the given value to the beginning of a doubly linked list.
|
|
2792
|
+
* @param {E} value - The `value` parameter represents the value of the new node that will be added to the beginning of the
|
|
2793
|
+
* doubly linked list.
|
|
2794
|
+
*/
|
|
2795
|
+
addFirst(value) {
|
|
2796
|
+
this.unshift(value);
|
|
2797
|
+
}
|
|
2802
2798
|
/**
|
|
2803
2799
|
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2804
|
-
* Space Complexity: O(
|
|
2800
|
+
* Space Complexity: O(1)
|
|
2805
2801
|
*/
|
|
2806
|
-
|
|
2807
|
-
|
|
2802
|
+
/**
|
|
2803
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2804
|
+
* Space Complexity: O(1)
|
|
2805
|
+
*
|
|
2806
|
+
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
2807
|
+
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
2808
|
+
*/
|
|
2809
|
+
get first() {
|
|
2810
|
+
var _a;
|
|
2811
|
+
return (_a = this.head) == null ? void 0 : _a.value;
|
|
2812
|
+
}
|
|
2813
|
+
/**
|
|
2814
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2815
|
+
* Space Complexity: O(1)
|
|
2816
|
+
*/
|
|
2817
|
+
/**
|
|
2818
|
+
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
2819
|
+
* Space Complexity: O(1)
|
|
2820
|
+
*
|
|
2821
|
+
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
2822
|
+
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
2823
|
+
*/
|
|
2824
|
+
get last() {
|
|
2825
|
+
var _a;
|
|
2826
|
+
return (_a = this.tail) == null ? void 0 : _a.value;
|
|
2808
2827
|
}
|
|
2809
2828
|
/**
|
|
2810
2829
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
@@ -2975,7 +2994,7 @@ var dataStructureTyped = (() => {
|
|
|
2975
2994
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
2976
2995
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
2977
2996
|
*/
|
|
2978
|
-
|
|
2997
|
+
get first() {
|
|
2979
2998
|
const firstNode = this.head.forward[0];
|
|
2980
2999
|
return firstNode ? firstNode.value : void 0;
|
|
2981
3000
|
}
|
|
@@ -2990,7 +3009,7 @@ var dataStructureTyped = (() => {
|
|
|
2990
3009
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
2991
3010
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
2992
3011
|
*/
|
|
2993
|
-
|
|
3012
|
+
get last() {
|
|
2994
3013
|
let current = this.head;
|
|
2995
3014
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
2996
3015
|
while (current.forward[i]) {
|
|
@@ -3147,7 +3166,7 @@ var dataStructureTyped = (() => {
|
|
|
3147
3166
|
*/
|
|
3148
3167
|
push(element) {
|
|
3149
3168
|
this.elements.push(element);
|
|
3150
|
-
return
|
|
3169
|
+
return true;
|
|
3151
3170
|
}
|
|
3152
3171
|
/**
|
|
3153
3172
|
* Time Complexity: O(1), as it only involves accessing the last element of the array.
|
|
@@ -3163,7 +3182,7 @@ var dataStructureTyped = (() => {
|
|
|
3163
3182
|
*/
|
|
3164
3183
|
pop() {
|
|
3165
3184
|
if (this.isEmpty())
|
|
3166
|
-
return
|
|
3185
|
+
return;
|
|
3167
3186
|
return this.elements.pop();
|
|
3168
3187
|
}
|
|
3169
3188
|
/**
|
|
@@ -3257,9 +3276,6 @@ var dataStructureTyped = (() => {
|
|
|
3257
3276
|
}
|
|
3258
3277
|
return newStack;
|
|
3259
3278
|
}
|
|
3260
|
-
print() {
|
|
3261
|
-
console.log([...this]);
|
|
3262
|
-
}
|
|
3263
3279
|
/**
|
|
3264
3280
|
* Custom iterator for the Stack class.
|
|
3265
3281
|
* @returns An iterator object.
|
|
@@ -3324,7 +3340,7 @@ var dataStructureTyped = (() => {
|
|
|
3324
3340
|
*/
|
|
3325
3341
|
push(element) {
|
|
3326
3342
|
this.nodes.push(element);
|
|
3327
|
-
return
|
|
3343
|
+
return true;
|
|
3328
3344
|
}
|
|
3329
3345
|
/**
|
|
3330
3346
|
* Time Complexity: O(n) - where n is the number of elements in the queue. In the worst case, it may need to shift all elements to update the offset.
|
|
@@ -3341,7 +3357,7 @@ var dataStructureTyped = (() => {
|
|
|
3341
3357
|
shift() {
|
|
3342
3358
|
if (this.size === 0)
|
|
3343
3359
|
return void 0;
|
|
3344
|
-
const first = this.
|
|
3360
|
+
const first = this.first;
|
|
3345
3361
|
this._offset += 1;
|
|
3346
3362
|
if (this.offset * 2 < this.nodes.length)
|
|
3347
3363
|
return first;
|
|
@@ -3357,11 +3373,11 @@ var dataStructureTyped = (() => {
|
|
|
3357
3373
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
3358
3374
|
* Space Complexity: O(1) - no additional space is used.
|
|
3359
3375
|
*
|
|
3360
|
-
* The `
|
|
3361
|
-
* @returns The `
|
|
3376
|
+
* The `first` function returns the first element of the array `_nodes` if it exists, otherwise it returns `undefined`.
|
|
3377
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
3362
3378
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
3363
3379
|
*/
|
|
3364
|
-
|
|
3380
|
+
get first() {
|
|
3365
3381
|
return this.size > 0 ? this.nodes[this.offset] : void 0;
|
|
3366
3382
|
}
|
|
3367
3383
|
/**
|
|
@@ -3377,7 +3393,7 @@ var dataStructureTyped = (() => {
|
|
|
3377
3393
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
3378
3394
|
*/
|
|
3379
3395
|
peek() {
|
|
3380
|
-
return this.
|
|
3396
|
+
return this.first;
|
|
3381
3397
|
}
|
|
3382
3398
|
/**
|
|
3383
3399
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -3387,11 +3403,11 @@ var dataStructureTyped = (() => {
|
|
|
3387
3403
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
3388
3404
|
* Space Complexity: O(1) - no additional space is used.
|
|
3389
3405
|
*
|
|
3390
|
-
* The `
|
|
3391
|
-
* @returns The method `
|
|
3406
|
+
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
3407
|
+
* @returns The method `get last()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
3392
3408
|
* array is empty, it returns `undefined`.
|
|
3393
3409
|
*/
|
|
3394
|
-
|
|
3410
|
+
get last() {
|
|
3395
3411
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : void 0;
|
|
3396
3412
|
}
|
|
3397
3413
|
/**
|
|
@@ -3407,7 +3423,7 @@ var dataStructureTyped = (() => {
|
|
|
3407
3423
|
* array is empty, it returns `undefined`.
|
|
3408
3424
|
*/
|
|
3409
3425
|
peekLast() {
|
|
3410
|
-
return this.
|
|
3426
|
+
return this.last;
|
|
3411
3427
|
}
|
|
3412
3428
|
/**
|
|
3413
3429
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
@@ -3499,9 +3515,6 @@ var dataStructureTyped = (() => {
|
|
|
3499
3515
|
clone() {
|
|
3500
3516
|
return new _Queue(this.nodes.slice(this.offset));
|
|
3501
3517
|
}
|
|
3502
|
-
print() {
|
|
3503
|
-
console.log([...this]);
|
|
3504
|
-
}
|
|
3505
3518
|
/**
|
|
3506
3519
|
* Time Complexity: O(n)
|
|
3507
3520
|
* Space Complexity: O(n)
|
|
@@ -3576,7 +3589,7 @@ var dataStructureTyped = (() => {
|
|
|
3576
3589
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
3577
3590
|
*/
|
|
3578
3591
|
enqueue(value) {
|
|
3579
|
-
this.push(value);
|
|
3592
|
+
return this.push(value);
|
|
3580
3593
|
}
|
|
3581
3594
|
/**
|
|
3582
3595
|
* The `dequeue` function removes and returns the first element from a queue, or returns undefined if the queue is empty.
|
|
@@ -3586,10 +3599,10 @@ var dataStructureTyped = (() => {
|
|
|
3586
3599
|
return this.shift();
|
|
3587
3600
|
}
|
|
3588
3601
|
/**
|
|
3589
|
-
* The `
|
|
3590
|
-
* @returns The `
|
|
3602
|
+
* The `get first` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
3603
|
+
* @returns The `get first()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
3591
3604
|
*/
|
|
3592
|
-
|
|
3605
|
+
get first() {
|
|
3593
3606
|
var _a;
|
|
3594
3607
|
return (_a = this.head) == null ? void 0 : _a.value;
|
|
3595
3608
|
}
|
|
@@ -3598,7 +3611,7 @@ var dataStructureTyped = (() => {
|
|
|
3598
3611
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
3599
3612
|
*/
|
|
3600
3613
|
peek() {
|
|
3601
|
-
return this.
|
|
3614
|
+
return this.first;
|
|
3602
3615
|
}
|
|
3603
3616
|
};
|
|
3604
3617
|
|
|
@@ -3668,95 +3681,6 @@ var dataStructureTyped = (() => {
|
|
|
3668
3681
|
return;
|
|
3669
3682
|
return this._buckets[this._bucketLast][this._lastInBucket];
|
|
3670
3683
|
}
|
|
3671
|
-
/**
|
|
3672
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
3673
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
3674
|
-
*/
|
|
3675
|
-
isEmpty() {
|
|
3676
|
-
return this.size === 0;
|
|
3677
|
-
}
|
|
3678
|
-
/**
|
|
3679
|
-
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
3680
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
3681
|
-
*/
|
|
3682
|
-
/**
|
|
3683
|
-
* Time Complexity: O(1)
|
|
3684
|
-
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
3685
|
-
*
|
|
3686
|
-
* The addLast function adds an element to the end of an array.
|
|
3687
|
-
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
3688
|
-
* data structure.
|
|
3689
|
-
*/
|
|
3690
|
-
addLast(element) {
|
|
3691
|
-
this.push(element);
|
|
3692
|
-
}
|
|
3693
|
-
/**
|
|
3694
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
3695
|
-
* Space Complexity: O(1) - In-place operation.
|
|
3696
|
-
*/
|
|
3697
|
-
/**
|
|
3698
|
-
* Time Complexity: O(1) - Removes the last element.
|
|
3699
|
-
* Space Complexity: O(1) - Operates in-place.
|
|
3700
|
-
*
|
|
3701
|
-
* The function "pollLast" removes and returns the last element of an array.
|
|
3702
|
-
* @returns The last element of the array is being returned.
|
|
3703
|
-
*/
|
|
3704
|
-
pollLast() {
|
|
3705
|
-
return this.pop();
|
|
3706
|
-
}
|
|
3707
|
-
/**
|
|
3708
|
-
* Time Complexity: O(1).
|
|
3709
|
-
* Space Complexity: O(n) - Due to potential resizing.
|
|
3710
|
-
*
|
|
3711
|
-
* The "addFirst" function adds an element to the beginning of an array.
|
|
3712
|
-
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
3713
|
-
* beginning of the data structure.
|
|
3714
|
-
*/
|
|
3715
|
-
addFirst(element) {
|
|
3716
|
-
this.unshift(element);
|
|
3717
|
-
}
|
|
3718
|
-
/**
|
|
3719
|
-
* Time Complexity: O(1) - Removes the first element.
|
|
3720
|
-
* Space Complexity: O(1) - In-place operation.
|
|
3721
|
-
*
|
|
3722
|
-
* The function "pollFirst" removes and returns the first element of an array.
|
|
3723
|
-
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
3724
|
-
* from the beginning. If the array is empty, it will return `undefined`.
|
|
3725
|
-
*/
|
|
3726
|
-
pollFirst() {
|
|
3727
|
-
return this.shift();
|
|
3728
|
-
}
|
|
3729
|
-
/**
|
|
3730
|
-
* The clear() function resets the state of the object by initializing all variables to their default
|
|
3731
|
-
* values.
|
|
3732
|
-
*/
|
|
3733
|
-
clear() {
|
|
3734
|
-
this._buckets = [new Array(this._bucketSize)];
|
|
3735
|
-
this._bucketCount = 1;
|
|
3736
|
-
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
3737
|
-
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3738
|
-
}
|
|
3739
|
-
/**
|
|
3740
|
-
* The below function is a generator that yields elements from a collection one by one.
|
|
3741
|
-
*/
|
|
3742
|
-
*begin() {
|
|
3743
|
-
let index = 0;
|
|
3744
|
-
while (index < this.size) {
|
|
3745
|
-
yield this.getAt(index);
|
|
3746
|
-
index++;
|
|
3747
|
-
}
|
|
3748
|
-
}
|
|
3749
|
-
/**
|
|
3750
|
-
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
3751
|
-
* the last element.
|
|
3752
|
-
*/
|
|
3753
|
-
*reverseBegin() {
|
|
3754
|
-
let index = this.size - 1;
|
|
3755
|
-
while (index >= 0) {
|
|
3756
|
-
yield this.getAt(index);
|
|
3757
|
-
index--;
|
|
3758
|
-
}
|
|
3759
|
-
}
|
|
3760
3684
|
/**
|
|
3761
3685
|
* Time Complexity - Amortized O(1) (possible reallocation)
|
|
3762
3686
|
* Space Complexity - O(n) (due to potential resizing).
|
|
@@ -3786,7 +3710,7 @@ var dataStructureTyped = (() => {
|
|
|
3786
3710
|
}
|
|
3787
3711
|
this._size += 1;
|
|
3788
3712
|
this._buckets[this._bucketLast][this._lastInBucket] = element;
|
|
3789
|
-
return
|
|
3713
|
+
return true;
|
|
3790
3714
|
}
|
|
3791
3715
|
/**
|
|
3792
3716
|
* Time Complexity: O(1)
|
|
@@ -3848,7 +3772,7 @@ var dataStructureTyped = (() => {
|
|
|
3848
3772
|
}
|
|
3849
3773
|
this._size += 1;
|
|
3850
3774
|
this._buckets[this._bucketFirst][this._firstInBucket] = element;
|
|
3851
|
-
return
|
|
3775
|
+
return true;
|
|
3852
3776
|
}
|
|
3853
3777
|
/**
|
|
3854
3778
|
* Time Complexity: O(1)
|
|
@@ -3881,6 +3805,44 @@ var dataStructureTyped = (() => {
|
|
|
3881
3805
|
this._size -= 1;
|
|
3882
3806
|
return element;
|
|
3883
3807
|
}
|
|
3808
|
+
/**
|
|
3809
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
3810
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
3811
|
+
*/
|
|
3812
|
+
isEmpty() {
|
|
3813
|
+
return this.size === 0;
|
|
3814
|
+
}
|
|
3815
|
+
/**
|
|
3816
|
+
* The clear() function resets the state of the object by initializing all variables to their default
|
|
3817
|
+
* values.
|
|
3818
|
+
*/
|
|
3819
|
+
clear() {
|
|
3820
|
+
this._buckets = [new Array(this._bucketSize)];
|
|
3821
|
+
this._bucketCount = 1;
|
|
3822
|
+
this._bucketFirst = this._bucketLast = this._size = 0;
|
|
3823
|
+
this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
|
|
3824
|
+
}
|
|
3825
|
+
/**
|
|
3826
|
+
* The below function is a generator that yields elements from a collection one by one.
|
|
3827
|
+
*/
|
|
3828
|
+
*begin() {
|
|
3829
|
+
let index = 0;
|
|
3830
|
+
while (index < this.size) {
|
|
3831
|
+
yield this.getAt(index);
|
|
3832
|
+
index++;
|
|
3833
|
+
}
|
|
3834
|
+
}
|
|
3835
|
+
/**
|
|
3836
|
+
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
3837
|
+
* the last element.
|
|
3838
|
+
*/
|
|
3839
|
+
*reverseBegin() {
|
|
3840
|
+
let index = this.size - 1;
|
|
3841
|
+
while (index >= 0) {
|
|
3842
|
+
yield this.getAt(index);
|
|
3843
|
+
index--;
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3884
3846
|
/**
|
|
3885
3847
|
* Time Complexity: O(1)
|
|
3886
3848
|
* Space Complexity: O(1)
|
|
@@ -3924,6 +3886,7 @@ var dataStructureTyped = (() => {
|
|
|
3924
3886
|
indexInBucket
|
|
3925
3887
|
} = this._getBucketAndPosition(pos);
|
|
3926
3888
|
this._buckets[bucketIndex][indexInBucket] = element;
|
|
3889
|
+
return true;
|
|
3927
3890
|
}
|
|
3928
3891
|
/**
|
|
3929
3892
|
* Time Complexity: O(n)
|
|
@@ -3933,7 +3896,7 @@ var dataStructureTyped = (() => {
|
|
|
3933
3896
|
* Time Complexity: O(n)
|
|
3934
3897
|
* Space Complexity: O(n)
|
|
3935
3898
|
*
|
|
3936
|
-
* The `
|
|
3899
|
+
* The `addAt` function inserts one or more elements at a specified position in an array-like data
|
|
3937
3900
|
* structure.
|
|
3938
3901
|
* @param {number} pos - The `pos` parameter represents the position at which the element(s) should
|
|
3939
3902
|
* be inserted. It is of type `number`.
|
|
@@ -3944,7 +3907,7 @@ var dataStructureTyped = (() => {
|
|
|
3944
3907
|
* will be inserted once. However, you can provide a different value for `num` if you want
|
|
3945
3908
|
* @returns The size of the array after the insertion is being returned.
|
|
3946
3909
|
*/
|
|
3947
|
-
|
|
3910
|
+
addAt(pos, element, num = 1) {
|
|
3948
3911
|
const length = this.size;
|
|
3949
3912
|
rangeCheck(pos, 0, length);
|
|
3950
3913
|
if (pos === 0) {
|
|
@@ -3964,7 +3927,7 @@ var dataStructureTyped = (() => {
|
|
|
3964
3927
|
for (let i = 0; i < arr.length; ++i)
|
|
3965
3928
|
this.push(arr[i]);
|
|
3966
3929
|
}
|
|
3967
|
-
return
|
|
3930
|
+
return true;
|
|
3968
3931
|
}
|
|
3969
3932
|
/**
|
|
3970
3933
|
* Time Complexity: O(1)
|
|
@@ -4032,7 +3995,7 @@ var dataStructureTyped = (() => {
|
|
|
4032
3995
|
}
|
|
4033
3996
|
this.pop();
|
|
4034
3997
|
}
|
|
4035
|
-
return
|
|
3998
|
+
return true;
|
|
4036
3999
|
}
|
|
4037
4000
|
/**
|
|
4038
4001
|
* Time Complexity: O(n)
|
|
@@ -4051,7 +4014,7 @@ var dataStructureTyped = (() => {
|
|
|
4051
4014
|
delete(element) {
|
|
4052
4015
|
const size = this.size;
|
|
4053
4016
|
if (size === 0)
|
|
4054
|
-
return
|
|
4017
|
+
return false;
|
|
4055
4018
|
let i = 0;
|
|
4056
4019
|
let index = 0;
|
|
4057
4020
|
while (i < size) {
|
|
@@ -4063,7 +4026,7 @@ var dataStructureTyped = (() => {
|
|
|
4063
4026
|
i += 1;
|
|
4064
4027
|
}
|
|
4065
4028
|
this.cut(index - 1);
|
|
4066
|
-
return
|
|
4029
|
+
return true;
|
|
4067
4030
|
}
|
|
4068
4031
|
/**
|
|
4069
4032
|
* Time Complexity: O(n)
|
|
@@ -4103,7 +4066,7 @@ var dataStructureTyped = (() => {
|
|
|
4103
4066
|
*/
|
|
4104
4067
|
unique() {
|
|
4105
4068
|
if (this.size <= 1) {
|
|
4106
|
-
return this
|
|
4069
|
+
return this;
|
|
4107
4070
|
}
|
|
4108
4071
|
let index = 1;
|
|
4109
4072
|
let prev = this.getAt(0);
|
|
@@ -4115,7 +4078,7 @@ var dataStructureTyped = (() => {
|
|
|
4115
4078
|
}
|
|
4116
4079
|
}
|
|
4117
4080
|
this.cut(index - 1);
|
|
4118
|
-
return this
|
|
4081
|
+
return this;
|
|
4119
4082
|
}
|
|
4120
4083
|
/**
|
|
4121
4084
|
* Time Complexity: O(n log n)
|
|
@@ -4129,7 +4092,7 @@ var dataStructureTyped = (() => {
|
|
|
4129
4092
|
* @param [comparator] - The `comparator` parameter is a function that takes in two elements `x` and
|
|
4130
4093
|
* `y` of type `E` and returns a number. The comparator function is used to determine the order of
|
|
4131
4094
|
* the elements in the sorted array.
|
|
4132
|
-
* @returns
|
|
4095
|
+
* @returns Deque<E>
|
|
4133
4096
|
*/
|
|
4134
4097
|
sort(comparator) {
|
|
4135
4098
|
const arr = [];
|
|
@@ -4199,7 +4162,7 @@ var dataStructureTyped = (() => {
|
|
|
4199
4162
|
return element;
|
|
4200
4163
|
}
|
|
4201
4164
|
}
|
|
4202
|
-
return
|
|
4165
|
+
return;
|
|
4203
4166
|
}
|
|
4204
4167
|
/**
|
|
4205
4168
|
* Time Complexity: O(n)
|
|
@@ -4236,11 +4199,7 @@ var dataStructureTyped = (() => {
|
|
|
4236
4199
|
* @returns The `toArray()` method is returning an array of elements of type `E`.
|
|
4237
4200
|
*/
|
|
4238
4201
|
toArray() {
|
|
4239
|
-
|
|
4240
|
-
for (let i = 0; i < this.size; ++i) {
|
|
4241
|
-
arr.push(this.getAt(i));
|
|
4242
|
-
}
|
|
4243
|
-
return arr;
|
|
4202
|
+
return [...this];
|
|
4244
4203
|
}
|
|
4245
4204
|
/**
|
|
4246
4205
|
* Time Complexity: O(n)
|
|
@@ -4300,11 +4259,55 @@ var dataStructureTyped = (() => {
|
|
|
4300
4259
|
return newDeque;
|
|
4301
4260
|
}
|
|
4302
4261
|
/**
|
|
4303
|
-
* Time Complexity: O(n)
|
|
4304
|
-
* Space Complexity: O(n)
|
|
4262
|
+
* Time Complexity: Amortized O(1) - Similar to push, resizing leads to O(n).
|
|
4263
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
4305
4264
|
*/
|
|
4306
|
-
|
|
4307
|
-
|
|
4265
|
+
/**
|
|
4266
|
+
* Time Complexity: O(1)
|
|
4267
|
+
* Space Complexity: O(n) - In worst case, resizing doubles the array size.
|
|
4268
|
+
*
|
|
4269
|
+
* The addLast function adds an element to the end of an array.
|
|
4270
|
+
* @param {E} element - The element parameter represents the element that you want to add to the end of the
|
|
4271
|
+
* data structure.
|
|
4272
|
+
*/
|
|
4273
|
+
addLast(element) {
|
|
4274
|
+
return this.push(element);
|
|
4275
|
+
}
|
|
4276
|
+
/**
|
|
4277
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
4278
|
+
* Space Complexity: O(1) - In-place operation.
|
|
4279
|
+
*/
|
|
4280
|
+
/**
|
|
4281
|
+
* Time Complexity: O(1) - Removes the last element.
|
|
4282
|
+
* Space Complexity: O(1) - Operates in-place.
|
|
4283
|
+
*
|
|
4284
|
+
* The function "pollLast" removes and returns the last element of an array.
|
|
4285
|
+
* @returns The last element of the array is being returned.
|
|
4286
|
+
*/
|
|
4287
|
+
pollLast() {
|
|
4288
|
+
return this.pop();
|
|
4289
|
+
}
|
|
4290
|
+
/**
|
|
4291
|
+
* Time Complexity: O(1).
|
|
4292
|
+
* Space Complexity: O(n) - Due to potential resizing.
|
|
4293
|
+
*
|
|
4294
|
+
* The "addFirst" function adds an element to the beginning of an array.
|
|
4295
|
+
* @param {E} element - The parameter "element" represents the element that you want to add to the
|
|
4296
|
+
* beginning of the data structure.
|
|
4297
|
+
*/
|
|
4298
|
+
addFirst(element) {
|
|
4299
|
+
return this.unshift(element);
|
|
4300
|
+
}
|
|
4301
|
+
/**
|
|
4302
|
+
* Time Complexity: O(1) - Removes the first element.
|
|
4303
|
+
* Space Complexity: O(1) - In-place operation.
|
|
4304
|
+
*
|
|
4305
|
+
* The function "pollFirst" removes and returns the first element of an array.
|
|
4306
|
+
* @returns The method `pollFirst()` is returning the first element of the array after removing it
|
|
4307
|
+
* from the beginning. If the array is empty, it will return `undefined`.
|
|
4308
|
+
*/
|
|
4309
|
+
pollFirst() {
|
|
4310
|
+
return this.shift();
|
|
4308
4311
|
}
|
|
4309
4312
|
/**
|
|
4310
4313
|
* Time Complexity: O(n)
|
|
@@ -4403,7 +4406,7 @@ var dataStructureTyped = (() => {
|
|
|
4403
4406
|
}
|
|
4404
4407
|
if (elements) {
|
|
4405
4408
|
for (const el of elements) {
|
|
4406
|
-
this.
|
|
4409
|
+
this.add(el);
|
|
4407
4410
|
}
|
|
4408
4411
|
}
|
|
4409
4412
|
}
|
|
@@ -4445,23 +4448,8 @@ var dataStructureTyped = (() => {
|
|
|
4445
4448
|
* @param element - The element to be inserted.
|
|
4446
4449
|
*/
|
|
4447
4450
|
add(element) {
|
|
4448
|
-
return this.push(element);
|
|
4449
|
-
}
|
|
4450
|
-
/**
|
|
4451
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
4452
|
-
* Space Complexity: O(1)
|
|
4453
|
-
*/
|
|
4454
|
-
/**
|
|
4455
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
4456
|
-
* Space Complexity: O(1)
|
|
4457
|
-
*
|
|
4458
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
4459
|
-
* @param element - The element to be inserted.
|
|
4460
|
-
*/
|
|
4461
|
-
push(element) {
|
|
4462
4451
|
this._elements.push(element);
|
|
4463
|
-
this._bubbleUp(this.elements.length - 1);
|
|
4464
|
-
return this;
|
|
4452
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
4465
4453
|
}
|
|
4466
4454
|
/**
|
|
4467
4455
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -4485,20 +4473,6 @@ var dataStructureTyped = (() => {
|
|
|
4485
4473
|
}
|
|
4486
4474
|
return value;
|
|
4487
4475
|
}
|
|
4488
|
-
/**
|
|
4489
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
4490
|
-
* Space Complexity: O(1)
|
|
4491
|
-
*/
|
|
4492
|
-
/**
|
|
4493
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
4494
|
-
* Space Complexity: O(1)
|
|
4495
|
-
*
|
|
4496
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
4497
|
-
* @returns The top element or undefined if the heap is empty.
|
|
4498
|
-
*/
|
|
4499
|
-
pop() {
|
|
4500
|
-
return this.poll();
|
|
4501
|
-
}
|
|
4502
4476
|
/**
|
|
4503
4477
|
* Peek at the top element of the heap without removing it.
|
|
4504
4478
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -4532,7 +4506,7 @@ var dataStructureTyped = (() => {
|
|
|
4532
4506
|
*/
|
|
4533
4507
|
refill(elements) {
|
|
4534
4508
|
this._elements = elements;
|
|
4535
|
-
this.fix();
|
|
4509
|
+
return this.fix();
|
|
4536
4510
|
}
|
|
4537
4511
|
/**
|
|
4538
4512
|
* Time Complexity: O(n), where n is the number of elements in the heap.
|
|
@@ -4569,7 +4543,7 @@ var dataStructureTyped = (() => {
|
|
|
4569
4543
|
if (index < 0)
|
|
4570
4544
|
return false;
|
|
4571
4545
|
if (index === 0) {
|
|
4572
|
-
this.
|
|
4546
|
+
this.poll();
|
|
4573
4547
|
} else if (index === this.elements.length - 1) {
|
|
4574
4548
|
this.elements.pop();
|
|
4575
4549
|
} else {
|
|
@@ -4676,8 +4650,10 @@ var dataStructureTyped = (() => {
|
|
|
4676
4650
|
* Fix the entire heap to maintain heap properties.
|
|
4677
4651
|
*/
|
|
4678
4652
|
fix() {
|
|
4653
|
+
const results = [];
|
|
4679
4654
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
4680
|
-
this._sinkDown(i, this.elements.length >> 1);
|
|
4655
|
+
results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
4656
|
+
return results;
|
|
4681
4657
|
}
|
|
4682
4658
|
/**
|
|
4683
4659
|
* Time Complexity: O(n)
|
|
@@ -4704,7 +4680,7 @@ var dataStructureTyped = (() => {
|
|
|
4704
4680
|
let index = 0;
|
|
4705
4681
|
for (const current of this) {
|
|
4706
4682
|
if (callback.call(thisArg, current, index, this)) {
|
|
4707
|
-
filteredList.
|
|
4683
|
+
filteredList.add(current);
|
|
4708
4684
|
}
|
|
4709
4685
|
index++;
|
|
4710
4686
|
}
|
|
@@ -4743,13 +4719,6 @@ var dataStructureTyped = (() => {
|
|
|
4743
4719
|
}
|
|
4744
4720
|
return mappedHeap;
|
|
4745
4721
|
}
|
|
4746
|
-
/**
|
|
4747
|
-
* Time Complexity: O(log n)
|
|
4748
|
-
* Space Complexity: O(1)
|
|
4749
|
-
*/
|
|
4750
|
-
print() {
|
|
4751
|
-
console.log([...this]);
|
|
4752
|
-
}
|
|
4753
4722
|
*_getIterator() {
|
|
4754
4723
|
for (const element of this.elements) {
|
|
4755
4724
|
yield element;
|
|
@@ -4777,6 +4746,7 @@ var dataStructureTyped = (() => {
|
|
|
4777
4746
|
index = parent;
|
|
4778
4747
|
}
|
|
4779
4748
|
this.elements[index] = element;
|
|
4749
|
+
return true;
|
|
4780
4750
|
}
|
|
4781
4751
|
/**
|
|
4782
4752
|
* Time Complexity: O(log n)
|
|
@@ -4802,6 +4772,7 @@ var dataStructureTyped = (() => {
|
|
|
4802
4772
|
index = left;
|
|
4803
4773
|
}
|
|
4804
4774
|
this.elements[index] = element;
|
|
4775
|
+
return true;
|
|
4805
4776
|
}
|
|
4806
4777
|
};
|
|
4807
4778
|
var FibonacciHeapNode = class {
|
|
@@ -12340,11 +12311,11 @@ var dataStructureTyped = (() => {
|
|
|
12340
12311
|
* @returns The `filter` method is returning an array of strings (`string[]`).
|
|
12341
12312
|
*/
|
|
12342
12313
|
filter(predicate, thisArg) {
|
|
12343
|
-
const results =
|
|
12314
|
+
const results = new _Trie();
|
|
12344
12315
|
let index = 0;
|
|
12345
12316
|
for (const word of this) {
|
|
12346
12317
|
if (predicate.call(thisArg, word, index, this)) {
|
|
12347
|
-
results.
|
|
12318
|
+
results.add(word);
|
|
12348
12319
|
}
|
|
12349
12320
|
index++;
|
|
12350
12321
|
}
|
|
@@ -12376,9 +12347,6 @@ var dataStructureTyped = (() => {
|
|
|
12376
12347
|
}
|
|
12377
12348
|
return newTrie;
|
|
12378
12349
|
}
|
|
12379
|
-
print() {
|
|
12380
|
-
console.log([...this]);
|
|
12381
|
-
}
|
|
12382
12350
|
*_getIterator() {
|
|
12383
12351
|
function* _dfs(node, path) {
|
|
12384
12352
|
if (node.isEnd) {
|