priority-queue-typed 1.49.4 → 1.49.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 (69) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +1 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
  3. package/dist/data-structures/binary-tree/avl-tree.js +55 -49
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +154 -143
  5. package/dist/data-structures/binary-tree/binary-tree.js +211 -198
  6. package/dist/data-structures/binary-tree/bst.d.ts +83 -71
  7. package/dist/data-structures/binary-tree/bst.js +113 -89
  8. package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
  9. package/dist/data-structures/binary-tree/rb-tree.js +62 -59
  10. package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -55
  11. package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
  12. package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
  13. package/dist/data-structures/graph/abstract-graph.js +3 -2
  14. package/dist/data-structures/hash/hash-map.d.ts +1 -1
  15. package/dist/data-structures/hash/hash-map.js +2 -2
  16. package/dist/data-structures/heap/heap.js +2 -3
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  18. package/dist/data-structures/matrix/index.d.ts +0 -2
  19. package/dist/data-structures/matrix/index.js +0 -2
  20. package/dist/data-structures/matrix/matrix.d.ts +128 -10
  21. package/dist/data-structures/matrix/matrix.js +400 -15
  22. package/dist/data-structures/queue/deque.d.ts +2 -2
  23. package/dist/data-structures/queue/deque.js +5 -7
  24. package/dist/data-structures/queue/queue.d.ts +1 -1
  25. package/dist/interfaces/binary-tree.d.ts +3 -3
  26. package/dist/types/common.d.ts +3 -3
  27. package/dist/types/common.js +2 -2
  28. package/dist/types/data-structures/base/base.d.ts +1 -1
  29. package/dist/types/data-structures/heap/heap.d.ts +1 -1
  30. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  31. package/dist/utils/utils.d.ts +1 -0
  32. package/dist/utils/utils.js +6 -1
  33. package/package.json +2 -2
  34. package/src/data-structures/base/index.ts +1 -1
  35. package/src/data-structures/base/iterable-base.ts +7 -10
  36. package/src/data-structures/binary-tree/avl-tree.ts +73 -61
  37. package/src/data-structures/binary-tree/binary-tree.ts +301 -270
  38. package/src/data-structures/binary-tree/bst.ts +139 -115
  39. package/src/data-structures/binary-tree/rb-tree.ts +81 -73
  40. package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
  41. package/src/data-structures/graph/abstract-graph.ts +13 -11
  42. package/src/data-structures/graph/directed-graph.ts +1 -3
  43. package/src/data-structures/graph/map-graph.ts +6 -1
  44. package/src/data-structures/graph/undirected-graph.ts +3 -6
  45. package/src/data-structures/hash/hash-map.ts +18 -16
  46. package/src/data-structures/heap/heap.ts +7 -10
  47. package/src/data-structures/heap/max-heap.ts +2 -1
  48. package/src/data-structures/heap/min-heap.ts +2 -1
  49. package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
  50. package/src/data-structures/matrix/index.ts +0 -2
  51. package/src/data-structures/matrix/matrix.ts +442 -13
  52. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
  53. package/src/data-structures/queue/deque.ts +18 -39
  54. package/src/data-structures/queue/queue.ts +1 -1
  55. package/src/interfaces/binary-tree.ts +9 -4
  56. package/src/types/common.ts +5 -5
  57. package/src/types/data-structures/base/base.ts +14 -3
  58. package/src/types/data-structures/base/index.ts +1 -1
  59. package/src/types/data-structures/graph/abstract-graph.ts +4 -2
  60. package/src/types/data-structures/hash/hash-map.ts +3 -3
  61. package/src/types/data-structures/heap/heap.ts +2 -2
  62. package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
  63. package/src/utils/utils.ts +7 -1
  64. package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
  65. package/dist/data-structures/matrix/matrix2d.js +0 -199
  66. package/dist/data-structures/matrix/vector2d.d.ts +0 -200
  67. package/dist/data-structures/matrix/vector2d.js +0 -290
  68. package/src/data-structures/matrix/matrix2d.ts +0 -211
  69. package/src/data-structures/matrix/vector2d.ts +0 -315
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
9
9
  import { uuidV4 } from '../../utils';
10
- import { IterableEntryBase } from "../base";
10
+ import { IterableEntryBase } from '../base';
11
11
  import { IGraph } from '../../interfaces';
