data-structure-typed 1.44.1 → 1.45.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/.eslintrc.js +6 -6
- package/CHANGELOG.md +1 -1
- package/README.md +15 -15
- package/benchmark/report.html +15 -15
- package/benchmark/report.json +121 -121
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +230 -37
- package/dist/cjs/data-structures/hash/hash-map.js +427 -115
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/cjs/types/data-structures/hash/index.d.ts +6 -0
- package/dist/cjs/types/data-structures/hash/index.js +20 -0
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +3 -0
- package/dist/cjs/utils/utils.js +15 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +230 -37
- package/dist/mjs/data-structures/hash/hash-map.js +433 -121
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +15 -1
- package/dist/mjs/types/data-structures/hash/index.d.ts +6 -0
- package/dist/mjs/types/data-structures/hash/index.js +6 -1
- package/dist/mjs/utils/utils.d.ts +3 -0
- package/dist/mjs/utils/utils.js +11 -0
- package/dist/umd/data-structure-typed.js +533 -207
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +7 -7
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +39 -31
- package/src/data-structures/binary-tree/bst.ts +12 -8
- package/src/data-structures/binary-tree/rb-tree.ts +17 -6
- package/src/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/data-structures/binary-tree/tree-multimap.ts +12 -9
- package/src/data-structures/graph/abstract-graph.ts +46 -31
- package/src/data-structures/graph/directed-graph.ts +10 -5
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +9 -9
- package/src/data-structures/hash/hash-map.ts +430 -123
- package/src/data-structures/hash/hash-table.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +8 -5
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- 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/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +3 -3
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +5 -4
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/data-structures/tree/tree.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multimap.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +17 -1
- package/src/types/data-structures/hash/index.ts +7 -0
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +18 -4
- package/src/utils/utils.ts +16 -3
- package/test/config.ts +1 -1
- package/test/integration/all-in-one.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +19 -19
- package/test/integration/heap.test.js +1 -1
- package/test/integration/index.html +7 -7
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +4 -4
- package/test/performance/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +6 -6
- package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/performance/data-structures/hash/hash-map.test.ts +6 -6
- package/test/performance/data-structures/heap/heap.test.ts +5 -5
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -4
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +7 -5
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +7 -7
- package/test/performance/data-structures/queue/deque.test.ts +5 -5
- package/test/performance/data-structures/queue/queue.test.ts +6 -6
- package/test/performance/data-structures/stack/stack.test.ts +6 -6
- package/test/performance/data-structures/trie/trie.test.ts +4 -4
- package/test/performance/reportor.ts +15 -13
- package/test/performance/types/reportor.ts +1 -1
- package/test/types/utils/json2html.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -76
- package/test/unit/data-structures/binary-tree/bst.test.ts +50 -46
- package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +21 -21
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -37
- package/test/unit/data-structures/graph/abstract-graph.test.ts +9 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +34 -14
- package/test/unit/data-structures/graph/map-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/overall.test.ts +1 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
- package/test/unit/data-structures/hash/coordinate-map.test.ts +1 -1
- package/test/unit/data-structures/hash/coordinate-set.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +150 -21
- package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +41 -29
- package/test/unit/data-structures/heap/max-heap.test.ts +6 -6
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -6
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +6 -6
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
- package/test/unit/data-structures/linked-list/skip-list.test.ts +5 -5
- package/test/unit/data-structures/matrix/matrix.test.ts +5 -5
- package/test/unit/data-structures/matrix/matrix2d.test.ts +3 -3
- package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
- package/test/unit/data-structures/matrix/vector2d.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -7
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +19 -13
- package/test/unit/data-structures/queue/deque.test.ts +28 -28
- package/test/unit/data-structures/queue/queue.test.ts +3 -3
- package/test/unit/data-structures/stack/stack.test.ts +1 -1
- package/test/unit/data-structures/tree/tree.test.ts +1 -1
- package/test/unit/data-structures/trie/trie.test.ts +10 -10
- package/test/utils/array.ts +2 -2
- package/test/utils/big-o.ts +4 -4
- package/test/utils/json2html.ts +7 -3
- package/test/utils/number.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {AbstractEdge, AbstractGraph, AbstractVertex, VertexKey} from '../../../../src';
|
|
1
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex, VertexKey } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
class MyVertex<V = any> extends AbstractVertex<V> {
|
|
4
4
|
data?: V;
|
|
@@ -22,12 +22,12 @@ class MyEdge<E = any> extends AbstractEdge<E> {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
class MyGraph<
|
|
26
|
-
V,
|
|
27
|
-
E,
|
|
28
|
-
VO
|
|
29
|
-
EO
|
|
30
|
-
> {
|
|
25
|
+
class MyGraph<
|
|
26
|
+
V = any,
|
|
27
|
+
E = any,
|
|
28
|
+
VO extends MyVertex<V> = MyVertex<V>,
|
|
29
|
+
EO extends MyEdge<E> = MyEdge<E>
|
|
30
|
+
> extends AbstractGraph<V, E, VO, EO> {
|
|
31
31
|
createVertex(key: VertexKey, value?: V): VO {
|
|
32
32
|
return new MyVertex(key, value) as VO;
|
|
33
33
|
}
|
|
@@ -74,7 +74,8 @@ class MyGraph<V = any, E = any, VO extends MyVertex<V> = MyVertex<V>, EO extends
|
|
|
74
74
|
describe('AbstractGraph Operation Test', () => {
|
|
75
75
|
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
|
76
76
|
|
|
77
|
-
beforeEach(() => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
});
|
|
78
79
|
it('should edge cases', function () {
|
|
79
80
|
myGraph.addVertex('A', 1);
|
|
80
81
|
myGraph.addVertex('B', 2);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DirectedEdge, DirectedGraph, DirectedVertex, VertexKey} from '../../../../src';
|
|
1
|
+
import { DirectedEdge, DirectedGraph, DirectedVertex, VertexKey } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('DirectedGraph Operation Test', () => {
|
|
4
4
|
let graph: DirectedGraph;
|
|
@@ -126,12 +126,12 @@ class MyEdge<E = any> extends DirectedEdge<E> {
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
class MyDirectedGraph<
|
|
130
|
-
V,
|
|
131
|
-
E,
|
|
132
|
-
VO
|
|
133
|
-
EO
|
|
134
|
-
> {
|
|
129
|
+
class MyDirectedGraph<
|
|
130
|
+
V = any,
|
|
131
|
+
E = any,
|
|
132
|
+
VO extends MyVertex<V> = MyVertex<V>,
|
|
133
|
+
EO extends MyEdge<E> = MyEdge<E>
|
|
134
|
+
> extends DirectedGraph<V, E, VO, EO> {
|
|
135
135
|
createVertex(key: VertexKey, value: V): VO {
|
|
136
136
|
return new MyVertex(key, value) as VO;
|
|
137
137
|
}
|
|
@@ -319,7 +319,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
319
319
|
const fordResult1 = myGraph.bellmanFord(1);
|
|
320
320
|
expect(fordResult1).toBeTruthy();
|
|
321
321
|
expect(fordResult1.hasNegativeCycle).toBeUndefined();
|
|
322
|
-
const {distMap, preMap, paths, min, minPath} = fordResult1;
|
|
322
|
+
const { distMap, preMap, paths, min, minPath } = fordResult1;
|
|
323
323
|
expect(distMap).toBeInstanceOf(Map);
|
|
324
324
|
expect(distMap.size).toBe(9);
|
|
325
325
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -343,7 +343,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
343
343
|
const floydResult = myGraph.floydWarshall();
|
|
344
344
|
expect(floydResult).toBeTruthy();
|
|
345
345
|
if (floydResult) {
|
|
346
|
-
const {costs, predecessor} = floydResult;
|
|
346
|
+
const { costs, predecessor } = floydResult;
|
|
347
347
|
expect(costs).toBeInstanceOf(Array);
|
|
348
348
|
expect(costs.length).toBe(9);
|
|
349
349
|
expect(costs[0]).toEqual([32, 12, 35, 14, 70, Infinity, 61, Infinity, 19]);
|
|
@@ -351,9 +351,29 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
351
351
|
expect(costs[2]).toEqual([3, 15, 38, 17, 35, Infinity, 64, Infinity, 22]);
|
|
352
352
|
expect(costs[3]).toEqual([123, 135, 120, 137, 155, Infinity, 47, Infinity, 126]);
|
|
353
353
|
expect(costs[4]).toEqual([133, 145, 130, 147, 165, Infinity, 57, Infinity, 136]);
|
|
354
|
-
expect(costs[5]).toEqual([
|
|
354
|
+
expect(costs[5]).toEqual([
|
|
355
|
+
Infinity,
|
|
356
|
+
Infinity,
|
|
357
|
+
Infinity,
|
|
358
|
+
Infinity,
|
|
359
|
+
Infinity,
|
|
360
|
+
Infinity,
|
|
361
|
+
Infinity,
|
|
362
|
+
Infinity,
|
|
363
|
+
Infinity
|
|
364
|
+
]);
|
|
355
365
|
expect(costs[6]).toEqual([76, 88, 73, 90, 108, Infinity, 137, Infinity, 79]);
|
|
356
|
-
expect(costs[7]).toEqual([
|
|
366
|
+
expect(costs[7]).toEqual([
|
|
367
|
+
Infinity,
|
|
368
|
+
Infinity,
|
|
369
|
+
Infinity,
|
|
370
|
+
Infinity,
|
|
371
|
+
Infinity,
|
|
372
|
+
Infinity,
|
|
373
|
+
Infinity,
|
|
374
|
+
Infinity,
|
|
375
|
+
Infinity
|
|
376
|
+
]);
|
|
357
377
|
expect(costs[8]).toEqual([173, 185, 170, 187, 205, Infinity, 97, Infinity, 176]);
|
|
358
378
|
|
|
359
379
|
expect(predecessor).toBeInstanceOf(Array);
|
|
@@ -369,7 +389,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
369
389
|
|
|
370
390
|
expect(dijkstraRes12tt).toBeTruthy();
|
|
371
391
|
if (dijkstraRes12tt) {
|
|
372
|
-
const {distMap, minDist, minPath, paths} = dijkstraRes12tt;
|
|
392
|
+
const { distMap, minDist, minPath, paths } = dijkstraRes12tt;
|
|
373
393
|
expect(distMap).toBeInstanceOf(Map);
|
|
374
394
|
expect(distMap.size).toBe(9);
|
|
375
395
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -420,7 +440,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
420
440
|
|
|
421
441
|
expect(dijkstraRes1ntt).toBeTruthy();
|
|
422
442
|
if (dijkstraRes1ntt) {
|
|
423
|
-
const {distMap, minDist, minPath, paths} = dijkstraRes1ntt;
|
|
443
|
+
const { distMap, minDist, minPath, paths } = dijkstraRes1ntt;
|
|
424
444
|
expect(distMap).toBeInstanceOf(Map);
|
|
425
445
|
expect(distMap.size).toBe(9);
|
|
426
446
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -482,7 +502,7 @@ describe('Inherit from DirectedGraph and perform operations test2.', () => {
|
|
|
482
502
|
const dijkstraWithoutHeapRes1ntt = myGraph.dijkstraWithoutHeap(1, null, true, true);
|
|
483
503
|
expect(dijkstraWithoutHeapRes1ntt).toBeTruthy();
|
|
484
504
|
if (dijkstraWithoutHeapRes1ntt) {
|
|
485
|
-
const {distMap, minDist, minPath, paths} = dijkstraWithoutHeapRes1ntt;
|
|
505
|
+
const { distMap, minDist, minPath, paths } = dijkstraWithoutHeapRes1ntt;
|
|
486
506
|
expect(distMap).toBeInstanceOf(Map);
|
|
487
507
|
expect(distMap.size).toBe(9);
|
|
488
508
|
expect(distMap.get(vertex1)).toBe(0);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {UndirectedEdge, UndirectedGraph, UndirectedVertex} from '../../../../src';
|
|
1
|
+
import { UndirectedEdge, UndirectedGraph, UndirectedVertex } from '../../../../src';
|
|
2
2
|
import saltyVertexes from './salty-vertexes.json';
|
|
3
3
|
import saltyEdges from './salty-edges.json';
|
|
4
4
|
|
|
@@ -150,7 +150,7 @@ describe('UndirectedGraph', () => {
|
|
|
150
150
|
});
|
|
151
151
|
|
|
152
152
|
it('should getAllPathsBetween work well in 66 vertexes 97 edges graph', () => {
|
|
153
|
-
const graph = new UndirectedGraph<{name: string}, number>();
|
|
153
|
+
const graph = new UndirectedGraph<{ name: string }, number>();
|
|
154
154
|
for (const v of saltyVertexes) {
|
|
155
155
|
graph.addVertex(v.name, v);
|
|
156
156
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {HashMap} from '../../../../src';
|
|
1
|
+
import { HashMap } from '../../../../src';
|
|
2
|
+
import { getRandomInt, getRandomIntArray } from '../../../utils';
|
|
2
3
|
|
|
3
4
|
describe('HashMap', () => {
|
|
4
5
|
let hashMap: HashMap<string, number>;
|
|
@@ -9,10 +10,10 @@ describe('HashMap', () => {
|
|
|
9
10
|
|
|
10
11
|
it('should initialize correctly', () => {
|
|
11
12
|
expect(hashMap.size).toBe(0);
|
|
12
|
-
expect(hashMap.table.length).toBe(16);
|
|
13
|
-
expect(hashMap.loadFactor).toBe(0.75);
|
|
14
|
-
expect(hashMap.capacityMultiplier).toBe(2);
|
|
15
|
-
expect(hashMap.initialCapacity).toBe(16);
|
|
13
|
+
// expect(hashMap.table.length).toBe(16);
|
|
14
|
+
// expect(hashMap.loadFactor).toBe(0.75);
|
|
15
|
+
// expect(hashMap.capacityMultiplier).toBe(2);
|
|
16
|
+
// expect(hashMap.initialCapacity).toBe(16);
|
|
16
17
|
expect(hashMap.isEmpty()).toBe(true);
|
|
17
18
|
});
|
|
18
19
|
|
|
@@ -58,26 +59,22 @@ describe('HashMap', () => {
|
|
|
58
59
|
hashMap.set('two', 2);
|
|
59
60
|
hashMap.set('three', 3);
|
|
60
61
|
|
|
61
|
-
const entries = Array.from(hashMap.entries());
|
|
62
|
-
expect(entries).
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
['two', 2],
|
|
66
|
-
['three', 3]
|
|
67
|
-
])
|
|
68
|
-
);
|
|
62
|
+
// const entries = Array.from(hashMap.entries());
|
|
63
|
+
// expect(entries).toContainEqual(['one', 1]);
|
|
64
|
+
// expect(entries).toContainEqual(['two', 2]);
|
|
65
|
+
// expect(entries).toContainEqual(['three', 3]);
|
|
69
66
|
});
|
|
70
67
|
|
|
71
68
|
it('should resize the table when load factor is exceeded', () => {
|
|
72
69
|
// Set a small initial capacity for testing resizing
|
|
73
|
-
hashMap = new HashMap<string, number>(
|
|
70
|
+
hashMap = new HashMap<string, number>();
|
|
74
71
|
|
|
75
72
|
hashMap.set('one', 1);
|
|
76
73
|
hashMap.set('two', 2);
|
|
77
74
|
hashMap.set('three', 3);
|
|
78
75
|
hashMap.set('four', 4); // This should trigger a resize
|
|
79
76
|
|
|
80
|
-
expect(hashMap.table.length).toBe(8);
|
|
77
|
+
// expect(hashMap.table.length).toBe(8);
|
|
81
78
|
expect(hashMap.get('one')).toBe(1);
|
|
82
79
|
expect(hashMap.get('two')).toBe(2);
|
|
83
80
|
expect(hashMap.get('three')).toBe(3);
|
|
@@ -85,11 +82,7 @@ describe('HashMap', () => {
|
|
|
85
82
|
});
|
|
86
83
|
|
|
87
84
|
it('should allow using a custom hash function', () => {
|
|
88
|
-
|
|
89
|
-
// A simple custom hash function that always returns 0
|
|
90
|
-
return 0;
|
|
91
|
-
};
|
|
92
|
-
hashMap = new HashMap<string, number>(16, 0.75, customHashFn);
|
|
85
|
+
hashMap = new HashMap<string, number>();
|
|
93
86
|
|
|
94
87
|
hashMap.set('one', 1);
|
|
95
88
|
hashMap.set('two', 2);
|
|
@@ -98,6 +91,142 @@ describe('HashMap', () => {
|
|
|
98
91
|
expect(hashMap.get('two')).toBe(2);
|
|
99
92
|
// Since the custom hash function always returns 0, these keys will collide.
|
|
100
93
|
// Make sure they are stored separately.
|
|
101
|
-
expect(hashMap.table[0].length).toBe(2);
|
|
94
|
+
// expect(hashMap.table[0].length).toBe(2);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('HashMap', () => {
|
|
99
|
+
let hashMap: HashMap;
|
|
100
|
+
|
|
101
|
+
beforeEach(() => {
|
|
102
|
+
hashMap = new HashMap();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should create an empty map', () => {
|
|
106
|
+
expect(hashMap.size).toBe(0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should add a key-value pair', () => {
|
|
110
|
+
hashMap.set('key1', 'value1');
|
|
111
|
+
expect(hashMap.get('key1')).toBe('value1');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should handle object keys correctly', () => {
|
|
115
|
+
const keyObj = { id: 1 };
|
|
116
|
+
hashMap.set(keyObj, 'objectValue');
|
|
117
|
+
expect(hashMap.get(keyObj)).toBe('objectValue');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should handle number keys correctly', () => {
|
|
121
|
+
hashMap.set(999, { a: '999Value' });
|
|
122
|
+
expect(hashMap.get(999)).toEqual({ a: '999Value' });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should update the value for an existing key', () => {
|
|
126
|
+
hashMap.set('key1', 'value1');
|
|
127
|
+
hashMap.set('key1', 'newValue');
|
|
128
|
+
expect(hashMap.get('key1')).toBe('newValue');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should return undefined for a non-existent key', () => {
|
|
132
|
+
expect(hashMap.get('nonExistentKey')).toBeUndefined();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should remove a key-value pair', () => {
|
|
136
|
+
hashMap.set('key1', 'value1');
|
|
137
|
+
hashMap.delete('key1');
|
|
138
|
+
expect(hashMap.get('key1')).toBeUndefined();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should clear the map', () => {
|
|
142
|
+
hashMap.set('key1', 'value1');
|
|
143
|
+
expect(hashMap.size).toBe(1);
|
|
144
|
+
|
|
145
|
+
hashMap.clear();
|
|
146
|
+
expect(hashMap.size).toBe(0);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('should iterate over values', () => {
|
|
150
|
+
hashMap.set('key1', 'value1');
|
|
151
|
+
hashMap.set('key2', 'value2');
|
|
152
|
+
const values = [];
|
|
153
|
+
for (const value of hashMap) {
|
|
154
|
+
values.push(value);
|
|
155
|
+
}
|
|
156
|
+
expect(values).toEqual([
|
|
157
|
+
['key1', 'value1'],
|
|
158
|
+
['key2', 'value2']
|
|
159
|
+
]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// test('should delete element at specific index', () => {
|
|
163
|
+
// hashMap.set('key1', 'value1');
|
|
164
|
+
// hashMap.set('key2', 'value2');
|
|
165
|
+
// hashMap.deleteAt(0);
|
|
166
|
+
// expect(hashMap.get('key1')).toBeUndefined();
|
|
167
|
+
// expect(hashMap.size).toBe(1);
|
|
168
|
+
// });
|
|
169
|
+
function compareHashMaps(hashMap: HashMap<unknown, unknown>, stdMap: Map<unknown, unknown>) {
|
|
170
|
+
expect(hashMap.size).toEqual(stdMap.size);
|
|
171
|
+
let index = 0;
|
|
172
|
+
stdMap.forEach((value, key) => {
|
|
173
|
+
if (index === 0) {
|
|
174
|
+
expect(hashMap.front).toEqual([key, value]);
|
|
175
|
+
expect(hashMap.begin.current[0]).toEqual(key);
|
|
176
|
+
} else if (index === hashMap.size - 1) {
|
|
177
|
+
expect(hashMap.back).toEqual([key, value]);
|
|
178
|
+
expect(hashMap.reverseBegin.current[0]).toEqual(key);
|
|
179
|
+
} else if (index <= 1000) {
|
|
180
|
+
expect(hashMap.getAt(index)).toEqual([key, value]);
|
|
181
|
+
}
|
|
182
|
+
expect(hashMap.get(key)).toEqual(value);
|
|
183
|
+
expect(hashMap.getIterator(key).current[1]).toEqual(value);
|
|
184
|
+
index++;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const stdMap: Map<unknown, unknown> = new Map();
|
|
189
|
+
const arr: number[] = getRandomIntArray(1000, 1, 10000);
|
|
190
|
+
|
|
191
|
+
it('delete test', () => {
|
|
192
|
+
for (const item of arr) {
|
|
193
|
+
stdMap.set(item, item);
|
|
194
|
+
hashMap.set(item, item);
|
|
195
|
+
}
|
|
196
|
+
for (const item of arr) {
|
|
197
|
+
if (Math.random() > 0.6) {
|
|
198
|
+
expect(hashMap.delete(item)).toEqual(stdMap.delete(item));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
compareHashMaps(hashMap, stdMap);
|
|
202
|
+
|
|
203
|
+
for (let i = 0; i < 1000; ++i) {
|
|
204
|
+
const random = getRandomInt(0, 100);
|
|
205
|
+
expect(hashMap.delete(random)).toEqual(stdMap.delete(random));
|
|
206
|
+
}
|
|
207
|
+
compareHashMaps(hashMap, stdMap);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('should iterate correctly with reverse iterators', () => {
|
|
211
|
+
hashMap.set('key1', 'value1');
|
|
212
|
+
hashMap.set('key2', 'value2');
|
|
213
|
+
const iterator = hashMap.reverseBegin;
|
|
214
|
+
expect(iterator.next().current).toEqual(['key1', 'value1']);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test('should return the last element', () => {
|
|
218
|
+
hashMap.set('key1', 'value1');
|
|
219
|
+
hashMap.set('key2', 'value2');
|
|
220
|
+
expect(hashMap.back).toEqual(['key2', 'value2']);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('should return undefined for empty map', () => {
|
|
224
|
+
expect(hashMap.back).toBeUndefined();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('should get element at specific index', () => {
|
|
228
|
+
hashMap.set('key1', 'value1');
|
|
229
|
+
hashMap.set('key2', 'value2');
|
|
230
|
+
expect(hashMap.getAt(1)).toEqual(['key2', 'value2']);
|
|
102
231
|
});
|
|
103
232
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {FibonacciHeap, MaxHeap, MinHeap} from '../../../../src';
|
|
2
|
-
import {logBigOMetricsWrap} from '../../../utils';
|
|
1
|
+
import { FibonacciHeap, MaxHeap, MinHeap } from '../../../../src';
|
|
2
|
+
import { logBigOMetricsWrap } from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('Heap Operation Test', () => {
|
|
5
5
|
it('should numeric heap work well', function () {
|
|
@@ -22,34 +22,46 @@ describe('Heap Operation Test', () => {
|
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('should object heap work well', function () {
|
|
25
|
-
const minHeap = new MinHeap<{a: string; key: number}>({comparator: (a, b) => a.key - b.key});
|
|
26
|
-
minHeap.add({key: 1, a: 'a1'});
|
|
27
|
-
minHeap.add({key: 6, a: 'a6'});
|
|
28
|
-
minHeap.add({key: 2, a: 'a2'});
|
|
29
|
-
minHeap.add({key: 0, a: 'a0'});
|
|
30
|
-
|
|
31
|
-
expect(minHeap.peek()).toEqual({a: 'a0', key: 0});
|
|
32
|
-
expect(minHeap.toArray().map(item => ({a: item.a}))).toEqual([
|
|
25
|
+
const minHeap = new MinHeap<{ a: string; key: number }>({ comparator: (a, b) => a.key - b.key });
|
|
26
|
+
minHeap.add({ key: 1, a: 'a1' });
|
|
27
|
+
minHeap.add({ key: 6, a: 'a6' });
|
|
28
|
+
minHeap.add({ key: 2, a: 'a2' });
|
|
29
|
+
minHeap.add({ key: 0, a: 'a0' });
|
|
30
|
+
|
|
31
|
+
expect(minHeap.peek()).toEqual({ a: 'a0', key: 0 });
|
|
32
|
+
expect(minHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
33
|
+
{ a: 'a0' },
|
|
34
|
+
{ a: 'a1' },
|
|
35
|
+
{ a: 'a2' },
|
|
36
|
+
{ a: 'a6' }
|
|
37
|
+
]);
|
|
33
38
|
let i = 0;
|
|
34
|
-
const expectPolled = [{a: 'a0'}, {a: 'a1'}, {a: 'a2'}, {a: 'a6'}];
|
|
39
|
+
const expectPolled = [{ a: 'a0' }, { a: 'a1' }, { a: 'a2' }, { a: 'a6' }];
|
|
35
40
|
while (minHeap.size > 0) {
|
|
36
|
-
expect({a: minHeap.poll()?.a}).toEqual(expectPolled[i]);
|
|
41
|
+
expect({ a: minHeap.poll()?.a }).toEqual(expectPolled[i]);
|
|
37
42
|
i++;
|
|
38
43
|
}
|
|
39
44
|
|
|
40
|
-
const maxHeap = new MaxHeap<{key: number; a: string}>({comparator: (a, b) => b.key - a.key});
|
|
41
|
-
maxHeap.add({key: 1, a: 'a1'});
|
|
42
|
-
maxHeap.add({key: 6, a: 'a6'});
|
|
43
|
-
maxHeap.add({key: 5, a: 'a5'});
|
|
44
|
-
maxHeap.add({key: 2, a: 'a2'});
|
|
45
|
-
maxHeap.add({key: 0, a: 'a0'});
|
|
46
|
-
maxHeap.add({key: 9, a: 'a9'});
|
|
47
|
-
expect(maxHeap.peek()).toEqual({a: 'a9', key: 9});
|
|
48
|
-
expect(maxHeap.toArray().map(item => ({a: item.a}))).toEqual([
|
|
49
|
-
|
|
45
|
+
const maxHeap = new MaxHeap<{ key: number; a: string }>({ comparator: (a, b) => b.key - a.key });
|
|
46
|
+
maxHeap.add({ key: 1, a: 'a1' });
|
|
47
|
+
maxHeap.add({ key: 6, a: 'a6' });
|
|
48
|
+
maxHeap.add({ key: 5, a: 'a5' });
|
|
49
|
+
maxHeap.add({ key: 2, a: 'a2' });
|
|
50
|
+
maxHeap.add({ key: 0, a: 'a0' });
|
|
51
|
+
maxHeap.add({ key: 9, a: 'a9' });
|
|
52
|
+
expect(maxHeap.peek()).toEqual({ a: 'a9', key: 9 });
|
|
53
|
+
expect(maxHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
54
|
+
{ a: 'a9' },
|
|
55
|
+
{ a: 'a2' },
|
|
56
|
+
{ a: 'a6' },
|
|
57
|
+
{ a: 'a1' },
|
|
58
|
+
{ a: 'a0' },
|
|
59
|
+
{ a: 'a5' }
|
|
60
|
+
]);
|
|
61
|
+
const maxExpectPolled = [{ a: 'a9' }, { a: 'a6' }, { a: 'a5' }, { a: 'a2' }, { a: 'a1' }, { a: 'a0' }];
|
|
50
62
|
let maxI = 0;
|
|
51
63
|
while (maxHeap.size > 0) {
|
|
52
|
-
expect({a: maxHeap.poll()?.a}).toEqual(maxExpectPolled[maxI]);
|
|
64
|
+
expect({ a: maxHeap.poll()?.a }).toEqual(maxExpectPolled[maxI]);
|
|
53
65
|
maxI++;
|
|
54
66
|
}
|
|
55
67
|
});
|
|
@@ -62,13 +74,13 @@ describe('FibonacciHeap', () => {
|
|
|
62
74
|
heap = new FibonacciHeap<number>();
|
|
63
75
|
});
|
|
64
76
|
|
|
65
|
-
|
|
77
|
+
it('push & peek', () => {
|
|
66
78
|
heap.push(10);
|
|
67
79
|
heap.push(5);
|
|
68
80
|
expect(heap.peek()).toBe(5);
|
|
69
81
|
});
|
|
70
82
|
|
|
71
|
-
|
|
83
|
+
it('pop', () => {
|
|
72
84
|
heap.push(10);
|
|
73
85
|
heap.push(5);
|
|
74
86
|
heap.push(15);
|
|
@@ -77,11 +89,11 @@ describe('FibonacciHeap', () => {
|
|
|
77
89
|
expect(heap.pop()).toBe(15);
|
|
78
90
|
});
|
|
79
91
|
|
|
80
|
-
|
|
92
|
+
it('pop on an empty heap', () => {
|
|
81
93
|
expect(heap.pop()).toBeUndefined();
|
|
82
94
|
});
|
|
83
95
|
|
|
84
|
-
|
|
96
|
+
it('size', () => {
|
|
85
97
|
expect(heap.size).toBe(0);
|
|
86
98
|
heap.push(10);
|
|
87
99
|
expect(heap.size).toBe(1);
|
|
@@ -89,7 +101,7 @@ describe('FibonacciHeap', () => {
|
|
|
89
101
|
expect(heap.size).toBe(0);
|
|
90
102
|
});
|
|
91
103
|
|
|
92
|
-
|
|
104
|
+
it('clear', () => {
|
|
93
105
|
heap.push(10);
|
|
94
106
|
heap.push(5);
|
|
95
107
|
heap.clear();
|
|
@@ -97,7 +109,7 @@ describe('FibonacciHeap', () => {
|
|
|
97
109
|
expect(heap.peek()).toBeUndefined();
|
|
98
110
|
});
|
|
99
111
|
|
|
100
|
-
|
|
112
|
+
it('custom comparator', () => {
|
|
101
113
|
const maxHeap = new FibonacciHeap<number>((a, b) => b - a);
|
|
102
114
|
maxHeap.push(10);
|
|
103
115
|
maxHeap.push(5);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {Comparator, MaxHeap} from '../../../../src';
|
|
1
|
+
import { Comparator, MaxHeap } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MaxHeap', () => {
|
|
4
4
|
const numberComparator: Comparator<number> = (a, b) => b - a;
|
|
5
5
|
let maxHeap: MaxHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
maxHeap = new MaxHeap({comparator: numberComparator});
|
|
8
|
+
maxHeap = new MaxHeap({ comparator: numberComparator });
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
it('add and poll elements in descending order', () => {
|
|
12
12
|
maxHeap.add(3);
|
|
13
13
|
maxHeap.add(1);
|
|
14
14
|
maxHeap.add(4);
|
|
@@ -20,7 +20,7 @@ describe('MaxHeap', () => {
|
|
|
20
20
|
expect(maxHeap.poll()).toBe(1);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
it('peek at the top element without removing it', () => {
|
|
24
24
|
maxHeap.add(3);
|
|
25
25
|
maxHeap.add(1);
|
|
26
26
|
maxHeap.add(4);
|
|
@@ -30,7 +30,7 @@ describe('MaxHeap', () => {
|
|
|
30
30
|
expect(maxHeap.size).toBe(4);
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
it('sort elements in descending order', () => {
|
|
34
34
|
maxHeap.add(3);
|
|
35
35
|
maxHeap.add(1);
|
|
36
36
|
maxHeap.add(4);
|
|
@@ -40,7 +40,7 @@ describe('MaxHeap', () => {
|
|
|
40
40
|
expect(sortedArray).toEqual([4, 3, 2, 1]);
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
it('check if the heap is empty', () => {
|
|
44
44
|
expect(maxHeap.isEmpty()).toBe(true);
|
|
45
45
|
|
|
46
46
|
maxHeap.add(5);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {Comparator, MinHeap} from '../../../../src';
|
|
1
|
+
import { Comparator, MinHeap } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MinHeap', () => {
|
|
4
4
|
const numberComparator: Comparator<number> = (a, b) => a - b;
|
|
5
5
|
let minHeap: MinHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
minHeap = new MinHeap({comparator: numberComparator});
|
|
8
|
+
minHeap = new MinHeap({ comparator: numberComparator });
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
it('add and poll elements in ascending order', () => {
|
|
12
12
|
minHeap.add(3);
|
|
13
13
|
minHeap.add(1);
|
|
14
14
|
minHeap.add(4);
|
|
@@ -20,7 +20,7 @@ describe('MinHeap', () => {
|
|
|
20
20
|
expect(minHeap.poll()).toBe(4);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
it('peek at the top element without removing it', () => {
|
|
24
24
|
minHeap.add(3);
|
|
25
25
|
minHeap.add(1);
|
|
26
26
|
minHeap.add(4);
|
|
@@ -30,7 +30,7 @@ describe('MinHeap', () => {
|
|
|
30
30
|
expect(minHeap.size).toBe(4);
|
|
31
31
|
});
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
it('sort elements in ascending order', () => {
|
|
34
34
|
minHeap.add(3);
|
|
35
35
|
minHeap.add(1);
|
|
36
36
|
minHeap.add(4);
|
|
@@ -40,7 +40,7 @@ describe('MinHeap', () => {
|
|
|
40
40
|
expect(sortedArray).toEqual([1, 2, 3, 4]);
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
it('check if the heap is empty', () => {
|
|
44
44
|
expect(minHeap.isEmpty()).toBe(true);
|
|
45
45
|
|
|
46
46
|
minHeap.add(5);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src';
|
|
1
|
+
import { DoublyLinkedList, DoublyLinkedListNode } from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('DoublyLinkedListNode', () => {
|
|
4
4
|
it('should DoublyLinkedListNode', () => {
|
|
@@ -60,7 +60,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
60
60
|
|
|
61
61
|
describe('DoublyLinkedList Operation Test', () => {
|
|
62
62
|
let list: DoublyLinkedList<number>;
|
|
63
|
-
let objectList: DoublyLinkedList<{keyA: number}>;
|
|
63
|
+
let objectList: DoublyLinkedList<{ keyA: number }>;
|
|
64
64
|
|
|
65
65
|
beforeEach(() => {
|
|
66
66
|
list = new DoublyLinkedList();
|
|
@@ -370,9 +370,9 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
370
370
|
});
|
|
371
371
|
|
|
372
372
|
it('should insert and manipulate objects with numeric properties', () => {
|
|
373
|
-
const obj1 = {keyA: 10};
|
|
374
|
-
const obj2 = {keyA: 20};
|
|
375
|
-
const obj3 = {keyA: 30};
|
|
373
|
+
const obj1 = { keyA: 10 };
|
|
374
|
+
const obj2 = { keyA: 20 };
|
|
375
|
+
const obj3 = { keyA: 30 };
|
|
376
376
|
|
|
377
377
|
objectList.push(obj1);
|
|
378
378
|
objectList.push(obj2);
|
|
@@ -380,7 +380,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
380
380
|
|
|
381
381
|
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
382
382
|
|
|
383
|
-
const newObj = {keyA: 25}; // Corrected newObj value
|
|
383
|
+
const newObj = { keyA: 25 }; // Corrected newObj value
|
|
384
384
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
385
385
|
expect(insertSuccess).toBe(true);
|
|
386
386
|
|