data-structure-typed 1.49.3 → 1.49.5
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 +70 -69
- package/README_zh-CN.md +44 -49
- package/benchmark/report.html +16 -16
- package/benchmark/report.json +187 -187
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
- 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.d.ts +0 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -11
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -19
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js +4 -0
- 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 +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- 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/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -11
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -20
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +361 -542
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +15 -8
- package/src/data-structures/binary-tree/binary-tree.ts +57 -74
- package/src/data-structures/binary-tree/bst.ts +16 -13
- package/src/data-structures/binary-tree/rb-tree.ts +16 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
- package/src/data-structures/graph/abstract-graph.ts +14 -24
- package/src/data-structures/graph/directed-graph.ts +8 -6
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +4 -7
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -5
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +7 -2
- package/src/types/common.ts +4 -4
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +18 -23
- package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -28
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
- package/test/performance/data-structures/queue/deque.test.ts +11 -12
- package/test/performance/data-structures/queue/queue.test.ts +15 -14
- package/test/performance/data-structures/stack/stack.test.ts +11 -11
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -15
- package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +112 -40
- package/test/unit/data-structures/graph/undirected-graph.test.ts +63 -33
- package/test/unit/data-structures/hash/hash-map.test.ts +64 -23
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -4
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/big-o.ts +14 -14
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -1,54 +1,347 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
describe('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
import { Matrix } from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('Matrix', () => {
|
|
4
|
+
let matrix: Matrix;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
// Initialize a 3x3 matrix with zeros
|
|
8
|
+
matrix = new Matrix([
|
|
9
|
+
[0, 0, 0],
|
|
10
|
+
[0, 0, 0],
|
|
11
|
+
[0, 0, 0]
|
|
12
|
+
]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should create a matrix with the correct dimensions and initial values', () => {
|
|
16
|
+
expect(matrix.rows).toBe(3);
|
|
17
|
+
expect(matrix.cols).toBe(3);
|
|
18
|
+
expect(matrix.data).toEqual([
|
|
19
|
+
[0, 0, 0],
|
|
20
|
+
[0, 0, 0],
|
|
21
|
+
[0, 0, 0]
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should get a value at a specific position', () => {
|
|
26
|
+
expect(matrix.get(1, 1)).toBe(0);
|
|
27
|
+
expect(matrix.get(2, 2)).toBe(0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should set a value at a specific position', () => {
|
|
31
|
+
matrix.set(1, 1, 42);
|
|
32
|
+
expect(matrix.get(1, 1)).toBe(42);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should not allow getting or setting values at invalid positions', () => {
|
|
36
|
+
expect(matrix.get(-1, 0)).toBeUndefined();
|
|
37
|
+
expect(matrix.get(0, 10)).toBeUndefined();
|
|
38
|
+
|
|
39
|
+
const originalValue = matrix.get(1, 1);
|
|
40
|
+
matrix.set(-1, 1, 42);
|
|
41
|
+
matrix.set(1, 10, 42);
|
|
42
|
+
expect(matrix.get(1, 1)).toBe(originalValue);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('add', () => {
|
|
46
|
+
it('should add two matrices correctly', () => {
|
|
47
|
+
const matrixA = new Matrix([
|
|
48
|
+
[1, 2],
|
|
49
|
+
[3, 4]
|
|
50
|
+
]);
|
|
51
|
+
const matrixB = new Matrix([
|
|
52
|
+
[5, 6],
|
|
53
|
+
[7, 8]
|
|
54
|
+
]);
|
|
55
|
+
const expectedResult = new Matrix([
|
|
56
|
+
[6, 8],
|
|
57
|
+
[10, 12]
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const result = matrixA.add(matrixB);
|
|
61
|
+
|
|
62
|
+
expect(result?.data).toEqual(expectedResult.data);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should throw an error for matrices with mismatched dimensions', () => {
|
|
66
|
+
const matrixA = new Matrix([
|
|
67
|
+
[1, 2],
|
|
68
|
+
[3, 4]
|
|
69
|
+
]);
|
|
70
|
+
const matrixB = new Matrix([
|
|
71
|
+
[5, 6, 7],
|
|
72
|
+
[8, 9, 10]
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
expect(() => matrixA.add(matrixB)).toThrowError('Matrix dimensions must match for addition.');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should throw an error for matrices with mismatched dimensions', () => {
|
|
79
|
+
const matrixA = new Matrix([
|
|
80
|
+
[1, 2],
|
|
81
|
+
[3, 4]
|
|
82
|
+
]);
|
|
83
|
+
const matrixB = new Matrix([
|
|
84
|
+
[5, 6, 7],
|
|
85
|
+
[8, 9, 10]
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
expect(() => matrixA.add(matrixB)).toThrowError('Matrix dimensions must match for addition.');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('subtract', () => {
|
|
93
|
+
it('should subtract two matrices with numbers correctly', () => {
|
|
94
|
+
const matrixA = new Matrix([
|
|
95
|
+
[5, 6],
|
|
96
|
+
[7, 8]
|
|
97
|
+
]);
|
|
98
|
+
const matrixB = new Matrix([
|
|
99
|
+
[1, 2],
|
|
100
|
+
[3, 4]
|
|
101
|
+
]);
|
|
102
|
+
const expectedResult = new Matrix([
|
|
103
|
+
[4, 4],
|
|
104
|
+
[4, 4]
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const result = matrixA.subtract(matrixB);
|
|
108
|
+
|
|
109
|
+
expect(result?.data).toEqual(expectedResult.data);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should subtract two matrices with custom subtract function correctly', () => {
|
|
113
|
+
const customSubtractFn = (a: number, b: number) => a * 10 - b; // Custom subtraction for arrays
|
|
114
|
+
const matrixA = new Matrix(
|
|
115
|
+
[
|
|
116
|
+
[5, 6],
|
|
117
|
+
[7, 8]
|
|
118
|
+
],
|
|
119
|
+
{ subtractFn: customSubtractFn }
|
|
120
|
+
);
|
|
121
|
+
const matrixB = new Matrix(
|
|
122
|
+
[
|
|
123
|
+
[1, 2],
|
|
124
|
+
[3, 4]
|
|
125
|
+
],
|
|
126
|
+
{ subtractFn: customSubtractFn }
|
|
127
|
+
);
|
|
128
|
+
const expectedResult = new Matrix(
|
|
129
|
+
[
|
|
130
|
+
[49, 58],
|
|
131
|
+
[67, 76]
|
|
132
|
+
],
|
|
133
|
+
{ subtractFn: customSubtractFn }
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const result = matrixA.subtract(matrixB);
|
|
137
|
+
|
|
138
|
+
expect(result?.data).toEqual(expectedResult.data);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should throw an error for matrices with mismatched dimensions', () => {
|
|
142
|
+
const matrixA = new Matrix([
|
|
143
|
+
[1, 2],
|
|
144
|
+
[3, 4]
|
|
145
|
+
]);
|
|
146
|
+
const matrixB = new Matrix([
|
|
147
|
+
[5, 6, 7],
|
|
148
|
+
[8, 9, 10]
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
expect(() => matrixA.subtract(matrixB)).toThrowError('Matrix dimensions must match for subtraction.');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('multiply', () => {
|
|
156
|
+
it('should multiply two matrices with numbers correctly', () => {
|
|
157
|
+
const matrixA = new Matrix([
|
|
158
|
+
[1, 2],
|
|
159
|
+
[3, 4]
|
|
160
|
+
]);
|
|
161
|
+
const matrixB = new Matrix([
|
|
162
|
+
[5, 6],
|
|
163
|
+
[7, 8]
|
|
164
|
+
]);
|
|
165
|
+
const expectedResult = new Matrix([
|
|
166
|
+
[19, 22],
|
|
167
|
+
[43, 50]
|
|
168
|
+
]);
|
|
169
|
+
|
|
170
|
+
const result = matrixA.multiply(matrixB);
|
|
171
|
+
|
|
172
|
+
expect(result?.data).toEqual(expectedResult.data);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should multiply two matrices with custom multiply function correctly', () => {
|
|
176
|
+
const customMultiplyFn = (a: number, b: number) => a * 10 * b; // Custom multiplication for arrays
|
|
177
|
+
const matrixA = new Matrix(
|
|
178
|
+
[
|
|
179
|
+
[1, 2],
|
|
180
|
+
[3, 4]
|
|
181
|
+
],
|
|
182
|
+
{ multiplyFn: customMultiplyFn }
|
|
183
|
+
);
|
|
184
|
+
const matrixB = new Matrix(
|
|
185
|
+
[
|
|
186
|
+
[5, 6],
|
|
187
|
+
[7, 8]
|
|
188
|
+
],
|
|
189
|
+
{ multiplyFn: customMultiplyFn }
|
|
190
|
+
);
|
|
191
|
+
const result = matrixA.multiply(matrixB);
|
|
192
|
+
|
|
193
|
+
expect(result?.data).toEqual([
|
|
194
|
+
[190, 220],
|
|
195
|
+
[430, 500]
|
|
196
|
+
]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should throw an error for matrices with mismatched dimensions', () => {
|
|
200
|
+
const matrixA = new Matrix([
|
|
201
|
+
[1, 2, 3],
|
|
202
|
+
[3, 4, 5]
|
|
203
|
+
]);
|
|
204
|
+
const matrixB = new Matrix([
|
|
205
|
+
[5, 6, 7],
|
|
206
|
+
[8, 9, 1]
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
expect(() => matrixA.multiply(matrixB)).toThrowError(
|
|
210
|
+
'Matrix dimensions must be compatible for multiplication (A.cols = B.rows).'
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('transpose', () => {
|
|
216
|
+
test('should transpose a matrix with numeric values correctly', () => {
|
|
217
|
+
const originalMatrix = new Matrix([
|
|
218
|
+
[1, 2, 3],
|
|
219
|
+
[4, 5, 6],
|
|
220
|
+
[7, 8, 9]
|
|
221
|
+
]);
|
|
222
|
+
|
|
223
|
+
const transposedMatrix = originalMatrix.transpose();
|
|
224
|
+
|
|
225
|
+
expect(transposedMatrix.rows).toBe(originalMatrix.cols);
|
|
226
|
+
expect(transposedMatrix.cols).toBe(originalMatrix.rows);
|
|
227
|
+
expect(transposedMatrix.data).toEqual([
|
|
228
|
+
[1, 4, 7],
|
|
229
|
+
[2, 5, 8],
|
|
230
|
+
[3, 6, 9]
|
|
231
|
+
]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('should transpose an empty matrix correctly', () => {
|
|
235
|
+
const originalMatrix = new Matrix([]);
|
|
236
|
+
|
|
237
|
+
const transposedMatrix = originalMatrix.transpose();
|
|
238
|
+
|
|
239
|
+
expect(transposedMatrix.rows).toBe(0);
|
|
240
|
+
expect(transposedMatrix.cols).toBe(0);
|
|
241
|
+
expect(transposedMatrix.data).toEqual([]);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('should throw an error when transposing a non-rectangular matrix', () => {
|
|
245
|
+
const originalMatrix = new Matrix([
|
|
246
|
+
[1, 2, 3],
|
|
247
|
+
[4, 5]
|
|
248
|
+
]);
|
|
249
|
+
|
|
250
|
+
// Using a lambda to call transpose because Jest expects the error to be thrown within a function
|
|
251
|
+
expect(() => originalMatrix.transpose()).toThrowError('Matrix must be rectangular for transposition.');
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe('inverse', () => {
|
|
256
|
+
it('should calculate the inverse of a 2x2 matrix', () => {
|
|
257
|
+
const data: number[][] = [
|
|
258
|
+
[1, 2],
|
|
259
|
+
[3, 4]
|
|
260
|
+
];
|
|
261
|
+
|
|
262
|
+
const matrix = new Matrix(data);
|
|
263
|
+
const inverseMatrix = matrix.inverse();
|
|
264
|
+
|
|
265
|
+
const expectedInverse: number[][] = [
|
|
266
|
+
[-2, 1],
|
|
267
|
+
[1.5, -0.5]
|
|
268
|
+
];
|
|
269
|
+
|
|
270
|
+
expect(inverseMatrix?.data).toEqual(expectedInverse);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should calculate the inverse of a 3x3 matrix', () => {
|
|
274
|
+
const data: number[][] = [
|
|
275
|
+
[4, 7, 2],
|
|
276
|
+
[2, 6, 3],
|
|
277
|
+
[1, 2, 5]
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
const matrix = new Matrix(data);
|
|
281
|
+
const inverseMatrix = matrix.inverse();
|
|
282
|
+
|
|
283
|
+
// const expectedInverse: number[][] = [
|
|
284
|
+
// [24 / 43, -31 / 43, 9 / 43],
|
|
285
|
+
// [-7 / 43, 18 / 43, -8 / 43],
|
|
286
|
+
// [-2 / 43, -1 / 43, 10 / 43],
|
|
287
|
+
// ];
|
|
288
|
+
|
|
289
|
+
expect(inverseMatrix?.data).toEqual([
|
|
290
|
+
[0.558139534883721, -0.7209302325581396, 0.2093023255813954],
|
|
291
|
+
[-0.16279069767441862, 0.4186046511627907, -0.18604651162790697],
|
|
292
|
+
[-0.046511627906976744, -0.023255813953488372, 0.23255813953488372]
|
|
293
|
+
]);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
describe('dot', () => {
|
|
298
|
+
test('should calculate the dot product of two matrices', () => {
|
|
299
|
+
const matrix1 = new Matrix([
|
|
300
|
+
[1, 2],
|
|
301
|
+
[3, 4]
|
|
302
|
+
]);
|
|
303
|
+
const matrix2 = new Matrix([
|
|
304
|
+
[5, 6],
|
|
305
|
+
[7, 8]
|
|
306
|
+
]);
|
|
307
|
+
|
|
308
|
+
const resultMatrix = matrix1.dot(matrix2);
|
|
309
|
+
|
|
310
|
+
expect(resultMatrix?.data).toEqual([
|
|
311
|
+
[19, 22],
|
|
312
|
+
[43, 50]
|
|
313
|
+
]);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('should throw an error for incompatible matrices', () => {
|
|
317
|
+
const matrix1 = new Matrix([
|
|
318
|
+
[1, 2],
|
|
319
|
+
[3, 4]
|
|
320
|
+
]);
|
|
321
|
+
const matrix2 = new Matrix([
|
|
322
|
+
[5, 6, 7],
|
|
323
|
+
[8, 9, 10],
|
|
324
|
+
[18, 19, 110]
|
|
325
|
+
]);
|
|
326
|
+
expect(() => matrix1.dot(matrix2)).toThrowError(
|
|
327
|
+
'Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.'
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should throw an error for incompatible matrices', () => {
|
|
332
|
+
const matrixA = new Matrix([
|
|
333
|
+
[1, 2],
|
|
334
|
+
[3, 4]
|
|
335
|
+
]);
|
|
336
|
+
const matrixB = new Matrix([
|
|
337
|
+
[5, 6],
|
|
338
|
+
[7, 8],
|
|
339
|
+
[9, 10]
|
|
340
|
+
]);
|
|
341
|
+
|
|
342
|
+
expect(() => matrixA.dot(matrixB)).toThrowError(
|
|
343
|
+
'Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.'
|
|
344
|
+
);
|
|
345
|
+
});
|
|
53
346
|
});
|
|
54
347
|
});
|
|
@@ -63,7 +63,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
63
63
|
|
|
64
64
|
it('should correctly heapify an object array', () => {
|
|
65
65
|
const elements = [{ keyA: 5 }, { keyA: 3 }, { keyA: 7 }, { keyA: 1 }];
|
|
66
|
-
debugger
|
|
66
|
+
debugger;
|
|
67
67
|
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>(elements, { comparator: (a, b) => b.keyA - a.keyA });
|
|
68
68
|
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
@@ -170,7 +170,6 @@ describe('Deque - Utility Operations', () => {
|
|
|
170
170
|
deque.print();
|
|
171
171
|
expect(consoleSpy).toHaveBeenCalledWith([1, 2]);
|
|
172
172
|
});
|
|
173
|
-
|
|
174
173
|
});
|
|
175
174
|
describe('Deque - Additional Operations', () => {
|
|
176
175
|
let deque: Deque<number>;
|
|
@@ -229,7 +228,6 @@ describe('Deque - Additional Operations', () => {
|
|
|
229
228
|
expect(iterator.next().value).toBe(2);
|
|
230
229
|
expect(iterator.next().value).toBe(1);
|
|
231
230
|
});
|
|
232
|
-
|
|
233
231
|
});
|
|
234
232
|
describe('Deque - push Method', () => {
|
|
235
233
|
let deque: Deque<number>;
|
|
@@ -264,7 +262,6 @@ describe('Deque - push Method', () => {
|
|
|
264
262
|
});
|
|
265
263
|
|
|
266
264
|
test('push should add an element and reallocate when last bucket and lastInBucket are at max', () => {
|
|
267
|
-
|
|
268
265
|
for (let i = 0; i < 100; i++) {
|
|
269
266
|
deque.push(i);
|
|
270
267
|
}
|
|
@@ -303,8 +300,6 @@ describe('Deque - pop Method', () => {
|
|
|
303
300
|
const lastElement = deque.last;
|
|
304
301
|
expect(deque.pop()).toBe(lastElement);
|
|
305
302
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
303
|
});
|
|
309
304
|
});
|
|
310
305
|
describe('Deque - unshift Method', () => {
|
|
@@ -368,6 +363,5 @@ describe('Deque - shift Method', () => {
|
|
|
368
363
|
const firstElement = deque.first;
|
|
369
364
|
expect(deque.shift()).toBe(firstElement);
|
|
370
365
|
}
|
|
371
|
-
|
|
372
366
|
});
|
|
373
367
|
});
|
|
@@ -66,7 +66,6 @@ describe('Stack', () => {
|
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
|
|
70
69
|
describe('Stack iterative methods', () => {
|
|
71
70
|
let stack: Stack<number>; // Declare a Stack instance
|
|
72
71
|
|
|
@@ -88,7 +87,7 @@ describe('Stack iterative methods', () => {
|
|
|
88
87
|
|
|
89
88
|
test('should apply forEach to the stack', () => {
|
|
90
89
|
const result: number[] = [];
|
|
91
|
-
stack.forEach(
|
|
90
|
+
stack.forEach(element => {
|
|
92
91
|
result.push(element);
|
|
93
92
|
});
|
|
94
93
|
|
|
@@ -96,14 +95,14 @@ describe('Stack iterative methods', () => {
|
|
|
96
95
|
});
|
|
97
96
|
|
|
98
97
|
test('should filter elements in the stack', () => {
|
|
99
|
-
const filteredStack = stack.filter(
|
|
98
|
+
const filteredStack = stack.filter(element => element > 1);
|
|
100
99
|
|
|
101
100
|
expect(filteredStack.size).toBe(2);
|
|
102
101
|
expect([...filteredStack]).toEqual([2, 3]);
|
|
103
102
|
});
|
|
104
103
|
|
|
105
104
|
test('should map elements in the stack', () => {
|
|
106
|
-
const mappedStack = stack.map(
|
|
105
|
+
const mappedStack = stack.map(element => element * 2);
|
|
107
106
|
|
|
108
107
|
expect(mappedStack.size).toBe(3);
|
|
109
108
|
expect([...mappedStack]).toEqual([2, 4, 6]);
|
|
@@ -17,122 +17,132 @@ import {
|
|
|
17
17
|
TreeMultimap,
|
|
18
18
|
Trie
|
|
19
19
|
} from '../../src';
|
|
20
|
-
import { isDebugTest } from
|
|
20
|
+
import { isDebugTest } from '../config';
|
|
21
21
|
|
|
22
22
|
const isDebug = isDebugTest;
|
|
23
23
|
const orgArr: number[] = [6, 1, 2, 7, 5, 3, 4, 9, 8];
|
|
24
24
|
const orgStrArr: string[] = [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
'trie',
|
|
26
|
+
'trial',
|
|
27
|
+
'trick',
|
|
28
|
+
'trip',
|
|
29
|
+
'tree',
|
|
30
|
+
'trend',
|
|
31
|
+
'triangle',
|
|
32
|
+
'track',
|
|
33
|
+
'trace',
|
|
34
|
+
'transmit'
|
|
35
|
+
];
|
|
36
|
+
const entries: [number, number][] = [
|
|
37
|
+
[6, 6],
|
|
38
|
+
[1, 1],
|
|
39
|
+
[2, 2],
|
|
40
|
+
[7, 7],
|
|
41
|
+
[5, 5],
|
|
42
|
+
[3, 3],
|
|
43
|
+
[4, 4],
|
|
44
|
+
[9, 9],
|
|
45
|
+
[8, 8]
|
|
35
46
|
];
|
|
36
|
-
const entries: [number, number][] = [[6, 6], [1, 1], [2, 2], [7, 7], [5, 5], [3, 3], [4, 4], [9, 9], [8, 8]];
|
|
37
47
|
|
|
38
48
|
describe('conversions', () => {
|
|
39
49
|
it('Array to Queue', () => {
|
|
40
50
|
const q = new Queue<number>(orgArr);
|
|
41
51
|
isDebug && q.print();
|
|
42
|
-
})
|
|
52
|
+
});
|
|
43
53
|
|
|
44
54
|
it('Array to Deque', () => {
|
|
45
55
|
const dq = new Deque<number>(orgArr);
|
|
46
56
|
isDebug && dq.print();
|
|
47
|
-
})
|
|
57
|
+
});
|
|
48
58
|
|
|
49
59
|
it('Array to SinglyLinkedList', () => {
|
|
50
60
|
const sl = new SinglyLinkedList<number>(orgArr);
|
|
51
61
|
isDebug && sl.print();
|
|
52
|
-
})
|
|
62
|
+
});
|
|
53
63
|
|
|
54
64
|
it('Array to DoublyLinkedList', () => {
|
|
55
65
|
const dl = new DoublyLinkedList<number>(orgArr);
|
|
56
66
|
isDebug && dl.print();
|
|
57
|
-
})
|
|
67
|
+
});
|
|
58
68
|
|
|
59
69
|
it('Array to Stack', () => {
|
|
60
70
|
const stack = new Stack<number>(orgArr);
|
|
61
71
|
isDebug && stack.print();
|
|
62
|
-
})
|
|
72
|
+
});
|
|
63
73
|
|
|
64
74
|
it('Array to MinHeap', () => {
|
|
65
75
|
const minHeap = new MinHeap<number>(orgArr);
|
|
66
76
|
isDebug && minHeap.print();
|
|
67
|
-
})
|
|
77
|
+
});
|
|
68
78
|
|
|
69
79
|
it('Array to MaxHeap', () => {
|
|
70
80
|
const maxHeap = new MaxHeap<number>(orgArr);
|
|
71
81
|
isDebug && maxHeap.print();
|
|
72
|
-
})
|
|
82
|
+
});
|
|
73
83
|
|
|
74
84
|
it('Array to MinPriorityQueue', () => {
|
|
75
85
|
const minPQ = new MinPriorityQueue<number>(orgArr);
|
|
76
86
|
isDebug && minPQ.print();
|
|
77
|
-
})
|
|
87
|
+
});
|
|
78
88
|
|
|
79
89
|
it('Array to MaxPriorityQueue', () => {
|
|
80
90
|
const maxPQ = new MaxPriorityQueue<number>(orgArr);
|
|
81
91
|
isDebug && maxPQ.print();
|
|
82
|
-
})
|
|
92
|
+
});
|
|
83
93
|
|
|
84
94
|
it('Entry Array to BinaryTree', () => {
|
|
85
95
|
const biTree = new BinaryTree<number>(entries);
|
|
86
96
|
isDebug && biTree.print();
|
|
87
|
-
})
|
|
97
|
+
});
|
|
88
98
|
|
|
89
99
|
it('Entry Array to BST', () => {
|
|
90
100
|
const bst = new BST<number>(entries);
|
|
91
|
-
expect(bst.size).toBe(9)
|
|
101
|
+
expect(bst.size).toBe(9);
|
|
92
102
|
isDebug && bst.print();
|
|
93
|
-
})
|
|
103
|
+
});
|
|
94
104
|
|
|
95
105
|
it('Entry Array to RedBlackTree', () => {
|
|
96
106
|
const rbTree = new RedBlackTree<number>(entries);
|
|
97
|
-
expect(rbTree.size).toBe(9)
|
|
107
|
+
expect(rbTree.size).toBe(9);
|
|
98
108
|
isDebug && rbTree.print();
|
|
99
|
-
})
|
|
109
|
+
});
|
|
100
110
|
|
|
101
111
|
it('Entry Array to AVLTree', () => {
|
|
102
112
|
const avl = new AVLTree<number>(entries);
|
|
103
|
-
expect(avl.size).toBe(9)
|
|
113
|
+
expect(avl.size).toBe(9);
|
|
104
114
|
isDebug && avl.print();
|
|
105
|
-
})
|
|
115
|
+
});
|
|
106
116
|
|
|
107
117
|
it('Entry Array to TreeMultimap', () => {
|
|
108
118
|
const treeMulti = new TreeMultimap<number>(entries);
|
|
109
|
-
expect(treeMulti.size).toBe(9)
|
|
119
|
+
expect(treeMulti.size).toBe(9);
|
|
110
120
|
isDebug && treeMulti.print();
|
|
111
|
-
})
|
|
121
|
+
});
|
|
112
122
|
|
|
113
123
|
it('HashMap to RedBlackTree', () => {
|
|
114
124
|
const hm = new HashMap(entries);
|
|
115
|
-
isDebug && hm.print()
|
|
125
|
+
isDebug && hm.print();
|
|
116
126
|
const rbTree = new RedBlackTree<number>(hm);
|
|
117
|
-
expect(rbTree.size).toBe(9)
|
|
127
|
+
expect(rbTree.size).toBe(9);
|
|
118
128
|
isDebug && rbTree.print();
|
|
119
|
-
})
|
|
129
|
+
});
|
|
120
130
|
|
|
121
131
|
it('PriorityQueue to BST', () => {
|
|
122
132
|
const pq = new MinPriorityQueue(orgArr);
|
|
123
133
|
isDebug && pq.print();
|
|
124
134
|
const bst = new BST<number>(pq);
|
|
125
|
-
expect(bst.size).toBe(9)
|
|
135
|
+
expect(bst.size).toBe(9);
|
|
126
136
|
isDebug && bst.print();
|
|
127
|
-
})
|
|
137
|
+
});
|
|
128
138
|
|
|
129
139
|
it('Deque to RedBlackTree', () => {
|
|
130
140
|
const dq = new Deque(orgArr);
|
|
131
141
|
isDebug && dq.print();
|
|
132
142
|
const rbTree = new RedBlackTree<number>(dq);
|
|
133
|
-
expect(rbTree.size).toBe(9)
|
|
143
|
+
expect(rbTree.size).toBe(9);
|
|
134
144
|
isDebug && rbTree.print();
|
|
135
|
-
})
|
|
145
|
+
});
|
|
136
146
|
|
|
137
147
|
it('Trie to Heap to Deque', () => {
|
|
138
148
|
const trie = new Trie(orgStrArr);
|
|
@@ -146,11 +156,7 @@ describe('conversions', () => {
|
|
|
146
156
|
isDebug && dq.print();
|
|
147
157
|
const entries = dq.map((el, i) => <[number, string]>[i, el]);
|
|
148
158
|
const avl = new AVLTree<number, string>(entries);
|
|
149
|
-
expect(avl.size).toBe(10)
|
|
159
|
+
expect(avl.size).toBe(10);
|
|
150
160
|
isDebug && avl.print();
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
161
|
+
});
|
|
162
|
+
});
|