data-structure-typed 0.8.17 → 0.9.16

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 (219) hide show
  1. package/.idea/modules.xml +1 -1
  2. package/README.md +197 -2
  3. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
  5. package/dist/data-structures/binary-tree/avl-tree.js +93 -46
  6. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
  8. package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
  10. package/dist/data-structures/binary-tree/binary-tree.js +480 -370
  11. package/dist/data-structures/binary-tree/bst.d.ts +4 -8
  12. package/dist/data-structures/binary-tree/bst.js +152 -107
  13. package/dist/data-structures/binary-tree/index.d.ts +7 -0
  14. package/dist/data-structures/binary-tree/index.js +7 -0
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +91 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
  23. package/dist/data-structures/graph/abstract-graph.js +546 -311
  24. package/dist/data-structures/graph/directed-graph.d.ts +5 -13
  25. package/dist/data-structures/graph/directed-graph.js +250 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
  27. package/dist/data-structures/graph/undirected-graph.js +166 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
  29. package/dist/data-structures/hash/coordinate-map.js +38 -20
  30. package/dist/data-structures/hash/coordinate-set.js +33 -15
  31. package/dist/data-structures/hash/index.d.ts +5 -0
  32. package/dist/data-structures/hash/index.js +5 -0
  33. package/dist/data-structures/heap/heap.d.ts +2 -8
  34. package/dist/data-structures/heap/heap.js +36 -31
  35. package/dist/data-structures/heap/max-heap.d.ts +3 -2
  36. package/dist/data-structures/heap/max-heap.js +27 -9
  37. package/dist/data-structures/heap/min-heap.d.ts +3 -2
  38. package/dist/data-structures/heap/min-heap.js +27 -9
  39. package/dist/data-structures/index.d.ts +2 -0
  40. package/dist/data-structures/index.js +2 -0
  41. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
  42. package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
  43. package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
  44. package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
  45. package/dist/data-structures/matrix/matrix.js +8 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
  47. package/dist/data-structures/matrix/matrix2d.js +80 -63
  48. package/dist/data-structures/matrix/navigator.d.ts +2 -16
  49. package/dist/data-structures/matrix/navigator.js +37 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +18 -18
  51. package/dist/data-structures/matrix/vector2d.js +117 -94
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +159 -116
  58. package/dist/data-structures/queue/deque.js +82 -56
  59. package/dist/data-structures/queue/index.d.ts +1 -0
  60. package/dist/data-structures/queue/index.js +1 -0
  61. package/dist/data-structures/queue/queue.d.ts +9 -10
  62. package/dist/data-structures/queue/queue.js +34 -34
  63. package/dist/data-structures/stack/stack.d.ts +9 -10
  64. package/dist/data-structures/stack/stack.js +31 -31
  65. package/dist/data-structures/trampoline.d.ts +14 -23
  66. package/dist/data-structures/trampoline.js +103 -25
  67. package/dist/data-structures/trie/trie.d.ts +13 -3
  68. package/dist/data-structures/trie/trie.js +234 -80
  69. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  70. package/dist/data-structures/types/abstract-graph.js +2 -0
  71. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  72. package/dist/data-structures/types/avl-tree.js +2 -0
  73. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  74. package/dist/data-structures/types/binary-tree.js +2 -0
  75. package/dist/data-structures/types/bst.d.ts +7 -0
  76. package/dist/data-structures/types/bst.js +2 -0
  77. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  78. package/dist/data-structures/types/directed-graph.js +2 -0
  79. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  80. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  81. package/dist/data-structures/types/heap.d.ts +7 -0
  82. package/dist/data-structures/types/heap.js +2 -0
  83. package/dist/data-structures/types/index.d.ts +13 -0
  84. package/dist/data-structures/types/index.js +29 -0
  85. package/dist/data-structures/types/navigator.d.ts +14 -0
  86. package/dist/data-structures/types/navigator.js +2 -0
  87. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  88. package/dist/data-structures/types/priority-queue.js +2 -0
  89. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  90. package/dist/data-structures/types/segment-tree.js +2 -0
  91. package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
  92. package/dist/data-structures/types/singly-linked-list.js +2 -0
  93. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  94. package/dist/data-structures/types/tree-multiset.js +2 -0
  95. package/dist/{types → data-structures/types}/utils.d.ts +7 -1
  96. package/dist/{types → data-structures/types}/utils.js +20 -19
  97. package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
  98. package/dist/utils/utils.js +651 -0
  99. package/package.json +20 -42
  100. package/src/data-structures/binary-tree/avl-tree.ts +1 -6
  101. package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
  102. package/src/data-structures/binary-tree/binary-tree.ts +184 -139
  103. package/src/data-structures/binary-tree/bst.ts +15 -24
  104. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  105. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  106. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  107. package/src/data-structures/binary-tree/index.ts +7 -0
  108. package/src/data-structures/binary-tree/segment-tree.ts +20 -12
  109. package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
  110. package/src/data-structures/diagrams/README.md +7 -0
  111. package/src/data-structures/graph/abstract-graph.ts +58 -94
  112. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  113. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  114. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  115. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  116. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  117. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  118. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  119. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  120. package/src/data-structures/graph/diagrams/mst.png +0 -0
  121. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  122. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  123. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  124. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  125. package/src/data-structures/graph/directed-graph.ts +12 -28
  126. package/src/data-structures/graph/undirected-graph.ts +11 -10
  127. package/src/data-structures/hash/coordinate-map.ts +1 -1
  128. package/src/data-structures/hash/index.ts +5 -0
  129. package/src/data-structures/heap/heap.ts +2 -11
  130. package/src/data-structures/heap/max-heap.ts +3 -2
  131. package/src/data-structures/heap/min-heap.ts +3 -2
  132. package/src/data-structures/index.ts +2 -0
  133. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
  134. package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
  135. package/src/data-structures/matrix/matrix2d.ts +11 -11
  136. package/src/data-structures/matrix/navigator.ts +2 -14
  137. package/src/data-structures/matrix/vector2d.ts +52 -52
  138. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  139. package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
  140. package/src/data-structures/priority-queue/priority-queue.ts +70 -78
  141. package/src/data-structures/queue/deque.ts +2 -2
  142. package/src/data-structures/queue/index.ts +1 -0
  143. package/src/data-structures/queue/queue.ts +12 -13
  144. package/src/data-structures/stack/stack.ts +12 -13
  145. package/src/data-structures/trampoline.ts +31 -71
  146. package/src/data-structures/trie/trie.ts +61 -11
  147. package/src/data-structures/types/abstract-graph.ts +51 -0
  148. package/src/data-structures/types/avl-tree.ts +6 -0
  149. package/src/data-structures/types/binary-tree.ts +15 -0
  150. package/src/data-structures/types/bst.ts +5 -0
  151. package/src/data-structures/types/directed-graph.ts +18 -0
  152. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  153. package/src/data-structures/types/heap.ts +8 -0
  154. package/src/data-structures/types/index.ts +13 -0
  155. package/src/data-structures/types/navigator.ts +12 -0
  156. package/src/data-structures/types/priority-queue.ts +9 -0
  157. package/src/data-structures/types/segment-tree.ts +1 -0
  158. package/src/data-structures/types/singly-linked-list.ts +15 -0
  159. package/src/data-structures/types/tree-multiset.ts +3 -0
  160. package/src/{types → data-structures/types}/utils.ts +20 -5
  161. package/src/utils/index.ts +1 -0
  162. package/src/{utils.ts → utils/utils.ts} +32 -132
  163. package/tsconfig.json +9 -6
  164. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  165. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  166. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  167. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  168. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  169. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  170. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  171. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  172. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  173. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  174. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  175. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  176. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  177. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  178. package/dist/types/data-structures/graph/index.d.ts +0 -3
  179. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  180. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  181. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  182. package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  185. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  186. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  187. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  188. package/dist/types/data-structures/heap/index.d.ts +0 -3
  189. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  190. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  191. package/dist/types/data-structures/index.d.ts +0 -9
  192. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  193. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  194. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  195. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  196. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  197. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  198. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  199. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  200. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  201. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  202. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  203. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  204. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  205. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  206. package/dist/types/data-structures/queue/index.d.ts +0 -1
  207. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  208. package/dist/types/data-structures/stack/index.d.ts +0 -1
  209. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  210. package/dist/types/data-structures/trampoline.d.ts +0 -25
  211. package/dist/types/data-structures/trie/index.d.ts +0 -1
  212. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  213. package/dist/types/types/index.d.ts +0 -1
  214. package/dist/types/types/utils.d.ts +0 -46
  215. package/dist/utils.js +0 -569
  216. package/src/types/index.ts +0 -1
  217. package/src/types/patches/index.d.ts +0 -0
  218. /package/dist/{types → utils}/index.d.ts +0 -0
  219. /package/dist/{types → utils}/index.js +0 -0
