data-structure-typed 0.8.6

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 (166) hide show
  1. package/.idea/data-structure-typed.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/vcs.xml +6 -0
  4. package/README.md +2 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +6 -0
  6. package/dist/data-structures/binary-tree/avl-tree.js +231 -0
  7. package/dist/data-structures/binary-tree/b-tree.js +6 -0
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
  9. package/dist/data-structures/binary-tree/binary-tree.js +992 -0
  10. package/dist/data-structures/binary-tree/bst.js +431 -0
  11. package/dist/data-structures/binary-tree/index.js +20 -0
  12. package/dist/data-structures/binary-tree/rb-tree.js +6 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +151 -0
  14. package/dist/data-structures/binary-tree/splay-tree.js +6 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
  16. package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
  17. package/dist/data-structures/graph/abstract-graph.js +648 -0
  18. package/dist/data-structures/graph/directed-graph.js +268 -0
  19. package/dist/data-structures/graph/index.js +19 -0
  20. package/dist/data-structures/graph/undirected-graph.js +142 -0
  21. package/dist/data-structures/hash/coordinate-map.js +24 -0
  22. package/dist/data-structures/hash/coordinate-set.js +21 -0
  23. package/dist/data-structures/hash/hash-table.js +2 -0
  24. package/dist/data-structures/hash/index.js +17 -0
  25. package/dist/data-structures/hash/pair.js +2 -0
  26. package/dist/data-structures/hash/tree-map.js +2 -0
  27. package/dist/data-structures/hash/tree-set.js +2 -0
  28. package/dist/data-structures/heap/heap.js +114 -0
  29. package/dist/data-structures/heap/index.js +19 -0
  30. package/dist/data-structures/heap/max-heap.js +22 -0
  31. package/dist/data-structures/heap/min-heap.js +22 -0
  32. package/dist/data-structures/index.js +25 -0
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
  34. package/dist/data-structures/linked-list/index.js +18 -0
  35. package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
  36. package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
  37. package/dist/data-structures/matrix/index.js +19 -0
  38. package/dist/data-structures/matrix/matrix.js +14 -0
  39. package/dist/data-structures/matrix/matrix2d.js +119 -0
  40. package/dist/data-structures/matrix/navigator.js +78 -0
  41. package/dist/data-structures/matrix/vector2d.js +161 -0
  42. package/dist/data-structures/priority-queue/index.js +19 -0
  43. package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
  44. package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
  45. package/dist/data-structures/priority-queue/priority-queue.js +174 -0
  46. package/dist/data-structures/queue/deque.js +132 -0
  47. package/dist/data-structures/queue/index.js +17 -0
  48. package/dist/data-structures/queue/queue.js +113 -0
  49. package/dist/data-structures/stack/index.js +17 -0
  50. package/dist/data-structures/stack/stack.js +97 -0
  51. package/dist/data-structures/trampoline.js +52 -0
  52. package/dist/data-structures/trie/index.js +17 -0
  53. package/dist/data-structures/trie/trie.js +141 -0
  54. package/dist/index.js +17 -0
  55. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
  56. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
  57. package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
  58. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  59. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
  60. package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
  61. package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
  62. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
  63. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
  64. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
  65. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
  66. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  67. package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
  68. package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
  69. package/dist/types/data-structures/graph/index.d.ts +3 -0
  70. package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
  71. package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
  72. package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
  73. package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
  74. package/dist/types/data-structures/hash/index.d.ts +1 -0
  75. package/dist/types/data-structures/hash/pair.d.ts +1 -0
  76. package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
  77. package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
  78. package/dist/types/data-structures/heap/heap.d.ts +72 -0
  79. package/dist/types/data-structures/heap/index.d.ts +3 -0
  80. package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
  81. package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
  82. package/dist/types/data-structures/index.d.ts +9 -0
  83. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
  84. package/dist/types/data-structures/linked-list/index.d.ts +2 -0
  85. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
  86. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
  87. package/dist/types/data-structures/matrix/index.d.ts +3 -0
  88. package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
  89. package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
  90. package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
  91. package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
  92. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  93. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
  94. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
  95. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
  96. package/dist/types/data-structures/queue/deque.d.ts +37 -0
  97. package/dist/types/data-structures/queue/index.d.ts +1 -0
  98. package/dist/types/data-structures/queue/queue.d.ts +76 -0
  99. package/dist/types/data-structures/stack/index.d.ts +1 -0
  100. package/dist/types/data-structures/stack/stack.d.ts +69 -0
  101. package/dist/types/data-structures/trampoline.d.ts +25 -0
  102. package/dist/types/data-structures/trie/index.d.ts +1 -0
  103. package/dist/types/data-structures/trie/trie.d.ts +28 -0
  104. package/dist/types/index.d.ts +1 -0
  105. package/dist/types/index.js +17 -0
  106. package/dist/types/types/index.d.ts +1 -0
  107. package/dist/types/types/utils.d.ts +46 -0
  108. package/dist/types/utils.d.ts +122 -0
  109. package/dist/types/utils.js +53 -0
  110. package/dist/utils.js +569 -0
  111. package/package.json +75 -0
  112. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  113. package/src/data-structures/binary-tree/avl-tree.ts +232 -0
  114. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  115. package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
  117. package/src/data-structures/binary-tree/bst.ts +404 -0
  118. package/src/data-structures/binary-tree/index.ts +4 -0
  119. package/src/data-structures/binary-tree/rb-tree.ts +3 -0
  120. package/src/data-structures/binary-tree/segment-tree.ts +164 -0
  121. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  122. package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
  123. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  124. package/src/data-structures/graph/abstract-graph.ts +789 -0
  125. package/src/data-structures/graph/directed-graph.ts +322 -0
  126. package/src/data-structures/graph/index.ts +3 -0
  127. package/src/data-structures/graph/undirected-graph.ts +154 -0
  128. package/src/data-structures/hash/coordinate-map.ts +24 -0
  129. package/src/data-structures/hash/coordinate-set.ts +20 -0
  130. package/src/data-structures/hash/hash-table.ts +1 -0
  131. package/src/data-structures/hash/index.ts +1 -0
  132. package/src/data-structures/hash/pair.ts +1 -0
  133. package/src/data-structures/hash/tree-map.ts +1 -0
  134. package/src/data-structures/hash/tree-set.ts +1 -0
  135. package/src/data-structures/heap/heap.ts +136 -0
  136. package/src/data-structures/heap/index.ts +3 -0
  137. package/src/data-structures/heap/max-heap.ts +22 -0
  138. package/src/data-structures/heap/min-heap.ts +24 -0
  139. package/src/data-structures/index.ts +10 -0
  140. package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
  141. package/src/data-structures/linked-list/index.ts +2 -0
  142. package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
  143. package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
  144. package/src/data-structures/matrix/index.ts +3 -0
  145. package/src/data-structures/matrix/matrix.ts +13 -0
  146. package/src/data-structures/matrix/matrix2d.ts +125 -0
  147. package/src/data-structures/matrix/navigator.ts +99 -0
  148. package/src/data-structures/matrix/vector2d.ts +189 -0
  149. package/src/data-structures/priority-queue/index.ts +3 -0
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +208 -0
  153. package/src/data-structures/queue/deque.ts +139 -0
  154. package/src/data-structures/queue/index.ts +1 -0
  155. package/src/data-structures/queue/queue.ts +123 -0
  156. package/src/data-structures/stack/index.ts +1 -0
  157. package/src/data-structures/stack/stack.ts +104 -0
  158. package/src/data-structures/trampoline.ts +91 -0
  159. package/src/data-structures/trie/index.ts +1 -0
  160. package/src/data-structures/trie/trie.ts +153 -0
  161. package/src/index.ts +1 -0
  162. package/src/types/index.ts +1 -0
  163. package/src/types/patches/index.d.ts +0 -0
  164. package/src/types/utils.ts +158 -0
  165. package/src/utils.ts +605 -0
  166. package/tsconfig.json +52 -0
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Matrix2D = void 0;
7
+ const vector2d_1 = __importDefault(require("./vector2d"));
8
+ class Matrix2D {
9
+ constructor(value) {
10
+ if (typeof value === 'undefined') {
11
+ this._matrix = Matrix2D.identity;
12
+ }
13
+ else if (value instanceof vector2d_1.default) {
14
+ this._matrix = Matrix2D.identity;
15
+ this._matrix[0][0] = value.x;
16
+ this._matrix[1][0] = value.y;
17
+ this._matrix[2][0] = value.w;
18
+ }
19
+ else {
20
+ this._matrix = value;
21
+ }
22
+ }
23
+ /**
24
+ * Return the matrix values
25
+ */
26
+ get m() {
27
+ return this._matrix;
28
+ }
29
+ static get empty() {
30
+ return [[], [], []];
31
+ }
32
+ get toVector() {
33
+ return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
34
+ }
35
+ /**
36
+ * Initialize an identity matrix
37
+ */
38
+ static get identity() {
39
+ return [
40
+ [1, 0, 0],
41
+ [0, 1, 0],
42
+ [0, 0, 1]
43
+ ];
44
+ }
45
+ static add(matrix1, matrix2) {
46
+ const result = Matrix2D.empty;
47
+ for (let i = 0; i < 3; i++) {
48
+ for (let j = 0; j < 3; j++) {
49
+ result[i][j] = matrix1.m[i][j] + matrix2.m[i][j];
50
+ }
51
+ }
52
+ return new Matrix2D(result);
53
+ }
54
+ static subtract(matrix1, matrix2) {
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
+ static multiply(matrix1, matrix2) {
64
+ const result = Matrix2D.empty;
65
+ for (let i = 0; i < 3; i++) {
66
+ for (let j = 0; j < 3; j++) {
67
+ result[i][j] = 0;
68
+ for (let k = 0; k < 3; k++) {
69
+ result[i][j] += matrix1.m[i][k] * matrix2.m[k][j];
70
+ }
71
+ }
72
+ }
73
+ return new Matrix2D(result);
74
+ }
75
+ static multiplyByValue(matrix, value) {
76
+ const result = Matrix2D.empty;
77
+ for (let i = 0; i < 3; i++) {
78
+ for (let j = 0; j < 3; j++) {
79
+ result[i][j] = matrix.m[i][j] * value;
80
+ }
81
+ }
82
+ return new Matrix2D(result);
83
+ }
84
+ static multiplyByVector(matrix, vector) {
85
+ return Matrix2D.multiply(matrix, new Matrix2D(vector)).toVector;
86
+ }
87
+ static view(width, height) {
88
+ const scaleStep = 1; // Scale every vector * scaleStep
89
+ const centerX = width / 2;
90
+ const centerY = height / 2;
91
+ const flipX = Math.cos(Math.PI); // rotate 180deg / 3.14radian around X-axis
92
+ return new Matrix2D([
93
+ [scaleStep, 0, centerX],
94
+ [0, flipX * scaleStep, centerY],
95
+ [0, 0, 1]
96
+ ]);
97
+ }
98
+ static scale(factor) {
99
+ return Matrix2D.multiplyByValue(new Matrix2D(), factor);
100
+ }
101
+ static rotate(radians) {
102
+ const cos = Math.cos(radians);
103
+ const sin = Math.sin(radians);
104
+ return new Matrix2D([
105
+ [cos, -sin, 0],
106
+ [sin, cos, 0],
107
+ [0, 0, 1]
108
+ ]);
109
+ }
110
+ static translate(vector) {
111
+ return new Matrix2D([
112
+ [1, 0, vector.x],
113
+ [0, 1, vector.y],
114
+ [0, 0, vector.w]
115
+ ]);
116
+ }
117
+ }
118
+ exports.Matrix2D = Matrix2D;
119
+ exports.default = Matrix2D;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Navigator = exports.Character = void 0;
4
+ class Character {
5
+ constructor(direction, turning) {
6
+ this.direction = direction;
7
+ this.turn = () => new Character(turning[direction], turning);
8
+ }
9
+ }
10
+ exports.Character = Character;
11
+ class Navigator {
12
+ constructor({ matrix, turning, onMove, init: { cur, charDir, VISITED } }) {
13
+ this._matrix = matrix;
14
+ this._cur = cur;
15
+ this._character = new Character(charDir, turning);
16
+ this.onMove = onMove;
17
+ this.onMove && this.onMove(this._cur);
18
+ this._VISITED = VISITED;
19
+ this._matrix[this._cur[0]][this._cur[1]] = this._VISITED;
20
+ }
21
+ start() {
22
+ while (this.check(this._character.direction) || this.check(this._character.turn().direction)) {
23
+ const { direction } = this._character;
24
+ if (this.check(direction)) {
25
+ this.move(direction);
26
+ }
27
+ else if (this.check(this._character.turn().direction)) {
28
+ this._character = this._character.turn();
29
+ }
30
+ }
31
+ }
32
+ check(direction) {
33
+ let forward, row;
34
+ const matrix = this._matrix;
35
+ const [i, j] = this._cur;
36
+ switch (direction) {
37
+ case 'up':
38
+ row = matrix[i - 1];
39
+ if (!row)
40
+ return false;
41
+ forward = row[j];
42
+ break;
43
+ case 'right':
44
+ forward = matrix[i][j + 1];
45
+ break;
46
+ case 'down':
47
+ row = matrix[i + 1];
48
+ if (!row)
49
+ return false;
50
+ forward = row[j];
51
+ break;
52
+ case 'left':
53
+ forward = matrix[i][j - 1];
54
+ break;
55
+ }
56
+ return forward !== undefined && forward !== this._VISITED;
57
+ }
58
+ move(direction) {
59
+ switch (direction) {
60
+ case 'up':
61
+ this._cur[0]--;
62
+ break;
63
+ case 'right':
64
+ this._cur[1]++;
65
+ break;
66
+ case 'down':
67
+ this._cur[0]++;
68
+ break;
69
+ case 'left':
70
+ this._cur[1]--;
71
+ break;
72
+ }
73
+ const [i, j] = this._cur;
74
+ this._matrix[i][j] = this._VISITED;
75
+ this.onMove && this.onMove(this._cur);
76
+ }
77
+ }
78
+ exports.Navigator = Navigator;
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class Vector2D {
4
+ static add(vector1, vector2) {
5
+ return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
6
+ }
7
+ static subtract(vector1, vector2) {
8
+ return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
9
+ }
10
+ static subtractValue(vector, value) {
11
+ return new Vector2D(vector.x - value, vector.y - value);
12
+ }
13
+ static multiply(vector, value) {
14
+ return new Vector2D(vector.x * value, vector.y * value);
15
+ }
16
+ static divide(vector, value) {
17
+ return new Vector2D(vector.x / value, vector.y / value);
18
+ }
19
+ static equals(vector1, vector2) {
20
+ return vector1.x === vector2.x && vector1.y === vector2.y;
21
+ }
22
+ static equalsRounded(vector1, vector2, roundingFactor = 12) {
23
+ const vector = Vector2D.abs(Vector2D.subtract(vector1, vector2));
24
+ if (vector.x < roundingFactor && vector.y < roundingFactor) {
25
+ return true;
26
+ }
27
+ return false;
28
+ }
29
+ /**
30
+ * Normalizes the vector if it matches a certain condition
31
+ */
32
+ static normalize(vector) {
33
+ const length = vector.length;
34
+ if (length > 2.220446049250313e-16) { // Epsilon
35
+ return Vector2D.divide(vector, length);
36
+ }
37
+ return vector;
38
+ }
39
+ /**
40
+ * Adjusts x and y so that the length of the vector does not exceed max
41
+ */
42
+ static truncate(vector, max) {
43
+ if (vector.length > max) {
44
+ return Vector2D.multiply(Vector2D.normalize(vector), max);
45
+ }
46
+ return vector;
47
+ }
48
+ /**
49
+ * The vector that is perpendicular to this one
50
+ */
51
+ static perp(vector) {
52
+ return new Vector2D(-vector.y, vector.x);
53
+ }
54
+ /**
55
+ * returns the vector that is the reverse of this vector
56
+ */
57
+ static reverse(vector) {
58
+ return new Vector2D(-vector.x, -vector.y);
59
+ }
60
+ static abs(vector) {
61
+ return new Vector2D(Math.abs(vector.x), Math.abs(vector.y));
62
+ }
63
+ /**
64
+ * The dot product of v1 and v2
65
+ */
66
+ static dot(vector1, vector2) {
67
+ return (vector1.x * vector2.x) + (vector1.y * vector2.y);
68
+ }
69
+ /**
70
+ * The distance between this and the vector
71
+ */
72
+ static distance(vector1, vector2) {
73
+ const ySeparation = vector2.y - vector1.y;
74
+ const xSeparation = vector2.x - vector1.x;
75
+ return Math.sqrt((ySeparation * ySeparation) + (xSeparation * xSeparation));
76
+ }
77
+ /**
78
+ * The distance between this and the vector squared
79
+ */
80
+ static distanceSq(vector1, vector2) {
81
+ const ySeparation = vector2.y - vector1.y;
82
+ const xSeparation = vector2.x - vector1.x;
83
+ return (ySeparation * ySeparation) + (xSeparation * xSeparation);
84
+ }
85
+ /**
86
+ * Returns positive if v2 is clockwise of this vector, negative if counterclockwise
87
+ * (assuming the Y axis is pointing down, X axis to right like a Window app)
88
+ */
89
+ static sign(vector1, vector2) {
90
+ if (vector1.y * vector2.x > vector1.x * vector2.y) {
91
+ return -1;
92
+ }
93
+ return 1;
94
+ }
95
+ /**
96
+ * Returns the angle between origin and the given vector in radians
97
+ * @param vector
98
+ */
99
+ static angle(vector) {
100
+ const origin = new Vector2D(0, -1);
101
+ const radian = Math.acos(Vector2D.dot(vector, origin) / (vector.length * origin.length));
102
+ return Vector2D.sign(vector, origin) === 1 ? ((Math.PI * 2) - radian) : radian;
103
+ }
104
+ static random(maxX, maxY) {
105
+ const randX = Math.floor(Math.random() * maxX - (maxX / 2));
106
+ const randY = Math.floor(Math.random() * maxY - (maxY / 2));
107
+ return new Vector2D(randX, randY);
108
+ }
109
+ // /**
110
+ // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
111
+ // * @param vectors The vectors to transform
112
+ // */
113
+ // public static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
114
+ // return Matrix2D.multiplyByVector(transformation, vector)
115
+ // }
116
+ // /**
117
+ // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
118
+ // * @param vectors The vectors to transform
119
+ // */
120
+ // public static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
121
+ // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
122
+ // }
123
+ constructor(x = 0, y = 0, w = 1 // needed for matrix multiplication
124
+ ) {
125
+ this.x = x;
126
+ this.y = y;
127
+ this.w = w;
128
+ }
129
+ /**
130
+ * Check wether both x and y are zero
131
+ */
132
+ zero() {
133
+ this.x = 0;
134
+ this.y = 0;
135
+ }
136
+ /**
137
+ * Set x and y both to zero
138
+ */
139
+ get isZero() {
140
+ return this.x === 0 && this.y === 0;
141
+ }
142
+ /**
143
+ * The length / magnitude of the vector
144
+ */
145
+ get length() {
146
+ return Math.sqrt((this.x * this.x) + (this.y * this.y));
147
+ }
148
+ /**
149
+ * The squared length of the vector
150
+ */
151
+ get lengthSq() {
152
+ return (this.x * this.x) + (this.y * this.y);
153
+ }
154
+ /**
155
+ * Return the vector with rounded values
156
+ */
157
+ get rounded() {
158
+ return new Vector2D(Math.round(this.x), Math.round(this.y));
159
+ }
160
+ }
161
+ exports.default = Vector2D;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./priority-queue"), exports);
18
+ __exportStar(require("./min-priority-queue"), exports);
19
+ __exportStar(require("./max-priority-queue"), exports);
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MaxPriorityQueue = void 0;
4
+ const priority_queue_1 = require("./priority-queue");
5
+ class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
6
+ constructor(options) {
7
+ super({
8
+ nodes: options.nodes, comparator: (a, b) => {
9
+ const aKey = a, bKey = b;
10
+ return bKey - aKey;
11
+ }
12
+ });
13
+ }
14
+ }
15
+ exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MinPriorityQueue = void 0;
4
+ const priority_queue_1 = require("./priority-queue");
5
+ class MinPriorityQueue extends priority_queue_1.PriorityQueue {
6
+ constructor(options) {
7
+ super({
8
+ nodes: options.nodes, comparator: (a, b) => {
9
+ const aKey = a, bKey = b;
10
+ return aKey - bKey;
11
+ }
12
+ });
13
+ }
14
+ }
15
+ exports.MinPriorityQueue = MinPriorityQueue;
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriorityQueue = void 0;
4
+ class PriorityQueue {
5
+ get size() {
6
+ return this.nodes.length;
7
+ }
8
+ constructor(options) {
9
+ this.nodes = [];
10
+ this._comparator = (a, b) => {
11
+ const aKey = a, bKey = b;
12
+ return aKey - bKey;
13
+ };
14
+ const { nodes, comparator, isFix = true } = options;
15
+ this._comparator = comparator;
16
+ if (nodes && nodes instanceof Array && nodes.length > 0) {
17
+ // TODO support distinct
18
+ this.nodes = Array.isArray(nodes) ? [...nodes] : [];
19
+ isFix && this._fix();
20
+ }
21
+ }
22
+ _compare(a, b) {
23
+ return this._comparator(this.nodes[a], this.nodes[b]) > 0;
24
+ }
25
+ _swap(a, b) {
26
+ const temp = this.nodes[a];
27
+ this.nodes[a] = this.nodes[b];
28
+ this.nodes[b] = temp;
29
+ }
30
+ _isValidIndex(index) {
31
+ return index > -1 && index < this.nodes.length;
32
+ }
33
+ _getParent(child) {
34
+ return Math.floor((child - 1) / 2);
35
+ }
36
+ _getLeft(parent) {
37
+ return (2 * parent) + 1;
38
+ }
39
+ _getRight(parent) {
40
+ return (2 * parent) + 2;
41
+ }
42
+ _getComparedChild(parent) {
43
+ let min = parent;
44
+ const left = this._getLeft(parent), right = this._getRight(parent);
45
+ if (left < this.size && this._compare(min, left)) {
46
+ min = left;
47
+ }
48
+ if (right < this.size && this._compare(min, right)) {
49
+ min = right;
50
+ }
51
+ return min;
52
+ }
53
+ _heapifyUp(start) {
54
+ while (start > 0 && this._compare(this._getParent(start), start)) {
55
+ const parent = this._getParent(start);
56
+ this._swap(start, parent);
57
+ start = parent;
58
+ }
59
+ }
60
+ _heapifyDown(start) {
61
+ let min = this._getComparedChild(start);
62
+ while (this._compare(start, min)) {
63
+ this._swap(min, start);
64
+ start = min;
65
+ min = this._getComparedChild(start);
66
+ }
67
+ }
68
+ _fix() {
69
+ for (let i = Math.floor(this.size / 2); i > -1; i--)
70
+ this._heapifyDown(i);
71
+ }
72
+ offer(node) {
73
+ this.nodes.push(node);
74
+ this._heapifyUp(this.size - 1);
75
+ }
76
+ peek() {
77
+ return this.size ? this.nodes[0] : null;
78
+ }
79
+ poll() {
80
+ var _a, _b;
81
+ let res = null;
82
+ if (this.size > 1) {
83
+ this._swap(0, this.nodes.length - 1);
84
+ res = (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
85
+ this._heapifyDown(0);
86
+ }
87
+ else if (this.size === 1) {
88
+ res = (_b = this.nodes.pop()) !== null && _b !== void 0 ? _b : null;
89
+ }
90
+ return res;
91
+ }
92
+ leaf() {
93
+ var _a;
94
+ return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
95
+ }
96
+ isEmpty() {
97
+ return this.size === 0;
98
+ }
99
+ clear() {
100
+ this.nodes = [];
101
+ }
102
+ toArray() {
103
+ return [...this.nodes];
104
+ }
105
+ clone() {
106
+ return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
107
+ }
108
+ // --- start additional methods ---
109
+ isValid() {
110
+ const isValidRecursive = (parentIndex) => {
111
+ let isValidLeft = true;
112
+ let isValidRight = true;
113
+ if (this._getLeft(parentIndex) !== -1) {
114
+ const leftChildIndex = (parentIndex * 2) + 1;
115
+ if (!this._compare(parentIndex, leftChildIndex))
116
+ return false;
117
+ isValidLeft = isValidRecursive(leftChildIndex);
118
+ }
119
+ if (this._getRight(parentIndex) !== -1) {
120
+ const rightChildIndex = (parentIndex * 2) + 2;
121
+ if (!this._compare(parentIndex, rightChildIndex))
122
+ return false;
123
+ isValidRight = isValidRecursive(rightChildIndex);
124
+ }
125
+ return isValidLeft && isValidRight;
126
+ };
127
+ return isValidRecursive(0);
128
+ }
129
+ sort() {
130
+ const visitedNode = [];
131
+ while (this.size !== 0) {
132
+ const top = this.poll();
133
+ if (top)
134
+ visitedNode.push(top);
135
+ }
136
+ return visitedNode;
137
+ }
138
+ DFS(dfsMode) {
139
+ const visitedNode = [];
140
+ const traverse = (cur) => {
141
+ var _a, _b, _c;
142
+ const leftChildIndex = this._getLeft(cur);
143
+ const rightChildIndex = this._getRight(cur);
144
+ switch (dfsMode) {
145
+ case 'in':
146
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
147
+ visitedNode.push((_a = this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
148
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
149
+ break;
150
+ case 'pre':
151
+ visitedNode.push((_b = this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
152
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
153
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
154
+ break;
155
+ case 'post':
156
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
157
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
158
+ visitedNode.push((_c = this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
159
+ break;
160
+ }
161
+ };
162
+ this._isValidIndex(0) && traverse(0);
163
+ return visitedNode;
164
+ }
165
+ static heapify(options) {
166
+ const heap = new PriorityQueue(options);
167
+ heap._fix();
168
+ return heap;
169
+ }
170
+ static isPriorityQueueified(options) {
171
+ return new PriorityQueue(Object.assign(Object.assign({}, options), { isFix: true })).isValid();
172
+ }
173
+ }
174
+ exports.PriorityQueue = PriorityQueue;