data-structure-typed 0.9.16 → 1.3.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.
Files changed (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +96 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1 +0,0 @@
1
- export {}
@@ -1,4 +0,0 @@
1
- export * from './matrix';
2
- export * from './vector2d';
3
- export * from './matrix2d';
4
- export * from './navigator';
@@ -1,13 +0,0 @@
1
- // todo need to be improved
2
- export class MatrixNTI2D<T = number> {
3
- private readonly _matrix: Array<Array<T>>;
4
-
5
- constructor(options: { row: number, col: number, initialVal?: T }) {
6
- const {row, col, initialVal} = options;
7
- this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
8
- }
9
-
10
- toArray(): Array<Array<T>> {
11
- return this._matrix;
12
- }
13
- }
@@ -1,125 +0,0 @@
1
- import Vector2D from './vector2d'
2
-
3
- export class Matrix2D {
4
- private readonly _matrix: number[][];
5
-
6
- constructor(value?: number[][] | Vector2D) {
7
- if (typeof value === 'undefined') {
8
- this._matrix = Matrix2D.identity
9
- } else if (value instanceof Vector2D) {
10
- this._matrix = Matrix2D.identity
11
- this._matrix[0][0] = value.x
12
- this._matrix[1][0] = value.y
13
- this._matrix[2][0] = value.w
14
- } else {
15
- this._matrix = value
16
- }
17
- }
18
-
19
- public static get empty(): number[][] {
20
- return [[], [], []]
21
- }
22
-
23
- /**
24
- * Initialize an identity matrix
25
- */
26
- public static get identity(): number[][] {
27
- return [
28
- [1, 0, 0],
29
- [0, 1, 0],
30
- [0, 0, 1]]
31
- }
32
-
33
- /**
34
- * Return the matrix values
35
- */
36
- public get m(): number[][] {
37
- return this._matrix
38
- }
39
-
40
- public get toVector(): Vector2D {
41
- return new Vector2D(this._matrix[0][0], this._matrix[1][0])
42
- }
43
-
44
- public static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
45
- const result = Matrix2D.empty
46
- for (let i = 0; i < 3; i++) {
47
- for (let j = 0; j < 3; j++) {
48
- result[i][j] = matrix1.m[i][j] + matrix2.m[i][j]
49
- }
50
- }
51
- return new Matrix2D(result);
52
- }
53
-
54
- public static subtract(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
55
- const result = Matrix2D.empty
56
- for (let i = 0; i < 3; i++) {
57
- for (let j = 0; j < 3; j++) {
58
- result[i][j] = matrix1.m[i][j] - matrix2.m[i][j]
59
- }
60
- }
61
- return new Matrix2D(result);
62
- }
63
-
64
- public static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
65
- const result = Matrix2D.empty
66
- for (let i = 0; i < 3; i++) {
67
- for (let j = 0; j < 3; j++) {
68
- result[i][j] = 0;
69
- for (let k = 0; k < 3; k++) {
70
- result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
71
- }
72
- }
73
- }
74
- return new Matrix2D(result);
75
- }
76
-
77
- public static multiplyByValue(matrix: Matrix2D, value: number): Matrix2D {
78
- const result = Matrix2D.empty
79
- for (let i = 0; i < 3; i++) {
80
- for (let j = 0; j < 3; j++) {
81
- result[i][j] = matrix.m[i][j] * value
82
- }
83
- }
84
- return new Matrix2D(result);
85
- }
86
-
87
- public static multiplyByVector(matrix: Matrix2D, vector: Vector2D): Vector2D {
88
- return Matrix2D.multiply(matrix, new Matrix2D(vector)).toVector
89
- }
90
-
91
- public static view(width: number, height: number): Matrix2D {
92
- const scaleStep = 1 // Scale every vector * scaleStep
93
- const centerX = width / 2
94
- const centerY = height / 2
95
- const flipX = Math.cos(Math.PI) // rotate 180deg / 3.14radian around X-axis
96
-
97
- return new Matrix2D([
98
- [scaleStep, 0, centerX],
99
- [0, flipX * scaleStep, centerY],
100
- [0, 0, 1]])
101
- }
102
-
103
- public static scale(factor: number) {
104
- return Matrix2D.multiplyByValue(new Matrix2D(), factor)
105
- }
106
-
107
- public static rotate(radians: number) {
108
- const cos = Math.cos(radians)
109
- const sin = Math.sin(radians)
110
-
111
- return new Matrix2D([
112
- [cos, -sin, 0],
113
- [sin, cos, 0],
114
- [0, 0, 1]])
115
- }
116
-
117
- public static translate(vector: Vector2D): Matrix2D {
118
- return new Matrix2D([
119
- [1, 0, vector.x],
120
- [0, 1, vector.y],
121
- [0, 0, vector.w]])
122
- }
123
- }
124
-
125
- export default Matrix2D
@@ -1,87 +0,0 @@
1
- import type {Direction, NavigatorParams, Turning} from '../types';
2
-
3
- export class Character {
4
- direction: Direction;
5
- turn: () => Character;
6
-
7
- constructor(direction: Direction, turning: Turning) {
8
- this.direction = direction;
9
- this.turn = () => new Character(turning[direction], turning);
10
- }
11
- }
12
-
13
- export class Navigator<T = number> {
14
- onMove: (cur: [number, number]) => void;
15
- private readonly _matrix: T[][];
16
- private readonly _cur: [number, number];
17
- private _character: Character;
18
- private readonly _VISITED: T;
19
-
20
- constructor({matrix, turning, onMove, init: {cur, charDir, VISITED}}: NavigatorParams<T>) {
21
- this._matrix = matrix;
22
- this._cur = cur;
23
- this._character = new Character(charDir, turning);
24
- this.onMove = onMove;
25
- this.onMove && this.onMove(this._cur);
26
- this._VISITED = VISITED;
27
- this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
28
- }
29
-
30
- start() {
31
- while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
32
- const {direction} = this._character;
33
- if (this.check(direction)) {
34
- this.move(direction);
35
- } else if (this.check(this._character.turn().direction)) {
36
- this._character = this._character.turn();
37
- }
38
- }
39
- }
40
-
41
- check(direction: Direction) {
42
- let forward: T | undefined, row: T[] | undefined;
43
- const matrix = this._matrix;
44
- const [i, j] = this._cur;
45
- switch (direction) {
46
- case 'up':
47
- row = matrix[i - 1];
48
- if (!row) return false;
49
- forward = row[j];
50
- break;
51
- case 'right':
52
- forward = matrix[i][j + 1];
53
- break;
54
- case 'down':
55
- row = matrix[i + 1];
56
- if (!row) return false;
57
- forward = row[j];
58
- break;
59
- case 'left':
60
- forward = matrix[i][j - 1];
61
- break;
62
- }
63
- return forward !== undefined && forward !== this._VISITED;
64
- }
65
-
66
- move(direction: Direction) {
67
- switch (direction) {
68
- case 'up':
69
- this._cur[0]--;
70
- break;
71
- case 'right':
72
- this._cur[1]++;
73
- break;
74
- case 'down':
75
- this._cur[0]++;
76
- break;
77
- case 'left':
78
- this._cur[1]--;
79
- break;
80
- }
81
-
82
- const [i, j] = this._cur;
83
- this._matrix[i][j] = this._VISITED;
84
- this.onMove && this.onMove(this._cur);
85
- }
86
- }
87
-
@@ -1,189 +0,0 @@
1
- export class Vector2D {
2
- constructor(
3
- public x: number = 0,
4
- public y: number = 0,
5
- public w: number = 1 // needed for matrix multiplication
6
- ) {
7
- }
8
-
9
- /**
10
- * Set x and y both to zero
11
- */
12
- public get isZero(): boolean {
13
- return this.x === 0 && this.y === 0
14
- }
15
-
16
- /**
17
- * The length / magnitude of the vector
18
- */
19
- public get length(): number {
20
- return Math.sqrt((this.x * this.x) + (this.y * this.y))
21
- }
22
-
23
- /**
24
- * The squared length of the vector
25
- */
26
- public get lengthSq(): number {
27
- return (this.x * this.x) + (this.y * this.y)
28
- }
29
-
30
- /**
31
- * Return the vector with rounded values
32
- */
33
- public get rounded(): Vector2D {
34
- return new Vector2D(Math.round(this.x), Math.round(this.y))
35
- }
36
-
37
- public static add(vector1: Vector2D, vector2: Vector2D): Vector2D {
38
- return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y)
39
- }
40
-
41
- public static subtract(vector1: Vector2D, vector2: Vector2D): Vector2D {
42
- return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y)
43
- }
44
-
45
- public static subtractValue(vector: Vector2D, value: number): Vector2D {
46
- return new Vector2D(vector.x - value, vector.y - value)
47
- }
48
-
49
- public static multiply(vector: Vector2D, value: number): Vector2D {
50
- return new Vector2D(vector.x * value, vector.y * value)
51
- }
52
-
53
- public static divide(vector: Vector2D, value: number): Vector2D {
54
- return new Vector2D(vector.x / value, vector.y / value)
55
- }
56
-
57
- public static equals(vector1: Vector2D, vector2: Vector2D): boolean {
58
- return vector1.x === vector2.x && vector1.y === vector2.y
59
- }
60
-
61
- public static equalsRounded(vector1: Vector2D, vector2: Vector2D, roundingFactor = 12): boolean {
62
- const vector = Vector2D.abs(Vector2D.subtract(vector1, vector2))
63
- if (vector.x < roundingFactor && vector.y < roundingFactor) {
64
- return true
65
- }
66
-
67
- return false
68
- }
69
-
70
- /**
71
- * Normalizes the vector if it matches a certain condition
72
- */
73
- public static normalize(vector: Vector2D): Vector2D {
74
- const length = vector.length
75
- if (length > 2.220446049250313e-16) { // Epsilon
76
- return Vector2D.divide(vector, length)
77
- }
78
-
79
- return vector
80
- }
81
-
82
- /**
83
- * Adjusts x and y so that the length of the vector does not exceed max
84
- */
85
- public static truncate(vector: Vector2D, max: number): Vector2D {
86
- if (vector.length > max) {
87
- return Vector2D.multiply(Vector2D.normalize(vector), max)
88
- }
89
-
90
- return vector
91
- }
92
-
93
- /**
94
- * The vector that is perpendicular to this one
95
- */
96
- public static perp(vector: Vector2D): Vector2D {
97
- return new Vector2D(-vector.y, vector.x)
98
- }
99
-
100
- /**
101
- * returns the vector that is the reverse of this vector
102
- */
103
- public static reverse(vector: Vector2D): Vector2D {
104
- return new Vector2D(-vector.x, -vector.y)
105
- }
106
-
107
- public static abs(vector: Vector2D): Vector2D {
108
- return new Vector2D(Math.abs(vector.x), Math.abs(vector.y))
109
- }
110
-
111
- /**
112
- * The dot product of v1 and v2
113
- */
114
- public static dot(vector1: Vector2D, vector2: Vector2D): number {
115
- return (vector1.x * vector2.x) + (vector1.y * vector2.y)
116
- }
117
-
118
- // /**
119
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
120
- // * @param vectors The vectors to transform
121
- // */
122
- // public static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
123
- // return Matrix2D.multiplyByVector(transformation, vector)
124
- // }
125
-
126
- // /**
127
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
128
- // * @param vectors The vectors to transform
129
- // */
130
- // public static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
131
- // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
132
- // }
133
-
134
- /**
135
- * The distance between this and the vector
136
- */
137
- public static distance(vector1: Vector2D, vector2: Vector2D): number {
138
- const ySeparation = vector2.y - vector1.y
139
- const xSeparation = vector2.x - vector1.x
140
- return Math.sqrt((ySeparation * ySeparation) + (xSeparation * xSeparation))
141
- }
142
-
143
- /**
144
- * The distance between this and the vector squared
145
- */
146
- public static distanceSq(vector1: Vector2D, vector2: Vector2D): number {
147
- const ySeparation = vector2.y - vector1.y
148
- const xSeparation = vector2.x - vector1.x
149
- return (ySeparation * ySeparation) + (xSeparation * xSeparation)
150
- }
151
-
152
- /**
153
- * Returns positive if v2 is clockwise of this vector, negative if counterclockwise
154
- * (assuming the Y axis is pointing down, X axis to right like a Window app)
155
- */
156
- public static sign(vector1: Vector2D, vector2: Vector2D): number {
157
- if (vector1.y * vector2.x > vector1.x * vector2.y) {
158
- return -1
159
- }
160
-
161
- return 1
162
- }
163
-
164
- /**
165
- * Returns the angle between origin and the given vector in radians
166
- * @param vector
167
- */
168
- public static angle(vector: Vector2D): number {
169
- const origin = new Vector2D(0, -1)
170
- const radian = Math.acos(Vector2D.dot(vector, origin) / (vector.length * origin.length))
171
- return Vector2D.sign(vector, origin) === 1 ? ((Math.PI * 2) - radian) : radian
172
- }
173
-
174
- public static random(maxX: number, maxY: number): Vector2D {
175
- const randX = Math.floor(Math.random() * maxX - (maxX / 2))
176
- const randY = Math.floor(Math.random() * maxY - (maxY / 2))
177
- return new Vector2D(randX, randY)
178
- }
179
-
180
- /**
181
- * Check wether both x and y are zero
182
- */
183
- public zero(): void {
184
- this.x = 0
185
- this.y = 0
186
- }
187
- }
188
-
189
- export default Vector2D
@@ -1,3 +0,0 @@
1
- export * from './priority-queue';
2
- export * from './min-priority-queue';
3
- export * from './max-priority-queue';
@@ -1,13 +0,0 @@
1
- import {PriorityQueue} from './priority-queue';
2
- import type {PriorityQueueOptions} from '../types';
3
-
4
- export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
5
- constructor(options?: PriorityQueueOptions<T>) {
6
- super({
7
- nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
8
- const aKey = a as unknown as number, bKey = b as unknown as number;
9
- return bKey - aKey;
10
- }
11
- });
12
- }
13
- }
@@ -1,13 +0,0 @@
1
- import {PriorityQueue} from './priority-queue';
2
- import type {PriorityQueueOptions} from '../types';
3
-
4
- export class MinPriorityQueue<T = number> extends PriorityQueue<T> {
5
- constructor(options?: PriorityQueueOptions<T>) {
6
- super({
7
- nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
8
- const aKey = a as unknown as number, bKey = b as unknown as number;
9
- return aKey - bKey;
10
- }
11
- });
12
- }
13
- }
@@ -1,200 +0,0 @@
1
- import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
2
-
3
- export class PriorityQueue<T = number> {
4
- protected nodes: T[] = [];
5
-
6
- constructor(options: PriorityQueueOptions<T>) {
7
- const {nodes, comparator, isFix = true} = options;
8
- this._comparator = comparator;
9
-
10
- if (nodes && nodes instanceof Array && nodes.length > 0) {
11
- // TODO support distinct
12
- this.nodes = Array.isArray(nodes) ? [...nodes] : [];
13
- isFix && this._fix();
14
- }
15
- }
16
-
17
- get size(): number {
18
- return this.nodes.length;
19
- }
20
-
21
- static heapify<T>(options: PriorityQueueOptions<T>) {
22
- const heap = new PriorityQueue(options);
23
- heap._fix();
24
- return heap;
25
- }
26
-
27
- static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
28
- return new PriorityQueue({...options, isFix: true}).isValid();
29
- }
30
-
31
- offer(node: T) {
32
- this.nodes.push(node);
33
- this._heapifyUp(this.size - 1);
34
- }
35
-
36
- peek(): T | null {
37
- return this.size ? this.nodes[0] : null;
38
- }
39
-
40
- poll(): T | null {
41
- let res: T | null = null;
42
- if (this.size > 1) {
43
- this._swap(0, this.nodes.length - 1);
44
- res = this.nodes.pop() ?? null;
45
- this._heapifyDown(0);
46
- } else if (this.size === 1) {
47
- res = this.nodes.pop() ?? null;
48
- }
49
- return res;
50
- }
51
-
52
- leaf(): T | null {
53
- return this.nodes[this.size - 1] ?? null;
54
- }
55
-
56
- isEmpty() {
57
- return this.size === 0;
58
- }
59
-
60
- clear() {
61
- this.nodes = [];
62
- }
63
-
64
- toArray(): T[] {
65
- return [...this.nodes];
66
- }
67
-
68
- clone(): PriorityQueue<T> {
69
- return new PriorityQueue<T>({nodes: this.nodes, comparator: this._comparator});
70
- }
71
-
72
- // --- start additional methods ---
73
- isValid(): boolean {
74
- const isValidRecursive = (parentIndex: number): boolean => {
75
- let isValidLeft = true;
76
- let isValidRight = true;
77
-
78
- if (this._getLeft(parentIndex) !== -1) {
79
- const leftChildIndex = (parentIndex * 2) + 1;
80
- if (!this._compare(parentIndex, leftChildIndex)) return false;
81
- isValidLeft = isValidRecursive(leftChildIndex);
82
- }
83
-
84
- if (this._getRight(parentIndex) !== -1) {
85
- const rightChildIndex = (parentIndex * 2) + 2;
86
- if (!this._compare(parentIndex, rightChildIndex)) return false;
87
- isValidRight = isValidRecursive(rightChildIndex);
88
- }
89
-
90
- return isValidLeft && isValidRight;
91
- };
92
-
93
- return isValidRecursive(0);
94
- }
95
-
96
- sort(): T[] {
97
- const visitedNode: T[] = [];
98
- while (this.size !== 0) {
99
- const top = this.poll();
100
- if (top) visitedNode.push(top);
101
- }
102
- return visitedNode;
103
- }
104
-
105
- DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[] {
106
- const visitedNode: (T | null)[] = [];
107
-
108
- const traverse = (cur: number) => {
109
- const leftChildIndex = this._getLeft(cur);
110
- const rightChildIndex = this._getRight(cur);
111
- switch (dfsMode) {
112
- case 'in':
113
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
114
- visitedNode.push(this.nodes[cur] ?? null);
115
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
116
- break;
117
- case 'pre':
118
- visitedNode.push(this.nodes[cur] ?? null);
119
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
120
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
121
- break;
122
- case 'post':
123
- this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
124
- this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
125
- visitedNode.push(this.nodes[cur] ?? null);
126
- break;
127
- }
128
- };
129
-
130
- this._isValidIndex(0) && traverse(0);
131
- return visitedNode;
132
- }
133
-
134
- protected readonly _comparator: PriorityQueueComparator<T> = (a: T, b: T) => {
135
- const aKey = a as unknown as number, bKey = b as unknown as number;
136
- return aKey - bKey;
137
- };
138
-
139
- protected _compare(a: number, b: number) {
140
- return this._comparator(this.nodes[a], this.nodes[b]) > 0;
141
- }
142
-
143
- protected _swap(a: number, b: number) {
144
- const temp = this.nodes[a];
145
- this.nodes[a] = this.nodes[b];
146
- this.nodes[b] = temp;
147
- }
148
-
149
- protected _isValidIndex(index: number): boolean {
150
- return index > -1 && index < this.nodes.length;
151
- }
152
-
153
- protected _getParent(child: number): number {
154
- return Math.floor((child - 1) / 2);
155
- }
156
-
157
- protected _getLeft(parent: number): number {
158
- return (2 * parent) + 1;
159
- }
160
-
161
- protected _getRight(parent: number): number {
162
- return (2 * parent) + 2;
163
- }
164
-
165
- protected _getComparedChild(parent: number) {
166
- let min = parent;
167
- const left = this._getLeft(parent), right = this._getRight(parent);
168
-
169
- if (left < this.size && this._compare(min, left)) {
170
- min = left;
171
- }
172
- if (right < this.size && this._compare(min, right)) {
173
- min = right;
174
- }
175
- return min;
176
- }
177
-
178
- protected _heapifyUp(start: number) {
179
- while (start > 0 && this._compare(this._getParent(start), start)) {
180
- const parent = this._getParent(start);
181
- this._swap(start, parent);
182
- start = parent;
183
- }
184
- }
185
-
186
- protected _heapifyDown(start: number) {
187
- let min = this._getComparedChild(start);
188
- while (this._compare(start, min)) {
189
- this._swap(min, start);
190
- start = min;
191
- min = this._getComparedChild(start);
192
- }
193
- }
194
-
195
- protected _fix() {
196
- for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
197
- }
198
-
199
- // --- end additional methods ---
200
- }