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.
Files changed (240) hide show
  1. package/.vitepress/cache/deps_temp_51f5f1b0/chunk-7OIKW5WK.js +12984 -0
  2. package/.vitepress/cache/deps_temp_51f5f1b0/package.json +3 -0
  3. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vue_devtools-api.js +4505 -0
  4. package/.vitepress/cache/deps_temp_51f5f1b0/vitepress___@vueuse_core.js +9731 -0
  5. package/.vitepress/cache/deps_temp_51f5f1b0/vue.js +347 -0
  6. package/CHANGELOG.md +3 -1
  7. package/README.md +78 -31
  8. package/dist/cjs/binary-tree.cjs +23698 -0
  9. package/dist/cjs/graph.cjs +5236 -0
  10. package/dist/cjs/hash.cjs +1262 -0
  11. package/dist/cjs/heap.cjs +1540 -0
  12. package/dist/cjs/index.cjs +24509 -2899
  13. package/dist/cjs/linked-list.cjs +4370 -0
  14. package/dist/cjs/matrix.cjs +1042 -0
  15. package/dist/cjs/priority-queue.cjs +1314 -0
  16. package/dist/cjs/queue.cjs +4090 -0
  17. package/dist/cjs/stack.cjs +861 -0
  18. package/dist/cjs/trie.cjs +1173 -0
  19. package/dist/cjs-legacy/binary-tree.cjs +23730 -0
  20. package/dist/cjs-legacy/graph.cjs +5234 -0
  21. package/dist/cjs-legacy/hash.cjs +1262 -0
  22. package/dist/cjs-legacy/heap.cjs +1537 -0
  23. package/dist/cjs-legacy/index.cjs +32555 -10936
  24. package/dist/cjs-legacy/linked-list.cjs +4376 -0
  25. package/dist/cjs-legacy/matrix.cjs +1045 -0
  26. package/dist/cjs-legacy/priority-queue.cjs +1312 -0
  27. package/dist/cjs-legacy/queue.cjs +4088 -0
  28. package/dist/cjs-legacy/stack.cjs +861 -0
  29. package/dist/cjs-legacy/trie.cjs +1172 -0
  30. package/dist/esm/binary-tree.mjs +23683 -0
  31. package/dist/esm/graph.mjs +5223 -0
  32. package/dist/esm/hash.mjs +1259 -0
  33. package/dist/esm/heap.mjs +1534 -0
  34. package/dist/esm/index.mjs +24507 -2898
  35. package/dist/esm/linked-list.mjs +4363 -0
  36. package/dist/esm/matrix.mjs +1038 -0
  37. package/dist/esm/priority-queue.mjs +1310 -0
  38. package/dist/esm/queue.mjs +4086 -0
  39. package/dist/esm/stack.mjs +859 -0
  40. package/dist/esm/trie.mjs +1170 -0
  41. package/dist/esm-legacy/binary-tree.mjs +23715 -0
  42. package/dist/esm-legacy/graph.mjs +5221 -0
  43. package/dist/esm-legacy/hash.mjs +1259 -0
  44. package/dist/esm-legacy/heap.mjs +1531 -0
  45. package/dist/esm-legacy/index.mjs +32553 -10935
  46. package/dist/esm-legacy/linked-list.mjs +4369 -0
  47. package/dist/esm-legacy/matrix.mjs +1041 -0
  48. package/dist/esm-legacy/priority-queue.mjs +1308 -0
  49. package/dist/esm-legacy/queue.mjs +4084 -0
  50. package/dist/esm-legacy/stack.mjs +859 -0
  51. package/dist/esm-legacy/trie.mjs +1169 -0
  52. package/dist/types/data-structures/base/index.d.ts +1 -0
  53. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  54. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  55. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +368 -51
  57. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +473 -147
  58. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +931 -80
  59. package/dist/types/data-structures/binary-tree/bst.d.ts +792 -29
  60. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +592 -32
  61. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +320 -135
  62. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3662 -6
  63. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3487 -201
  64. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2778 -65
  65. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3414 -6
  66. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  67. package/dist/types/data-structures/graph/directed-graph.d.ts +419 -47
  68. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  69. package/dist/types/data-structures/graph/undirected-graph.d.ts +384 -59
  70. package/dist/types/data-structures/hash/hash-map.d.ts +462 -89
  71. package/dist/types/data-structures/heap/heap.d.ts +567 -99
  72. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  73. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  74. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +631 -49
  75. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +581 -68
  76. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +775 -12
  77. package/dist/types/data-structures/matrix/matrix.d.ts +491 -0
  78. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  79. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  80. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  81. package/dist/types/data-structures/queue/deque.d.ts +578 -71
  82. package/dist/types/data-structures/queue/queue.d.ts +451 -42
  83. package/dist/types/data-structures/stack/stack.d.ts +374 -32
  84. package/dist/types/data-structures/trie/trie.d.ts +458 -48
  85. package/dist/types/interfaces/graph.d.ts +1 -1
  86. package/dist/types/types/common.d.ts +2 -2
  87. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  88. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  89. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  90. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  91. package/dist/types/types/utils/validate-type.d.ts +4 -4
  92. package/dist/umd/data-structure-typed.js +32432 -10808
  93. package/dist/umd/data-structure-typed.min.js +10 -4
  94. package/docs-site-docusaurus/README.md +41 -0
  95. package/docs-site-docusaurus/docs/api/README.md +52 -0
  96. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +6130 -0
  97. package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +282 -0
  98. package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +2266 -0
  99. package/docs-site-docusaurus/docs/api/classes/BST.md +5831 -0
  100. package/docs-site-docusaurus/docs/api/classes/BSTNode.md +333 -0
  101. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +455 -0
  102. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +4647 -0
  103. package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +331 -0
  104. package/docs-site-docusaurus/docs/api/classes/Deque.md +2767 -0
  105. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +2999 -0
  106. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +2685 -0
  107. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +221 -0
  108. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +253 -0
  109. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +21 -0
  110. package/docs-site-docusaurus/docs/api/classes/HashMap.md +1333 -0
  111. package/docs-site-docusaurus/docs/api/classes/Heap.md +1881 -0
  112. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +800 -0
  113. package/docs-site-docusaurus/docs/api/classes/IterableEntryBase.md +644 -0
  114. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +1632 -0
  115. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +1853 -0
  116. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +1108 -0
  117. package/docs-site-docusaurus/docs/api/classes/LinkedListNode.md +156 -0
  118. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +2824 -0
  119. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +2929 -0
  120. package/docs-site-docusaurus/docs/api/classes/Matrix.md +1026 -0
  121. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +1866 -0
  122. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +1883 -0
  123. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +1879 -0
  124. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +1882 -0
  125. package/docs-site-docusaurus/docs/api/classes/Navigator.md +109 -0
  126. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +1839 -0
  127. package/docs-site-docusaurus/docs/api/classes/Queue.md +2244 -0
  128. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +6374 -0
  129. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +372 -0
  130. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +2897 -0
  131. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +169 -0
  132. package/docs-site-docusaurus/docs/api/classes/SkipList.md +1229 -0
  133. package/docs-site-docusaurus/docs/api/classes/Stack.md +1573 -0
  134. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +1257 -0
  135. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +1475 -0
  136. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +1117 -0
  137. package/docs-site-docusaurus/docs/api/classes/Trie.md +1708 -0
  138. package/docs-site-docusaurus/docs/api/classes/TrieNode.md +199 -0
  139. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +2979 -0
  140. package/docs-site-docusaurus/docs/guide/_category_.json +6 -0
  141. package/docs-site-docusaurus/docs/guide/architecture.md +613 -0
  142. package/docs-site-docusaurus/docs/guide/concepts.md +420 -0
  143. package/docs-site-docusaurus/docs/guide/guides.md +611 -0
  144. package/docs-site-docusaurus/docs/guide/installation.md +60 -0
  145. package/docs-site-docusaurus/docs/guide/integrations.md +823 -0
  146. package/docs-site-docusaurus/docs/guide/overview.md +638 -0
  147. package/docs-site-docusaurus/docs/guide/performance.md +833 -0
  148. package/docs-site-docusaurus/docs/guide/quick-start.md +73 -0
  149. package/docs-site-docusaurus/docusaurus.config.ts +159 -0
  150. package/docs-site-docusaurus/fix-mdx-generics.mjs +75 -0
  151. package/docs-site-docusaurus/package-lock.json +18667 -0
  152. package/docs-site-docusaurus/package.json +50 -0
  153. package/docs-site-docusaurus/prefix-class-to-methods.mjs +48 -0
  154. package/docs-site-docusaurus/sidebars.ts +23 -0
  155. package/docs-site-docusaurus/sort-protected.mjs +87 -0
  156. package/docs-site-docusaurus/src/css/custom.css +96 -0
  157. package/docs-site-docusaurus/src/pages/index.module.css +13 -0
  158. package/docs-site-docusaurus/src/pages/index.tsx +71 -0
  159. package/docs-site-docusaurus/src/pages/markdown-page.md +7 -0
  160. package/docs-site-docusaurus/src/theme/TOCItems/index.tsx +34 -0
  161. package/docs-site-docusaurus/static/.nojekyll +0 -0
  162. package/docs-site-docusaurus/static/img/docusaurus-social-card.jpg +0 -0
  163. package/docs-site-docusaurus/static/img/docusaurus.png +0 -0
  164. package/docs-site-docusaurus/static/img/favicon.ico +0 -0
  165. package/docs-site-docusaurus/static/img/favicon.png +0 -0
  166. package/docs-site-docusaurus/static/img/logo-180.png +0 -0
  167. package/docs-site-docusaurus/static/img/logo.jpg +0 -0
  168. package/docs-site-docusaurus/static/img/logo.png +0 -0
  169. package/docs-site-docusaurus/static/img/logo.svg +1 -0
  170. package/docs-site-docusaurus/static/img/og-image.png +0 -0
  171. package/docs-site-docusaurus/static/img/undraw_docusaurus_mountain.svg +171 -0
  172. package/docs-site-docusaurus/static/img/undraw_docusaurus_react.svg +170 -0
  173. package/docs-site-docusaurus/static/img/undraw_docusaurus_tree.svg +40 -0
  174. package/docs-site-docusaurus/static/robots.txt +4 -0
  175. package/docs-site-docusaurus/typedoc.json +23 -0
  176. package/package.json +109 -12
  177. package/src/data-structures/base/index.ts +1 -0
  178. package/src/data-structures/base/iterable-element-base.ts +4 -5
  179. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  180. package/src/data-structures/base/linear-base.ts +3 -3
  181. package/src/data-structures/binary-tree/avl-tree.ts +386 -51
  182. package/src/data-structures/binary-tree/binary-indexed-tree.ts +596 -247
  183. package/src/data-structures/binary-tree/binary-tree.ts +956 -81
  184. package/src/data-structures/binary-tree/bst.ts +840 -35
  185. package/src/data-structures/binary-tree/red-black-tree.ts +689 -97
  186. package/src/data-structures/binary-tree/segment-tree.ts +498 -249
  187. package/src/data-structures/binary-tree/tree-map.ts +3784 -7
  188. package/src/data-structures/binary-tree/tree-multi-map.ts +3614 -211
  189. package/src/data-structures/binary-tree/tree-multi-set.ts +2874 -65
  190. package/src/data-structures/binary-tree/tree-set.ts +3531 -10
  191. package/src/data-structures/graph/abstract-graph.ts +4 -4
  192. package/src/data-structures/graph/directed-graph.ts +429 -47
  193. package/src/data-structures/graph/map-graph.ts +59 -1
  194. package/src/data-structures/graph/undirected-graph.ts +393 -59
  195. package/src/data-structures/hash/hash-map.ts +476 -92
  196. package/src/data-structures/heap/heap.ts +581 -99
  197. package/src/data-structures/heap/max-heap.ts +46 -0
  198. package/src/data-structures/heap/min-heap.ts +59 -0
  199. package/src/data-structures/linked-list/doubly-linked-list.ts +646 -47
  200. package/src/data-structures/linked-list/singly-linked-list.ts +596 -68
  201. package/src/data-structures/linked-list/skip-linked-list.ts +1067 -90
  202. package/src/data-structures/matrix/matrix.ts +584 -12
  203. package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
  204. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  205. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  206. package/src/data-structures/queue/deque.ts +592 -70
  207. package/src/data-structures/queue/queue.ts +463 -42
  208. package/src/data-structures/stack/stack.ts +384 -32
  209. package/src/data-structures/trie/trie.ts +470 -48
  210. package/src/interfaces/graph.ts +1 -1
  211. package/src/types/common.ts +2 -2
  212. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  213. package/src/types/data-structures/heap/heap.ts +1 -0
  214. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  215. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  216. package/src/types/utils/validate-type.ts +4 -4
  217. package/vercel.json +6 -0
  218. package/dist/leetcode/avl-tree-counter.mjs +0 -2957
  219. package/dist/leetcode/avl-tree-multi-map.mjs +0 -2889
  220. package/dist/leetcode/avl-tree.mjs +0 -2720
  221. package/dist/leetcode/binary-tree.mjs +0 -1594
  222. package/dist/leetcode/bst.mjs +0 -2398
  223. package/dist/leetcode/deque.mjs +0 -683
  224. package/dist/leetcode/directed-graph.mjs +0 -1733
  225. package/dist/leetcode/doubly-linked-list.mjs +0 -709
  226. package/dist/leetcode/hash-map.mjs +0 -493
  227. package/dist/leetcode/heap.mjs +0 -542
  228. package/dist/leetcode/max-heap.mjs +0 -375
  229. package/dist/leetcode/max-priority-queue.mjs +0 -383
  230. package/dist/leetcode/min-heap.mjs +0 -363
  231. package/dist/leetcode/min-priority-queue.mjs +0 -371
  232. package/dist/leetcode/priority-queue.mjs +0 -363
  233. package/dist/leetcode/queue.mjs +0 -943
  234. package/dist/leetcode/red-black-tree.mjs +0 -2765
  235. package/dist/leetcode/singly-linked-list.mjs +0 -754
  236. package/dist/leetcode/stack.mjs +0 -217
  237. package/dist/leetcode/tree-counter.mjs +0 -3039
  238. package/dist/leetcode/tree-multi-map.mjs +0 -2913
  239. package/dist/leetcode/trie.mjs +0 -413
  240. package/dist/leetcode/undirected-graph.mjs +0 -1650
