deque-typed 1.52.1 → 1.52.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/dist/data-structures/binary-tree/binary-tree.js +2 -2
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/queue/queue.d.ts +18 -0
- package/dist/data-structures/queue/queue.js +32 -6
- package/dist/types/data-structures/queue/queue.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -12
- package/src/data-structures/binary-tree/avl-tree.ts +9 -8
- package/src/data-structures/binary-tree/binary-tree.ts +11 -10
- package/src/data-structures/binary-tree/bst.ts +10 -9
- package/src/data-structures/binary-tree/rb-tree.ts +8 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +8 -7
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +38 -7
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +2 -2
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
|
@@ -922,8 +922,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
922
922
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
923
923
|
node = stack.pop();
|
|
924
924
|
if (this.isRealNode(node)) {
|
|
925
|
-
const leftMinHeight = this.isRealNode(node.left) ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
926
|
-
const rightMinHeight = this.isRealNode(node.right) ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
925
|
+
const leftMinHeight = this.isRealNode(node.left) ? ((_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1) : -1;
|
|
926
|
+
const rightMinHeight = this.isRealNode(node.right) ? ((_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1) : -1;
|
|
927
927
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
928
928
|
last = node;
|
|
929
929
|
node = null;
|
|
@@ -683,8 +683,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
683
683
|
if (!node.right || last === node.right) {
|
|
684
684
|
node = stack.pop();
|
|
685
685
|
if (node) {
|
|
686
|
-
const left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
687
|
-
const right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
686
|
+
const left = node.left ? ((_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1) : -1;
|
|
687
|
+
const right = node.right ? ((_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1) : -1;
|
|
688
688
|
if (Math.abs(left - right) > 1)
|
|
689
689
|
return false;
|
|
690
690
|
depths.set(node, 1 + Math.max(left, right));
|
|
@@ -60,6 +60,18 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
60
60
|
* array is empty, it returns `undefined`.
|
|
61
61
|
*/
|
|
62
62
|
get last(): E | undefined;
|
|
63
|
+
_autoCompactRatio: number;
|
|
64
|
+
/**
|
|
65
|
+
* This function returns the value of the autoCompactRatio property.
|
|
66
|
+
* @returns The `autoCompactRatio` property of the object, which is a number.
|
|
67
|
+
*/
|
|
68
|
+
get autoCompactRatio(): number;
|
|
69
|
+
/**
|
|
70
|
+
* The above function sets the autoCompactRatio property to a specified number in TypeScript.
|
|
71
|
+
* @param {number} v - The parameter `v` represents the value that will be assigned to the
|
|
72
|
+
* `_autoCompactRatio` property.
|
|
73
|
+
*/
|
|
74
|
+
set autoCompactRatio(v: number);
|
|
63
75
|
/**
|
|
64
76
|
* Time Complexity: O(n)
|
|
65
77
|
* Space Complexity: O(n)
|
|
@@ -159,6 +171,12 @@ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Q
|
|
|
159
171
|
* The clear function resets the elements array and offset to their initial values.
|
|
160
172
|
*/
|
|
161
173
|
clear(): void;
|
|
174
|
+
/**
|
|
175
|
+
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
176
|
+
* offset to zero.
|
|
177
|
+
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
178
|
+
*/
|
|
179
|
+
compact(): boolean;
|
|
162
180
|
/**
|
|
163
181
|
* Time Complexity: O(n)
|
|
164
182
|
* Space Complexity: O(n)
|
|
@@ -17,6 +17,11 @@ class Queue extends base_1.IterableElementBase {
|
|
|
17
17
|
super(options);
|
|
18
18
|
this._elements = [];
|
|
19
19
|
this._offset = 0;
|
|
20
|
+
this._autoCompactRatio = 0.5;
|
|
21
|
+
if (options) {
|
|
22
|
+
const { autoCompactRatio = 0.5 } = options;
|
|
23
|
+
this._autoCompactRatio = autoCompactRatio;
|
|
24
|
+
}
|
|
20
25
|
if (elements) {
|
|
21
26
|
for (const el of elements) {
|
|
22
27
|
if (this.toElementFn)
|
|
@@ -77,6 +82,21 @@ class Queue extends base_1.IterableElementBase {
|
|
|
77
82
|
get last() {
|
|
78
83
|
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
79
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* This function returns the value of the autoCompactRatio property.
|
|
87
|
+
* @returns The `autoCompactRatio` property of the object, which is a number.
|
|
88
|
+
*/
|
|
89
|
+
get autoCompactRatio() {
|
|
90
|
+
return this._autoCompactRatio;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The above function sets the autoCompactRatio property to a specified number in TypeScript.
|
|
94
|
+
* @param {number} v - The parameter `v` represents the value that will be assigned to the
|
|
95
|
+
* `_autoCompactRatio` property.
|
|
96
|
+
*/
|
|
97
|
+
set autoCompactRatio(v) {
|
|
98
|
+
this._autoCompactRatio = v;
|
|
99
|
+
}
|
|
80
100
|
/**
|
|
81
101
|
* Time Complexity: O(n)
|
|
82
102
|
* Space Complexity: O(n)
|
|
@@ -127,12 +147,8 @@ class Queue extends base_1.IterableElementBase {
|
|
|
127
147
|
return undefined;
|
|
128
148
|
const first = this.first;
|
|
129
149
|
this._offset += 1;
|
|
130
|
-
if (this.offset
|
|
131
|
-
|
|
132
|
-
// only delete dequeued elements when reaching half size
|
|
133
|
-
// to decrease latency of shifting elements.
|
|
134
|
-
this._elements = this.elements.slice(this.offset);
|
|
135
|
-
this._offset = 0;
|
|
150
|
+
if (this.offset / this.elements.length > this.autoCompactRatio)
|
|
151
|
+
this.compact();
|
|
136
152
|
return first;
|
|
137
153
|
}
|
|
138
154
|
/**
|
|
@@ -208,6 +224,16 @@ class Queue extends base_1.IterableElementBase {
|
|
|
208
224
|
this._elements = [];
|
|
209
225
|
this._offset = 0;
|
|
210
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
229
|
+
* offset to zero.
|
|
230
|
+
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
231
|
+
*/
|
|
232
|
+
compact() {
|
|
233
|
+
this._elements = this.elements.slice(this.offset);
|
|
234
|
+
this._offset = 0;
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
211
237
|
/**
|
|
212
238
|
* Time Complexity: O(n)
|
|
213
239
|
* Space Complexity: O(n)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deque-typed",
|
|
3
|
-
"version": "1.52.
|
|
3
|
+
"version": "1.52.2",
|
|
4
4
|
"description": "Deque. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -119,6 +119,6 @@
|
|
|
119
119
|
"typescript": "^4.9.5"
|
|
120
120
|
},
|
|
121
121
|
"dependencies": {
|
|
122
|
-
"data-structure-typed": "^1.52.
|
|
122
|
+
"data-structure-typed": "^1.52.2"
|
|
123
123
|
}
|
|
124
124
|
}
|
|
@@ -42,7 +42,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
42
42
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
43
43
|
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
44
44
|
*/
|
|
45
|
-
*
|
|
45
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<E> {
|
|
46
46
|
yield* this._getIterator(...args);
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -56,7 +56,7 @@ export abstract class IterableElementBase<E, R, C> {
|
|
|
56
56
|
*
|
|
57
57
|
* The function returns an iterator that yields all the values in the object.
|
|
58
58
|
*/
|
|
59
|
-
*
|
|
59
|
+
*values(): IterableIterator<E> {
|
|
60
60
|
for (const item of this) {
|
|
61
61
|
yield item;
|
|
62
62
|
}
|
|
@@ -35,7 +35,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
35
35
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
36
36
|
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
37
37
|
*/
|
|
38
|
-
*
|
|
38
|
+
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
39
39
|
yield* this._getIterator(...args);
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -50,7 +50,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
50
50
|
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
51
51
|
* be undefined.
|
|
52
52
|
*/
|
|
53
|
-
*
|
|
53
|
+
*entries(): IterableIterator<[K, V | undefined]> {
|
|
54
54
|
for (const item of this) {
|
|
55
55
|
yield item;
|
|
56
56
|
}
|
|
@@ -66,7 +66,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
66
66
|
*
|
|
67
67
|
* The function returns an iterator that yields the keys of a data structure.
|
|
68
68
|
*/
|
|
69
|
-
*
|
|
69
|
+
*keys(): IterableIterator<K> {
|
|
70
70
|
for (const item of this) {
|
|
71
71
|
yield item[0];
|
|
72
72
|
}
|
|
@@ -82,7 +82,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
82
82
|
*
|
|
83
83
|
* The function returns an iterator that yields the values of a collection.
|
|
84
84
|
*/
|
|
85
|
-
*
|
|
85
|
+
*values(): IterableIterator<V> {
|
|
86
86
|
for (const item of this) {
|
|
87
87
|
yield item[1];
|
|
88
88
|
}
|
|
@@ -63,20 +63,21 @@ export class AVLTreeMultiMapNode<
|
|
|
63
63
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
64
64
|
*/
|
|
65
65
|
export class AVLTreeMultiMap<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
K = any,
|
|
67
|
+
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
69
|
+
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
70
|
+
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
71
|
+
K,
|
|
72
|
+
V,
|
|
73
|
+
R,
|
|
74
|
+
NODE,
|
|
75
|
+
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
76
|
+
>
|
|
76
77
|
>
|
|
77
|
-
>
|
|
78
78
|
extends AVLTree<K, V, R, NODE, TREE>
|
|
79
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
79
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
80
|
+
{
|
|
80
81
|
/**
|
|
81
82
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
82
83
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -66,14 +66,15 @@ export class AVLTreeNode<
|
|
|
66
66
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
67
67
|
*/
|
|
68
68
|
export class AVLTree<
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
>
|
|
69
|
+
K = any,
|
|
70
|
+
V = any,
|
|
71
|
+
R = BTNEntry<K, V>,
|
|
72
|
+
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
73
|
+
TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
|
|
74
|
+
>
|
|
75
75
|
extends BST<K, V, R, NODE, TREE>
|
|
76
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
76
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
77
|
+
{
|
|
77
78
|
/**
|
|
78
79
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
79
80
|
* entries, or raw elements.
|
|
@@ -503,7 +504,7 @@ export class AVLTree<
|
|
|
503
504
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
504
505
|
switch (
|
|
505
506
|
this._balanceFactor(A) // second O(1)
|
|
506
|
-
|
|
507
|
+
) {
|
|
507
508
|
case -2:
|
|
508
509
|
if (A && A.left) {
|
|
509
510
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -131,14 +131,15 @@ export class BinaryTreeNode<
|
|
|
131
131
|
*/
|
|
132
132
|
|
|
133
133
|
export class BinaryTree<
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
>
|
|
134
|
+
K = any,
|
|
135
|
+
V = any,
|
|
136
|
+
R = BTNEntry<K, V>,
|
|
137
|
+
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
138
|
+
TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>
|
|
139
|
+
>
|
|
140
140
|
extends IterableEntryBase<K, V | undefined>
|
|
141
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
141
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
142
|
+
{
|
|
142
143
|
iterationType: IterationType = 'ITERATIVE';
|
|
143
144
|
|
|
144
145
|
/**
|
|
@@ -1155,8 +1156,8 @@ export class BinaryTree<
|
|
|
1155
1156
|
if (!this.isRealNode(node.right) || last === node.right) {
|
|
1156
1157
|
node = stack.pop();
|
|
1157
1158
|
if (this.isRealNode(node)) {
|
|
1158
|
-
const leftMinHeight = this.isRealNode(node.left) ? depths.get(node.left) ?? -1 : -1;
|
|
1159
|
-
const rightMinHeight = this.isRealNode(node.right) ? depths.get(node.right) ?? -1 : -1;
|
|
1159
|
+
const leftMinHeight = this.isRealNode(node.left) ? (depths.get(node.left) ?? -1) : -1;
|
|
1160
|
+
const rightMinHeight = this.isRealNode(node.right) ? (depths.get(node.right) ?? -1) : -1;
|
|
1160
1161
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
1161
1162
|
last = node;
|
|
1162
1163
|
node = null;
|
|
@@ -1937,7 +1938,7 @@ export class BinaryTree<
|
|
|
1937
1938
|
* initially set to the root node of the tree.
|
|
1938
1939
|
* @returns an IterableIterator<[K, V | undefined]>.
|
|
1939
1940
|
*/
|
|
1940
|
-
protected*
|
|
1941
|
+
protected *_getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1941
1942
|
if (!node) return;
|
|
1942
1943
|
|
|
1943
1944
|
if (this.iterationType === 'ITERATIVE') {
|
|
@@ -94,14 +94,15 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
94
94
|
* 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
|
|
95
95
|
*/
|
|
96
96
|
export class BST<
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
>
|
|
97
|
+
K = any,
|
|
98
|
+
V = any,
|
|
99
|
+
R = BTNEntry<K, V>,
|
|
100
|
+
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
101
|
+
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
102
|
+
>
|
|
103
103
|
extends BinaryTree<K, V, R, NODE, TREE>
|
|
104
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
104
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
105
|
+
{
|
|
105
106
|
/**
|
|
106
107
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
107
108
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -786,8 +787,8 @@ export class BST<
|
|
|
786
787
|
if (!node.right || last === node.right) {
|
|
787
788
|
node = stack.pop();
|
|
788
789
|
if (node) {
|
|
789
|
-
const left = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
790
|
-
const right = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
790
|
+
const left = node.left ? (depths.get(node.left) ?? -1) : -1;
|
|
791
|
+
const right = node.right ? (depths.get(node.right) ?? -1) : -1;
|
|
791
792
|
if (Math.abs(left - right) > 1) return false;
|
|
792
793
|
depths.set(node, 1 + Math.max(left, right));
|
|
793
794
|
last = node;
|
|
@@ -53,14 +53,15 @@ export class RedBlackTreeNode<
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export class RedBlackTree<
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
>
|
|
56
|
+
K = any,
|
|
57
|
+
V = any,
|
|
58
|
+
R = BTNEntry<K, V>,
|
|
59
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
60
|
+
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
61
|
+
>
|
|
62
62
|
extends BST<K, V, R, NODE, TREE>
|
|
63
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
63
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
64
|
+
{
|
|
64
65
|
/**
|
|
65
66
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
66
67
|
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
@@ -63,14 +63,15 @@ export class TreeMultiMapNode<
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export class TreeMultiMap<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
>
|
|
66
|
+
K = any,
|
|
67
|
+
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
69
|
+
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
70
|
+
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
71
|
+
>
|
|
72
72
|
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
73
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
73
|
+
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
74
|
+
{
|
|
74
75
|
/**
|
|
75
76
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
76
77
|
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
@@ -61,13 +61,14 @@ export abstract class AbstractEdge<E = any> {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export abstract class AbstractGraph<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
>
|
|
64
|
+
V = any,
|
|
65
|
+
E = any,
|
|
66
|
+
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
|
+
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
+
>
|
|
69
69
|
extends IterableEntryBase<VertexKey, V | undefined>
|
|
70
|
-
implements IGraph<V, E, VO, EO>
|
|
70
|
+
implements IGraph<V, E, VO, EO>
|
|
71
|
+
{
|
|
71
72
|
constructor() {
|
|
72
73
|
super();
|
|
73
74
|
}
|
|
@@ -618,14 +619,14 @@ export abstract class AbstractGraph<
|
|
|
618
619
|
}
|
|
619
620
|
|
|
620
621
|
getMinDist &&
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
622
|
+
distMap.forEach((d, v) => {
|
|
623
|
+
if (v !== srcVertex) {
|
|
624
|
+
if (d < minDist) {
|
|
625
|
+
minDist = d;
|
|
626
|
+
if (genPaths) minDest = v;
|
|
627
|
+
}
|
|
626
628
|
}
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
+
});
|
|
629
630
|
|
|
630
631
|
genPaths && getPaths(minDest);
|
|
631
632
|
|
|
@@ -1069,7 +1070,7 @@ export abstract class AbstractGraph<
|
|
|
1069
1070
|
return mapped;
|
|
1070
1071
|
}
|
|
1071
1072
|
|
|
1072
|
-
protected*
|
|
1073
|
+
protected *_getIterator(): IterableIterator<[VertexKey, V | undefined]> {
|
|
1073
1074
|
for (const vertex of this._vertexMap.values()) {
|
|
1074
1075
|
yield [vertex.key, vertex.value];
|
|
1075
1076
|
}
|
|
@@ -46,13 +46,14 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export class DirectedGraph<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
>
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
54
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
55
|
+
implements IGraph<V, E, VO, EO>
|
|
56
|
+
{
|
|
56
57
|
/**
|
|
57
58
|
* The constructor function initializes an instance of a class.
|
|
58
59
|
*/
|
|
@@ -43,13 +43,14 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class UndirectedGraph<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
>
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
-
implements IGraph<V, E, VO, EO>
|
|
52
|
+
implements IGraph<V, E, VO, EO>
|
|
53
|
+
{
|
|
53
54
|
/**
|
|
54
55
|
* The constructor initializes a new Map object to store edgeMap.
|
|
55
56
|
*/
|
|
@@ -322,7 +322,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
322
322
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
323
323
|
* object map.
|
|
324
324
|
*/
|
|
325
|
-
protected*
|
|
325
|
+
protected *_getIterator(): IterableIterator<[K, V]> {
|
|
326
326
|
for (const node of Object.values(this.store)) {
|
|
327
327
|
yield [node.key, node.value] as [K, V];
|
|
328
328
|
}
|
|
@@ -537,7 +537,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
537
537
|
/**
|
|
538
538
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
539
539
|
*/
|
|
540
|
-
*
|
|
540
|
+
*begin() {
|
|
541
541
|
let node = this.head;
|
|
542
542
|
while (node !== this._sentinel) {
|
|
543
543
|
yield [node.key, node.value];
|
|
@@ -549,7 +549,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
549
549
|
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
550
550
|
* key and value.
|
|
551
551
|
*/
|
|
552
|
-
*
|
|
552
|
+
*reverseBegin() {
|
|
553
553
|
let node = this.tail;
|
|
554
554
|
while (node !== this._sentinel) {
|
|
555
555
|
yield [node.key, node.value];
|
|
@@ -942,7 +942,7 @@ export class LinkedHashMap<K = any, V = any, R = [K, V]> extends IterableEntryBa
|
|
|
942
942
|
*
|
|
943
943
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
944
944
|
*/
|
|
945
|
-
protected*
|
|
945
|
+
protected *_getIterator() {
|
|
946
946
|
let node = this.head;
|
|
947
947
|
while (node !== this._sentinel) {
|
|
948
948
|
yield [node.key, node.value] as [K, V];
|
|
@@ -367,7 +367,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
367
367
|
/**
|
|
368
368
|
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
369
369
|
*/
|
|
370
|
-
protected*
|
|
370
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
371
371
|
for (const element of this.elements) {
|
|
372
372
|
yield element;
|
|
373
373
|
}
|
|
@@ -803,7 +803,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
803
803
|
/**
|
|
804
804
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
805
805
|
*/
|
|
806
|
-
protected*
|
|
806
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
807
807
|
let current = this.head;
|
|
808
808
|
|
|
809
809
|
while (current) {
|
|
@@ -751,7 +751,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
751
751
|
/**
|
|
752
752
|
* The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
|
|
753
753
|
*/
|
|
754
|
-
protected*
|
|
754
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
755
755
|
let current = this.head;
|
|
756
756
|
|
|
757
757
|
while (current) {
|
|
@@ -344,7 +344,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
344
344
|
/**
|
|
345
345
|
* The below function is a generator that yields elements from a collection one by one.
|
|
346
346
|
*/
|
|
347
|
-
*
|
|
347
|
+
*begin(): Generator<E> {
|
|
348
348
|
let index = 0;
|
|
349
349
|
while (index < this.size) {
|
|
350
350
|
yield this.at(index);
|
|
@@ -356,7 +356,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
356
356
|
* The function `reverseBegin()` is a generator that yields elements in reverse order starting from
|
|
357
357
|
* the last element.
|
|
358
358
|
*/
|
|
359
|
-
*
|
|
359
|
+
*reverseBegin(): Generator<E> {
|
|
360
360
|
let index = this.size - 1;
|
|
361
361
|
while (index >= 0) {
|
|
362
362
|
yield this.at(index);
|
|
@@ -848,7 +848,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
|
|
|
848
848
|
* The above function is an implementation of the iterator protocol in TypeScript, allowing the
|
|
849
849
|
* object to be iterated over using a for...of loop.
|
|
850
850
|
*/
|
|
851
|
-
protected*
|
|
851
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
852
852
|
for (let i = 0; i < this.size; ++i) {
|
|
853
853
|
yield this.at(i);
|
|
854
854
|
}
|
|
@@ -19,6 +19,12 @@ import { SinglyLinkedList } from '../linked-list';
|
|
|
19
19
|
export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
|
|
20
20
|
constructor(elements: Iterable<E> | Iterable<R> = [], options?: QueueOptions<E, R>) {
|
|
21
21
|
super(options);
|
|
22
|
+
|
|
23
|
+
if (options) {
|
|
24
|
+
const { autoCompactRatio = 0.5 } = options;
|
|
25
|
+
this._autoCompactRatio = autoCompactRatio;
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
if (elements) {
|
|
23
29
|
for (const el of elements) {
|
|
24
30
|
if (this.toElementFn) this.push(this.toElementFn(el as R));
|
|
@@ -89,6 +95,25 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
89
95
|
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
90
96
|
}
|
|
91
97
|
|
|
98
|
+
_autoCompactRatio: number = 0.5;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* This function returns the value of the autoCompactRatio property.
|
|
102
|
+
* @returns The `autoCompactRatio` property of the object, which is a number.
|
|
103
|
+
*/
|
|
104
|
+
get autoCompactRatio(): number {
|
|
105
|
+
return this._autoCompactRatio;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The above function sets the autoCompactRatio property to a specified number in TypeScript.
|
|
110
|
+
* @param {number} v - The parameter `v` represents the value that will be assigned to the
|
|
111
|
+
* `_autoCompactRatio` property.
|
|
112
|
+
*/
|
|
113
|
+
set autoCompactRatio(v: number) {
|
|
114
|
+
this._autoCompactRatio = v;
|
|
115
|
+
}
|
|
116
|
+
|
|
92
117
|
/**
|
|
93
118
|
* Time Complexity: O(n)
|
|
94
119
|
* Space Complexity: O(n)
|
|
@@ -145,12 +170,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
145
170
|
const first = this.first;
|
|
146
171
|
this._offset += 1;
|
|
147
172
|
|
|
148
|
-
if (this.offset
|
|
149
|
-
|
|
150
|
-
// only delete dequeued elements when reaching half size
|
|
151
|
-
// to decrease latency of shifting elements.
|
|
152
|
-
this._elements = this.elements.slice(this.offset);
|
|
153
|
-
this._offset = 0;
|
|
173
|
+
if (this.offset / this.elements.length > this.autoCompactRatio) this.compact();
|
|
154
174
|
return first;
|
|
155
175
|
}
|
|
156
176
|
|
|
@@ -237,6 +257,17 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
237
257
|
this._offset = 0;
|
|
238
258
|
}
|
|
239
259
|
|
|
260
|
+
/**
|
|
261
|
+
* The `compact` function in TypeScript slices the elements array based on the offset and resets the
|
|
262
|
+
* offset to zero.
|
|
263
|
+
* @returns The `compact()` method is returning a boolean value of `true`.
|
|
264
|
+
*/
|
|
265
|
+
compact(): boolean {
|
|
266
|
+
this._elements = this.elements.slice(this.offset);
|
|
267
|
+
this._offset = 0;
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
|
|
240
271
|
/**
|
|
241
272
|
* Time Complexity: O(n)
|
|
242
273
|
* Space Complexity: O(n)
|
|
@@ -317,7 +348,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
|
|
|
317
348
|
*
|
|
318
349
|
* The function `_getIterator` returns an iterable iterator for the elements in the class.
|
|
319
350
|
*/
|
|
320
|
-
protected*
|
|
351
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
321
352
|
for (const item of this.elements.slice(this.offset)) {
|
|
322
353
|
yield item;
|
|
323
354
|
}
|
|
@@ -274,7 +274,7 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
|
|
|
274
274
|
* Custom iterator for the Stack class.
|
|
275
275
|
* @returns An iterator object.
|
|
276
276
|
*/
|
|
277
|
-
protected*
|
|
277
|
+
protected *_getIterator(): IterableIterator<E> {
|
|
278
278
|
for (let i = 0; i < this.elements.length; i++) {
|
|
279
279
|
yield this.elements[i];
|
|
280
280
|
}
|
|
@@ -560,7 +560,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
560
560
|
* The function `_getIterator` returns an iterable iterator that performs a depth-first search on a
|
|
561
561
|
* trie data structure and yields all the paths to the end nodes.
|
|
562
562
|
*/
|
|
563
|
-
protected*
|
|
563
|
+
protected *_getIterator(): IterableIterator<string> {
|
|
564
564
|
function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
|
|
565
565
|
if (node.isEnd) {
|
|
566
566
|
yield path;
|
|
@@ -2,12 +2,12 @@ export type VertexKey = string | number;
|
|
|
2
2
|
|
|
3
3
|
export type DijkstraResult<V> =
|
|
4
4
|
| {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
5
|
+
distMap: Map<V, number>;
|
|
6
|
+
distPaths?: Map<V, V[]>;
|
|
7
|
+
preMap: Map<V, V | undefined>;
|
|
8
|
+
seen: Set<V>;
|
|
9
|
+
paths: V[][];
|
|
10
|
+
minDist: number;
|
|
11
|
+
minPath: V[];
|
|
12
|
+
}
|
|
13
13
|
| undefined;
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -13,9 +13,9 @@ export type Comparable =
|
|
|
13
13
|
| bigint
|
|
14
14
|
| boolean
|
|
15
15
|
| ({ [key in string]: any } & {
|
|
16
|
-
|
|
17
|
-
})
|
|
16
|
+
valueOf(): Comparable;
|
|
17
|
+
})
|
|
18
18
|
| ({ [key in string]: any } & {
|
|
19
|
-
|
|
20
|
-
})
|
|
19
|
+
toString(): Comparable;
|
|
20
|
+
})
|
|
21
21
|
| (() => Comparable);
|