data-structure-typed 2.5.0 → 2.5.2

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