@@ -1,10 +1,11 @@
1
1
  /**
2
- * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
2
+ * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import {Heap, HeapItem, HeapOptions} from './heap';
6
+ import {Heap} from './heap';
7
7
  import {PriorityQueue} from '../priority-queue';
8
+ import type {HeapItem, HeapOptions} from '../types';
8
9
 
9
10
  /**
10
11
  * @class MaxHeap
@@ -1,10 +1,11 @@
1
1
  /**
2
- * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
2
+ * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
3
3
  * @license MIT
4
4
  */
5
5
 
6
- import {Heap, HeapItem, HeapOptions} from './heap';
6
+ import {Heap} from './heap';
7
7
  import {PriorityQueue} from '../priority-queue';
8
+ import type {HeapItem, HeapOptions} from '../types';
8
9
 
9
10
  /**
10
11
  * @class MinHeap
@@ -7,4 +7,6 @@ export * from './binary-tree';
7
7
  export * from './heap';
8
8
  export * from './priority-queue';
9
9
  export * from './matrix';
10
+ export * from './trie';
11
+ export * from './types';
10
12
 
@@ -1,10 +1,4 @@
1
- // 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby
2
- // 尾部插入 inject, snoc Append offerLast push push_back append push array_push push
3
- // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift
4
- // 尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop
5
- // 头部删除 pop Delete_First pollFirst shift pop_front popleft shift array_shift shift
6
- // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end last
7
- // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
1
+ import type {DoublyLinkedListGetBy} from '../types';
8
2
 
9
3
  export class DoublyLinkedListNode<T> {
10
4
  val: T;
@@ -18,8 +12,6 @@ export class DoublyLinkedListNode<T> {
18
12
  }
19
13
  }
20
14
 
21
- export type DoublyLinkedListGetBy = 'node' | 'val';
22
-
23
15
  export class DoublyLinkedList<T> {
24
16
  private _first: DoublyLinkedListNode<T> | null = null;
25
17
  private _last: DoublyLinkedListNode<T> | null = null;
@@ -1,16 +1,4 @@
1
- /** Type used for filter and find methods, returning a boolean */
2
- type TTestFunction<NodeData> = (
3
- data: NodeData,
4
- index: number,
5
- list: SinglyLinkedList<NodeData>,
6
- ) => boolean;
7
-
8
- /** Type used for map and forEach methods, returning anything */
9
- type TMapFunction<NodeData> = (
10
- data: any,
11
- index: number,
12
- list: SinglyLinkedList<NodeData>,
13
- ) => any;
1
+ import type {TMapFunction, TTestFunction} from '../types';
14
2
 
