data-structure-typed 1.49.4 → 1.49.6
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 +15 -26
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +57 -50
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +212 -199
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +115 -90
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +64 -60
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +61 -95
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +853 -943
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -16
- package/test/performance/data-structures/comparison/comparison.test.ts +19 -47
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +14 -38
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/deque.test.ts +6 -7
- package/test/performance/data-structures/queue/queue.test.ts +13 -29
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -29
- package/test/unit/data-structures/binary-tree/bst.test.ts +69 -27
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
- package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
- package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -21,16 +21,16 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
constructor(
|
|
24
|
+
constructor(nodes, options) {
|
|
25
25
|
super([], options);
|
|
26
|
-
if (
|
|
27
|
-
this.addMany(
|
|
26
|
+
if (nodes)
|
|
27
|
+
this.addMany(nodes);
|
|
28
28
|
}
|
|
29
29
|
_count = 0;
|
|
30
30
|
// TODO the _count is not accurate after nodes count modified
|
|
31
31
|
get count() {
|
|
32
32
|
let sum = 0;
|
|
33
|
-
this.subTreeTraverse(node => sum += node.count);
|
|
33
|
+
this.subTreeTraverse(node => (sum += node.count));
|
|
34
34
|
return sum;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -48,30 +48,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
48
48
|
createTree(options) {
|
|
49
49
|
return new TreeMultimap([], {
|
|
50
50
|
iterationType: this.iterationType,
|
|
51
|
-
variant: this.variant,
|
|
51
|
+
variant: this.variant,
|
|
52
|
+
...options
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
55
|
/**
|
|
55
|
-
* The function
|
|
56
|
-
* @param
|
|
57
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
|
-
* class.
|
|
59
|
-
*/
|
|
60
|
-
isNode(exemplar) {
|
|
61
|
-
return exemplar instanceof TreeMultimapNode;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
-
* data type.
|
|
67
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
-
*/
|
|
69
|
-
isNotNodeInstance(potentialKey) {
|
|
70
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
74
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
56
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
57
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
75
58
|
* can be one of the following:
|
|
76
59
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
77
60
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -80,16 +63,16 @@ export class TreeMultimap extends AVLTree {
|
|
|
80
63
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
81
64
|
* @returns a node of type `N` or `undefined`.
|
|
82
65
|
*/
|
|
83
|
-
exemplarToNode(
|
|
66
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
84
67
|
let node;
|
|
85
|
-
if (
|
|
68
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
86
69
|
return;
|
|
87
70
|
}
|
|
88
|
-
else if (this.isNode(
|
|
89
|
-
node =
|
|
71
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
72
|
+
node = keyOrNodeOrEntry;
|
|
90
73
|
}
|
|
91
|
-
else if (this.isEntry(
|
|
92
|
-
const [key, value] =
|
|
74
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
75
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
93
76
|
if (key === undefined || key === null) {
|
|
94
77
|
return;
|
|
95
78
|
}
|
|
@@ -97,8 +80,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
97
80
|
node = this.createNode(key, value, count);
|
|
98
81
|
}
|
|
99
82
|
}
|
|
100
|
-
else if (this.isNotNodeInstance(
|
|
101
|
-
node = this.createNode(
|
|
83
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
84
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
102
85
|
}
|
|
103
86
|
else {
|
|
104
87
|
return;
|
|
@@ -106,12 +89,31 @@ export class TreeMultimap extends AVLTree {
|
|
|
106
89
|
return node;
|
|
107
90
|
}
|
|
108
91
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
92
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
93
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
94
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
95
|
+
* class.
|
|
96
|
+
*/
|
|
97
|
+
isNode(keyOrNodeOrEntry) {
|
|
98
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
102
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
103
|
+
* data type.
|
|
104
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
105
|
+
*/
|
|
106
|
+
isNotNodeInstance(potentialKey) {
|
|
107
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(log n)
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
111
113
|
*/
|
|
112
114
|
/**
|
|
113
|
-
* Time Complexity: O(log n)
|
|
114
|
-
* Space Complexity: O(1)
|
|
115
|
+
* Time Complexity: O(log n)
|
|
116
|
+
* Space Complexity: O(1)
|
|
115
117
|
*
|
|
116
118
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
117
119
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -128,21 +130,22 @@ export class TreeMultimap extends AVLTree {
|
|
|
128
130
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
129
131
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
130
132
|
if (newNode === undefined)
|
|
131
|
-
return;
|
|
133
|
+
return false;
|
|
132
134
|
const orgNodeCount = newNode?.count || 0;
|
|
133
135
|
const inserted = super.add(newNode);
|
|
134
136
|
if (inserted) {
|
|
135
137
|
this._count += orgNodeCount;
|
|
136
138
|
}
|
|
137
|
-
return
|
|
139
|
+
return true;
|
|
138
140
|
}
|
|
139
141
|
/**
|
|
140
|
-
* Time Complexity: O(k log n)
|
|
141
|
-
* Space Complexity: O(1)
|
|
142
|
+
* Time Complexity: O(k log n)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
142
145
|
*/
|
|
143
146
|
/**
|
|
144
|
-
* Time Complexity: O(k log n)
|
|
145
|
-
* Space Complexity: O(1)
|
|
147
|
+
* Time Complexity: O(k log n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
146
149
|
*
|
|
147
150
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
148
151
|
* structure.
|
|
@@ -154,12 +157,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
154
157
|
return super.addMany(keysOrNodesOrEntries);
|
|
155
158
|
}
|
|
156
159
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(
|
|
160
|
+
* Time Complexity: O(n log n)
|
|
161
|
+
* Space Complexity: O(n)
|
|
162
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
159
163
|
*/
|
|
160
164
|
/**
|
|
161
|
-
* Time Complexity: O(n log n)
|
|
162
|
-
* Space Complexity: O(n)
|
|
165
|
+
* Time Complexity: O(n log n)
|
|
166
|
+
* Space Complexity: O(n)
|
|
163
167
|
*
|
|
164
168
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
165
169
|
* tree using either a recursive or iterative approach.
|
|
@@ -205,12 +209,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
205
209
|
}
|
|
206
210
|
}
|
|
207
211
|
/**
|
|
208
|
-
* Time Complexity: O(k log n)
|
|
209
|
-
* Space Complexity: O(1)
|
|
212
|
+
* Time Complexity: O(k log n)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
210
215
|
*/
|
|
211
216
|
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
217
|
+
* Time Complexity: O(k log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
214
219
|
*
|
|
215
220
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
216
221
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -285,10 +290,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
285
290
|
return deletedResult;
|
|
286
291
|
}
|
|
287
292
|
/**
|
|
288
|
-
* Time Complexity: O(
|
|
289
|
-
* Space Complexity: O(
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
290
295
|
*/
|
|
291
296
|
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
292
300
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
293
301
|
*/
|
|
294
302
|
clear() {
|
|
@@ -311,48 +319,6 @@ export class TreeMultimap extends AVLTree {
|
|
|
311
319
|
this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
312
320
|
return cloned;
|
|
313
321
|
}
|
|
314
|
-
/**
|
|
315
|
-
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
316
|
-
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
317
|
-
*
|
|
318
|
-
* The function adds a new node to a binary tree, either as the left child or the right child of a
|
|
319
|
-
* given parent node.
|
|
320
|
-
* @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
|
|
321
|
-
* added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
|
|
322
|
-
* `undefined` if there is no node to add.
|
|
323
|
-
* @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
|
|
324
|
-
* which the new node will be added as a child. It can be either a node object (`N`) or a key value
|
|
325
|
-
* (`K`).
|
|
326
|
-
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
327
|
-
* added, or `undefined` if no node was added.
|
|
328
|
-
*/
|
|
329
|
-
_addTo(newNode, parent) {
|
|
330
|
-
parent = this.ensureNode(parent);
|
|
331
|
-
if (parent) {
|
|
332
|
-
if (parent.left === undefined) {
|
|
333
|
-
parent.left = newNode;
|
|
334
|
-
if (newNode !== undefined) {
|
|
335
|
-
this._size = this.size + 1;
|
|
336
|
-
this._count += newNode.count;
|
|
337
|
-
}
|
|
338
|
-
return parent.left;
|
|
339
|
-
}
|
|
340
|
-
else if (parent.right === undefined) {
|
|
341
|
-
parent.right = newNode;
|
|
342
|
-
if (newNode !== undefined) {
|
|
343
|
-
this._size = this.size + 1;
|
|
344
|
-
this._count += newNode.count;
|
|
345
|
-
}
|
|
346
|
-
return parent.right;
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
else {
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
322
|
/**
|
|
357
323
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
358
324
|
* @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
9
|
-
import { IterableEntryBase } from
|
|
9
|
+
import { IterableEntryBase } from '../base';
|
|
10
10
|
import { IGraph } from '../../interfaces';
|
|
11
11
|
export declare abstract class AbstractVertex<V = any> {
|
|
12
12
|
key: VertexKey;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { uuidV4 } from '../../utils';
|
|
2
|
-
import { IterableEntryBase } from
|
|
2
|
+
import { IterableEntryBase } from '../base';
|
|
3
3
|
import { Heap } from '../heap';
|
|
4
4
|
import { Queue } from '../queue';
|
|
5
5
|
export class AbstractVertex {
|
|
@@ -95,7 +95,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
95
95
|
}
|
|
96
96
|
isVertexKey(potentialKey) {
|
|
97
97
|
const potentialKeyType = typeof potentialKey;
|
|
98
|
-
return potentialKeyType ===
|
|
98
|
+
return potentialKeyType === 'string' || potentialKeyType === 'number';
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
101
|
* Time Complexity: O(K), where K is the number of vertexMap to be removed.
|
|
@@ -1010,7 +1010,8 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
1010
1010
|
const visited = new Set();
|
|
1011
1011
|
const dfs = (vertex, currentPath, visited) => {
|
|
1012
1012
|
if (visited.has(vertex)) {
|
|
1013
|
-
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) &&
|
|
1013
|
+
if (((!isInclude2Cycle && currentPath.length > 2) || (isInclude2Cycle && currentPath.length >= 2)) &&
|
|
1014
|
+
currentPath[0] === vertex.key) {
|
|
1014
1015
|
cycles.push([...currentPath]);
|
|
1015
1016
|
}
|
|
1016
1017
|
return;
|
|
@@ -121,7 +121,7 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
121
121
|
*/
|
|
122
122
|
protected _getIterator(): IterableIterator<[K, V]>;
|
|
123
123
|
protected _hashFn: (key: K) => string;
|
|
124
|
-
protected _isObjKey(key: any): key is
|
|
124
|
+
protected _isObjKey(key: any): key is object | ((...args: any[]) => any);
|
|
125
125
|
protected _getNoObjKey(key: K): string;
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
@@ -215,11 +215,11 @@ export class HashMap extends IterableEntryBase {
|
|
|
215
215
|
_getNoObjKey(key) {
|
|
216
216
|
const keyType = typeof key;
|
|
217
217
|
let strKey;
|
|
218
|
-
if (keyType !==
|
|
218
|
+
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
219
219
|
strKey = this._hashFn(key);
|
|
220
220
|
}
|
|
221
221
|
else {
|
|
222
|
-
if (keyType ===
|
|
222
|
+
if (keyType === 'number') {
|
|
223
223
|
// TODO numeric key should has its own hash
|
|
224
224
|
strKey = key;
|
|
225
225
|
}
|
|
@@ -398,11 +398,10 @@ export class Heap extends IterableElementBase {
|
|
|
398
398
|
_sinkDown(index, halfLength) {
|
|
399
399
|
const element = this.elements[index];
|
|
400
400
|
while (index < halfLength) {
|
|
401
|
-
let left = index << 1 | 1;
|
|
401
|
+
let left = (index << 1) | 1;
|
|
402
402
|
const right = left + 1;
|
|
403
403
|
let minItem = this.elements[left];
|
|
404
|
-
if (right < this.elements.length &&
|
|
405
|
-
this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
404
|
+
if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
406
405
|
left = right;
|
|
407
406
|
minItem = this.elements[right];
|
|
408
407
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback } from
|
|
9
|
-
import { IterableElementBase } from
|
|
8
|
+
import type { ElementCallback } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
10
|
export declare class SinglyLinkedListNode<E = any> {
|
|
11
11
|
value: E;
|
|
12
12
|
next: SinglyLinkedListNode<E> | undefined;
|
|
@@ -5,17 +5,135 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
export declare class
|
|
9
|
-
protected readonly _matrix: Array<Array<V>>;
|
|
8
|
+
export declare class Matrix {
|
|
10
9
|
/**
|
|
11
|
-
* The constructor
|
|
12
|
-
*
|
|
13
|
-
* @param
|
|
10
|
+
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
11
|
+
* default values if no options are provided.
|
|
12
|
+
* @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
|
|
13
|
+
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
14
|
+
* properties:
|
|
14
15
|
*/
|
|
15
|
-
constructor(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
constructor(data: number[][], options?: {
|
|
17
|
+
rows?: number;
|
|
18
|
+
cols?: number;
|
|
19
|
+
addFn?: (a: number, b: number) => any;
|
|
20
|
+
subtractFn?: (a: number, b: number) => any;
|
|
21
|
+
multiplyFn?: (a: number, b: number) => any;
|
|
19
22
|
});
|
|
20
|
-
|
|
23
|
+
protected _rows: number;
|
|
24
|
+
get rows(): number;
|
|
25
|
+
protected _cols: number;
|
|
26
|
+
get cols(): number;
|
|
27
|
+
protected _data: number[][];
|
|
28
|
+
get data(): number[][];
|
|
29
|
+
get addFn(): (a: number | undefined, b: number) => number | undefined;
|
|
30
|
+
get subtractFn(): (a: number, b: number) => number;
|
|
31
|
+
get multiplyFn(): (a: number, b: number) => number;
|
|
32
|
+
/**
|
|
33
|
+
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
34
|
+
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
35
|
+
* retrieve from the data array.
|
|
36
|
+
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
37
|
+
* retrieve from the data array.
|
|
38
|
+
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
39
|
+
* Otherwise, it returns `undefined`.
|
|
40
|
+
*/
|
|
41
|
+
get(row: number, col: number): number | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
44
|
+
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
45
|
+
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
46
|
+
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
47
|
+
* two-dimensional array.
|
|
48
|
+
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
49
|
+
* specified row and column in the data array.
|
|
50
|
+
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
51
|
+
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
52
|
+
* set.
|
|
53
|
+
*/
|
|
54
|
+
set(row: number, col: number, value: number): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* The function checks if the dimensions of the given matrix match the dimensions of the current
|
|
57
|
+
* matrix.
|
|
58
|
+
* @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
|
|
59
|
+
* @returns a boolean value.
|
|
60
|
+
*/
|
|
61
|
+
isMatchForCalculate(matrix: Matrix): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
64
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
65
|
+
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
66
|
+
* current matrix with the provided `matrix` parameter.
|
|
67
|
+
*/
|
|
68
|
+
add(matrix: Matrix): Matrix | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
71
|
+
* matrix with the result.
|
|
72
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
73
|
+
* represents the matrix that you want to subtract from the current matrix.
|
|
74
|
+
* @returns a new Matrix object with the result of the subtraction operation.
|
|
75
|
+
*/
|
|
76
|
+
subtract(matrix: Matrix): Matrix | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
79
|
+
* as a new matrix.
|
|
80
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
81
|
+
* @returns a new Matrix object.
|
|
82
|
+
*/
|
|
83
|
+
multiply(matrix: Matrix): Matrix | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
86
|
+
* original matrix.
|
|
87
|
+
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
88
|
+
*/
|
|
89
|
+
transpose(): Matrix;
|
|
90
|
+
/**
|
|
91
|
+
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
92
|
+
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
93
|
+
*/
|
|
94
|
+
inverse(): Matrix | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
97
|
+
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
98
|
+
* @returns a new Matrix object.
|
|
99
|
+
*/
|
|
100
|
+
dot(matrix: Matrix): Matrix | undefined;
|
|
101
|
+
protected _addFn(a: number | undefined, b: number): number | undefined;
|
|
102
|
+
protected _subtractFn(a: number, b: number): number;
|
|
103
|
+
protected _multiplyFn(a: number, b: number): number;
|
|
104
|
+
/**
|
|
105
|
+
* The function checks if a given row and column index is valid within a specified range.
|
|
106
|
+
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
|
|
107
|
+
* matrix. It is a number that indicates the specific row in the matrix.
|
|
108
|
+
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
|
|
109
|
+
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
|
|
110
|
+
* @returns A boolean value is being returned.
|
|
111
|
+
*/
|
|
112
|
+
protected isValidIndex(row: number, col: number): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* The function `_swapRows` swaps the positions of two rows in an array.
|
|
115
|
+
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
|
|
116
|
+
* @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
|
|
117
|
+
* with the first row.
|
|
118
|
+
*/
|
|
119
|
+
protected _swapRows(row1: number, row2: number): void;
|
|
120
|
+
/**
|
|
121
|
+
* The function scales a specific row in a matrix by a given scalar value.
|
|
122
|
+
* @param {number} row - The `row` parameter represents the index of the row in the matrix that you
|
|
123
|
+
* want to scale. It is a number that indicates the position of the row within the matrix.
|
|
124
|
+
* @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
|
|
125
|
+
* a specific row of a matrix.
|
|
126
|
+
*/
|
|
127
|
+
protected _scaleRow(row: number, scalar: number): void;
|
|
128
|
+
/**
|
|
129
|
+
* The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
|
|
130
|
+
* row.
|
|
131
|
+
* @param {number} targetRow - The targetRow parameter represents the index of the row in which the
|
|
132
|
+
* scaled values will be added.
|
|
133
|
+
* @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
|
|
134
|
+
* values will be scaled and added to the targetRow.
|
|
135
|
+
* @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
|
|
136
|
+
* source row before adding them to the target row.
|
|
137
|
+
*/
|
|
138
|
+
protected _addScaledRow(targetRow: number, sourceRow: number, scalar: number): void;
|
|
21
139
|
}
|