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,1076 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/common/error.ts
5
+ function raise(ErrorClass, message) {
6
+ throw new ErrorClass(message);
7
+ }
8
+ __name(raise, "raise");
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
+ // Order statistic
32
+ orderStatisticNotEnabled: /* @__PURE__ */ __name((method, ctx) => `${ctx ? ctx + ": " : ""}${method}() requires enableOrderStatistic: true.`, "orderStatisticNotEnabled")
33
+ };
34
+
35
+ // src/data-structures/matrix/matrix.ts
36
+ var Matrix = class _Matrix {
37
+ static {
38
+ __name(this, "Matrix");
39
+ }
40
+ /**
41
+ * The constructor function initializes a matrix object with the provided data and options, or with
42
+ * default values if no options are provided.
43
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
44
+ * @param [options] - The `options` parameter is an optional object that can contain the following
45
+ * properties:
46
+ */
47
+ constructor(data, options) {
48
+ if (options) {
49
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
50
+ if (typeof rows === "number" && rows > 0) this._rows = rows;
51
+ else this._rows = data.length;
52
+ if (typeof cols === "number" && cols > 0) this._cols = cols;
53
+ else this._cols = data[0]?.length || 0;
54
+ if (addFn) this._addFn = addFn;
55
+ if (subtractFn) this._subtractFn = subtractFn;
56
+ if (multiplyFn) this._multiplyFn = multiplyFn;
57
+ } else {
58
+ this._rows = data.length;
59
+ this._cols = data[0]?.length ?? 0;
60
+ }
61
+ if (data.length > 0) {
62
+ this._data = data;
63
+ } else {
64
+ this._data = [];
65
+ for (let i = 0; i < this.rows; i++) {
66
+ this._data[i] = new Array(this.cols).fill(0);
67
+ }
68
+ }
69
+ }
70
+ _rows = 0;
71
+ /**
72
+ * The function returns the number of rows.
73
+ * @returns The number of rows.
74
+ */
75
+ get rows() {
76
+ return this._rows;
77
+ }
78
+ _cols = 0;
79
+ /**
80
+ * The function returns the value of the protected variable _cols.
81
+ * @returns The number of columns.
82
+ */
83
+ get cols() {
84
+ return this._cols;
85
+ }
86
+ _data;
87
+ /**
88
+ * The function returns a two-dimensional array of numbers.
89
+ * @returns The data property, which is a two-dimensional array of numbers.
90
+ */
91
+ get data() {
92
+ return this._data;
93
+ }
94
+ /**
95
+ * The above function returns the value of the _addFn property.
96
+ * @returns The value of the property `_addFn` is being returned.
97
+ */
98
+ get addFn() {
99
+ return this._addFn;
100
+ }
101
+ /**
102
+ * The function returns the value of the _subtractFn property.
103
+ * @returns The `_subtractFn` property is being returned.
104
+ */
105
+ get subtractFn() {
106
+ return this._subtractFn;
107
+ }
108
+ /**
109
+ * The function returns the value of the _multiplyFn property.
110
+ * @returns The `_multiplyFn` property is being returned.
111
+ */
112
+ get multiplyFn() {
113
+ return this._multiplyFn;
114
+ }
115
+ /**
116
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
117
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
118
+ * retrieve from the data array.
119
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
120
+ * retrieve from the data array.
121
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
122
+ * Otherwise, it returns `undefined`.
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
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+ * @example
159
+ * // Get and set individual cells
160
+ * const m = new Matrix([
161
+ * [0, 0, 0],
162
+ * [0, 0, 0]
163
+ * ]);
164
+ *
165
+ * m.set(0, 1, 42);
166
+ * m.set(1, 2, 99);
167
+ *
168
+ * console.log(m.get(0, 1)); // 42;
169
+ * console.log(m.get(1, 2)); // 99;
170
+ * console.log(m.get(0, 0)); // 0;
171
+ *
172
+ * // Out of bounds returns undefined
173
+ * console.log(m.get(5, 5)); // undefined;
174
+ */
175
+ get(row, col) {
176
+ if (this.isValidIndex(row, col)) {
177
+ return this.data[row][col];
178
+ }
179
+ }
180
+ /**
181
+ * The set function updates the value at a specified row and column in a two-dimensional array.
182
+ * @param {number} row - The "row" parameter represents the row index of the element in a
183
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
184
+ * @param {number} col - The "col" parameter represents the column index of the element in a
185
+ * two-dimensional array.
186
+ * @param {number} value - The value parameter represents the number that you want to set at the
187
+ * specified row and column in the data array.
188
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
189
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
190
+ * set.
191
+
192
+
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
+ * @example
227
+ * // Modify individual cells
228
+ * const m = Matrix.zeros(2, 2);
229
+ * console.log(m.set(0, 0, 5)); // true;
230
+ * console.log(m.set(1, 1, 10)); // true;
231
+ * console.log(m.get(0, 0)); // 5;
232
+ * console.log(m.get(1, 1)); // 10;
233
+ */
234
+ set(row, col, value) {
235
+ if (this.isValidIndex(row, col)) {
236
+ this.data[row][col] = value;
237
+ return true;
238
+ }
239
+ return false;
240
+ }
241
+ /**
242
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
243
+ * matrix.
244
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
245
+ * @returns a boolean value.
246
+ */
247
+ isMatchForCalculate(matrix) {
248
+ return this.rows === matrix.rows && this.cols === matrix.cols;
249
+ }
250
+ /**
251
+ * The `add` function adds two matrices together, returning a new matrix with the result.
252
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
253
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
254
+ * current matrix with the provided `matrix` parameter.
255
+
256
+
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
+ * @example
291
+ * // Basic matrix arithmetic
292
+ * const a = new Matrix([
293
+ * [1, 2],
294
+ * [3, 4]
295
+ * ]);
296
+ * const b = new Matrix([
297
+ * [5, 6],
298
+ * [7, 8]
299
+ * ]);
300
+ *
301
+ * const sum = a.add(b);
302
+ * console.log(sum?.data); // [
303
+ * // [6, 8],
304
+ * // [10, 12]
305
+ * // ];
306
+ *
307
+ * const diff = b.subtract(a);
308
+ * console.log(diff?.data); // [
309
+ * // [4, 4],
310
+ * // [4, 4]
311
+ * // ];
312
+ */
313
+ add(matrix) {
314
+ if (!this.isMatchForCalculate(matrix)) {
315
+ raise(Error, ERR.matrixDimensionMismatch("addition"));
316
+ }
317
+ const resultData = [];
318
+ for (let i = 0; i < this.rows; i++) {
319
+ resultData[i] = [];
320
+ for (let j = 0; j < this.cols; j++) {
321
+ const a = this.get(i, j), b = matrix.get(i, j);
322
+ if (a !== void 0 && b !== void 0) {
323
+ resultData[i][j] = this._addFn(a, b) ?? 0;
324
+ }
325
+ }
326
+ }
327
+ return new _Matrix(resultData, {
328
+ rows: this.rows,
329
+ cols: this.cols,
330
+ addFn: this.addFn,
331
+ subtractFn: this.subtractFn,
332
+ multiplyFn: this.multiplyFn
333
+ });
334
+ }
335
+ /**
336
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
337
+ * matrix with the result.
338
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
339
+ * represents the matrix that you want to subtract from the current matrix.
340
+ * @returns a new Matrix object with the result of the subtraction operation.
341
+
342
+
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
+ * @example
377
+ * // Element-wise subtraction
378
+ * const a = Matrix.from([[5, 6], [7, 8]]);
379
+ * const b = Matrix.from([[1, 2], [3, 4]]);
380
+ * const result = a.subtract(b);
381
+ * console.log(result?.toArray()); // [[4, 4], [4, 4]];
382
+ */
383
+ subtract(matrix) {
384
+ if (!this.isMatchForCalculate(matrix)) {
385
+ raise(Error, ERR.matrixDimensionMismatch("subtraction"));
386
+ }
387
+ const resultData = [];
388
+ for (let i = 0; i < this.rows; i++) {
389
+ resultData[i] = [];
390
+ for (let j = 0; j < this.cols; j++) {
391
+ const a = this.get(i, j), b = matrix.get(i, j);
392
+ if (a !== void 0 && b !== void 0) {
393
+ resultData[i][j] = this._subtractFn(a, b) ?? 0;
394
+ }
395
+ }
396
+ }
397
+ return new _Matrix(resultData, {
398
+ rows: this.rows,
399
+ cols: this.cols,
400
+ addFn: this.addFn,
401
+ subtractFn: this.subtractFn,
402
+ multiplyFn: this.multiplyFn
403
+ });
404
+ }
405
+ /**
406
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
407
+ * as a new matrix.
408
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
409
+ * @returns a new Matrix object.
410
+
411
+
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
+ * @example
446
+ * // Matrix multiplication for transformations
447
+ * // 2x3 matrix * 3x2 matrix = 2x2 matrix
448
+ * const a = new Matrix([
449
+ * [1, 2, 3],
450
+ * [4, 5, 6]
451
+ * ]);
452
+ * const b = new Matrix([
453
+ * [7, 8],
454
+ * [9, 10],
455
+ * [11, 12]
456
+ * ]);
457
+ *
458
+ * const product = a.multiply(b);
459
+ * console.log(product?.rows); // 2;
460
+ * console.log(product?.cols); // 2;
461
+ * // Row 0: 1*7+2*9+3*11=58, 1*8+2*10+3*12=64
462
+ * // Row 1: 4*7+5*9+6*11=139, 4*8+5*10+6*12=154
463
+ * console.log(product?.data); // [
464
+ * // [58, 64],
465
+ * // [139, 154]
466
+ * // ];
467
+ */
468
+ multiply(matrix) {
469
+ if (this.cols !== matrix.rows) {
470
+ raise(Error, ERR.matrixDimensionMismatch("multiplication (A.cols must equal B.rows)"));
471
+ }
472
+ const resultData = [];
473
+ for (let i = 0; i < this.rows; i++) {
474
+ resultData[i] = [];
475
+ for (let j = 0; j < matrix.cols; j++) {
476
+ let sum;
477
+ for (let k = 0; k < this.cols; k++) {
478
+ const a = this.get(i, k), b = matrix.get(k, j);
479
+ if (a !== void 0 && b !== void 0) {
480
+ const multiplied = this.multiplyFn(a, b);
481
+ if (multiplied !== void 0) {
482
+ sum = this.addFn(sum, multiplied);
483
+ }
484
+ }
485
+ }
486
+ if (sum !== void 0) resultData[i][j] = sum;
487
+ }
488
+ }
489
+ return new _Matrix(resultData, {
490
+ rows: this.rows,
491
+ cols: matrix.cols,
492
+ addFn: this.addFn,
493
+ subtractFn: this.subtractFn,
494
+ multiplyFn: this.multiplyFn
495
+ });
496
+ }
497
+ /**
498
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
499
+ * original matrix.
500
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
501
+
502
+
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
+ * @example
537
+ * // Matrix transpose (square matrix)
538
+ * const m = new Matrix([
539
+ * [1, 2, 3],
540
+ * [4, 5, 6],
541
+ * [7, 8, 9]
542
+ * ]);
543
+ *
544
+ * const transposed = m.transpose();
545
+ * console.log(transposed.rows); // 3;
546
+ * console.log(transposed.cols); // 3;
547
+ * console.log(transposed.data); // [
548
+ * // [1, 4, 7],
549
+ * // [2, 5, 8],
550
+ * // [3, 6, 9]
551
+ * // ];
552
+ *
553
+ * // Transpose of transpose = original
554
+ * console.log(transposed.transpose().data); // m.data;
555
+ */
556
+ transpose() {
557
+ if (this.data.some((row) => row.length !== this.cols)) {
558
+ raise(Error, ERR.matrixNotRectangular());
559
+ }
560
+ const resultData = [];
561
+ for (let j = 0; j < this.cols; j++) {
562
+ resultData[j] = [];
563
+ for (let i = 0; i < this.rows; i++) {
564
+ const trans = this.get(i, j);
565
+ if (trans !== void 0) resultData[j][i] = trans;
566
+ }
567
+ }
568
+ return new _Matrix(resultData, {
569
+ rows: this.cols,
570
+ cols: this.rows,
571
+ addFn: this.addFn,
572
+ subtractFn: this.subtractFn,
573
+ multiplyFn: this.multiplyFn
574
+ });
575
+ }
576
+ /**
577
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
578
+ * @returns a Matrix object, which represents the inverse of the original matrix.
579
+
580
+
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
+ * @example
615
+ * // Compute the inverse of a 2x2 matrix
616
+ * const m = Matrix.from([[4, 7], [2, 6]]);
617
+ * const inv = m.inverse();
618
+ * console.log(inv); // defined;
619
+ * // A * A^-1 should ≈ Identity
620
+ * const product = m.multiply(inv!);
621
+ * console.log(product?.get(0, 0)); // toBeCloseTo;
622
+ * console.log(product?.get(0, 1)); // toBeCloseTo;
623
+ * console.log(product?.get(1, 0)); // toBeCloseTo;
624
+ * console.log(product?.get(1, 1)); // toBeCloseTo;
625
+ */
626
+ inverse() {
627
+ if (this.rows !== this.cols) {
628
+ raise(Error, ERR.matrixNotSquare());
629
+ }
630
+ const augmentedMatrixData = [];
631
+ for (let i = 0; i < this.rows; i++) {
632
+ augmentedMatrixData[i] = this.data[i].slice();
633
+ for (let j = 0; j < this.cols; j++) {
634
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0;
635
+ }
636
+ }
637
+ const augmentedMatrix = new _Matrix(augmentedMatrixData, {
638
+ rows: this.rows,
639
+ cols: this.cols * 2,
640
+ addFn: this.addFn,
641
+ subtractFn: this.subtractFn,
642
+ multiplyFn: this.multiplyFn
643
+ });
644
+ for (let i = 0; i < this.rows; i++) {
645
+ let pivotRow = i;
646
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
647
+ pivotRow++;
648
+ }
649
+ if (pivotRow === this.rows) {
650
+ raise(Error, ERR.matrixSingular());
651
+ }
652
+ augmentedMatrix._swapRows(i, pivotRow);
653
+ const pivotElement = augmentedMatrix.get(i, i) ?? 1;
654
+ if (pivotElement === 0) {
655
+ raise(Error, ERR.matrixSingular());
656
+ }
657
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
658
+ for (let j = 0; j < this.rows; j++) {
659
+ if (j !== i) {
660
+ let factor = augmentedMatrix.get(j, i);
661
+ if (factor === void 0) factor = 0;
662
+ augmentedMatrix._addScaledRow(j, i, -factor);
663
+ }
664
+ }
665
+ }
666
+ const inverseData = [];
667
+ for (let i = 0; i < this.rows; i++) {
668
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
669
+ }
670
+ return new _Matrix(inverseData, {
671
+ rows: this.rows,
672
+ cols: this.cols,
673
+ addFn: this.addFn,
674
+ subtractFn: this.subtractFn,
675
+ multiplyFn: this.multiplyFn
676
+ });
677
+ }
678
+ /**
679
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
680
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
681
+ * @returns a new Matrix object.
682
+
683
+
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
+ * @example
718
+ * // Dot product of two matrices
719
+ * const a = Matrix.from([[1, 2], [3, 4]]);
720
+ * const b = Matrix.from([[5, 6], [7, 8]]);
721
+ * const result = a.dot(b);
722
+ * console.log(result?.toArray()); // [[19, 22], [43, 50]];
723
+ */
724
+ dot(matrix) {
725
+ if (this.cols !== matrix.rows) {
726
+ raise(Error, ERR.matrixDimensionMismatch("dot product (A.cols must equal B.rows)"));
727
+ }
728
+ const resultData = [];
729
+ for (let i = 0; i < this.rows; i++) {
730
+ resultData[i] = [];
731
+ for (let j = 0; j < matrix.cols; j++) {
732
+ let sum;
733
+ for (let k = 0; k < this.cols; k++) {
734
+ const a = this.get(i, k), b = matrix.get(k, j);
735
+ if (a !== void 0 && b !== void 0) {
736
+ const multiplied = this.multiplyFn(a, b);
737
+ if (multiplied !== void 0) {
738
+ sum = this.addFn(sum, multiplied);
739
+ }
740
+ }
741
+ }
742
+ if (sum !== void 0) resultData[i][j] = sum;
743
+ }
744
+ }
745
+ return new _Matrix(resultData, {
746
+ rows: this.rows,
747
+ cols: matrix.cols,
748
+ addFn: this.addFn,
749
+ subtractFn: this.subtractFn,
750
+ multiplyFn: this.multiplyFn
751
+ });
752
+ }
753
+ /**
754
+ * The function checks if a given row and column index is valid within a specified range.
755
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
756
+ * matrix. It is a number that indicates the specific row in the matrix.
757
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
758
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
759
+ * @returns A boolean value is being returned.
760
+ */
761
+ isValidIndex(row, col) {
762
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
763
+ }
764
+ /**
765
+ * The `clone` function returns a new instance of the Matrix class with the same data and properties
766
+ * as the original instance.
767
+ * @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
768
+ * and properties as the current instance.
769
+ */
770
+ clone() {
771
+ return new _Matrix(
772
+ this._data.map((row) => [...row]),
773
+ {
774
+ rows: this.rows,
775
+ cols: this.cols,
776
+ addFn: this.addFn,
777
+ subtractFn: this.subtractFn,
778
+ multiplyFn: this.multiplyFn
779
+ }
780
+ );
781
+ }
782
+ // ─── Standard interface ─────────────────────────────────────
783
+ /**
784
+ * Returns [rows, cols] dimensions tuple.
785
+ */
786
+ get size() {
787
+ return [this._rows, this._cols];
788
+ }
789
+ isEmpty() {
790
+ return this._rows === 0 || this._cols === 0;
791
+ }
792
+ /**
793
+ * Returns a deep copy of the data as a plain 2D array.
794
+ */
795
+ toArray() {
796
+ return this._data.map((row) => [...row]);
797
+ }
798
+ /**
799
+ * Returns a flat row-major array.
800
+ */
801
+ flatten() {
802
+ const result = [];
803
+ for (const row of this._data) {
804
+ for (const v of row) result.push(v);
805
+ }
806
+ return result;
807
+ }
808
+ /**
809
+ * Iterates over rows.
810
+ */
811
+ [Symbol.iterator]() {
812
+ const data = this._data;
813
+ let i = 0;
814
+ return {
815
+ [Symbol.iterator]() {
816
+ return this;
817
+ },
818
+ next() {
819
+ if (i < data.length) {
820
+ return { value: [...data[i++]], done: false };
821
+ }
822
+ return { value: void 0, done: true };
823
+ }
824
+ };
825
+ }
826
+ /**
827
+ * Visits each element with its row and column index.
828
+ */
829
+ forEach(callback) {
830
+ for (let i = 0; i < this._rows; i++) {
831
+ for (let j = 0; j < this._cols; j++) {
832
+ callback(this._data[i][j], i, j);
833
+ }
834
+ }
835
+ }
836
+ /**
837
+ * Maps each element (number → number) and returns a new Matrix.
838
+ */
839
+ map(callback) {
840
+ const resultData = [];
841
+ for (let i = 0; i < this._rows; i++) {
842
+ resultData[i] = [];
843
+ for (let j = 0; j < this._cols; j++) {
844
+ resultData[i][j] = callback(this._data[i][j], i, j);
845
+ }
846
+ }
847
+ return new _Matrix(resultData, {
848
+ rows: this._rows,
849
+ cols: this._cols,
850
+ addFn: this.addFn,
851
+ subtractFn: this.subtractFn,
852
+ multiplyFn: this.multiplyFn
853
+ });
854
+ }
855
+ print() {
856
+ for (const row of this._data) {
857
+ console.log(row.join(" "));
858
+ }
859
+ }
860
+ // ─── Factory methods ────────────────────────────────────────
861
+ /**
862
+ * Creates a rows×cols zero matrix.
863
+ * @example
864
+ * ```ts
865
+ * const z = Matrix.zeros(2, 3); // [[0,0,0],[0,0,0]]
866
+ * ```
867
+ */
868
+ static zeros(rows, cols) {
869
+ const data = Array.from({ length: rows }, () => new Array(cols).fill(0));
870
+ return new _Matrix(data);
871
+ }
872
+ /**
873
+ * Creates an n×n identity matrix.
874
+ * @example
875
+ * ```ts
876
+ * const I = Matrix.identity(3); // [[1,0,0],[0,1,0],[0,0,1]]
877
+ * ```
878
+ */
879
+ static identity(n) {
880
+ const data = Array.from(
881
+ { length: n },
882
+ (_, i) => Array.from({ length: n }, (_2, j) => i === j ? 1 : 0)
883
+ );
884
+ return new _Matrix(data);
885
+ }
886
+ /**
887
+ * Creates a Matrix from a plain 2D array (deep copy).
888
+ * @example
889
+ * ```ts
890
+ * const m = Matrix.from([[1, 2], [3, 4]]);
891
+ * m.get(0, 1); // 2
892
+ * ```
893
+ */
894
+ static from(data) {
895
+ return new _Matrix(data.map((row) => [...row]));
896
+ }
897
+ _addFn(a, b) {
898
+ if (a === void 0) return b;
899
+ return a + b;
900
+ }
901
+ _subtractFn(a, b) {
902
+ return a - b;
903
+ }
904
+ _multiplyFn(a, b) {
905
+ return a * b;
906
+ }
907
+ /**
908
+ * The function `_swapRows` swaps the positions of two rows in an array.
909
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
910
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
911
+ * with the first row.
912
+ */
913
+ _swapRows(row1, row2) {
914
+ const temp = this.data[row1];
915
+ this.data[row1] = this.data[row2];
916
+ this.data[row2] = temp;
917
+ }
918
+ /**
919
+ * The function scales a specific row in a matrix by a given scalar value.
920
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
921
+ * want to scale. It is a number that indicates the position of the row within the matrix.
922
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
923
+ * a specific row of a matrix.
924
+ */
925
+ _scaleRow(row, scalar) {
926
+ for (let j = 0; j < this.cols; j++) {
927
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
928
+ if (multiplied === void 0) multiplied = 0;
929
+ this.data[row][j] = multiplied;
930
+ }
931
+ }
932
+ /**
933
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
934
+ * row.
935
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
936
+ * scaled values will be added.
937
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
938
+ * values will be scaled and added to the targetRow.
939
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
940
+ * source row before adding them to the target row.
941
+ */
942
+ _addScaledRow(targetRow, sourceRow, scalar) {
943
+ for (let j = 0; j < this.cols; j++) {
944
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
945
+ if (multiplied === void 0) multiplied = 0;
946
+ const scaledValue = multiplied;
947
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
948
+ if (added === void 0) added = 0;
949
+ this.data[targetRow][j] = added;
950
+ }
951
+ }
952
+ };
953
+
954
+ // src/data-structures/matrix/navigator.ts
955
+ var Character = class _Character {
956
+ static {
957
+ __name(this, "Character");
958
+ }
959
+ direction;
960
+ turn;
961
+ /**
962
+ * The constructor function takes in a direction and turning object and sets the direction and turn properties of the
963
+ * Character class.
964
+ * @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
965
+ * can be any value that represents a direction, such as "north", "south", "east", or "west".
966
+ * @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
967
+ * turning direction. It is used to determine the new direction when the character turns.
968
+ */
969
+ constructor(direction, turning) {
970
+ this.direction = direction;
971
+ this.turn = () => new _Character(turning[direction], turning);
972
+ }
973
+ };
974
+ var Navigator = class {
975
+ static {
976
+ __name(this, "Navigator");
977
+ }
978
+ onMove;
979
+ _matrix;
980
+ _cur;
981
+ _character;
982
+ _VISITED;
983
+ /**
984
+ * The constructor initializes the Navigator object with the given parameters and sets the current position as visited
985
+ * in the matrix.
986
+ * @param - - `matrix`: a 2D array representing the grid or map
987
+ */
988
+ constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
989
+ this._matrix = matrix;
990
+ this._cur = cur;
991
+ this._character = new Character(charDir, turning);
992
+ this.onMove = onMove;
993
+ if (this.onMove) this.onMove(this._cur);
994
+ this._VISITED = VISITED;
995
+ this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
996
+ }
997
+ /**
998
+ * The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
999
+ * character and repeats the process.
1000
+ */
1001
+ start() {
1002
+ while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
1003
+ const { direction } = this._character;
1004
+ if (this.check(direction)) {
1005
+ this.move(direction);
1006
+ } else if (this.check(this._character.turn().direction)) {
1007
+ this._character = this._character.turn();
1008
+ }
1009
+ }
1010
+ }
1011
+ /**
1012
+ * The function checks if there is a valid move in the specified direction in a matrix.
1013
+ * @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
1014
+ * It can be one of the following values: 'up', 'right', 'down', or 'left'.
1015
+ * @returns a boolean value.
1016
+ */
1017
+ check(direction) {
1018
+ let forward, row;
1019
+ const matrix = this._matrix;
1020
+ const [i, j] = this._cur;
1021
+ switch (direction) {
1022
+ case "up":
1023
+ row = matrix[i - 1];
1024
+ if (!row) return false;
1025
+ forward = row[j];
1026
+ break;
1027
+ case "right":
1028
+ forward = matrix[i][j + 1];
1029
+ break;
1030
+ case "down":
1031
+ row = matrix[i + 1];
1032
+ if (!row) return false;
1033
+ forward = row[j];
1034
+ break;
1035
+ case "left":
1036
+ forward = matrix[i][j - 1];
1037
+ break;
1038
+ }
1039
+ return forward !== void 0 && forward !== this._VISITED;
1040
+ }
1041
+ /**
1042
+ * The `move` function updates the current position based on the given direction and updates the matrix accordingly.
1043
+ * @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
1044
+ * It can have one of the following values: 'up', 'right', 'down', or 'left'.
1045
+ */
1046
+ move(direction) {
1047
+ switch (direction) {
1048
+ case "up":
1049
+ this._cur[0]--;
1050
+ break;
1051
+ case "right":
1052
+ this._cur[1]++;
1053
+ break;
1054
+ case "down":
1055
+ this._cur[0]++;
1056
+ break;
1057
+ case "left":
1058
+ this._cur[1]--;
1059
+ break;
1060
+ }
1061
+ const [i, j] = this._cur;
1062
+ this._matrix[i][j] = this._VISITED;
1063
+ if (this.onMove) this.onMove(this._cur);
1064
+ }
1065
+ };
1066
+ /**
1067
+ * data-structure-typed
1068
+ *
1069
+ * @author Pablo Zeng
1070
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
1071
+ * @license MIT License
1072
+ */
1073
+
1074
+ export { Character, Matrix, Navigator };
1075
+ //# sourceMappingURL=matrix.mjs.map
1076
+ //# sourceMappingURL=matrix.mjs.map