data-structure-typed 1.46.2 → 1.46.4

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 (97) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +16 -16
  3. package/benchmark/report.html +1 -46
  4. package/benchmark/report.json +8 -389
  5. package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -116
  6. package/dist/cjs/data-structures/hash/hash-map.js +127 -254
  7. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  8. package/dist/cjs/data-structures/hash/index.d.ts +0 -4
  9. package/dist/cjs/data-structures/hash/index.js +0 -4
  10. package/dist/cjs/data-structures/hash/index.js.map +1 -1
  11. package/dist/cjs/data-structures/queue/deque.d.ts +6 -74
  12. package/dist/cjs/data-structures/queue/deque.js +20 -142
  13. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  14. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -0
  15. package/dist/cjs/types/data-structures/hash/index.d.ts +0 -4
  16. package/dist/cjs/types/data-structures/hash/index.js +0 -4
  17. package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
  18. package/dist/cjs/utils/utils.d.ts +1 -1
  19. package/dist/cjs/utils/utils.js +3 -3
  20. package/dist/cjs/utils/utils.js.map +1 -1
  21. package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -116
  22. package/dist/mjs/data-structures/hash/hash-map.js +129 -257
  23. package/dist/mjs/data-structures/hash/index.d.ts +0 -4
  24. package/dist/mjs/data-structures/hash/index.js +0 -4
  25. package/dist/mjs/data-structures/queue/deque.d.ts +6 -74
  26. package/dist/mjs/data-structures/queue/deque.js +16 -140
  27. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -0
  28. package/dist/mjs/types/data-structures/hash/index.d.ts +0 -4
  29. package/dist/mjs/types/data-structures/hash/index.js +0 -4
  30. package/dist/mjs/utils/utils.d.ts +1 -1
  31. package/dist/mjs/utils/utils.js +1 -1
  32. package/dist/umd/data-structure-typed.js +169 -531
  33. package/dist/umd/data-structure-typed.min.js +1 -1
  34. package/dist/umd/data-structure-typed.min.js.map +1 -1
  35. package/package.json +4 -3
  36. package/src/data-structures/hash/hash-map.ts +154 -275
  37. package/src/data-structures/hash/index.ts +0 -4
  38. package/src/data-structures/queue/deque.ts +22 -161
  39. package/src/types/data-structures/hash/hash-map.ts +6 -0
  40. package/src/types/data-structures/hash/index.ts +0 -4
  41. package/src/utils/utils.ts +1 -1
  42. package/test/performance/data-structures/hash/hash-map.test.ts +86 -11
  43. package/test/performance/reportor.ts +6 -3
  44. package/test/unit/data-structures/hash/hash-map.test.ts +36 -5
  45. package/test/unit/data-structures/queue/deque.test.ts +28 -0
  46. package/dist/cjs/data-structures/hash/coordinate-map.d.ts +0 -44
  47. package/dist/cjs/data-structures/hash/coordinate-map.js +0 -63
  48. package/dist/cjs/data-structures/hash/coordinate-map.js.map +0 -1
  49. package/dist/cjs/data-structures/hash/coordinate-set.d.ts +0 -36
  50. package/dist/cjs/data-structures/hash/coordinate-set.js +0 -53
  51. package/dist/cjs/data-structures/hash/coordinate-set.js.map +0 -1
  52. package/dist/cjs/data-structures/hash/tree-map.d.ts +0 -2
  53. package/dist/cjs/data-structures/hash/tree-map.js +0 -7
  54. package/dist/cjs/data-structures/hash/tree-map.js.map +0 -1
  55. package/dist/cjs/data-structures/hash/tree-set.d.ts +0 -2
  56. package/dist/cjs/data-structures/hash/tree-set.js +0 -7
  57. package/dist/cjs/data-structures/hash/tree-set.js.map +0 -1
  58. package/dist/cjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
  59. package/dist/cjs/types/data-structures/hash/coordinate-map.js +0 -3
  60. package/dist/cjs/types/data-structures/hash/coordinate-map.js.map +0 -1
  61. package/dist/cjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
  62. package/dist/cjs/types/data-structures/hash/coordinate-set.js +0 -3
  63. package/dist/cjs/types/data-structures/hash/coordinate-set.js.map +0 -1
  64. package/dist/cjs/types/data-structures/hash/tree-map.d.ts +0 -1
  65. package/dist/cjs/types/data-structures/hash/tree-map.js +0 -3
  66. package/dist/cjs/types/data-structures/hash/tree-map.js.map +0 -1
  67. package/dist/cjs/types/data-structures/hash/tree-set.d.ts +0 -1
  68. package/dist/cjs/types/data-structures/hash/tree-set.js +0 -3
  69. package/dist/cjs/types/data-structures/hash/tree-set.js.map +0 -1
  70. package/dist/mjs/data-structures/hash/coordinate-map.d.ts +0 -44
  71. package/dist/mjs/data-structures/hash/coordinate-map.js +0 -58
  72. package/dist/mjs/data-structures/hash/coordinate-set.d.ts +0 -36
  73. package/dist/mjs/data-structures/hash/coordinate-set.js +0 -48
  74. package/dist/mjs/data-structures/hash/tree-map.d.ts +0 -2
  75. package/dist/mjs/data-structures/hash/tree-map.js +0 -2
  76. package/dist/mjs/data-structures/hash/tree-set.d.ts +0 -2
  77. package/dist/mjs/data-structures/hash/tree-set.js +0 -2
  78. package/dist/mjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
  79. package/dist/mjs/types/data-structures/hash/coordinate-map.js +0 -1
  80. package/dist/mjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
  81. package/dist/mjs/types/data-structures/hash/coordinate-set.js +0 -1
  82. package/dist/mjs/types/data-structures/hash/tree-map.d.ts +0 -1
  83. package/dist/mjs/types/data-structures/hash/tree-map.js +0 -1
  84. package/dist/mjs/types/data-structures/hash/tree-set.d.ts +0 -1
  85. package/dist/mjs/types/data-structures/hash/tree-set.js +0 -1
  86. package/src/data-structures/hash/coordinate-map.ts +0 -63
  87. package/src/data-structures/hash/coordinate-set.ts +0 -52
  88. package/src/data-structures/hash/tree-map.ts +0 -2
  89. package/src/data-structures/hash/tree-set.ts +0 -2
  90. package/src/types/data-structures/hash/coordinate-map.ts +0 -1
  91. package/src/types/data-structures/hash/coordinate-set.ts +0 -1
  92. package/src/types/data-structures/hash/tree-map.ts +0 -1
  93. package/src/types/data-structures/hash/tree-set.ts +0 -1
  94. package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
  95. package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
  96. package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -74
  97. package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -66
