linked-list-typed 1.54.3 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- 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-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- 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 +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -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 +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -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/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/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-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- 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 +307 -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 +196 -63
- package/src/data-structures/stack/stack.ts +124 -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/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
|
@@ -41,11 +41,169 @@ exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
|
41
41
|
/**
|
|
42
42
|
*
|
|
43
43
|
* @example
|
|
44
|
-
* //
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
44
|
+
* // players ranked by score with their equipment
|
|
45
|
+
* type Equipment = {
|
|
46
|
+
* name: string; // Equipment name
|
|
47
|
+
* quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
48
|
+
* level: number;
|
|
49
|
+
* };
|
|
50
|
+
*
|
|
51
|
+
* type Player = {
|
|
52
|
+
* name: string;
|
|
53
|
+
* score: number;
|
|
54
|
+
* equipments: Equipment[];
|
|
55
|
+
* };
|
|
56
|
+
*
|
|
57
|
+
* // Mock player data with their scores and equipment
|
|
58
|
+
* const players: Player[] = [
|
|
59
|
+
* {
|
|
60
|
+
* name: 'DragonSlayer',
|
|
61
|
+
* score: 8750,
|
|
62
|
+
* equipments: [
|
|
63
|
+
* { name: 'AWM', quality: 'legendary', level: 85 },
|
|
64
|
+
* { name: 'Level 3 Helmet', quality: 'epic', level: 80 },
|
|
65
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 75 },
|
|
66
|
+
* { name: 'Compensator', quality: 'epic', level: 78 },
|
|
67
|
+
* { name: 'Vertical Grip', quality: 'rare', level: 72 }
|
|
68
|
+
* ]
|
|
69
|
+
* },
|
|
70
|
+
* {
|
|
71
|
+
* name: 'ShadowNinja',
|
|
72
|
+
* score: 7200,
|
|
73
|
+
* equipments: [
|
|
74
|
+
* { name: 'M416', quality: 'epic', level: 75 },
|
|
75
|
+
* { name: 'Ghillie Suit', quality: 'rare', level: 70 },
|
|
76
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 65 },
|
|
77
|
+
* { name: 'Extended QuickDraw Mag', quality: 'rare', level: 68 }
|
|
78
|
+
* ]
|
|
79
|
+
* },
|
|
80
|
+
* {
|
|
81
|
+
* name: 'RuneMaster',
|
|
82
|
+
* score: 9100,
|
|
83
|
+
* equipments: [
|
|
84
|
+
* { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
85
|
+
* { name: 'Level 3 Vest', quality: 'legendary', level: 85 },
|
|
86
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
87
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
88
|
+
* { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
89
|
+
* ]
|
|
90
|
+
* },
|
|
91
|
+
* {
|
|
92
|
+
* name: 'BattleKing',
|
|
93
|
+
* score: 8500,
|
|
94
|
+
* equipments: [
|
|
95
|
+
* { name: 'AUG', quality: 'epic', level: 82 },
|
|
96
|
+
* { name: 'Red Dot Sight', quality: 'rare', level: 75 },
|
|
97
|
+
* { name: 'Extended Mag', quality: 'common', level: 70 },
|
|
98
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 76 }
|
|
99
|
+
* ]
|
|
100
|
+
* },
|
|
101
|
+
* {
|
|
102
|
+
* name: 'SniperElite',
|
|
103
|
+
* score: 7800,
|
|
104
|
+
* equipments: [
|
|
105
|
+
* { name: 'M24', quality: 'legendary', level: 88 },
|
|
106
|
+
* { name: 'Compensator', quality: 'epic', level: 80 },
|
|
107
|
+
* { name: 'Scope 8x', quality: 'legendary', level: 85 },
|
|
108
|
+
* { name: 'Level 2 Helmet', quality: 'rare', level: 75 }
|
|
109
|
+
* ]
|
|
110
|
+
* },
|
|
111
|
+
* {
|
|
112
|
+
* name: 'RushMaster',
|
|
113
|
+
* score: 7500,
|
|
114
|
+
* equipments: [
|
|
115
|
+
* { name: 'Vector', quality: 'rare', level: 72 },
|
|
116
|
+
* { name: 'Level 2 Helmet', quality: 'common', level: 65 },
|
|
117
|
+
* { name: 'Quickdraw Mag', quality: 'common', level: 60 },
|
|
118
|
+
* { name: 'Laser Sight', quality: 'rare', level: 68 }
|
|
119
|
+
* ]
|
|
120
|
+
* },
|
|
121
|
+
* {
|
|
122
|
+
* name: 'GhostWarrior',
|
|
123
|
+
* score: 8200,
|
|
124
|
+
* equipments: [
|
|
125
|
+
* { name: 'SCAR-L', quality: 'epic', level: 78 },
|
|
126
|
+
* { name: 'Extended Quickdraw Mag', quality: 'rare', level: 70 },
|
|
127
|
+
* { name: 'Holographic Sight', quality: 'epic', level: 75 },
|
|
128
|
+
* { name: 'Suppressor', quality: 'rare', level: 72 },
|
|
129
|
+
* { name: 'Vertical Grip', quality: 'common', level: 65 }
|
|
130
|
+
* ]
|
|
131
|
+
* },
|
|
132
|
+
* {
|
|
133
|
+
* name: 'DeathDealer',
|
|
134
|
+
* score: 7300,
|
|
135
|
+
* equipments: [
|
|
136
|
+
* { name: 'SKS', quality: 'epic', level: 76 },
|
|
137
|
+
* { name: 'Holographic Sight', quality: 'rare', level: 68 },
|
|
138
|
+
* { name: 'Extended Mag', quality: 'common', level: 65 }
|
|
139
|
+
* ]
|
|
140
|
+
* },
|
|
141
|
+
* {
|
|
142
|
+
* name: 'StormRider',
|
|
143
|
+
* score: 8900,
|
|
144
|
+
* equipments: [
|
|
145
|
+
* { name: 'MK14', quality: 'legendary', level: 92 },
|
|
146
|
+
* { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
147
|
+
* { name: 'Scope 8x', quality: 'epic', level: 80 },
|
|
148
|
+
* { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
149
|
+
* { name: 'Tactical Stock', quality: 'rare', level: 75 }
|
|
150
|
+
* ]
|
|
151
|
+
* },
|
|
152
|
+
* {
|
|
153
|
+
* name: 'CombatLegend',
|
|
154
|
+
* score: 7600,
|
|
155
|
+
* equipments: [
|
|
156
|
+
* { name: 'UMP45', quality: 'rare', level: 74 },
|
|
157
|
+
* { name: 'Level 2 Vest', quality: 'common', level: 67 },
|
|
158
|
+
* { name: 'Red Dot Sight', quality: 'common', level: 62 },
|
|
159
|
+
* { name: 'Extended Mag', quality: 'rare', level: 70 }
|
|
160
|
+
* ]
|
|
161
|
+
* }
|
|
162
|
+
* ];
|
|
163
|
+
*
|
|
164
|
+
* // Create a TreeMultiMap for player rankings
|
|
165
|
+
* const playerRankings = new TreeMultiMap<number, Equipment, Player>(players, {
|
|
166
|
+
* toEntryFn: ({ score, equipments }) => [score, equipments],
|
|
167
|
+
* isMapMode: false
|
|
168
|
+
* });
|
|
169
|
+
*
|
|
170
|
+
* const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node));
|
|
171
|
+
* console.log(topPlayersEquipments); // [
|
|
172
|
+
* // [
|
|
173
|
+
* // {
|
|
174
|
+
* // name: 'MK14',
|
|
175
|
+
* // quality: 'legendary',
|
|
176
|
+
* // level: 92
|
|
177
|
+
* // },
|
|
178
|
+
* // { name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
179
|
+
* // {
|
|
180
|
+
* // name: 'Scope 8x',
|
|
181
|
+
* // quality: 'epic',
|
|
182
|
+
* // level: 80
|
|
183
|
+
* // },
|
|
184
|
+
* // { name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
185
|
+
* // {
|
|
186
|
+
* // name: 'Tactical Stock',
|
|
187
|
+
* // quality: 'rare',
|
|
188
|
+
* // level: 75
|
|
189
|
+
* // }
|
|
190
|
+
* // ],
|
|
191
|
+
* // [
|
|
192
|
+
* // { name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
193
|
+
* // {
|
|
194
|
+
* // name: 'Level 3 Vest',
|
|
195
|
+
* // quality: 'legendary',
|
|
196
|
+
* // level: 85
|
|
197
|
+
* // },
|
|
198
|
+
* // { name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
199
|
+
* // {
|
|
200
|
+
* // name: 'Suppressor',
|
|
201
|
+
* // quality: 'legendary',
|
|
202
|
+
* // level: 88
|
|
203
|
+
* // },
|
|
204
|
+
* // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
205
|
+
* // ]
|
|
206
|
+
* // ]
|
|
49
207
|
*/
|
|
50
208
|
class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
51
209
|
/**
|
|
@@ -60,7 +218,7 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
60
218
|
* additional options for configuring the TreeMultiMap instance.
|
|
61
219
|
*/
|
|
62
220
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
63
|
-
super([], Object.assign(
|
|
221
|
+
super([], Object.assign({}, options));
|
|
64
222
|
if (keysNodesEntriesOrRaws) {
|
|
65
223
|
this.addMany(keysNodesEntriesOrRaws);
|
|
66
224
|
}
|
|
@@ -79,34 +237,36 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
79
237
|
* data and the provided options merged with the existing properties of the current object.
|
|
80
238
|
*/
|
|
81
239
|
createTree(options) {
|
|
82
|
-
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
240
|
+
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse, isMapMode: this._isMapMode }, options));
|
|
83
241
|
}
|
|
84
242
|
/**
|
|
85
243
|
* Time Complexity: O(1)
|
|
86
244
|
* Space Complexity: O(1)
|
|
87
245
|
*
|
|
88
|
-
* The function `createNode` overrides the
|
|
89
|
-
*
|
|
90
|
-
* @param {K} key - The `key` parameter
|
|
91
|
-
*
|
|
92
|
-
* @
|
|
93
|
-
*
|
|
246
|
+
* The function `createNode` overrides the creation of a new TreeMultiMapNode with a specified key
|
|
247
|
+
* and value array.
|
|
248
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
249
|
+
* `TreeMultiMap`.
|
|
250
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
251
|
+
* values associated with a specific key in the TreeMultiMap data structure.
|
|
252
|
+
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned with the specified key and
|
|
253
|
+
* value. If `_isMapMode` is true, an empty array is passed as the value, otherwise the provided
|
|
254
|
+
* value is used.
|
|
94
255
|
*/
|
|
95
|
-
createNode(key) {
|
|
96
|
-
return new TreeMultiMapNode(key, []);
|
|
256
|
+
createNode(key, value = []) {
|
|
257
|
+
return new TreeMultiMapNode(key, this._isMapMode ? [] : value);
|
|
97
258
|
}
|
|
98
259
|
/**
|
|
99
260
|
* Time Complexity: O(log n)
|
|
100
261
|
* Space Complexity: O(log n)
|
|
101
262
|
*
|
|
102
|
-
* The function
|
|
103
|
-
*
|
|
104
|
-
* @param
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* @param {V} [
|
|
108
|
-
* you want to add to the TreeMultiMap.
|
|
109
|
-
* be added to the existing list of values associated with that key. If the key is not present,
|
|
263
|
+
* The function overrides the add method to handle different types of input for a TreeMultiMap data
|
|
264
|
+
* structure.
|
|
265
|
+
* @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
|
|
266
|
+
* be added to the TreeMultiMap. It can be of type `K`, which is the key type of the TreeMultiMap, or
|
|
267
|
+
* it can be a TreeMultiMapNode containing the key and its
|
|
268
|
+
* @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
|
|
269
|
+
* that you want to add to the TreeMultiMap. It can contain one or more values of type `V`.
|
|
110
270
|
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
111
271
|
* successful or not.
|
|
112
272
|
*/
|
|
@@ -116,30 +276,40 @@ class TreeMultiMap extends red_black_tree_1.RedBlackTree {
|
|
|
116
276
|
const _commonAdd = (key, values) => {
|
|
117
277
|
if (key === undefined || key === null)
|
|
118
278
|
return false;
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
existingValues.push(value);
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
const existingNode = this.getNode(key);
|
|
126
|
-
if (this.isRealNode(existingNode)) {
|
|
127
|
-
if (existingValues === undefined) {
|
|
128
|
-
super.add(key, values);
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
if (values !== undefined) {
|
|
279
|
+
const _addToValues = () => {
|
|
280
|
+
const existingValues = this.get(key);
|
|
281
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
132
282
|
for (const value of values)
|
|
133
283
|
existingValues.push(value);
|
|
134
284
|
return true;
|
|
135
285
|
}
|
|
286
|
+
return false;
|
|
287
|
+
};
|
|
288
|
+
const _addByNode = () => {
|
|
289
|
+
const existingNode = this.getNode(key);
|
|
290
|
+
if (this.isRealNode(existingNode)) {
|
|
291
|
+
const existingValues = this.get(existingNode);
|
|
292
|
+
if (existingValues === undefined) {
|
|
293
|
+
super.add(key, values);
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
if (values !== undefined) {
|
|
297
|
+
for (const value of values)
|
|
298
|
+
existingValues.push(value);
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
136
305
|
else {
|
|
137
|
-
return
|
|
306
|
+
return super.add(key, values);
|
|
138
307
|
}
|
|
308
|
+
};
|
|
309
|
+
if (this._isMapMode) {
|
|
310
|
+
return _addByNode() || _addToValues();
|
|
139
311
|
}
|
|
140
|
-
|
|
141
|
-
return super.add(key, values);
|
|
142
|
-
}
|
|
312
|
+
return _addToValues() || _addByNode();
|
|
143
313
|
};
|
|
144
314
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
145
315
|
const [key, values] = keyNodeOrEntry;
|
|
@@ -250,7 +250,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
250
250
|
isWeight = false;
|
|
251
251
|
if (isWeight) {
|
|
252
252
|
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
253
|
-
let min =
|
|
253
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
254
254
|
for (const path of allPaths) {
|
|
255
255
|
min = Math.min(this.getPathSumWeight(path), min);
|
|
256
256
|
}
|
|
@@ -267,8 +267,8 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
267
267
|
const queue = new queue_1.Queue([vertex1]);
|
|
268
268
|
visited.set(vertex1, true);
|
|
269
269
|
let cost = 0;
|
|
270
|
-
while (queue.
|
|
271
|
-
for (let i = 0; i < queue.
|
|
270
|
+
while (queue.length > 0) {
|
|
271
|
+
for (let i = 0; i < queue.length; i++) {
|
|
272
272
|
const cur = queue.shift();
|
|
273
273
|
if (cur === vertex2) {
|
|
274
274
|
return cost;
|
|
@@ -315,7 +315,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
315
315
|
if (isWeight) {
|
|
316
316
|
if (isDFS) {
|
|
317
317
|
const allPaths = this.getAllPathsBetween(v1, v2, 10000);
|
|
318
|
-
let min =
|
|
318
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
319
319
|
let minIndex = -1;
|
|
320
320
|
let index = 0;
|
|
321
321
|
for (const path of allPaths) {
|
|
@@ -379,7 +379,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
379
379
|
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
380
380
|
*/
|
|
381
381
|
dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
382
|
-
let minDist =
|
|
382
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
383
383
|
let minDest = undefined;
|
|
384
384
|
let minPath = [];
|
|
385
385
|
const paths = [];
|
|
@@ -395,12 +395,12 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
395
395
|
for (const vertex of vertexMap) {
|
|
396
396
|
const vertexOrKey = vertex[1];
|
|
397
397
|
if (vertexOrKey instanceof AbstractVertex)
|
|
398
|
-
distMap.set(vertexOrKey,
|
|
398
|
+
distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
399
399
|
}
|
|
400
400
|
distMap.set(srcVertex, 0);
|
|
401
401
|
preMap.set(srcVertex, undefined);
|
|
402
402
|
const getMinOfNoSeen = () => {
|
|
403
|
-
let min =
|
|
403
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
404
404
|
let minV = undefined;
|
|
405
405
|
for (const [key, value] of distMap) {
|
|
406
406
|
if (!seen.has(key)) {
|
|
@@ -435,7 +435,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
435
435
|
seen.add(cur);
|
|
436
436
|
if (destVertex && destVertex === cur) {
|
|
437
437
|
if (getMinDist) {
|
|
438
|
-
minDist = distMap.get(destVertex) ||
|
|
438
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
439
439
|
}
|
|
440
440
|
if (genPaths) {
|
|
441
441
|
getPaths(destVertex);
|
|
@@ -497,7 +497,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
497
497
|
*/
|
|
498
498
|
dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
499
499
|
var _a;
|
|
500
|
-
let minDist =
|
|
500
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
501
501
|
let minDest = undefined;
|
|
502
502
|
let minPath = [];
|
|
503
503
|
const paths = [];
|
|
@@ -512,7 +512,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
512
512
|
for (const vertex of vertexMap) {
|
|
513
513
|
const vertexOrKey = vertex[1];
|
|
514
514
|
if (vertexOrKey instanceof AbstractVertex)
|
|
515
|
-
distMap.set(vertexOrKey,
|
|
515
|
+
distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
516
516
|
}
|
|
517
517
|
const heap = new heap_1.Heap([], { comparator: (a, b) => a.key - b.key });
|
|
518
518
|
heap.add({ key: 0, value: srcVertex });
|
|
@@ -549,7 +549,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
549
549
|
seen.add(cur);
|
|
550
550
|
if (destVertex && destVertex === cur) {
|
|
551
551
|
if (getMinDist) {
|
|
552
|
-
minDist = distMap.get(destVertex) ||
|
|
552
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
553
553
|
}
|
|
554
554
|
if (genPaths) {
|
|
555
555
|
getPaths(destVertex);
|
|
@@ -618,7 +618,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
618
618
|
const paths = [];
|
|
619
619
|
const distMap = new Map();
|
|
620
620
|
const preMap = new Map(); // predecessor
|
|
621
|
-
let min =
|
|
621
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
622
622
|
let minPath = [];
|
|
623
623
|
// TODO
|
|
624
624
|
let hasNegativeCycle;
|
|
@@ -631,7 +631,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
631
631
|
const edgeMap = this.edgeSet();
|
|
632
632
|
const numOfEdges = edgeMap.length;
|
|
633
633
|
this._vertexMap.forEach(vertex => {
|
|
634
|
-
distMap.set(vertex,
|
|
634
|
+
distMap.set(vertex, Number.MAX_SAFE_INTEGER);
|
|
635
635
|
});
|
|
636
636
|
distMap.set(srcVertex, 0);
|
|
637
637
|
for (let i = 1; i < numOfVertices; ++i) {
|
|
@@ -643,7 +643,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
643
643
|
const sWeight = distMap.get(s);
|
|
644
644
|
const dWeight = distMap.get(d);
|
|
645
645
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
646
|
-
if (distMap.get(s) !==
|
|
646
|
+
if (distMap.get(s) !== Number.MAX_SAFE_INTEGER && sWeight + weight < dWeight) {
|
|
647
647
|
distMap.set(d, sWeight + weight);
|
|
648
648
|
if (genPath)
|
|
649
649
|
preMap.set(d, s);
|
|
@@ -688,7 +688,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
688
688
|
const weight = edgeMap[j].weight;
|
|
689
689
|
const sWeight = distMap.get(s);
|
|
690
690
|
if (sWeight) {
|
|
691
|
-
if (sWeight !==
|
|
691
|
+
if (sWeight !== Number.MAX_SAFE_INTEGER && sWeight + weight < sWeight)
|
|
692
692
|
hasNegativeCycle = true;
|
|
693
693
|
}
|
|
694
694
|
}
|
|
@@ -738,7 +738,7 @@ class AbstractGraph extends base_1.IterableEntryBase {
|
|
|
738
738
|
}
|
|
739
739
|
for (let i = 0; i < n; i++) {
|
|
740
740
|
for (let j = 0; j < n; j++) {
|
|
741
|
-
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) ||
|
|
741
|
+
costs[i][j] = ((_a = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])) === null || _a === void 0 ? void 0 : _a.weight) || Number.MAX_SAFE_INTEGER;
|
|
742
742
|
}
|
|
743
743
|
}
|
|
744
744
|
for (let k = 0; k < n; k++) {
|
|
@@ -13,6 +13,52 @@ import { IterableEntryBase } from '../base';
|
|
|
13
13
|
* 3. Unique Keys: Keys are unique.
|
|
14
14
|
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
15
15
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
16
|
+
* @example
|
|
17
|
+
* // should maintain insertion order
|
|
18
|
+
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
19
|
+
* linkedHashMap.set(1, 'A');
|
|
20
|
+
* linkedHashMap.set(2, 'B');
|
|
21
|
+
* linkedHashMap.set(3, 'C');
|
|
22
|
+
*
|
|
23
|
+
* const result = Array.from(linkedHashMap);
|
|
24
|
+
* console.log(result); // [
|
|
25
|
+
* // [1, 'A'],
|
|
26
|
+
* // [2, 'B'],
|
|
27
|
+
* // [3, 'C']
|
|
28
|
+
* // ]
|
|
29
|
+
* @example
|
|
30
|
+
* // fast lookup of values by key
|
|
31
|
+
* const hashMap = new HashMap<number, string>();
|
|
32
|
+
* hashMap.set(1, 'A');
|
|
33
|
+
* hashMap.set(2, 'B');
|
|
34
|
+
* hashMap.set(3, 'C');
|
|
35
|
+
*
|
|
36
|
+
* console.log(hashMap.get(1)); // 'A'
|
|
37
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
38
|
+
* console.log(hashMap.get(3)); // 'C'
|
|
39
|
+
* console.log(hashMap.get(99)); // undefined
|
|
40
|
+
* @example
|
|
41
|
+
* // remove duplicates when adding multiple entries
|
|
42
|
+
* const hashMap = new HashMap<number, string>();
|
|
43
|
+
* hashMap.set(1, 'A');
|
|
44
|
+
* hashMap.set(2, 'B');
|
|
45
|
+
* hashMap.set(1, 'C'); // Update value for key 1
|
|
46
|
+
*
|
|
47
|
+
* console.log(hashMap.size); // 2
|
|
48
|
+
* console.log(hashMap.get(1)); // 'C'
|
|
49
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
50
|
+
* @example
|
|
51
|
+
* // count occurrences of keys
|
|
52
|
+
* const data = [1, 2, 1, 3, 2, 1];
|
|
53
|
+
*
|
|
54
|
+
* const countMap = new HashMap<number, number>();
|
|
55
|
+
* for (const key of data) {
|
|
56
|
+
* countMap.set(key, (countMap.get(key) || 0) + 1);
|
|
57
|
+
* }
|
|
58
|
+
*
|
|
59
|
+
* console.log(countMap.get(1)); // 3
|
|
60
|
+
* console.log(countMap.get(2)); // 2
|
|
61
|
+
* console.log(countMap.get(3)); // 1
|
|
16
62
|
*/
|
|
17
63
|
export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
18
64
|
/**
|
|
@@ -9,6 +9,52 @@ const utils_1 = require("../../utils");
|
|
|
9
9
|
* 3. Unique Keys: Keys are unique.
|
|
10
10
|
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
11
11
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
12
|
+
* @example
|
|
13
|
+
* // should maintain insertion order
|
|
14
|
+
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
15
|
+
* linkedHashMap.set(1, 'A');
|
|
16
|
+
* linkedHashMap.set(2, 'B');
|
|
17
|
+
* linkedHashMap.set(3, 'C');
|
|
18
|
+
*
|
|
19
|
+
* const result = Array.from(linkedHashMap);
|
|
20
|
+
* console.log(result); // [
|
|
21
|
+
* // [1, 'A'],
|
|
22
|
+
* // [2, 'B'],
|
|
23
|
+
* // [3, 'C']
|
|
24
|
+
* // ]
|
|
25
|
+
* @example
|
|
26
|
+
* // fast lookup of values by key
|
|
27
|
+
* const hashMap = new HashMap<number, string>();
|
|
28
|
+
* hashMap.set(1, 'A');
|
|
29
|
+
* hashMap.set(2, 'B');
|
|
30
|
+
* hashMap.set(3, 'C');
|
|
31
|
+
*
|
|
32
|
+
* console.log(hashMap.get(1)); // 'A'
|
|
33
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
34
|
+
* console.log(hashMap.get(3)); // 'C'
|
|
35
|
+
* console.log(hashMap.get(99)); // undefined
|
|
36
|
+
* @example
|
|
37
|
+
* // remove duplicates when adding multiple entries
|
|
38
|
+
* const hashMap = new HashMap<number, string>();
|
|
39
|
+
* hashMap.set(1, 'A');
|
|
40
|
+
* hashMap.set(2, 'B');
|
|
41
|
+
* hashMap.set(1, 'C'); // Update value for key 1
|
|
42
|
+
*
|
|
43
|
+
* console.log(hashMap.size); // 2
|
|
44
|
+
* console.log(hashMap.get(1)); // 'C'
|
|
45
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
46
|
+
* @example
|
|
47
|
+
* // count occurrences of keys
|
|
48
|
+
* const data = [1, 2, 1, 3, 2, 1];
|
|
49
|
+
*
|
|
50
|
+
* const countMap = new HashMap<number, number>();
|
|
51
|
+
* for (const key of data) {
|
|
52
|
+
* countMap.set(key, (countMap.get(key) || 0) + 1);
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* console.log(countMap.get(1)); // 3
|
|
56
|
+
* console.log(countMap.get(2)); // 2
|
|
57
|
+
* console.log(countMap.get(3)); // 1
|
|
12
58
|
*/
|
|
13
59
|
class HashMap extends base_1.IterableEntryBase {
|
|
14
60
|
/**
|
|
@@ -183,7 +183,7 @@ import { IterableElementBase } from '../base';
|
|
|
183
183
|
* ]);
|
|
184
184
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
185
185
|
*/
|
|
186
|
-
export declare class Heap<E = any, R = any> extends IterableElementBase<E, R
|
|
186
|
+
export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
187
187
|
/**
|
|
188
188
|
* The constructor initializes a heap data structure with optional elements and options.
|
|
189
189
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
@@ -308,14 +308,6 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R, He
|
|
|
308
308
|
* @returns An array containing elements traversed in the specified order.
|
|
309
309
|
*/
|
|
310
310
|
dfs(order?: DFSOrderPattern): E[];
|
|
311
|
-
/**
|
|
312
|
-
* Time Complexity: O(n)
|
|
313
|
-
* Space Complexity: O(n)
|
|
314
|
-
*
|
|
315
|
-
* Convert the heap to an array.
|
|
316
|
-
* @returns An array containing the elements of the heap.
|
|
317
|
-
*/
|
|
318
|
-
toArray(): E[];
|
|
319
311
|
/**
|
|
320
312
|
* Time Complexity: O(n)
|
|
321
313
|
* Space Complexity: O(n)
|
|
@@ -355,7 +347,7 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R, He
|
|
|
355
347
|
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
356
348
|
* the filter condition specified by the `callback` function.
|
|
357
349
|
*/
|
|
358
|
-
filter(callback: ElementCallback<E, R, boolean
|
|
350
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): Heap<E, R>;
|
|
359
351
|
/**
|
|
360
352
|
* Time Complexity: O(n)
|
|
361
353
|
* Space Complexity: O(n)
|
|
@@ -377,7 +369,7 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R, He
|
|
|
377
369
|
* value of
|
|
378
370
|
* @returns a new instance of the `Heap` class with the mapped elements.
|
|
379
371
|
*/
|
|
380
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM
|
|
372
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Heap<EM, RM>;
|
|
381
373
|
protected _DEFAULT_COMPARATOR: (a: E, b: E) => number;
|
|
382
374
|
protected _comparator: Comparator<E>;
|
|
383
375
|
/**
|
|
@@ -413,16 +413,6 @@ class Heap extends base_1.IterableElementBase {
|
|
|
413
413
|
_dfs(0); // Traverse starting from the root node
|
|
414
414
|
return result;
|
|
415
415
|
}
|
|
416
|
-
/**
|
|
417
|
-
* Time Complexity: O(n)
|
|
418
|
-
* Space Complexity: O(n)
|
|
419
|
-
*
|
|
420
|
-
* Convert the heap to an array.
|
|
421
|
-
* @returns An array containing the elements of the heap.
|
|
422
|
-
*/
|
|
423
|
-
toArray() {
|
|
424
|
-
return [...this.elements];
|
|
425
|
-
}
|
|
426
416
|
/**
|
|
427
417
|
* Time Complexity: O(n)
|
|
428
418
|
* Space Complexity: O(n)
|
|
@@ -42,7 +42,7 @@ export declare class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
42
42
|
* @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
|
|
43
43
|
* the filter condition specified by the `callback` function.
|
|
44
44
|
*/
|
|
45
|
-
filter(callback: ElementCallback<E, R, boolean
|
|
45
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MaxHeap<E, R>;
|
|
46
46
|
/**
|
|
47
47
|
* Time Complexity: O(n log n)
|
|
48
48
|
* Space Complexity: O(n)
|
|
@@ -64,5 +64,5 @@ export declare class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
64
64
|
* value of
|
|
65
65
|
* @returns a new instance of the `MaxHeap` class with the mapped elements.
|
|
66
66
|
*/
|
|
67
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM
|
|
67
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxHeap<EM, RM>;
|
|
68
68
|
}
|
|
@@ -42,7 +42,7 @@ export declare class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
42
42
|
* @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
|
|
43
43
|
* the filter condition specified by the `callback` function.
|
|
44
44
|
*/
|
|
45
|
-
filter(callback: ElementCallback<E, R, boolean
|
|
45
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MinHeap<E, R>;
|
|
46
46
|
/**
|
|
47
47
|
* Time Complexity: O(n log n)
|
|
48
48
|
* Space Complexity: O(n)
|
|
@@ -64,5 +64,5 @@ export declare class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
64
64
|
* value of
|
|
65
65
|
* @returns a new instance of the `MinHeap` class with the mapped elements.
|
|
66
66
|
*/
|
|
67
|
-
map<EM, RM>(callback: ElementCallback<E, R, EM
|
|
67
|
+
map<EM, RM>(callback: ElementCallback<E, R, EM>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinHeap<EM, RM>;
|
|
68
68
|
}
|