@@ -0,0 +1,1045 @@
1
+ 'use strict';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+
8
+ // src/common/error.ts
9
+ var ERR = {
10
+ // Range / index
11
+ indexOutOfRange: /* @__PURE__ */ __name((index, min, max, ctx) => `${ctx ? ctx + ": " : ""}Index ${index} is out of range [${min}, ${max}].`, "indexOutOfRange"),
12
+ invalidIndex: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Index must be an integer.`, "invalidIndex"),
13
+ // Type / argument
14
+ invalidArgument: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidArgument"),
15
+ comparatorRequired: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Comparator is required for non-number/non-string/non-Date keys.`, "comparatorRequired"),
16
+ invalidKey: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidKey"),
17
+ notAFunction: /* @__PURE__ */ __name((name, ctx) => `${ctx ? ctx + ": " : ""}${name} must be a function.`, "notAFunction"),
18
+ invalidEntry: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Each entry must be a [key, value] tuple.`, "invalidEntry"),
19
+ invalidNaN: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}NaN is not a valid key.`, "invalidNaN"),
20
+ invalidDate: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Invalid Date key.`, "invalidDate"),
21
+ reduceEmpty: /* @__PURE__ */ __name((ctx) => `${ctx ? ctx + ": " : ""}Reduce of empty structure with no initial value.`, "reduceEmpty"),
22
+ callbackReturnType: /* @__PURE__ */ __name((expected, got, ctx) => `${ctx ? ctx + ": " : ""}Callback must return ${expected}; got ${got}.`, "callbackReturnType"),
23
+ // State / operation
24
+ invalidOperation: /* @__PURE__ */ __name((reason, ctx) => `${ctx ? ctx + ": " : ""}${reason}`, "invalidOperation"),
25
+ // Matrix
26
+ matrixDimensionMismatch: /* @__PURE__ */ __name((op) => `Matrix: Dimensions must be compatible for ${op}.`, "matrixDimensionMismatch"),
27
+ matrixSingular: /* @__PURE__ */ __name(() => "Matrix: Singular matrix, inverse does not exist.", "matrixSingular"),
28
+ matrixNotSquare: /* @__PURE__ */ __name(() => "Matrix: Must be square for inversion.", "matrixNotSquare"),
29
+ matrixNotRectangular: /* @__PURE__ */ __name(() => "Matrix: Must be rectangular for transposition.", "matrixNotRectangular"),
30
+ matrixRowMismatch: /* @__PURE__ */ __name((expected, got) => `Matrix: Expected row length ${expected}, but got ${got}.`, "matrixRowMismatch")
31
+ };
32
+
33
+ // src/data-structures/matrix/matrix.ts
34
+ var _Matrix = class _Matrix {
35
+ /**
36
+ * The constructor function initializes a matrix object with the provided data and options, or with
37
+ * default values if no options are provided.
38
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
39
+ * @param [options] - The `options` parameter is an optional object that can contain the following
40
+ * properties:
41
+ */
42
+ constructor(data, options) {
43
+ __publicField(this, "_rows", 0);
44
+ __publicField(this, "_cols", 0);
45
+ __publicField(this, "_data");
46
+ var _a, _b, _c;
47
+ if (options) {
48
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
49
+ if (typeof rows === "number" && rows > 0) this._rows = rows;
50
+ else this._rows = data.length;
51
+ if (typeof cols === "number" && cols > 0) this._cols = cols;
52
+ else this._cols = ((_a = data[0]) == null ? void 0 : _a.length) || 0;
53
+ if (addFn) this._addFn = addFn;
54
+ if (subtractFn) this._subtractFn = subtractFn;
55
+ if (multiplyFn) this._multiplyFn = multiplyFn;
56
+ } else {
57
+ this._rows = data.length;
58
+ this._cols = (_c = (_b = data[0]) == null ? void 0 : _b.length) != null ? _c : 0;
59
+ }
60
+ if (data.length > 0) {
61
+ this._data = data;
62
+ } else {
63
+ this._data = [];
64
+ for (let i = 0; i < this.rows; i++) {
65
+ this._data[i] = new Array(this.cols).fill(0);
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * The function returns the number of rows.
71
+ * @returns The number of rows.
72
+ */
73
+ get rows() {
74
+ return this._rows;
75
+ }
76
+ /**
77
+ * The function returns the value of the protected variable _cols.
78
+ * @returns The number of columns.
79
+ */
80
+ get cols() {
81
+ return this._cols;
82
+ }
83
+ /**
84
+ * The function returns a two-dimensional array of numbers.
85
+ * @returns The data property, which is a two-dimensional array of numbers.
86
+ */
87
+ get data() {
88
+ return this._data;
89
+ }
90
+ /**
91
+ * The above function returns the value of the _addFn property.
92
+ * @returns The value of the property `_addFn` is being returned.
93
+ */
94
+ get addFn() {
95
+ return this._addFn;
96
+ }
97
+ /**
98
+ * The function returns the value of the _subtractFn property.
99
+ * @returns The `_subtractFn` property is being returned.
100
+ */
101
+ get subtractFn() {
102
+ return this._subtractFn;
103
+ }
104
+ /**
105
+ * The function returns the value of the _multiplyFn property.
106
+ * @returns The `_multiplyFn` property is being returned.
107
+ */
108
+ get multiplyFn() {
109
+ return this._multiplyFn;
110
+ }
111
+ /**
112
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
113
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
114
+ * retrieve from the data array.
115
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
116
+ * retrieve from the data array.
117
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
118
+ * Otherwise, it returns `undefined`.
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+ * @example
151
+ * // Get and set individual cells
152
+ * const m = new Matrix([
153
+ * [0, 0, 0],
154
+ * [0, 0, 0]
155
+ * ]);
156
+ *
157
+ * m.set(0, 1, 42);
158
+ * m.set(1, 2, 99);
159
+ *
160
+ * console.log(m.get(0, 1)); // 42;
161
+ * console.log(m.get(1, 2)); // 99;
162
+ * console.log(m.get(0, 0)); // 0;
163
+ *
164
+ * // Out of bounds returns undefined
165
+ * console.log(m.get(5, 5)); // undefined;
166
+ */
167
+ get(row, col) {
168
+ if (this.isValidIndex(row, col)) {
169
+ return this.data[row][col];
170
+ }
171
+ }
172
+ /**
173
+ * The set function updates the value at a specified row and column in a two-dimensional array.
174
+ * @param {number} row - The "row" parameter represents the row index of the element in a
175
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
176
+ * @param {number} col - The "col" parameter represents the column index of the element in a
177
+ * two-dimensional array.
178
+ * @param {number} value - The value parameter represents the number that you want to set at the
179
+ * specified row and column in the data array.
180
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
181
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
182
+ * set.
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+ * @example
215
+ * // Modify individual cells
216
+ * const m = Matrix.zeros(2, 2);
217
+ * console.log(m.set(0, 0, 5)); // true;
218
+ * console.log(m.set(1, 1, 10)); // true;
219
+ * console.log(m.get(0, 0)); // 5;
220
+ * console.log(m.get(1, 1)); // 10;
221
+ */
222
+ set(row, col, value) {
223
+ if (this.isValidIndex(row, col)) {
224
+ this.data[row][col] = value;
225
+ return true;
226
+ }
227
+ return false;
228
+ }
229
+ /**
230
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
231
+ * matrix.
232
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
233
+ * @returns a boolean value.
234
+ */
235
+ isMatchForCalculate(matrix) {
236
+ return this.rows === matrix.rows && this.cols === matrix.cols;
237
+ }
238
+ /**
239
+ * The `add` function adds two matrices together, returning a new matrix with the result.
240
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
241
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
242
+ * current matrix with the provided `matrix` parameter.
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+
270
+
271
+
272
+
273
+
274
+ * @example
275
+ * // Basic matrix arithmetic
276
+ * const a = new Matrix([
277
+ * [1, 2],
278
+ * [3, 4]
279
+ * ]);
280
+ * const b = new Matrix([
281
+ * [5, 6],
282
+ * [7, 8]
283
+ * ]);
284
+ *
285
+ * const sum = a.add(b);
286
+ * console.log(sum?.data); // [
287
+ * // [6, 8],
288
+ * // [10, 12]
289
+ * // ];
290
+ *
291
+ * const diff = b.subtract(a);
292
+ * console.log(diff?.data); // [
293
+ * // [4, 4],
294
+ * // [4, 4]
295
+ * // ];
296
+ */
297
+ add(matrix) {
298
+ var _a;
299
+ if (!this.isMatchForCalculate(matrix)) {
300
+ throw new Error(ERR.matrixDimensionMismatch("addition"));
301
+ }
302
+ const resultData = [];
303
+ for (let i = 0; i < this.rows; i++) {
304
+ resultData[i] = [];
305
+ for (let j = 0; j < this.cols; j++) {
306
+ const a = this.get(i, j), b = matrix.get(i, j);
307
+ if (a !== void 0 && b !== void 0) {
308
+ resultData[i][j] = (_a = this._addFn(a, b)) != null ? _a : 0;
309
+ }
310
+ }
311
+ }
312
+ return new _Matrix(resultData, {
313
+ rows: this.rows,
314
+ cols: this.cols,
315
+ addFn: this.addFn,
316
+ subtractFn: this.subtractFn,
317
+ multiplyFn: this.multiplyFn
318
+ });
319
+ }
320
+ /**
321
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
322
+ * matrix with the result.
323
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
324
+ * represents the matrix that you want to subtract from the current matrix.
325
+ * @returns a new Matrix object with the result of the subtraction operation.
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+
356
+
357
+ * @example
358
+ * // Element-wise subtraction
359
+ * const a = Matrix.from([[5, 6], [7, 8]]);
360
+ * const b = Matrix.from([[1, 2], [3, 4]]);
361
+ * const result = a.subtract(b);
362
+ * console.log(result?.toArray()); // [[4, 4], [4, 4]];
363
+ */
364
+ subtract(matrix) {
365
+ var _a;
366
+ if (!this.isMatchForCalculate(matrix)) {
367
+ throw new Error(ERR.matrixDimensionMismatch("subtraction"));
368
+ }
369
+ const resultData = [];
370
+ for (let i = 0; i < this.rows; i++) {
371
+ resultData[i] = [];
372
+ for (let j = 0; j < this.cols; j++) {
373
+ const a = this.get(i, j), b = matrix.get(i, j);
374
+ if (a !== void 0 && b !== void 0) {
375
+ resultData[i][j] = (_a = this._subtractFn(a, b)) != null ? _a : 0;
376
+ }
377
+ }
378
+ }
379
+ return new _Matrix(resultData, {
380
+ rows: this.rows,
381
+ cols: this.cols,
382
+ addFn: this.addFn,
383
+ subtractFn: this.subtractFn,
384
+ multiplyFn: this.multiplyFn
385
+ });
386
+ }
387
+ /**
388
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
389
+ * as a new matrix.
390
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
391
+ * @returns a new Matrix object.
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
423
+ * @example
424
+ * // Matrix multiplication for transformations
425
+ * // 2x3 matrix * 3x2 matrix = 2x2 matrix
426
+ * const a = new Matrix([
427
+ * [1, 2, 3],
428
+ * [4, 5, 6]
429
+ * ]);
430
+ * const b = new Matrix([
431
+ * [7, 8],
432
+ * [9, 10],
433
+ * [11, 12]
434
+ * ]);
435
+ *
436
+ * const product = a.multiply(b);
437
+ * console.log(product?.rows); // 2;
438
+ * console.log(product?.cols); // 2;
439
+ * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
440
+ * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
441
+ * console.log(product?.data); // [
442
+ * // [58, 64],
443
+ * // [139, 154]
444
+ * // ];
445
+ */
446
+ multiply(matrix) {
447
+ if (this.cols !== matrix.rows) {
448
+ throw new Error(ERR.matrixDimensionMismatch("multiplication (A.cols must equal B.rows)"));
449
+ }
450
+ const resultData = [];
451
+ for (let i = 0; i < this.rows; i++) {
452
+ resultData[i] = [];
453
+ for (let j = 0; j < matrix.cols; j++) {
454
+ let sum;
455
+ for (let k = 0; k < this.cols; k++) {
456
+ const a = this.get(i, k), b = matrix.get(k, j);
457
+ if (a !== void 0 && b !== void 0) {
458
+ const multiplied = this.multiplyFn(a, b);
459
+ if (multiplied !== void 0) {
460
+ sum = this.addFn(sum, multiplied);
461
+ }
462
+ }
463
+ }
464
+ if (sum !== void 0) resultData[i][j] = sum;
465
+ }
466
+ }
467
+ return new _Matrix(resultData, {
468
+ rows: this.rows,
469
+ cols: matrix.cols,
470
+ addFn: this.addFn,
471
+ subtractFn: this.subtractFn,
472
+ multiplyFn: this.multiplyFn
473
+ });
474
+ }
475
+ /**
476
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
477
+ * original matrix.
478
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
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
+
508
+
509
+
510
+ * @example
511
+ * // Matrix transpose (square matrix)
512
+ * const m = new Matrix([
513
+ * [1, 2, 3],
514
+ * [4, 5, 6],
515
+ * [7, 8, 9]
516
+ * ]);
517
+ *
518
+ * const transposed = m.transpose();
519
+ * console.log(transposed.rows); // 3;
520
+ * console.log(transposed.cols); // 3;
521
+ * console.log(transposed.data); // [
522
+ * // [1, 4, 7],
523
+ * // [2, 5, 8],
524
+ * // [3, 6, 9]
525
+ * // ];
526
+ *
527
+ * // Transpose of transpose = original
528
+ * console.log(transposed.transpose().data); // m.data;
529
+ */
530
+ transpose() {
531
+ if (this.data.some((row) => row.length !== this.cols)) {
532
+ throw new Error(ERR.matrixNotRectangular());
533
+ }
534
+ const resultData = [];
535
+ for (let j = 0; j < this.cols; j++) {
536
+ resultData[j] = [];
537
+ for (let i = 0; i < this.rows; i++) {
538
+ const trans = this.get(i, j);
539
+ if (trans !== void 0) resultData[j][i] = trans;
540
+ }
541
+ }
542
+ return new _Matrix(resultData, {
543
+ rows: this.cols,
544
+ cols: this.rows,
545
+ addFn: this.addFn,
546
+ subtractFn: this.subtractFn,
547
+ multiplyFn: this.multiplyFn
548
+ });
549
+ }
550
+ /**
551
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
552
+ * @returns a Matrix object, which represents the inverse of the original matrix.
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+ * @example
585
+ * // Compute the inverse of a 2x2 matrix
586
+ * const m = Matrix.from([[4, 7], [2, 6]]);
587
+ * const inv = m.inverse();
588
+ * console.log(inv); // defined;
589
+ * // A * A^-1 should ≈ Identity
590
+ * const product = m.multiply(inv!);
591
+ * console.log(product?.get(0, 0)); // toBeCloseTo;
592
+ * console.log(product?.get(0, 1)); // toBeCloseTo;
593
+ * console.log(product?.get(1, 0)); // toBeCloseTo;
594
+ * console.log(product?.get(1, 1)); // toBeCloseTo;
595
+ */
596
+ inverse() {
597
+ var _a;
598
+ if (this.rows !== this.cols) {
599
+ throw new Error(ERR.matrixNotSquare());
600
+ }
601
+ const augmentedMatrixData = [];
602
+ for (let i = 0; i < this.rows; i++) {
603
+ augmentedMatrixData[i] = this.data[i].slice();
604
+ for (let j = 0; j < this.cols; j++) {
605
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0;
606
+ }
607
+ }
608
+ const augmentedMatrix = new _Matrix(augmentedMatrixData, {
609
+ rows: this.rows,
610
+ cols: this.cols * 2,
611
+ addFn: this.addFn,
612
+ subtractFn: this.subtractFn,
613
+ multiplyFn: this.multiplyFn
614
+ });
615
+ for (let i = 0; i < this.rows; i++) {
616
+ let pivotRow = i;
617
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
618
+ pivotRow++;
619
+ }
620
+ if (pivotRow === this.rows) {
621
+ throw new Error(ERR.matrixSingular());
622
+ }
623
+ augmentedMatrix._swapRows(i, pivotRow);
624
+ const pivotElement = (_a = augmentedMatrix.get(i, i)) != null ? _a : 1;
625
+ if (pivotElement === 0) {
626
+ throw new Error(ERR.matrixSingular());
627
+ }
628
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
629
+ for (let j = 0; j < this.rows; j++) {
630
+ if (j !== i) {
631
+ let factor = augmentedMatrix.get(j, i);
632
+ if (factor === void 0) factor = 0;
633
+ augmentedMatrix._addScaledRow(j, i, -factor);
634
+ }
635
+ }
636
+ }
637
+ const inverseData = [];
638
+ for (let i = 0; i < this.rows; i++) {
639
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
640
+ }
641
+ return new _Matrix(inverseData, {
642
+ rows: this.rows,
643
+ cols: this.cols,
644
+ addFn: this.addFn,
645
+ subtractFn: this.subtractFn,
646
+ multiplyFn: this.multiplyFn
647
+ });
648
+ }
649
+ /**
650
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
651
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
652
+ * @returns a new Matrix object.
653
+
654
+
655
+
656
+
657
+
658
+
659
+
660
+
661
+
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+ * @example
685
+ * // Dot product of two matrices
686
+ * const a = Matrix.from([[1, 2], [3, 4]]);
687
+ * const b = Matrix.from([[5, 6], [7, 8]]);
688
+ * const result = a.dot(b);
689
+ * console.log(result?.toArray()); // [[19, 22], [43, 50]];
690
+ */
691
+ dot(matrix) {
692
+ if (this.cols !== matrix.rows) {
693
+ throw new Error(ERR.matrixDimensionMismatch("dot product (A.cols must equal B.rows)"));
694
+ }
695
+ const resultData = [];
696
+ for (let i = 0; i < this.rows; i++) {
697
+ resultData[i] = [];
698
+ for (let j = 0; j < matrix.cols; j++) {
699
+ let sum;
700
+ for (let k = 0; k < this.cols; k++) {
701
+ const a = this.get(i, k), b = matrix.get(k, j);
702
+ if (a !== void 0 && b !== void 0) {
703
+ const multiplied = this.multiplyFn(a, b);
704
+ if (multiplied !== void 0) {
705
+ sum = this.addFn(sum, multiplied);
706
+ }
707
+ }
708
+ }
709
+ if (sum !== void 0) resultData[i][j] = sum;
710
+ }
711
+ }
712
+ return new _Matrix(resultData, {
713
+ rows: this.rows,
714
+ cols: matrix.cols,
715
+ addFn: this.addFn,
716
+ subtractFn: this.subtractFn,
717
+ multiplyFn: this.multiplyFn
718
+ });
719
+ }
720
+ /**
721
+ * The function checks if a given row and column index is valid within a specified range.
722
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
723
+ * matrix. It is a number that indicates the specific row in the matrix.
724
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
725
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
726
+ * @returns A boolean value is being returned.
727
+ */
728
+ isValidIndex(row, col) {
729
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
730
+ }
731
+ /**
732
+ * The `clone` function returns a new instance of the Matrix class with the same data and properties
733
+ * as the original instance.
734
+ * @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
735
+ * and properties as the current instance.
736
+ */
737
+ clone() {
738
+ return new _Matrix(
739
+ this._data.map((row) => [...row]),
740
+ {
741
+ rows: this.rows,
742
+ cols: this.cols,
743
+ addFn: this.addFn,
744
+ subtractFn: this.subtractFn,
745
+ multiplyFn: this.multiplyFn
746
+ }
747
+ );
748
+ }
749
+ // ─── Standard interface ─────────────────────────────────────
750
+ /**
751
+ * Returns [rows, cols] dimensions tuple.
752
+ */
753
+ get size() {
754
+ return [this._rows, this._cols];
755
+ }
756
+ isEmpty() {
757
+ return this._rows === 0 || this._cols === 0;
758
+ }
759
+ /**
760
+ * Returns a deep copy of the data as a plain 2D array.
761
+ */
762
+ toArray() {
763
+ return this._data.map((row) => [...row]);
764
+ }
765
+ /**
766
+ * Returns a flat row-major array.
767
+ */
768
+ flatten() {
769
+ const result = [];
770
+ for (const row of this._data) {
771
+ for (const v of row) result.push(v);
772
+ }
773
+ return result;
774
+ }
775
+ /**
776
+ * Iterates over rows.
777
+ */
778
+ [Symbol.iterator]() {
779
+ const data = this._data;
780
+ let i = 0;
781
+ return {
782
+ [Symbol.iterator]() {
783
+ return this;
784
+ },
785
+ next() {
786
+ if (i < data.length) {
787
+ return { value: [...data[i++]], done: false };
788
+ }
789
+ return { value: void 0, done: true };
790
+ }
791
+ };
792
+ }
793
+ /**
794
+ * Visits each element with its row and column index.
795
+ */
796
+ forEach(callback) {
797
+ for (let i = 0; i < this._rows; i++) {
798
+ for (let j = 0; j < this._cols; j++) {
799
+ callback(this._data[i][j], i, j);
800
+ }
801
+ }
802
+ }
803
+ /**
804
+ * Maps each element (number → number) and returns a new Matrix.
805
+ */
806
+ map(callback) {
807
+ const resultData = [];
808
+ for (let i = 0; i < this._rows; i++) {
809
+ resultData[i] = [];
810
+ for (let j = 0; j < this._cols; j++) {
811
+ resultData[i][j] = callback(this._data[i][j], i, j);
812
+ }
813
+ }
814
+ return new _Matrix(resultData, {
815
+ rows: this._rows,
816
+ cols: this._cols,
817
+ addFn: this.addFn,
818
+ subtractFn: this.subtractFn,
819
+ multiplyFn: this.multiplyFn
820
+ });
821
+ }
822
+ print() {
823
+ for (const row of this._data) {
824
+ console.log(row.join(" "));
825
+ }
826
+ }
827
+ // ─── Factory methods ────────────────────────────────────────
828
+ /**
829
+ * Creates a rows×cols zero matrix.
830
+ * @example
831
+ * ```ts
832
+ * const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
833
+ * ```
834
+ */
835
+ static zeros(rows, cols) {
836
+ const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
837
+ return new _Matrix(data);
838
+ }
839
+ /**
840
+ * Creates an n×n identity matrix.
841
+ * @example
842
+ * ```ts
843
+ * const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
844
+ * ```
845
+ */
846
+ static identity(n) {
847
+ const data = Array.from(
848
+ { length: n },
849
+ (_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
850
+ );
851
+ return new _Matrix(data);
852
+ }
853
+ /**
854
+ * Creates a Matrix from a plain 2D array (deep copy).
855
+ * @example
856
+ * ```ts
857
+ * const m = Matrix.from([[1, 2], [3, 4]]);
858
+ * m.get(0, 1); // 2
859
+ * ```
860
+ */
861
+ static from(data) {
862
+ return new _Matrix(data.map((row) => [...row]));
863
+ }
864
+ _addFn(a, b) {
865
+ if (a === void 0) return b;
866
+ return a + b;
867
+ }
868
+ _subtractFn(a, b) {
869
+ return a - b;
870
+ }
871
+ _multiplyFn(a, b) {
872
+ return a * b;
873
+ }
874
+ /**
875
+ * The function `_swapRows` swaps the positions of two rows in an array.
876
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
877
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
878
+ * with the first row.
879
+ */
880
+ _swapRows(row1, row2) {
881
+ const temp = this.data[row1];
882
+ this.data[row1] = this.data[row2];
883
+ this.data[row2] = temp;
884
+ }
885
+ /**
886
+ * The function scales a specific row in a matrix by a given scalar value.
887
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
888
+ * want to scale. It is a number that indicates the position of the row within the matrix.
889
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
890
+ * a specific row of a matrix.
891
+ */
892
+ _scaleRow(row, scalar) {
893
+ for (let j = 0; j < this.cols; j++) {
894
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
895
+ if (multiplied === void 0) multiplied = 0;
896
+ this.data[row][j] = multiplied;
897
+ }
898
+ }
899
+ /**
900
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
901
+ * row.
902
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
903
+ * scaled values will be added.
904
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
905
+ * values will be scaled and added to the targetRow.
906
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
907
+ * source row before adding them to the target row.
908
+ */
909
+ _addScaledRow(targetRow, sourceRow, scalar) {
910
+ for (let j = 0; j < this.cols; j++) {
911
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
912
+ if (multiplied === void 0) multiplied = 0;
913
+ const scaledValue = multiplied;
914
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
915
+ if (added === void 0) added = 0;
916
+ this.data[targetRow][j] = added;
917
+ }
918
+ }
919
+ };
920
+ __name(_Matrix, "Matrix");
921
+ var Matrix = _Matrix;
922
+
923
+ // src/data-structures/matrix/navigator.ts
924
+ var _Character = class _Character {
925
+ /**
926
+ * The constructor function takes in a direction and turning object and sets the direction and turn properties of the
927
+ * Character class.
928
+ * @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
929
+ * can be any value that represents a direction, such as "north", "south", "east", or "west".
930
+ * @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
931
+ * turning direction. It is used to determine the new direction when the character turns.
932
+ */
933
+ constructor(direction, turning) {
934
+ __publicField(this, "direction");
935
+ __publicField(this, "turn");
936
+ this.direction = direction;
937
+ this.turn = () => new _Character(turning[direction], turning);
938
+ }
939
+ };
940
+ __name(_Character, "Character");
941
+ var Character = _Character;
942
+ var _Navigator = class _Navigator {
943
+ /**
944
+ * The constructor initializes the Navigator object with the given parameters and sets the current position as visited
945
+ * in the matrix.
946
+ * @param - - `matrix`: a 2D array representing the grid or map
947
+ */
948
+ constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
949
+ __publicField(this, "onMove");
950
+ __publicField(this, "_matrix");
951
+ __publicField(this, "_cur");
952
+ __publicField(this, "_character");
953
+ __publicField(this, "_VISITED");
954
+ this._matrix = matrix;
955
+ this._cur = cur;
956
+ this._character = new Character(charDir, turning);
957
+ this.onMove = onMove;
958
+ if (this.onMove) this.onMove(this._cur);
959
+ this._VISITED = VISITED;
960
+ this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
961
+ }
962
+ /**
963
+ * The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
964
+ * character and repeats the process.
965
+ */
966
+ start() {
967
+ while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
968
+ const { direction } = this._character;
969
+ if (this.check(direction)) {
970
+ this.move(direction);
971
+ } else if (this.check(this._character.turn().direction)) {
972
+ this._character = this._character.turn();
973
+ }
974
+ }
975
+ }
976
+ /**
977
+ * The function checks if there is a valid move in the specified direction in a matrix.
978
+ * @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
979
+ * It can be one of the following values: 'up', 'right', 'down', or 'left'.
980
+ * @returns a boolean value.
981
+ */
982
+ check(direction) {
983
+ let forward, row;
984
+ const matrix = this._matrix;
985
+ const [i, j] = this._cur;
986
+ switch (direction) {
987
+ case "up":
988
+ row = matrix[i - 1];
989
+ if (!row) return false;
990
+ forward = row[j];
991
+ break;
992
+ case "right":
993
+ forward = matrix[i][j + 1];
994
+ break;
995
+ case "down":
996
+ row = matrix[i + 1];
997
+ if (!row) return false;
998
+ forward = row[j];
999
+ break;
1000
+ case "left":
1001
+ forward = matrix[i][j - 1];
1002
+ break;
1003
+ }
1004
+ return forward !== void 0 && forward !== this._VISITED;
1005
+ }
1006
+ /**
1007
+ * The `move` function updates the current position based on the given direction and updates the matrix accordingly.
1008
+ * @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
1009
+ * It can have one of the following values: 'up', 'right', 'down', or 'left'.
1010
+ */
1011
+ move(direction) {
1012
+ switch (direction) {
1013
+ case "up":
1014
+ this._cur[0]--;
1015
+ break;
1016
+ case "right":
1017
+ this._cur[1]++;
1018
+ break;
1019
+ case "down":
1020
+ this._cur[0]++;
1021
+ break;
1022
+ case "left":
1023
+ this._cur[1]--;
1024
+ break;
1025
+ }
1026
+ const [i, j] = this._cur;
1027
+ this._matrix[i][j] = this._VISITED;
1028
+ if (this.onMove) this.onMove(this._cur);
1029
+ }
1030
+ };
1031
+ __name(_Navigator, "Navigator");
1032
+ var Navigator = _Navigator;
1033
+ /**
1034
+ * data-structure-typed
1035
+ *
1036
+ * @author Pablo Zeng
1037
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1038
+ * @license MIT License
1039
+ */
1040
+
1041
+ exports.Character = Character;
1042
+ exports.Matrix = Matrix;
1043
+ exports.Navigator = Navigator;
1044
+ //# sourceMappingURL=matrix.cjs.map
1045
+ //# sourceMappingURL=matrix.cjs.map