@@ -5,87 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
8
+ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
9
9
  /**
10
10
  * Deque can provide random access with O(1) time complexity
11
11
  * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
12
12
  * Deque may experience performance jitter, but DoublyLinkedList will not
13
13
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
14
14
  */
15
- export class DequeIterator {
16
- iterateDirection;
17
- index;
18
- deque;
19
- /**
20
- * The constructor initializes the index, iterate direction, and prev/next functions for a
21
- * DequeIterator object.
22
- * @param {number} index - The index parameter represents the current index position of the iterator
23
- * within the deque. It is a number that indicates the position of the element that the iterator is
24
- * currently pointing to.
25
- * @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
26
- * double-ended queue data structure, which allows elements to be added or removed from both ends.
27
- * @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
28
- * the direction in which the iterator should iterate over the elements of the `deque`. It has a
29
- * default value of `IterateDirection.DEFAULT`.
30
- * @returns The constructor is not returning anything. It is used to initialize the properties of the
31
- * object being created.
32
- */
33
- constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
34
- this.index = index;
35
- this.iterateDirection = iterateDirection;
36
- if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
37
- this.prev = function () {
38
- if (this.index === 0) {
39
- throwRangeError();
40
- }
41
- this.index -= 1;
42
- return this;
43
- };
44
- this.next = function () {
45
- if (this.index === this.deque.size) {
46
- throwRangeError();
47
- }
48
- this.index += 1;
49
- return this;
50
- };
51
- }
52
- else {
53
- this.prev = function () {
54
- if (this.index === this.deque.size - 1) {
55
- throwRangeError();
56
- }
57
- this.index += 1;
58
- return this;
59
- };
60
- this.next = function () {
61
- if (this.index === -1) {
62
- throwRangeError();
63
- }
64
- this.index -= 1;
65
- return this;
66
- };
67
- }
68
- this.deque = deque;
69
- }
70
- get current() {
71
- return this.deque.getAt(this.index);
72
- }
73
- set current(newElement) {
74
- this.deque.setAt(this.index, newElement);
75
- }
76
- isAccessible() {
77
- return this.index !== this.deque.size;
78
- }
79
- prev() {
80
- return this;
81
- }
82
- next() {
83
- return this;
84
- }
85
- clone() {
86
- return new DequeIterator(this.index, this.deque, this.iterateDirection);
87
- }
88
- }
89
15
  export class Deque {
90
16
  _bucketFirst = 0;
91
17
  _firstInBucket = 0;
@@ -220,35 +146,25 @@ export class Deque {
220
146
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
221
147
  }
222
148
  /**
223
- * The `begin()` function returns a new iterator for a deque starting from the first element.
224
- * @returns A new instance of the DequeIterator class is being returned.
225
- */
226
- begin() {
227
- return new DequeIterator(0, this);
228
- }
229
- /**
230
- * The `end()` function returns a new `DequeIterator` object with the size and reference to the
231
- * current deque.
232
- * @returns A new instance of the DequeIterator class is being returned.
149
+ * The below function is a generator that yields elements from a collection one by one.
233
150
  */
234
- end() {
235
- return new DequeIterator(this.size, this);
236
- }
237
- /**
238
- * The reverseBegin function returns a new DequeIterator object that starts at the last element of
239
- * the deque and iterates in reverse direction.
240
- * @returns A new instance of the DequeIterator class is being returned.
241
- */
242
- reverseBegin() {
243
- return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
151
+ *begin() {
152
+ let index = 0;
153
+ while (index < this.size) {
154
+ yield this.getAt(index);
155
+ index++;
156
+ }
244
157
  }
245
158
  /**
246
- * The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
247
- * Deque in reverse order.
248
- * @returns A new instance of the DequeIterator class is being returned.
159
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
160
+ * the last element.
249
161
  */
250
- reverseEnd() {
251
- return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */);
162
+ *reverseBegin() {
163
+ let index = this.size - 1;
164
+ while (index >= 0) {
165
+ yield this.getAt(index);
166
+ index--;
167
+ }
252
168
  }
253
169
  /**
254
170
  * Time Complexity - Amortized O(1) (possible reallocation)
@@ -555,46 +471,6 @@ export class Deque {
555
471
  this.cut(index - 1);
556
472
  return this.size;
557
473
  }
558
- /**
559
- * Time Complexity: O(n)
560
- * Space Complexity: O(1)
561
- */
562
- /**
563
- * Time Complexity: O(n)
564
- * Space Complexity: O(1)
565
- *
566
- * The function deletes an element from a deque using an iterator and returns the next iterator.
567
- * @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
568
- * that is used to iterate over elements in a deque (double-ended queue).
569
- * @returns the updated iterator after deleting an element from the deque.
570
- */
571
- deleteByIterator(iter) {
572
- const index = iter.index;
573
- this.deleteAt(index);
574
- iter = iter.next();
575
- return iter;
576
- }
577
- /**
578
- * Time Complexity: O(n)
579
- * Space Complexity: O(1)
580
- */
581
- /**
582
- * Time Complexity: O(n)
583
- * Space Complexity: O(1)
584
- *
585
- * The function `findIterator` searches for an element in a deque and returns an iterator pointing to
586
- * the element if found, otherwise it returns an iterator pointing to the end of the deque.
587
- * @param {E} element - The `element` parameter is the element that you want to find in the deque.
588
- * @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
589
- */
590
- findIterator(element) {
591
- for (let i = 0; i < this.size; ++i) {
592
- if (this.getAt(i) === element) {
593
- return new DequeIterator(i, this);
594
- }
595
- }
596
- return this.end();
597
- }
598
474
  /**
599
475
  * Time Complexity: O(n)
600
476
  * Space Complexity: O(1)
@@ -4,3 +4,8 @@ export type HashMapLinkedNode<K, V> = {
4
4
  next: HashMapLinkedNode<K, V>;
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
+ export type HashMapOptions<K, V> = {
8
+ elements: Iterable<[K, V]>;
9
+ hashFn: (key: K) => string;
10
+ objHashFn: (key: K) => WeakKey;
11
+ };
@@ -1,7 +1,3 @@
1
- export * from './coordinate-map';
2
- export * from './coordinate-set';
3
1
  export * from './hash-map';
4
2
  export * from './hash-table';
5
- export * from './tree-map';
6
- export * from './tree-set';
7
3
  export type HashFunction<K> = (key: K) => number;
@@ -1,6 +1,2 @@
1
- export * from './coordinate-map';
2
- export * from './coordinate-set';
3
1
  export * from './hash-map';
4
2
  export * from './hash-table';
5
- export * from './tree-map';
6
- export * from './tree-set';
@@ -20,5 +20,5 @@ export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Paramet
20
20
  export declare const getMSB: (value: number) => number;
21
21
  export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
22
22
  export declare const throwRangeError: (message?: string) => void;
23
- export declare const isObjOrFunc: (input: unknown) => input is Record<string, unknown> | ((...args: any[]) => any);
23
+ export declare const isWeakKey: (input: unknown) => input is WeakKey;
24
24
  export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
@@ -59,7 +59,7 @@ export const rangeCheck = (index, min, max, message = 'Index out of bounds.') =>
59
59
  export const throwRangeError = (message = 'The value is off-limits.') => {
60
60
  throw new RangeError(message);
61
61
  };
62
- export const isObjOrFunc = (input) => {
62
+ export const isWeakKey = (input) => {
63
63
  const inputType = typeof input;
64
64
  return (inputType === 'object' && input !== null) || inputType === 'function';
65
65
  };