deque-typed 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1223 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1219 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +3 -3
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/deque-typed.js +1232 -0
- package/dist/umd/deque-typed.js.map +1 -0
- package/dist/umd/deque-typed.min.js +9 -0
- package/dist/umd/deque-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
- package/src/data-structures/binary-tree/avl-tree.ts +6 -8
- package/src/data-structures/binary-tree/binary-tree.ts +13 -15
- package/src/data-structures/binary-tree/bst.ts +6 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
- package/src/data-structures/binary-tree/tree-counter.ts +8 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +3 -3
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Matrix = void 0;
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
*/
|
|
7
|
-
class Matrix {
|
|
8
|
-
/**
|
|
9
|
-
* The constructor function initializes a matrix object with the provided data and options, or with
|
|
10
|
-
* default values if no options are provided.
|
|
11
|
-
* @param {number[][]} data - A 2D array of numbers representing the data for the matrix.
|
|
12
|
-
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
13
|
-
* properties:
|
|
14
|
-
*/
|
|
15
|
-
constructor(data, options) {
|
|
16
|
-
var _a, _b, _c;
|
|
17
|
-
this._rows = 0;
|
|
18
|
-
this._cols = 0;
|
|
19
|
-
if (options) {
|
|
20
|
-
const { rows, cols, addFn, subtractFn, multiplyFn } = options;
|
|
21
|
-
if (typeof rows === 'number' && rows > 0)
|
|
22
|
-
this._rows = rows;
|
|
23
|
-
else
|
|
24
|
-
this._rows = data.length;
|
|
25
|
-
if (typeof cols === 'number' && cols > 0)
|
|
26
|
-
this._cols = cols;
|
|
27
|
-
else
|
|
28
|
-
this._cols = ((_a = data[0]) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
29
|
-
if (addFn)
|
|
30
|
-
this._addFn = addFn;
|
|
31
|
-
if (subtractFn)
|
|
32
|
-
this._subtractFn = subtractFn;
|
|
33
|
-
if (multiplyFn)
|
|
34
|
-
this._multiplyFn = multiplyFn;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this._rows = data.length;
|
|
38
|
-
this._cols = (_c = (_b = data[0]) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0;
|
|
39
|
-
}
|
|
40
|
-
if (data.length > 0) {
|
|
41
|
-
this._data = data;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
this._data = [];
|
|
45
|
-
for (let i = 0; i < this.rows; i++) {
|
|
46
|
-
this._data[i] = new Array(this.cols).fill(0);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* The function returns the number of rows.
|
|
52
|
-
* @returns The number of rows.
|
|
53
|
-
*/
|
|
54
|
-
get rows() {
|
|
55
|
-
return this._rows;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* The function returns the value of the protected variable _cols.
|
|
59
|
-
* @returns The number of columns.
|
|
60
|
-
*/
|
|
61
|
-
get cols() {
|
|
62
|
-
return this._cols;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* The function returns a two-dimensional array of numbers.
|
|
66
|
-
* @returns The data property, which is a two-dimensional array of numbers.
|
|
67
|
-
*/
|
|
68
|
-
get data() {
|
|
69
|
-
return this._data;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* The above function returns the value of the _addFn property.
|
|
73
|
-
* @returns The value of the property `_addFn` is being returned.
|
|
74
|
-
*/
|
|
75
|
-
get addFn() {
|
|
76
|
-
return this._addFn;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* The function returns the value of the _subtractFn property.
|
|
80
|
-
* @returns The `_subtractFn` property is being returned.
|
|
81
|
-
*/
|
|
82
|
-
get subtractFn() {
|
|
83
|
-
return this._subtractFn;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The function returns the value of the _multiplyFn property.
|
|
87
|
-
* @returns The `_multiplyFn` property is being returned.
|
|
88
|
-
*/
|
|
89
|
-
get multiplyFn() {
|
|
90
|
-
return this._multiplyFn;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* The `get` function returns the value at the specified row and column index if it is a valid index.
|
|
94
|
-
* @param {number} row - The `row` parameter represents the row index of the element you want to
|
|
95
|
-
* retrieve from the data array.
|
|
96
|
-
* @param {number} col - The parameter "col" represents the column number of the element you want to
|
|
97
|
-
* retrieve from the data array.
|
|
98
|
-
* @returns The `get` function returns a number if the provided row and column indices are valid.
|
|
99
|
-
* Otherwise, it returns `undefined`.
|
|
100
|
-
*/
|
|
101
|
-
get(row, col) {
|
|
102
|
-
if (this.isValidIndex(row, col)) {
|
|
103
|
-
return this.data[row][col];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* The set function updates the value at a specified row and column in a two-dimensional array.
|
|
108
|
-
* @param {number} row - The "row" parameter represents the row index of the element in a
|
|
109
|
-
* two-dimensional array or matrix. It specifies the row where the value will be set.
|
|
110
|
-
* @param {number} col - The "col" parameter represents the column index of the element in a
|
|
111
|
-
* two-dimensional array.
|
|
112
|
-
* @param {number} value - The value parameter represents the number that you want to set at the
|
|
113
|
-
* specified row and column in the data array.
|
|
114
|
-
* @returns a boolean value. It returns true if the index (row, col) is valid and the value is
|
|
115
|
-
* successfully set in the data array. It returns false if the index is invalid and the value is not
|
|
116
|
-
* set.
|
|
117
|
-
*/
|
|
118
|
-
set(row, col, value) {
|
|
119
|
-
if (this.isValidIndex(row, col)) {
|
|
120
|
-
this.data[row][col] = value;
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* The function checks if the dimensions of the given matrix match the dimensions of the current
|
|
127
|
-
* matrix.
|
|
128
|
-
* @param {Matrix} matrix - The parameter `matrix` is of type `Matrix`.
|
|
129
|
-
* @returns a boolean value.
|
|
130
|
-
*/
|
|
131
|
-
isMatchForCalculate(matrix) {
|
|
132
|
-
return this.rows === matrix.rows && this.cols === matrix.cols;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* The `add` function adds two matrices together, returning a new matrix with the result.
|
|
136
|
-
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
137
|
-
* @returns The `add` method returns a new `Matrix` object that represents the result of adding the
|
|
138
|
-
* current matrix with the provided `matrix` parameter.
|
|
139
|
-
*/
|
|
140
|
-
add(matrix) {
|
|
141
|
-
if (!this.isMatchForCalculate(matrix)) {
|
|
142
|
-
throw new Error('Matrix dimensions must match for addition.');
|
|
143
|
-
}
|
|
144
|
-
const resultData = [];
|
|
145
|
-
for (let i = 0; i < this.rows; i++) {
|
|
146
|
-
resultData[i] = [];
|
|
147
|
-
for (let j = 0; j < this.cols; j++) {
|
|
148
|
-
const a = this.get(i, j), b = matrix.get(i, j);
|
|
149
|
-
if (a !== undefined && b !== undefined) {
|
|
150
|
-
const added = this._addFn(a, b);
|
|
151
|
-
if (added) {
|
|
152
|
-
resultData[i][j] = added;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return new Matrix(resultData, {
|
|
158
|
-
rows: this.rows,
|
|
159
|
-
cols: this.cols,
|
|
160
|
-
addFn: this.addFn,
|
|
161
|
-
subtractFn: this.subtractFn,
|
|
162
|
-
multiplyFn: this.multiplyFn
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* The `subtract` function performs element-wise subtraction between two matrices and returns a new
|
|
167
|
-
* matrix with the result.
|
|
168
|
-
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class. It
|
|
169
|
-
* represents the matrix that you want to subtract from the current matrix.
|
|
170
|
-
* @returns a new Matrix object with the result of the subtraction operation.
|
|
171
|
-
*/
|
|
172
|
-
subtract(matrix) {
|
|
173
|
-
if (!this.isMatchForCalculate(matrix)) {
|
|
174
|
-
throw new Error('Matrix dimensions must match for subtraction.');
|
|
175
|
-
}
|
|
176
|
-
const resultData = [];
|
|
177
|
-
for (let i = 0; i < this.rows; i++) {
|
|
178
|
-
resultData[i] = [];
|
|
179
|
-
for (let j = 0; j < this.cols; j++) {
|
|
180
|
-
const a = this.get(i, j), b = matrix.get(i, j);
|
|
181
|
-
if (a !== undefined && b !== undefined) {
|
|
182
|
-
const subtracted = this._subtractFn(a, b);
|
|
183
|
-
if (subtracted) {
|
|
184
|
-
resultData[i][j] = subtracted;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return new Matrix(resultData, {
|
|
190
|
-
rows: this.rows,
|
|
191
|
-
cols: this.cols,
|
|
192
|
-
addFn: this.addFn,
|
|
193
|
-
subtractFn: this.subtractFn,
|
|
194
|
-
multiplyFn: this.multiplyFn
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* The `multiply` function performs matrix multiplication between two matrices and returns the result
|
|
199
|
-
* as a new matrix.
|
|
200
|
-
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
201
|
-
* @returns a new Matrix object.
|
|
202
|
-
*/
|
|
203
|
-
multiply(matrix) {
|
|
204
|
-
if (this.cols !== matrix.rows) {
|
|
205
|
-
throw new Error('Matrix dimensions must be compatible for multiplication (A.cols = B.rows).');
|
|
206
|
-
}
|
|
207
|
-
const resultData = [];
|
|
208
|
-
for (let i = 0; i < this.rows; i++) {
|
|
209
|
-
resultData[i] = [];
|
|
210
|
-
for (let j = 0; j < matrix.cols; j++) {
|
|
211
|
-
let sum;
|
|
212
|
-
for (let k = 0; k < this.cols; k++) {
|
|
213
|
-
const a = this.get(i, k), b = matrix.get(k, j);
|
|
214
|
-
if (a !== undefined && b !== undefined) {
|
|
215
|
-
const multiplied = this.multiplyFn(a, b);
|
|
216
|
-
if (multiplied !== undefined) {
|
|
217
|
-
sum = this.addFn(sum, multiplied);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (sum !== undefined)
|
|
222
|
-
resultData[i][j] = sum;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return new Matrix(resultData, {
|
|
226
|
-
rows: this.rows,
|
|
227
|
-
cols: matrix.cols,
|
|
228
|
-
addFn: this.addFn,
|
|
229
|
-
subtractFn: this.subtractFn,
|
|
230
|
-
multiplyFn: this.multiplyFn
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* The transpose function takes a matrix and returns a new matrix that is the transpose of the
|
|
235
|
-
* original matrix.
|
|
236
|
-
* @returns The transpose() function returns a new Matrix object with the transposed data.
|
|
237
|
-
*/
|
|
238
|
-
transpose() {
|
|
239
|
-
if (this.data.some(row => row.length !== this.rows)) {
|
|
240
|
-
throw new Error('Matrix must be rectangular for transposition.');
|
|
241
|
-
}
|
|
242
|
-
const resultData = [];
|
|
243
|
-
for (let j = 0; j < this.cols; j++) {
|
|
244
|
-
resultData[j] = [];
|
|
245
|
-
for (let i = 0; i < this.rows; i++) {
|
|
246
|
-
const trans = this.get(i, j);
|
|
247
|
-
if (trans !== undefined)
|
|
248
|
-
resultData[j][i] = trans;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return new Matrix(resultData, {
|
|
252
|
-
rows: this.cols,
|
|
253
|
-
cols: this.rows,
|
|
254
|
-
addFn: this.addFn,
|
|
255
|
-
subtractFn: this.subtractFn,
|
|
256
|
-
multiplyFn: this.multiplyFn
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* The `inverse` function calculates the inverse of a square matrix using Gaussian elimination.
|
|
261
|
-
* @returns a Matrix object, which represents the inverse of the original matrix.
|
|
262
|
-
*/
|
|
263
|
-
inverse() {
|
|
264
|
-
var _a;
|
|
265
|
-
// Check if the matrix is square
|
|
266
|
-
if (this.rows !== this.cols) {
|
|
267
|
-
throw new Error('Matrix must be square for inversion.');
|
|
268
|
-
}
|
|
269
|
-
// Create an augmented matrix [this | I]
|
|
270
|
-
const augmentedMatrixData = [];
|
|
271
|
-
for (let i = 0; i < this.rows; i++) {
|
|
272
|
-
augmentedMatrixData[i] = this.data[i].slice(); // Copy the original matrix
|
|
273
|
-
for (let j = 0; j < this.cols; j++) {
|
|
274
|
-
augmentedMatrixData[i][this.cols + j] = i === j ? 1 : 0; // Append the identity matrix
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
const augmentedMatrix = new Matrix(augmentedMatrixData, {
|
|
278
|
-
rows: this.rows,
|
|
279
|
-
cols: this.cols * 2,
|
|
280
|
-
addFn: this.addFn,
|
|
281
|
-
subtractFn: this.subtractFn,
|
|
282
|
-
multiplyFn: this.multiplyFn
|
|
283
|
-
});
|
|
284
|
-
// Apply Gaussian elimination to transform the left half into the identity matrix
|
|
285
|
-
for (let i = 0; i < this.rows; i++) {
|
|
286
|
-
// Find pivot
|
|
287
|
-
let pivotRow = i;
|
|
288
|
-
while (pivotRow < this.rows && augmentedMatrix.get(pivotRow, i) === 0) {
|
|
289
|
-
pivotRow++;
|
|
290
|
-
}
|
|
291
|
-
if (pivotRow === this.rows) {
|
|
292
|
-
// Matrix is singular, and its inverse does not exist
|
|
293
|
-
throw new Error('Matrix is singular, and its inverse does not exist.');
|
|
294
|
-
}
|
|
295
|
-
// Swap rows to make the pivot the current row
|
|
296
|
-
augmentedMatrix._swapRows(i, pivotRow);
|
|
297
|
-
// Scale the pivot row to make the pivot element 1
|
|
298
|
-
const pivotElement = (_a = augmentedMatrix.get(i, i)) !== null && _a !== void 0 ? _a : 1;
|
|
299
|
-
if (pivotElement === 0) {
|
|
300
|
-
// Handle division by zero
|
|
301
|
-
throw new Error('Matrix is singular, and its inverse does not exist (division by zero).');
|
|
302
|
-
}
|
|
303
|
-
augmentedMatrix._scaleRow(i, 1 / pivotElement);
|
|
304
|
-
// Eliminate other rows to make elements in the current column zero
|
|
305
|
-
for (let j = 0; j < this.rows; j++) {
|
|
306
|
-
if (j !== i) {
|
|
307
|
-
let factor = augmentedMatrix.get(j, i);
|
|
308
|
-
if (factor === undefined)
|
|
309
|
-
factor = 0;
|
|
310
|
-
augmentedMatrix._addScaledRow(j, i, -factor);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
// Extract the right half of the augmented matrix as the inverse
|
|
315
|
-
const inverseData = [];
|
|
316
|
-
for (let i = 0; i < this.rows; i++) {
|
|
317
|
-
inverseData[i] = augmentedMatrix.data[i].slice(this.cols);
|
|
318
|
-
}
|
|
319
|
-
return new Matrix(inverseData, {
|
|
320
|
-
rows: this.rows,
|
|
321
|
-
cols: this.cols,
|
|
322
|
-
addFn: this.addFn,
|
|
323
|
-
subtractFn: this.subtractFn,
|
|
324
|
-
multiplyFn: this.multiplyFn
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* The dot function calculates the dot product of two matrices and returns a new matrix.
|
|
329
|
-
* @param {Matrix} matrix - The `matrix` parameter is an instance of the `Matrix` class.
|
|
330
|
-
* @returns a new Matrix object.
|
|
331
|
-
*/
|
|
332
|
-
dot(matrix) {
|
|
333
|
-
if (this.cols !== matrix.rows) {
|
|
334
|
-
throw new Error('Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.');
|
|
335
|
-
}
|
|
336
|
-
const resultData = [];
|
|
337
|
-
for (let i = 0; i < this.rows; i++) {
|
|
338
|
-
resultData[i] = [];
|
|
339
|
-
for (let j = 0; j < matrix.cols; j++) {
|
|
340
|
-
let sum;
|
|
341
|
-
for (let k = 0; k < this.cols; k++) {
|
|
342
|
-
const a = this.get(i, k), b = matrix.get(k, j);
|
|
343
|
-
if (a !== undefined && b !== undefined) {
|
|
344
|
-
const multiplied = this.multiplyFn(a, b);
|
|
345
|
-
if (multiplied !== undefined) {
|
|
346
|
-
sum = this.addFn(sum, multiplied);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
if (sum !== undefined)
|
|
351
|
-
resultData[i][j] = sum;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return new Matrix(resultData, {
|
|
355
|
-
rows: this.rows,
|
|
356
|
-
cols: matrix.cols,
|
|
357
|
-
addFn: this.addFn,
|
|
358
|
-
subtractFn: this.subtractFn,
|
|
359
|
-
multiplyFn: this.multiplyFn
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* The function checks if a given row and column index is valid within a specified range.
|
|
364
|
-
* @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
|
|
365
|
-
* matrix. It is a number that indicates the specific row in the matrix.
|
|
366
|
-
* @param {number} col - The "col" parameter represents the column index in a two-dimensional array
|
|
367
|
-
* or grid. It is used to check if the given column index is valid within the bounds of the grid.
|
|
368
|
-
* @returns A boolean value is being returned.
|
|
369
|
-
*/
|
|
370
|
-
isValidIndex(row, col) {
|
|
371
|
-
return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* The `clone` function returns a new instance of the Matrix class with the same data and properties
|
|
375
|
-
* as the original instance.
|
|
376
|
-
* @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
|
|
377
|
-
* and properties as the current instance.
|
|
378
|
-
*/
|
|
379
|
-
clone() {
|
|
380
|
-
return new Matrix(this.data, {
|
|
381
|
-
rows: this.rows,
|
|
382
|
-
cols: this.cols,
|
|
383
|
-
addFn: this.addFn,
|
|
384
|
-
subtractFn: this.subtractFn,
|
|
385
|
-
multiplyFn: this.multiplyFn
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
_addFn(a, b) {
|
|
389
|
-
if (a === undefined)
|
|
390
|
-
return b;
|
|
391
|
-
return a + b;
|
|
392
|
-
}
|
|
393
|
-
_subtractFn(a, b) {
|
|
394
|
-
return a - b;
|
|
395
|
-
}
|
|
396
|
-
_multiplyFn(a, b) {
|
|
397
|
-
return a * b;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* The function `_swapRows` swaps the positions of two rows in an array.
|
|
401
|
-
* @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
|
|
402
|
-
* @param {number} row2 - The `row2` parameter is the index of the second row that you want to swap
|
|
403
|
-
* with the first row.
|
|
404
|
-
*/
|
|
405
|
-
_swapRows(row1, row2) {
|
|
406
|
-
const temp = this.data[row1];
|
|
407
|
-
this.data[row1] = this.data[row2];
|
|
408
|
-
this.data[row2] = temp;
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* The function scales a specific row in a matrix by a given scalar value.
|
|
412
|
-
* @param {number} row - The `row` parameter represents the index of the row in the matrix that you
|
|
413
|
-
* want to scale. It is a number that indicates the position of the row within the matrix.
|
|
414
|
-
* @param {number} scalar - The scalar parameter is a number that is used to multiply each element in
|
|
415
|
-
* a specific row of a matrix.
|
|
416
|
-
*/
|
|
417
|
-
_scaleRow(row, scalar) {
|
|
418
|
-
for (let j = 0; j < this.cols; j++) {
|
|
419
|
-
let multiplied = this.multiplyFn(this.data[row][j], scalar);
|
|
420
|
-
if (multiplied === undefined)
|
|
421
|
-
multiplied = 0;
|
|
422
|
-
this.data[row][j] = multiplied;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
/**
|
|
426
|
-
* The function `_addScaledRow` multiplies a row in a matrix by a scalar value and adds it to another
|
|
427
|
-
* row.
|
|
428
|
-
* @param {number} targetRow - The targetRow parameter represents the index of the row in which the
|
|
429
|
-
* scaled values will be added.
|
|
430
|
-
* @param {number} sourceRow - The sourceRow parameter represents the index of the row from which the
|
|
431
|
-
* values will be scaled and added to the targetRow.
|
|
432
|
-
* @param {number} scalar - The scalar parameter is a number that is used to scale the values in the
|
|
433
|
-
* source row before adding them to the target row.
|
|
434
|
-
*/
|
|
435
|
-
_addScaledRow(targetRow, sourceRow, scalar) {
|
|
436
|
-
for (let j = 0; j < this.cols; j++) {
|
|
437
|
-
let multiplied = this.multiplyFn(this.data[sourceRow][j], scalar);
|
|
438
|
-
if (multiplied === undefined)
|
|
439
|
-
multiplied = 0;
|
|
440
|
-
const scaledValue = multiplied;
|
|
441
|
-
let added = this.addFn(this.data[targetRow][j], scaledValue);
|
|
442
|
-
if (added === undefined)
|
|
443
|
-
added = 0;
|
|
444
|
-
this.data[targetRow][j] = added;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
exports.Matrix = Matrix;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Pablo Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Pablo 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
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
*/
|
|
25
|
-
export declare class Navigator<T = number> {
|
|
26
|
-
onMove: (cur: [number, number]) => void;
|
|
27
|
-
protected readonly _matrix: T[][];
|
|
28
|
-
protected readonly _cur: [number, number];
|
|
29
|
-
protected _character: Character;
|
|
30
|
-
protected readonly _VISITED: T;
|
|
31
|
-
/**
|
|
32
|
-
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
33
|
-
* in the matrix.
|
|
34
|
-
* @param - - `matrix`: a 2D array representing the grid or map
|
|
35
|
-
*/
|
|
36
|
-
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }: NavigatorParams<T>);
|
|
37
|
-
/**
|
|
38
|
-
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
39
|
-
* character and repeats the process.
|
|
40
|
-
*/
|
|
41
|
-
start(): void;
|
|
42
|
-
/**
|
|
43
|
-
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
44
|
-
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
45
|
-
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
46
|
-
* @returns a boolean value.
|
|
47
|
-
*/
|
|
48
|
-
check(direction: Direction): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
51
|
-
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
52
|
-
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
53
|
-
*/
|
|
54
|
-
move(direction: Direction): void;
|
|
55
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Navigator = exports.Character = void 0;
|
|
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
|
-
*/
|
|
13
|
-
constructor(direction, turning) {
|
|
14
|
-
this.direction = direction;
|
|
15
|
-
this.turn = () => new Character(turning[direction], turning);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.Character = Character;
|
|
19
|
-
/**
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
class Navigator {
|
|
23
|
-
/**
|
|
24
|
-
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
25
|
-
* in the matrix.
|
|
26
|
-
* @param - - `matrix`: a 2D array representing the grid or map
|
|
27
|
-
*/
|
|
28
|
-
constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
|
|
29
|
-
this._matrix = matrix;
|
|
30
|
-
this._cur = cur;
|
|
31
|
-
this._character = new Character(charDir, turning);
|
|
32
|
-
this.onMove = onMove;
|
|
33
|
-
if (this.onMove)
|
|
34
|
-
this.onMove(this._cur);
|
|
35
|
-
this._VISITED = VISITED;
|
|
36
|
-
this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* The "start" function moves the character in its current direction until it encounters an obstacle, then it turns the
|
|
40
|
-
* character and repeats the process.
|
|
41
|
-
*/
|
|
42
|
-
start() {
|
|
43
|
-
while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
|
|
44
|
-
const { direction } = this._character;
|
|
45
|
-
if (this.check(direction)) {
|
|
46
|
-
this.move(direction);
|
|
47
|
-
}
|
|
48
|
-
else if (this.check(this._character.turn().direction)) {
|
|
49
|
-
this._character = this._character.turn();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* The function checks if there is a valid move in the specified direction in a matrix.
|
|
55
|
-
* @param {Direction} direction - The direction parameter is a string that represents the direction in which to check.
|
|
56
|
-
* It can be one of the following values: 'up', 'right', 'down', or 'left'.
|
|
57
|
-
* @returns a boolean value.
|
|
58
|
-
*/
|
|
59
|
-
check(direction) {
|
|
60
|
-
let forward, row;
|
|
61
|
-
const matrix = this._matrix;
|
|
62
|
-
const [i, j] = this._cur;
|
|
63
|
-
switch (direction) {
|
|
64
|
-
case 'up':
|
|
65
|
-
row = matrix[i - 1];
|
|
66
|
-
if (!row)
|
|
67
|
-
return false;
|
|
68
|
-
forward = row[j];
|
|
69
|
-
break;
|
|
70
|
-
case 'right':
|
|
71
|
-
forward = matrix[i][j + 1];
|
|
72
|
-
break;
|
|
73
|
-
case 'down':
|
|
74
|
-
row = matrix[i + 1];
|
|
75
|
-
if (!row)
|
|
76
|
-
return false;
|
|
77
|
-
forward = row[j];
|
|
78
|
-
break;
|
|
79
|
-
case 'left':
|
|
80
|
-
forward = matrix[i][j - 1];
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
return forward !== undefined && forward !== this._VISITED;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The `move` function updates the current position based on the given direction and updates the matrix accordingly.
|
|
87
|
-
* @param {Direction} direction - The `direction` parameter is a string that represents the direction in which to move.
|
|
88
|
-
* It can have one of the following values: 'up', 'right', 'down', or 'left'.
|
|
89
|
-
*/
|
|
90
|
-
move(direction) {
|
|
91
|
-
switch (direction) {
|
|
92
|
-
case 'up':
|
|
93
|
-
this._cur[0]--;
|
|
94
|
-
break;
|
|
95
|
-
case 'right':
|
|
96
|
-
this._cur[1]++;
|
|
97
|
-
break;
|
|
98
|
-
case 'down':
|
|
99
|
-
this._cur[0]++;
|
|
100
|
-
break;
|
|
101
|
-
case 'left':
|
|
102
|
-
this._cur[1]--;
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
const [i, j] = this._cur;
|
|
106
|
-
this._matrix[i][j] = this._VISITED;
|
|
107
|
-
if (this.onMove)
|
|
108
|
-
this.onMove(this._cur);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
exports.Navigator = Navigator;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./priority-queue"), exports);
|
|
18
|
-
__exportStar(require("./min-priority-queue"), exports);
|
|
19
|
-
__exportStar(require("./max-priority-queue"), exports);
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Kirk Qi
|
|
5
|
-
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import type { PriorityQueueOptions } from '../../types';
|
|
9
|
-
import { PriorityQueue } from './priority-queue';
|
|
10
|
-
/**
|
|
11
|
-
* Max-oriented priority queue (max-heap) built on {@link PriorityQueue}.
|
|
12
|
-
* The default comparator orders primitive values in descending order. If you store objects,
|
|
13
|
-
* you must provide a custom comparator via {@link PriorityQueueOptions}.
|
|
14
|
-
* @template E Element type stored in the queue.
|
|
15
|
-
* @template R Extra record/metadata associated with each element.
|
|
16
|
-
* @example
|
|
17
|
-
*/
|
|
18
|
-
export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
|
|
19
|
-
/**
|
|
20
|
-
* Creates a max-priority queue.
|
|
21
|
-
* @param elements Optional initial elements to insert.
|
|
22
|
-
* @param options Optional configuration (e.g., `comparator`, `toElementFn`).
|
|
23
|
-
* @throws {TypeError} Thrown when using the default comparator with object elements (provide a custom comparator).
|
|
24
|
-
* @remarks Complexity — Time: O(n log n) when inserting n elements incrementally; Space: O(n).
|
|
25
|
-
*/
|
|
26
|
-
constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
|
|
27
|
-
}
|