data-structure-typed 0.9.16 → 1.3.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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +134 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,37 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.ArrayDeque = exports.ObjectDeque = exports.Deque = void 0;
|
|
19
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
const linked_list_1 = require("../linked-list");
|
|
20
12
|
// O(n) time complexity of obtaining the value
|
|
21
13
|
// O(1) time complexity of adding at the beginning and the end
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
function Deque() {
|
|
25
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
26
|
-
}
|
|
27
|
-
return Deque;
|
|
28
|
-
}(linked_list_1.DoublyLinkedList));
|
|
14
|
+
class Deque extends linked_list_1.DoublyLinkedList {
|
|
15
|
+
}
|
|
29
16
|
exports.Deque = Deque;
|
|
30
17
|
// O(1) time complexity of obtaining the value
|
|
31
18
|
// O(n) time complexity of adding at the beginning and the end
|
|
32
19
|
// todo tested slowest one
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
class ObjectDeque {
|
|
21
|
+
constructor(capacity) {
|
|
35
22
|
this._nodes = {};
|
|
36
23
|
this._capacity = Number.MAX_SAFE_INTEGER;
|
|
37
24
|
this._first = -1;
|
|
@@ -40,12 +27,38 @@ var ObjectDeque = /** @class */ (function () {
|
|
|
40
27
|
if (capacity !== undefined)
|
|
41
28
|
this._capacity = capacity;
|
|
42
29
|
}
|
|
43
|
-
|
|
30
|
+
get nodes() {
|
|
31
|
+
return this._nodes;
|
|
32
|
+
}
|
|
33
|
+
get capacity() {
|
|
34
|
+
return this._capacity;
|
|
35
|
+
}
|
|
36
|
+
set capacity(value) {
|
|
37
|
+
this._capacity = value;
|
|
38
|
+
}
|
|
39
|
+
get first() {
|
|
40
|
+
return this._first;
|
|
41
|
+
}
|
|
42
|
+
set first(value) {
|
|
43
|
+
this._first = value;
|
|
44
|
+
}
|
|
45
|
+
get last() {
|
|
46
|
+
return this._last;
|
|
47
|
+
}
|
|
48
|
+
set last(value) {
|
|
49
|
+
this._last = value;
|
|
50
|
+
}
|
|
51
|
+
get size() {
|
|
44
52
|
return this._size;
|
|
45
|
-
}
|
|
46
|
-
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The "addFirst" function adds a value to the beginning of an array-like data structure.
|
|
56
|
+
* @param {T} value - The `value` parameter represents the value that you want to add to the beginning of the data
|
|
57
|
+
* structure.
|
|
58
|
+
*/
|
|
59
|
+
addFirst(value) {
|
|
47
60
|
if (this._size === 0) {
|
|
48
|
-
|
|
61
|
+
const mid = Math.floor(this._capacity / 2);
|
|
49
62
|
this._first = mid;
|
|
50
63
|
this._last = mid;
|
|
51
64
|
}
|
|
@@ -54,10 +67,14 @@ var ObjectDeque = /** @class */ (function () {
|
|
|
54
67
|
}
|
|
55
68
|
this._nodes[this._first] = value;
|
|
56
69
|
this._size++;
|
|
57
|
-
}
|
|
58
|
-
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The addLast function adds a value to the end of an array-like data structure.
|
|
73
|
+
* @param {T} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
74
|
+
*/
|
|
75
|
+
addLast(value) {
|
|
59
76
|
if (this._size === 0) {
|
|
60
|
-
|
|
77
|
+
const mid = Math.floor(this._capacity / 2);
|
|
61
78
|
this._first = mid;
|
|
62
79
|
this._last = mid;
|
|
63
80
|
}
|
|
@@ -66,93 +83,194 @@ var ObjectDeque = /** @class */ (function () {
|
|
|
66
83
|
}
|
|
67
84
|
this._nodes[this._last] = value;
|
|
68
85
|
this._size++;
|
|
69
|
-
}
|
|
70
|
-
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The function `pollFirst()` removes and returns the first element in a data structure.
|
|
89
|
+
* @returns The value of the first element in the data structure.
|
|
90
|
+
*/
|
|
91
|
+
pollFirst() {
|
|
71
92
|
if (!this._size)
|
|
72
93
|
return;
|
|
73
|
-
|
|
94
|
+
const value = this.peekFirst();
|
|
74
95
|
delete this._nodes[this._first];
|
|
75
96
|
this._first++;
|
|
76
97
|
this._size--;
|
|
77
98
|
return value;
|
|
78
|
-
}
|
|
79
|
-
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The `peekFirst` function returns the first element in an array-like data structure if it exists.
|
|
102
|
+
* @returns The element at the first position of the `_nodes` array.
|
|
103
|
+
*/
|
|
104
|
+
peekFirst() {
|
|
80
105
|
if (this._size)
|
|
81
106
|
return this._nodes[this._first];
|
|
82
|
-
}
|
|
83
|
-
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The `pollLast()` function removes and returns the last element in a data structure.
|
|
110
|
+
* @returns The value that was removed from the data structure.
|
|
111
|
+
*/
|
|
112
|
+
pollLast() {
|
|
84
113
|
if (!this._size)
|
|
85
114
|
return;
|
|
86
|
-
|
|
115
|
+
const value = this.peekLast();
|
|
87
116
|
delete this._nodes[this._last];
|
|
88
117
|
this._last--;
|
|
89
118
|
this._size--;
|
|
90
119
|
return value;
|
|
91
|
-
}
|
|
92
|
-
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The `peekLast()` function returns the last element in an array-like data structure.
|
|
123
|
+
* @returns The last element in the array "_nodes" is being returned.
|
|
124
|
+
*/
|
|
125
|
+
peekLast() {
|
|
93
126
|
if (this._size)
|
|
94
127
|
return this._nodes[this._last];
|
|
95
|
-
}
|
|
96
|
-
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The get function returns the element at the specified index in an array-like data structure.
|
|
131
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
132
|
+
* retrieve from the array.
|
|
133
|
+
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
134
|
+
* index, `null` is returned.
|
|
135
|
+
*/
|
|
136
|
+
get(index) {
|
|
97
137
|
return this._nodes[this._first + index] || null;
|
|
98
|
-
}
|
|
99
|
-
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The function checks if the size of a data structure is less than or equal to zero.
|
|
141
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
142
|
+
*/
|
|
143
|
+
isEmpty() {
|
|
100
144
|
return this._size <= 0;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
145
|
+
}
|
|
146
|
+
_seNodes(value) {
|
|
147
|
+
this._nodes = value;
|
|
148
|
+
}
|
|
149
|
+
_setSize(value) {
|
|
150
|
+
this._size = value;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
104
153
|
exports.ObjectDeque = ObjectDeque;
|
|
105
154
|
// O(1) time complexity of obtaining the value
|
|
106
155
|
// O(n) time complexity of adding at the beginning and the end
|
|
107
|
-
|
|
108
|
-
|
|
156
|
+
class ArrayDeque {
|
|
157
|
+
constructor() {
|
|
109
158
|
this._nodes = [];
|
|
110
159
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
160
|
+
get size() {
|
|
161
|
+
return this._nodes.length;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* O(n) time complexity of adding at the beginning and the end
|
|
165
|
+
*/
|
|
166
|
+
/**
|
|
167
|
+
* The function "addLast" adds a value to the end of an array.
|
|
168
|
+
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
|
|
169
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
170
|
+
*/
|
|
171
|
+
addLast(value) {
|
|
119
172
|
return this._nodes.push(value);
|
|
120
|
-
}
|
|
121
|
-
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
176
|
+
* @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
177
|
+
*/
|
|
178
|
+
pollLast() {
|
|
122
179
|
var _a;
|
|
123
180
|
return (_a = this._nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
124
|
-
}
|
|
125
|
-
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
184
|
+
* @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
185
|
+
* empty.
|
|
186
|
+
*/
|
|
187
|
+
pollFirst() {
|
|
126
188
|
var _a;
|
|
127
189
|
return (_a = this._nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
128
|
-
}
|
|
129
|
-
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* O(n) time complexity of adding at the beginning and the end
|
|
193
|
+
*/
|
|
194
|
+
/**
|
|
195
|
+
* The function "addFirst" adds a value to the beginning of an array.
|
|
196
|
+
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
197
|
+
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
198
|
+
* `value` at the beginning.
|
|
199
|
+
*/
|
|
200
|
+
addFirst(value) {
|
|
130
201
|
return this._nodes.unshift(value);
|
|
131
|
-
}
|
|
132
|
-
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The `peekFirst` function returns the first element of an array or null if the array is empty.
|
|
205
|
+
* @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
|
|
206
|
+
* empty, it will return `null`.
|
|
207
|
+
*/
|
|
208
|
+
peekFirst() {
|
|
133
209
|
var _a;
|
|
134
210
|
return (_a = this._nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
135
|
-
}
|
|
136
|
-
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The `peekLast` function returns the last element of an array or null if the array is empty.
|
|
214
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
215
|
+
*/
|
|
216
|
+
peekLast() {
|
|
137
217
|
var _a;
|
|
138
218
|
return (_a = this._nodes[this._nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
139
|
-
}
|
|
140
|
-
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* O(1) time complexity of obtaining the value
|
|
222
|
+
*/
|
|
223
|
+
/**
|
|
224
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
225
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
226
|
+
* retrieve from the array.
|
|
227
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
228
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
229
|
+
*/
|
|
230
|
+
get(index) {
|
|
141
231
|
var _a;
|
|
142
232
|
return (_a = this._nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
143
|
-
}
|
|
144
|
-
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* The set function assigns a value to a specific index in an array.
|
|
236
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
237
|
+
* that you want to set a new value for.
|
|
238
|
+
* @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
239
|
+
* _nodes array.
|
|
240
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
241
|
+
*/
|
|
242
|
+
set(index, value) {
|
|
145
243
|
return this._nodes[index] = value;
|
|
146
|
-
}
|
|
147
|
-
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* The insert function adds a value at a specified index in an array.
|
|
247
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
248
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
249
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
250
|
+
* @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
251
|
+
* index.
|
|
252
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
253
|
+
* are being removed, an empty array will be returned.
|
|
254
|
+
*/
|
|
255
|
+
insert(index, value) {
|
|
148
256
|
return this._nodes.splice(index, 0, value);
|
|
149
|
-
}
|
|
150
|
-
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* The remove function removes an element from an array at a specified index.
|
|
260
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
261
|
+
* is a number that represents the index of the element to be removed.
|
|
262
|
+
* @returns The method is returning an array containing the removed element.
|
|
263
|
+
*/
|
|
264
|
+
remove(index) {
|
|
151
265
|
return this._nodes.splice(index, 1);
|
|
152
|
-
}
|
|
153
|
-
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* The function checks if an array called "_nodes" is empty.
|
|
269
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
270
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
271
|
+
*/
|
|
272
|
+
isEmpty() {
|
|
154
273
|
return this._nodes.length === 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
}());
|
|
274
|
+
}
|
|
275
|
+
}
|
|
158
276
|
exports.ArrayDeque = ArrayDeque;
|
|
@@ -1,75 +1,102 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license MIT
|
|
3
|
-
* @copyright
|
|
3
|
+
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
|
-
|
|
6
|
+
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
+
export declare class Queue<T = any> extends SinglyLinkedList<T> {
|
|
8
|
+
/**
|
|
9
|
+
* The enqueue function adds a value to the end of an array.
|
|
10
|
+
* @param {T} value - The value parameter represents the value that you want to add to the queue.
|
|
11
|
+
*/
|
|
12
|
+
enqueue(value: T): void;
|
|
13
|
+
/**
|
|
14
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
15
|
+
* @returns The method is returning the element at the front of the queue, or null if the queue is empty.
|
|
16
|
+
*/
|
|
17
|
+
dequeue(): T | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
|
+
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
21
|
+
*/
|
|
22
|
+
peek(): T | undefined;
|
|
23
|
+
[Symbol.iterator](): Generator<T, void, unknown>;
|
|
24
|
+
}
|
|
25
|
+
export declare class ArrayQueue<T = number> {
|
|
7
26
|
protected _nodes: T[];
|
|
8
27
|
protected _offset: number;
|
|
9
28
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param {
|
|
29
|
+
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
30
|
+
* @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
|
|
31
|
+
* will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
|
|
32
|
+
* initialized as an empty array.
|
|
12
33
|
*/
|
|
13
34
|
constructor(elements?: T[]);
|
|
14
35
|
/**
|
|
15
|
-
*
|
|
36
|
+
* The size function returns the number of elements in an array.
|
|
37
|
+
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
38
|
+
*/
|
|
39
|
+
get size(): number;
|
|
40
|
+
/**
|
|
41
|
+
* The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
|
|
16
42
|
* @public
|
|
17
43
|
* @static
|
|
18
|
-
* @param {
|
|
19
|
-
* @
|
|
44
|
+
* @param {T[]} elements - The "elements" parameter is an array of elements of type T.
|
|
45
|
+
* @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
|
|
46
|
+
* array.
|
|
20
47
|
*/
|
|
21
|
-
static fromArray<T>(elements: T[]):
|
|
48
|
+
static fromArray<T>(elements: T[]): ArrayQueue<T>;
|
|
22
49
|
/**
|
|
23
|
-
* Adds an element at the back of the queue.
|
|
24
|
-
* @
|
|
25
|
-
* @
|
|
50
|
+
* The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
|
|
51
|
+
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
|
|
52
|
+
* @returns The `add` method is returning a `Queue<T>` object.
|
|
26
53
|
*/
|
|
27
|
-
|
|
54
|
+
push(element: T): ArrayQueue<T>;
|
|
28
55
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @returns
|
|
56
|
+
* The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
|
|
57
|
+
* necessary to optimize performance.
|
|
58
|
+
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
|
|
32
59
|
*/
|
|
33
|
-
|
|
60
|
+
shift(): T | null;
|
|
34
61
|
/**
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
62
|
+
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
63
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
64
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
38
65
|
*/
|
|
39
66
|
peek(): T | null;
|
|
40
67
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @
|
|
43
|
-
*
|
|
68
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
69
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
70
|
+
* array is empty, it returns `null`.
|
|
44
71
|
*/
|
|
45
72
|
peekLast(): T | null;
|
|
46
73
|
/**
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
* @returns {number}
|
|
74
|
+
* The enqueue function adds a value to the end of a queue.
|
|
75
|
+
* @param {T} value - The value parameter represents the value that you want to add to the queue.
|
|
50
76
|
*/
|
|
51
|
-
|
|
77
|
+
enqueue(value: T): void;
|
|
52
78
|
/**
|
|
53
|
-
*
|
|
54
|
-
* @
|
|
55
|
-
|
|
79
|
+
* The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
|
|
80
|
+
* @returns The method is returning a value of type T or null.
|
|
81
|
+
*/
|
|
82
|
+
dequeue(): T | null;
|
|
83
|
+
/**
|
|
84
|
+
* The function checks if a data structure is empty by comparing its size to zero.
|
|
85
|
+
* @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
|
|
56
86
|
*/
|
|
57
87
|
isEmpty(): boolean;
|
|
58
88
|
/**
|
|
59
|
-
*
|
|
60
|
-
* @
|
|
61
|
-
* @returns {array}
|
|
89
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
|
|
90
|
+
* @returns An array of type T is being returned.
|
|
62
91
|
*/
|
|
63
92
|
toArray(): T[];
|
|
64
93
|
/**
|
|
65
|
-
*
|
|
66
|
-
* @public
|
|
94
|
+
* The clear function resets the nodes array and offset to their initial values.
|
|
67
95
|
*/
|
|
68
96
|
clear(): void;
|
|
69
97
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
* @return {Queue}
|
|
98
|
+
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
99
|
+
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
73
100
|
*/
|
|
74
|
-
clone():
|
|
101
|
+
clone(): ArrayQueue<T>;
|
|
75
102
|
}
|