data-structure-typed 1.48.1 → 1.48.2

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 (114) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +182 -182
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +78 -129
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  15. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  16. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/hash/hash-map.d.ts +59 -100
  18. package/dist/cjs/data-structures/hash/hash-map.js +69 -173
  19. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  20. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  21. package/dist/cjs/data-structures/heap/heap.js +60 -30
  22. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  23. package/dist/cjs/data-structures/index.d.ts +1 -0
  24. package/dist/cjs/data-structures/index.js +1 -0
  25. package/dist/cjs/data-structures/index.js.map +1 -1
  26. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  27. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  28. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  29. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  30. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  31. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  33. package/dist/cjs/data-structures/queue/deque.js +36 -71
  34. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  35. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  36. package/dist/cjs/data-structures/queue/queue.js +69 -82
  37. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  38. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  39. package/dist/cjs/data-structures/stack/stack.js +50 -31
  40. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  41. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  42. package/dist/cjs/data-structures/trie/trie.js +53 -32
  43. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  44. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  45. package/dist/cjs/types/data-structures/base/base.js +3 -0
  46. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  47. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  48. package/dist/cjs/types/data-structures/base/index.js +18 -0
  49. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  50. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  51. package/dist/cjs/types/data-structures/index.js +1 -0
  52. package/dist/cjs/types/data-structures/index.js.map +1 -1
  53. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  54. package/dist/mjs/data-structures/base/index.js +1 -0
  55. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  56. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  57. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +36 -69
  58. package/dist/mjs/data-structures/binary-tree/binary-tree.js +78 -129
  59. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  60. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  61. package/dist/mjs/data-structures/hash/hash-map.d.ts +59 -100
  62. package/dist/mjs/data-structures/hash/hash-map.js +69 -173
  63. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  64. package/dist/mjs/data-structures/heap/heap.js +60 -30
  65. package/dist/mjs/data-structures/index.d.ts +1 -0
  66. package/dist/mjs/data-structures/index.js +1 -0
  67. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  68. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  69. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  70. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  71. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  72. package/dist/mjs/data-structures/queue/deque.js +36 -71
  73. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  74. package/dist/mjs/data-structures/queue/queue.js +66 -79
  75. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  76. package/dist/mjs/data-structures/stack/stack.js +50 -31
  77. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  78. package/dist/mjs/data-structures/trie/trie.js +53 -32
  79. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  80. package/dist/mjs/types/data-structures/base/base.js +1 -0
  81. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  82. package/dist/mjs/types/data-structures/base/index.js +1 -0
  83. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  84. package/dist/mjs/types/data-structures/index.js +1 -0
  85. package/dist/umd/data-structure-typed.js +846 -715
  86. package/dist/umd/data-structure-typed.min.js +2 -2
  87. package/dist/umd/data-structure-typed.min.js.map +1 -1
  88. package/package.json +1 -1
  89. package/src/data-structures/base/index.ts +1 -0
  90. package/src/data-structures/base/iterable-base.ts +329 -0
  91. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  92. package/src/data-structures/graph/abstract-graph.ts +55 -28
  93. package/src/data-structures/hash/hash-map.ts +76 -185
  94. package/src/data-structures/heap/heap.ts +63 -36
  95. package/src/data-structures/index.ts +1 -0
  96. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  97. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  98. package/src/data-structures/queue/deque.ts +40 -82
  99. package/src/data-structures/queue/queue.ts +72 -87
  100. package/src/data-structures/stack/stack.ts +53 -34
  101. package/src/data-structures/trie/trie.ts +58 -35
  102. package/src/types/data-structures/base/base.ts +6 -0
  103. package/src/types/data-structures/base/index.ts +1 -0
  104. package/src/types/data-structures/index.ts +1 -0
  105. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
  106. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
  107. package/test/unit/data-structures/binary-tree/bst.test.ts +10 -10
  108. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  109. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +10 -10
  110. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  111. package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
  112. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  113. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  114. package/test/unit/data-structures/queue/deque.test.ts +25 -0
