min-heap-typed 1.40.0-rc → 1.41.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/.dependency-cruiser.js +422 -422
- package/.eslintrc.js +59 -59
- package/.prettierrc.js +14 -14
- package/README.md +20 -3
- package/coverage/clover.xml +11 -7
- package/coverage/coverage-final.json +95 -1
- package/coverage/coverage-summary.json +59 -2
- package/coverage/lcov-report/base.css +278 -99
- package/coverage/lcov-report/index.html +69 -65
- package/coverage/lcov-report/index.ts.html +36 -35
- package/coverage/lcov-report/sorter.js +15 -5
- package/dist/data-structures/binary-tree/avl-tree.d.ts +106 -0
- package/dist/data-structures/binary-tree/avl-tree.js +347 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +149 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +269 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +363 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1135 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +97 -0
- package/dist/data-structures/binary-tree/rb-tree.js +388 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +67 -0
- package/dist/data-structures/binary-tree/segment-tree.js +180 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +126 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +355 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +313 -0
- package/dist/data-structures/graph/abstract-graph.js +884 -0
- package/dist/data-structures/graph/directed-graph.d.ts +194 -0
- package/dist/data-structures/graph/directed-graph.js +404 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -0
- package/dist/data-structures/graph/map-graph.d.ts +73 -0
- package/dist/data-structures/graph/map-graph.js +93 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +120 -0
- package/dist/data-structures/graph/undirected-graph.js +239 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +44 -0
- package/dist/data-structures/hash/coordinate-map.js +62 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +36 -0
- package/dist/data-structures/hash/coordinate-set.js +52 -0
- package/dist/data-structures/hash/hash-map.d.ts +50 -0
- package/dist/data-structures/hash/hash-map.js +153 -0
- package/dist/data-structures/hash/hash-table.d.ts +103 -0
- package/dist/data-structures/hash/hash-table.js +236 -0
- package/dist/data-structures/hash/index.d.ts +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +235 -0
- package/dist/data-structures/heap/heap.js +515 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +15 -0
- package/dist/data-structures/heap/max-heap.js +26 -0
- package/dist/data-structures/heap/min-heap.d.ts +15 -0
- package/dist/data-structures/heap/min-heap.js +26 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +253 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +569 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +232 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +533 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +80 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +187 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +107 -0
- package/dist/data-structures/matrix/matrix2d.js +199 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +200 -0
- package/dist/data-structures/matrix/vector2d.js +290 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +26 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +161 -0
- package/dist/data-structures/queue/deque.js +264 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +122 -0
- package/dist/data-structures/queue/queue.js +187 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +64 -0
- package/dist/data-structures/stack/stack.js +94 -0
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +8 -0
- package/dist/data-structures/tree/tree.js +40 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +79 -0
- package/dist/data-structures/trie/trie.js +251 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +18 -4
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +13 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/helpers.d.ts +8 -0
- package/dist/types/helpers.js +9 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +73 -0
- package/jest.config.js +6 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +350 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +306 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1284 -0
- package/src/data-structures/binary-tree/bst.ts +522 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +426 -0
- package/src/data-structures/binary-tree/segment-tree.ts +190 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +379 -0
- package/src/data-structures/graph/abstract-graph.ts +1000 -0
- package/src/data-structures/graph/directed-graph.ts +449 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +106 -0
- package/src/data-structures/graph/undirected-graph.ts +259 -0
- package/src/data-structures/hash/coordinate-map.ts +63 -0
- package/src/data-structures/hash/coordinate-set.ts +52 -0
- package/src/data-structures/hash/hash-map.ts +185 -0
- package/src/data-structures/hash/hash-table.ts +268 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +589 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +26 -0
- package/src/data-structures/heap/min-heap.ts +26 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +605 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +576 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +221 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +211 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +315 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +25 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +282 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +209 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +102 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +41 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +262 -0
- package/src/index.ts +4 -1
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +11 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +86 -0
- package/tsconfig.json +1 -2
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SkipList = exports.SkipListNode = void 0;
|
|
11
|
+
class SkipListNode {
|
|
12
|
+
constructor(key, value, level) {
|
|
13
|
+
this.key = key;
|
|
14
|
+
this.value = value;
|
|
15
|
+
this.forward = new Array(level);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.SkipListNode = SkipListNode;
|
|
19
|
+
class SkipList {
|
|
20
|
+
/**
|
|
21
|
+
* The constructor initializes a SkipList with a specified maximum level and probability.
|
|
22
|
+
* @param [maxLevel=16] - The `maxLevel` parameter represents the maximum level that a skip list can have. It determines
|
|
23
|
+
* the maximum number of levels that can be created in the skip list.
|
|
24
|
+
* @param [probability=0.5] - The probability parameter represents the probability of a node being promoted to a higher
|
|
25
|
+
* level in the skip list. It is used to determine the height of each node in the skip list.
|
|
26
|
+
*/
|
|
27
|
+
constructor(maxLevel = 16, probability = 0.5) {
|
|
28
|
+
this._head = new SkipListNode(null, null, maxLevel);
|
|
29
|
+
this._level = 0;
|
|
30
|
+
this._maxLevel = maxLevel;
|
|
31
|
+
this._probability = probability;
|
|
32
|
+
}
|
|
33
|
+
get head() {
|
|
34
|
+
return this._head;
|
|
35
|
+
}
|
|
36
|
+
get level() {
|
|
37
|
+
return this._level;
|
|
38
|
+
}
|
|
39
|
+
get maxLevel() {
|
|
40
|
+
return this._maxLevel;
|
|
41
|
+
}
|
|
42
|
+
get probability() {
|
|
43
|
+
return this._probability;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
47
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
48
|
+
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
|
|
49
|
+
* List.
|
|
50
|
+
*/
|
|
51
|
+
add(key, value) {
|
|
52
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
53
|
+
const update = new Array(this.maxLevel).fill(this.head);
|
|
54
|
+
let current = this.head;
|
|
55
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
56
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
57
|
+
current = current.forward[i];
|
|
58
|
+
}
|
|
59
|
+
update[i] = current;
|
|
60
|
+
}
|
|
61
|
+
for (let i = 0; i < newNode.forward.length; i++) {
|
|
62
|
+
newNode.forward[i] = update[i].forward[i];
|
|
63
|
+
update[i].forward[i] = newNode;
|
|
64
|
+
}
|
|
65
|
+
if (newNode.forward[0] !== null) {
|
|
66
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The function `get` retrieves the value associated with a given key from a skip list data structure.
|
|
71
|
+
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
|
|
72
|
+
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
|
|
73
|
+
* otherwise it returns `undefined`.
|
|
74
|
+
*/
|
|
75
|
+
get(key) {
|
|
76
|
+
let current = this.head;
|
|
77
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
78
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
79
|
+
current = current.forward[i];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
current = current.forward[0];
|
|
83
|
+
if (current && current.key === key) {
|
|
84
|
+
return current.value;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
has(key) {
|
|
89
|
+
return this.get(key) !== undefined;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
93
|
+
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
94
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
|
|
95
|
+
* skip list, and `false` if the key was not found in the skip list.
|
|
96
|
+
*/
|
|
97
|
+
delete(key) {
|
|
98
|
+
const update = new Array(this.maxLevel).fill(this.head);
|
|
99
|
+
let current = this.head;
|
|
100
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
101
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
102
|
+
current = current.forward[i];
|
|
103
|
+
}
|
|
104
|
+
update[i] = current;
|
|
105
|
+
}
|
|
106
|
+
current = current.forward[0];
|
|
107
|
+
if (current && current.key === key) {
|
|
108
|
+
for (let i = 0; i < this.level; i++) {
|
|
109
|
+
if (update[i].forward[i] !== current) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
update[i].forward[i] = current.forward[i];
|
|
113
|
+
}
|
|
114
|
+
while (this.level > 0 && this.head.forward[this.level - 1] === null) {
|
|
115
|
+
this._level--;
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
123
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
124
|
+
*/
|
|
125
|
+
getFirst() {
|
|
126
|
+
const firstNode = this.head.forward[0];
|
|
127
|
+
return firstNode ? firstNode.value : undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
131
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
132
|
+
*/
|
|
133
|
+
getLast() {
|
|
134
|
+
let current = this.head;
|
|
135
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
136
|
+
while (current.forward[i]) {
|
|
137
|
+
current = current.forward[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return current.value;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
144
|
+
* @param key - the given key.
|
|
145
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
146
|
+
*/
|
|
147
|
+
higher(key) {
|
|
148
|
+
let current = this.head;
|
|
149
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
150
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
151
|
+
current = current.forward[i];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const nextNode = current.forward[0];
|
|
155
|
+
return nextNode ? nextNode.value : undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
159
|
+
* @param key - the given key.
|
|
160
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
161
|
+
*/
|
|
162
|
+
lower(key) {
|
|
163
|
+
let current = this.head;
|
|
164
|
+
let lastLess = null;
|
|
165
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
166
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
167
|
+
current = current.forward[i];
|
|
168
|
+
}
|
|
169
|
+
if (current.key < key) {
|
|
170
|
+
lastLess = current;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return lastLess ? lastLess.value : undefined;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
177
|
+
* @returns the level, which is a number.
|
|
178
|
+
*/
|
|
179
|
+
_randomLevel() {
|
|
180
|
+
let level = 1;
|
|
181
|
+
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
182
|
+
level++;
|
|
183
|
+
}
|
|
184
|
+
return level;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.SkipList = SkipList;
|
|
@@ -0,0 +1,20 @@
|
|
|
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("./matrix"), exports);
|
|
18
|
+
__exportStar(require("./vector2d"), exports);
|
|
19
|
+
__exportStar(require("./matrix2d"), exports);
|
|
20
|
+
__exportStar(require("./navigator"), exports);
|
|
@@ -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
|
+
protected readonly _matrix: Array<Array<V>>;
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
|
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
|
+
*/
|
|
18
|
+
constructor(options) {
|
|
19
|
+
const { row, col, initialVal } = options;
|
|
20
|
+
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
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. */
|
|
24
|
+
toArray() {
|
|
25
|
+
return this._matrix;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.MatrixNTI2D = MatrixNTI2D;
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
protected readonly _matrix: number[][];
|
|
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 takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
36
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
37
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
38
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
39
|
+
*/
|
|
40
|
+
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
41
|
+
/**
|
|
42
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
43
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
44
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
45
|
+
* representing the matrix elements.
|
|
46
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
47
|
+
*/
|
|
48
|
+
static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
49
|
+
/**
|
|
50
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
51
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
52
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
53
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
54
|
+
*/
|
|
55
|
+
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D;
|
|
56
|
+
/**
|
|
57
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
58
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
59
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
60
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
61
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
62
|
+
*/
|
|
63
|
+
static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D;
|
|
64
|
+
/**
|
|
65
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
66
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
67
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
68
|
+
* @returns a Vector2D.
|
|
69
|
+
*/
|
|
70
|
+
static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D;
|
|
71
|
+
/**
|
|
72
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
73
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
74
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
75
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
76
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
77
|
+
* @returns a Matrix2D object.
|
|
78
|
+
*/
|
|
79
|
+
static view(width: number, height: number): Matrix2D;
|
|
80
|
+
/**
|
|
81
|
+
* The function scales a matrix by a given factor.
|
|
82
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
83
|
+
* should be scaled.
|
|
84
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
85
|
+
*/
|
|
86
|
+
static scale(factor: number): Matrix2D;
|
|
87
|
+
/**
|
|
88
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
89
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
90
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
91
|
+
*/
|
|
92
|
+
static rotate(radians: number): Matrix2D;
|
|
93
|
+
/**
|
|
94
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
95
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
96
|
+
* and y, and an optional w component.
|
|
97
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
98
|
+
*/
|
|
99
|
+
static translate(vector: Vector2D): Matrix2D;
|
|
100
|
+
/**
|
|
101
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
102
|
+
* _matrix array.
|
|
103
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
104
|
+
* the first column of the matrix.
|
|
105
|
+
*/
|
|
106
|
+
toVector(): Vector2D;
|
|
107
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
+
*/
|
|
11
|
+
const vector2d_1 = require("./vector2d");
|
|
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
|
+
*/
|
|
19
|
+
constructor(value) {
|
|
20
|
+
if (typeof value === 'undefined') {
|
|
21
|
+
this._matrix = Matrix2D.identity;
|
|
22
|
+
}
|
|
23
|
+
else if (value instanceof vector2d_1.Vector2D) {
|
|
24
|
+
this._matrix = Matrix2D.identity;
|
|
25
|
+
this._matrix[0][0] = value.x;
|
|
26
|
+
this._matrix[1][0] = value.y;
|
|
27
|
+
this._matrix[2][0] = value.w;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this._matrix = value;
|
|
31
|
+
}
|
|
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
|
+
*/
|
|
37
|
+
static get empty() {
|
|
38
|
+
return [[], [], []];
|
|
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
|
+
*/
|
|
44
|
+
static get identity() {
|
|
45
|
+
return [
|
|
46
|
+
[1, 0, 0],
|
|
47
|
+
[0, 1, 0],
|
|
48
|
+
[0, 0, 1]
|
|
49
|
+
];
|
|
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
|
+
*/
|
|
56
|
+
get m() {
|
|
57
|
+
return this._matrix;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The function takes two 2D matrices as input and returns their sum as a new 2D matrix.
|
|
61
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to be added.
|
|
62
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a Matrix2D object.
|
|
63
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
64
|
+
*/
|
|
65
|
+
static add(matrix1, matrix2) {
|
|
66
|
+
const result = Matrix2D.empty;
|
|
67
|
+
for (let i = 0; i < 3; i++) {
|
|
68
|
+
for (let j = 0; j < 3; j++) {
|
|
69
|
+
result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return new Matrix2D(result);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The function subtracts two 2D matrices and returns the result as a new Matrix2D object.
|
|
76
|
+
* @param {Matrix2D} matrix1 - Matrix2D - The first matrix to subtract from.
|
|
77
|
+
* @param {Matrix2D} matrix2 - Matrix2D is a class representing a 2D matrix. It has a property `m` which is a 2D array
|
|
78
|
+
* representing the matrix elements.
|
|
79
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
80
|
+
*/
|
|
81
|
+
static subtract(matrix1, matrix2) {
|
|
82
|
+
const result = Matrix2D.empty;
|
|
83
|
+
for (let i = 0; i < 3; i++) {
|
|
84
|
+
for (let j = 0; j < 3; j++) {
|
|
85
|
+
result[i][j] = matrix1.m[i][j] - matrix2.m[i][j];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return new Matrix2D(result);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The function multiplies two 2D matrices and returns the result as a new Matrix2D object.
|
|
92
|
+
* @param {Matrix2D} matrix1 - A 2D matrix represented by the Matrix2D class.
|
|
93
|
+
* @param {Matrix2D} matrix2 - The parameter `matrix2` is a 2D matrix of size 3x3.
|
|
94
|
+
* @returns a new instance of the Matrix2D class, created using the result array.
|
|
95
|
+
*/
|
|
96
|
+
static multiply(matrix1, matrix2) {
|
|
97
|
+
const result = Matrix2D.empty;
|
|
98
|
+
for (let i = 0; i < 3; i++) {
|
|
99
|
+
for (let j = 0; j < 3; j++) {
|
|
100
|
+
result[i][j] = 0;
|
|
101
|
+
for (let k = 0; k < 3; k++) {
|
|
102
|
+
result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return new Matrix2D(result);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The function multiplies each element of a 2D matrix by a given value and returns the resulting matrix.
|
|
110
|
+
* @param {Matrix2D} matrix - The `matrix` parameter is an instance of the `Matrix2D` class, which represents a 2D
|
|
111
|
+
* matrix. It contains a property `m` that is a 2D array representing the matrix elements.
|
|
112
|
+
* @param {number} value - The `value` parameter is a number that you want to multiply each element of the `matrix` by.
|
|
113
|
+
* @returns a new instance of the Matrix2D class, which is created using the result array.
|
|
114
|
+
*/
|
|
115
|
+
static multiplyByValue(matrix, value) {
|
|
116
|
+
const result = Matrix2D.empty;
|
|
117
|
+
for (let i = 0; i < 3; i++) {
|
|
118
|
+
for (let j = 0; j < 3; j++) {
|
|
119
|
+
result[i][j] = matrix.m[i][j] * value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return new Matrix2D(result);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The function multiplies a 2D matrix by a 2D vector and returns the result as a 2D vector.
|
|
126
|
+
* @param {Matrix2D} matrix - The parameter "matrix" is of type Matrix2D. It represents a 2-dimensional matrix.
|
|
127
|
+
* @param {Vector2D} vector - The "vector" parameter is a 2D vector, represented by an object of type Vector2D.
|
|
128
|
+
* @returns a Vector2D.
|
|
129
|
+
*/
|
|
130
|
+
static multiplyByVector(matrix, vector) {
|
|
131
|
+
const resultMatrix = Matrix2D.multiply(matrix, new Matrix2D(vector));
|
|
132
|
+
return resultMatrix.toVector();
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The function returns a 2D matrix that scales and flips a vector around the center of a given width and height.
|
|
136
|
+
* @param {number} width - The width parameter represents the width of the view or the canvas. It is a number that
|
|
137
|
+
* specifies the width in pixels or any other unit of measurement.
|
|
138
|
+
* @param {number} height - The height parameter represents the height of the view or the canvas. It is used to
|
|
139
|
+
* calculate the centerY value, which is the vertical center of the view.
|
|
140
|
+
* @returns a Matrix2D object.
|
|
141
|
+
*/
|
|
142
|
+
static view(width, height) {
|
|
143
|
+
const scaleStep = 1; // Scale every vector * scaleStep
|
|
144
|
+
const centerX = width / 2;
|
|
145
|
+
const centerY = height / 2;
|
|
146
|
+
const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
|
|
147
|
+
return new Matrix2D([
|
|
148
|
+
[scaleStep, 0, centerX],
|
|
149
|
+
[0, flipX * scaleStep, centerY],
|
|
150
|
+
[0, 0, 1]
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The function scales a matrix by a given factor.
|
|
155
|
+
* @param {number} factor - The factor parameter is a number that represents the scaling factor by which the matrix
|
|
156
|
+
* should be scaled.
|
|
157
|
+
* @returns the result of multiplying a new instance of Matrix2D by the given factor.
|
|
158
|
+
*/
|
|
159
|
+
static scale(factor) {
|
|
160
|
+
return Matrix2D.multiplyByValue(new Matrix2D(), factor);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The function "rotate" takes an angle in radians and returns a 2D transformation matrix for rotating objects.
|
|
164
|
+
* @param {number} radians - The "radians" parameter is the angle in radians by which you want to rotate an object.
|
|
165
|
+
* @returns The code is returning a new instance of a Matrix2D object.
|
|
166
|
+
*/
|
|
167
|
+
static rotate(radians) {
|
|
168
|
+
const cos = Math.cos(radians);
|
|
169
|
+
const sin = Math.sin(radians);
|
|
170
|
+
return new Matrix2D([
|
|
171
|
+
[cos, -sin, 0],
|
|
172
|
+
[sin, cos, 0],
|
|
173
|
+
[0, 0, 1]
|
|
174
|
+
]);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The translate function takes a 2D vector and returns a 2D matrix that represents a translation transformation.
|
|
178
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D. It represents a 2D vector with components x
|
|
179
|
+
* and y, and an optional w component.
|
|
180
|
+
* @returns The method is returning a new instance of the Matrix2D class.
|
|
181
|
+
*/
|
|
182
|
+
static translate(vector) {
|
|
183
|
+
return new Matrix2D([
|
|
184
|
+
[1, 0, vector.x],
|
|
185
|
+
[0, 1, vector.y],
|
|
186
|
+
[0, 0, vector.w]
|
|
187
|
+
]);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The function "toVector" returns a new Vector2D object with the values from the first and second elements of the
|
|
191
|
+
* _matrix array.
|
|
192
|
+
* @returns A new instance of the Vector2D class is being returned. The values of the returned vector are taken from
|
|
193
|
+
* the first column of the matrix.
|
|
194
|
+
*/
|
|
195
|
+
toVector() {
|
|
196
|
+
return new vector2d_1.Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
exports.Matrix2D = Matrix2D;
|
|
@@ -0,0 +1,52 @@
|
|
|
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 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
|
+
protected readonly _matrix: T[][];
|
|
25
|
+
protected readonly _cur: [number, number];
|
|
26
|
+
protected _character: Character;
|
|
27
|
+
protected readonly _VISITED: T;
|
|
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
|
+
}
|