deque-typed 1.54.2 → 2.0.0
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/README.md +91 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
- package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
- package/dist/data-structures/binary-tree/avl-tree.js +76 -8
- package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/data-structures/binary-tree/binary-tree.js +244 -149
- package/dist/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/data-structures/binary-tree/bst.js +89 -133
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
- package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
- package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +84 -40
- package/dist/data-structures/queue/queue.js +134 -50
- package/dist/data-structures/stack/stack.d.ts +3 -11
- package/dist/data-structures/stack/stack.js +0 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/utils/utils.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
- package/src/data-structures/binary-tree/avl-tree.ts +99 -29
- package/src/data-structures/binary-tree/binary-tree.ts +474 -257
- package/src/data-structures/binary-tree/bst.ts +150 -152
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/src/utils/utils.ts +2 -2
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BTNOptKeyOrNull,
|
|
8
|
+
import type { BTNOptKeyOrNull, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
|
|
12
12
|
export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
|
|
13
|
+
override parent?: TreeMultiMapNode<K, V> = undefined;
|
|
14
|
+
|
|
13
15
|
/**
|
|
14
16
|
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
17
|
* type V.
|
|
@@ -19,32 +21,30 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
19
21
|
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of
|
|
20
22
|
* type `V`.
|
|
21
23
|
*/
|
|
22
|
-
constructor(key: K, value
|
|
24
|
+
constructor(key: K, value?: V[]) {
|
|
23
25
|
super(key, value);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
override
|
|
27
|
-
|
|
28
|
-
override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
|
|
28
|
+
override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
29
29
|
|
|
30
|
-
override get left():
|
|
30
|
+
override get left(): TreeMultiMapNode<K, V> | null | undefined {
|
|
31
31
|
return this._left;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override set left(v:
|
|
34
|
+
override set left(v: TreeMultiMapNode<K, V> | null | undefined) {
|
|
35
35
|
if (v) {
|
|
36
36
|
v.parent = this;
|
|
37
37
|
}
|
|
38
38
|
this._left = v;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
override _right?:
|
|
41
|
+
override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
|
|
42
42
|
|
|
43
|
-
override get right():
|
|
43
|
+
override get right(): TreeMultiMapNode<K, V> | null | undefined {
|
|
44
44
|
return this._right;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
override set right(v:
|
|
47
|
+
override set right(v: TreeMultiMapNode<K, V> | null | undefined) {
|
|
48
48
|
if (v) {
|
|
49
49
|
v.parent = this;
|
|
50
50
|
}
|
|
@@ -55,11 +55,169 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
|
|
|
55
55
|
/**
|
|
56
56
|
*
|
|
57
57
|
* @example
|
|
58
|
-
* //
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
58
|
+
* // players ranked by score with their equipment
|
|
59
|
+
* type Equipment = {
|
|
60
|
+
* name: string; // Equipment name
|
|
61
|
+
* quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
62
|
+
* level: number;
|
|
63
|
+
* };
|
|
64
|
+
*
|
|
65
|
+
* type Player = {
|
|
66
|
+
* name: string;
|
|
67
|
+
* score: number;
|
|
68
|
+
* equipments: Equipment[];
|
|
69
|
+
* };
|
|
70
|
+
*
|
|
71
|
+
* // Mock player data with their scores and equipment
|
|
72
|
+
* const players: Player[] = [
|
|
73
|
+
* {
|
|
74
|
+
* name: 'DragonSlayer',
|
|
75
|
+
* score: 8750,
|
|
76
|
+
* equipments: [
|
|
77
|
+
* { name: 'AWM', quality: 'legendary', level: 85 },
|
|
78
|
+
* { name: 'Level 3 Helmet', quality: 'epic', level: 80 },
|
|
79
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 75 },
|
|
80
|
+
* { name: 'Compensator', quality: 'epic', level: 78 },
|
|
81
|
+
* { name: 'Vertical Grip', quality: 'rare', level: 72 }
|
|
82
|
+
* ]
|
|
83
|
+
* },
|
|
84
|
+
* {
|
|
85
|
+
* name: 'ShadowNinja',
|
|
86
|
+
* score: 7200,
|
|
87
|
+
* equipments: [
|
|
88
|
+
* { name: 'M416', quality: 'epic', level: 75 },
|
|
89
|
+
* { name: 'Ghillie Suit', quality: 'rare', level: 70 },
|
|
90
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 65 },
|
|
91
|
+
* { name: 'Extended QuickDraw Mag', quality: 'rare', level: 68 }
|
|
92
|
+
* ]
|
|
93
|
+
* },
|
|
94
|
+
* {
|
|
95
|
+
* name: 'RuneMaster',
|
|
96
|
+
* score: 9100,
|
|
97
|
+
* equipments: [
|
|
98
|
+
* { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
99
|
+
* { name: 'Level 3 Vest', quality: 'legendary', level: 85 },
|
|
100
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
101
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
102
|
+
* { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
103
|
+
* ]
|
|
104
|
+
* },
|
|
105
|
+
* {
|
|
106
|
+
* name: 'BattleKing',
|
|
107
|
+
* score: 8500,
|
|
108
|
+
* equipments: [
|
|
109
|
+
* { name: 'AUG', quality: 'epic', level: 82 },
|
|
110
|
+
* { name: 'Red Dot Sight', quality: 'rare', level: 75 },
|
|
111
|
+
* { name: 'Extended Mag', quality: 'common', level: 70 },
|
|
112
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 76 }
|
|
113
|
+
* ]
|
|
114
|
+
* },
|
|
115
|
+
* {
|
|
116
|
+
* name: 'SniperElite',
|
|
117
|
+
* score: 7800,
|
|
118
|
+
* equipments: [
|
|
119
|
+
* { name: 'M24', quality: 'legendary', level: 88 },
|
|
120
|
+
* { name: 'Compensator', quality: 'epic', level: 80 },
|
|
121
|
+
* { name: 'Scope 8x', quality: 'legendary', level: 85 },
|
|
122
|
+
* { name: 'Level 2 Helmet', quality: 'rare', level: 75 }
|
|
123
|
+
* ]
|
|
124
|
+
* },
|
|
125
|
+
* {
|
|
126
|
+
* name: 'RushMaster',
|
|
127
|
+
* score: 7500,
|
|
128
|
+
* equipments: [
|
|
129
|
+
* { name: 'Vector', quality: 'rare', level: 72 },
|
|
130
|
+
* { name: 'Level 2 Helmet', quality: 'common', level: 65 },
|
|
131
|
+
* { name: 'Quickdraw Mag', quality: 'common', level: 60 },
|
|
132
|
+
* { name: 'Laser Sight', quality: 'rare', level: 68 }
|
|
133
|
+
* ]
|
|
134
|
+
* },
|
|
135
|
+
* {
|
|
136
|
+
* name: 'GhostWarrior',
|
|
137
|
+
* score: 8200,
|
|
138
|
+
* equipments: [
|
|
139
|
+
* { name: 'SCAR-L', quality: 'epic', level: 78 },
|
|
140
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 70 },
|
|
141
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 75 },
|
|
142
|
+
* { name: 'Suppressor', quality: 'rare', level: 72 },
|
|
143
|
+
* { name: 'Vertical Grip', quality: 'common', level: 65 }
|
|
144
|
+
* ]
|
|
145
|
+
* },
|
|
146
|
+
* {
|
|
147
|
+
* name: 'DeathDealer',
|
|
148
|
+
* score: 7300,
|
|
149
|
+
* equipments: [
|
|
150
|
+
* { name: 'SKS', quality: 'epic', level: 76 },
|
|
151
|
+
* { name: 'Holographic Sight', quality: 'rare', level: 68 },
|
|
152
|
+
* { name: 'Extended Mag', quality: 'common', level: 65 }
|
|
153
|
+
* ]
|
|
154
|
+
* },
|
|
155
|
+
* {
|
|
156
|
+
* name: 'StormRider',
|
|
157
|
+
* score: 8900,
|
|
158
|
+
* equipments: [
|
|
159
|
+
* { name: 'MK14', quality: 'legendary', level: 92 },
|
|
160
|
+
* { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
161
|
+
* { name: 'Scope 8x', quality: 'epic', level: 80 },
|
|
162
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
163
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 75 }
|
|
164
|
+
* ]
|
|
165
|
+
* },
|
|
166
|
+
* {
|
|
167
|
+
* name: 'CombatLegend',
|
|
168
|
+
* score: 7600,
|
|
169
|
+
* equipments: [
|
|
170
|
+
* { name: 'UMP45', quality: 'rare', level: 74 },
|
|
171
|
+
* { name: 'Level 2 Vest', quality: 'common', level: 67 },
|
|
172
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 62 },
|
|
173
|
+
* { name: 'Extended Mag', quality: 'rare', level: 70 }
|
|
174
|
+
* ]
|
|
175
|
+
* }
|
|
176
|
+
* ];
|
|
177
|
+
*
|
|
178
|
+
* // Create a TreeMultiMap for player rankings
|
|
179
|
+
* const playerRankings = new TreeMultiMap<number, Equipment, Player>(players, {
|
|
180
|
+
* toEntryFn: ({ score, equipments }) => [score, equipments],
|
|
181
|
+
* isMapMode: false
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node));
|
|
185
|
+
* console.log(topPlayersEquipments); // [
|
|
186
|
+
* // [
|
|
187
|
+
* // {
|
|
188
|
+
* // name: 'MK14',
|
|
189
|
+
* // quality: 'legendary',
|
|
190
|
+
* // level: 92
|
|
191
|
+
* // },
|
|
192
|
+
* // { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
193
|
+
* // {
|
|
194
|
+
* // name: 'Scope 8x',
|
|
195
|
+
* // quality: 'epic',
|
|
196
|
+
* // level: 80
|
|
197
|
+
* // },
|
|
198
|
+
* // { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
199
|
+
* // {
|
|
200
|
+
* // name: 'Tactical Stock',
|
|
201
|
+
* // quality: 'rare',
|
|
202
|
+
* // level: 75
|
|
203
|
+
* // }
|
|
204
|
+
* // ],
|
|
205
|
+
* // [
|
|
206
|
+
* // { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
207
|
+
* // {
|
|
208
|
+
* // name: 'Level 3 Vest',
|
|
209
|
+
* // quality: 'legendary',
|
|
210
|
+
* // level: 85
|
|
211
|
+
* // },
|
|
212
|
+
* // { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
213
|
+
* // {
|
|
214
|
+
* // name: 'Suppressor',
|
|
215
|
+
* // quality: 'legendary',
|
|
216
|
+
* // level: 88
|
|
217
|
+
* // },
|
|
218
|
+
* // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
219
|
+
* // ]
|
|
220
|
+
* // ]
|
|
63
221
|
*/
|
|
64
222
|
export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
65
223
|
extends RedBlackTree<K, V[], R, MK, MV[], MR>
|
|
@@ -77,10 +235,12 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
77
235
|
* additional options for configuring the TreeMultiMap instance.
|
|
78
236
|
*/
|
|
79
237
|
constructor(
|
|
80
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
238
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
239
|
+
K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R
|
|
240
|
+
> = [],
|
|
81
241
|
options?: TreeMultiMapOptions<K, V[], R>
|
|
82
242
|
) {
|
|
83
|
-
super([], { ...options
|
|
243
|
+
super([], { ...options });
|
|
84
244
|
if (keysNodesEntriesOrRaws) {
|
|
85
245
|
this.addMany(keysNodesEntriesOrRaws);
|
|
86
246
|
}
|
|
@@ -105,6 +265,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
105
265
|
specifyComparable: this._specifyComparable,
|
|
106
266
|
toEntryFn: this._toEntryFn,
|
|
107
267
|
isReverse: this._isReverse,
|
|
268
|
+
isMapMode: this._isMapMode,
|
|
108
269
|
...options
|
|
109
270
|
});
|
|
110
271
|
}
|
|
@@ -113,18 +274,23 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
113
274
|
* Time Complexity: O(1)
|
|
114
275
|
* Space Complexity: O(1)
|
|
115
276
|
*
|
|
116
|
-
* The function `createNode` overrides the
|
|
117
|
-
*
|
|
118
|
-
* @param {K} key - The `key` parameter
|
|
119
|
-
*
|
|
120
|
-
* @
|
|
121
|
-
*
|
|
277
|
+
* The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
|
|
278
|
+
* and value array.
|
|
279
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
280
|
+
* `TreeMultiMap`.
|
|
281
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
282
|
+
* values associated with a specific key in the TreeMultiMap data structure.
|
|
283
|
+
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
|
|
284
|
+
* value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
|
|
285
|
+
* value is used.
|
|
122
286
|
*/
|
|
123
|
-
override createNode(key: K): TreeMultiMapNode<K, V> {
|
|
124
|
-
return new TreeMultiMapNode<K, V>(key, []);
|
|
287
|
+
override createNode(key: K, value: V[] = []): TreeMultiMapNode<K, V> {
|
|
288
|
+
return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
|
|
125
289
|
}
|
|
126
290
|
|
|
127
|
-
override add(
|
|
291
|
+
override add(
|
|
292
|
+
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
293
|
+
): boolean;
|
|
128
294
|
|
|
129
295
|
override add(key: K, value: V): boolean;
|
|
130
296
|
|
|
@@ -132,44 +298,57 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
132
298
|
* Time Complexity: O(log n)
|
|
133
299
|
* Space Complexity: O(log n)
|
|
134
300
|
*
|
|
135
|
-
* The function
|
|
136
|
-
*
|
|
137
|
-
* @param
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* @param {V} [
|
|
141
|
-
* you want to add to the TreeMultiMap.
|
|
142
|
-
* be added to the existing list of values associated with that key. If the key is not present,
|
|
301
|
+
* The function overrides the add method to handle different types of input for a TreeMultiMap data
|
|
302
|
+
* structure.
|
|
303
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
304
|
+
* be added to the TreeMultiMap. It can be of type `K`, which is the key type of the TreeMultiMap, or
|
|
305
|
+
* it can be a TreeMultiMapNode containing the key and its
|
|
306
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
307
|
+
* that you want to add to the TreeMultiMap. It can contain one or more values of type `V`.
|
|
143
308
|
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
144
309
|
* successful or not.
|
|
145
310
|
*/
|
|
146
|
-
override add(
|
|
311
|
+
override add(
|
|
312
|
+
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
313
|
+
value?: V
|
|
314
|
+
): boolean {
|
|
147
315
|
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
148
316
|
|
|
149
317
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
150
318
|
if (key === undefined || key === null) return false;
|
|
151
319
|
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const existingNode = this.getNode(key);
|
|
159
|
-
if (this.isRealNode(existingNode)) {
|
|
160
|
-
if (existingValues === undefined) {
|
|
161
|
-
super.add(key, values);
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
if (values !== undefined) {
|
|
320
|
+
const _addToValues = () => {
|
|
321
|
+
const existingValues = this.get(key);
|
|
322
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
165
323
|
for (const value of values) existingValues.push(value);
|
|
166
324
|
return true;
|
|
325
|
+
}
|
|
326
|
+
return false;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const _addByNode = () => {
|
|
330
|
+
const existingNode = this.getNode(key);
|
|
331
|
+
if (this.isRealNode(existingNode)) {
|
|
332
|
+
const existingValues = this.get(existingNode);
|
|
333
|
+
if (existingValues === undefined) {
|
|
334
|
+
super.add(key, values);
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
if (values !== undefined) {
|
|
338
|
+
for (const value of values) existingValues.push(value);
|
|
339
|
+
return true;
|
|
340
|
+
} else {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
167
343
|
} else {
|
|
168
|
-
return
|
|
344
|
+
return super.add(key, values);
|
|
169
345
|
}
|
|
170
|
-
}
|
|
171
|
-
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
if (this._isMapMode) {
|
|
349
|
+
return _addByNode() || _addToValues();
|
|
172
350
|
}
|
|
351
|
+
return _addToValues() || _addByNode();
|
|
173
352
|
};
|
|
174
353
|
|
|
175
354
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
@@ -186,7 +365,7 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
186
365
|
*
|
|
187
366
|
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
|
|
188
367
|
* and deletes the entire node if no values are left for that key.
|
|
189
|
-
* @param {
|
|
368
|
+
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
190
369
|
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
|
|
191
370
|
* array of values, or just a key itself.
|
|
192
371
|
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
@@ -196,7 +375,10 @@ export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR =
|
|
|
196
375
|
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
|
|
197
376
|
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
|
|
198
377
|
*/
|
|
199
|
-
deleteValue(
|
|
378
|
+
deleteValue(
|
|
379
|
+
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
380
|
+
value: V
|
|
381
|
+
): boolean {
|
|
200
382
|
const values = this.get(keyNodeOrEntry);
|
|
201
383
|
if (Array.isArray(values)) {
|
|
202
384
|
const index = values.indexOf(value);
|
|
@@ -356,8 +356,8 @@ export abstract class AbstractGraph<
|
|
|
356
356
|
const queue = new Queue<VO>([vertex1]);
|
|
357
357
|
visited.set(vertex1, true);
|
|
358
358
|
let cost = 0;
|
|
359
|
-
while (queue.
|
|
360
|
-
for (let i = 0; i < queue.
|
|
359
|
+
while (queue.length > 0) {
|
|
360
|
+
for (let i = 0; i < queue.length; i++) {
|
|
361
361
|
const cur = queue.shift();
|
|
362
362
|
if (cur === vertex2) {
|
|
363
363
|
return cost;
|
|
@@ -185,7 +185,7 @@ import { IterableElementBase } from '../base';
|
|
|
185
185
|
* ]);
|
|
186
186
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
187
187
|
*/
|
|
188
|
-
export class Heap<E = any, R = any> extends IterableElementBase<E, R
|
|
188
|
+
export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
189
189
|
/**
|
|
190
190
|
* The constructor initializes a heap data structure with optional elements and options.
|
|
191
191
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
@@ -416,17 +416,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
416
416
|
return result;
|
|
417
417
|
}
|
|
418
418
|
|
|
419
|
-
/**
|
|
420
|
-
* Time Complexity: O(n)
|
|
421
|
-
* Space Complexity: O(n)
|
|
422
|
-
*
|
|
423
|
-
* Convert the heap to an array.
|
|
424
|
-
* @returns An array containing the elements of the heap.
|
|
425
|
-
*/
|
|
426
|
-
toArray(): E[] {
|
|
427
|
-
return [...this.elements];
|
|
428
|
-
}
|
|
429
|
-
|
|
430
419
|
/**
|
|
431
420
|
* Time Complexity: O(n)
|
|
432
421
|
* Space Complexity: O(n)
|
|
@@ -483,7 +472,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
483
472
|
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
484
473
|
* the filter condition specified by the `callback` function.
|
|
485
474
|
*/
|
|
486
|
-
filter(callback: ElementCallback<E, R, boolean
|
|
475
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): Heap<E, R> {
|
|
487
476
|
const filteredList = new Heap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
488
477
|
let index = 0;
|
|
489
478
|
for (const current of this) {
|
|
@@ -517,7 +506,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
517
506
|
* @returns a new instance of the `Heap` class with the mapped elements.
|
|
518
507
|
*/
|
|
519
508
|
map<EM, RM>(
|
|
520
|
-
callback: ElementCallback<E, R, EM
|
|
509
|
+
callback: ElementCallback<E, R, EM>,
|
|
521
510
|
comparator: Comparator<EM>,
|
|
522
511
|
toElementFn?: (rawElement: RM) => EM,
|
|
523
512
|
thisArg?: any
|
|
@@ -61,7 +61,7 @@ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
61
61
|
* @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
|
|
62
62
|
* the filter condition specified by the `callback` function.
|
|
63
63
|
*/
|
|
64
|
-
override filter(callback: ElementCallback<E, R, boolean
|
|
64
|
+
override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MaxHeap<E, R> {
|
|
65
65
|
const filteredList = new MaxHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
66
66
|
let index = 0;
|
|
67
67
|
for (const current of this) {
|
|
@@ -95,7 +95,7 @@ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
95
95
|
* @returns a new instance of the `MaxHeap` class with the mapped elements.
|
|
96
96
|
*/
|
|
97
97
|
override map<EM, RM>(
|
|
98
|
-
callback: ElementCallback<E, R, EM
|
|
98
|
+
callback: ElementCallback<E, R, EM>,
|
|
99
99
|
comparator: Comparator<EM>,
|
|
100
100
|
toElementFn?: (rawElement: RM) => EM,
|
|
101
101
|
thisArg?: any
|
|
@@ -49,7 +49,7 @@ export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
49
49
|
* @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
|
|
50
50
|
* the filter condition specified by the `callback` function.
|
|
51
51
|
*/
|
|
52
|
-
override filter(callback: ElementCallback<E, R, boolean
|
|
52
|
+
override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MinHeap<E, R> {
|
|
53
53
|
const filteredList = new MinHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
54
54
|
let index = 0;
|
|
55
55
|
for (const current of this) {
|
|
@@ -83,7 +83,7 @@ export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
83
83
|
* @returns a new instance of the `MinHeap` class with the mapped elements.
|
|
84
84
|
*/
|
|
85
85
|
override map<EM, RM>(
|
|
86
|
-
callback: ElementCallback<E, R, EM
|
|
86
|
+
callback: ElementCallback<E, R, EM>,
|
|
87
87
|
comparator: Comparator<EM>,
|
|
88
88
|
toElementFn?: (rawElement: RM) => EM,
|
|
89
89
|
thisArg?: any
|