linked-list-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.
- package/dist/data-structures/base/index.d.ts +1 -0
- package/dist/data-structures/base/index.js +17 -0
- package/dist/data-structures/base/iterable-base.d.ts +232 -0
- package/dist/data-structures/base/iterable-base.js +312 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -69
- package/dist/data-structures/binary-tree/binary-tree.js +78 -129
- package/dist/data-structures/graph/abstract-graph.d.ts +44 -6
- package/dist/data-structures/graph/abstract-graph.js +50 -27
- package/dist/data-structures/hash/hash-map.d.ts +59 -100
- package/dist/data-structures/hash/hash-map.js +69 -173
- package/dist/data-structures/heap/heap.d.ts +50 -7
- package/dist/data-structures/heap/heap.js +60 -30
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
- package/dist/data-structures/linked-list/doubly-linked-list.js +46 -73
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +32 -51
- package/dist/data-structures/linked-list/singly-linked-list.js +40 -73
- package/dist/data-structures/queue/deque.d.ts +29 -51
- package/dist/data-structures/queue/deque.js +36 -71
- package/dist/data-structures/queue/queue.d.ts +49 -48
- package/dist/data-structures/queue/queue.js +69 -82
- package/dist/data-structures/stack/stack.d.ts +43 -10
- package/dist/data-structures/stack/stack.js +50 -31
- package/dist/data-structures/trie/trie.d.ts +41 -6
- package/dist/data-structures/trie/trie.js +53 -32
- package/dist/types/data-structures/base/base.d.ts +5 -0
- package/dist/types/data-structures/base/base.js +2 -0
- package/dist/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/data-structures/base/index.js +17 -0
- package/dist/types/data-structures/index.d.ts +1 -0
- package/dist/types/data-structures/index.js +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -0
- package/src/data-structures/base/iterable-base.ts +329 -0
- package/src/data-structures/binary-tree/binary-tree.ts +82 -138
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +76 -185
- package/src/data-structures/heap/heap.ts +63 -36
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
- package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
- package/src/data-structures/queue/deque.ts +40 -82
- package/src/data-structures/queue/queue.ts +72 -87
- package/src/data-structures/stack/stack.ts +53 -34
- package/src/data-structures/trie/trie.ts +58 -35
- package/src/types/data-structures/base/base.ts +6 -0
- package/src/types/data-structures/base/index.ts +1 -0
- 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
|
-
|
|
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
|
-
*
|
|
71
|
-
*
|
|
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:
|
|
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:
|
|
109
|
+
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): HashMap<K, V>;
|
|
110
|
+
print(): void;
|
|
144
111
|
/**
|
|
145
|
-
* The
|
|
146
|
-
*
|
|
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
|
-
|
|
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)
|
|
282
|
-
* Space Complexity: O(
|
|
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
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* `
|
|
295
|
-
*
|
|
296
|
-
*
|
|
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:
|
|
256
|
+
filter(predicate: PairCallback<K, V, boolean>, thisArg?: any): LinkedHashMap<K, V>;
|
|
299
257
|
/**
|
|
300
|
-
*
|
|
301
|
-
*
|
|
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
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
* @param
|
|
314
|
-
*
|
|
315
|
-
* the
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
139
|
-
*
|
|
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
|
|
270
|
-
*
|
|
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
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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)
|
|
595
|
-
* Space Complexity: O(
|
|
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
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
* `
|
|
615
|
-
*
|
|
616
|
-
*
|
|
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(
|
|
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
|
-
*
|
|
631
|
-
*
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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)
|