data-structure-typed 1.35.0 → 1.36.0
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.
- package/.github/workflows/ci.yml +3 -0
- package/CHANGELOG.md +8 -1
- package/CONTRIBUTING.md +18 -0
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +270 -7
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +100 -0
- package/dist/data-structures/heap/heap.js +215 -76
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +12 -0
- package/dist/data-structures/heap/max-heap.js +16 -6
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +12 -0
- package/dist/data-structures/heap/min-heap.js +16 -6
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/priority-queue.js +11 -174
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +1 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/lib/data-structures/graph/abstract-graph.js +3 -5
- package/lib/data-structures/heap/heap.d.ts +85 -68
- package/lib/data-structures/heap/heap.js +186 -108
- package/lib/data-structures/heap/max-heap.d.ts +6 -17
- package/lib/data-structures/heap/max-heap.js +11 -17
- package/lib/data-structures/heap/min-heap.d.ts +6 -18
- package/lib/data-structures/heap/min-heap.js +11 -18
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +5 -8
- package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +5 -8
- package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
- package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
- package/lib/data-structures/priority-queue/priority-queue.js +11 -315
- package/lib/types/data-structures/heap.d.ts +1 -3
- package/package.json +11 -7
- package/src/data-structures/graph/abstract-graph.ts +3 -5
- package/src/data-structures/heap/heap.ts +182 -140
- package/src/data-structures/heap/max-heap.ts +15 -22
- package/src/data-structures/heap/min-heap.ts +15 -23
- package/src/data-structures/priority-queue/max-priority-queue.ts +14 -47
- package/src/data-structures/priority-queue/min-priority-queue.ts +14 -48
- package/src/data-structures/priority-queue/priority-queue.ts +7 -350
- package/src/types/data-structures/heap.ts +1 -5
- package/test/integration/avl-tree.test.ts +4 -4
- package/test/integration/bst.test.ts +8 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/heap/heap.test.ts +26 -18
- package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
- package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -9
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +14 -30
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.LICENSE.txt +8 -0
- package/umd/bundle.min.js.map +1 -1
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/linked-list/index.ts +0 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"skip-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/skip-linked-list.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"skip-linked-list.js","sourceRoot":"","sources":["../../../src/data-structures/linked-list/skip-linked-list.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,MAAa,YAAY;IAKvB,YAAY,GAAM,EAAE,KAAQ,EAAE,KAAa;QACzC,IAAI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AI,OAAO,IAAI,OAAO,CAAC,GAAG,KAAK,GAAG,EAAE;YAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE;gBACnC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE;oBACpC,MAAM;iBACP;gBACD,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;aAC3C;YACD,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,IAAI,EAAE;gBACnE,IAAI,CAAC,KAAK,EAAE,CAAC;aACd;YACD,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAjJD,4BAiJC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
export declare class MatrixNTI2D<V = any> {
|
|
9
|
+
private readonly _matrix;
|
|
10
|
+
/**
|
|
11
|
+
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
12
|
+
* given initial value or 0 if not provided.
|
|
13
|
+
* @param options - An object containing the following properties:
|
|
14
|
+
*/
|
|
15
|
+
constructor(options: {
|
|
16
|
+
row: number;
|
|
17
|
+
col: number;
|
|
18
|
+
initialVal?: V;
|
|
19
|
+
});
|
|
20
|
+
toArray(): Array<Array<V>>;
|
|
21
|
+
}
|
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MatrixNTI2D = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
// todo need to be improved
|
|
4
12
|
class MatrixNTI2D {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
15
|
+
* given initial value or 0 if not provided.
|
|
16
|
+
* @param options - An object containing the following properties:
|
|
17
|
+
*/
|
|
5
18
|
constructor(options) {
|
|
6
19
|
const { row, col, initialVal } = options;
|
|
7
20
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
8
21
|
}
|
|
22
|
+
/* The `toArray` method returns the matrix as a two-dimensional array. It converts the internal representation of the
|
|
23
|
+
matrix, which is an array of arrays, into a format that is more commonly used in JavaScript. */
|
|
9
24
|
toArray() {
|
|
10
25
|
return this._matrix;
|
|
11
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"matrix.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,2BAA2B;AAC3B,MAAa,WAAW;IAGtB;;;;OAIG;IACH,YAAY,OAAmD;QAC7D,MAAM,EAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAC,GAAG,OAAO,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC,CAAC,CAAC;IAChG,CAAC;IAED;mGAC+F;IAC/F,OAAO;QACL,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAlBD,kCAkBC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import Vector2D from './vector2d';
|
|
9
|
+
export declare class Matrix2D {
|
|
10
|
+
private readonly _matrix;
|
|
11
|
+
/**
|
|
12
|
+
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
13
|
+
* or Vector2D object.
|
|
14
|
+
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
15
|
+
* an instance of the `Vector2D` class.
|
|
16
|
+
*/
|
|
17
|
+
constructor(value?: number[][] | Vector2D);
|
|
18
|
+
/**
|
|
19
|
+
* The function returns a 2D array with three empty arrays.
|
|
20
|
+
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
21
|
+
*/
|
|
22
|
+
static get empty(): number[][];
|
|
23
|
+
/**
|
|
24
|
+
* The above function returns a 3x3 identity matrix.
|
|
25
|
+
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
26
|
+
*/
|
|
27
|
+
static get identity(): number[][];
|
|
28
|
+
/**
|
|
29
|
+
* The function returns a two-dimensional array of numbers.
|
|
30
|
+
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
31
|
+
* array of numbers.
|
|
32
|
+
*/
|
|
33
|
+
get m(): number[][];
|
|
34
|
+
/**
|
|
35
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
36
|
+
* _matrix array.
|
|
37
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
38
|
+
* the first column of the matrix.
|
|
39
|
+
*/
|
|
40
|
+
toVector(): Vector2D;
|
|
41
|
+
/**
|
|
42
|
+
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
43
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
44
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
45
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
46
|
+
*/
|
|
47
|
+
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
48
|
+
/**
|
|
49
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
50
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
51
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
52
|
+
* representing the matrix elements.
|
|
53
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
54
|
+
*/
|
|
55
|
+
static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
56
|
+
/**
|
|
57
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
58
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
59
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
60
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
61
|
+
*/
|
|
62
|
+
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
63
|
+
/**
|
|
64
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
65
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
66
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
67
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
68
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
69
|
+
*/
|
|
70
|
+
static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D;
|
|
71
|
+
/**
|
|
72
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
73
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
74
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
75
|
+
* @returns a Vector2D.
|
|
76
|
+
*/
|
|
77
|
+
static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D;
|
|
78
|
+
/**
|
|
79
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
80
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
81
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
82
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
83
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
84
|
+
* @returns a Matrix2D object.
|
|
85
|
+
*/
|
|
86
|
+
static view(width: number, height: number): Matrix2D;
|
|
87
|
+
/**
|
|
88
|
+
* The function scales a matrix by a given factor.
|
|
89
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
90
|
+
* should be scaled.
|
|
91
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
92
|
+
*/
|
|
93
|
+
static scale(factor: number): Matrix2D;
|
|
94
|
+
/**
|
|
95
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
96
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
97
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
98
|
+
*/
|
|
99
|
+
static rotate(radians: number): Matrix2D;
|
|
100
|
+
/**
|
|
101
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
102
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
103
|
+
* and y, and an optional w component.
|
|
104
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
105
|
+
*/
|
|
106
|
+
static translate(vector: Vector2D): Matrix2D;
|
|
107
|
+
}
|
|
108
|
+
export default Matrix2D;
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Matrix2D = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
4
11
|
const vector2d_1 = require("./vector2d");
|
|
5
12
|
class Matrix2D {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
15
|
+
* or Vector2D object.
|
|
16
|
+
* @param {number[][] | Vector2D} [value] - The `value` parameter can be either a 2D array of numbers (`number[][]`) or
|
|
17
|
+
* an instance of the `Vector2D` class.
|
|
18
|
+
*/
|
|
6
19
|
constructor(value) {
|
|
7
20
|
if (typeof value === 'undefined') {
|
|
8
21
|
this._matrix = Matrix2D.identity;
|
|
@@ -17,9 +30,17 @@ class Matrix2D {
|
|
|
17
30
|
this._matrix = value;
|
|
18
31
|
}
|
|
19
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* The function returns a 2D array with three empty arrays.
|
|
35
|
+
* @returns An empty 2-dimensional array with 3 empty arrays inside.
|
|
36
|
+
*/
|
|
20
37
|
static get empty() {
|
|
21
38
|
return [[], [], []];
|
|
22
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* The above function returns a 3x3 identity matrix.
|
|
42
|
+
* @returns The method is returning a 2-dimensional array of numbers representing the identity matrix.
|
|
43
|
+
*/
|
|
23
44
|
static get identity() {
|
|
24
45
|
return [
|
|
25
46
|
[1, 0, 0],
|
|
@@ -27,12 +48,29 @@ class Matrix2D {
|
|
|
27
48
|
[0, 0, 1]
|
|
28
49
|
];
|
|
29
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* The function returns a two-dimensional array of numbers.
|
|
53
|
+
* @returns The getter method is returning the value of the private variable `_matrix`, which is a two-dimensional
|
|
54
|
+
* array of numbers.
|
|
55
|
+
*/
|
|
30
56
|
get m() {
|
|
31
57
|
return this._matrix;
|
|
32
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
61
|
+
* _matrix array.
|
|
62
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
63
|
+
* the first column of the matrix.
|
|
64
|
+
*/
|
|
33
65
|
toVector() {
|
|
34
66
|
return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
|
|
35
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
70
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
71
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
72
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
73
|
+
*/
|
|
36
74
|
static add(matrix1, matrix2) {
|
|
37
75
|
const result = Matrix2D.empty;
|
|
38
76
|
for (let i = 0; i < 3; i++) {
|
|
@@ -42,6 +80,13 @@ class Matrix2D {
|
|
|
42
80
|
}
|
|
43
81
|
return new Matrix2D(result);
|
|
44
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
85
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
86
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
87
|
+
* representing the matrix elements.
|
|
88
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
89
|
+
*/
|
|
45
90
|
static subtract(matrix1, matrix2) {
|
|
46
91
|
const result = Matrix2D.empty;
|
|
47
92
|
for (let i = 0; i < 3; i++) {
|
|
@@ -51,6 +96,12 @@ class Matrix2D {
|
|
|
51
96
|
}
|
|
52
97
|
return new Matrix2D(result);
|
|
53
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
101
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
102
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
103
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
104
|
+
*/
|
|
54
105
|
static multiply(matrix1, matrix2) {
|
|
55
106
|
const result = Matrix2D.empty;
|
|
56
107
|
for (let i = 0; i < 3; i++) {
|
|
@@ -63,6 +114,13 @@ class Matrix2D {
|
|
|
63
114
|
}
|
|
64
115
|
return new Matrix2D(result);
|
|
65
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
119
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
120
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
121
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
122
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
123
|
+
*/
|
|
66
124
|
static multiplyByValue(matrix, value) {
|
|
67
125
|
const result = Matrix2D.empty;
|
|
68
126
|
for (let i = 0; i < 3; i++) {
|
|
@@ -72,24 +130,49 @@ class Matrix2D {
|
|
|
72
130
|
}
|
|
73
131
|
return new Matrix2D(result);
|
|
74
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
135
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
136
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
137
|
+
* @returns a Vector2D.
|
|
138
|
+
*/
|
|
75
139
|
static multiplyByVector(matrix, vector) {
|
|
76
140
|
const resultMatrix = Matrix2D.multiply(matrix, new Matrix2D(vector));
|
|
77
141
|
return resultMatrix.toVector();
|
|
78
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
145
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
146
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
147
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
148
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
149
|
+
* @returns a Matrix2D object.
|
|
150
|
+
*/
|
|
79
151
|
static view(width, height) {
|
|
80
|
-
const scaleStep = 1;
|
|
152
|
+
const scaleStep = 1; // Scale every vector * scaleStep
|
|
81
153
|
const centerX = width / 2;
|
|
82
154
|
const centerY = height / 2;
|
|
83
|
-
const flipX = Math.cos(Math.PI);
|
|
155
|
+
const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
|
|
84
156
|
return new Matrix2D([
|
|
85
157
|
[scaleStep, 0, centerX],
|
|
86
158
|
[0, flipX * scaleStep, centerY],
|
|
87
159
|
[0, 0, 1]
|
|
88
160
|
]);
|
|
89
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* The function scales a matrix by a given factor.
|
|
164
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
165
|
+
* should be scaled.
|
|
166
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
167
|
+
*/
|
|
90
168
|
static scale(factor) {
|
|
91
169
|
return Matrix2D.multiplyByValue(new Matrix2D(), factor);
|
|
92
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
173
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
174
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
175
|
+
*/
|
|
93
176
|
static rotate(radians) {
|
|
94
177
|
const cos = Math.cos(radians);
|
|
95
178
|
const sin = Math.sin(radians);
|
|
@@ -99,6 +182,12 @@ class Matrix2D {
|
|
|
99
182
|
[0, 0, 1]
|
|
100
183
|
]);
|
|
101
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
187
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
188
|
+
* and y, and an optional w component.
|
|
189
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
190
|
+
*/
|
|
102
191
|
static translate(vector) {
|
|
103
192
|
return new Matrix2D([
|
|
104
193
|
[1, 0, vector.x],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matrix2d.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix2d.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"matrix2d.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/matrix2d.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,yCAAkC;AAElC,MAAa,QAAQ;IAGnB;;;;;OAKG;IACH,YAAY,KAA6B;QACvC,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;YAChC,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAAC;SAClC;aAAM,IAAI,KAAK,YAAY,kBAAQ,EAAE;YACpC,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,QAAQ,CAA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Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { Direction, NavigatorParams, Turning } from '../../types';
|
|
9
|
+
export declare class Character {
|
|
10
|
+
direction: Direction;
|
|
11
|
+
turn: () => Character;
|
|
12
|
+
/**
|
|
13
|
+
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
|
|
14
|
+
* Character class.
|
|
15
|
+
* @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
|
|
16
|
+
* can be any value that represents a direction, such as "north", "south", "east", or "west".
|
|
17
|
+
* @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
|
|
18
|
+
* turning direction. It is used to determine the new direction when the character turns.
|
|
19
|
+
*/
|
|
20
|
+
constructor(direction: Direction, turning: Turning);
|
|
21
|
+
}
|
|
22
|
+
export declare class Navigator<T = number> {
|
|
23
|
+
onMove: (cur: [number, number]) => void;
|
|
24
|
+
private readonly _matrix;
|
|
25
|
+
private readonly _cur;
|
|
26
|
+
private _character;
|
|
27
|
+
private readonly _VISITED;
|
|
28
|
+
/**
|
|
29
|
+
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
30
|
+
* in the matrix.
|
|
31
|
+
* @param - - `matrix`: a 2D array representing the grid or map
|
|
32
|
+
*/
|
|
33
|
+
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }: NavigatorParams<T>);
|
|
34
|
+
/**
|
|
35
|
+
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
36
|
+
* character and repeats the process.
|
|
37
|
+
*/
|
|
38
|
+
start(): void;
|
|
39
|
+
/**
|
|
40
|
+
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
41
|
+
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
42
|
+
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
43
|
+
* @returns a boolean value.
|
|
44
|
+
*/
|
|
45
|
+
check(direction: Direction): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
48
|
+
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
49
|
+
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
50
|
+
*/
|
|
51
|
+
move(direction: Direction): void;
|
|
52
|
+
}
|
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Navigator = exports.Character = void 0;
|
|
4
4
|
class Character {
|
|
5
|
+
/**
|
|
6
|
+
* The constructor function takes in a direction and turning object and sets the direction and turn properties of the
|
|
7
|
+
* Character class.
|
|
8
|
+
* @param {Direction} direction - The direction parameter is used to specify the current direction of the character. It
|
|
9
|
+
* can be any value that represents a direction, such as "north", "south", "east", or "west".
|
|
10
|
+
* @param {Turning} turning - The `turning` parameter is an object that maps each direction to the corresponding
|
|
11
|
+
* turning direction. It is used to determine the new direction when the character turns.
|
|
12
|
+
*/
|
|
5
13
|
constructor(direction, turning) {
|
|
6
14
|
this.direction = direction;
|
|
7
15
|
this.turn = () => new Character(turning[direction], turning);
|
|
@@ -9,6 +17,11 @@ class Character {
|
|
|
9
17
|
}
|
|
10
18
|
exports.Character = Character;
|
|
11
19
|
class Navigator {
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
22
|
+
* in the matrix.
|
|
23
|
+
* @param - - `matrix`: a 2D array representing the grid or map
|
|
24
|
+
*/
|
|
12
25
|
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
|
|
13
26
|
this._matrix = matrix;
|
|
14
27
|
this._cur = cur;
|
|
@@ -18,6 +31,10 @@ class Navigator {
|
|
|
18
31
|
this._VISITED = VISITED;
|
|
19
32
|
this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
|
|
20
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
36
|
+
* character and repeats the process.
|
|
37
|
+
*/
|
|
21
38
|
start() {
|
|
22
39
|
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
|
|
23
40
|
const { direction } = this._character;
|
|
@@ -29,6 +46,12 @@ class Navigator {
|
|
|
29
46
|
}
|
|
30
47
|
}
|
|
31
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
51
|
+
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
52
|
+
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
53
|
+
* @returns a boolean value.
|
|
54
|
+
*/
|
|
32
55
|
check(direction) {
|
|
33
56
|
let forward, row;
|
|
34
57
|
const matrix = this._matrix;
|
|
@@ -55,6 +78,11 @@ class Navigator {
|
|
|
55
78
|
}
|
|
56
79
|
return forward !== undefined && forward !== this._VISITED;
|
|
57
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
83
|
+
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
84
|
+
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
85
|
+
*/
|
|
58
86
|
move(direction) {
|
|
59
87
|
switch (direction) {
|
|
60
88
|
case 'up':
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"navigator.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/navigator.ts"],"names":[],"mappings":";;;AASA,MAAa,SAAS;
|
|
1
|
+
{"version":3,"file":"navigator.js","sourceRoot":"","sources":["../../../src/data-structures/matrix/navigator.ts"],"names":[],"mappings":";;;AASA,MAAa,SAAS;IAIpB;;;;;;;OAOG;IACH,YAAY,SAAoB,EAAE,OAAgB;QAChD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,GAAG,EAAE,CAAC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