data-structure-typed 1.49.3 → 1.49.5

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 (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +70 -69
  3. package/README_zh-CN.md +44 -49
  4. package/benchmark/report.html +16 -16
  5. package/benchmark/report.json +187 -187
  6. package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
  7. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  15. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -11
  18. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -19
  19. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  20. package/dist/cjs/data-structures/graph/directed-graph.js +4 -0
  21. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  24. package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
  25. package/dist/cjs/data-structures/hash/hash-map.js +2 -2
  26. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  27. package/dist/cjs/data-structures/heap/heap.js +2 -3
  28. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  29. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  30. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  32. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  33. package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
  34. package/dist/cjs/data-structures/matrix/index.js +0 -2
  35. package/dist/cjs/data-structures/matrix/index.js.map +1 -1
  36. package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
  37. package/dist/cjs/data-structures/matrix/matrix.js +400 -15
  38. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  39. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  40. package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
  41. package/dist/cjs/data-structures/queue/deque.js +5 -7
  42. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  43. package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  45. package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
  46. package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  47. package/dist/cjs/utils/utils.d.ts +1 -0
  48. package/dist/cjs/utils/utils.js +6 -1
  49. package/dist/cjs/utils/utils.js.map +1 -1
  50. package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
  51. package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
  52. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -14
  53. package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
  54. package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
  55. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
  56. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
  57. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
  58. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -11
  59. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -20
  60. package/dist/mjs/data-structures/graph/directed-graph.js +4 -0
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
  62. package/dist/mjs/data-structures/hash/hash-map.js +2 -2
  63. package/dist/mjs/data-structures/heap/heap.js +2 -3
  64. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  65. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
  66. package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
  67. package/dist/mjs/data-structures/matrix/index.js +0 -2
  68. package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
  69. package/dist/mjs/data-structures/matrix/matrix.js +399 -16
  70. package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
  71. package/dist/mjs/data-structures/queue/deque.js +7 -9
  72. package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
  73. package/dist/mjs/data-structures/queue/queue.js +1 -1
  74. package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
  75. package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
  76. package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  77. package/dist/mjs/utils/utils.d.ts +1 -0
  78. package/dist/mjs/utils/utils.js +4 -0
  79. package/dist/umd/data-structure-typed.js +361 -542
  80. package/dist/umd/data-structure-typed.min.js +2 -2
  81. package/dist/umd/data-structure-typed.min.js.map +1 -1
  82. package/package.json +4 -3
  83. package/src/data-structures/base/index.ts +1 -1
  84. package/src/data-structures/base/iterable-base.ts +7 -10
  85. package/src/data-structures/binary-tree/avl-tree.ts +15 -8
  86. package/src/data-structures/binary-tree/binary-tree.ts +57 -74
  87. package/src/data-structures/binary-tree/bst.ts +16 -13
  88. package/src/data-structures/binary-tree/rb-tree.ts +16 -10
  89. package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
  90. package/src/data-structures/graph/abstract-graph.ts +14 -24
  91. package/src/data-structures/graph/directed-graph.ts +8 -6
  92. package/src/data-structures/graph/map-graph.ts +6 -1
  93. package/src/data-structures/graph/undirected-graph.ts +4 -7
  94. package/src/data-structures/hash/hash-map.ts +18 -16
  95. package/src/data-structures/heap/heap.ts +7 -10
  96. package/src/data-structures/heap/max-heap.ts +2 -1
  97. package/src/data-structures/heap/min-heap.ts +2 -1
  98. package/src/data-structures/linked-list/singly-linked-list.ts +3 -5
  99. package/src/data-structures/matrix/index.ts +0 -2
  100. package/src/data-structures/matrix/matrix.ts +442 -13
  101. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  102. package/src/data-structures/queue/deque.ts +18 -39
  103. package/src/data-structures/queue/queue.ts +1 -1
  104. package/src/interfaces/binary-tree.ts +7 -2
  105. package/src/types/common.ts +4 -4
  106. package/src/types/data-structures/base/base.ts +14 -3
  107. package/src/types/data-structures/base/index.ts +1 -1
  108. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  109. package/src/types/data-structures/hash/hash-map.ts +3 -3
  110. package/src/types/data-structures/heap/heap.ts +2 -2
  111. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  112. package/src/utils/utils.ts +7 -1
  113. package/test/integration/avl-tree.test.ts +18 -1
  114. package/test/integration/bst.test.ts +2 -2
  115. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
  116. package/test/performance/data-structures/comparison/comparison.test.ts +18 -23
  117. package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
  118. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +16 -28
  119. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +4 -12
  120. package/test/performance/data-structures/queue/deque.test.ts +11 -12
  121. package/test/performance/data-structures/queue/queue.test.ts +15 -14
  122. package/test/performance/data-structures/stack/stack.test.ts +11 -11
  123. package/test/performance/reportor.ts +14 -15
  124. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
  125. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +65 -15
  126. package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
  127. package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
  128. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
  129. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
  130. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  131. package/test/unit/data-structures/graph/directed-graph.test.ts +112 -40
  132. package/test/unit/data-structures/graph/undirected-graph.test.ts +63 -33
  133. package/test/unit/data-structures/hash/hash-map.test.ts +64 -23
  134. package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
  135. package/test/unit/data-structures/heap/heap.test.ts +0 -1
  136. package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
  137. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -4
  138. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
  139. package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
  140. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
  141. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
  142. package/test/unit/data-structures/queue/deque.test.ts +0 -6
  143. package/test/unit/data-structures/queue/queue.test.ts +1 -1
  144. package/test/unit/data-structures/stack/stack.test.ts +3 -4
  145. package/test/unit/unrestricted-interconversion.test.ts +50 -44
  146. package/test/utils/big-o.ts +14 -14
  147. package/test/utils/performanc.ts +1 -1
  148. package/typedoc.json +30 -0
  149. package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
  150. package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
  151. package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
  152. package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
  153. package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
  154. package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
  155. package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
  156. package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
  157. package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
  158. package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
  159. package/src/data-structures/matrix/matrix2d.ts +0 -211
  160. package/src/data-structures/matrix/vector2d.ts +0 -315
  161. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
  162. package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
@@ -135,8 +135,7 @@ var dataStructureTyped = (() => {
135
135
  MapEdge: () => MapEdge,
136
136
  MapGraph: () => MapGraph,
137
137
  MapVertex: () => MapVertex,
138
- Matrix2D: () => Matrix2D,
139
- MatrixNTI2D: () => MatrixNTI2D,
138
+ Matrix: () => Matrix,
140
139
  MaxHeap: () => MaxHeap,
141
140
  MaxPriorityQueue: () => MaxPriorityQueue,
142
141
  MinHeap: () => MinHeap,
@@ -163,13 +162,13 @@ var dataStructureTyped = (() => {
163
162
  UndirectedEdge: () => UndirectedEdge,
164
163
  UndirectedGraph: () => UndirectedGraph,
165
164
  UndirectedVertex: () => UndirectedVertex,
166
- Vector2D: () => Vector2D,
167
165
  arrayRemove: () => arrayRemove,
168
166
  calcMinUnitsRequired: () => calcMinUnitsRequired,
169
167
  getMSB: () => getMSB,
170
168
  isThunk: () => isThunk,
171
169
  isWeakKey: () => isWeakKey,
172
170
  rangeCheck: () => rangeCheck,
171
+ roundFixed: () => roundFixed,
173
172
  throwRangeError: () => throwRangeError,
174
173
  toThunk: () => toThunk,
175
174
  trampoline: () => trampoline,
@@ -847,6 +846,10 @@ var dataStructureTyped = (() => {
847
846
  return inputType === "object" && input !== null || inputType === "function";
848
847
  };
849
848
  var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
849
+ var roundFixed = (num, digit = 10) => {
850
+ const multiplier = Math.pow(10, digit);
851
+ return Math.round(num * multiplier) / multiplier;
852
+ };
850
853
 
851
854
  // src/data-structures/hash/hash-map.ts
852
855
  var HashMap = class _HashMap extends IterableEntryBase {
@@ -3859,10 +3862,7 @@ var dataStructureTyped = (() => {
3859
3862
  */
3860
3863
  getAt(pos) {
3861
3864
  rangeCheck(pos, 0, this.size - 1);
3862
- const {
3863
- bucketIndex,
3864
- indexInBucket
3865
- } = this._getBucketAndPosition(pos);
3865
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3866
3866
  return this._buckets[bucketIndex][indexInBucket];
3867
3867
  }
3868
3868
  /**
@@ -3881,10 +3881,7 @@ var dataStructureTyped = (() => {
3881
3881
  */
3882
3882
  setAt(pos, element) {
3883
3883
  rangeCheck(pos, 0, this.size - 1);
3884
- const {
3885
- bucketIndex,
3886
- indexInBucket
3887
- } = this._getBucketAndPosition(pos);
3884
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3888
3885
  this._buckets[bucketIndex][indexInBucket] = element;
3889
3886
  return true;
3890
3887
  }
@@ -3948,10 +3945,7 @@ var dataStructureTyped = (() => {
3948
3945
  this.clear();
3949
3946
  return 0;
3950
3947
  }
3951
- const {
3952
- bucketIndex,
3953
- indexInBucket
3954
- } = this._getBucketAndPosition(pos);
3948
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
3955
3949
  this._bucketLast = bucketIndex;
3956
3950
  this._lastInBucket = indexInBucket;
3957
3951
  this._size = pos + 1;
@@ -3980,15 +3974,9 @@ var dataStructureTyped = (() => {
3980
3974
  this.pop();
3981
3975
  else {
3982
3976
  const length = this.size - 1;
3983
- let {
3984
- bucketIndex: curBucket,
3985
- indexInBucket: curPointer
3986
- } = this._getBucketAndPosition(pos);
3977
+ let { bucketIndex: curBucket, indexInBucket: curPointer } = this._getBucketAndPosition(pos);
3987
3978
  for (let i = pos; i < length; ++i) {
3988
- const {
3989
- bucketIndex: nextBucket,
3990
- indexInBucket: nextPointer
3991
- } = this._getBucketAndPosition(pos + 1);
3979
+ const { bucketIndex: nextBucket, indexInBucket: nextPointer } = this._getBucketAndPosition(pos + 1);
3992
3980
  this._buckets[curBucket][curPointer] = this._buckets[nextBucket][nextPointer];
3993
3981
  curBucket = nextBucket;
3994
3982
  curPointer = nextPointer;
@@ -5252,23 +5240,6 @@ var dataStructureTyped = (() => {
5252
5240
  const potentialKeyType = typeof potentialKey;
5253
5241
  return potentialKeyType === "string" || potentialKeyType === "number";
5254
5242
  }
5255
- /**
5256
- * Time Complexity: O(1) - Constant time for Map operations.
5257
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
5258
- */
5259
- /**
5260
- * Time Complexity: O(1) - Constant time for Map operations.
5261
- * Space Complexity: O(1) - Constant space, as it creates only a few variables.
5262
- *
5263
- * The `deleteVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
5264
- * @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
5265
- * (`VertexKey`).
5266
- * @returns The method is returning a boolean value.
5267
- */
5268
- deleteVertex(vertexOrKey) {
5269
- const vertexKey = this._getVertexKey(vertexOrKey);
5270
- return this._vertexMap.delete(vertexKey);
5271
- }
5272
5243
  /**
5273
5244
  * Time Complexity: O(K), where K is the number of vertexMap to be removed.
5274
5245
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -6187,9 +6158,7 @@ var dataStructureTyped = (() => {
6187
6158
  uniqueCycles.set(sorted, cycle);
6188
6159
  }
6189
6160
  }
6190
- return [...uniqueCycles].map(
6191
- (cycleString) => cycleString[1]
6192
- );
6161
+ return [...uniqueCycles].map((cycleString) => cycleString[1]);
6193
6162
  }
6194
6163
  /**
6195
6164
  * Time Complexity: O(n)
@@ -6476,6 +6445,10 @@ var dataStructureTyped = (() => {
6476
6445
  vertexKey = this._getVertexKey(vertexOrKey);
6477
6446
  }
6478
6447
  if (vertex) {
6448
+ const neighbors = this.getNeighbors(vertex);
6449
+ for (const neighbor of neighbors) {
6450
+ this._inEdgeMap.delete(neighbor);
6451
+ }
6479
6452
  this._outEdgeMap.delete(vertex);
6480
6453
  this._inEdgeMap.delete(vertex);
6481
6454
  }
@@ -7356,7 +7329,7 @@ var dataStructureTyped = (() => {
7356
7329
  __publicField(this, "_extractor", (key) => Number(key));
7357
7330
  __publicField(this, "_root");
7358
7331
  __publicField(this, "_size");
7359
- __publicField(this, "_defaultOneParamCallback", (node) => node.key);
7332
+ __publicField(this, "_defaultOneParamCallback", (node) => node ? node.key : void 0);
7360
7333
  if (options) {
7361
7334
  const { iterationType, extractor } = options;
7362
7335
  if (iterationType) {
@@ -8800,7 +8773,12 @@ var dataStructureTyped = (() => {
8800
8773
  return emptyDisplayLayout;
8801
8774
  } else if (node !== null && node !== void 0) {
8802
8775
  const key = node.key, line = isNaN(this.extractor(key)) ? "S" : this.extractor(key).toString(), width = line.length;
8803
- return _buildNodeDisplay(line, width, this._displayAux(node.left, options), this._displayAux(node.right, options));
8776
+ return _buildNodeDisplay(
8777
+ line,
8778
+ width,
8779
+ this._displayAux(node.left, options),
8780
+ this._displayAux(node.right, options)
8781
+ );
8804
8782
  } else {
8805
8783
  const line = node === void 0 ? "U" : "N", width = line.length;
8806
8784
  return _buildNodeDisplay(line, width, [[""], 1, 0, 0], [[""], 1, 0, 0]);
@@ -8816,7 +8794,12 @@ var dataStructureTyped = (() => {
8816
8794
  const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
8817
8795
  mergedLines.push(leftLine + " ".repeat(width) + rightLine);
8818
8796
  }
8819
- return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
8797
+ return [
8798
+ mergedLines,
8799
+ leftWidth + width + rightWidth,
8800
+ Math.max(leftHeight, rightHeight) + 2,
8801
+ leftWidth + Math.floor(width / 2)
8802
+ ];
8820
8803
  }
8821
8804
  }
8822
8805
  /**
@@ -8865,40 +8848,6 @@ var dataStructureTyped = (() => {
8865
8848
  }
8866
8849
  return newNode;
8867
8850
  }
8868
- /**
8869
- * The function `_addTo` adds a new node to a binary tree if there is an available position.
8870
- * @param {N | null | undefined} newNode - The `newNode` parameter represents the node that you want to add to
8871
- * the binary tree. It can be either a node object or `null`.
8872
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
8873
- * be added as a child.
8874
- * @returns either the left or right child node of the parent node, depending on which child is
8875
- * available for adding the new node. If a new node is added, the function also updates the size of
8876
- * the binary tree. If neither the left nor right child is available, the function returns undefined.
8877
- * If the parent node is null, the function also returns undefined.
8878
- */
8879
- _addTo(newNode, parent) {
8880
- if (this.isNotNodeInstance(parent))
8881
- parent = this.getNode(parent);
8882
- if (parent) {
8883
- if (parent.left === void 0) {
8884
- parent.left = newNode;
8885
- if (newNode) {
8886
- this._size = this.size + 1;
8887
- }
8888
- return parent.left;
8889
- } else if (parent.right === void 0) {
8890
- parent.right = newNode;
8891
- if (newNode) {
8892
- this._size = this.size + 1;
8893
- }
8894
- return parent.right;
8895
- } else {
8896
- return;
8897
- }
8898
- } else {
8899
- return;
8900
- }
8901
- }
8902
8851
  /**
8903
8852
  * The function sets the root property of an object to a given value, and if the value is not null,
8904
8853
  * it also sets the parent property of the value to undefined.
@@ -11268,45 +11217,6 @@ var dataStructureTyped = (() => {
11268
11217
  this.bfs((node) => cloned.add(node.key, node.value, node.count));
11269
11218
  return cloned;
11270
11219
  }
11271
- /**
11272
- * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
11273
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
11274
- *
11275
- * The function adds a new node to a binary tree, either as the left child or the right child of a
11276
- * given parent node.
11277
- * @param {N | undefined} newNode - The `newNode` parameter represents the node that needs to be
11278
- * added to the binary tree. It can be of type `N` (which represents a node in the binary tree) or
11279
- * `undefined` if there is no node to add.
11280
- * @param {K | N | undefined} parent - The `parent` parameter represents the parent node to
11281
- * which the new node will be added as a child. It can be either a node object (`N`) or a key value
11282
- * (`K`).
11283
- * @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
11284
- * added, or `undefined` if no node was added.
11285
- */
11286
- _addTo(newNode, parent) {
11287
- parent = this.ensureNode(parent);
11288
- if (parent) {
11289
- if (parent.left === void 0) {
11290
- parent.left = newNode;
11291
- if (newNode !== void 0) {
11292
- this._size = this.size + 1;
11293
- this._count += newNode.count;
11294
- }
11295
- return parent.left;
11296
- } else if (parent.right === void 0) {
11297
- parent.right = newNode;
11298
- if (newNode !== void 0) {
11299
- this._size = this.size + 1;
11300
- this._count += newNode.count;
11301
- }
11302
- return parent.right;
11303
- } else {
11304
- return;
11305
- }
11306
- } else {
11307
- return;
11308
- }
11309
- }
11310
11220
  /**
11311
11221
  * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
11312
11222
  * @param {K | N | undefined} srcNode - The `srcNode` parameter represents the source node from
@@ -11421,488 +11331,397 @@ var dataStructureTyped = (() => {
11421
11331
  };
11422
11332
 
11423
11333
  // src/data-structures/matrix/matrix.ts
11424
- var MatrixNTI2D = class {
11425
- /**
11426
- * The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
11427
- * given initial value or 0 if not provided.
11428
- * @param options - An object containing the following properties:
11429
- */
11430
- constructor(options) {
11431
- __publicField(this, "_matrix");
11432
- const { row, col, initialVal } = options;
11433
- this._matrix = new Array(row).fill(void 0).map(() => new Array(col).fill(initialVal || 0));
11434
- }
11435
- /* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
11436
- matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
11437
- toArray() {
11438
- return this._matrix;
11439
- }
11440
- };
11441
-
11442
- // src/data-structures/matrix/vector2d.ts
11443
- var Vector2D = class _Vector2D {
11444
- constructor(x = 0, y = 0, w = 1) {
11445
- this.x = x;
11446
- this.y = y;
11447
- this.w = w;
11448
- }
11449
- /**
11450
- * The function checks if the x and y values of a point are both zero.
11451
- * @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
11452
- */
11453
- get isZero() {
11454
- return this.x === 0 && this.y === 0;
11455
- }
11456
- /**
11457
- * The above function calculates the length of a vector using the Pythagorean theorem.
11458
- * @returns The length of a vector, calculated using the Pythagorean theorem.
11459
- */
11460
- get length() {
11461
- return Math.sqrt(this.x * this.x + this.y * this.y);
11334
+ var Matrix = class _Matrix {
11335
+ /**
11336
+ * The constructor function initializes a matrix object with the provided data and options, or with
11337
+ * default values if no options are provided.
11338
+ * @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
11339
+ * @param [options] - The `options` parameter is an optional object that can contain the following
11340
+ * properties:
11341
+ */
11342
+ constructor(data, options) {
11343
+ __publicField(this, "_rows", 0);
11344
+ __publicField(this, "_cols", 0);
11345
+ __publicField(this, "_data");
11346
+ var _a, _b, _c;
11347
+ if (options) {
11348
+ const { rows, cols, addFn, subtractFn, multiplyFn } = options;
11349
+ if (typeof rows === "number" && rows > 0)
11350
+ this._rows = rows;
11351
+ else
11352
+ this._rows = data.length;
11353
+ if (typeof cols === "number" && cols > 0)
11354
+ this._cols = cols;
11355
+ else
11356
+ this._cols = ((_a = data[0]) == null ? void 0 : _a.length) || 0;
11357
+ if (addFn)
11358
+ this._addFn = addFn;
11359
+ if (subtractFn)
11360
+ this._subtractFn = subtractFn;
11361
+ if (multiplyFn)
11362
+ this._multiplyFn = multiplyFn;
11363
+ } else {
11364
+ this._rows = data.length;
11365
+ this._cols = (_c = (_b = data[0]) == null ? void 0 : _b.length) != null ? _c : 0;
11366
+ }
11367
+ if (data.length > 0) {
11368
+ this._data = data;
11369
+ } else {
11370
+ this._data = [];
11371
+ for (let i = 0; i < this.rows; i++) {
11372
+ this._data[i] = new Array(this.cols).fill(0);
11373
+ }
11374
+ }
11462
11375
  }
11463
- /**
11464
- * The function calculates the square of the length of a vector.
11465
- * @returns The method is returning the sum of the squares of the x and y values.
11466
- */
11467
- get lengthSq() {
11468
- return this.x * this.x + this.y * this.y;
11376
+ get rows() {
11377
+ return this._rows;
11469
11378
  }
11470
- /**
11471
- * The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
11472
- * @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
11473
- * whole number.
11474
- */
11475
- get rounded() {
11476
- return new _Vector2D(Math.round(this.x), Math.round(this.y));
11379
+ get cols() {
11380
+ return this._cols;
11477
11381
  }
11478
- /**
11479
- * The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
11480
- * x and y components.
11481
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
11482
- * 2-dimensional vector with an `x` and `y` component.
11483
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
11484
- * an x and y component.
11485
- * @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
11486
- * vectors added together.
11487
- */
11488
- static add(vector1, vector2) {
11489
- return new _Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
11382
+ get data() {
11383
+ return this._data;
11490
11384
  }
11491
- /**
11492
- * The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
11493
- * components subtracted.
11494
- * @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
11495
- * 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
11496
- * respectively.
11497
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
11498
- * want to subtract from the first vector.
11499
- * @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
11500
- * vector2.
11501
- */
11502
- static subtract(vector1, vector2) {
11503
- return new _Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
11385
+ get addFn() {
11386
+ return this._addFn;
11504
11387
  }
11505
- /**
11506
- * The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
11507
- * object.
11508
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11509
- * x and y components.
11510
- * @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
11511
- * of the "vector" parameter.
11512
- * @returns A new Vector2D object with the x and y values subtracted by the given value.
11513
- */
11514
- static subtractValue(vector, value) {
11515
- return new _Vector2D(vector.x - value, vector.y - value);
11388
+ get subtractFn() {
11389
+ return this._subtractFn;
11516
11390
  }
11517
- /**
11518
- * The function multiplies a Vector2D object by a given value.
11519
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11520
- * x and y components.
11521
- * @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
11522
- * of the vector will be multiplied.
11523
- * @returns A new Vector2D object with the x and y values multiplied by the given value.
11524
- */
11525
- static multiply(vector, value) {
11526
- return new _Vector2D(vector.x * value, vector.y * value);
11391
+ get multiplyFn() {
11392
+ return this._multiplyFn;
11527
11393
  }
11528
11394
  /**
11529
- * The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
11530
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
11531
- * x and y components.
11532
- * @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
11533
- * vector.
11534
- * @returns A new instance of the Vector2D class with the x and y values divided by the given value.
11395
+ * The `get` function returns the value at the specified row and column index if it is a valid index.
11396
+ * @param {number} row - The `row` parameter represents the row index of the element you want to
11397
+ * retrieve from the data array.
11398
+ * @param {number} col - The parameter "col" represents the column number of the element you want to
11399
+ * retrieve from the data array.
11400
+ * @returns The `get` function returns a number if the provided row and column indices are valid.
11401
+ * Otherwise, it returns `undefined`.
11535
11402
  */
11536
- static divide(vector, value) {
11537
- return new _Vector2D(vector.x / value, vector.y / value);
11538
- }
11539
- /**
11540
- * The function checks if two Vector2D objects are equal by comparing their x and y values.
11541
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
11542
- * It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
11543
- * @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
11544
- * @returns a boolean value, which indicates whether the two input vectors are equal or not.
11545
- */
11546
- static equals(vector1, vector2) {
11547
- return vector1.x === vector2.x && vector1.y === vector2.y;
11403
+ get(row, col) {
11404
+ if (this.isValidIndex(row, col)) {
11405
+ return this.data[row][col];
11406
+ }
11548
11407
  }
11549
11408
  /**
11550
- * The function checks if two Vector2D objects are equal within a specified rounding factor.
11551
- * @param {Vector2D} vector1 - The first vector to compare.
11552
- * @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
11553
- * It is used as one of the inputs for the "equalsRounded" function.
11554
- * @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
11555
- * vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
11556
- * roundingFactor, the vectors are considered equal.
11557
- * @returns a boolean value.
11409
+ * The set function updates the value at a specified row and column in a two-dimensional array.
11410
+ * @param {number} row - The "row" parameter represents the row index of the element in a
11411
+ * two-dimensional array or matrix. It specifies the row where the value will be set.
11412
+ * @param {number} col - The "col" parameter represents the column index of the element in a
11413
+ * two-dimensional array.
11414
+ * @param {number} value - The value parameter represents the number that you want to set at the
11415
+ * specified row and column in the data array.
11416
+ * @returns a boolean value. It returns true if the index (row, col) is valid and the value is
11417
+ * successfully set in the data array. It returns false if the index is invalid and the value is not
11418
+ * set.
11558
11419
  */
11559
- static equalsRounded(vector1, vector2, roundingFactor = 12) {
11560
- const vector = _Vector2D.abs(_Vector2D.subtract(vector1, vector2));
11561
- if (vector.x < roundingFactor && vector.y < roundingFactor) {
11420
+ set(row, col, value) {
11421
+ if (this.isValidIndex(row, col)) {
11422
+ this.data[row][col] = value;
11562
11423
  return true;
11563
11424
  }
11564
11425
  return false;
11565
11426
  }
11566
11427
  /**
11567
- * The normalize function takes a vector as input and returns a normalized version of the vector.Normalizes the vector if it matches a certain condition
11568
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
11569
- * @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
11570
- * original vector.
11428
+ * The function checks if the dimensions of the given matrix match the dimensions of the current
11429
+ * matrix.
11430
+ * @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
11431
+ * @returns a boolean value.
11571
11432
  */
11572
- static normalize(vector) {
11573
- const length = vector.length;
11574
- if (length > 2220446049250313e-31) {
11575
- return _Vector2D.divide(vector, length);
11576
- }
11577
- return vector;
11433
+ isMatchForCalculate(matrix) {
11434
+ return this.rows === matrix.rows && this.cols === matrix.cols;
11578
11435
  }
11579
11436
  /**
11580
- * The function truncates a vector to a maximum length if it exceeds that length.Adjusts x and y so that the length of the vector does not exceed max
11581
- * @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
11582
- * @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
11583
- * have.
11584
- * @returns either the original vector or a truncated version of the vector, depending on whether the length of the
11585
- * vector is greater than the maximum value specified.
11437
+ * The `add` function adds two matrices together, returning a new matrix with the result.
11438
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11439
+ * @returns The `add` method returns a new `Matrix` object that represents the result of adding the
11440
+ * current matrix with the provided `matrix` parameter.
11586
11441
  */
11587
- static truncate(vector, max) {
11588
- if (vector.length > max) {
11589
- return _Vector2D.multiply(_Vector2D.normalize(vector), max);
11442
+ add(matrix) {
11443
+ if (!this.isMatchForCalculate(matrix)) {
11444
+ throw new Error("Matrix dimensions must match for addition.");
11590
11445
  }
11591
- return vector;
11592
- }
11593
- /**
11594
- * The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
11595
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
11596
- * @returns A new Vector2D object is being returned.
11597
- */
11598
- static perp(vector) {
11599
- return new _Vector2D(-vector.y, vector.x);
11600
- }
11601
- /**
11602
- * The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
11603
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
11604
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
11605
- * @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
11606
- */
11607
- static reverse(vector) {
11608
- return new _Vector2D(-vector.x, -vector.y);
11609
- }
11610
- /**
11611
- * The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
11612
- * and y components.
11613
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
11614
- * has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
11615
- * @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
11616
- * input vector.
11617
- */
11618
- static abs(vector) {
11619
- return new _Vector2D(Math.abs(vector.x), Math.abs(vector.y));
11620
- }
11621
- /**
11622
- * The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
11623
- * @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
11624
- * @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
11625
- * with an x and y component.
11626
- * @returns The dot product of the two input vectors.
11627
- */
11628
- static dot(vector1, vector2) {
11629
- return vector1.x * vector2.x + vector1.y * vector2.y;
11630
- }
11631
- // /**
11632
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
11633
- // * @param vectors The vectors to transform
11634
- // */
11635
- // static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
11636
- // return Matrix2D.multiplyByVector(transformation, vector)
11637
- // }
11638
- // /**
11639
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
11640
- // * @param vectors The vectors to transform
11641
- // */
11642
- // static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
11643
- // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
11644
- // }
11645
- /**
11646
- * The function calculates the distance between two points in a two-dimensional space.
11647
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
11648
- * represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
11649
- * in the 2D space.
11650
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
11651
- * is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
11652
- * the vector in a 2D space.
11653
- * @returns The distance between vector1 and vector2.
11654
- */
11655
- static distance(vector1, vector2) {
11656
- const ySeparation = vector2.y - vector1.y;
11657
- const xSeparation = vector2.x - vector1.x;
11658
- return Math.sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
11659
- }
11660
- /**
11661
- * The function calculates the squared distance between two 2D vectors.
11662
- * @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
11663
- * `Vector2D` class. It contains the x and y coordinates of the vector.
11664
- * @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
11665
- * properties `x` and `y` which represent the coordinates of the vector.
11666
- * @returns the square of the distance between the two input vectors.
11667
- */
11668
- static distanceSq(vector1, vector2) {
11669
- const ySeparation = vector2.y - vector1.y;
11670
- const xSeparation = vector2.x - vector1.x;
11671
- return ySeparation * ySeparation + xSeparation * xSeparation;
11672
- }
11673
- /**
11674
- * The sign function determines the sign of the cross product between two 2D vectors.
11675
- * (assuming the Y axis is pointing down, X axis to right like a Window app)
11676
- * @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
11677
- * It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
11678
- * @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
11679
- * vector2. Both vector1 and vector2 are of type Vector2D.
11680
- * @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
11681
- */
11682
- static sign(vector1, vector2) {
11683
- if (vector1.y * vector2.x > vector1.x * vector2.y) {
11684
- return -1;
11446
+ const resultData = [];
11447
+ for (let i = 0; i < this.rows; i++) {
11448
+ resultData[i] = [];
11449
+ for (let j = 0; j < this.cols; j++) {
11450
+ const a = this.get(i, j), b = matrix.get(i, j);
11451
+ if (a !== void 0 && b !== void 0) {
11452
+ const added = this._addFn(a, b);
11453
+ if (added) {
11454
+ resultData[i][j] = added;
11455
+ }
11456
+ }
11457
+ }
11685
11458
  }
11686
- return 1;
11687
- }
11688
- /**
11689
- * The function calculates the angle between a given vector and the negative y-axis.
11690
- * @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
11691
- * 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
11692
- * respectively.
11693
- * @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
11694
- */
11695
- static angle(vector) {
11696
- const origin = new _Vector2D(0, -1);
11697
- const radian = Math.acos(_Vector2D.dot(vector, origin) / (vector.length * origin.length));
11698
- return _Vector2D.sign(vector, origin) === 1 ? Math.PI * 2 - radian : radian;
11699
- }
11700
- /**
11701
- * The function "random" generates a random Vector2D object with x and y values within the specified range.
11702
- * @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
11703
- * @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
11704
- * random vector.
11705
- * @returns a new instance of the Vector2D class with random x and y values.
11706
- */
11707
- static random(maxX, maxY) {
11708
- const randX = Math.floor(Math.random() * maxX - maxX / 2);
11709
- const randY = Math.floor(Math.random() * maxY - maxY / 2);
11710
- return new _Vector2D(randX, randY);
11711
- }
11712
- /**
11713
- * The function sets the values of x and y to zero.
11714
- */
11715
- zero() {
11716
- this.x = 0;
11717
- this.y = 0;
11459
+ return new _Matrix(resultData, {
11460
+ rows: this.rows,
11461
+ cols: this.cols,
11462
+ addFn: this.addFn,
11463
+ subtractFn: this.subtractFn,
11464
+ multiplyFn: this.multiplyFn
11465
+ });
11718
11466
  }
11719
- };
11720
-
11721
- // src/data-structures/matrix/matrix2d.ts
11722
- var Matrix2D = class _Matrix2D {
11723
11467
  /**
11724
- * The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
11725
- * or Vector2D object.
11726
- * @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
11727
- * an instance of the `Vector2D` class.
11728
- */
11729
- constructor(value) {
11730
- __publicField(this, "_matrix");
11731
- if (typeof value === "undefined") {
11732
- this._matrix = _Matrix2D.identity;
11733
- } else if (value instanceof Vector2D) {
11734
- this._matrix = _Matrix2D.identity;
11735
- this._matrix[0][0] = value.x;
11736
- this._matrix[1][0] = value.y;
11737
- this._matrix[2][0] = value.w;
11738
- } else {
11739
- this._matrix = value;
11468
+ * The `subtract` function performs element-wise subtraction between two matrices and returns a new
11469
+ * matrix with the result.
11470
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
11471
+ * represents the matrix that you want to subtract from the current matrix.
11472
+ * @returns a new Matrix object with the result of the subtraction operation.
11473
+ */
11474
+ subtract(matrix) {
11475
+ if (!this.isMatchForCalculate(matrix)) {
11476
+ throw new Error("Matrix dimensions must match for subtraction.");
11477
+ }
11478
+ const resultData = [];
11479
+ for (let i = 0; i < this.rows; i++) {
11480
+ resultData[i] = [];
11481
+ for (let j = 0; j < this.cols; j++) {
11482
+ const a = this.get(i, j), b = matrix.get(i, j);
11483
+ if (a !== void 0 && b !== void 0) {
11484
+ const subtracted = this._subtractFn(a, b);
11485
+ if (subtracted) {
11486
+ resultData[i][j] = subtracted;
11487
+ }
11488
+ }
11489
+ }
11740
11490
  }
11491
+ return new _Matrix(resultData, {
11492
+ rows: this.rows,
11493
+ cols: this.cols,
11494
+ addFn: this.addFn,
11495
+ subtractFn: this.subtractFn,
11496
+ multiplyFn: this.multiplyFn
11497
+ });
11741
11498
  }
11742
11499
  /**
11743
- * The function returns a 2D array with three empty arrays.
11744
- * @returns An empty 2-dimensional array with 3 empty arrays inside.
11745
- */
11746
- static get empty() {
11747
- return [[], [], []];
11748
- }
11749
- /**
11750
- * The above function returns a 3x3 identity matrix.
11751
- * @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
11752
- */
11753
- static get identity() {
11754
- return [
11755
- [1, 0, 0],
11756
- [0, 1, 0],
11757
- [0, 0, 1]
11758
- ];
11759
- }
11760
- /**
11761
- * The function returns a two-dimensional array of numbers.
11762
- * @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
11763
- * array of numbers.
11764
- */
11765
- get m() {
11766
- return this._matrix;
11767
- }
11768
- /**
11769
- * The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
11770
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
11771
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
11772
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11773
- */
11774
- static add(matrix1, matrix2) {
11775
- const result = _Matrix2D.empty;
11776
- for (let i = 0; i < 3; i++) {
11777
- for (let j = 0; j < 3; j++) {
11778
- result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
11500
+ * The `multiply` function performs matrix multiplication between two matrices and returns the result
11501
+ * as a new matrix.
11502
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11503
+ * @returns a new Matrix object.
11504
+ */
11505
+ multiply(matrix) {
11506
+ if (this.cols !== matrix.rows) {
11507
+ throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");
11508
+ }
11509
+ const resultData = [];
11510
+ for (let i = 0; i < this.rows; i++) {
11511
+ resultData[i] = [];
11512
+ for (let j = 0; j < matrix.cols; j++) {
11513
+ let sum;
11514
+ for (let k = 0; k < this.cols; k++) {
11515
+ const a = this.get(i, k), b = matrix.get(k, j);
11516
+ if (a !== void 0 && b !== void 0) {
11517
+ const multiplied = this.multiplyFn(a, b);
11518
+ if (multiplied !== void 0) {
11519
+ sum = this.addFn(sum, multiplied);
11520
+ }
11521
+ }
11522
+ }
11523
+ if (sum !== void 0)
11524
+ resultData[i][j] = sum;
11779
11525
  }
11780
11526
  }
11781
- return new _Matrix2D(result);
11527
+ return new _Matrix(resultData, {
11528
+ rows: this.rows,
11529
+ cols: matrix.cols,
11530
+ addFn: this.addFn,
11531
+ subtractFn: this.subtractFn,
11532
+ multiplyFn: this.multiplyFn
11533
+ });
11782
11534
  }
11783
11535
  /**
11784
- * The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
11785
- * @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
11786
- * @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
11787
- * representing the matrix elements.
11788
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11536
+ * The transpose function takes a matrix and returns a new matrix that is the transpose of the
11537
+ * original matrix.
11538
+ * @returns The transpose() function returns a new Matrix object with the transposed data.
11789
11539
  */
11790
- static subtract(matrix1, matrix2) {
11791
- const result = _Matrix2D.empty;
11792
- for (let i = 0; i < 3; i++) {
11793
- for (let j = 0; j < 3; j++) {
11794
- result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
11540
+ transpose() {
11541
+ if (this.data.some((row) => row.length !== this.rows)) {
11542
+ throw new Error("Matrix must be rectangular for transposition.");
11543
+ }
11544
+ const resultData = [];
11545
+ for (let j = 0; j < this.cols; j++) {
11546
+ resultData[j] = [];
11547
+ for (let i = 0; i < this.rows; i++) {
11548
+ const trans = this.get(i, j);
11549
+ if (trans !== void 0)
11550
+ resultData[j][i] = trans;
11795
11551
  }
11796
11552
  }
11797
- return new _Matrix2D(result);
11553
+ return new _Matrix(resultData, {
11554
+ rows: this.cols,
11555
+ cols: this.rows,
11556
+ addFn: this.addFn,
11557
+ subtractFn: this.subtractFn,
11558
+ multiplyFn: this.multiplyFn
11559
+ });
11798
11560
  }
11799
11561
  /**
11800
- * The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
11801
- * @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
11802
- * @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
11803
- * @returns a new instance of the Matrix2D class, created using the result array.
11562
+ * The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
11563
+ * @returns a Matrix object, which represents the inverse of the original matrix.
11804
11564
  */
11805
- static multiply(matrix1, matrix2) {
11806
- const result = _Matrix2D.empty;
11807
- for (let i = 0; i < 3; i++) {
11808
- for (let j = 0; j < 3; j++) {
11809
- result[i][j] = 0;
11810
- for (let k = 0; k < 3; k++) {
11811
- result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
11565
+ inverse() {
11566
+ var _a;
11567
+ if (this.rows !== this.cols) {
11568
+ throw new Error("Matrix must be square for inversion.");
11569
+ }
11570
+ const augmentedMatrixData = [];
11571
+ for (let i = 0; i < this.rows; i++) {
11572
+ augmentedMatrixData[i] = this.data[i].slice();
11573
+ for (let j = 0; j < this.cols; j++) {
11574
+ augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0;
11575
+ }
11576
+ }
11577
+ const augmentedMatrix = new _Matrix(augmentedMatrixData, {
11578
+ rows: this.rows,
11579
+ cols: this.cols * 2,
11580
+ addFn: this.addFn,
11581
+ subtractFn: this.subtractFn,
11582
+ multiplyFn: this.multiplyFn
11583
+ });
11584
+ for (let i = 0; i < this.rows; i++) {
11585
+ let pivotRow = i;
11586
+ while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
11587
+ pivotRow++;
11588
+ }
11589
+ if (pivotRow === this.rows) {
11590
+ throw new Error("Matrix is singular, and its inverse does not exist.");
11591
+ }
11592
+ augmentedMatrix._swapRows(i, pivotRow);
11593
+ const pivotElement = (_a = augmentedMatrix.get(i, i)) != null ? _a : 1;
11594
+ if (pivotElement === 0) {
11595
+ throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");
11596
+ }
11597
+ augmentedMatrix._scaleRow(i, 1 / pivotElement);
11598
+ for (let j = 0; j < this.rows; j++) {
11599
+ if (j !== i) {
11600
+ let factor = augmentedMatrix.get(j, i);
11601
+ if (factor === void 0)
11602
+ factor = 0;
11603
+ augmentedMatrix._addScaledRow(j, i, -factor);
11812
11604
  }
11813
11605
  }
11814
11606
  }
11815
- return new _Matrix2D(result);
11607
+ const inverseData = [];
11608
+ for (let i = 0; i < this.rows; i++) {
11609
+ inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
11610
+ }
11611
+ return new _Matrix(inverseData, {
11612
+ rows: this.rows,
11613
+ cols: this.cols,
11614
+ addFn: this.addFn,
11615
+ subtractFn: this.subtractFn,
11616
+ multiplyFn: this.multiplyFn
11617
+ });
11816
11618
  }
11817
11619
  /**
11818
- * The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
11819
- * @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
11820
- * matrix. It contains a property `m` that is a 2D array representing the matrix elements.
11821
- * @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
11822
- * @returns a new instance of the Matrix2D class, which is created using the result array.
11823
- */
11824
- static multiplyByValue(matrix, value) {
11825
- const result = _Matrix2D.empty;
11826
- for (let i = 0; i < 3; i++) {
11827
- for (let j = 0; j < 3; j++) {
11828
- result[i][j] = matrix.m[i][j] * value;
11620
+ * The dot function calculates the dot product of two matrices and returns a new matrix.
11621
+ * @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
11622
+ * @returns a new Matrix object.
11623
+ */
11624
+ dot(matrix) {
11625
+ if (this.cols !== matrix.rows) {
11626
+ throw new Error(
11627
+ "Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product."
11628
+ );
11629
+ }
11630
+ const resultData = [];
11631
+ for (let i = 0; i < this.rows; i++) {
11632
+ resultData[i] = [];
11633
+ for (let j = 0; j < matrix.cols; j++) {
11634
+ let sum;
11635
+ for (let k = 0; k < this.cols; k++) {
11636
+ const a = this.get(i, k), b = matrix.get(k, j);
11637
+ if (a !== void 0 && b !== void 0) {
11638
+ const multiplied = this.multiplyFn(a, b);
11639
+ if (multiplied !== void 0) {
11640
+ sum = this.addFn(sum, multiplied);
11641
+ }
11642
+ }
11643
+ }
11644
+ if (sum !== void 0)
11645
+ resultData[i][j] = sum;
11829
11646
  }
11830
11647
  }
11831
- return new _Matrix2D(result);
11648
+ return new _Matrix(resultData, {
11649
+ rows: this.rows,
11650
+ cols: matrix.cols,
11651
+ addFn: this.addFn,
11652
+ subtractFn: this.subtractFn,
11653
+ multiplyFn: this.multiplyFn
11654
+ });
11832
11655
  }
11833
- /**
11834
- * The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
11835
- * @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
11836
- * @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
11837
- * @returns a Vector2D.
11838
- */
11839
- static multiplyByVector(matrix, vector) {
11840
- const resultMatrix = _Matrix2D.multiply(matrix, new _Matrix2D(vector));
11841
- return resultMatrix.toVector();
11656
+ _addFn(a, b) {
11657
+ if (a === void 0)
11658
+ return b;
11659
+ return a + b;
11842
11660
  }
11843
- /**
11844
- * The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
11845
- * @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
11846
- * specifies the width in pixels or any other unit of measurement.
11847
- * @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
11848
- * calculate the centerY value, which is the vertical center of the view.
11849
- * @returns a Matrix2D object.
11850
- */
11851
- static view(width, height) {
11852
- const scaleStep = 1;
11853
- const centerX = width / 2;
11854
- const centerY = height / 2;
11855
- const flipX = Math.cos(Math.PI);
11856
- return new _Matrix2D([
11857
- [scaleStep, 0, centerX],
11858
- [0, flipX * scaleStep, centerY],
11859
- [0, 0, 1]
11860
- ]);
11661
+ _subtractFn(a, b) {
11662
+ return a - b;
11663
+ }
11664
+ _multiplyFn(a, b) {
11665
+ return a * b;
11861
11666
  }
11862
11667
  /**
11863
- * The function scales a matrix by a given factor.
11864
- * @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
11865
- * should be scaled.
11866
- * @returns the result of multiplying a new instance of Matrix2D by the given factor.
11668
+ * The function checks if a given row and column index is valid within a specified range.
11669
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
11670
+ * matrix. It is a number that indicates the specific row in the matrix.
11671
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
11672
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
11673
+ * @returns A boolean value is being returned.
11867
11674
  */
11868
- static scale(factor) {
11869
- return _Matrix2D.multiplyByValue(new _Matrix2D(), factor);
11675
+ isValidIndex(row, col) {
11676
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
11870
11677
  }
11871
11678
  /**
11872
- * The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
11873
- * @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
11874
- * @returns The code is returning a new instance of a Matrix2D object.
11679
+ * The function `_swapRows` swaps the positions of two rows in an array.
11680
+ * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
11681
+ * @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
11682
+ * with the first row.
11875
11683
  */
11876
- static rotate(radians) {
11877
- const cos = Math.cos(radians);
11878
- const sin = Math.sin(radians);
11879
- return new _Matrix2D([
11880
- [cos, -sin, 0],
11881
- [sin, cos, 0],
11882
- [0, 0, 1]
11883
- ]);
11684
+ _swapRows(row1, row2) {
11685
+ const temp = this.data[row1];
11686
+ this.data[row1] = this.data[row2];
11687
+ this.data[row2] = temp;
11884
11688
  }
11885
11689
  /**
11886
- * The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
11887
- * @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
11888
- * and y, and an optional w component.
11889
- * @returns The method is returning a new instance of the Matrix2D class.
11690
+ * The function scales a specific row in a matrix by a given scalar value.
11691
+ * @param {number} row - The `row` parameter represents the index of the row in the matrix that you
11692
+ * want to scale. It is a number that indicates the position of the row within the matrix.
11693
+ * @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
11694
+ * a specific row of a matrix.
11890
11695
  */
11891
- static translate(vector) {
11892
- return new _Matrix2D([
11893
- [1, 0, vector.x],
11894
- [0, 1, vector.y],
11895
- [0, 0, vector.w]
11896
- ]);
11696
+ _scaleRow(row, scalar) {
11697
+ for (let j = 0; j < this.cols; j++) {
11698
+ let multiplied = this.multiplyFn(this.data[row][j], scalar);
11699
+ if (multiplied === void 0)
11700
+ multiplied = 0;
11701
+ this.data[row][j] = multiplied;
11702
+ }
11897
11703
  }
11898
11704
  /**
11899
- * The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
11900
- * _matrix array.
11901
- * @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
11902
- * the first column of the matrix.
11705
+ * The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
11706
+ * row.
11707
+ * @param {number} targetRow - The targetRow parameter represents the index of the row in which the
11708
+ * scaled values will be added.
11709
+ * @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
11710
+ * values will be scaled and added to the targetRow.
11711
+ * @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
11712
+ * source row before adding them to the target row.
11903
11713
  */
11904
- toVector() {
11905
- return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
11714
+ _addScaledRow(targetRow, sourceRow, scalar) {
11715
+ for (let j = 0; j < this.cols; j++) {
11716
+ let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
11717
+ if (multiplied === void 0)
11718
+ multiplied = 0;
11719
+ const scaledValue = multiplied;
11720
+ let added = this.addFn(this.data[targetRow][j], scaledValue);
11721
+ if (added === void 0)
11722
+ added = 0;
11723
+ this.data[targetRow][j] = added;
11724
+ }
11906
11725
  }
11907
11726
  };
11908
11727