data-structure-typed 1.49.1 → 1.49.3
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 +16 -16
- package/README_zh-CN.md +2 -2
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +457 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/cjs/data-structures/base/iterable-base.js +21 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/cjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/cjs/data-structures/hash/hash-map.js +16 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/cjs/data-structures/heap/heap.js +10 -42
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +143 -146
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +42 -42
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/cjs/data-structures/queue/deque.js +100 -110
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/cjs/data-structures/queue/queue.js +46 -49
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/cjs/data-structures/stack/stack.js +2 -5
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/cjs/data-structures/trie/trie.js +2 -5
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +11 -0
- package/dist/mjs/data-structures/base/iterable-base.js +21 -0
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +7 -7
- package/dist/mjs/data-structures/graph/abstract-graph.js +43 -12
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/undirected-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +9 -9
- package/dist/mjs/data-structures/hash/hash-map.js +16 -15
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -35
- package/dist/mjs/data-structures/heap/heap.js +10 -42
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +99 -105
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +142 -145
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +16 -21
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +43 -43
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +25 -25
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +36 -36
- package/dist/mjs/data-structures/queue/deque.d.ts +70 -75
- package/dist/mjs/data-structures/queue/deque.js +100 -110
- package/dist/mjs/data-structures/queue/queue.d.ts +37 -38
- package/dist/mjs/data-structures/queue/queue.js +45 -48
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -3
- package/dist/mjs/data-structures/stack/stack.js +2 -5
- package/dist/mjs/data-structures/trie/trie.d.ts +1 -2
- package/dist/mjs/data-structures/trie/trie.js +2 -5
- package/dist/umd/data-structure-typed.js +531 -532
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -14
- package/src/data-structures/graph/directed-graph.ts +3 -2
- package/src/data-structures/graph/undirected-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -28
- package/src/data-structures/heap/heap.ts +19 -57
- package/src/data-structures/linked-list/doubly-linked-list.ts +157 -161
- package/src/data-structures/linked-list/singly-linked-list.ts +49 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +40 -40
- package/src/data-structures/queue/deque.ts +122 -135
- package/src/data-structures/queue/queue.ts +54 -58
- package/src/data-structures/stack/stack.ts +4 -8
- package/src/data-structures/trie/trie.ts +5 -9
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +48 -3
- package/test/unit/data-structures/graph/undirected-graph.test.ts +48 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +6 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +21 -21
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/queue/deque.test.ts +5 -5
- package/test/unit/data-structures/queue/queue.test.ts +4 -4
- package/test/unit/data-structures/trie/trie.test.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.49.
|
|
3
|
+
"version": "1.49.3",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -181,6 +181,21 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
181
181
|
return accumulator;
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
+
hasValue(value: V): boolean {
|
|
185
|
+
for (const [, elementValue] of this) {
|
|
186
|
+
if (elementValue === value) return true;
|
|
187
|
+
}
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Time Complexity: O(n)
|
|
193
|
+
* Space Complexity: O(n)
|
|
194
|
+
*/
|
|
195
|
+
print(): void {
|
|
196
|
+
console.log([...this])
|
|
197
|
+
}
|
|
198
|
+
|
|
184
199
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
185
200
|
}
|
|
186
201
|
|
|
@@ -325,5 +340,14 @@ export abstract class IterableElementBase<V> {
|
|
|
325
340
|
return accumulator;
|
|
326
341
|
}
|
|
327
342
|
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Time Complexity: O(n)
|
|
346
|
+
* Space Complexity: O(n)
|
|
347
|
+
*/
|
|
348
|
+
print(): void {
|
|
349
|
+
console.log([...this])
|
|
350
|
+
}
|
|
351
|
+
|
|
328
352
|
protected abstract _getIterator(...args: any[]): IterableIterator<V>;
|
|
329
353
|
}
|
|
@@ -156,10 +156,10 @@ export abstract class AbstractGraph<
|
|
|
156
156
|
|
|
157
157
|
addVertex(keyOrVertex: VertexKey | VO, value?: V): boolean {
|
|
158
158
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
159
|
-
return this.
|
|
159
|
+
return this._addVertex(keyOrVertex);
|
|
160
160
|
} else {
|
|
161
161
|
const newVertex = this.createVertex(keyOrVertex, value);
|
|
162
|
-
return this.
|
|
162
|
+
return this._addVertex(newVertex);
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
@@ -242,14 +242,14 @@ export abstract class AbstractGraph<
|
|
|
242
242
|
|
|
243
243
|
addEdge(srcOrEdge: VO | VertexKey | EO, dest?: VO | VertexKey, weight?: number, value?: E): boolean {
|
|
244
244
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
245
|
-
return this.
|
|
245
|
+
return this._addEdge(srcOrEdge);
|
|
246
246
|
} else {
|
|
247
247
|
if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
|
|
248
248
|
if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest))) return false;
|
|
249
249
|
if (srcOrEdge instanceof AbstractVertex) srcOrEdge = srcOrEdge.key;
|
|
250
250
|
if (dest instanceof AbstractVertex) dest = dest.key;
|
|
251
251
|
const newEdge = this.createEdge(srcOrEdge, dest, weight, value);
|
|
252
|
-
return this.
|
|
252
|
+
return this._addEdge(newEdge);
|
|
253
253
|
} else {
|
|
254
254
|
throw new Error('dest must be a Vertex or vertex key while srcOrEdge is an Edge');
|
|
255
255
|
}
|
|
@@ -1147,14 +1147,6 @@ export abstract class AbstractGraph<
|
|
|
1147
1147
|
return this.tarjan(false, false, false, false).lowMap;
|
|
1148
1148
|
}
|
|
1149
1149
|
|
|
1150
|
-
/**
|
|
1151
|
-
* The function `getCycles` returns a map of cycles found using the Tarjan algorithm.
|
|
1152
|
-
* @returns The function `getCycles()` is returning a `Map<number, VO[]>`.
|
|
1153
|
-
*/
|
|
1154
|
-
getCycles(): Map<number, VO[]> {
|
|
1155
|
-
return this.tarjan(false, false, false, true).cycles;
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
1150
|
/**
|
|
1159
1151
|
* The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
|
|
1160
1152
|
* @returns an array of VO objects, specifically the cut vertexes.
|
|
@@ -1180,6 +1172,55 @@ export abstract class AbstractGraph<
|
|
|
1180
1172
|
return this.tarjan(false, true, false, false).bridges;
|
|
1181
1173
|
}
|
|
1182
1174
|
|
|
1175
|
+
/**
|
|
1176
|
+
* O(V+E+C)
|
|
1177
|
+
* O(V+C)
|
|
1178
|
+
*/
|
|
1179
|
+
getCycles(isInclude2Cycle: boolean = false): VertexKey[][] {
|
|
1180
|
+
const cycles: VertexKey[][] = [];
|
|
1181
|
+
const visited: Set<VO> = new Set();
|
|
1182
|
+
|
|
1183
|
+
const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
|
|
1184
|
+
if (visited.has(vertex)) {
|
|
1185
|
+
if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
|
|
1186
|
+
cycles.push([...currentPath]);
|
|
1187
|
+
}
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
visited.add(vertex);
|
|
1192
|
+
currentPath.push(vertex.key);
|
|
1193
|
+
|
|
1194
|
+
for (const neighbor of this.getNeighbors(vertex)) {
|
|
1195
|
+
neighbor && dfs(neighbor, currentPath, visited);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
visited.delete(vertex);
|
|
1199
|
+
currentPath.pop();
|
|
1200
|
+
};
|
|
1201
|
+
|
|
1202
|
+
for (const vertex of this.vertexMap.values()) {
|
|
1203
|
+
dfs(vertex, [], visited);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// Use a set to eliminate duplicate cycles
|
|
1207
|
+
const uniqueCycles = new Map<string, VertexKey[]>();
|
|
1208
|
+
|
|
1209
|
+
for (const cycle of cycles) {
|
|
1210
|
+
const sorted = [...cycle].sort().toString()
|
|
1211
|
+
|
|
1212
|
+
if (uniqueCycles.has(sorted)) continue
|
|
1213
|
+
else {
|
|
1214
|
+
uniqueCycles.set(sorted, cycle)
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// Convert the unique cycles back to an array
|
|
1219
|
+
return [...uniqueCycles].map(cycleString =>
|
|
1220
|
+
cycleString[1]
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1183
1224
|
/**
|
|
1184
1225
|
* Time Complexity: O(n)
|
|
1185
1226
|
* Space Complexity: O(n)
|
|
@@ -1247,9 +1288,9 @@ export abstract class AbstractGraph<
|
|
|
1247
1288
|
}
|
|
1248
1289
|
}
|
|
1249
1290
|
|
|
1250
|
-
protected abstract
|
|
1291
|
+
protected abstract _addEdge(edge: EO): boolean;
|
|
1251
1292
|
|
|
1252
|
-
protected
|
|
1293
|
+
protected _addVertex(newVertex: VO): boolean {
|
|
1253
1294
|
if (this.hasVertex(newVertex)) {
|
|
1254
1295
|
return false;
|
|
1255
1296
|
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
@@ -596,6 +596,7 @@ export class DirectedGraph<
|
|
|
596
596
|
}
|
|
597
597
|
}
|
|
598
598
|
|
|
599
|
+
|
|
599
600
|
/**
|
|
600
601
|
* Time Complexity: O(1)
|
|
601
602
|
* Space Complexity: O(1)
|
|
@@ -605,13 +606,13 @@ export class DirectedGraph<
|
|
|
605
606
|
* Time Complexity: O(1)
|
|
606
607
|
* Space Complexity: O(1)
|
|
607
608
|
*
|
|
608
|
-
* The function `
|
|
609
|
+
* The function `_addEdge` adds an edge to a graph if the source and destination vertexMap exist.
|
|
609
610
|
* @param {EO} edge - The parameter `edge` is of type `EO`, which represents an edge in a graph. It is the edge that
|
|
610
611
|
* needs to be added to the graph.
|
|
611
612
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
612
613
|
* source or destination vertex does not exist in the graph.
|
|
613
614
|
*/
|
|
614
|
-
protected
|
|
615
|
+
protected _addEdge(edge: EO): boolean {
|
|
615
616
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
616
617
|
return false;
|
|
617
618
|
}
|
|
@@ -381,7 +381,7 @@ export class UndirectedGraph<
|
|
|
381
381
|
* @param {EO} edge - The parameter "edge" is of type EO, which represents an edge in a graph.
|
|
382
382
|
* @returns a boolean value.
|
|
383
383
|
*/
|
|
384
|
-
protected
|
|
384
|
+
protected _addEdge(edge: EO): boolean {
|
|
385
385
|
for (const end of edge.vertexMap) {
|
|
386
386
|
const endVertex = this._getVertex(end);
|
|
387
387
|
if (endVertex === undefined) return false;
|
|
@@ -35,7 +35,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
35
35
|
const { hashFn } = options;
|
|
36
36
|
if (hashFn) {
|
|
37
37
|
this._hashFn = hashFn;
|
|
38
|
-
|
|
39
38
|
}
|
|
40
39
|
}
|
|
41
40
|
if (elements) {
|
|
@@ -68,8 +67,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
68
67
|
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
69
68
|
* key in the data structure.
|
|
70
69
|
*/
|
|
71
|
-
set(key: K, value: V) {
|
|
72
|
-
|
|
70
|
+
set(key: K, value: V): boolean {
|
|
73
71
|
if (this._isObjKey(key)) {
|
|
74
72
|
if (!this._objMap.has(key)) {
|
|
75
73
|
this._size++;
|
|
@@ -83,6 +81,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
83
81
|
}
|
|
84
82
|
this._store[strKey] = { key, value };
|
|
85
83
|
}
|
|
84
|
+
return true;
|
|
86
85
|
}
|
|
87
86
|
|
|
88
87
|
/**
|
|
@@ -90,8 +89,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
90
89
|
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
91
90
|
* key-value pair is represented as an array with two elements: the key and the value.
|
|
92
91
|
*/
|
|
93
|
-
setMany(elements: Iterable<[K, V]>) {
|
|
94
|
-
|
|
92
|
+
setMany(elements: Iterable<[K, V]>): boolean[] {
|
|
93
|
+
const results: boolean[] = [];
|
|
94
|
+
for (const [key, value] of elements) results.push(this.set(key, value));
|
|
95
|
+
return results;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
/**
|
|
@@ -215,6 +216,10 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
215
216
|
console.log([...this.entries()]);
|
|
216
217
|
}
|
|
217
218
|
|
|
219
|
+
put(key: K, value: V): boolean {
|
|
220
|
+
return this.set(key, value);
|
|
221
|
+
}
|
|
222
|
+
|
|
218
223
|
/**
|
|
219
224
|
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
220
225
|
* object map.
|
|
@@ -286,7 +291,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
286
291
|
this.set(el[0], el[1]);
|
|
287
292
|
}
|
|
288
293
|
}
|
|
289
|
-
|
|
290
294
|
}
|
|
291
295
|
|
|
292
296
|
protected _size = 0;
|
|
@@ -356,7 +360,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
356
360
|
* value associated with the key being set in the data structure.
|
|
357
361
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
358
362
|
*/
|
|
359
|
-
set(key: K, value?: V) {
|
|
363
|
+
set(key: K, value?: V): boolean {
|
|
360
364
|
let node;
|
|
361
365
|
const isNewKey = !this.has(key); // Check if the key is new
|
|
362
366
|
|
|
@@ -398,7 +402,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
398
402
|
this._size++;
|
|
399
403
|
}
|
|
400
404
|
|
|
401
|
-
return
|
|
405
|
+
return true;
|
|
402
406
|
}
|
|
403
407
|
|
|
404
408
|
has(key: K): boolean {
|
|
@@ -411,18 +415,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
411
415
|
}
|
|
412
416
|
}
|
|
413
417
|
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
if (elementValue === value) return true;
|
|
417
|
-
}
|
|
418
|
-
return false;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
setMany(entries: Iterable<[K, V]>): void {
|
|
418
|
+
setMany(entries: Iterable<[K, V]>): boolean[] {
|
|
419
|
+
const results: boolean[] = [];
|
|
422
420
|
for (const entry of entries) {
|
|
423
421
|
const [key, value] = entry;
|
|
424
|
-
this.set(key, value);
|
|
422
|
+
results.push(this.set(key, value));
|
|
425
423
|
}
|
|
424
|
+
return results;
|
|
426
425
|
}
|
|
427
426
|
|
|
428
427
|
/**
|
|
@@ -461,13 +460,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
461
460
|
* the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`,
|
|
462
461
|
* where `K` is the key and `V` is the value.
|
|
463
462
|
*/
|
|
464
|
-
getAt(index: number) {
|
|
463
|
+
getAt(index: number): V | undefined {
|
|
465
464
|
rangeCheck(index, 0, this._size - 1);
|
|
466
465
|
let node = this._head;
|
|
467
466
|
while (index--) {
|
|
468
467
|
node = node.next;
|
|
469
468
|
}
|
|
470
|
-
return
|
|
469
|
+
return node.value;
|
|
471
470
|
}
|
|
472
471
|
|
|
473
472
|
/**
|
|
@@ -480,7 +479,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
480
479
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
481
480
|
* was not found.
|
|
482
481
|
*/
|
|
483
|
-
delete(key: K) {
|
|
482
|
+
delete(key: K): boolean {
|
|
484
483
|
let node;
|
|
485
484
|
|
|
486
485
|
if (isWeakKey(key)) {
|
|
@@ -521,14 +520,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
521
520
|
* deleted in the linked list.
|
|
522
521
|
* @returns The size of the list after deleting the element at the specified index.
|
|
523
522
|
*/
|
|
524
|
-
deleteAt(index: number) {
|
|
523
|
+
deleteAt(index: number): boolean {
|
|
525
524
|
rangeCheck(index, 0, this._size - 1);
|
|
526
525
|
let node = this._head;
|
|
527
526
|
while (index--) {
|
|
528
527
|
node = node.next;
|
|
529
528
|
}
|
|
530
|
-
this._deleteNode(node);
|
|
531
|
-
return this._size;
|
|
529
|
+
return this._deleteNode(node);
|
|
532
530
|
}
|
|
533
531
|
|
|
534
532
|
/**
|
|
@@ -539,7 +537,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
539
537
|
* @returns The method is returning a boolean value indicating whether the size of the object is 0 or
|
|
540
538
|
* not.
|
|
541
539
|
*/
|
|
542
|
-
isEmpty() {
|
|
540
|
+
isEmpty(): boolean {
|
|
543
541
|
return this._size === 0;
|
|
544
542
|
}
|
|
545
543
|
|
|
@@ -549,7 +547,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
549
547
|
*
|
|
550
548
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
551
549
|
*/
|
|
552
|
-
clear() {
|
|
550
|
+
clear(): void {
|
|
553
551
|
this._noObjMap = {};
|
|
554
552
|
this._size = 0;
|
|
555
553
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
@@ -629,8 +627,8 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
629
627
|
return mappedMap;
|
|
630
628
|
}
|
|
631
629
|
|
|
632
|
-
|
|
633
|
-
|
|
630
|
+
put(key: K, value: V): boolean {
|
|
631
|
+
return this.set(key, value);
|
|
634
632
|
}
|
|
635
633
|
|
|
636
634
|
/**
|
|
@@ -657,7 +655,7 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
657
655
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
658
656
|
* and next nodes in the list.
|
|
659
657
|
*/
|
|
660
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>) {
|
|
658
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): boolean {
|
|
661
659
|
const { prev, next } = node;
|
|
662
660
|
prev.next = next;
|
|
663
661
|
next.prev = prev;
|
|
@@ -671,5 +669,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
671
669
|
}
|
|
672
670
|
|
|
673
671
|
this._size -= 1;
|
|
672
|
+
return true;
|
|
674
673
|
}
|
|
675
674
|
}
|
|
@@ -42,7 +42,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
42
42
|
|
|
43
43
|
if (elements) {
|
|
44
44
|
for (const el of elements) {
|
|
45
|
-
this.
|
|
45
|
+
this.add(el);
|
|
46
46
|
}
|
|
47
47
|
// this.fix();
|
|
48
48
|
}
|
|
@@ -91,26 +91,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
91
91
|
* Insert an element into the heap and maintain the heap properties.
|
|
92
92
|
* @param element - The element to be inserted.
|
|
93
93
|
*/
|
|
94
|
-
add(element: E):
|
|
95
|
-
return this.push(element);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
100
|
-
* Space Complexity: O(1)
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
105
|
-
* Space Complexity: O(1)
|
|
106
|
-
*
|
|
107
|
-
* Insert an element into the heap and maintain the heap properties.
|
|
108
|
-
* @param element - The element to be inserted.
|
|
109
|
-
*/
|
|
110
|
-
push(element: E): Heap<E> {
|
|
94
|
+
add(element: E): boolean {
|
|
111
95
|
this._elements.push(element);
|
|
112
|
-
this._bubbleUp(this.elements.length - 1);
|
|
113
|
-
return this;
|
|
96
|
+
return this._bubbleUp(this.elements.length - 1);
|
|
114
97
|
}
|
|
115
98
|
|
|
116
99
|
/**
|
|
@@ -136,22 +119,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
136
119
|
return value;
|
|
137
120
|
}
|
|
138
121
|
|
|
139
|
-
/**
|
|
140
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
141
|
-
* Space Complexity: O(1)
|
|
142
|
-
*/
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
146
|
-
* Space Complexity: O(1)
|
|
147
|
-
*
|
|
148
|
-
* Remove and return the top element (smallest or largest element) from the heap.
|
|
149
|
-
* @returns The top element or undefined if the heap is empty.
|
|
150
|
-
*/
|
|
151
|
-
pop(): E | undefined {
|
|
152
|
-
return this.poll();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
122
|
/**
|
|
156
123
|
* Peek at the top element of the heap without removing it.
|
|
157
124
|
* @returns The top element or undefined if the heap is empty.
|
|
@@ -164,14 +131,14 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
164
131
|
* Check if the heap is empty.
|
|
165
132
|
* @returns True if the heap is empty, otherwise false.
|
|
166
133
|
*/
|
|
167
|
-
isEmpty() {
|
|
134
|
+
isEmpty(): boolean {
|
|
168
135
|
return this.size === 0;
|
|
169
136
|
}
|
|
170
137
|
|
|
171
138
|
/**
|
|
172
139
|
* Reset the elements of the heap. Make the elements empty.
|
|
173
140
|
*/
|
|
174
|
-
clear() {
|
|
141
|
+
clear(): void {
|
|
175
142
|
this._elements = [];
|
|
176
143
|
}
|
|
177
144
|
|
|
@@ -187,9 +154,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
187
154
|
* Clear and add elements of the heap
|
|
188
155
|
* @param elements
|
|
189
156
|
*/
|
|
190
|
-
refill(elements: E[]) {
|
|
157
|
+
refill(elements: E[]): boolean[] {
|
|
191
158
|
this._elements = elements;
|
|
192
|
-
this.fix();
|
|
159
|
+
return this.fix();
|
|
193
160
|
}
|
|
194
161
|
|
|
195
162
|
/**
|
|
@@ -225,11 +192,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
225
192
|
* @returns The `delete` function is returning a boolean value. It returns `true` if the element was
|
|
226
193
|
* successfully deleted from the array, and `false` if the element was not found in the array.
|
|
227
194
|
*/
|
|
228
|
-
delete(element: E) {
|
|
195
|
+
delete(element: E): boolean {
|
|
229
196
|
const index = this.elements.indexOf(element);
|
|
230
197
|
if (index < 0) return false;
|
|
231
198
|
if (index === 0) {
|
|
232
|
-
this.
|
|
199
|
+
this.poll();
|
|
233
200
|
} else if (index === this.elements.length - 1) {
|
|
234
201
|
this.elements.pop();
|
|
235
202
|
} else {
|
|
@@ -348,8 +315,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
348
315
|
*
|
|
349
316
|
* Fix the entire heap to maintain heap properties.
|
|
350
317
|
*/
|
|
351
|
-
fix() {
|
|
352
|
-
|
|
318
|
+
fix(): boolean[] {
|
|
319
|
+
const results: boolean[] = [];
|
|
320
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) results.push(this._sinkDown(i, this.elements.length >> 1));
|
|
321
|
+
return results;
|
|
353
322
|
}
|
|
354
323
|
|
|
355
324
|
/**
|
|
@@ -378,7 +347,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
378
347
|
let index = 0;
|
|
379
348
|
for (const current of this) {
|
|
380
349
|
if (callback.call(thisArg, current, index, this)) {
|
|
381
|
-
filteredList.
|
|
350
|
+
filteredList.add(current);
|
|
382
351
|
}
|
|
383
352
|
index++;
|
|
384
353
|
}
|
|
@@ -421,16 +390,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
421
390
|
return mappedHeap;
|
|
422
391
|
}
|
|
423
392
|
|
|
424
|
-
|
|
425
|
-
* Time Complexity: O(log n)
|
|
426
|
-
* Space Complexity: O(1)
|
|
427
|
-
*/
|
|
428
|
-
|
|
429
|
-
print(): void {
|
|
430
|
-
console.log([...this]);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
protected* _getIterator() {
|
|
393
|
+
protected* _getIterator(): IterableIterator<E> {
|
|
434
394
|
for (const element of this.elements) {
|
|
435
395
|
yield element;
|
|
436
396
|
}
|
|
@@ -448,7 +408,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
448
408
|
* Float operation to maintain heap properties after adding an element.
|
|
449
409
|
* @param index - The index of the newly added element.
|
|
450
410
|
*/
|
|
451
|
-
protected _bubbleUp(index: number) {
|
|
411
|
+
protected _bubbleUp(index: number): boolean {
|
|
452
412
|
const element = this.elements[index];
|
|
453
413
|
while (index > 0) {
|
|
454
414
|
const parent = (index - 1) >> 1;
|
|
@@ -458,6 +418,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
458
418
|
index = parent;
|
|
459
419
|
}
|
|
460
420
|
this.elements[index] = element;
|
|
421
|
+
return true;
|
|
461
422
|
}
|
|
462
423
|
|
|
463
424
|
/**
|
|
@@ -468,7 +429,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
468
429
|
* @param index - The index from which to start sinking.
|
|
469
430
|
* @param halfLength
|
|
470
431
|
*/
|
|
471
|
-
protected _sinkDown(index: number, halfLength: number) {
|
|
432
|
+
protected _sinkDown(index: number, halfLength: number): boolean {
|
|
472
433
|
const element = this.elements[index];
|
|
473
434
|
while (index < halfLength) {
|
|
474
435
|
let left = index << 1 | 1;
|
|
@@ -486,6 +447,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
486
447
|
index = left;
|
|
487
448
|
}
|
|
488
449
|
this.elements[index] = element;
|
|
450
|
+
return true;
|
|
489
451
|
}
|
|
490
452
|
}
|
|
491
453
|
|