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,315 @@
|
|
|
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 class Vector2D {
|
|
9
|
+
constructor(
|
|
10
|
+
public x: number = 0,
|
|
11
|
+
public y: number = 0,
|
|
12
|
+
public w: number = 1 // needed for matrix multiplication
|
|
13
|
+
) {
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The function checks if the x and y values of a point are both zero.
|
|
18
|
+
* @returns A boolean value indicating whether both the x and y properties of the object are equal to 0.
|
|
19
|
+
*/
|
|
20
|
+
get isZero(): boolean {
|
|
21
|
+
return this.x === 0 && this.y === 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The above function calculates the length of a vector using the Pythagorean theorem.
|
|
26
|
+
* @returns The length of a vector, calculated using the Pythagorean theorem.
|
|
27
|
+
*/
|
|
28
|
+
get length(): number {
|
|
29
|
+
return Math.sqrt(this.x * this.x + this.y * this.y);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The function calculates the square of the length of a vector.
|
|
34
|
+
* @returns The method is returning the sum of the squares of the x and y values.
|
|
35
|
+
*/
|
|
36
|
+
get lengthSq(): number {
|
|
37
|
+
return this.x * this.x + this.y * this.y;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The "rounded" function returns a new Vector2D object with the x and y values rounded to the nearest whole number.
|
|
42
|
+
* @returns The method is returning a new instance of the Vector2D class with the x and y values rounded to the nearest
|
|
43
|
+
* whole number.
|
|
44
|
+
*/
|
|
45
|
+
get rounded(): Vector2D {
|
|
46
|
+
return new Vector2D(Math.round(this.x), Math.round(this.y));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The function "add" takes two Vector2D objects as parameters and returns a new Vector2D object with the sum of their
|
|
51
|
+
* x and y components.
|
|
52
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class. It represents a
|
|
53
|
+
* 2-dimensional vector with an `x` and `y` component.
|
|
54
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D. It represents a 2-dimensional vector with
|
|
55
|
+
* an x and y component.
|
|
56
|
+
* @returns The method is returning a new instance of the Vector2D class with the x and y components of the two input
|
|
57
|
+
* vectors added together.
|
|
58
|
+
*/
|
|
59
|
+
static add(vector1: Vector2D, vector2: Vector2D): Vector2D {
|
|
60
|
+
return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The subtract function takes two Vector2D objects as parameters and returns a new Vector2D object with the x and y
|
|
65
|
+
* components subtracted.
|
|
66
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is an instance of the `Vector2D` class, representing a
|
|
67
|
+
* 2-dimensional vector. It has properties `x` and `y` which represent the x and y components of the vector
|
|
68
|
+
* respectively.
|
|
69
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object. It represents the second vector that you
|
|
70
|
+
* want to subtract from the first vector.
|
|
71
|
+
* @returns The method is returning a new Vector2D object with the x and y components subtracted from vector1 and
|
|
72
|
+
* vector2.
|
|
73
|
+
*/
|
|
74
|
+
static subtract(vector1: Vector2D, vector2: Vector2D): Vector2D {
|
|
75
|
+
return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The function subtracts a given value from the x and y components of a Vector2D object and returns a new Vector2D
|
|
80
|
+
* object.
|
|
81
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
82
|
+
* x and y components.
|
|
83
|
+
* @param {number} value - The "value" parameter is a number that will be subtracted from both the x and y components
|
|
84
|
+
* of the "vector" parameter.
|
|
85
|
+
* @returns A new Vector2D object with the x and y values subtracted by the given value.
|
|
86
|
+
*/
|
|
87
|
+
static subtractValue(vector: Vector2D, value: number): Vector2D {
|
|
88
|
+
return new Vector2D(vector.x - value, vector.y - value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The function multiplies a Vector2D object by a given value.
|
|
93
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
94
|
+
* x and y components.
|
|
95
|
+
* @param {number} value - The "value" parameter is a number that represents the value by which the x and y components
|
|
96
|
+
* of the vector will be multiplied.
|
|
97
|
+
* @returns A new Vector2D object with the x and y values multiplied by the given value.
|
|
98
|
+
*/
|
|
99
|
+
static multiply(vector: Vector2D, value: number): Vector2D {
|
|
100
|
+
return new Vector2D(vector.x * value, vector.y * value);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The function divides the x and y components of a Vector2D by a given value and returns a new Vector2D.
|
|
105
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector with
|
|
106
|
+
* x and y components.
|
|
107
|
+
* @param {number} value - The value parameter is a number that will be used to divide the x and y components of the
|
|
108
|
+
* vector.
|
|
109
|
+
* @returns A new instance of the Vector2D class with the x and y values divided by the given value.
|
|
110
|
+
*/
|
|
111
|
+
static divide(vector: Vector2D, value: number): Vector2D {
|
|
112
|
+
return new Vector2D(vector.x / value, vector.y / value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The function checks if two Vector2D objects are equal by comparing their x and y values.
|
|
117
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
118
|
+
* It has two properties: `x` and `y`, which represent the x and y components of the vector, respectively.
|
|
119
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is of type Vector2D.
|
|
120
|
+
* @returns a boolean value, which indicates whether the two input vectors are equal or not.
|
|
121
|
+
*/
|
|
122
|
+
static equals(vector1: Vector2D, vector2: Vector2D): boolean {
|
|
123
|
+
return vector1.x === vector2.x && vector1.y === vector2.y;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The function checks if two Vector2D objects are equal within a specified rounding factor.
|
|
128
|
+
* @param {Vector2D} vector1 - The first vector to compare.
|
|
129
|
+
* @param {Vector2D} vector2 - The parameter "vector2" is a Vector2D object, which represents a 2-dimensional vector.
|
|
130
|
+
* It is used as one of the inputs for the "equalsRounded" function.
|
|
131
|
+
* @param [roundingFactor=12] - The roundingFactor parameter is used to determine the threshold for considering two
|
|
132
|
+
* vectors as equal. If the absolute difference in the x and y components of the vectors is less than the
|
|
133
|
+
* roundingFactor, the vectors are considered equal.
|
|
134
|
+
* @returns a boolean value.
|
|
135
|
+
*/
|
|
136
|
+
static equalsRounded(vector1: Vector2D, vector2: Vector2D, roundingFactor = 12): boolean {
|
|
137
|
+
const vector = Vector2D.abs(Vector2D.subtract(vector1, vector2));
|
|
138
|
+
if (vector.x < roundingFactor && vector.y < roundingFactor) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The normalize function takes a vector as input and returns a normalized version of the vector.Normalizes the vector if it matches a certain condition
|
|
147
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
148
|
+
* @returns the normalized vector if its length is greater than a very small value (epsilon), otherwise it returns the
|
|
149
|
+
* original vector.
|
|
150
|
+
*/
|
|
151
|
+
static normalize(vector: Vector2D): Vector2D {
|
|
152
|
+
const length = vector.length;
|
|
153
|
+
if (length > 2.220446049250313e-16) {
|
|
154
|
+
// Epsilon
|
|
155
|
+
return Vector2D.divide(vector, length);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return vector;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The function truncates a vector to a maximum length if it exceeds that length.Adjusts x and y so that the length of the vector does not exceed max
|
|
163
|
+
* @param {Vector2D} vector - A 2D vector represented by the Vector2D class.
|
|
164
|
+
* @param {number} max - The `max` parameter is a number that represents the maximum length that the `vector` should
|
|
165
|
+
* have.
|
|
166
|
+
* @returns either the original vector or a truncated version of the vector, depending on whether the length of the
|
|
167
|
+
* vector is greater than the maximum value specified.
|
|
168
|
+
*/
|
|
169
|
+
static truncate(vector: Vector2D, max: number): Vector2D {
|
|
170
|
+
if (vector.length > max) {
|
|
171
|
+
return Vector2D.multiply(Vector2D.normalize(vector), max);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return vector;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The function returns a new Vector2D object that is perpendicular to the input vector.The vector that is perpendicular to this one
|
|
179
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D.
|
|
180
|
+
* @returns A new Vector2D object is being returned.
|
|
181
|
+
*/
|
|
182
|
+
static perp(vector: Vector2D): Vector2D {
|
|
183
|
+
return new Vector2D(-vector.y, vector.x);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The reverse function takes a Vector2D object and returns a new Vector2D object with the negated x and y values.
|
|
188
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
189
|
+
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
190
|
+
* @returns A new Vector2D object with the negated x and y values of the input vector. Returns the vector that is the reverse of this vector
|
|
191
|
+
*/
|
|
192
|
+
static reverse(vector: Vector2D): Vector2D {
|
|
193
|
+
return new Vector2D(-vector.x, -vector.y);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The function takes a Vector2D object as input and returns a new Vector2D object with the absolute values of its x
|
|
198
|
+
* and y components.
|
|
199
|
+
* @param {Vector2D} vector - The parameter "vector" is of type Vector2D, which represents a 2-dimensional vector. It
|
|
200
|
+
* has two properties: "x" and "y", which represent the x and y components of the vector, respectively.
|
|
201
|
+
* @returns The method is returning a new Vector2D object with the absolute values of the x and y components of the
|
|
202
|
+
* input vector.
|
|
203
|
+
*/
|
|
204
|
+
static abs(vector: Vector2D): Vector2D {
|
|
205
|
+
return new Vector2D(Math.abs(vector.x), Math.abs(vector.y));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The dot function calculates the dot product of two 2D vectors.The dot product of v1 and v2
|
|
210
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents a 2D vector with its x and y components.
|
|
211
|
+
* @param {Vector2D} vector2 - The "vector2" parameter is a Vector2D object. It represents a two-dimensional vector
|
|
212
|
+
* with an x and y component.
|
|
213
|
+
* @returns The dot product of the two input vectors.
|
|
214
|
+
*/
|
|
215
|
+
static dot(vector1: Vector2D, vector2: Vector2D): number {
|
|
216
|
+
return vector1.x * vector2.x + vector1.y * vector2.y;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// /**
|
|
220
|
+
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
221
|
+
// * @param vectors The vectors to transform
|
|
222
|
+
// */
|
|
223
|
+
// static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
|
|
224
|
+
// return Matrix2D.multiplyByVector(transformation, vector)
|
|
225
|
+
// }
|
|
226
|
+
|
|
227
|
+
// /**
|
|
228
|
+
// * Transform vectors based on the current tranformation matrices: translation, rotation and scale
|
|
229
|
+
// * @param vectors The vectors to transform
|
|
230
|
+
// */
|
|
231
|
+
// static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
|
|
232
|
+
// return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
|
|
233
|
+
// }
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The function calculates the distance between two points in a two-dimensional space.
|
|
237
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector in 2D space, while `vector2`
|
|
238
|
+
* represents the second vector. Each vector has an `x` and `y` component, which represent their respective coordinates
|
|
239
|
+
* in the 2D space.
|
|
240
|
+
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in the calculation of distance. It
|
|
241
|
+
* is an instance of the `Vector2D` class, which typically has properties `x` and `y` representing the coordinates of
|
|
242
|
+
* the vector in a 2D space.
|
|
243
|
+
* @returns The distance between vector1 and vector2.
|
|
244
|
+
*/
|
|
245
|
+
static distance(vector1: Vector2D, vector2: Vector2D): number {
|
|
246
|
+
const ySeparation = vector2.y - vector1.y;
|
|
247
|
+
const xSeparation = vector2.x - vector1.x;
|
|
248
|
+
return Math.sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The function calculates the squared distance between two 2D vectors.
|
|
253
|
+
* @param {Vector2D} vector1 - The parameter `vector1` represents the first vector, which is an instance of the
|
|
254
|
+
* `Vector2D` class. It contains the x and y coordinates of the vector.
|
|
255
|
+
* @param {Vector2D} vector2 - The `vector2` parameter represents the second vector in a two-dimensional space. It has
|
|
256
|
+
* properties `x` and `y` which represent the coordinates of the vector.
|
|
257
|
+
* @returns the square of the distance between the two input vectors.
|
|
258
|
+
*/
|
|
259
|
+
static distanceSq(vector1: Vector2D, vector2: Vector2D): number {
|
|
260
|
+
const ySeparation = vector2.y - vector1.y;
|
|
261
|
+
const xSeparation = vector2.x - vector1.x;
|
|
262
|
+
return ySeparation * ySeparation + xSeparation * xSeparation;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* The sign function determines the sign of the cross product between two 2D vectors.
|
|
267
|
+
* (assuming the Y axis is pointing down, X axis to right like a Window app)
|
|
268
|
+
* @param {Vector2D} vector1 - The parameter `vector1` is of type `Vector2D`, which represents a 2-dimensional vector.
|
|
269
|
+
* It likely has properties `x` and `y` representing the x and y components of the vector, respectively.
|
|
270
|
+
* @param {Vector2D} vector2 - The above code defines a function called "sign" that takes two parameters: vector1 and
|
|
271
|
+
* vector2. Both vector1 and vector2 are of type Vector2D.
|
|
272
|
+
* @returns either -1 or 1. Returns positive if v2 is clockwise of this vector, negative if counterclockwise
|
|
273
|
+
*/
|
|
274
|
+
static sign(vector1: Vector2D, vector2: Vector2D): number {
|
|
275
|
+
if (vector1.y * vector2.x > vector1.x * vector2.y) {
|
|
276
|
+
return -1;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return 1;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The function calculates the angle between a given vector and the negative y-axis.
|
|
284
|
+
* @param {Vector2D} vector - The "vector" parameter is an instance of the Vector2D class, which represents a
|
|
285
|
+
* 2-dimensional vector. It has two properties: "x" and "y", which represent the x and y components of the vector,
|
|
286
|
+
* respectively.
|
|
287
|
+
* @returns the angle between the given vector and the vector (0, -1) in radians.Returns the angle between origin and the given vector in radians
|
|
288
|
+
*/
|
|
289
|
+
static angle(vector: Vector2D): number {
|
|
290
|
+
const origin = new Vector2D(0, -1);
|
|
291
|
+
const radian = Math.acos(Vector2D.dot(vector, origin) / (vector.length * origin.length));
|
|
292
|
+
return Vector2D.sign(vector, origin) === 1 ? Math.PI * 2 - radian : radian;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* The function "random" generates a random Vector2D object with x and y values within the specified range.
|
|
297
|
+
* @param {number} maxX - The maxX parameter represents the maximum value for the x-coordinate of the random vector.
|
|
298
|
+
* @param {number} maxY - The `maxY` parameter represents the maximum value for the y-coordinate of the generated
|
|
299
|
+
* random vector.
|
|
300
|
+
* @returns a new instance of the Vector2D class with random x and y values.
|
|
301
|
+
*/
|
|
302
|
+
static random(maxX: number, maxY: number): Vector2D {
|
|
303
|
+
const randX = Math.floor(Math.random() * maxX - maxX / 2);
|
|
304
|
+
const randY = Math.floor(Math.random() * maxY - maxY / 2);
|
|
305
|
+
return new Vector2D(randX, randY);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The function sets the values of x and y to zero.
|
|
310
|
+
*/
|
|
311
|
+
zero(): void {
|
|
312
|
+
this.x = 0;
|
|
313
|
+
this.y = 0;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 {PriorityQueue} from './priority-queue';
|
|
9
|
+
import type {Comparator} from '../../types';
|
|
10
|
+
|
|
11
|
+
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
|
+
constructor(
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return b - a;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
) {
|
|
23
|
+
super(options);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 {PriorityQueue} from './priority-queue';
|
|
9
|
+
import type {Comparator} from '../../types';
|
|
10
|
+
|
|
11
|
+
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
|
+
constructor(
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
) {
|
|
23
|
+
super(options);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
|
|
9
|
+
import {Heap} from '../heap';
|
|
10
|
+
import {Comparator} from '../../types';
|
|
11
|
+
|
|
12
|
+
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
|
+
super(options);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
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 {DoublyLinkedList} from '../linked-list';
|
|
9
|
+
|
|
10
|
+
// O(n) time complexity of obtaining the value
|
|
11
|
+
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// O(1) time complexity of obtaining the value
|
|
16
|
+
// O(n) time complexity of adding at the beginning and the end
|
|
17
|
+
// todo tested slowest one
|
|
18
|
+
export class ObjectDeque<E = number> {
|
|
19
|
+
constructor(capacity?: number) {
|
|
20
|
+
if (capacity !== undefined) this._capacity = capacity;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected _nodes: { [key: number]: E } = {};
|
|
24
|
+
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
26
|
+
return this._nodes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
30
|
+
|
|
31
|
+
get capacity(): number {
|
|
32
|
+
return this._capacity;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected _first = -1;
|
|
36
|
+
|
|
37
|
+
get first(): number {
|
|
38
|
+
return this._first;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected _last = -1;
|
|
42
|
+
|
|
43
|
+
get last(): number {
|
|
44
|
+
return this._last;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected _size = 0;
|
|
48
|
+
|
|
49
|
+
get size(): number {
|
|
50
|
+
return this._size;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The "addFirst" function adds a value to the beginning of an array-like data structure.
|
|
55
|
+
* @param {E} value - The `value` parameter represents the value that you want to add to the beginning of the data
|
|
56
|
+
* structure.
|
|
57
|
+
*/
|
|
58
|
+
addFirst(value: E) {
|
|
59
|
+
if (this.size === 0) {
|
|
60
|
+
const mid = Math.floor(this.capacity / 2);
|
|
61
|
+
this._first = mid;
|
|
62
|
+
this._last = mid;
|
|
63
|
+
} else {
|
|
64
|
+
this._first--;
|
|
65
|
+
}
|
|
66
|
+
this.nodes[this.first] = value;
|
|
67
|
+
this._size++;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The addLast function adds a value to the end of an array-like data structure.
|
|
72
|
+
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
73
|
+
*/
|
|
74
|
+
addLast(value: E) {
|
|
75
|
+
if (this.size === 0) {
|
|
76
|
+
const mid = Math.floor(this.capacity / 2);
|
|
77
|
+
this._first = mid;
|
|
78
|
+
this._last = mid;
|
|
79
|
+
} else {
|
|
80
|
+
this._last++;
|
|
81
|
+
}
|
|
82
|
+
this.nodes[this.last] = value;
|
|
83
|
+
this._size++;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
88
|
+
* @returns The value of the first element in the data structure.
|
|
89
|
+
*/
|
|
90
|
+
popFirst() {
|
|
91
|
+
if (!this.size) return;
|
|
92
|
+
const value = this.getFirst();
|
|
93
|
+
delete this.nodes[this.first];
|
|
94
|
+
this._first++;
|
|
95
|
+
this._size--;
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
101
|
+
* @returns The element at the first position of the `_nodes` array.
|
|
102
|
+
*/
|
|
103
|
+
getFirst() {
|
|
104
|
+
if (this.size) return this.nodes[this.first];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
109
|
+
* @returns The value that was removed from the data structure.
|
|
110
|
+
*/
|
|
111
|
+
popLast() {
|
|
112
|
+
if (!this.size) return;
|
|
113
|
+
const value = this.getLast();
|
|
114
|
+
delete this.nodes[this.last];
|
|
115
|
+
this._last--;
|
|
116
|
+
this._size--;
|
|
117
|
+
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
123
|
+
* @returns The last element in the array "_nodes" is being returned.
|
|
124
|
+
*/
|
|
125
|
+
getLast() {
|
|
126
|
+
if (this.size) return this.nodes[this.last];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The get function returns the element at the specified index in an array-like data structure.
|
|
131
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
132
|
+
* retrieve from the array.
|
|
133
|
+
* @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
|
|
134
|
+
* index, `null` is returned.
|
|
135
|
+
*/
|
|
136
|
+
get(index: number) {
|
|
137
|
+
return this.nodes[this.first + index] || null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The function checks if the size of a data structure is less than or equal to zero.
|
|
142
|
+
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
143
|
+
*/
|
|
144
|
+
isEmpty() {
|
|
145
|
+
return this.size <= 0;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// O(1) time complexity of obtaining the value
|
|
150
|
+
// O(n) time complexity of adding at the beginning and the end
|
|
151
|
+
export class ArrayDeque<E> {
|
|
152
|
+
protected _nodes: E[] = [];
|
|
153
|
+
|
|
154
|
+
get nodes(): E[] {
|
|
155
|
+
return this._nodes;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get size() {
|
|
159
|
+
return this.nodes.length;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* O(n) time complexity of adding at the beginning and the end
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The function "addLast" adds a value to the end of an array.
|
|
168
|
+
* @param {E} value - The value parameter represents the value that you want to add to the end of the array.
|
|
169
|
+
* @returns The return value is the new length of the array after the value has been added.
|
|
170
|
+
*/
|
|
171
|
+
addLast(value: E) {
|
|
172
|
+
return this.nodes.push(value);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
177
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
178
|
+
*/
|
|
179
|
+
popLast(): E | null {
|
|
180
|
+
return this.nodes.pop() ?? null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
185
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
186
|
+
* empty.
|
|
187
|
+
*/
|
|
188
|
+
popFirst(): E | null {
|
|
189
|
+
return this.nodes.shift() ?? null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* O(n) time complexity of adding at the beginning and the end
|
|
194
|
+
*/
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The function "addFirst" adds a value to the beginning of an array.
|
|
198
|
+
* @param {E} value - The value parameter represents the value that you want to add to the beginning of the array.
|
|
199
|
+
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
|
|
200
|
+
* `value` at the beginning.
|
|
201
|
+
*/
|
|
202
|
+
addFirst(value: E) {
|
|
203
|
+
return this.nodes.unshift(value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
208
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
209
|
+
* empty, it will return `null`.
|
|
210
|
+
*/
|
|
211
|
+
getFirst(): E | null {
|
|
212
|
+
return this.nodes[0] ?? null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
217
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
218
|
+
*/
|
|
219
|
+
getLast(): E | null {
|
|
220
|
+
return this.nodes[this.nodes.length - 1] ?? null;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* O(1) time complexity of obtaining the value
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The get function returns the element at the specified index in an array, or null if the index is out of bounds.
|
|
229
|
+
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
230
|
+
* retrieve from the array.
|
|
231
|
+
* @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
|
|
232
|
+
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
233
|
+
*/
|
|
234
|
+
get(index: number): E | null {
|
|
235
|
+
return this.nodes[index] ?? null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The set function assigns a value to a specific index in an array.
|
|
240
|
+
* @param {number} index - The index parameter is a number that represents the position of the element in the array
|
|
241
|
+
* that you want to set a new value for.
|
|
242
|
+
* @param {E} value - The value parameter represents the new value that you want to set at the specified index in the
|
|
243
|
+
* _nodes array.
|
|
244
|
+
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
245
|
+
*/
|
|
246
|
+
set(index: number, value: E) {
|
|
247
|
+
return (this.nodes[index] = value);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The insert function adds a value at a specified index in an array.
|
|
252
|
+
* @param {number} index - The index parameter specifies the position at which the value should be inserted in the
|
|
253
|
+
* array. It is a number that represents the index of the array where the value should be inserted. The index starts
|
|
254
|
+
* from 0, so the first element of the array has an index of 0, the second element has
|
|
255
|
+
* @param {E} value - The value parameter represents the value that you want to insert into the array at the specified
|
|
256
|
+
* index.
|
|
257
|
+
* @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
|
|
258
|
+
* are being removed, an empty array will be returned.
|
|
259
|
+
*/
|
|
260
|
+
insert(index: number, value: E) {
|
|
261
|
+
return this.nodes.splice(index, 0, value);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The delete function removes an element from an array at a specified index.
|
|
266
|
+
* @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
|
|
267
|
+
* is a number that represents the index of the element to be removed.
|
|
268
|
+
* @returns The method is returning an array containing the removed element.
|
|
269
|
+
*/
|
|
270
|
+
delete(index: number) {
|
|
271
|
+
return this.nodes.splice(index, 1);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The function checks if an array called "_nodes" is empty.
|
|
276
|
+
* @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
|
|
277
|
+
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
278
|
+
*/
|
|
279
|
+
isEmpty() {
|
|
280
|
+
return this.nodes.length === 0;
|
|
281
|
+
}
|
|
282
|
+
}
|