15
3
  /**
16
4
  * The class which represents one link or node in a linked list
@@ -104,6 +92,23 @@ export class SinglyLinkedListNode<NodeData = any> {
104
92
  */
105
93
  export class SinglyLinkedList<NodeData = any> {
106
94
 
95
+ /** The head of the list, the first node */
96
+ public head: SinglyLinkedListNode<NodeData> | null;
97
+ /** The tail of the list, the last node */
98
+ public tail: SinglyLinkedListNode<NodeData> | null;
99
+ /** Internal size reference */
100
+ private size: number;
101
+
102
+ constructor(...args: NodeData[]) {
103
+ this.head = null;
104
+ this.tail = null;
105
+ this.size = 0;
106
+
107
+ for (let i = 0; i < arguments.length; i++) {
108
+ this.append(args[i]);
109
+ }
110
+ }
111
+
107
112
  /**
108
113
  * The length of the list
109
114
  */
@@ -123,25 +128,6 @@ export class SinglyLinkedList<NodeData = any> {
123
128
  return new SinglyLinkedList(...iterable);
124
129
  }
125
130
 
126
- /** The head of the list, the first node */
127
- public head: SinglyLinkedListNode<NodeData> | null;
128
-
129
- /** The tail of the list, the last node */
130
- public tail: SinglyLinkedListNode<NodeData> | null;
131
-
132
- /** Internal size reference */
133
- private size: number;
134
-
135
- constructor(...args: NodeData[]) {
136
- this.head = null;
137
- this.tail = null;
138
- this.size = 0;
139
-
140
- for (let i = 0; i < arguments.length; i++) {
141
- this.append(args[i]);
142
- }
143
- }
144
-
145
131
  /**
146
132
  * Get the node val at a specified index, zero based
147
133
  * ```ts
@@ -16,21 +16,10 @@ export class Matrix2D {
16
16
  }
17
17
  }
18
18
 
19
- /**
20
- * Return the matrix values
21
- */
22
- public get m(): number[][] {
23
- return this._matrix
24
- }
25
-
26
19
  public static get empty(): number[][] {
27
20
  return [[], [], []]
28
21
  }
29
22
 
30
- public get toVector(): Vector2D {
31
- return new Vector2D(this._matrix[0][0], this._matrix[1][0])
32
- }
33
-
34
23
  /**
35
24
  * Initialize an identity matrix
36
25
  */
@@ -41,6 +30,17 @@ export class Matrix2D {
41
30
  [0, 0, 1]]
42
31
  }
