data-structure-typed 2.6.0 → 2.6.1

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 (80) hide show
  1. package/.github/workflows/ci.yml +7 -2
  2. package/.github/workflows/release-package.yml +9 -2
  3. package/docs-site-docusaurus/docs/api/classes/AVLTree.md +108 -108
  4. package/docs-site-docusaurus/docs/api/classes/BST.md +101 -101
  5. package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -13
  6. package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +66 -66
  7. package/docs-site-docusaurus/docs/api/classes/Deque.md +235 -51
  8. package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +21 -21
  9. package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +231 -67
  10. package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
  11. package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
  12. package/docs-site-docusaurus/docs/api/classes/HashMap.md +14 -14
  13. package/docs-site-docusaurus/docs/api/classes/Heap.md +117 -34
  14. package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +83 -13
  15. package/docs-site-docusaurus/docs/api/classes/LinearBase.md +124 -20
  16. package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +140 -32
  17. package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
  18. package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +159 -51
  19. package/docs-site-docusaurus/docs/api/classes/MapGraph.md +20 -20
  20. package/docs-site-docusaurus/docs/api/classes/Matrix.md +23 -23
  21. package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +117 -34
  22. package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +117 -34
  23. package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -34
  24. package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +117 -34
  25. package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +117 -34
  26. package/docs-site-docusaurus/docs/api/classes/Queue.md +142 -34
  27. package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +117 -117
  28. package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +8 -8
  29. package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +158 -50
  30. package/docs-site-docusaurus/docs/api/classes/SkipList.md +21 -21
  31. package/docs-site-docusaurus/docs/api/classes/Stack.md +108 -26
  32. package/docs-site-docusaurus/docs/api/classes/TreeMap.md +33 -33
  33. package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +75 -39
  34. package/docs-site-docusaurus/docs/api/classes/TreeSet.md +301 -39
  35. package/docs-site-docusaurus/docs/api/classes/Trie.md +110 -28
  36. package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +20 -20
  37. package/package.json +45 -46
  38. package/src/common/error.ts +15 -32
  39. package/src/common/index.ts +0 -3
  40. package/src/data-structures/base/iterable-element-base.ts +0 -3
  41. package/src/data-structures/base/linear-base.ts +2 -36
  42. package/src/data-structures/binary-tree/avl-tree.ts +31 -529
  43. package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
  44. package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
  45. package/src/data-structures/binary-tree/bst.ts +158 -1082
  46. package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
  47. package/src/data-structures/binary-tree/segment-tree.ts +73 -351
  48. package/src/data-structures/binary-tree/tree-map.ts +462 -5124
  49. package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
  50. package/src/data-structures/binary-tree/tree-multi-set.ts +284 -3972
  51. package/src/data-structures/binary-tree/tree-set.ts +338 -4836
  52. package/src/data-structures/graph/abstract-graph.ts +98 -167
  53. package/src/data-structures/graph/directed-graph.ts +137 -562
  54. package/src/data-structures/graph/map-graph.ts +0 -3
  55. package/src/data-structures/graph/undirected-graph.ts +132 -511
  56. package/src/data-structures/hash/hash-map.ts +154 -582
  57. package/src/data-structures/heap/heap.ts +200 -795
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
  59. package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
  60. package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
  61. package/src/data-structures/matrix/matrix.ts +179 -518
  62. package/src/data-structures/matrix/navigator.ts +0 -1
  63. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
  64. package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
  65. package/src/data-structures/priority-queue/priority-queue.ts +1 -2
  66. package/src/data-structures/queue/deque.ts +214 -882
  67. package/src/data-structures/queue/queue.ts +102 -625
  68. package/src/data-structures/stack/stack.ts +76 -505
  69. package/src/data-structures/trie/trie.ts +98 -628
  70. package/src/types/common.ts +0 -10
  71. package/src/types/data-structures/binary-tree/bst.ts +0 -7
  72. package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
  73. package/src/types/data-structures/graph/abstract-graph.ts +0 -2
  74. package/src/types/data-structures/hash/hash-map.ts +0 -3
  75. package/src/types/data-structures/hash/index.ts +0 -1
  76. package/src/types/data-structures/matrix/navigator.ts +0 -2
  77. package/src/types/utils/utils.ts +0 -7
  78. package/src/types/utils/validate-type.ts +0 -7
  79. package/src/utils/number.ts +0 -2
  80. package/src/utils/utils.ts +0 -5