@@ -5,8 +5,9 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
9
- export declare class HashMap<K = any, V = any> {
8
+ import { HashMapLinkedNode, HashMapOptions, HashMapStoreItem, PairCallback } from '../../types';
9
+ import { IterablePairBase } from "../base";
10
+ export declare class HashMap<K = any, V = any> extends IterablePairBase<K, V> {
10
11
  protected _store: {
11
12
  [key: string]: HashMapStoreItem<K, V>;
12
13
  };
@@ -67,55 +68,13 @@ export declare class HashMap<K = any, V = any> {
67
68
  */
68
69
  delete(key: K): boolean;
69
70
  /**
70
- * The function returns an iterator that yields key-value pairs from both an object store and an
71
- * object map.
72
- */
73
- [Symbol.iterator](): IterableIterator<[K, V]>;
74
- /**
75
- * The function returns an iterator that yields key-value pairs from the object.
76
- */
77
- entries(): IterableIterator<[K, V]>;
78
- /**
79
- * The function `keys()` returns an iterator that yields all the keys of the object.
71
+ * Time Complexity: O(n)
72
+ * Space Complexity: O(n)
80
73
  */
81
- keys(): IterableIterator<K>;
82
- values(): IterableIterator<V>;
83
- /**
84
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
85
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
86
- * index, and map. It is used to test each element in the map against a condition. If the predicate
87
- * function returns false for any element, the every() method will return false. If the predicate
88
- * function returns true for all
89
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
90
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
91
- * passed as the `this` value to the `predicate` function. If `thisArg` is
92
- * @returns The method is returning a boolean value. It returns true if the predicate function
93
- * returns true for every element in the map, and false otherwise.
94
- */
95
- every(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean;
96
- /**
97
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
98
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
99
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
100
- * key-value pair in the `HashMap`.
101
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
102
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
103
- * passed as the `this` value to the `predicate` function. If `thisArg` is
104
- * @returns a boolean value. It returns true if the predicate function returns true for any element
105
- * in the map, and false otherwise.
106
- */
107
- some(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): boolean;
108
- /**
109
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
110
- * each element.
111
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
112
- * takes four parameters:
113
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
114
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
115
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
116
- */
117
- forEach(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => void, thisArg?: any): void;
118
74
  /**
75
+ * Time Complexity: O(n)
76
+ * Space Complexity: O(n)
77
+ *
119
78
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
120
79
  * key-value pair in the original HashMap.
121
80
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -126,8 +85,15 @@ export declare class HashMap<K = any, V = any> {
126
85
  * @returns The `map` method is returning a new `HashMap` object with the transformed values based on
127
86
  * the provided callback function.
128
87
  */
129
- map<U>(callbackfn: (value: V, key: K, index: number, map: HashMap<K, V>) => U, thisArg?: any): HashMap<K, U>;
88
+ map<U>(callbackfn: PairCallback<K, V, U>, thisArg?: any): HashMap<K, U>;
89
+ /**
90
+ * Time Complexity: O(n)
91
+ * Space Complexity: O(n)
92
+ */
130
93
  /**
94
+ * Time Complexity: O(n)
95
+ * Space Complexity: O(n)
96
+ *
131
97
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
132
98
  * that satisfy a given predicate function.
133
99
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -140,25 +106,18 @@ export declare class HashMap<K = any, V = any> {
140
106
  * @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
141
107
  * from the original `HashMap` that pass the provided `predicate` function.
142
108
  */
143
- filter(predicate: (value: V, key: K, index: number, map: HashMap<K, V>) => boolean, thisArg?: any): HashMap<K, V>;
109
+ filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
110
+ print(): void;
144
111
  /**
145
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
146
- * each element, accumulating a single value.
147
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
148
- * takes five parameters:
149
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
150
- * is the value that will be used as the first argument of the callback function when reducing the
151
- * elements of the map.
152
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
153
- * all the elements in the `HashMap`.
112
+ * The function returns an iterator that yields key-value pairs from both an object store and an
113
+ * object map.
154
114
  */
155
- reduce<U>(callbackfn: (accumulator: U, currentValue: V, currentKey: K, index: number, map: HashMap<K, V>) => U, initialValue: U): U;
156
- print(): void;
115
+ protected _getIterator(): IterableIterator<[K, V]>;
157
116
  protected _hashFn: (key: K) => string;
158
117
  protected _isObjKey(key: any): key is (object | ((...args: any[]) => any));
159
118
  protected _getNoObjKey(key: K): string;
160
119
  }
161
- export declare class LinkedHashMap<K = any, V = any> {
120
+ export declare class LinkedHashMap<K = any, V = any> extends IterablePairBase<K, V> {
162
121
  protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
163
122
  protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
164
123
  protected _head: HashMapLinkedNode<K, V | undefined>;
@@ -211,8 +170,6 @@ export declare class LinkedHashMap<K = any, V = any> {
211
170
  set(key: K, value?: V): number;
212
171
  has(key: K): boolean;
213
172
  setMany(entries: Iterable<[K, V]>): void;
214
- keys(): K[];
215
- values(): V[];
216
173
  /**
217
174
  * Time Complexity: O(1)
218
175
  * Space Complexity: O(1)
@@ -278,53 +235,55 @@ export declare class LinkedHashMap<K = any, V = any> {
278
235
  clear(): void;
279
236
  clone(): LinkedHashMap<K, V>;
280
237
  /**
281
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
282
- * Space Complexity: O(1)
283
- *
284
- * The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
285
- * each element.
286
- * @param callback - The callback parameter is a function that will be called for each element in the
287
- * LinkedHashMap. It takes three arguments:
238
+ * Time Complexity: O(n)
239
+ * Space Complexity: O(n)
288
240
  */
289
- forEach(callback: (element: [K, V], index: number, hashMap: LinkedHashMap<K, V>) => void): void;
290
241
  /**
291
- * The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
292
- * key-value pairs that satisfy the predicate.
293
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
294
- * `map`.
295
- * @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
296
- * satisfy the given predicate function.
242
+ * Time Complexity: O(n)
243
+ * Space Complexity: O(n)
244
+ *
245
+ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
246
+ * map that satisfy a given predicate function.
247
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
248
+ * `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
249
+ * current element should be included in the filtered map or not.
250
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
251
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
252
+ * specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
253
+ * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
254
+ * `LinkedHashMap` object that satisfy the given predicate function.
297
255
  */
298
- filter(predicate: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => boolean): LinkedHashMap<K, V>;
256
+ filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
299
257
  /**
300
- * The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
301
- * by the callback.
302
- * @param callback - The `callback` parameter is a function that takes two arguments: `element` and
303
- * `map`.
304
- * @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
258
+ * Time Complexity: O(n)
259
+ * Space Complexity: O(n)
305
260
  */
306
- map<NV>(callback: (element: [K, V], index: number, map: LinkedHashMap<K, V>) => NV): LinkedHashMap<K, NV>;
307
261
  /**
308
- * The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
309
- * each element, accumulating a single value.
310
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
311
- * element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
312
- * result.
313
- * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
314
- * is the value that will be passed as the first argument to the `callback` function when reducing
315
- * the elements of the map.
316
- * @returns The `reduce` function is returning the final value of the accumulator after iterating
317
- * over all the elements in the LinkedHashMap and applying the callback function to each element.
318
- */
319
- reduce<A>(callback: (accumulator: A, element: [K, V], index: number, map: LinkedHashMap<K, V>) => A, initialValue: A): A;
262
+ * Time Complexity: O(n)
263
+ * Space Complexity: O(n)
264
+ *
265
+ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
266
+ * each key-value pair in the original map.
267
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
268
+ * in the map. It takes four arguments: the value of the current key-value pair, the key of the
269
+ * current key-value pair, the index of the current key-value pair, and the map itself. The callback
270
+ * function should
271
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
272
+ * specify the value of `this` within the callback function. If provided, the callback function will
273
+ * be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
274
+ * map
275
+ * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
276
+ * function.
277
+ */
278
+ map<NV>(callback: PairCallback<K, V, NV>, thisArg?: any): LinkedHashMap<K, NV>;
279
+ print(): void;
320
280
  /**
321
281
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
322
282
  * Space Complexity: O(1)
323
283
  *
324
284
  * The above function is an iterator that yields key-value pairs from a linked list.
325
285
  */
326
- [Symbol.iterator](): Generator<[K, V], void, unknown>;
327
- print(): void;
286
+ protected _getIterator(): Generator<[K, V], void, unknown>;
328
287
  /**
329
288
  * Time Complexity: O(1)
330
289
  * Space Complexity: O(1)
@@ -6,7 +6,8 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { isWeakKey, rangeCheck } from '../../utils';
9
- export class HashMap {
9
+ import { IterablePairBase } from "../base";
10
+ export class HashMap extends IterablePairBase {
10
11
  _store = {};
11
12
  _objMap = new Map();
12
13
  /**
@@ -18,6 +19,7 @@ export class HashMap {
18
19
  * configuration options for the constructor. In this case, it has one property:
19
20
  */
20
21
  constructor(elements = [], options) {
22
+ super();
21
23
  if (options) {
22
24
  const { hashFn } = options;
23
25
  if (hashFn) {
@@ -130,95 +132,13 @@ export class HashMap {
130
132
  }
131
133
  }
132
134
  /**
133
- * The function returns an iterator that yields key-value pairs from both an object store and an
134
- * object map.
135
- */
136
- *[Symbol.iterator]() {
137
- for (const node of Object.values(this._store)) {
138
- yield [node.key, node.value];
139
- }
140
- for (const node of this._objMap) {
141
- yield node;
142
- }
143
- }
144
- /**
145
- * The function returns an iterator that yields key-value pairs from the object.
146
- */
147
- *entries() {
148
- for (const item of this) {
149
- yield item;
150
- }
151
- }
152
- /**
153
- * The function `keys()` returns an iterator that yields all the keys of the object.
154
- */
155
- *keys() {
156
- for (const [key] of this) {
157
- yield key;
158
- }
159
- }
160
- *values() {
161
- for (const [, value] of this) {
162
- yield value;
163
- }
164
- }
165
- /**
166
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
167
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
168
- * index, and map. It is used to test each element in the map against a condition. If the predicate
169
- * function returns false for any element, the every() method will return false. If the predicate
170
- * function returns true for all
171
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
172
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
173
- * passed as the `this` value to the `predicate` function. If `thisArg` is
174
- * @returns The method is returning a boolean value. It returns true if the predicate function
175
- * returns true for every element in the map, and false otherwise.
176
- */
177
- every(predicate, thisArg) {
178
- let index = 0;
179
- for (const [key, value] of this) {
180
- if (!predicate.call(thisArg, value, key, index++, this)) {
181
- return false;
182
- }
183
- }
184
- return true;
185
- }
186
- /**
187
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
188
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
189
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
190
- * key-value pair in the `HashMap`.
191
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
192
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
193
- * passed as the `this` value to the `predicate` function. If `thisArg` is
194
- * @returns a boolean value. It returns true if the predicate function returns true for any element
195
- * in the map, and false otherwise.
135
+ * Time Complexity: O(n)
136
+ * Space Complexity: O(n)
196
137
  */
197
- some(predicate, thisArg) {
198
- let index = 0;
199
- for (const [key, value] of this) {
200
- if (predicate.call(thisArg, value, key, index++, this)) {
201
- return true;
202
- }
203
- }
204
- return false;
205
- }
206
- /**
207
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
208
- * each element.
209
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
210
- * takes four parameters:
211
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
212
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
213
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
214
- */
215
- forEach(callbackfn, thisArg) {
216
- let index = 0;
217
- for (const [key, value] of this) {
218
- callbackfn.call(thisArg, value, key, index++, this);
219
- }
220
- }
221
138
  /**
139
+ * Time Complexity: O(n)
140
+ * Space Complexity: O(n)
141
+ *
222
142
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
223
143
  * key-value pair in the original HashMap.
224
144
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -238,6 +158,13 @@ export class HashMap {
238
158
  return resultMap;
239
159
  }
240
160
  /**
161
+ * Time Complexity: O(n)
162
+ * Space Complexity: O(n)
163
+ */
164
+ /**
165
+ * Time Complexity: O(n)
166
+ * Space Complexity: O(n)
167
+ *
241
168
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
242
169
  * that satisfy a given predicate function.
243
170
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -260,27 +187,20 @@ export class HashMap {
260
187
  }
261
188
  return filteredMap;
262
189
  }
190
+ print() {
191
+ console.log([...this.entries()]);
192
+ }
263
193
  /**
264
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
265
- * each element, accumulating a single value.
266
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
267
- * takes five parameters:
268
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
269
- * is the value that will be used as the first argument of the callback function when reducing the
270
- * elements of the map.
271
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
272
- * all the elements in the `HashMap`.
194
+ * The function returns an iterator that yields key-value pairs from both an object store and an
195
+ * object map.
273
196
  */
274
- reduce(callbackfn, initialValue) {
275
- let accumulator = initialValue;
276
- let index = 0;
277
- for (const [key, value] of this) {
278
- accumulator = callbackfn(accumulator, value, key, index++, this);
197
+ *_getIterator() {
198
+ for (const node of Object.values(this._store)) {
199
+ yield [node.key, node.value];
200
+ }
201
+ for (const node of this._objMap) {
202
+ yield node;
279
203
  }
280
- return accumulator;
281
- }
282
- print() {
283
- console.log([...this.entries()]);
284
204
  }
285
205
  _hashFn = (key) => String(key);
286
206
  _isObjKey(key) {
@@ -305,7 +225,7 @@ export class HashMap {
305
225
  return strKey;
306
226
  }
307
227
  }
308
- export class LinkedHashMap {
228
+ export class LinkedHashMap extends IterablePairBase {
309
229
  _noObjMap = {};
310
230
  _objMap = new WeakMap();
311
231
  _head;
@@ -317,6 +237,7 @@ export class LinkedHashMap {
317
237
  hashFn: (key) => String(key),
318
238
  objHashFn: (key) => key
319
239
  }) {
240
+ super();
320
241
  this._sentinel = {};
321
242
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
322
243
  const { hashFn, objHashFn } = options;
@@ -450,18 +371,6 @@ export class LinkedHashMap {
450
371
  this.set(key, value);
451
372
  }
452
373
  }
453
- keys() {
454
- const keys = [];
455
- for (const [key] of this)
456
- keys.push(key);
457
- return keys;
458
- }
459
- values() {
460
- const values = [];
461
- for (const [, value] of this)
462
- values.push(value);
463
- return values;
464
- }
465
374
  /**
466
375
  * Time Complexity: O(1)
467
376
  * Space Complexity: O(1)
@@ -591,35 +500,29 @@ export class LinkedHashMap {
591
500
  return cloned;
592
501
  }
593
502
  /**
594
- * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
595
- * Space Complexity: O(1)
596
- *
597
- * The `forEach` function iterates over each element in a LinkedHashMap and executes a callback function on
598
- * each element.
599
- * @param callback - The callback parameter is a function that will be called for each element in the
600
- * LinkedHashMap. It takes three arguments:
503
+ * Time Complexity: O(n)
504
+ * Space Complexity: O(n)
601
505
  */
602
- forEach(callback) {
603
- let index = 0;
604
- let node = this._head;
605
- while (node !== this._sentinel) {
606
- callback([node.key, node.value], index++, this);
607
- node = node.next;
608
- }
609
- }
610
506
  /**
611
- * The `filter` function takes a predicate function and returns a new LinkedHashMap containing only the
612
- * key-value pairs that satisfy the predicate.
613
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
614
- * `map`.
615
- * @returns a new LinkedHashMap object that contains the key-value pairs from the original LinkedHashMap that
616
- * satisfy the given predicate function.
507
+ * Time Complexity: O(n)
508
+ * Space Complexity: O(n)
509
+ *
510
+ * The `filter` function creates a new `LinkedHashMap` containing key-value pairs from the original
511
+ * map that satisfy a given predicate function.
512
+ * @param predicate - The `predicate` parameter is a callback function that takes four arguments:
513
+ * `value`, `key`, `index`, and `this`. It should return a boolean value indicating whether the
514
+ * current element should be included in the filtered map or not.
515
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
516
+ * specify the value of `this` within the `predicate` function. It is used when you want to bind a
517
+ * specific object as the context for the `predicate` function. If `thisArg` is not provided, `this
518
+ * @returns a new `LinkedHashMap` object that contains the key-value pairs from the original
519
+ * `LinkedHashMap` object that satisfy the given predicate function.
617
520
  */
618
- filter(predicate) {
521
+ filter(predicate, thisArg) {
619
522
  const filteredMap = new LinkedHashMap();
620
523
  let index = 0;
621
524
  for (const [key, value] of this) {
622
- if (predicate([key, value], index, this)) {
525
+ if (predicate.call(thisArg, value, key, index, this)) {
623
526
  filteredMap.set(key, value);
624
527
  }
625
528
  index++;
@@ -627,42 +530,38 @@ export class LinkedHashMap {
627
530
  return filteredMap;
628
531
  }
629
532
  /**
630
- * The `map` function takes a callback function and returns a new LinkedHashMap with the values transformed
631
- * by the callback.
632
- * @param callback - The `callback` parameter is a function that takes two arguments: `element` and
633
- * `map`.
634
- * @returns a new LinkedHashMap object with the values mapped according to the provided callback function.
533
+ * Time Complexity: O(n)
534
+ * Space Complexity: O(n)
635
535
  */
636
- map(callback) {
536
+ /**
537
+ * Time Complexity: O(n)
538
+ * Space Complexity: O(n)
539
+ *
540
+ * The `map` function in TypeScript creates a new `LinkedHashMap` by applying a callback function to
541
+ * each key-value pair in the original map.
542
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
543
+ * in the map. It takes four arguments: the value of the current key-value pair, the key of the
544
+ * current key-value pair, the index of the current key-value pair, and the map itself. The callback
545
+ * function should
546
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
547
+ * specify the value of `this` within the callback function. If provided, the callback function will
548
+ * be called with `thisArg` as its `this` value. If not provided, `this` will refer to the current
549
+ * map
550
+ * @returns a new `LinkedHashMap` object with the values mapped according to the provided callback
551
+ * function.
552
+ */
553
+ map(callback, thisArg) {
637
554
  const mappedMap = new LinkedHashMap();
638
555
  let index = 0;
639
556
  for (const [key, value] of this) {
640
- const newValue = callback([key, value], index, this);
557
+ const newValue = callback.call(thisArg, value, key, index, this);
641
558
  mappedMap.set(key, newValue);
642
559
  index++;
643
560
  }
644
561
  return mappedMap;
645
562
  }
646
- /**
647
- * The `reduce` function iterates over the elements of a LinkedHashMap and applies a callback function to
648
- * each element, accumulating a single value.
649
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
650
- * element, and map. It is called for each element in the LinkedHashMap and is used to accumulate a single
651
- * result.
652
- * @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
653
- * is the value that will be passed as the first argument to the `callback` function when reducing
654
- * the elements of the map.
655
- * @returns The `reduce` function is returning the final value of the accumulator after iterating
656
- * over all the elements in the LinkedHashMap and applying the callback function to each element.
657
- */
658
- reduce(callback, initialValue) {
659
- let accumulator = initialValue;
660
- let index = 0;
661
- for (const entry of this) {
662
- accumulator = callback(accumulator, entry, index, this);
663
- index++;
664
- }
665
- return accumulator;
563
+ print() {
564
+ console.log([...this]);
666
565
  }
667
566
  /**
668
567
  * Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
@@ -670,16 +569,13 @@ export class LinkedHashMap {
670
569
  *
671
570
  * The above function is an iterator that yields key-value pairs from a linked list.
672
571
  */
673
- *[Symbol.iterator]() {
572
+ *_getIterator() {
674
573
  let node = this._head;
675
574
  while (node !== this._sentinel) {
676
575
  yield [node.key, node.value];
677
576
  node = node.next;
678
577
  }
679
578
  }
680
- print() {
681
- console.log([...this]);
682
- }
683
579
  /**
684
580
  * Time Complexity: O(1)
685
581
  * Space Complexity: O(1)
@@ -4,9 +4,10 @@
4
4
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
5
  * @license MIT License
6
6
  */
7
- import type { Comparator, DFSOrderPattern } from '../../types';
7
+ import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
8
8
  import { HeapOptions } from "../../types";
9
- export declare class Heap<E = any> {
9
+ import { IterableElementBase } from "../base";
10
+ export declare class Heap<E = any> extends IterableElementBase<E> {
10
11
  options: HeapOptions<E>;
11
12
  constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
12
13
  protected _elements: E[];
@@ -192,16 +193,58 @@ export declare class Heap<E = any> {
192
193
  * Fix the entire heap to maintain heap properties.
193
194
  */
194
195
  fix(): void;
195
- [Symbol.iterator](): Generator<E, void, unknown>;
196
- forEach(callback: (element: E, index: number, heap: this) => void): void;
197
- filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E>;
198
- map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T>;
199
- reduce<T>(callback: (accumulator: T, currentValue: E, currentIndex: number, heap: Heap<E>) => T, initialValue: T): T;
196
+ /**
197
+ * Time Complexity: O(n)
198
+ * Space Complexity: O(n)
199
+ */
200
+ /**
201
+ * Time Complexity: O(n)
202
+ * Space Complexity: O(n)
203
+ *
204
+ * The `filter` function creates a new Heap object containing elements that pass a given callback
205
+ * function.
206
+ * @param callback - The `callback` parameter is a function that will be called for each element in
207
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
208
+ * heap itself. The callback function should return a boolean value indicating whether the current
209
+ * element should be included in the filtered list
210
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
211
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
212
+ * passed as the `this` value to the `callback` function. If `thisArg` is
213
+ * @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
214
+ * the filter condition specified by the `callback` function.
215
+ */
216
+ filter(callback: ElementCallback<E, boolean>, thisArg?: any): Heap<E>;
217
+ /**
218
+ * Time Complexity: O(n)
219
+ * Space Complexity: O(n)
220
+ */
221
+ /**
222
+ * Time Complexity: O(n)
223
+ * Space Complexity: O(n)
224
+ *
225
+ * The `map` function creates a new heap by applying a callback function to each element of the
226
+ * original heap.
227
+ * @param callback - The callback parameter is a function that will be called for each element in the
228
+ * original heap. It takes three arguments: the current element, the index of the current element,
229
+ * and the original heap itself. The callback function should return a value of type T, which will be
230
+ * added to the mapped heap.
231
+ * @param comparator - The `comparator` parameter is a function that is used to compare elements in
232
+ * the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
233
+ * `b`, a positive number if `a` is greater than `b`, or
234
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
235
+ * specify the value of `this` within the callback function. It is used when you want to bind a
236
+ * specific object as the context for the callback function. If `thisArg` is not provided,
237
+ * `undefined` is used as
238
+ * @returns a new instance of the Heap class, which is created using the mapped elements from the
239
+ * original Heap.
240
+ */
241
+ map<T>(callback: ElementCallback<E, T>, comparator: Comparator<T>, thisArg?: any): Heap<T>;
200
242
  /**
201
243
  * Time Complexity: O(log n)
202
244
  * Space Complexity: O(1)
203
245
  */
204
246
  print(): void;
247
+ protected _getIterator(): Generator<E, void, unknown>;
205
248
  /**
206
249
  * Time Complexity: O(n)
207
250
  * Space Complexity: O(1)