43
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
44
  public static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D {
45
45
  const result = Matrix2D.empty
46
46
  for (let i = 0; i < 3; i++) {
@@ -1,5 +1,4 @@
1
- type Direction = 'up' | 'right' | 'down' | 'left';
2
- type Turning = { [key in Direction]: Direction };
1
+ import type {Direction, NavigatorParams, Turning} from '../types';
3
2
 
4
3
  export class Character {
5
4
  direction: Direction;
@@ -11,23 +10,12 @@ export class Character {
11
10
  }
12
11
  }
13
12
 
14
- interface NavigatorParams<T> {
15
- matrix: T[][],
16
- turning: Turning,
17
- onMove: (cur: [number, number]) => void
18
- init: {
19
- cur: [number, number],
20
- charDir: Direction,
21
- VISITED: T,
22
- }
23
- }
24
-
25
13
  export class Navigator<T = number> {
14
+ onMove: (cur: [number, number]) => void;
26
15
  private readonly _matrix: T[][];
27
16
  private readonly _cur: [number, number];
28
17
  private _character: Character;
29
18
  private readonly _VISITED: T;
30
- onMove: (cur: [number, number]) => void;
31
19
 
32
20
  constructor({matrix, turning, onMove, init: {cur, charDir, VISITED}}: NavigatorParams<T>) {
33
21
  this._matrix = matrix;
@@ -1,4 +1,39 @@
1
- class Vector2D {
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
+
2
37
  public static add(vector1: Vector2D, vector2: Vector2D): Vector2D {
3
38
  return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y)
4
39
  }
@@ -80,6 +115,22 @@ class Vector2D {
80
115
  return (vector1.x * vector2.x) + (vector1.y * vector2.y)
81
116
  }
82
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
+
83
134
  /**
84
135
  * The distance between this and the vector
85
136
  */
@@ -126,29 +177,6 @@ class Vector2D {
126
177
  return new Vector2D(randX, randY)
127
178
  }
128
179
 
129
- // /**
130
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
131
- // * @param vectors The vectors to transform
132
- // */
133
- // public static transform(vector: Vector2D, transformation: Matrix2D): Vector2D {
134
- // return Matrix2D.multiplyByVector(transformation, vector)
135
- // }
136
-
137
- // /**
138
- // * Transform vectors based on the current tranformation matrices: translation, rotation and scale
139
- // * @param vectors The vectors to transform
140
- // */
141
- // public static transformList(vectors: Vector2D[], transformation: Matrix2D): Vector2D[] {
142
- // return vectors.map(vector => Matrix2D.multiplyByVector(transformation, vector))
143
- // }
144
-
145
- constructor(
146
- public x: number = 0,
147
- public y: number = 0,
148
- public w: number = 1 // needed for matrix multiplication
149
- ) {
150
- }
151
-
152
180
  /**
153
181
  * Check wether both x and y are zero
154
182
  */
@@ -156,34 +184,6 @@ class Vector2D {
156
184
  this.x = 0
157
185
  this.y = 0
158
186
  }
159
-
160
- /**
161
- * Set x and y both to zero
162
- */
163
- public get isZero(): boolean {
164
- return this.x === 0 && this.y === 0
165
- }
166
-
167
- /**
168
- * The length / magnitude of the vector
169
- */
170
- public get length(): number {
171
- return Math.sqrt((this.x * this.x) + (this.y * this.y))
172
- }
173
-
174
- /**
175
- * The squared length of the vector
176
- */
177
- public get lengthSq(): number {
178
- return (this.x * this.x) + (this.y * this.y)
179
- }
180
-
181
- /**
182
- * Return the vector with rounded values
183
- */
184
- public get rounded(): Vector2D {
185
- return new Vector2D(Math.round(this.x), Math.round(this.y))
186
- }
187
187
  }
188
188
 
189
189
  export default Vector2D
@@ -1,9 +1,10 @@
1
- import {PriorityQueue, PriorityQueueOptions} from './priority-queue';
1
+ import {PriorityQueue} from './priority-queue';
2
+ import type {PriorityQueueOptions} from '../types';
2
3
 
3
4
  export class MaxPriorityQueue<T = number> extends PriorityQueue<T> {
4
- constructor(options: PriorityQueueOptions<T>) {
5
+ constructor(options?: PriorityQueueOptions<T>) {
5
6
  super({
6
- nodes: options.nodes, comparator: (a: T, b: T) => {
7
+ nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
7
8
  const aKey = a as unknown as number, bKey = b as unknown as number;
8
9
  return bKey - aKey;
9
10
  }
@@ -1,9 +1,10 @@
1
- import {PriorityQueue, PriorityQueueOptions} from './priority-queue';
1
+ import {PriorityQueue} from './priority-queue';
2
+ import type {PriorityQueueOptions} from '../types';
2
3
 
3
4
  export class MinPriorityQueue<T = number> extends PriorityQueue<T> {
4
- constructor(options: PriorityQueueOptions<T>) {
5
+ constructor(options?: PriorityQueueOptions<T>) {
5
6
  super({
6
- nodes: options.nodes, comparator: (a: T, b: T) => {
7
+ nodes: options?.nodes, comparator: options?.comparator ? options.comparator : (a: T, b: T) => {
7
8
  const aKey = a as unknown as number, bKey = b as unknown as number;
8
9
  return aKey - bKey;
9
10
  }
@@ -1,25 +1,8 @@
1
- export type PriorityQueueComparator<T> = (a: T, b: T) => number;
2
-
3
- export interface PriorityQueueOptions<T> {
4
- nodes?: T[];
5
- isFix?: boolean;
6
- comparator: PriorityQueueComparator<T>;
7
- }
8
-
9
- export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post';
1
+ import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
10
2
 
11
3
  export class PriorityQueue<T = number> {
12
4
  protected nodes: T[] = [];
13
5
 
14
- get size(): number {
15
- return this.nodes.length;
16
- }
17
-
18
- protected readonly _comparator: PriorityQueueComparator<T> = (a: T, b: T) => {
19
- const aKey = a as unknown as number, bKey = b as unknown as number;
20
- return aKey - bKey;
21
- };
22
-
23
6
  constructor(options: PriorityQueueOptions<T>) {
24
7
  const {nodes, comparator, isFix = true} = options;
25
8
  this._comparator = comparator;
@@ -31,64 +14,18 @@ export class PriorityQueue<T = number> {
31
14
  }
32
15
  }
33
16
 
34
- protected _compare(a: number, b: number) {
35
- return this._comparator(this.nodes[a], this.nodes[b]) > 0;
36
- }
37
-
38
- protected _swap(a: number, b: number) {
39
- const temp = this.nodes[a];
40
- this.nodes[a] = this.nodes[b];
41
- this.nodes[b] = temp;
42
- }
43
-
44
- protected _isValidIndex(index: number): boolean {
45
- return index > -1 && index < this.nodes.length;
46
- }
47
-
48
- protected _getParent(child: number): number {
49
- return Math.floor((child - 1) / 2);
50
- }
51
-
52
- protected _getLeft(parent: number): number {
53
- return (2 * parent) + 1;
54
- }
55
-
56
- protected _getRight(parent: number): number {
57
- return (2 * parent) + 2;
58
- }
59
-
60
- protected _getComparedChild(parent: number) {
61
- let min = parent;
62
- const left = this._getLeft(parent), right = this._getRight(parent);
63
-
64
- if (left < this.size && this._compare(min, left)) {
65
- min = left;
66
- }
67
- if (right < this.size && this._compare(min, right)) {
68
- min = right;
69
- }
70
- return min;
71
- }
72
-
73
- protected _heapifyUp(start: number) {
74
- while (start > 0 && this._compare(this._getParent(start), start)) {
75
- const parent = this._getParent(start);
76
- this._swap(start, parent);
77
- start = parent;
78
- }
17
+ get size(): number {
18
+ return this.nodes.length;
79
19
  }
80
20
 
81
- protected _heapifyDown(start: number) {
82
- let min = this._getComparedChild(start);
83
- while (this._compare(start, min)) {
84
- this._swap(min, start);
85
- start = min;
86
- min = this._getComparedChild(start);
87
- }
21
+ static heapify<T>(options: PriorityQueueOptions<T>) {
22
+ const heap = new PriorityQueue(options);
23
+ heap._fix();
24
+ return heap;
88
25
  }
89
26
 
90
- protected _fix() {
91
- for (let i = Math.floor(this.size / 2); i > -1; i--) this._heapifyDown(i);
27
+ static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
28
+ return new PriorityQueue({...options, isFix: true}).isValid();
92
29
  }
93
30
 
94
31
  offer(node: T) {
@@ -194,14 +131,69 @@ export class PriorityQueue<T = number> {
194
131
  return visitedNode;
195
132
  }
196
133
 
197
- static heapify<T>(options: PriorityQueueOptions<T>) {
198
- const heap = new PriorityQueue(options);
199
- heap._fix();
200
- return heap;
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;
201
141
  }
202
142
 
203
- static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>) {
204
- return new PriorityQueue({...options, isFix: true}).isValid();
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);
205
197
  }
206
198
 
207
199
  // --- end additional methods ---
@@ -50,7 +50,7 @@ export class ObjectDeque<T> {
50
50
 
51
51
  pollFirst() {
52
52
  if (!this._size) return;
53
- let value = this.peekFirst();
53
+ const value = this.peekFirst();
54
54
  delete this._nodes[this._first];
55
55
  this._first++;
56
56
  this._size--;
@@ -63,7 +63,7 @@ export class ObjectDeque<T> {
63
63
 
64
64
  pollLast() {
65
65
  if (!this._size) return;
66
- let value = this.peekLast();
66
+ const value = this.peekLast();
67
67
  delete this._nodes[this._last];
68
68
  this._last--;
69
69
  this._size--;
@@ -1 +1,2 @@
1
1
  export * from './queue';
2
+ export * from './deque';
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2020 Pablo
4
- *
3
+ * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
5
4
  * @class
6
5
  */
7
6
  export class Queue<T> {
@@ -17,6 +16,17 @@ export class Queue<T> {
17
16
  this._offset = 0;
18
17
  }
19
18
 
19
+ /**
20
+ * Creates a queue from an existing array.
21
+ * @public
22
+ * @static
23
+ * @param {array} elements
24
+ * @return {Queue}
25
+ */
26
+ static fromArray<T>(elements: T[]): Queue<T> {
27
+ return new Queue(elements);
28
+ }
29
+
20
30
  /**
21
31
  * Adds an element at the back of the queue.
22
32
  * @public
@@ -109,15 +119,4 @@ export class Queue<T> {
109
119
  clone(): Queue<T> {
110
120
  return new Queue(this._nodes.slice(this._offset));
111
121
  }
112
-
113
- /**
114
- * Creates a queue from an existing array.
115
- * @public
116
- * @static
117
- * @param {array} elements
118
- * @return {Queue}
119
- */
120
- static fromArray<T>(elements: T[]): Queue<T> {
121
- return new Queue(elements);
122
- }
123
122
  }
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
4
- *
3
+ * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
5
4
  * @class
6
5
  */
7
6
  export class Stack<T> {
@@ -15,6 +14,17 @@ export class Stack<T> {
15
14
  this._elements = Array.isArray(elements) ? elements : [];
16
15
  }
17
16
 
17
+ /**
18
+ * Creates a stack from an existing array
19
+ * @public
20
+ * @static
21
+ * @param {array} [elements]
22
+ * @return {Stack}
23
+ */
24
+ static fromArray<T>(elements: T[]): Stack<T> {
25
+ return new Stack(elements);
26
+ }
27
+
18
28
  /**
19
29
  * Checks if the stack is empty.
20
30
  * @public
@@ -90,15 +100,4 @@ export class Stack<T> {
90
100
  clone(): Stack<T> {
91
101
  return new Stack(this._elements.slice());
92
102
  }
93
-
94
- /**
95
- * Creates a stack from an existing array
96
- * @public
97
- * @static
98
- * @param {array} [elements]
99
- * @return {Stack}
100
- */
101
- static fromArray<T>(elements: T[]): Stack<T> {
102
- return new Stack(elements);
103
- }
104
103
  }