heap-typed 1.48.0 → 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 +45 -40
- package/dist/data-structures/binary-tree/binary-tree.js +91 -88
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +12 -0
- package/dist/data-structures/binary-tree/tree-multimap.js +16 -0
- 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 +160 -44
- package/dist/data-structures/hash/hash-map.js +314 -82
- 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/hash/hash-map.d.ts +4 -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 +98 -93
- package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
- package/src/data-structures/graph/abstract-graph.ts +55 -28
- package/src/data-structures/hash/hash-map.ts +334 -83
- 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/hash/hash-map.ts +2 -0
- package/src/types/data-structures/index.ts +1 -0
|
@@ -7,13 +7,235 @@
|
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.HashMap = void 0;
|
|
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 {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a new instance of a class with optional elements and options.
|
|
16
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
|
|
17
|
+
* is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
|
|
18
|
+
* key-value pairs.
|
|
19
|
+
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
20
|
+
* configuration options for the constructor. In this case, it has one property:
|
|
21
|
+
*/
|
|
22
|
+
constructor(elements = [], options) {
|
|
23
|
+
super();
|
|
24
|
+
this._store = {};
|
|
25
|
+
this._objMap = new Map();
|
|
26
|
+
this._size = 0;
|
|
27
|
+
this._hashFn = (key) => String(key);
|
|
28
|
+
if (options) {
|
|
29
|
+
const { hashFn } = options;
|
|
30
|
+
if (hashFn) {
|
|
31
|
+
this._hashFn = hashFn;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (elements) {
|
|
35
|
+
this.setMany(elements);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
get size() {
|
|
39
|
+
return this._size;
|
|
40
|
+
}
|
|
41
|
+
isEmpty() {
|
|
42
|
+
return this.size === 0;
|
|
43
|
+
}
|
|
44
|
+
clear() {
|
|
45
|
+
this._store = {};
|
|
46
|
+
this._objMap.clear();
|
|
47
|
+
this._size = 0;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The `set` function adds a key-value pair to a map-like data structure, incrementing the size if
|
|
51
|
+
* the key is not already present.
|
|
52
|
+
* @param {K} key - The key parameter is the key used to identify the value in the data structure. It
|
|
53
|
+
* can be of any type, but if it is an object, it will be stored in a Map, otherwise it will be
|
|
54
|
+
* stored in a regular JavaScript object.
|
|
55
|
+
* @param {V} value - The value parameter represents the value that you want to associate with the
|
|
56
|
+
* key in the data structure.
|
|
57
|
+
*/
|
|
58
|
+
set(key, value) {
|
|
59
|
+
if (this._isObjKey(key)) {
|
|
60
|
+
if (!this._objMap.has(key)) {
|
|
61
|
+
this._size++;
|
|
62
|
+
}
|
|
63
|
+
this._objMap.set(key, value);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const strKey = this._getNoObjKey(key);
|
|
67
|
+
if (this._store[strKey] === undefined) {
|
|
68
|
+
this._size++;
|
|
69
|
+
}
|
|
70
|
+
this._store[strKey] = { key, value };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The function "setMany" sets multiple key-value pairs in a map.
|
|
75
|
+
* @param elements - The `elements` parameter is an iterable containing key-value pairs. Each
|
|
76
|
+
* key-value pair is represented as an array with two elements: the key and the value.
|
|
77
|
+
*/
|
|
78
|
+
setMany(elements) {
|
|
79
|
+
for (const [key, value] of elements)
|
|
80
|
+
this.set(key, value);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
84
|
+
* a string map.
|
|
85
|
+
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
86
|
+
* of any type, but it should be compatible with the key type used when the map was created.
|
|
87
|
+
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
|
|
88
|
+
* or `_store`, otherwise it returns `undefined`.
|
|
89
|
+
*/
|
|
90
|
+
get(key) {
|
|
91
|
+
var _a;
|
|
92
|
+
if (this._isObjKey(key)) {
|
|
93
|
+
return this._objMap.get(key);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
const strKey = this._getNoObjKey(key);
|
|
97
|
+
return (_a = this._store[strKey]) === null || _a === void 0 ? void 0 : _a.value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The `has` function checks if a given key exists in the `_objMap` or `_store` based on whether it
|
|
102
|
+
* is an object key or not.
|
|
103
|
+
* @param {K} key - The parameter "key" is of type K, which means it can be any type.
|
|
104
|
+
* @returns The `has` method is returning a boolean value.
|
|
105
|
+
*/
|
|
106
|
+
has(key) {
|
|
107
|
+
if (this._isObjKey(key)) {
|
|
108
|
+
return this._objMap.has(key);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const strKey = this._getNoObjKey(key);
|
|
112
|
+
return strKey in this._store;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The `delete` function removes an element from a map-like data structure based on the provided key.
|
|
117
|
+
* @param {K} key - The `key` parameter is the key of the element that you want to delete from the
|
|
118
|
+
* data structure.
|
|
119
|
+
* @returns The `delete` method returns a boolean value. It returns `true` if the key was
|
|
120
|
+
* successfully deleted from the map, and `false` if the key was not found in the map.
|
|
121
|
+
*/
|
|
122
|
+
delete(key) {
|
|
123
|
+
if (this._isObjKey(key)) {
|
|
124
|
+
if (this._objMap.has(key)) {
|
|
125
|
+
this._size--;
|
|
126
|
+
}
|
|
127
|
+
return this._objMap.delete(key);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const strKey = this._getNoObjKey(key);
|
|
131
|
+
if (strKey in this._store) {
|
|
132
|
+
delete this._store[strKey];
|
|
133
|
+
this._size--;
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(n)
|
|
141
|
+
* Space Complexity: O(n)
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(n)
|
|
145
|
+
* Space Complexity: O(n)
|
|
146
|
+
*
|
|
147
|
+
* The `map` function in TypeScript creates a new HashMap by applying a callback function to each
|
|
148
|
+
* key-value pair in the original HashMap.
|
|
149
|
+
* @param callbackfn - The callback function that will be called for each key-value pair in the
|
|
150
|
+
* HashMap. It takes four parameters:
|
|
151
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
152
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
153
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
154
|
+
* @returns The `map` method is returning a new `HashMap` object with the transformed values based on
|
|
155
|
+
* the provided callback function.
|
|
156
|
+
*/
|
|
157
|
+
map(callbackfn, thisArg) {
|
|
158
|
+
const resultMap = new HashMap();
|
|
159
|
+
let index = 0;
|
|
160
|
+
for (const [key, value] of this) {
|
|
161
|
+
resultMap.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
162
|
+
}
|
|
163
|
+
return resultMap;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Time Complexity: O(n)
|
|
167
|
+
* Space Complexity: O(n)
|
|
168
|
+
*/
|
|
169
|
+
/**
|
|
170
|
+
* Time Complexity: O(n)
|
|
171
|
+
* Space Complexity: O(n)
|
|
172
|
+
*
|
|
173
|
+
* The `filter` function creates a new HashMap containing key-value pairs from the original HashMap
|
|
174
|
+
* that satisfy a given predicate function.
|
|
175
|
+
* @param predicate - The predicate parameter is a function that takes four arguments: value, key,
|
|
176
|
+
* index, and map. It is used to determine whether an element should be included in the filtered map
|
|
177
|
+
* or not. The function should return a boolean value - true if the element should be included, and
|
|
178
|
+
* false otherwise.
|
|
179
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
180
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
181
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
182
|
+
* @returns The `filter` method is returning a new `HashMap` object that contains the key-value pairs
|
|
183
|
+
* from the original `HashMap` that pass the provided `predicate` function.
|
|
184
|
+
*/
|
|
185
|
+
filter(predicate, thisArg) {
|
|
186
|
+
const filteredMap = new HashMap();
|
|
187
|
+
let index = 0;
|
|
188
|
+
for (const [key, value] of this) {
|
|
189
|
+
if (predicate.call(thisArg, value, key, index++, this)) {
|
|
190
|
+
filteredMap.set(key, value);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return filteredMap;
|
|
194
|
+
}
|
|
195
|
+
print() {
|
|
196
|
+
console.log([...this.entries()]);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* The function returns an iterator that yields key-value pairs from both an object store and an
|
|
200
|
+
* object map.
|
|
201
|
+
*/
|
|
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;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
_isObjKey(key) {
|
|
211
|
+
const keyType = typeof key;
|
|
212
|
+
return (keyType === 'object' || keyType === 'function') && key !== null;
|
|
213
|
+
}
|
|
214
|
+
_getNoObjKey(key) {
|
|
215
|
+
const keyType = typeof key;
|
|
216
|
+
let strKey;
|
|
217
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
218
|
+
strKey = this._hashFn(key);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
if (keyType === "number") {
|
|
222
|
+
// TODO numeric key should has its own hash
|
|
223
|
+
strKey = key;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
strKey = key;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return strKey;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
exports.HashMap = HashMap;
|
|
233
|
+
class LinkedHashMap extends base_1.IterablePairBase {
|
|
13
234
|
constructor(elements, options = {
|
|
14
235
|
hashFn: (key) => String(key),
|
|
15
236
|
objHashFn: (key) => key
|
|
16
237
|
}) {
|
|
238
|
+
super();
|
|
17
239
|
this._noObjMap = {};
|
|
18
240
|
this._objMap = new WeakMap();
|
|
19
241
|
this._size = 0;
|
|
@@ -92,47 +314,62 @@ class HashMap {
|
|
|
92
314
|
*/
|
|
93
315
|
set(key, value) {
|
|
94
316
|
let node;
|
|
317
|
+
const isNewKey = !this.has(key); // Check if the key is new
|
|
95
318
|
if ((0, utils_1.isWeakKey)(key)) {
|
|
96
319
|
const hash = this._objHashFn(key);
|
|
97
320
|
node = this._objMap.get(hash);
|
|
98
|
-
if (node) {
|
|
99
|
-
// If the node already exists, update its value
|
|
100
|
-
node.value = value;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
321
|
+
if (!node && isNewKey) {
|
|
103
322
|
// Create new node
|
|
104
323
|
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
105
|
-
// Add new nodes to _objMap and linked list
|
|
106
324
|
this._objMap.set(hash, node);
|
|
107
325
|
}
|
|
326
|
+
else if (node) {
|
|
327
|
+
// Update the value of an existing node
|
|
328
|
+
node.value = value;
|
|
329
|
+
}
|
|
108
330
|
}
|
|
109
331
|
else {
|
|
110
332
|
const hash = this._hashFn(key);
|
|
111
|
-
// Non-object keys are handled in the same way as the original implementation
|
|
112
333
|
node = this._noObjMap[hash];
|
|
113
|
-
if (node) {
|
|
334
|
+
if (!node && isNewKey) {
|
|
335
|
+
this._noObjMap[hash] = node = { key, value, prev: this._tail, next: this._sentinel };
|
|
336
|
+
}
|
|
337
|
+
else if (node) {
|
|
338
|
+
// Update the value of an existing node
|
|
114
339
|
node.value = value;
|
|
115
340
|
}
|
|
341
|
+
}
|
|
342
|
+
if (node && isNewKey) {
|
|
343
|
+
// Update the head and tail of the linked list
|
|
344
|
+
if (this._size === 0) {
|
|
345
|
+
this._head = node;
|
|
346
|
+
this._sentinel.next = node;
|
|
347
|
+
}
|
|
116
348
|
else {
|
|
117
|
-
this.
|
|
118
|
-
|
|
119
|
-
value,
|
|
120
|
-
prev: this._tail,
|
|
121
|
-
next: this._sentinel
|
|
122
|
-
};
|
|
349
|
+
this._tail.next = node;
|
|
350
|
+
node.prev = this._tail; // Make sure that the prev of the new node points to the current tail node
|
|
123
351
|
}
|
|
352
|
+
this._tail = node;
|
|
353
|
+
this._sentinel.prev = node;
|
|
354
|
+
this._size++;
|
|
124
355
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
356
|
+
return this._size;
|
|
357
|
+
}
|
|
358
|
+
has(key) {
|
|
359
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
360
|
+
const hash = this._objHashFn(key);
|
|
361
|
+
return this._objMap.has(hash);
|
|
128
362
|
}
|
|
129
363
|
else {
|
|
130
|
-
|
|
364
|
+
const hash = this._hashFn(key);
|
|
365
|
+
return hash in this._noObjMap;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
setMany(entries) {
|
|
369
|
+
for (const entry of entries) {
|
|
370
|
+
const [key, value] = entry;
|
|
371
|
+
this.set(key, value);
|
|
131
372
|
}
|
|
132
|
-
this._tail = node;
|
|
133
|
-
this._sentinel.prev = node;
|
|
134
|
-
this._size++;
|
|
135
|
-
return this._size;
|
|
136
373
|
}
|
|
137
374
|
/**
|
|
138
375
|
* Time Complexity: O(1)
|
|
@@ -254,36 +491,38 @@ class HashMap {
|
|
|
254
491
|
this._size = 0;
|
|
255
492
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
256
493
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
* each element.
|
|
263
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
264
|
-
* HashMap. It takes three arguments:
|
|
265
|
-
*/
|
|
266
|
-
forEach(callback) {
|
|
267
|
-
let index = 0;
|
|
268
|
-
let node = this._head;
|
|
269
|
-
while (node !== this._sentinel) {
|
|
270
|
-
callback([node.key, node.value], index++, this);
|
|
271
|
-
node = node.next;
|
|
494
|
+
clone() {
|
|
495
|
+
const cloned = new LinkedHashMap([], { hashFn: this._hashFn, objHashFn: this._objHashFn });
|
|
496
|
+
for (const entry of this) {
|
|
497
|
+
const [key, value] = entry;
|
|
498
|
+
cloned.set(key, value);
|
|
272
499
|
}
|
|
500
|
+
return cloned;
|
|
273
501
|
}
|
|
274
502
|
/**
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
278
|
-
* `map`.
|
|
279
|
-
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
280
|
-
* satisfy the given predicate function.
|
|
503
|
+
* Time Complexity: O(n)
|
|
504
|
+
* Space Complexity: O(n)
|
|
281
505
|
*/
|
|
282
|
-
|
|
283
|
-
|
|
506
|
+
/**
|
|
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.
|
|
520
|
+
*/
|
|
521
|
+
filter(predicate, thisArg) {
|
|
522
|
+
const filteredMap = new LinkedHashMap();
|
|
284
523
|
let index = 0;
|
|
285
524
|
for (const [key, value] of this) {
|
|
286
|
-
if (predicate(
|
|
525
|
+
if (predicate.call(thisArg, value, key, index, this)) {
|
|
287
526
|
filteredMap.set(key, value);
|
|
288
527
|
}
|
|
289
528
|
index++;
|
|
@@ -291,59 +530,52 @@ class HashMap {
|
|
|
291
530
|
return filteredMap;
|
|
292
531
|
}
|
|
293
532
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
*
|
|
533
|
+
* Time Complexity: O(n)
|
|
534
|
+
* Space Complexity: O(n)
|
|
535
|
+
*/
|
|
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.
|
|
299
552
|
*/
|
|
300
|
-
map(callback) {
|
|
301
|
-
const mappedMap = new
|
|
553
|
+
map(callback, thisArg) {
|
|
554
|
+
const mappedMap = new LinkedHashMap();
|
|
302
555
|
let index = 0;
|
|
303
556
|
for (const [key, value] of this) {
|
|
304
|
-
const newValue = callback(
|
|
557
|
+
const newValue = callback.call(thisArg, value, key, index, this);
|
|
305
558
|
mappedMap.set(key, newValue);
|
|
306
559
|
index++;
|
|
307
560
|
}
|
|
308
561
|
return mappedMap;
|
|
309
562
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
* each element, accumulating a single value.
|
|
313
|
-
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
314
|
-
* element, and map. It is called for each element in the HashMap and is used to accumulate a single
|
|
315
|
-
* result.
|
|
316
|
-
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
317
|
-
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
318
|
-
* the elements of the map.
|
|
319
|
-
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
320
|
-
* over all the elements in the HashMap and applying the callback function to each element.
|
|
321
|
-
*/
|
|
322
|
-
reduce(callback, initialValue) {
|
|
323
|
-
let accumulator = initialValue;
|
|
324
|
-
let index = 0;
|
|
325
|
-
for (const entry of this) {
|
|
326
|
-
accumulator = callback(accumulator, entry, index, this);
|
|
327
|
-
index++;
|
|
328
|
-
}
|
|
329
|
-
return accumulator;
|
|
563
|
+
print() {
|
|
564
|
+
console.log([...this]);
|
|
330
565
|
}
|
|
331
566
|
/**
|
|
332
|
-
* Time Complexity: O(n), where n is the number of elements in the
|
|
567
|
+
* Time Complexity: O(n), where n is the number of elements in the LinkedHashMap.
|
|
333
568
|
* Space Complexity: O(1)
|
|
334
569
|
*
|
|
335
570
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
336
571
|
*/
|
|
337
|
-
*
|
|
572
|
+
*_getIterator() {
|
|
338
573
|
let node = this._head;
|
|
339
574
|
while (node !== this._sentinel) {
|
|
340
575
|
yield [node.key, node.value];
|
|
341
576
|
node = node.next;
|
|
342
577
|
}
|
|
343
578
|
}
|
|
344
|
-
print() {
|
|
345
|
-
console.log([...this]);
|
|
346
|
-
}
|
|
347
579
|
/**
|
|
348
580
|
* Time Complexity: O(1)
|
|
349
581
|
* Space Complexity: O(1)
|
|
@@ -367,4 +599,4 @@ class HashMap {
|
|
|
367
599
|
this._size -= 1;
|
|
368
600
|
}
|
|
369
601
|
}
|
|
370
|
-
exports.
|
|
602
|
+
exports.LinkedHashMap = LinkedHashMap;
|
|
@@ -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)
|
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
|
-
|
|
10
|
+
const base_1 = require("../base");
|
|
11
|
+
class Heap extends base_1.IterableElementBase {
|
|
11
12
|
constructor(elements, options) {
|
|
13
|
+
super();
|
|
12
14
|
this._elements = [];
|
|
13
15
|
const defaultComparator = (a, b) => {
|
|
14
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
@@ -310,47 +312,70 @@ class Heap {
|
|
|
310
312
|
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
311
313
|
this._sinkDown(i, this.elements.length >> 1);
|
|
312
314
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
315
|
+
/**
|
|
316
|
+
* Time Complexity: O(n)
|
|
317
|
+
* Space Complexity: O(n)
|
|
318
|
+
*/
|
|
319
|
+
/**
|
|
320
|
+
* Time Complexity: O(n)
|
|
321
|
+
* Space Complexity: O(n)
|
|
322
|
+
*
|
|
323
|
+
* The `filter` function creates a new Heap object containing elements that pass a given callback
|
|
324
|
+
* function.
|
|
325
|
+
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
326
|
+
* the heap. It takes three arguments: the current element, the index of the current element, and the
|
|
327
|
+
* heap itself. The callback function should return a boolean value indicating whether the current
|
|
328
|
+
* element should be included in the filtered list
|
|
329
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
330
|
+
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
331
|
+
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
332
|
+
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
333
|
+
* the filter condition specified by the `callback` function.
|
|
334
|
+
*/
|
|
335
|
+
filter(callback, thisArg) {
|
|
336
|
+
const filteredList = new Heap();
|
|
327
337
|
let index = 0;
|
|
328
|
-
for (const
|
|
329
|
-
if (
|
|
330
|
-
|
|
338
|
+
for (const current of this) {
|
|
339
|
+
if (callback.call(thisArg, current, index, this)) {
|
|
340
|
+
filteredList.push(current);
|
|
331
341
|
}
|
|
332
342
|
index++;
|
|
333
343
|
}
|
|
334
|
-
return
|
|
344
|
+
return filteredList;
|
|
335
345
|
}
|
|
336
|
-
|
|
346
|
+
/**
|
|
347
|
+
* Time Complexity: O(n)
|
|
348
|
+
* Space Complexity: O(n)
|
|
349
|
+
*/
|
|
350
|
+
/**
|
|
351
|
+
* Time Complexity: O(n)
|
|
352
|
+
* Space Complexity: O(n)
|
|
353
|
+
*
|
|
354
|
+
* The `map` function creates a new heap by applying a callback function to each element of the
|
|
355
|
+
* original heap.
|
|
356
|
+
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
357
|
+
* original heap. It takes three arguments: the current element, the index of the current element,
|
|
358
|
+
* and the original heap itself. The callback function should return a value of type T, which will be
|
|
359
|
+
* added to the mapped heap.
|
|
360
|
+
* @param comparator - The `comparator` parameter is a function that is used to compare elements in
|
|
361
|
+
* the heap. It takes two arguments, `a` and `b`, and returns a negative number if `a` is less than
|
|
362
|
+
* `b`, a positive number if `a` is greater than `b`, or
|
|
363
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
364
|
+
* specify the value of `this` within the callback function. It is used when you want to bind a
|
|
365
|
+
* specific object as the context for the callback function. If `thisArg` is not provided,
|
|
366
|
+
* `undefined` is used as
|
|
367
|
+
* @returns a new instance of the Heap class, which is created using the mapped elements from the
|
|
368
|
+
* original Heap.
|
|
369
|
+
*/
|
|
370
|
+
map(callback, comparator, thisArg) {
|
|
337
371
|
const mappedHeap = new Heap([], { comparator: comparator });
|
|
338
372
|
let index = 0;
|
|
339
373
|
for (const el of this) {
|
|
340
|
-
mappedHeap.add(callback(el, index, this));
|
|
374
|
+
mappedHeap.add(callback.call(thisArg, el, index, this));
|
|
341
375
|
index++;
|
|
342
376
|
}
|
|
343
377
|
return mappedHeap;
|
|
344
378
|
}
|
|
345
|
-
reduce(callback, initialValue) {
|
|
346
|
-
let accumulator = initialValue;
|
|
347
|
-
let index = 0;
|
|
348
|
-
for (const el of this) {
|
|
349
|
-
accumulator = callback(accumulator, el, index, this);
|
|
350
|
-
index++;
|
|
351
|
-
}
|
|
352
|
-
return accumulator;
|
|
353
|
-
}
|
|
354
379
|
/**
|
|
355
380
|
* Time Complexity: O(log n)
|
|
356
381
|
* Space Complexity: O(1)
|
|
@@ -358,6 +383,11 @@ class Heap {
|
|
|
358
383
|
print() {
|
|
359
384
|
console.log([...this]);
|
|
360
385
|
}
|
|
386
|
+
*_getIterator() {
|
|
387
|
+
for (const element of this.elements) {
|
|
388
|
+
yield element;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
361
391
|
/**
|
|
362
392
|
* Time Complexity: O(n)
|
|
363
393
|
* Space Complexity: O(1)
|