data-structure-typed 1.52.0 → 1.52.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +141 -54
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +162 -162
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +97 -97
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +52 -52
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +35 -35
- package/dist/cjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.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 +2 -1
- package/dist/cjs/data-structures/graph/directed-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.js.map +1 -1
- package/dist/cjs/data-structures/heap/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/queue/deque.d.ts +7 -0
- package/dist/cjs/data-structures/queue/deque.js +16 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +18 -1
- package/dist/cjs/data-structures/queue/queue.js +32 -7
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +1 -22
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -0
- package/dist/cjs/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/types/data-structures/queue/queue.d.ts +3 -1
- package/dist/cjs/utils/number.js +1 -2
- package/dist/cjs/utils/number.js.map +1 -1
- package/dist/cjs/utils/utils.js +2 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +11 -11
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +97 -97
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +52 -52
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +35 -35
- package/dist/mjs/data-structures/binary-tree/bst.js +17 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +5 -5
- package/dist/mjs/data-structures/graph/directed-graph.js +2 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +7 -0
- package/dist/mjs/data-structures/queue/deque.js +16 -1
- package/dist/mjs/data-structures/queue/queue.d.ts +18 -1
- package/dist/mjs/data-structures/queue/queue.js +32 -7
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +1 -22
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -0
- package/dist/mjs/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/types/data-structures/queue/queue.d.ts +3 -1
- package/dist/umd/data-structure-typed.js +137 -95
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +8 -8
- package/src/data-structures/base/iterable-element-base.ts +2 -2
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -24
- package/src/data-structures/binary-tree/avl-tree.ts +20 -19
- package/src/data-structures/binary-tree/binary-tree.ts +162 -157
- package/src/data-structures/binary-tree/bst.ts +54 -50
- package/src/data-structures/binary-tree/rb-tree.ts +18 -17
- package/src/data-structures/binary-tree/tree-multi-map.ts +18 -17
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +9 -7
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +4 -4
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +18 -4
- package/src/data-structures/queue/queue.ts +38 -8
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -24
- package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
- package/src/types/data-structures/binary-tree/bst.ts +7 -0
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/queue/deque.ts +4 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/utils/utils.ts +4 -4
- package/test/integration/bst.test.ts +4 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +12 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +6 -2
- package/test/performance/reportor.ts +23 -8
- package/test/performance/types/reportor.ts +5 -1
- package/test/types/utils/json2html.ts +5 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +6 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +36 -11
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -1
- package/test/unit/data-structures/binary-tree/bst.test.ts +18 -5
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +27 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +6 -2
- package/test/unit/data-structures/heap/heap.test.ts +64 -12
- package/test/unit/data-structures/heap/max-heap.test.ts +10 -3
- package/test/unit/data-structures/heap/min-heap.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +3 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +6 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +22 -6
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +12 -4
- package/test/unit/data-structures/queue/deque.test.ts +31 -6
- package/test/unit/data-structures/queue/queue.test.ts +14 -1
- package/test/unit/data-structures/trie/trie.test.ts +3 -1
- package/test/unit/unrestricted-interconversion.test.ts +3 -1
- package/test/utils/big-o.ts +3 -1
- package/test/utils/json2html.ts +2 -6
|
@@ -8,13 +8,18 @@ describe('BinaryIndexedTree simple', () => {
|
|
|
8
8
|
|
|
9
9
|
beforeEach(() => {
|
|
10
10
|
//Create a new BinaryIndexedTree instance before each test case
|
|
11
|
-
bit = new BinaryIndexedTree({
|
|
11
|
+
bit = new BinaryIndexedTree({
|
|
12
|
+
frequency: 0,
|
|
13
|
+
max: 10
|
|
14
|
+
}); // Modify the value of max as needed
|
|
12
15
|
});
|
|
13
16
|
|
|
14
17
|
it('should initialize correctly', () => {
|
|
15
18
|
expect(bit.freq).toBe(0);
|
|
16
19
|
expect(bit.max).toBe(10);
|
|
17
|
-
expect(bit.freqMap).toEqual({
|
|
20
|
+
expect(bit.freqMap).toEqual({
|
|
21
|
+
0: 0
|
|
22
|
+
}); // Modify the initialized record value according to the actual situation
|
|
18
23
|
// More initialization checks can be added
|
|
19
24
|
});
|
|
20
25
|
|
|
@@ -54,7 +59,10 @@ describe('BinaryIndexedTree', () => {
|
|
|
54
59
|
let bit: BinaryIndexedTree;
|
|
55
60
|
|
|
56
61
|
beforeEach(function () {
|
|
57
|
-
bit = new BinaryIndexedTree({
|
|
62
|
+
bit = new BinaryIndexedTree({
|
|
63
|
+
frequency,
|
|
64
|
+
max
|
|
65
|
+
});
|
|
58
66
|
});
|
|
59
67
|
it('should validate the index', function () {
|
|
60
68
|
expect(() => bit.readSingle(-1)).toThrow('Index out of range');
|
|
@@ -73,7 +81,10 @@ describe('BinaryIndexedTree', () => {
|
|
|
73
81
|
it('should frequency and max', function () {
|
|
74
82
|
const frequency = 200;
|
|
75
83
|
const max = 1000;
|
|
76
|
-
const bit = new BinaryIndexedTree({
|
|
84
|
+
const bit = new BinaryIndexedTree({
|
|
85
|
+
frequency,
|
|
86
|
+
max
|
|
87
|
+
});
|
|
77
88
|
|
|
78
89
|
expect(bit.freq).toBe(frequency);
|
|
79
90
|
expect(bit.max).toBe(max);
|
|
@@ -123,7 +134,9 @@ describe('designated values', function () {
|
|
|
123
134
|
let bit: BinaryIndexedTree;
|
|
124
135
|
|
|
125
136
|
beforeEach(function () {
|
|
126
|
-
bit = new BinaryIndexedTree({
|
|
137
|
+
bit = new BinaryIndexedTree({
|
|
138
|
+
max: array.length
|
|
139
|
+
});
|
|
127
140
|
array.forEach((value, i) => bit.writeSingle(i, value));
|
|
128
141
|
});
|
|
129
142
|
|
|
@@ -182,7 +195,9 @@ describe('descending sequence', function () {
|
|
|
182
195
|
let bit: BinaryIndexedTree;
|
|
183
196
|
|
|
184
197
|
beforeEach(function () {
|
|
185
|
-
bit = new BinaryIndexedTree({
|
|
198
|
+
bit = new BinaryIndexedTree({
|
|
199
|
+
max: array.length
|
|
200
|
+
});
|
|
186
201
|
array.forEach((value, i) => bit.writeSingle(i, value));
|
|
187
202
|
});
|
|
188
203
|
|
|
@@ -219,7 +234,9 @@ describe('descending sequence', function () {
|
|
|
219
234
|
|
|
220
235
|
describe('BinaryIndexedTree additional tests', () => {
|
|
221
236
|
it('should handle read method correctly', () => {
|
|
222
|
-
const bit = new BinaryIndexedTree({
|
|
237
|
+
const bit = new BinaryIndexedTree({
|
|
238
|
+
max: 10
|
|
239
|
+
});
|
|
223
240
|
bit.writeSingle(2, 10);
|
|
224
241
|
bit.writeSingle(5, 20);
|
|
225
242
|
bit.writeSingle(8, 30);
|
|
@@ -227,7 +244,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
227
244
|
});
|
|
228
245
|
|
|
229
246
|
it('should handle consecutive operations', () => {
|
|
230
|
-
const bit = new BinaryIndexedTree({
|
|
247
|
+
const bit = new BinaryIndexedTree({
|
|
248
|
+
max: 10
|
|
249
|
+
});
|
|
231
250
|
bit.writeSingle(2, 10);
|
|
232
251
|
bit.update(2, 5);
|
|
233
252
|
expect(bit.readSingle(2)).toBe(15);
|
|
@@ -237,7 +256,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
237
256
|
});
|
|
238
257
|
|
|
239
258
|
it('should handle frequent increment updates', () => {
|
|
240
|
-
const bit = new BinaryIndexedTree({
|
|
259
|
+
const bit = new BinaryIndexedTree({
|
|
260
|
+
max: 10
|
|
261
|
+
});
|
|
241
262
|
for (let i = 0; i < 10; i++) {
|
|
242
263
|
bit.update(2, 5);
|
|
243
264
|
}
|
|
@@ -245,7 +266,9 @@ describe('BinaryIndexedTree additional tests', () => {
|
|
|
245
266
|
});
|
|
246
267
|
|
|
247
268
|
it('should handle edge cases', () => {
|
|
248
|
-
const bit = new BinaryIndexedTree({
|
|
269
|
+
const bit = new BinaryIndexedTree({
|
|
270
|
+
max: 10
|
|
271
|
+
});
|
|
249
272
|
bit.writeSingle(9, 100);
|
|
250
273
|
expect(bit.readSingle(9)).toBe(100);
|
|
251
274
|
expect(bit.lowerBound(200)).toBe(10);
|
|
@@ -291,7 +314,9 @@ describe('', () => {
|
|
|
291
314
|
|
|
292
315
|
constructor(nums: number[]) {
|
|
293
316
|
this._nums = nums;
|
|
294
|
-
this._tree = new BinaryIndexedTree({
|
|
317
|
+
this._tree = new BinaryIndexedTree({
|
|
318
|
+
max: nums.length + 1
|
|
319
|
+
});
|
|
295
320
|
for (let i = 0; i < nums.length; i++) {
|
|
296
321
|
this._tree.update(i + 1, nums[i]);
|
|
297
322
|
}
|
|
@@ -570,7 +570,9 @@ describe('BinaryTree', () => {
|
|
|
570
570
|
let tree: BinaryTree<number, string>;
|
|
571
571
|
|
|
572
572
|
beforeEach(() => {
|
|
573
|
-
tree = new BinaryTree<number, string>([], {
|
|
573
|
+
tree = new BinaryTree<number, string>([], {
|
|
574
|
+
iterationType: 'RECURSIVE'
|
|
575
|
+
});
|
|
574
576
|
});
|
|
575
577
|
|
|
576
578
|
afterEach(() => {
|
|
@@ -252,7 +252,10 @@ describe('BST operations test', () => {
|
|
|
252
252
|
expect(leftMost?.key).toBe(1);
|
|
253
253
|
|
|
254
254
|
const node15 = objBST.getNode(15);
|
|
255
|
-
expect(node15?.value).toEqual({
|
|
255
|
+
expect(node15?.value).toEqual({
|
|
256
|
+
name: 'Alice',
|
|
257
|
+
age: 15
|
|
258
|
+
});
|
|
256
259
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
257
260
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
258
261
|
|
|
@@ -411,7 +414,9 @@ describe('BST operations test', () => {
|
|
|
411
414
|
|
|
412
415
|
describe('BST operations test recursively', () => {
|
|
413
416
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
414
|
-
const bst = new BST<number>([], {
|
|
417
|
+
const bst = new BST<number>([], {
|
|
418
|
+
iterationType: 'RECURSIVE'
|
|
419
|
+
});
|
|
415
420
|
expect(bst).toBeInstanceOf(BST);
|
|
416
421
|
bst.add([11, 11]);
|
|
417
422
|
bst.add([3, 3]);
|
|
@@ -626,7 +631,10 @@ describe('BST operations test recursively', () => {
|
|
|
626
631
|
expect(objBST.has(6)).toBe(true);
|
|
627
632
|
|
|
628
633
|
const node6 = objBST.getNode(6);
|
|
629
|
-
expect(objBST.get(6)).toEqual({
|
|
634
|
+
expect(objBST.get(6)).toEqual({
|
|
635
|
+
key: 6,
|
|
636
|
+
keyA: 6
|
|
637
|
+
});
|
|
630
638
|
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
631
639
|
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
632
640
|
|
|
@@ -640,7 +648,10 @@ describe('BST operations test recursively', () => {
|
|
|
640
648
|
expect(leftMost?.key).toBe(1);
|
|
641
649
|
|
|
642
650
|
const node15 = objBST.getNode(15);
|
|
643
|
-
expect(node15?.value).toEqual({
|
|
651
|
+
expect(node15?.value).toEqual({
|
|
652
|
+
key: 15,
|
|
653
|
+
keyA: 15
|
|
654
|
+
});
|
|
644
655
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
645
656
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
646
657
|
|
|
@@ -869,7 +880,9 @@ describe('BST isBST', function () {
|
|
|
869
880
|
});
|
|
870
881
|
|
|
871
882
|
test('isBST when variant is Max', () => {
|
|
872
|
-
const bst = new BST<number, number>([1, 2, 3, 9, 8, 5, 6, 7, 4], {
|
|
883
|
+
const bst = new BST<number, number>([1, 2, 3, 9, 8, 5, 6, 7, 4], {
|
|
884
|
+
comparator: (a, b) => b - a
|
|
885
|
+
});
|
|
873
886
|
bst.addMany([1, 2, 3, 9, 8, 5, 6, 7, 4]);
|
|
874
887
|
expect(bst.isBST()).toBe(true);
|
|
875
888
|
});
|
|
@@ -350,7 +350,9 @@ describe('TreeMultiMap operations test1', () => {
|
|
|
350
350
|
|
|
351
351
|
describe('TreeMultiMap operations test recursively1', () => {
|
|
352
352
|
it('should perform various operations on a Binary Search Tree with numeric values1', () => {
|
|
353
|
-
const tmm = new TreeMultiMap<number>([], {
|
|
353
|
+
const tmm = new TreeMultiMap<number>([], {
|
|
354
|
+
iterationType: 'RECURSIVE'
|
|
355
|
+
});
|
|
354
356
|
|
|
355
357
|
expect(tmm instanceof TreeMultiMap);
|
|
356
358
|
tmm.add([11, 11]);
|
|
@@ -81,8 +81,7 @@ class MyGraph<
|
|
|
81
81
|
return true;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
clear(): void {
|
|
85
|
-
}
|
|
84
|
+
clear(): void {}
|
|
86
85
|
|
|
87
86
|
clone(): any {
|
|
88
87
|
return {};
|
|
@@ -96,8 +95,7 @@ class MyGraph<
|
|
|
96
95
|
describe('AbstractGraph Operation Test', () => {
|
|
97
96
|
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
|
98
97
|
|
|
99
|
-
beforeEach(() => {
|
|
100
|
-
});
|
|
98
|
+
beforeEach(() => {});
|
|
101
99
|
it('should edge cases', function () {
|
|
102
100
|
myGraph.addVertex('A', 1);
|
|
103
101
|
myGraph.addVertex('B', 2);
|
|
@@ -995,3 +995,30 @@ describe('DirectedGraph tarjan', () => {
|
|
|
995
995
|
expect(getAsVerticesArrays(sccs)).toEqual([['K', 'J', 'I', 'H', 'D', 'C', 'B'], ['G', 'F', 'E'], ['A']]);
|
|
996
996
|
});
|
|
997
997
|
});
|
|
998
|
+
|
|
999
|
+
describe('delete', () => {
|
|
1000
|
+
it(`deleteVertex deletes all of it's neighbors from the inEdge Map`, () => {
|
|
1001
|
+
const graph = new DirectedGraph();
|
|
1002
|
+
graph.addVertex('A');
|
|
1003
|
+
graph.addVertex('B');
|
|
1004
|
+
graph.addVertex('C');
|
|
1005
|
+
|
|
1006
|
+
graph.addEdge('B', 'A');
|
|
1007
|
+
graph.addEdge('C', 'A');
|
|
1008
|
+
|
|
1009
|
+
// 'Incoming to A should contain ['B','C']
|
|
1010
|
+
expect(graph.incomingEdgesOf('A').map(e => e.src)).toEqual(['B', 'C']); // ['B','C']
|
|
1011
|
+
|
|
1012
|
+
// Now delete B, which has no direct link to C, only that C -> A.
|
|
1013
|
+
graph.deleteVertex('B');
|
|
1014
|
+
|
|
1015
|
+
// Now if we do the same call to incoming edges for we should get only ['C']
|
|
1016
|
+
expect(graph.incomingEdgesOf('A').map(e => e.src)).toEqual(['C']); // [];
|
|
1017
|
+
|
|
1018
|
+
// but it only shows an empty array, since we deleted all of `A's edges, not just the one to `B`.
|
|
1019
|
+
|
|
1020
|
+
// If we check C, it correctly shows A as an outgoing edge,
|
|
1021
|
+
// even though A no longer has any knowledge of C linking to it.
|
|
1022
|
+
expect(graph.outgoingEdgesOf('C').map(e => e.dest)).toEqual(['A']);
|
|
1023
|
+
});
|
|
1024
|
+
});
|
|
@@ -147,7 +147,9 @@ describe('HashMap', () => {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
const eHM = new ExtendedHashMap<string, number>([], {
|
|
150
|
+
const eHM = new ExtendedHashMap<string, number>([], {
|
|
151
|
+
someOtherParam: 'someOtherParam'
|
|
152
|
+
});
|
|
151
153
|
eHM.set('one', 1);
|
|
152
154
|
expect(eHM.get('one')).toBe(1);
|
|
153
155
|
});
|
|
@@ -357,7 +359,9 @@ describe('LinkedHashMap', () => {
|
|
|
357
359
|
|
|
358
360
|
it('should handle number keys correctly', () => {
|
|
359
361
|
hashMap.set(999, { a: '999Value' });
|
|
360
|
-
expect(hashMap.get(999)).toEqual({
|
|
362
|
+
expect(hashMap.get(999)).toEqual({
|
|
363
|
+
a: '999Value'
|
|
364
|
+
});
|
|
361
365
|
});
|
|
362
366
|
|
|
363
367
|
it('should update the value for an existing key', () => {
|
|
@@ -2,6 +2,25 @@ import { FibonacciHeap, Heap, MaxHeap, MinHeap } from '../../../../src';
|
|
|
2
2
|
import { logBigOMetricsWrap } from '../../../utils';
|
|
3
3
|
|
|
4
4
|
describe('Heap Operation Test', () => {
|
|
5
|
+
it('should heap add and delete work well', function () {
|
|
6
|
+
const hp = new MinHeap<number>();
|
|
7
|
+
|
|
8
|
+
hp.add(2);
|
|
9
|
+
hp.add(3);
|
|
10
|
+
hp.add(1);
|
|
11
|
+
hp.add(4);
|
|
12
|
+
hp.add(6);
|
|
13
|
+
hp.add(5);
|
|
14
|
+
hp.add(7);
|
|
15
|
+
|
|
16
|
+
hp.delete(4);
|
|
17
|
+
hp.delete(7);
|
|
18
|
+
hp.delete(1);
|
|
19
|
+
|
|
20
|
+
expect(hp.size).toBe(4);
|
|
21
|
+
expect(hp.peek()).toBe(2);
|
|
22
|
+
});
|
|
23
|
+
|
|
5
24
|
it('should numeric heap work well', function () {
|
|
6
25
|
const minNumHeap = new MinHeap<number>();
|
|
7
26
|
minNumHeap.add(1);
|
|
@@ -27,7 +46,9 @@ describe('Heap Operation Test', () => {
|
|
|
27
46
|
});
|
|
28
47
|
|
|
29
48
|
it('should clone', function () {
|
|
30
|
-
const minNumHeap = new Heap<string>([], {
|
|
49
|
+
const minNumHeap = new Heap<string>([], {
|
|
50
|
+
comparator: (a, b) => Number(a) - Number(b)
|
|
51
|
+
});
|
|
31
52
|
minNumHeap.add('1');
|
|
32
53
|
minNumHeap.add('6');
|
|
33
54
|
minNumHeap.add('2');
|
|
@@ -44,13 +65,21 @@ describe('Heap Operation Test', () => {
|
|
|
44
65
|
});
|
|
45
66
|
|
|
46
67
|
it('should object heap work well', function () {
|
|
47
|
-
const minHeap = new MinHeap<{
|
|
68
|
+
const minHeap = new MinHeap<{
|
|
69
|
+
a: string;
|
|
70
|
+
key: number;
|
|
71
|
+
}>([], {
|
|
72
|
+
comparator: (a, b) => a.key - b.key
|
|
73
|
+
});
|
|
48
74
|
minHeap.add({ key: 1, a: 'a1' });
|
|
49
75
|
minHeap.add({ key: 6, a: 'a6' });
|
|
50
76
|
minHeap.add({ key: 2, a: 'a2' });
|
|
51
77
|
minHeap.add({ key: 0, a: 'a0' });
|
|
52
78
|
|
|
53
|
-
expect(minHeap.peek()).toEqual({
|
|
79
|
+
expect(minHeap.peek()).toEqual({
|
|
80
|
+
a: 'a0',
|
|
81
|
+
key: 0
|
|
82
|
+
});
|
|
54
83
|
expect(minHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
55
84
|
{ a: 'a0' },
|
|
56
85
|
{ a: 'a1' },
|
|
@@ -60,18 +89,28 @@ describe('Heap Operation Test', () => {
|
|
|
60
89
|
let i = 0;
|
|
61
90
|
const expectPolled = [{ a: 'a0' }, { a: 'a1' }, { a: 'a2' }, { a: 'a6' }];
|
|
62
91
|
while (minHeap.size > 0) {
|
|
63
|
-
expect({
|
|
92
|
+
expect({
|
|
93
|
+
a: minHeap.poll()?.a
|
|
94
|
+
}).toEqual(expectPolled[i]);
|
|
64
95
|
i++;
|
|
65
96
|
}
|
|
66
97
|
|
|
67
|
-
const maxHeap = new MaxHeap<{
|
|
98
|
+
const maxHeap = new MaxHeap<{
|
|
99
|
+
key: number;
|
|
100
|
+
a: string;
|
|
101
|
+
}>([], {
|
|
102
|
+
comparator: (a, b) => b.key - a.key
|
|
103
|
+
});
|
|
68
104
|
maxHeap.add({ key: 1, a: 'a1' });
|
|
69
105
|
maxHeap.add({ key: 6, a: 'a6' });
|
|
70
106
|
maxHeap.add({ key: 5, a: 'a5' });
|
|
71
107
|
maxHeap.add({ key: 2, a: 'a2' });
|
|
72
108
|
maxHeap.add({ key: 0, a: 'a0' });
|
|
73
109
|
maxHeap.add({ key: 9, a: 'a9' });
|
|
74
|
-
expect(maxHeap.peek()).toEqual({
|
|
110
|
+
expect(maxHeap.peek()).toEqual({
|
|
111
|
+
a: 'a9',
|
|
112
|
+
key: 9
|
|
113
|
+
});
|
|
75
114
|
expect(maxHeap.toArray().map(item => ({ a: item.a }))).toEqual([
|
|
76
115
|
{ a: 'a9' },
|
|
77
116
|
{ a: 'a2' },
|
|
@@ -83,13 +122,18 @@ describe('Heap Operation Test', () => {
|
|
|
83
122
|
const maxExpectPolled = [{ a: 'a9' }, { a: 'a6' }, { a: 'a5' }, { a: 'a2' }, { a: 'a1' }, { a: 'a0' }];
|
|
84
123
|
let maxI = 0;
|
|
85
124
|
while (maxHeap.size > 0) {
|
|
86
|
-
expect({
|
|
125
|
+
expect({
|
|
126
|
+
a: maxHeap.poll()?.a
|
|
127
|
+
}).toEqual(maxExpectPolled[maxI]);
|
|
87
128
|
maxI++;
|
|
88
129
|
}
|
|
89
130
|
});
|
|
90
131
|
|
|
91
132
|
it('should object heap map & filter', function () {
|
|
92
|
-
const minHeap = new MinHeap<{
|
|
133
|
+
const minHeap = new MinHeap<{
|
|
134
|
+
a: string;
|
|
135
|
+
key: number;
|
|
136
|
+
}>(
|
|
93
137
|
[
|
|
94
138
|
{ key: 1, a: 'a1' },
|
|
95
139
|
{ key: 6, a: 'a6' },
|
|
@@ -99,7 +143,9 @@ describe('Heap Operation Test', () => {
|
|
|
99
143
|
{ key: 4, a: 'a4' },
|
|
100
144
|
{ key: 0, a: 'a0' }
|
|
101
145
|
],
|
|
102
|
-
{
|
|
146
|
+
{
|
|
147
|
+
comparator: (a, b) => a.key - b.key
|
|
148
|
+
}
|
|
103
149
|
);
|
|
104
150
|
|
|
105
151
|
const mappedMinHeap = minHeap.map(
|
|
@@ -127,7 +173,9 @@ describe('Heap Operation Test', () => {
|
|
|
127
173
|
});
|
|
128
174
|
|
|
129
175
|
it('should object heap', () => {
|
|
130
|
-
const heap = new Heap<{
|
|
176
|
+
const heap = new Heap<{
|
|
177
|
+
rawItem: { id: number };
|
|
178
|
+
}>(
|
|
131
179
|
[
|
|
132
180
|
{ rawItem: { id: 4 } },
|
|
133
181
|
{ rawItem: { id: 8 } },
|
|
@@ -137,7 +185,9 @@ describe('Heap Operation Test', () => {
|
|
|
137
185
|
{ rawItem: { id: 3 } },
|
|
138
186
|
{ rawItem: { id: 5 } }
|
|
139
187
|
],
|
|
140
|
-
{
|
|
188
|
+
{
|
|
189
|
+
comparator: (a, b) => a.rawItem.id - b.rawItem.id
|
|
190
|
+
}
|
|
141
191
|
);
|
|
142
192
|
|
|
143
193
|
expect([...heap.sort()]).toEqual([
|
|
@@ -162,7 +212,9 @@ describe('Heap Operation Test', () => {
|
|
|
162
212
|
{ rawItem: { id: 3 } },
|
|
163
213
|
{ rawItem: { id: 5 } }
|
|
164
214
|
],
|
|
165
|
-
{
|
|
215
|
+
{
|
|
216
|
+
toElementFn: rawElement => rawElement.rawItem.id
|
|
217
|
+
}
|
|
166
218
|
);
|
|
167
219
|
|
|
168
220
|
expect([...heap.sort()]).toEqual([1, 3, 4, 5, 6, 7, 8]);
|
|
@@ -5,7 +5,9 @@ describe('MaxHeap', () => {
|
|
|
5
5
|
let maxHeap: MaxHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
maxHeap = new MaxHeap<number>([], {
|
|
8
|
+
maxHeap = new MaxHeap<number>([], {
|
|
9
|
+
comparator: numberComparator
|
|
10
|
+
});
|
|
9
11
|
});
|
|
10
12
|
|
|
11
13
|
it('add and poll elements in descending order', () => {
|
|
@@ -51,7 +53,10 @@ describe('MaxHeap', () => {
|
|
|
51
53
|
});
|
|
52
54
|
|
|
53
55
|
it('should object heap map & filter', function () {
|
|
54
|
-
const maxHeap = new MaxHeap<{
|
|
56
|
+
const maxHeap = new MaxHeap<{
|
|
57
|
+
a: string;
|
|
58
|
+
key: number;
|
|
59
|
+
}>(
|
|
55
60
|
[
|
|
56
61
|
{ key: 1, a: 'a1' },
|
|
57
62
|
{ key: 6, a: 'a6' },
|
|
@@ -61,7 +66,9 @@ describe('MaxHeap', () => {
|
|
|
61
66
|
{ key: 4, a: 'a4' },
|
|
62
67
|
{ key: 0, a: 'a0' }
|
|
63
68
|
],
|
|
64
|
-
{
|
|
69
|
+
{
|
|
70
|
+
comparator: (a, b) => b.key - a.key
|
|
71
|
+
}
|
|
65
72
|
);
|
|
66
73
|
|
|
67
74
|
const mappedMaxHeap = maxHeap.map(
|
|
@@ -5,7 +5,9 @@ describe('MinHeap', () => {
|
|
|
5
5
|
let minHeap: MinHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
8
|
-
minHeap = new MinHeap<number>([], {
|
|
8
|
+
minHeap = new MinHeap<number>([], {
|
|
9
|
+
comparator: numberComparator
|
|
10
|
+
});
|
|
9
11
|
});
|
|
10
12
|
|
|
11
13
|
it('add and poll elements in ascending order', () => {
|
|
@@ -79,7 +79,9 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
79
79
|
|
|
80
80
|
describe('DoublyLinkedList Operation Test', () => {
|
|
81
81
|
let list: DoublyLinkedList<number>;
|
|
82
|
-
let objectList: DoublyLinkedList<{
|
|
82
|
+
let objectList: DoublyLinkedList<{
|
|
83
|
+
keyA: number;
|
|
84
|
+
}>;
|
|
83
85
|
|
|
84
86
|
beforeEach(() => {
|
|
85
87
|
list = new DoublyLinkedList();
|
|
@@ -11,10 +11,14 @@ describe('SinglyLinkedListNode', () => {
|
|
|
11
11
|
|
|
12
12
|
describe('SinglyLinkedList Operation Test', () => {
|
|
13
13
|
let list: SinglyLinkedList<number>;
|
|
14
|
-
let objectList: SinglyLinkedList<{
|
|
14
|
+
let objectList: SinglyLinkedList<{
|
|
15
|
+
keyA: number;
|
|
16
|
+
}>;
|
|
15
17
|
beforeEach(() => {
|
|
16
18
|
list = new SinglyLinkedList<number>();
|
|
17
|
-
objectList = new SinglyLinkedList<{
|
|
19
|
+
objectList = new SinglyLinkedList<{
|
|
20
|
+
keyA: number;
|
|
21
|
+
}>();
|
|
18
22
|
});
|
|
19
23
|
|
|
20
24
|
describe('push', () => {
|
|
@@ -16,7 +16,11 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
19
|
-
const priorityQueue = new MaxPriorityQueue<{
|
|
19
|
+
const priorityQueue = new MaxPriorityQueue<{
|
|
20
|
+
keyA: number;
|
|
21
|
+
}>([], {
|
|
22
|
+
comparator: (a, b) => b.keyA - a.keyA
|
|
23
|
+
});
|
|
20
24
|
priorityQueue.refill([{ keyA: 5 }, { keyA: 3 }, { keyA: 1 }]);
|
|
21
25
|
priorityQueue.add({ keyA: 7 });
|
|
22
26
|
|
|
@@ -52,7 +56,9 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
52
56
|
|
|
53
57
|
it('should correctly heapify an array', () => {
|
|
54
58
|
const array = [5, 3, 7, 1];
|
|
55
|
-
const heap = MaxPriorityQueue.heapify<number>(array, {
|
|
59
|
+
const heap = MaxPriorityQueue.heapify<number>(array, {
|
|
60
|
+
comparator: (a, b) => b - a
|
|
61
|
+
});
|
|
56
62
|
heap.refill(array);
|
|
57
63
|
|
|
58
64
|
expect(heap.poll()).toBe(7);
|
|
@@ -64,7 +70,11 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
64
70
|
it('should correctly heapify an object array', () => {
|
|
65
71
|
const elements = [{ keyA: 5 }, { keyA: 3 }, { keyA: 7 }, { keyA: 1 }];
|
|
66
72
|
debugger;
|
|
67
|
-
const maxPQ = MaxPriorityQueue.heapify<{
|
|
73
|
+
const maxPQ = MaxPriorityQueue.heapify<{
|
|
74
|
+
keyA: number;
|
|
75
|
+
}>(elements, {
|
|
76
|
+
comparator: (a, b) => b.keyA - a.keyA
|
|
77
|
+
});
|
|
68
78
|
|
|
69
79
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
70
80
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
@@ -73,7 +83,9 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
73
83
|
});
|
|
74
84
|
|
|
75
85
|
it('should object priority queue', () => {
|
|
76
|
-
const maxPQ = new MaxPriorityQueue<{
|
|
86
|
+
const maxPQ = new MaxPriorityQueue<{
|
|
87
|
+
rawItem: { id: number };
|
|
88
|
+
}>(
|
|
77
89
|
[
|
|
78
90
|
{ rawItem: { id: 4 } },
|
|
79
91
|
{ rawItem: { id: 8 } },
|
|
@@ -83,7 +95,9 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
83
95
|
{ rawItem: { id: 3 } },
|
|
84
96
|
{ rawItem: { id: 5 } }
|
|
85
97
|
],
|
|
86
|
-
{
|
|
98
|
+
{
|
|
99
|
+
comparator: (a, b) => b.rawItem.id - a.rawItem.id
|
|
100
|
+
}
|
|
87
101
|
);
|
|
88
102
|
|
|
89
103
|
expect([...maxPQ.sort()]).toEqual([
|
|
@@ -108,7 +122,9 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
108
122
|
{ rawItem: { id: 3 } },
|
|
109
123
|
{ rawItem: { id: 5 } }
|
|
110
124
|
],
|
|
111
|
-
{
|
|
125
|
+
{
|
|
126
|
+
toElementFn: rawElement => rawElement.rawItem.id
|
|
127
|
+
}
|
|
112
128
|
);
|
|
113
129
|
|
|
114
130
|
expect([...maxPQ.sort()]).toEqual([8, 7, 6, 5, 4, 3, 1]);
|
|
@@ -5,7 +5,9 @@ import { isDebugTest } from '../../../config';
|
|
|
5
5
|
const isDebug = isDebugTest;
|
|
6
6
|
describe('PriorityQueue Operation Test', () => {
|
|
7
7
|
it('should PriorityQueue poll, pee, heapify, toArray work well', function () {
|
|
8
|
-
const minPQ = new PriorityQueue<number>([], {
|
|
8
|
+
const minPQ = new PriorityQueue<number>([], {
|
|
9
|
+
comparator: (a, b) => a - b
|
|
10
|
+
});
|
|
9
11
|
minPQ.refill([5, 2, 3, 4, 6, 1]);
|
|
10
12
|
expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]);
|
|
11
13
|
minPQ.poll();
|
|
@@ -21,7 +23,9 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
21
23
|
});
|
|
22
24
|
|
|
23
25
|
it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () {
|
|
24
|
-
const maxPriorityQueue = new PriorityQueue<number>([], {
|
|
26
|
+
const maxPriorityQueue = new PriorityQueue<number>([], {
|
|
27
|
+
comparator: (a, b) => b - a
|
|
28
|
+
});
|
|
25
29
|
maxPriorityQueue.refill([5, 2, 3, 4, 6, 1]);
|
|
26
30
|
expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]);
|
|
27
31
|
maxPriorityQueue.poll();
|
|
@@ -37,7 +41,9 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
37
41
|
});
|
|
38
42
|
|
|
39
43
|
it('should PriorityQueue clone, sort, getNodes, dfs work well', function () {
|
|
40
|
-
const minPQ1 = new PriorityQueue<number>([], {
|
|
44
|
+
const minPQ1 = new PriorityQueue<number>([], {
|
|
45
|
+
comparator: (a, b) => a - b
|
|
46
|
+
});
|
|
41
47
|
minPQ1.refill([2, 5, 8, 3, 1, 6, 7, 4]);
|
|
42
48
|
const clonedPriorityQueue = minPQ1.clone();
|
|
43
49
|
expect(clonedPriorityQueue.elements).toEqual(minPQ1.elements);
|
|
@@ -50,7 +56,9 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
50
56
|
|
|
51
57
|
describe('Priority Queue Performance Test', () => {
|
|
52
58
|
it('should numeric heap work well', function () {
|
|
53
|
-
const pq = new PriorityQueue<number>([], {
|
|
59
|
+
const pq = new PriorityQueue<number>([], {
|
|
60
|
+
comparator: (a, b) => b - a
|
|
61
|
+
});
|
|
54
62
|
|
|
55
63
|
const tS = performance.now();
|
|
56
64
|
|