data-structure-typed 2.4.5 → 2.5.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/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
- package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
- package/CHANGELOG.md +3 -1
- package/README.md +78 -31
- package/dist/cjs/binary-tree.cjs +23698 -0
- package/dist/cjs/graph.cjs +5236 -0
- package/dist/cjs/hash.cjs +1262 -0
- package/dist/cjs/heap.cjs +1540 -0
- package/dist/cjs/index.cjs +24509 -2899
- package/dist/cjs/linked-list.cjs +4370 -0
- package/dist/cjs/matrix.cjs +1042 -0
- package/dist/cjs/priority-queue.cjs +1314 -0
- package/dist/cjs/queue.cjs +4090 -0
- package/dist/cjs/stack.cjs +861 -0
- package/dist/cjs/trie.cjs +1173 -0
- package/dist/cjs-legacy/binary-tree.cjs +23730 -0
- package/dist/cjs-legacy/graph.cjs +5234 -0
- package/dist/cjs-legacy/hash.cjs +1262 -0
- package/dist/cjs-legacy/heap.cjs +1537 -0
- package/dist/cjs-legacy/index.cjs +32555 -10936
- package/dist/cjs-legacy/linked-list.cjs +4376 -0
- package/dist/cjs-legacy/matrix.cjs +1045 -0
- package/dist/cjs-legacy/priority-queue.cjs +1312 -0
- package/dist/cjs-legacy/queue.cjs +4088 -0
- package/dist/cjs-legacy/stack.cjs +861 -0
- package/dist/cjs-legacy/trie.cjs +1172 -0
- package/dist/esm/binary-tree.mjs +23683 -0
- package/dist/esm/graph.mjs +5223 -0
- package/dist/esm/hash.mjs +1259 -0
- package/dist/esm/heap.mjs +1534 -0
- package/dist/esm/index.mjs +24507 -2898
- package/dist/esm/linked-list.mjs +4363 -0
- package/dist/esm/matrix.mjs +1038 -0
- package/dist/esm/priority-queue.mjs +1310 -0
- package/dist/esm/queue.mjs +4086 -0
- package/dist/esm/stack.mjs +859 -0
- package/dist/esm/trie.mjs +1170 -0
- package/dist/esm-legacy/binary-tree.mjs +23715 -0
- package/dist/esm-legacy/graph.mjs +5221 -0
- package/dist/esm-legacy/hash.mjs +1259 -0
- package/dist/esm-legacy/heap.mjs +1531 -0
- package/dist/esm-legacy/index.mjs +32553 -10935
- package/dist/esm-legacy/linked-list.mjs +4369 -0
- package/dist/esm-legacy/matrix.mjs +1041 -0
- package/dist/esm-legacy/priority-queue.mjs +1308 -0
- package/dist/esm-legacy/queue.mjs +4084 -0
- package/dist/esm-legacy/stack.mjs +859 -0
- package/dist/esm-legacy/trie.mjs +1169 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
- package/dist/types/data-structures/base/linear-base.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
- package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
- package/dist/types/data-structures/heap/heap.d.ts +567 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +578 -71
- package/dist/types/data-structures/queue/queue.d.ts +451 -42
- package/dist/types/data-structures/stack/stack.d.ts +374 -32
- package/dist/types/data-structures/trie/trie.d.ts +458 -48
- package/dist/types/interfaces/graph.d.ts +1 -1
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/types/utils/validate-type.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +32432 -10808
- package/dist/umd/data-structure-typed.min.js +10 -4
- package/docs-site-docusaurus/README.md +41 -0
- package/docs-site-docusaurus/docs/api/README.md +52 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
- package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
- package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
- package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
- package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
- package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
- package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
- package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
- package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
- package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
- package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
- package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
- package/docs-site-docusaurus/docs/guide/guides.md +611 -0
- package/docs-site-docusaurus/docs/guide/installation.md +60 -0
- package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
- package/docs-site-docusaurus/docs/guide/overview.md +638 -0
- package/docs-site-docusaurus/docs/guide/performance.md +833 -0
- package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
- package/docs-site-docusaurus/docusaurus.config.ts +159 -0
- package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
- package/docs-site-docusaurus/package-lock.json +18667 -0
- package/docs-site-docusaurus/package.json +50 -0
- package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
- package/docs-site-docusaurus/sidebars.ts +23 -0
- package/docs-site-docusaurus/sort-protected.mjs +87 -0
- package/docs-site-docusaurus/src/css/custom.css +96 -0
- package/docs-site-docusaurus/src/pages/index.module.css +13 -0
- package/docs-site-docusaurus/src/pages/index.tsx +71 -0
- package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
- package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
- package/docs-site-docusaurus/static/.nojekyll +0 -0
- package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
- package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
- package/docs-site-docusaurus/static/img/favicon.ico +0 -0
- package/docs-site-docusaurus/static/img/favicon.png +0 -0
- package/docs-site-docusaurus/static/img/logo-180.png +0 -0
- package/docs-site-docusaurus/static/img/logo.jpg +0 -0
- package/docs-site-docusaurus/static/img/logo.png +0 -0
- package/docs-site-docusaurus/static/img/logo.svg +1 -0
- package/docs-site-docusaurus/static/img/og-image.png +0 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
- package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
- package/docs-site-docusaurus/static/robots.txt +4 -0
- package/docs-site-docusaurus/typedoc.json +23 -0
- package/package.json +109 -12
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/base/iterable-entry-base.ts +8 -8
- package/src/data-structures/base/linear-base.ts +3 -3
- package/src/data-structures/binary-tree/avl-tree.ts +386 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
- package/src/data-structures/binary-tree/binary-tree.ts +956 -81
- package/src/data-structures/binary-tree/bst.ts +840 -35
- package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
- package/src/data-structures/binary-tree/segment-tree.ts +498 -249
- package/src/data-structures/binary-tree/tree-map.ts +3784 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
- package/src/data-structures/binary-tree/tree-set.ts +3531 -10
- package/src/data-structures/graph/abstract-graph.ts +4 -4
- package/src/data-structures/graph/directed-graph.ts +429 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +393 -59
- package/src/data-structures/hash/hash-map.ts +476 -92
- package/src/data-structures/heap/heap.ts +581 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
- package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
- package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
- package/src/data-structures/matrix/matrix.ts +584 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +592 -70
- package/src/data-structures/queue/queue.ts +463 -42
- package/src/data-structures/stack/stack.ts +384 -32
- package/src/data-structures/trie/trie.ts +470 -48
- package/src/interfaces/graph.ts +1 -1
- package/src/types/common.ts +2 -2
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/utils/validate-type.ts +4 -4
- package/vercel.json +6 -0
- package/dist/leetcode/avl-tree-counter.mjs +0 -2957
- package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
- package/dist/leetcode/avl-tree.mjs +0 -2720
- package/dist/leetcode/binary-tree.mjs +0 -1594
- package/dist/leetcode/bst.mjs +0 -2398
- package/dist/leetcode/deque.mjs +0 -683
- package/dist/leetcode/directed-graph.mjs +0 -1733
- package/dist/leetcode/doubly-linked-list.mjs +0 -709
- package/dist/leetcode/hash-map.mjs +0 -493
- package/dist/leetcode/heap.mjs +0 -542
- package/dist/leetcode/max-heap.mjs +0 -375
- package/dist/leetcode/max-priority-queue.mjs +0 -383
- package/dist/leetcode/min-heap.mjs +0 -363
- package/dist/leetcode/min-priority-queue.mjs +0 -371
- package/dist/leetcode/priority-queue.mjs +0 -363
- package/dist/leetcode/queue.mjs +0 -943
- package/dist/leetcode/red-black-tree.mjs +0 -2765
- package/dist/leetcode/singly-linked-list.mjs +0 -754
- package/dist/leetcode/stack.mjs +0 -217
- package/dist/leetcode/tree-counter.mjs +0 -3039
- package/dist/leetcode/tree-multi-map.mjs +0 -2913
- package/dist/leetcode/trie.mjs +0 -413
- package/dist/leetcode/undirected-graph.mjs +0 -1650
|
@@ -9,6 +9,89 @@ import type { MatrixOptions } from '../../types';
|
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* Matrix — a numeric matrix with standard linear algebra operations.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Basic matrix arithmetic
|
|
17
|
+
* const a = new Matrix([
|
|
18
|
+
* [1, 2],
|
|
19
|
+
* [3, 4]
|
|
20
|
+
* ]);
|
|
21
|
+
* const b = new Matrix([
|
|
22
|
+
* [5, 6],
|
|
23
|
+
* [7, 8]
|
|
24
|
+
* ]);
|
|
25
|
+
*
|
|
26
|
+
* const sum = a.add(b);
|
|
27
|
+
* console.log(sum?.data); // [
|
|
28
|
+
* // [6, 8],
|
|
29
|
+
* // [10, 12]
|
|
30
|
+
* // ];
|
|
31
|
+
*
|
|
32
|
+
* const diff = b.subtract(a);
|
|
33
|
+
* console.log(diff?.data); // [
|
|
34
|
+
* // [4, 4],
|
|
35
|
+
* // [4, 4]
|
|
36
|
+
* // ];
|
|
37
|
+
* @example
|
|
38
|
+
* // Matrix multiplication for transformations
|
|
39
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
40
|
+
* const a = new Matrix([
|
|
41
|
+
* [1, 2, 3],
|
|
42
|
+
* [4, 5, 6]
|
|
43
|
+
* ]);
|
|
44
|
+
* const b = new Matrix([
|
|
45
|
+
* [7, 8],
|
|
46
|
+
* [9, 10],
|
|
47
|
+
* [11, 12]
|
|
48
|
+
* ]);
|
|
49
|
+
*
|
|
50
|
+
* const product = a.multiply(b);
|
|
51
|
+
* console.log(product?.rows); // 2;
|
|
52
|
+
* console.log(product?.cols); // 2;
|
|
53
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
54
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
55
|
+
* console.log(product?.data); // [
|
|
56
|
+
* // [58, 64],
|
|
57
|
+
* // [139, 154]
|
|
58
|
+
* // ];
|
|
59
|
+
* @example
|
|
60
|
+
* // Matrix transpose (square matrix)
|
|
61
|
+
* const m = new Matrix([
|
|
62
|
+
* [1, 2, 3],
|
|
63
|
+
* [4, 5, 6],
|
|
64
|
+
* [7, 8, 9]
|
|
65
|
+
* ]);
|
|
66
|
+
*
|
|
67
|
+
* const transposed = m.transpose();
|
|
68
|
+
* console.log(transposed.rows); // 3;
|
|
69
|
+
* console.log(transposed.cols); // 3;
|
|
70
|
+
* console.log(transposed.data); // [
|
|
71
|
+
* // [1, 4, 7],
|
|
72
|
+
* // [2, 5, 8],
|
|
73
|
+
* // [3, 6, 9]
|
|
74
|
+
* // ];
|
|
75
|
+
*
|
|
76
|
+
* // Transpose of transpose = original
|
|
77
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
78
|
+
* @example
|
|
79
|
+
* // Get and set individual cells
|
|
80
|
+
* const m = new Matrix([
|
|
81
|
+
* [0, 0, 0],
|
|
82
|
+
* [0, 0, 0]
|
|
83
|
+
* ]);
|
|
84
|
+
*
|
|
85
|
+
* m.set(0, 1, 42);
|
|
86
|
+
* m.set(1, 2, 99);
|
|
87
|
+
*
|
|
88
|
+
* console.log(m.get(0, 1)); // 42;
|
|
89
|
+
* console.log(m.get(1, 2)); // 99;
|
|
90
|
+
* console.log(m.get(0, 0)); // 0;
|
|
91
|
+
*
|
|
92
|
+
* // Out of bounds returns undefined
|
|
93
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
94
|
+
*/
|
|
12
95
|
export declare class Matrix {
|
|
13
96
|
/**
|
|
14
97
|
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
@@ -59,6 +142,53 @@ export declare class Matrix {
|
|
|
59
142
|
* retrieve from the data array.
|
|
60
143
|
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
61
144
|
* Otherwise, it returns `undefined`.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
* @example
|
|
177
|
+
* // Get and set individual cells
|
|
178
|
+
* const m = new Matrix([
|
|
179
|
+
* [0, 0, 0],
|
|
180
|
+
* [0, 0, 0]
|
|
181
|
+
* ]);
|
|
182
|
+
*
|
|
183
|
+
* m.set(0, 1, 42);
|
|
184
|
+
* m.set(1, 2, 99);
|
|
185
|
+
*
|
|
186
|
+
* console.log(m.get(0, 1)); // 42;
|
|
187
|
+
* console.log(m.get(1, 2)); // 99;
|
|
188
|
+
* console.log(m.get(0, 0)); // 0;
|
|
189
|
+
*
|
|
190
|
+
* // Out of bounds returns undefined
|
|
191
|
+
* console.log(m.get(5, 5)); // undefined;
|
|
62
192
|
*/
|
|
63
193
|
get(row: number, col: number): number | undefined;
|
|
64
194
|
/**
|
|
@@ -72,6 +202,44 @@ export declare class Matrix {
|
|
|
72
202
|
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
73
203
|
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
74
204
|
* set.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
* @example
|
|
237
|
+
* // Modify individual cells
|
|
238
|
+
* const m = Matrix.zeros(2, 2);
|
|
239
|
+
* console.log(m.set(0, 0, 5)); // true;
|
|
240
|
+
* console.log(m.set(1, 1, 10)); // true;
|
|
241
|
+
* console.log(m.get(0, 0)); // 5;
|
|
242
|
+
* console.log(m.get(1, 1)); // 10;
|
|
75
243
|
*/
|
|
76
244
|
set(row: number, col: number, value: number): boolean;
|
|
77
245
|
/**
|
|
@@ -86,6 +254,59 @@ export declare class Matrix {
|
|
|
86
254
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
87
255
|
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
88
256
|
* current matrix with the provided `matrix` parameter.
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
* @example
|
|
289
|
+
* // Basic matrix arithmetic
|
|
290
|
+
* const a = new Matrix([
|
|
291
|
+
* [1, 2],
|
|
292
|
+
* [3, 4]
|
|
293
|
+
* ]);
|
|
294
|
+
* const b = new Matrix([
|
|
295
|
+
* [5, 6],
|
|
296
|
+
* [7, 8]
|
|
297
|
+
* ]);
|
|
298
|
+
*
|
|
299
|
+
* const sum = a.add(b);
|
|
300
|
+
* console.log(sum?.data); // [
|
|
301
|
+
* // [6, 8],
|
|
302
|
+
* // [10, 12]
|
|
303
|
+
* // ];
|
|
304
|
+
*
|
|
305
|
+
* const diff = b.subtract(a);
|
|
306
|
+
* console.log(diff?.data); // [
|
|
307
|
+
* // [4, 4],
|
|
308
|
+
* // [4, 4]
|
|
309
|
+
* // ];
|
|
89
310
|
*/
|
|
90
311
|
add(matrix: Matrix): Matrix | undefined;
|
|
91
312
|
/**
|
|
@@ -94,6 +315,43 @@ export declare class Matrix {
|
|
|
94
315
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
95
316
|
* represents the matrix that you want to subtract from the current matrix.
|
|
96
317
|
* @returns a new Matrix object with the result of the subtraction operation.
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
* @example
|
|
350
|
+
* // Element-wise subtraction
|
|
351
|
+
* const a = Matrix.from([[5, 6], [7, 8]]);
|
|
352
|
+
* const b = Matrix.from([[1, 2], [3, 4]]);
|
|
353
|
+
* const result = a.subtract(b);
|
|
354
|
+
* console.log(result?.toArray()); // [[4, 4], [4, 4]];
|
|
97
355
|
*/
|
|
98
356
|
subtract(matrix: Matrix): Matrix | undefined;
|
|
99
357
|
/**
|
|
@@ -101,23 +359,205 @@ export declare class Matrix {
|
|
|
101
359
|
* as a new matrix.
|
|
102
360
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
103
361
|
* @returns a new Matrix object.
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
* @example
|
|
394
|
+
* // Matrix multiplication for transformations
|
|
395
|
+
* // 2x3 matrix * 3x2 matrix = 2x2 matrix
|
|
396
|
+
* const a = new Matrix([
|
|
397
|
+
* [1, 2, 3],
|
|
398
|
+
* [4, 5, 6]
|
|
399
|
+
* ]);
|
|
400
|
+
* const b = new Matrix([
|
|
401
|
+
* [7, 8],
|
|
402
|
+
* [9, 10],
|
|
403
|
+
* [11, 12]
|
|
404
|
+
* ]);
|
|
405
|
+
*
|
|
406
|
+
* const product = a.multiply(b);
|
|
407
|
+
* console.log(product?.rows); // 2;
|
|
408
|
+
* console.log(product?.cols); // 2;
|
|
409
|
+
* // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
|
|
410
|
+
* // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
|
|
411
|
+
* console.log(product?.data); // [
|
|
412
|
+
* // [58, 64],
|
|
413
|
+
* // [139, 154]
|
|
414
|
+
* // ];
|
|
104
415
|
*/
|
|
105
416
|
multiply(matrix: Matrix): Matrix | undefined;
|
|
106
417
|
/**
|
|
107
418
|
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
108
419
|
* original matrix.
|
|
109
420
|
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
* @example
|
|
453
|
+
* // Matrix transpose (square matrix)
|
|
454
|
+
* const m = new Matrix([
|
|
455
|
+
* [1, 2, 3],
|
|
456
|
+
* [4, 5, 6],
|
|
457
|
+
* [7, 8, 9]
|
|
458
|
+
* ]);
|
|
459
|
+
*
|
|
460
|
+
* const transposed = m.transpose();
|
|
461
|
+
* console.log(transposed.rows); // 3;
|
|
462
|
+
* console.log(transposed.cols); // 3;
|
|
463
|
+
* console.log(transposed.data); // [
|
|
464
|
+
* // [1, 4, 7],
|
|
465
|
+
* // [2, 5, 8],
|
|
466
|
+
* // [3, 6, 9]
|
|
467
|
+
* // ];
|
|
468
|
+
*
|
|
469
|
+
* // Transpose of transpose = original
|
|
470
|
+
* console.log(transposed.transpose().data); // m.data;
|
|
110
471
|
*/
|
|
111
472
|
transpose(): Matrix;
|
|
112
473
|
/**
|
|
113
474
|
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
114
475
|
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
* @example
|
|
508
|
+
* // Compute the inverse of a 2x2 matrix
|
|
509
|
+
* const m = Matrix.from([[4, 7], [2, 6]]);
|
|
510
|
+
* const inv = m.inverse();
|
|
511
|
+
* console.log(inv); // defined;
|
|
512
|
+
* // A * A^-1 should ≈ Identity
|
|
513
|
+
* const product = m.multiply(inv!);
|
|
514
|
+
* console.log(product?.get(0, 0)); // toBeCloseTo;
|
|
515
|
+
* console.log(product?.get(0, 1)); // toBeCloseTo;
|
|
516
|
+
* console.log(product?.get(1, 0)); // toBeCloseTo;
|
|
517
|
+
* console.log(product?.get(1, 1)); // toBeCloseTo;
|
|
115
518
|
*/
|
|
116
519
|
inverse(): Matrix | undefined;
|
|
117
520
|
/**
|
|
118
521
|
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
119
522
|
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
120
523
|
* @returns a new Matrix object.
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
* @example
|
|
556
|
+
* // Dot product of two matrices
|
|
557
|
+
* const a = Matrix.from([[1, 2], [3, 4]]);
|
|
558
|
+
* const b = Matrix.from([[5, 6], [7, 8]]);
|
|
559
|
+
* const result = a.dot(b);
|
|
560
|
+
* console.log(result?.toArray()); // [[19, 22], [43, 50]];
|
|
121
561
|
*/
|
|
122
562
|
dot(matrix: Matrix): Matrix | undefined;
|
|
123
563
|
/**
|
|
@@ -136,6 +576,57 @@ export declare class Matrix {
|
|
|
136
576
|
* and properties as the current instance.
|
|
137
577
|
*/
|
|
138
578
|
clone(): Matrix;
|
|
579
|
+
/**
|
|
580
|
+
* Returns [rows, cols] dimensions tuple.
|
|
581
|
+
*/
|
|
582
|
+
get size(): [number, number];
|
|
583
|
+
isEmpty(): boolean;
|
|
584
|
+
/**
|
|
585
|
+
* Returns a deep copy of the data as a plain 2D array.
|
|
586
|
+
*/
|
|
587
|
+
toArray(): number[][];
|
|
588
|
+
/**
|
|
589
|
+
* Returns a flat row-major array.
|
|
590
|
+
*/
|
|
591
|
+
flatten(): number[];
|
|
592
|
+
/**
|
|
593
|
+
* Iterates over rows.
|
|
594
|
+
*/
|
|
595
|
+
[Symbol.iterator](): IterableIterator<number[]>;
|
|
596
|
+
/**
|
|
597
|
+
* Visits each element with its row and column index.
|
|
598
|
+
*/
|
|
599
|
+
forEach(callback: (value: number, row: number, col: number) => void): void;
|
|
600
|
+
/**
|
|
601
|
+
* Maps each element (number → number) and returns a new Matrix.
|
|
602
|
+
*/
|
|
603
|
+
map(callback: (value: number, row: number, col: number) => number): Matrix;
|
|
604
|
+
print(): void;
|
|
605
|
+
/**
|
|
606
|
+
* Creates a rows×cols zero matrix.
|
|
607
|
+
* @example
|
|
608
|
+
* ```ts
|
|
609
|
+
* const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
static zeros(rows: number, cols: number): Matrix;
|
|
613
|
+
/**
|
|
614
|
+
* Creates an n×n identity matrix.
|
|
615
|
+
* @example
|
|
616
|
+
* ```ts
|
|
617
|
+
* const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
static identity(n: number): Matrix;
|
|
621
|
+
/**
|
|
622
|
+
* Creates a Matrix from a plain 2D array (deep copy).
|
|
623
|
+
* @example
|
|
624
|
+
* ```ts
|
|
625
|
+
* const m = Matrix.from([[1, 2], [3, 4]]);
|
|
626
|
+
* m.get(0, 1); // 2
|
|
627
|
+
* ```
|
|
628
|
+
*/
|
|
629
|
+
static from(data: number[][]): Matrix;
|
|
139
630
|
protected _addFn(a: number | undefined, b: number): number | undefined;
|
|
140
631
|
protected _subtractFn(a: number, b: number): number;
|
|
141
632
|
protected _multiplyFn(a: number, b: number): number;
|
|
@@ -14,6 +14,63 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Job scheduling by priority
|
|
18
|
+
* const jobs = new MaxPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(3); // low priority
|
|
21
|
+
* jobs.add(7); // high priority
|
|
22
|
+
* jobs.add(5); // medium priority
|
|
23
|
+
* jobs.add(10); // critical
|
|
24
|
+
*
|
|
25
|
+
* // Highest priority job first
|
|
26
|
+
* console.log(jobs.poll()); // 10;
|
|
27
|
+
* console.log(jobs.poll()); // 7;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 3;
|
|
30
|
+
* @example
|
|
31
|
+
* // Auction system with highest bid tracking
|
|
32
|
+
* interface Bid {
|
|
33
|
+
* bidder: string;
|
|
34
|
+
* amount: number;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const auction = new MaxPriorityQueue<Bid>([], {
|
|
38
|
+
* comparator: (a, b) => b.amount - a.amount
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* auction.add({ bidder: 'Alice', amount: 100 });
|
|
42
|
+
* auction.add({ bidder: 'Bob', amount: 250 });
|
|
43
|
+
* auction.add({ bidder: 'Charlie', amount: 175 });
|
|
44
|
+
*
|
|
45
|
+
* // Current highest bid
|
|
46
|
+
* console.log(auction.peek()?.bidder); // 'Bob';
|
|
47
|
+
* console.log(auction.peek()?.amount); // 250;
|
|
48
|
+
*
|
|
49
|
+
* // Process winning bid
|
|
50
|
+
* const winner = auction.poll()!;
|
|
51
|
+
* console.log(winner.bidder); // 'Bob';
|
|
52
|
+
* console.log(auction.peek()?.bidder); // 'Charlie';
|
|
53
|
+
* @example
|
|
54
|
+
* // CPU process scheduling
|
|
55
|
+
* const cpuQueue = new MaxPriorityQueue<[number, string]>([], {
|
|
56
|
+
* comparator: (a, b) => b[0] - a[0]
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* cpuQueue.add([5, 'System process']);
|
|
60
|
+
* cpuQueue.add([1, 'Background task']);
|
|
61
|
+
* cpuQueue.add([8, 'User interaction']);
|
|
62
|
+
* cpuQueue.add([3, 'Network sync']);
|
|
63
|
+
*
|
|
64
|
+
* const order = [];
|
|
65
|
+
* while (cpuQueue.size > 0) {
|
|
66
|
+
* order.push(cpuQueue.poll()![1]);
|
|
67
|
+
* }
|
|
68
|
+
* console.log(order); // [
|
|
69
|
+
* // 'User interaction',
|
|
70
|
+
* // 'System process',
|
|
71
|
+
* // 'Network sync',
|
|
72
|
+
* // 'Background task'
|
|
73
|
+
* // ];
|
|
17
74
|
*/
|
|
18
75
|
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
76
|
/**
|
|
@@ -14,6 +14,66 @@ import { PriorityQueue } from './priority-queue';
|
|
|
14
14
|
* @template E Element type stored in the queue.
|
|
15
15
|
* @template R Extra record/metadata associated with each element.
|
|
16
16
|
* @example
|
|
17
|
+
* // Shortest job first scheduling
|
|
18
|
+
* const jobs = new MinPriorityQueue<number>();
|
|
19
|
+
*
|
|
20
|
+
* jobs.add(8); // 8 seconds
|
|
21
|
+
* jobs.add(2); // 2 seconds
|
|
22
|
+
* jobs.add(5); // 5 seconds
|
|
23
|
+
* jobs.add(1); // 1 second
|
|
24
|
+
*
|
|
25
|
+
* // Shortest job first
|
|
26
|
+
* console.log(jobs.poll()); // 1;
|
|
27
|
+
* console.log(jobs.poll()); // 2;
|
|
28
|
+
* console.log(jobs.poll()); // 5;
|
|
29
|
+
* console.log(jobs.poll()); // 8;
|
|
30
|
+
* @example
|
|
31
|
+
* // Event-driven simulation with timestamps
|
|
32
|
+
* interface Event {
|
|
33
|
+
* time: number;
|
|
34
|
+
* action: string;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const timeline = new MinPriorityQueue<Event>([], {
|
|
38
|
+
* comparator: (a, b) => a.time - b.time
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* timeline.add({ time: 300, action: 'Timeout' });
|
|
42
|
+
* timeline.add({ time: 100, action: 'Request received' });
|
|
43
|
+
* timeline.add({ time: 200, action: 'Processing done' });
|
|
44
|
+
* timeline.add({ time: 150, action: 'Cache hit' });
|
|
45
|
+
*
|
|
46
|
+
* const order = [];
|
|
47
|
+
* while (timeline.size > 0) {
|
|
48
|
+
* order.push(timeline.poll()!.action);
|
|
49
|
+
* }
|
|
50
|
+
* console.log(order); // [
|
|
51
|
+
* // 'Request received',
|
|
52
|
+
* // 'Cache hit',
|
|
53
|
+
* // 'Processing done',
|
|
54
|
+
* // 'Timeout'
|
|
55
|
+
* // ];
|
|
56
|
+
* @example
|
|
57
|
+
* // Huffman coding frequency selection
|
|
58
|
+
* // Character frequencies for Huffman tree building
|
|
59
|
+
* const freq = new MinPriorityQueue<[number, string]>([], {
|
|
60
|
+
* comparator: (a, b) => a[0] - b[0]
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* freq.add([5, 'a']);
|
|
64
|
+
* freq.add([9, 'b']);
|
|
65
|
+
* freq.add([12, 'c']);
|
|
66
|
+
* freq.add([2, 'd']);
|
|
67
|
+
*
|
|
68
|
+
* // Always pick two lowest frequencies
|
|
69
|
+
* const first = freq.poll()!;
|
|
70
|
+
* const second = freq.poll()!;
|
|
71
|
+
* console.log(first[1]); // 'd'; // freq 2
|
|
72
|
+
* console.log(second[1]); // 'a'; // freq 5
|
|
73
|
+
*
|
|
74
|
+
* // Combined node goes back
|
|
75
|
+
* freq.add([first[0] + second[0], first[1] + second[1]]);
|
|
76
|
+
* console.log(freq.peek()![0]); // 7;
|
|
17
77
|
*/
|
|
18
78
|
export declare class MinPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
79
|
/**
|