12
12
  import { Heap } from '../heap';
13
13
  import { Queue } from '../queue';
@@ -65,7 +65,9 @@ export abstract class AbstractGraph<
65
65
  E = any,
66
66
  VO extends AbstractVertex<V> = AbstractVertex<V>,
67
67
  EO extends AbstractEdge<E> = AbstractEdge<E>
68
- > extends IterableEntryBase<VertexKey, V | undefined> implements IGraph<V, E, VO, EO> {
68
+ >
69
+ extends IterableEntryBase<VertexKey, V | undefined>
70
+ implements IGraph<V, E, VO, EO> {
69
71
  constructor() {
70
72
  super();
71
73
  }
@@ -165,7 +167,7 @@ export abstract class AbstractGraph<
165
167
 
166
168
  isVertexKey(potentialKey: any): potentialKey is VertexKey {
167
169
  const potentialKeyType = typeof potentialKey;
168
- return potentialKeyType === "string" || potentialKeyType === "number"
170
+ return potentialKeyType === 'string' || potentialKeyType === 'number';
169
171
  }
170
172
 
171
173
  /**
@@ -662,7 +664,6 @@ export abstract class AbstractGraph<
662
664
  getMinDist: boolean = false,
663
665
  genPaths: boolean = false
664
666
  ): DijkstraResult<VO> {
665
-
666
667
  let minDist = Infinity;
667
668
  let minDest: VO | undefined = undefined;
668
669
  let minPath: VO[] = [];
@@ -1170,7 +1171,10 @@ export abstract class AbstractGraph<
1170
1171
 
1171
1172
  const dfs = (vertex: VO, currentPath: VertexKey[], visited: Set<VO>) => {
1172
1173
  if (visited.has(vertex)) {
1173
- if ((!isInclude2Cycle && currentPath.length > 2 || isInclude2Cycle && currentPath.length >= 2) && currentPath[0] === vertex.key) {
1174
+ if (
1175
+ ((!isInclude2Cycle && currentPath.length > 2) || (isInclude2Cycle && currentPath.length >= 2)) &&
1176
+ currentPath[0] === vertex.key
1177
+ ) {
1174
1178
  cycles.push([...currentPath]);
1175
1179
  }
1176
1180
  return;
@@ -1195,18 +1199,16 @@ export abstract class AbstractGraph<
1195
1199
  const uniqueCycles = new Map<string, VertexKey[]>();
1196
1200
 
1197
1201
  for (const cycle of cycles) {
1198
- const sorted = [...cycle].sort().toString()
1202
+ const sorted = [...cycle].sort().toString();
1199
1203
 
1200
- if (uniqueCycles.has(sorted)) continue
1204
+ if (uniqueCycles.has(sorted)) continue;
1201
1205
  else {
1202
- uniqueCycles.set(sorted, cycle)
1206
+ uniqueCycles.set(sorted, cycle);
1203
1207
  }
1204
1208
  }
1205
1209
 
1206
1210
  // Convert the unique cycles back to an array
1207
- return [...uniqueCycles].map(cycleString =>
1208
- cycleString[1]
1209
- );
1211
+ return [...uniqueCycles].map(cycleString => cycleString[1]);
1210
1212
  }
1211
1213
 
1212
1214
  /**
@@ -182,7 +182,6 @@ export class DirectedGraph<
182
182
  * Space Complexity: O(1)
183
183
  */
184
184
 
185
-
186
185
  /**
187
186
  * Time Complexity: O(E) where E is the number of edgeMap
188
187
  * Space Complexity: O(1)
@@ -248,7 +247,7 @@ export class DirectedGraph<
248
247
  vertexKey = vertexOrKey;
249
248
  } else {
250
249
  vertex = vertexOrKey;
251
- vertexKey = this._getVertexKey(vertexOrKey)
250
+ vertexKey = this._getVertexKey(vertexOrKey);
252
251
  }
253
252
 
254
253
  if (vertex) {
@@ -600,7 +599,6 @@ export class DirectedGraph<
600
599
  }
601
600
  }
602
601
 
603
-
604
602
  /**
605
603
  * Time Complexity: O(1)
606
604
  * Space Complexity: O(1)
@@ -84,7 +84,12 @@ export class MapGraph<
84
84
  * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
85
85
  * @returns The method is returning a new instance of the `MapVertex` class, casted as type `VO`.
86
86
  */
87
- override createVertex(key: VertexKey, value?: V, lat: number = this.originCoord[0], long: number = this.originCoord[1]): VO {
87
+ override createVertex(
88
+ key: VertexKey,
89
+ value?: V,
90
+ lat: number = this.originCoord[0],
91
+ long: number = this.originCoord[1]
92
+ ): VO {
88
93
  return new MapVertex(key, value, lat, long) as VO;
89
94
  }
90
95
 
@@ -162,7 +162,6 @@ export class UndirectedGraph<
162
162
  * Space Complexity: O(1)
163
163
  */
164
164
 
165
-
166
165
  /**
167
166
  * Time Complexity: O(E), where E is the number of edgeMap incident to the given vertex.
168
167
  * Space Complexity: O(1)
@@ -192,7 +191,6 @@ export class UndirectedGraph<
192
191
 
193
192
  if (oneSide && otherSide) {
194
193
  return this.deleteEdgeBetween(oneSide, otherSide);
195
-
196
194
  } else {
197
195
  return;
198
196
  }
@@ -220,10 +218,10 @@ export class UndirectedGraph<
220
218
  vertexKey = vertexOrKey;
221
219
  } else {
222
220
  vertex = vertexOrKey;
223
- vertexKey = this._getVertexKey(vertexOrKey)
221
+ vertexKey = this._getVertexKey(vertexOrKey);
224
222
  }
225
223
 
226
- const neighbors = this.getNeighbors(vertexOrKey)
224
+ const neighbors = this.getNeighbors(vertexOrKey);
227
225
 
228
226
  if (vertex) {
229
227
  neighbors.forEach(neighbor => {
@@ -234,9 +232,8 @@ export class UndirectedGraph<
234
232
  });
235
233
  this._edgeMap.set(neighbor, restEdges);
236
234
  }
237
- })
235
+ });
238
236
  this._edgeMap.delete(vertex);
239
-
240
237
  }
241
238
 
242
239
  return this._vertexMap.delete(vertexKey);
@@ -27,9 +27,12 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
27
27
  * @param [options] - The `options` parameter is an optional object that can contain additional
28
28
  * configuration options for the constructor. In this case, it has one property:
29
29
  */
30
- constructor(elements: Iterable<[K, V]> = [], options?: {
31
- hashFn: (key: K) => string
32
- }) {
30
+ constructor(
31
+ elements: Iterable<[K, V]> = [],
32
+ options?: {
33
+ hashFn: (key: K) => string;
34
+ }
35
+ ) {
33
36
  super();
34
37
  if (options) {
35
38
  const { hashFn } = options;
@@ -73,7 +76,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
73
76
  this._size++;
74
77
  }
75
78
  this._objMap.set(key, value);
76
-
77
79
  } else {
78
80
  const strKey = this._getNoObjKey(key);
79
81
  if (this._store[strKey] === undefined) {
@@ -137,7 +139,7 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
137
139
  delete(key: K): boolean {
138
140
  if (this._isObjKey(key)) {
139
141
  if (this._objMap.has(key)) {
140
- this._size--
142
+ this._size--;
141
143
  }
142
144
 
143
145
  return this._objMap.delete(key);
@@ -235,19 +237,19 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
235
237
 
236
238
  protected _hashFn: (key: K) => string = (key: K) => String(key);
237
239
 
238
- protected _isObjKey(key: any): key is (object | ((...args: any[]) => any)) {
240
+ protected _isObjKey(key: any): key is object | ((...args: any[]) => any) {
239
241
  const keyType = typeof key;
240
- return (keyType === 'object' || keyType === 'function') && key !== null
242
+ return (keyType === 'object' || keyType === 'function') && key !== null;
241
243
  }
242
244
 
243
245
  protected _getNoObjKey(key: K): string {
244
246
  const keyType = typeof key;
245
247
 
246
248
  let strKey: string;
247
- if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
249
+ if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
248
250
  strKey = this._hashFn(key);
249
251
  } else {
250
- if (keyType === "number") {
252
+ if (keyType === 'number') {
251
253
  // TODO numeric key should has its own hash
252
254
  strKey = <string>key;
253
255
  } else {
@@ -264,7 +266,6 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
264
266
  * 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
265
267
  */
266
268
  export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
267
-
268
269
  protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
269
270
  protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
270
271
  protected _head: HashMapLinkedNode<K, V | undefined>;
@@ -273,12 +274,13 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
273
274
  protected _hashFn: (key: K) => string;
274
275
  protected _objHashFn: (key: K) => object;
275
276
 
276
-
277
- constructor(elements?: Iterable<[K, V]>, options: HashMapOptions<K> = {
278
-
279
- hashFn: (key: K) => String(key),
280
- objHashFn: (key: K) => (<object>key)
281
- }) {
277
+ constructor(
278
+ elements?: Iterable<[K, V]>,
279
+ options: HashMapOptions<K> = {
280
+ hashFn: (key: K) => String(key),
281
+ objHashFn: (key: K) => <object>key
282
+ }
283
+ ) {
282
284
  super();
283
285
  this._sentinel = <HashMapLinkedNode<K, V>>{};
284
286
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
@@ -31,13 +31,13 @@ export class Heap<E = any> extends IterableElementBase<E> {
31
31
  } else {
32
32
  return a - b;
33
33
  }
34
- }
34
+ };
35
35
  if (options) {
36
- this.options = options
36
+ this.options = options;
37
37
  } else {
38
38
  this.options = {
39
39
  comparator: defaultComparator
40
- }
40
+ };
41
41
  }
42
42
 
43
43
  if (elements) {
@@ -225,7 +225,8 @@ export class Heap<E = any> extends IterableElementBase<E> {
225
225
 
226
226
  // Auxiliary recursive function, traverses the binary heap according to the traversal order
227
227
  const _dfs = (index: number) => {
228
- const left = 2 * index + 1, right = left + 1;
228
+ const left = 2 * index + 1,
229
+ right = left + 1;
229
230
  if (index < this.size) {
230
231
  if (order === 'in') {
231
232
  _dfs(left);
@@ -380,7 +381,6 @@ export class Heap<E = any> extends IterableElementBase<E> {
380
381
  * original Heap.
381
382
  */
382
383
  map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T> {
383
-
384
384
  const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
385
385
  let index = 0;
386
386
  for (const el of this) {
@@ -432,13 +432,10 @@ export class Heap<E = any> extends IterableElementBase<E> {
432
432
  protected _sinkDown(index: number, halfLength: number): boolean {
433
433
  const element = this.elements[index];
434
434
  while (index < halfLength) {
435
- let left = index << 1 | 1;
435
+ let left = (index << 1) | 1;
436
436
  const right = left + 1;
437
437
  let minItem = this.elements[left];
438
- if (
439
- right < this.elements.length &&
440
- this.options.comparator(minItem, this.elements[right]) > 0
441
- ) {
438
+ if (right < this.elements.length && this.options.comparator(minItem, this.elements[right]) > 0) {
442
439
  left = right;
443
440
  minItem = this.elements[right];
444
441
  }
@@ -29,7 +29,8 @@ export class MaxHeap<E = any> extends Heap<E> {
29
29
  return b - a;
30
30
  }
31
31
  }
32
- }) {
32
+ }
33
+ ) {
33
34
  super(elements, options);
34
35
  }
35
36
  }
@@ -29,7 +29,8 @@ export class MinHeap<E = any> extends Heap<E> {
29
29
  return a - b;
30
30
  }
31
31
  }
32
- }) {
32
+ }
33
+ ) {
33
34
  super(elements, options);
34
35
  }
35
36
  }
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from "../../types";
9
- import { IterableElementBase } from "../base";
8
+ import type { ElementCallback } from '../../types';
9
+ import { IterableElementBase } from '../base';
10
10
 
11
11
  export class SinglyLinkedListNode<E = any> {
12
12
  value: E;
@@ -715,7 +715,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
715
715
  return filteredList;
716
716
  }
717
717
 
718
-
719
718
  /**
720
719
  * Time Complexity: O(n), where n is the number of elements in the linked list.
721
720
  * Space Complexity: O(n)
@@ -1,4 +1,2 @@
1
1
  export * from './matrix';
2
- export * from './vector2d';
3
- export * from './matrix2d';
4
2
  export * from './navigator';