data-structure-typed 2.5.0 → 2.5.1

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