avl-tree-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 (49) hide show
  1. package/dist/data-structures/base/index.d.ts +1 -0
  2. package/dist/data-structures/base/index.js +17 -0
  3. package/dist/data-structures/base/iterable-base.d.ts +232 -0
  4. package/dist/data-structures/base/iterable-base.js +312 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
  6. package/dist/data-structures/binary-tree/binary-tree.js +78 -129
  7. package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
  8. package/dist/data-structures/graph/abstract-graph.js +50 -27
  9. package/dist/data-structures/hash/hash-map.d.ts +59 -100
  10. package/dist/data-structures/hash/hash-map.js +69 -173
  11. package/dist/data-structures/heap/heap.d.ts +50 -7
  12. package/dist/data-structures/heap/heap.js +60 -30
  13. package/dist/data-structures/index.d.ts +1 -0
  14. package/dist/data-structures/index.js +1 -0
  15. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  16. package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
  17. package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  18. package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
  19. package/dist/data-structures/queue/deque.d.ts +29 -51
  20. package/dist/data-structures/queue/deque.js +36 -71
  21. package/dist/data-structures/queue/queue.d.ts +49 -48
  22. package/dist/data-structures/queue/queue.js +69 -82
  23. package/dist/data-structures/stack/stack.d.ts +43 -10
  24. package/dist/data-structures/stack/stack.js +50 -31
  25. package/dist/data-structures/trie/trie.d.ts +41 -6
  26. package/dist/data-structures/trie/trie.js +53 -32
  27. package/dist/types/data-structures/base/base.d.ts +5 -0
  28. package/dist/types/data-structures/base/base.js +2 -0
  29. package/dist/types/data-structures/base/index.d.ts +1 -0
  30. package/dist/types/data-structures/base/index.js +17 -0
  31. package/dist/types/data-structures/index.d.ts +1 -0
  32. package/dist/types/data-structures/index.js +1 -0
  33. package/package.json +2 -2
  34. package/src/data-structures/base/index.ts +1 -0
  35. package/src/data-structures/base/iterable-base.ts +329 -0
  36. package/src/data-structures/binary-tree/binary-tree.ts +82 -138
  37. package/src/data-structures/graph/abstract-graph.ts +55 -28
  38. package/src/data-structures/hash/hash-map.ts +76 -185
  39. package/src/data-structures/heap/heap.ts +63 -36
  40. package/src/data-structures/index.ts +1 -0
  41. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  42. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  43. package/src/data-structures/queue/deque.ts +40 -82
  44. package/src/data-structures/queue/queue.ts +72 -87
  45. package/src/data-structures/stack/stack.ts +53 -34
  46. package/src/data-structures/trie/trie.ts +58 -35
  47. package/src/types/data-structures/base/base.ts +6 -0
  48. package/src/types/data-structures/base/index.ts +1 -0
  49. package/src/types/data-structures/index.ts +1 -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)
@@ -9,7 +9,8 @@
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.LinkedHashMap = exports.HashMap = void 0;
11
11
  const utils_1 = require("../../utils");