@@ -5,10 +5,7 @@
5
5
  * @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.)
6
6
  * @param message - The error message.
7
7
  */
8
- export function raise(
9
- ErrorClass: new (msg: string) => Error,
10
- message: string
11
- ): never {
8
+ export function raise(ErrorClass: new (msg: string) => Error, message: string): never {
12
9
  throw new ErrorClass(message);
13
10
  }
14
11
 
@@ -21,56 +18,42 @@ export const ERR = {
21
18
  indexOutOfRange: (index: number, min: number, max: number, ctx?: string) =>
22
19
  `${ctx ? ctx + ': ' : ''}Index ${index} is out of range [${min}, ${max}].`,
23
20
 
24
- invalidIndex: (ctx?: string) =>
25
- `${ctx ? ctx + ': ' : ''}Index must be an integer.`,
21
+ invalidIndex: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Index must be an integer.`,
26
22
 
27
23
  // Type / argument
28
- invalidArgument: (reason: string, ctx?: string) =>
29
- `${ctx ? ctx + ': ' : ''}${reason}`,
24
+ invalidArgument: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
30
25
 
31
26
  comparatorRequired: (ctx?: string) =>
32
27
  `${ctx ? ctx + ': ' : ''}Comparator is required for non-number/non-string/non-Date keys.`,
33
28
 
34
- invalidKey: (reason: string, ctx?: string) =>
35
- `${ctx ? ctx + ': ' : ''}${reason}`,
29
+ invalidKey: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
36
30
 
37
- notAFunction: (name: string, ctx?: string) =>
38
- `${ctx ? ctx + ': ' : ''}${name} must be a function.`,
31
+ notAFunction: (name: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${name} must be a function.`,
39
32
 
40
- invalidEntry: (ctx?: string) =>
41
- `${ctx ? ctx + ': ' : ''}Each entry must be a [key, value] tuple.`,
33
+ invalidEntry: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Each entry must be a [key, value] tuple.`,
42
34
 
43
- invalidNaN: (ctx?: string) =>
44
- `${ctx ? ctx + ': ' : ''}NaN is not a valid key.`,
35
+ invalidNaN: (ctx?: string) => `${ctx ? ctx + ': ' : ''}NaN is not a valid key.`,
45
36
 
46
- invalidDate: (ctx?: string) =>
47
- `${ctx ? ctx + ': ' : ''}Invalid Date key.`,
37
+ invalidDate: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Invalid Date key.`,
48
38
 
49
- reduceEmpty: (ctx?: string) =>
50
- `${ctx ? ctx + ': ' : ''}Reduce of empty structure with no initial value.`,
39
+ reduceEmpty: (ctx?: string) => `${ctx ? ctx + ': ' : ''}Reduce of empty structure with no initial value.`,
51
40
 
52
41
  callbackReturnType: (expected: string, got: string, ctx?: string) =>
53
42
  `${ctx ? ctx + ': ' : ''}Callback must return ${expected}; got ${got}.`,
54
43
 
55
44
  // State / operation
56
- invalidOperation: (reason: string, ctx?: string) =>
57
- `${ctx ? ctx + ': ' : ''}${reason}`,
45
+ invalidOperation: (reason: string, ctx?: string) => `${ctx ? ctx + ': ' : ''}${reason}`,
58
46
 
59
47
  // Matrix
60
- matrixDimensionMismatch: (op: string) =>
61
- `Matrix: Dimensions must be compatible for ${op}.`,
48
+ matrixDimensionMismatch: (op: string) => `Matrix: Dimensions must be compatible for ${op}.`,
62
49
 
63
- matrixSingular: () =>
64
- 'Matrix: Singular matrix, inverse does not exist.',
50
+ matrixSingular: () => 'Matrix: Singular matrix, inverse does not exist.',
65
51
 
66
- matrixNotSquare: () =>
67
- 'Matrix: Must be square for inversion.',
52
+ matrixNotSquare: () => 'Matrix: Must be square for inversion.',
68
53
 
69
- matrixNotRectangular: () =>
70
- 'Matrix: Must be rectangular for transposition.',
54
+ matrixNotRectangular: () => 'Matrix: Must be rectangular for transposition.',
71
55
 
72
- matrixRowMismatch: (expected: number, got: number) =>
73
- `Matrix: Expected row length ${expected}, but got ${got}.`,
56
+ matrixRowMismatch: (expected: number, got: number) => `Matrix: Expected row length ${expected}, but got ${got}.`,
74
57
 
75
58
  // Order statistic
76
59
  orderStatisticNotEnabled: (method: string, ctx?: string) =>
@@ -1,10 +1,8 @@
1
1
  export { ERR, raise } from './error';
2
-
3
2
  export enum DFSOperation {
4
3
  VISIT = 0,
5
4
  PROCESS = 1
6
5
  }
7
-
8
6
  export class Range<K> {
9
7
  constructor(
10
8
  public low: K,
@@ -15,7 +13,6 @@ export class Range<K> {
15
13
  // if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable');
16
14
  // if (low > high) throw new RangeError('low must be less than or equal to high');
17
15
  }
18
-
19
16
  // Determine whether a key is within the range
20
17
  isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
21
18
  const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
@@ -162,7 +162,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
162
162
  */
163
163
  find<S extends E>(predicate: ElementCallback<E, R, S>, thisArg?: unknown): S | undefined;
164
164
  find(predicate: ElementCallback<E, R, unknown>, thisArg?: unknown): E | undefined;
165
-
166
165
  // Implementation signature
167
166
  find(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): E | undefined {
168
167
  let index = 0;
@@ -252,7 +251,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
252
251
  let index = 0;
253
252
  const iter = this[Symbol.iterator]();
254
253
  let acc: U;
255
-
256
254
  if (arguments.length >= 2) {
257
255
  acc = initialValue as U;
258
256
  } else {
@@ -261,7 +259,6 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
261
259
  acc = first.value as unknown as U;
262
260
  index = 1;
263
261
  }
264
-
265
262
  for (const value of iter as unknown as Iterable<E>) {
266
263
  acc = callbackfn(acc, value, index++, this);
267
264
  }
@@ -75,7 +75,7 @@ export abstract class LinearBase<
75
75
  * @param options - `{ maxLen?, ... }` bounds/behavior options.
76
76
  * @remarks Time O(1), Space O(1)
77
77
  */
78
- constructor(options?: LinearBaseOptions<E, R>) {
78
+ constructor(options?: LinearBaseOptions<E, R>) {
79
79
  super(options);
80
80
  if (options) {
81
81
  const { maxLen } = options;
@@ -112,12 +112,10 @@ export abstract class LinearBase<
112
112
  if (this.length === 0) return -1;
113
113
  if (fromIndex < 0) fromIndex = this.length + fromIndex;
114
114
  if (fromIndex < 0) fromIndex = 0;
115
-
116
115
  for (let i = fromIndex; i < this.length; i++) {
117
116
  const element = this.at(i);
118
117
  if (element === searchElement) return i;
119
118
  }
120
-
121
119
  return -1;
122
120
  }
123
121
 
@@ -132,12 +130,10 @@ export abstract class LinearBase<
132
130
  if (this.length === 0) return -1;
133
131
  if (fromIndex >= this.length) fromIndex = this.length - 1;
134
132
  if (fromIndex < 0) fromIndex = this.length + fromIndex;
135
-
136
133
  for (let i = fromIndex; i >= 0; i--) {
137
134
  const element = this.at(i);
138
135
  if (element === searchElement) return i;
139
136
  }
140
-
141
137
  return -1;
142
138
  }
143
139
 
@@ -164,7 +160,6 @@ export abstract class LinearBase<
164
160
  */
165
161
  concat(...items: (E | this)[]): this {
166
162
  const newList = this.clone();
167
-
168
163
  for (const item of items) {
169
164
  if (item instanceof LinearBase) {
170
165
  newList.pushMany(item);
@@ -172,7 +167,6 @@ export abstract class LinearBase<
172
167
  newList.push(item);
173
168
  }
174
169
  }
175
-
176
170
  return newList;
177
171
  }
178
172
 
@@ -200,22 +194,18 @@ export abstract class LinearBase<
200
194
  */
201
195
  splice(start: number, deleteCount: number = 0, ...items: E[]): this {
202
196
  const removedList = this._createInstance();
203
-
204
197
  start = start < 0 ? this.length + start : start;
205
198
  start = Math.max(0, Math.min(start, this.length));
206
199
  deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
207
-
208
200
  for (let i = 0; i < deleteCount; i++) {
209
201
  const removed = this.deleteAt(start);
210
202
  if (removed !== undefined) {
211
203
  removedList.push(removed);
212
204
  }
213
205
  }
214
-
215
206
  for (let i = 0; i < items.length; i++) {
216
207
  this.addAt(start + i, items[i]);
217
208
  }
218
-
219
209
  return removedList;
220
210
  }
221
211
 
@@ -243,7 +233,6 @@ export abstract class LinearBase<
243
233
  }
244
234
 
245
235
  reduceRight(callbackfn: ReduceLinearCallback<E>): E;
246
-
247
236
  reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
248
237
 
249
238
  /**
@@ -254,7 +243,6 @@ export abstract class LinearBase<
254
243
  * @remarks Time O(n), Space O(1)
255
244
  */
256
245
  reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
257
-
258
246
  reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
259
247
  let accumulator = initialValue ?? (0 as U);
260
248
  for (let i = this.length - 1; i >= 0; i--) {
@@ -273,7 +261,6 @@ export abstract class LinearBase<
273
261
  slice(start: number = 0, end: number = this.length): this {
274
262
  start = start < 0 ? this.length + start : start;
275
263
  end = end < 0 ? this.length + end : end;
276
-
277
264
  const newList = this._createInstance();
278
265
  for (let i = start; i < end; i++) {
279
266
  newList.push(this.at(i)!);
@@ -292,15 +279,12 @@ export abstract class LinearBase<
292
279
  fill(value: E, start = 0, end = this.length): this {
293
280
  start = start < 0 ? this.length + start : start;
294
281
  end = end < 0 ? this.length + end : end;
295
-
296
282
  if (start < 0) start = 0;
297
283
  if (end > this.length) end = this.length;
298
284
  if (start >= end) return this;
299
-
300
285
  for (let i = start; i < end; i++) {
301
286
  this.setAt(i, value);
302
287
  }
303
-
304
288
  return this;
305
289
  }
306
290
 
@@ -415,7 +399,7 @@ export abstract class LinearLinkedBase<
415
399
  R = any,
416
400
  NODE extends LinkedListNode<E> = LinkedListNode<E>
417
401
  > extends LinearBase<E, R, NODE> {
418
- constructor(options?: LinearBaseOptions<E, R>) {
402
+ constructor(options?: LinearBaseOptions<E, R>) {
419
403
  super(options);
420
404
  if (options) {
421
405
  const { maxLen } = options;
@@ -433,19 +417,16 @@ export abstract class LinearLinkedBase<
433
417
  override indexOf(searchElement: E, fromIndex: number = 0): number {
434
418
  const iterator = this._getIterator();
435
419
  let current = iterator.next();
436
-
437
420
  let index = 0;
438
421
  while (index < fromIndex) {
439
422
  current = iterator.next();
440
423
  index++;
441
424
  }
442
-
443
425
  while (!current.done) {
444
426
  if (current.value === searchElement) return index;
445
427
  current = iterator.next();
446
428
  index++;
447
429
  }
448
-
449
430
  return -1;
450
431
  }
451
432
 
@@ -459,19 +440,16 @@ export abstract class LinearLinkedBase<
459
440
  override lastIndexOf(searchElement: E, fromIndex: number = this.length - 1): number {
460
441
  const iterator = this._getReverseIterator();
461
442
  let current = iterator.next();
462
-
463
443
  let index = this.length - 1;
464
444
  while (index > fromIndex) {
465
445
  current = iterator.next();
466
446
  index--;
467
447
  }
468
-
469
448
  while (!current.done) {
470
449
  if (current.value === searchElement) return index;
471
450
  current = iterator.next();
472
451
  index--;
473
452
  }
474
-
475
453
  return -1;
476
454
  }
477
455
 
@@ -483,7 +461,6 @@ export abstract class LinearLinkedBase<
483
461
  */
484
462
  override concat(...items: (E | LinearBase<E, R>)[]): this {
485
463
  const newList = this.clone();
486
-
487
464
  for (const item of items) {
488
465
  if (item instanceof LinearBase) {
489
466
  newList.pushMany(item);
@@ -491,7 +468,6 @@ export abstract class LinearLinkedBase<
491
468
  newList.push(item);
492
469
  }
493
470
  }
494
-
495
471
  return newList;
496
472
  }
497
473
 
@@ -505,7 +481,6 @@ export abstract class LinearLinkedBase<
505
481
  override slice(start: number = 0, end: number = this.length): this {
506
482
  start = start < 0 ? this.length + start : start;
507
483
  end = end < 0 ? this.length + end : end;
508
-
509
484
  const newList = this._createInstance();
510
485
  const iterator = this._getIterator();
511
486
  let current = iterator.next();
@@ -518,7 +493,6 @@ export abstract class LinearLinkedBase<
518
493
  newList.push(current.value);
519
494
  current = iterator.next();
520
495
  }
521
-
522
496
  return newList;
523
497
  }
524
498
 
@@ -532,15 +506,12 @@ export abstract class LinearLinkedBase<
532
506
  */
533
507
  override splice(start: number, deleteCount: number = 0, ...items: E[]): this {
534
508
  const removedList = this._createInstance();
535
-
536
509
  start = start < 0 ? this.length + start : start;
537
510
  start = Math.max(0, Math.min(start, this.length));
538
511
  deleteCount = Math.max(0, deleteCount);
539
-
540
512
  let currentIndex = 0;
541
513
  let currentNode: NODE | undefined = undefined;
542
514
  let previousNode: NODE | undefined = undefined;
543
-
544
515
  const iterator = this._getNodeIterator();
545
516
  for (const node of iterator) {
546
517
  if (currentIndex === start) {
@@ -550,14 +521,12 @@ export abstract class LinearLinkedBase<
550
521
  previousNode = node;
551
522
  currentIndex++;
552
523
  }
553
-
554
524
  for (let i = 0; i < deleteCount && currentNode; i++) {
555
525
  removedList.push(currentNode.value);
556
526
  const nextNode = currentNode.next;
557
527
  this.delete(currentNode);
558
528
  currentNode = nextNode as NODE;
559
529
  }
560
-
561
530
  for (let i = 0; i < items.length; i++) {
562
531
  if (previousNode) {
563
532
  this.addAfter(previousNode, items[i]);
@@ -567,12 +536,10 @@ export abstract class LinearLinkedBase<
567
536
  previousNode = this._getNodeIterator().next().value;
568
537
  }
569
538
  }
570
-
571
539
  return removedList;
572
540
  }
573
541
 
574
542
  override reduceRight(callbackfn: ReduceLinearCallback<E>): E;
575
-
576
543
  override reduceRight(callbackfn: ReduceLinearCallback<E>, initialValue: E): E;
577
544
 
578
545
  /**
@@ -583,7 +550,6 @@ export abstract class LinearLinkedBase<
583
550
  * @remarks Time O(n), Space O(1)
584
551
  */
585
552
  override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue: U): U;
586
-
587
553
  override reduceRight<U>(callbackfn: ReduceLinearCallback<E, U>, initialValue?: U): U {
588
554
  let accumulator = initialValue ?? (0 as U);
589
555
  let index = this.length - 1;