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,322 @@
1
+ import {arrayRemove} from '../../utils';
2
+ import {AbstractEdge, AbstractGraph, AbstractVertex, VertexId} from './abstract-graph';
3
+
4
+ export class DirectedVertex extends AbstractVertex {
5
+ constructor(id: VertexId) {
6
+ super(id);
7
+ }
8
+ }
9
+
10
+ export class DirectedEdge extends AbstractEdge {
11
+ constructor(src: VertexId, dest: VertexId, weight?: number) {
12
+ super(weight);
13
+ this._src = src;
14
+ this._dest = dest;
15
+ }
16
+
17
+ private _src: VertexId;
18
+ get src(): VertexId {
19
+ return this._src;
20
+ }
21
+
22
+ set src(v: VertexId) {
23
+ this._src = v;
24
+ }
25
+
26
+
27
+ private _dest: VertexId;
28
+ get dest(): VertexId {
29
+ return this._dest;
30
+ }
31
+
32
+ set dest(v: VertexId) {
33
+ this._dest = v;
34
+ }
35
+ }
36
+
37
+ export interface I_DirectedGraph<V, E> {
38
+ incomingEdgesOf(vertex: V): E[];
39
+
40
+ outgoingEdgesOf(vertex: V): E[];
41
+
42
+ inDegreeOf(vertexOrId: V | VertexId): number;
43
+
44
+ outDegreeOf(vertexOrId: V | VertexId): number;
45
+
46
+ getEdgeSrc(e: E): V | null;
47
+
48
+ getEdgeDest(e: E): V | null;
49
+ }
50
+
51
+ // 0 means unknown, 1 means visiting, 2 means visited;
52
+ export type TopologicalStatus = 0 | 1 | 2;
53
+
54
+ // Strongly connected, One direction connected, Weakly connected
55
+ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements I_DirectedGraph<V, E> {
56
+
57
+ protected _outEdgeMap: Map<V, E[]> = new Map<V, E[]>();
58
+
59
+ protected _inEdgeMap: Map<V, E[]> = new Map<V, E[]>();
60
+
61
+ constructor() {
62
+ super();
63
+ }
64
+
65
+ getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
66
+ let edges: E[] = [];
67
+
68
+ if (srcOrId !== null && destOrId !== null) {
69
+ const src: V | null = this.getVertex(srcOrId);
70
+ const dest: V | null = this.getVertex(destOrId);
71
+
72
+ if (src && dest) {
73
+ const srcOutEdges = this._outEdgeMap.get(src);
74
+ if (srcOutEdges) {
75
+ edges = srcOutEdges.filter(edge => edge.dest === dest.id);
76
+ }
77
+ }
78
+ }
79
+
80
+ return edges[0] || null;
81
+ }
82
+
83
+ addEdge(edge: E): boolean {
84
+ if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
85
+ return false;
86
+ }
87
+
88
+ const srcVertex = this.getVertex(edge.src);
89
+ const destVertex = this.getVertex(edge.dest);
90
+
91
+ // TODO after no-non-null-assertion not ensure the logic
92
+ if (srcVertex && destVertex) {
93
+ const srcOutEdges = this._outEdgeMap.get(srcVertex);
94
+ if (srcOutEdges) {
95
+ srcOutEdges.push(edge);
96
+ } else {
97
+ this._outEdgeMap.set(srcVertex, [edge]);
98
+ }
99
+
100
+ const destInEdges = this._inEdgeMap.get(destVertex);
101
+ if (destInEdges) {
102
+ destInEdges.push(edge);
103
+ } else {
104
+ this._inEdgeMap.set(destVertex, [edge]);
105
+ }
106
+ return true;
107
+ } else {
108
+ return false;
109
+ }
110
+ }
111
+
112
+ removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
113
+
114
+ const src: V | null = this.getVertex(srcOrId);
115
+ const dest: V | null = this.getVertex(destOrId);
116
+ let removed: E | null = null;
117
+ if (!src || !dest) {
118
+ return null;
119
+ }
120
+
121
+ const srcOutEdges = this._outEdgeMap.get(src);
122
+ if (srcOutEdges) {
123
+ arrayRemove<E>(srcOutEdges, edge => edge.dest === dest.id);
124
+ }
125
+
126
+ const destInEdges = this._inEdgeMap.get(dest);
127
+ if (destInEdges) {
128
+ removed = arrayRemove<E>(destInEdges, edge => edge.src === src.id)[0] || null;
129
+ }
130
+ return removed;
131
+ }
132
+
133
+ removeEdge(edge: E): E | null {
134
+ let removed: E | null = null;
135
+ const src = this.getVertex(edge.src);
136
+ const dest = this.getVertex(edge.dest);
137
+ if (src && dest) {
138
+ const srcOutEdges = this._outEdgeMap.get(src);
139
+ if (srcOutEdges && srcOutEdges.length > 0) {
140
+ arrayRemove(srcOutEdges, edge => edge.src === src.id);
141
+ }
142
+
143
+ const destInEdges = this._inEdgeMap.get(dest);
144
+ if (destInEdges && destInEdges.length > 0) {
145
+ removed = arrayRemove(destInEdges, edge => edge.dest === dest.id)[0];
146
+ }
147
+
148
+ }
149
+
150
+ return removed;
151
+ }
152
+
153
+ removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
154
+ return [];
155
+ }
156
+
157
+ incomingEdgesOf(vertexOrId: V | VertexId): E[] {
158
+ const target = this.getVertex(vertexOrId);
159
+ if (target) {
160
+ return this._inEdgeMap.get(target) || [];
161
+ }
162
+ return [];
163
+ }
164
+
165
+ outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
166
+ const target = this.getVertex(vertexOrId);
167
+ if (target) {
168
+ return this._outEdgeMap.get(target) || [];
169
+ }
170
+ return [];
171
+ }
172
+
173
+ degreeOf(vertexOrId: VertexId | V): number {
174
+ return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
175
+ }
176
+
177
+ inDegreeOf(vertexOrId: VertexId | V): number {
178
+ return this.incomingEdgesOf(vertexOrId).length;
179
+ }
180
+
181
+ outDegreeOf(vertexOrId: VertexId | V): number {
182
+ return this.outgoingEdgesOf(vertexOrId).length;
183
+ }
184
+
185
+ edgesOf(vertexOrId: VertexId | V): E[] {
186
+ return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
187
+ }
188
+
189
+ getEdgeSrc(e: E): V | null {
190
+ return this.getVertex(e.src);
191
+ }
192
+
193
+ getEdgeDest(e: E): V | null {
194
+ return this.getVertex(e.dest);
195
+ }
196
+
197
+ getDestinations(vertex: V | null): V[] {
198
+ if (vertex === null) {
199
+ return [];
200
+ }
201
+ const destinations: V[] = [];
202
+ const outgoingEdges = this.outgoingEdgesOf(vertex);
203
+ for (const outEdge of outgoingEdges) {
204
+ const child = this.getEdgeDest(outEdge);
205
+ if (child) {
206
+ destinations.push(child);
207
+ }
208
+ }
209
+ return destinations;
210
+ }
211
+
212
+ /**--- start find cycles --- */
213
+
214
+ /**
215
+ * when stored with adjacency list time: O(V+E)
216
+ * when stored with adjacency matrix time: O(V^2)
217
+ */
218
+ topologicalSort(): V[] | null {
219
+ // vector<vector<int>> g;
220
+ // vector<int> color;
221
+ // int last;
222
+ // bool hasCycle;
223
+ //
224
+ // bool topo_sort() {
225
+ // int n = g.size();
226
+ // vector<int> degree(n, 0);
227
+ // queue<int> q;
228
+ // for (int i = 0; i < n; i++) {
229
+ // degree[i] = g[i].size();
230
+ // if (degree[i] <= 1) {
231
+ // q.push(i);
232
+ // }
233
+ // }
234
+ // int cnt = 0;
235
+ // while (!q.empty()) {
236
+ // cnt++;
237
+ // int root = q.front();
238
+ // q.pop();
239
+ // for (auto child : g[root]) {
240
+ // degree[child]--;
241
+ // if (degree[child] == 1) {
242
+ // q.push(child);
243
+ // }
244
+ // }
245
+ // }
246
+ // return (cnt != n);
247
+ // }
248
+ // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
249
+ // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
250
+ const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
251
+ for (const entry of this._vertices) {
252
+ statusMap.set(entry[1], 0);
253
+ }
254
+
255
+ const sorted: V[] = [];
256
+ let hasCycle = false;
257
+ const dfs = (cur: V) => {
258
+ statusMap.set(cur, 1);
259
+ const children = this.getDestinations(cur);
260
+ for (const child of children) {
261
+ const childStatus = statusMap.get(child);
262
+ if (childStatus === 0) {
263
+ dfs(child);
264
+ } else if (childStatus === 1) {
265
+ hasCycle = true;
266
+ }
267
+ }
268
+ statusMap.set(cur, 2);
269
+ sorted.push(cur);
270
+ };
271
+
272
+ for (const entry of this._vertices) {
273
+ if (statusMap.get(entry[1]) === 0) {
274
+ dfs(entry[1]);
275
+ }
276
+ }
277
+
278
+ if (hasCycle) {
279
+ return null;
280
+ }
281
+ return sorted.reverse();
282
+ }
283
+
284
+ /**--- end find cycles --- */
285
+
286
+ edgeSet(): E[] {
287
+ let edges: E[] = [];
288
+ this._outEdgeMap.forEach(outEdges => {
289
+ edges = [...edges, ...outEdges];
290
+ });
291
+ return edges;
292
+ }
293
+
294
+ getNeighbors(vertexOrId: V | VertexId): V[] {
295
+ const neighbors: V[] = [];
296
+ const vertex = this.getVertex(vertexOrId);
297
+ if (vertex) {
298
+ const outEdges = this.outgoingEdgesOf(vertex);
299
+ for (const outEdge of outEdges) {
300
+ const neighbor = this.getVertex(outEdge.dest);
301
+ // TODO after no-non-null-assertion not ensure the logic
302
+ if (neighbor) {
303
+ neighbors.push(neighbor);
304
+ }
305
+ }
306
+ }
307
+ return neighbors;
308
+ }
309
+
310
+ getEndsOfEdge(edge: E): [V, V] | null {
311
+ if (!this.containsEdge(edge.src, edge.dest)) {
312
+ return null;
313
+ }
314
+ const v1 = this.getVertex(edge.src);
315
+ const v2 = this.getVertex(edge.dest);
316
+ if (v1 && v2) {
317
+ return [v1, v2];
318
+ } else {
319
+ return null;
320
+ }
321
+ }
322
+ }
@@ -0,0 +1,3 @@
1
+ export * from './abstract-graph';
2
+ export * from './directed-graph';
3
+ export * from './undirected-graph';
@@ -0,0 +1,154 @@
1
+ import {arrayRemove} from '../../utils';
2
+ import {AbstractEdge, AbstractGraph, AbstractVertex, VertexId} from './abstract-graph';
3
+
4
+ export class UndirectedVertex extends AbstractVertex {
5
+ constructor(id: VertexId) {
6
+ super(id);
7
+ }
8
+ }
9
+
10
+ export class UndirectedEdge extends AbstractEdge {
11
+ private _vertices: [VertexId, VertexId];
12
+
13
+ public get vertices() {
14
+ return this._vertices;
15
+ }
16
+
17
+ public set vertices(v: [VertexId, VertexId]) {
18
+ this._vertices = v;
19
+ }
20
+
21
+ constructor(v1: VertexId, v2: VertexId, weight?: number) {
22
+ super(weight);
23
+ this._vertices = [v1, v2];
24
+ }
25
+ }
26
+
27
+ export class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
28
+ constructor() {
29
+ super();
30
+ }
31
+
32
+ protected _edges: Map<V, E[]> = new Map();
33
+
34
+ getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
35
+ let edges: E[] | undefined = [];
36
+
37
+ if (v1 !== null && v2 !== null) {
38
+ const vertex1: V | null = this.getVertex(v1);
39
+ const vertex2: V | null = this.getVertex(v2);
40
+
41
+ if (vertex1 && vertex2) {
42
+ edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
43
+ }
44
+ }
45
+
46
+ return edges ? edges[0] || null : null;
47
+ }
48
+
49
+ addEdge(edge: E): boolean {
50
+ for (const end of edge.vertices) {
51
+ const endVertex = this.getVertex(end);
52
+ if (endVertex === null) return false;
53
+ if (endVertex) {
54
+ const edges = this._edges.get(endVertex);
55
+ if (edges) {
56
+ edges.push(edge);
57
+ } else {
58
+ this._edges.set(endVertex, [edge]);
59
+ }
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+
65
+ removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
66
+
67
+ const vertex1: V | null = this.getVertex(v1);
68
+ const vertex2: V | null = this.getVertex(v2);
69
+
70
+ if (!vertex1 || !vertex2) {
71
+ return null;
72
+ }
73
+
74
+ const v1Edges = this._edges.get(vertex1);
75
+ let removed: E | null = null;
76
+ if (v1Edges) {
77
+ removed = arrayRemove<E>(v1Edges, e => e.vertices.includes(vertex2.id))[0] || null;
78
+ }
79
+ const v2Edges = this._edges.get(vertex2);
80
+ if (v2Edges) {
81
+ arrayRemove<E>(v2Edges, e => e.vertices.includes(vertex1.id));
82
+ }
83
+ return removed;
84
+ }
85
+
86
+
87
+ removeEdge(edge: E): E | null {
88
+ return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
89
+ }
90
+
91
+ degreeOf(vertexOrId: VertexId | V): number {
92
+ const vertex = this.getVertex(vertexOrId);
93
+ if (vertex) {
94
+ return this._edges.get(vertex)?.length || 0;
95
+ } else {
96
+ return 0;
97
+ }
98
+ }
99
+
100
+ edgesOf(vertexOrId: VertexId | V): E[] {
101
+ const vertex = this.getVertex(vertexOrId);
102
+ if (vertex) {
103
+ return this._edges.get(vertex) || [];
104
+ } else {
105
+ return [];
106
+ }
107
+ }
108
+
109
+ edgeSet(): E[] {
110
+ const edgeSet: Set<E> = new Set();
111
+ this._edges.forEach(edges => {
112
+ edges.forEach(edge => {
113
+ edgeSet.add(edge);
114
+ });
115
+ });
116
+ return [...edgeSet];
117
+ }
118
+
119
+ getEdgesOf(vertexOrId: V | VertexId): E[] {
120
+ const vertex = this.getVertex(vertexOrId);
121
+ if (!vertex) {
122
+ return [];
123
+ }
124
+ return this._edges.get(vertex) || [];
125
+ }
126
+
127
+ getNeighbors(vertexOrId: V | VertexId): V[] {
128
+ const neighbors: V[] = [];
129
+ const vertex = this.getVertex(vertexOrId);
130
+ if (vertex) {
131
+ const neighborEdges = this.getEdgesOf(vertex);
132
+ for (const edge of neighborEdges) {
133
+ const neighbor = this.getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
134
+ if (neighbor) {
135
+ neighbors.push(neighbor);
136
+ }
137
+ }
138
+ }
139
+ return neighbors;
140
+ }
141
+
142
+ getEndsOfEdge(edge: E): [V, V] | null {
143
+ if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
144
+ return null;
145
+ }
146
+ const v1 = this.getVertex(edge.vertices[0]);
147
+ const v2 = this.getVertex(edge.vertices[1]);
148
+ if (v1 && v2) {
149
+ return [v1, v2];
150
+ } else {
151
+ return null;
152
+ }
153
+ }
154
+ }
@@ -0,0 +1,24 @@
1
+ export class CoordinateSet<V> extends Map<any, V> {
2
+ private readonly _joint: string = '_';
3
+
4
+ constructor(joint?: string) {
5
+ super();
6
+ if (joint !== undefined) this._joint = joint;
7
+ }
8
+
9
+ override has(key: number[]) {
10
+ return super.has(key.join(this._joint));
11
+ }
12
+
13
+ override set(key: number[], value: V) {
14
+ return super.set(key.join(this._joint), value);
15
+ }
16
+
17
+ override get(key: number[]) {
18
+ return super.get(key.join(this._joint));
19
+ }
20
+
21
+ override delete(key: number[]) {
22
+ return super.delete(key.join(this._joint));
23
+ }
24
+ }
@@ -0,0 +1,20 @@
1
+ export class CoordinateSet extends Set {
2
+ private readonly _joint: string = '_';
3
+
4
+ constructor(joint?: string) {
5
+ super();
6
+ if (joint !== undefined) this._joint = joint;
7
+ }
8
+
9
+ override has(value: number[]) {
10
+ return super.has(value.join(this._joint));
11
+ }
12
+
13
+ override add(value: number[]) {
14
+ return super.add(value.join(this._joint));
15
+ }
16
+
17
+ override delete(value: number[]) {
18
+ return super.delete(value.join(this._joint));
19
+ }
20
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from './hash-table';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,136 @@
1
+ import {PriorityQueue} from '../priority-queue';
2
+
3
+ export interface HeapOptions<T> {
4
+ priority?: (element: T) => number;
5
+ }
6
+
7
+ export interface HeapItem<T> {
8
+ priority: number;
9
+ element: T | null;
10
+ }
11
+
12
+
13
+ /**
14
+ * @copyright 2021 Pablo Rios <zrwusa@gmail.com>
15
+ * @license MIT
16
+ *
17
+ * @abstract
18
+ * @class Heap
19
+ */
20
+ export abstract class Heap<T> {
21
+ protected abstract _pq: PriorityQueue<HeapItem<T>>;
22
+ protected _priorityCb: (element: T) => number;
23
+
24
+ /**
25
+ * Creates a priority queue
26
+ * @public
27
+ * @params {object} [options]
28
+ */
29
+ protected constructor(options?: HeapOptions<T>) {
30
+ if (options) {
31
+ const {priority} = options;
32
+ if (priority !== undefined && typeof priority !== 'function') {
33
+ throw new Error('.constructor expects a valid priority function');
34
+ }
35
+ this._priorityCb = priority || ((el) => +el);
36
+ } else {
37
+ this._priorityCb = (el) => +el;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * @public
43
+ * @returns {number}
44
+ */
45
+ get size(): number {
46
+ return this._pq.size;
47
+ }
48
+
49
+ /**
50
+ * @public
51
+ * @returns {boolean}
52
+ */
53
+ isEmpty(): boolean {
54
+ return this._pq.size < 1;
55
+ }
56
+
57
+ /**
58
+ * Returns an element with highest priority in the queue
59
+ * @public
60
+ * @returns {object}
61
+ */
62
+ peek(): HeapItem<T> | null {
63
+ return this._pq.peek();
64
+ }
65
+
66
+ /**
67
+ * Returns an element with lowest priority in the queue
68
+ * @public
69
+ * @returns {object}
70
+ */
71
+ peekLast(): HeapItem<T> | null {
72
+ return this._pq.leaf();
73
+ }
74
+
75
+ /**
76
+ * Adds an element to the queue
77
+ * @public
78
+ * @param {any} element
79
+ * @param priority
80
+ * @throws {Error} if priority is not a valid number
81
+ */
82
+ offer(element: T, priority?: number): Heap<T> {
83
+ if (typeof element === 'number') {
84
+ priority = element;
85
+ } else {
86
+ if (priority === undefined) {
87
+ throw new Error('.offer expects a numeric priority');
88
+ }
89
+ }
90
+
91
+ if (priority && Number.isNaN(+priority)) {
92
+ throw new Error('.offer expects a numeric priority');
93
+ }
94
+
95
+ if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
96
+ throw new Error(
97
+ '.offer expects a numeric priority '
98
+ + 'or a constructor callback that returns a number'
99
+ );
100
+ }
101
+
102
+ const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
103
+ this._pq.offer({priority: _priority, element});
104
+ return this;
105
+ }
106
+
107
+ /**
108
+ * Removes and returns an element with highest priority in the queue
109
+ * @public
110
+ * @returns {object}
111
+ */
112
+ poll(): HeapItem<T> | null {
113
+ const top = this._pq.poll();
114
+ if (!top) {
115
+ return null;
116
+ }
117
+ return top;
118
+ }
119
+
120
+ /**
121
+ * Returns a sorted list of elements
122
+ * @public
123
+ * @returns {array}
124
+ */
125
+ toArray(): HeapItem<T>[] {
126
+ return this._pq.toArray();
127
+ }
128
+
129
+ /**
130
+ * Clears the queue
131
+ * @public
132
+ */
133
+ clear(): void {
134
+ this._pq.clear();
135
+ }
136
+ }
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
5
+
6
+ import {Heap, HeapItem, HeapOptions} from './heap';
7
+ import {PriorityQueue} from '../priority-queue';
8
+
9
+ /**
10
+ * @class MaxHeap
11
+ * @extends Heap
12
+ */
13
+ export class MaxHeap<T> extends Heap<T> {
14
+ protected _pq: PriorityQueue<HeapItem<T>>;
15
+
16
+ constructor(options?: HeapOptions<T>) {
17
+ super(options);
18
+ this._pq = new PriorityQueue<HeapItem<T>>({
19
+ comparator: (a, b) => b.priority - a.priority
20
+ });
21
+ }
22
+ }