12
- class HashMap {
12
+ const base_1 = require("../base");
13
+ class HashMap extends base_1.IterablePairBase {
13
14
  /**
14
15
  * The constructor function initializes a new instance of a class with optional elements and options.
15
16
  * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
@@ -19,6 +20,7 @@ class HashMap {
19
20
  * configuration options for the constructor. In this case, it has one property:
20
21
  */
21
22
  constructor(elements = [], options) {
23
+ super();
22
24
  this._store = {};
23
25
  this._objMap = new Map();
24
26
  this._size = 0;
@@ -135,95 +137,13 @@ class HashMap {
135
137
  }
136
138
  }
137
139
  /**
138
- * The function returns an iterator that yields key-value pairs from both an object store and an
139
- * object map.
140
- */
141
- *[Symbol.iterator]() {
142
- for (const node of Object.values(this._store)) {
143
- yield [node.key, node.value];
144
- }
145
- for (const node of this._objMap) {
146
- yield node;
147
- }
148
- }
149
- /**
150
- * The function returns an iterator that yields key-value pairs from the object.
151
- */
152
- *entries() {
153
- for (const item of this) {
154
- yield item;
155
- }
156
- }
157
- /**
158
- * The function `keys()` returns an iterator that yields all the keys of the object.
159
- */
160
- *keys() {
161
- for (const [key] of this) {
162
- yield key;
163
- }
164
- }
165
- *values() {
166
- for (const [, value] of this) {
167
- yield value;
168
- }
169
- }
170
- /**
171
- * The `every` function checks if every element in a HashMap satisfies a given predicate function.
172
- * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
173
- * index, and map. It is used to test each element in the map against a condition. If the predicate
174
- * function returns false for any element, the every() method will return false. If the predicate
175
- * function returns true for all
176
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
177
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
178
- * passed as the `this` value to the `predicate` function. If `thisArg` is
179
- * @returns The method is returning a boolean value. It returns true if the predicate function
180
- * returns true for every element in the map, and false otherwise.
181
- */
182
- every(predicate, thisArg) {
183
- let index = 0;
184
- for (const [key, value] of this) {
185
- if (!predicate.call(thisArg, value, key, index++, this)) {
186
- return false;
187
- }
188
- }
189
- return true;
190
- }
191
- /**
192
- * The "some" function checks if at least one element in a HashMap satisfies a given predicate.
193
- * @param predicate - The `predicate` parameter is a function that takes four arguments: `value`,
194
- * `key`, `index`, and `map`. It is used to determine whether a specific condition is met for a given
195
- * key-value pair in the `HashMap`.
196
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
197
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
198
- * passed as the `this` value to the `predicate` function. If `thisArg` is
199
- * @returns a boolean value. It returns true if the predicate function returns true for any element
200
- * in the map, and false otherwise.
140
+ * Time Complexity: O(n)
141
+ * Space Complexity: O(n)
201
142
  */
202
- some(predicate, thisArg) {
203
- let index = 0;
204
- for (const [key, value] of this) {
205
- if (predicate.call(thisArg, value, key, index++, this)) {
206
- return true;
207
- }
208
- }
209
- return false;
210
- }
211
- /**
212
- * The `forEach` function iterates over the elements of a HashMap and applies a callback function to
213
- * each element.
214
- * @param callbackfn - A function that will be called for each key-value pair in the HashMap. It
215
- * takes four parameters:
216
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
217
- * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
218
- * be passed as the `this` value inside the `callbackfn` function. If `thisArg
219
- */
220
- forEach(callbackfn, thisArg) {
221
- let index = 0;
222
- for (const [key, value] of this) {
223
- callbackfn.call(thisArg, value, key, index++, this);
224
- }
225
- }
226
143
  /**
144
+ * Time Complexity: O(n)
145
+ * Space Complexity: O(n)
146
+ *
227
147
  * The `map` function in TypeScript creates a new HashMap by applying a callback function to each
228
148
  * key-value pair in the original HashMap.
229
149
  * @param callbackfn - The callback function that will be called for each key-value pair in the
@@ -243,6 +163,13 @@ class HashMap {
243
163
  return resultMap;
244
164
  }
245
165
  /**
166
+ * Time Complexity: O(n)
167
+ * Space Complexity: O(n)
168
+ */
169
+ /**
170
+ * Time Complexity: O(n)
171
+ * Space Complexity: O(n)
172
+ *
246
173
  * The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
247
174
  * that satisfy a given predicate function.
248
175
  * @param predicate - The predicate parameter is a function that takes four arguments: value, key,
@@ -265,27 +192,20 @@ class HashMap {
265
192
  }
266
193
  return filteredMap;
267
194
  }
195
+ print() {
196
+ console.log([...this.entries()]);
197
+ }
268
198
  /**
269
- * The `reduce` function iterates over the elements of a HashMap and applies a callback function to
270
- * each element, accumulating a single value.
271
- * @param callbackfn - The callback function that will be called for each element in the HashMap. It
272
- * takes five parameters:
273
- * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
274
- * is the value that will be used as the first argument of the callback function when reducing the
275
- * elements of the map.
276
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
277
- * all the elements in the `HashMap`.
199
+ * The function returns an iterator that yields key-value pairs from both an object store and an
200
+ * object map.
278
201
  */
279
- reduce(callbackfn, initialValue) {
280
- let accumulator = initialValue;
281
- let index = 0;
282
- for (const [key, value] of this) {
283
- accumulator = callbackfn(accumulator, value, key, index++, this);
202
+ *_getIterator() {
203
+ for (const node of Object.values(this._store)) {
204
+ yield [node.key, node.value];
205
+ }
206
+ for (const node of this._objMap) {
207
+ yield node;
284
208
  }
285
- return accumulator;
286
- }
287
- print() {
288
- console.log([...this.entries()]);
289
209
  }
290
210
  _isObjKey(key) {
291
211
  const keyType = typeof key;
@@ -310,11 +230,12 @@ class HashMap {
310
230
  }
311
231
  }
312
232
  exports.HashMap = HashMap;
313
- class LinkedHashMap {
233
+ class LinkedHashMap extends base_1.IterablePairBase {
314
234
  constructor(elements, options = {
315
235
  hashFn: (key) => String(key),
316
236
  objHashFn: (key) => key
317
237
  }) {
238
+ super();
318
239
  this._noObjMap = {};
319
240
  this._objMap = new WeakMap();
320
241
  this._size = 0;
@@ -450,18 +371,6 @@ 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 @@ 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 @@ 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 @@ 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)