data-structure-typed 2.2.6 → 2.2.8
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/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +47 -1
- package/README.md +19 -8
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +109 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +109 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +109 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +109 -107
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +105 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.leetcode.config.js +1 -1
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
var IterableEntryBase = class {
|
|
4
|
+
static {
|
|
5
|
+
__name(this, "IterableEntryBase");
|
|
6
|
+
}
|
|
7
|
+
*[Symbol.iterator](...args) {
|
|
8
|
+
yield* this._getIterator(...args);
|
|
9
|
+
}
|
|
10
|
+
*entries() {
|
|
11
|
+
for (const item of this) {
|
|
12
|
+
yield item;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
*keys() {
|
|
16
|
+
for (const item of this) {
|
|
17
|
+
yield item[0];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
*values() {
|
|
21
|
+
for (const item of this) {
|
|
22
|
+
yield item[1];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
every(predicate, thisArg) {
|
|
26
|
+
let index = 0;
|
|
27
|
+
for (const item of this) {
|
|
28
|
+
if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
some(predicate, thisArg) {
|
|
35
|
+
let index = 0;
|
|
36
|
+
for (const item of this) {
|
|
37
|
+
if (predicate.call(thisArg, item[1], item[0], index++, this)) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
forEach(callbackfn, thisArg) {
|
|
44
|
+
let index = 0;
|
|
45
|
+
for (const item of this) {
|
|
46
|
+
const [key, value] = item;
|
|
47
|
+
callbackfn.call(thisArg, value, key, index++, this);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
find(callbackfn, thisArg) {
|
|
51
|
+
let index = 0;
|
|
52
|
+
for (const item of this) {
|
|
53
|
+
const [key, value] = item;
|
|
54
|
+
if (callbackfn.call(thisArg, value, key, index++, this)) return item;
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
has(key) {
|
|
59
|
+
for (const item of this) {
|
|
60
|
+
const [itemKey] = item;
|
|
61
|
+
if (itemKey === key) return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
hasValue(value) {
|
|
66
|
+
for (const [, elementValue] of this) {
|
|
67
|
+
if (elementValue === value) return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
get(key) {
|
|
72
|
+
for (const item of this) {
|
|
73
|
+
const [itemKey, value] = item;
|
|
74
|
+
if (itemKey === key) return value;
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
reduce(callbackfn, initialValue) {
|
|
79
|
+
let accumulator = initialValue;
|
|
80
|
+
let index = 0;
|
|
81
|
+
for (const item of this) {
|
|
82
|
+
const [key, value] = item;
|
|
83
|
+
accumulator = callbackfn(accumulator, value, key, index++, this);
|
|
84
|
+
}
|
|
85
|
+
return accumulator;
|
|
86
|
+
}
|
|
87
|
+
toArray() {
|
|
88
|
+
return [...this];
|
|
89
|
+
}
|
|
90
|
+
toVisual() {
|
|
91
|
+
return [...this];
|
|
92
|
+
}
|
|
93
|
+
print() {
|
|
94
|
+
console.log(this.toVisual());
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var rangeCheck = __name((index, min, max, message = "Index out of bounds.") => {
|
|
98
|
+
if (index < min || index > max) throw new RangeError(message);
|
|
99
|
+
}, "rangeCheck");
|
|
100
|
+
var isWeakKey = __name((input) => {
|
|
101
|
+
const inputType = typeof input;
|
|
102
|
+
return inputType === "object" && input !== null || inputType === "function";
|
|
103
|
+
}, "isWeakKey");
|
|
104
|
+
var HashMap = class extends IterableEntryBase {
|
|
105
|
+
static {
|
|
106
|
+
__name(this, "HashMap");
|
|
107
|
+
}
|
|
108
|
+
constructor(entryOrRawElements = [], options) {
|
|
109
|
+
super();
|
|
110
|
+
if (options) {
|
|
111
|
+
const { hashFn, toEntryFn } = options;
|
|
112
|
+
if (hashFn) this._hashFn = hashFn;
|
|
113
|
+
if (toEntryFn) this._toEntryFn = toEntryFn;
|
|
114
|
+
}
|
|
115
|
+
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
116
|
+
}
|
|
117
|
+
_store = {};
|
|
118
|
+
get store() {
|
|
119
|
+
return this._store;
|
|
120
|
+
}
|
|
121
|
+
_objMap = new Map();
|
|
122
|
+
get objMap() {
|
|
123
|
+
return this._objMap;
|
|
124
|
+
}
|
|
125
|
+
_toEntryFn;
|
|
126
|
+
get toEntryFn() {
|
|
127
|
+
return this._toEntryFn;
|
|
128
|
+
}
|
|
129
|
+
_size = 0;
|
|
130
|
+
get size() {
|
|
131
|
+
return this._size;
|
|
132
|
+
}
|
|
133
|
+
_hashFn = __name((key) => String(key), "_hashFn");
|
|
134
|
+
get hashFn() {
|
|
135
|
+
return this._hashFn;
|
|
136
|
+
}
|
|
137
|
+
isEmpty() {
|
|
138
|
+
return this._size === 0;
|
|
139
|
+
}
|
|
140
|
+
clear() {
|
|
141
|
+
this._store = {};
|
|
142
|
+
this._objMap.clear();
|
|
143
|
+
this._size = 0;
|
|
144
|
+
}
|
|
145
|
+
isEntry(rawElement) {
|
|
146
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
147
|
+
}
|
|
148
|
+
set(key, value) {
|
|
149
|
+
if (this._isObjKey(key)) {
|
|
150
|
+
if (!this.objMap.has(key)) this._size++;
|
|
151
|
+
this.objMap.set(key, value);
|
|
152
|
+
} else {
|
|
153
|
+
const strKey = this._getNoObjKey(key);
|
|
154
|
+
if (this.store[strKey] === void 0) this._size++;
|
|
155
|
+
this._store[strKey] = { key, value };
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
setMany(entryOrRawElements) {
|
|
160
|
+
const results = [];
|
|
161
|
+
for (const rawEle of entryOrRawElements) {
|
|
162
|
+
let key, value;
|
|
163
|
+
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
164
|
+
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
165
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
166
|
+
}
|
|
167
|
+
return results;
|
|
168
|
+
}
|
|
169
|
+
get(key) {
|
|
170
|
+
if (this._isObjKey(key)) return this.objMap.get(key);
|
|
171
|
+
const strKey = this._getNoObjKey(key);
|
|
172
|
+
return this._store[strKey]?.value;
|
|
173
|
+
}
|
|
174
|
+
has(key) {
|
|
175
|
+
if (this._isObjKey(key)) return this.objMap.has(key);
|
|
176
|
+
const strKey = this._getNoObjKey(key);
|
|
177
|
+
return strKey in this.store;
|
|
178
|
+
}
|
|
179
|
+
delete(key) {
|
|
180
|
+
if (this._isObjKey(key)) {
|
|
181
|
+
if (this.objMap.has(key)) this._size--;
|
|
182
|
+
return this.objMap.delete(key);
|
|
183
|
+
}
|
|
184
|
+
const strKey = this._getNoObjKey(key);
|
|
185
|
+
if (strKey in this.store) {
|
|
186
|
+
delete this.store[strKey];
|
|
187
|
+
this._size--;
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
setHashFn(fn) {
|
|
193
|
+
if (this._hashFn === fn) return this;
|
|
194
|
+
this._hashFn = fn;
|
|
195
|
+
this._rehashNoObj();
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
clone() {
|
|
199
|
+
const opts = { hashFn: this._hashFn, toEntryFn: this._toEntryFn };
|
|
200
|
+
return this._createLike(this, opts);
|
|
201
|
+
}
|
|
202
|
+
map(callbackfn, thisArg) {
|
|
203
|
+
const out = this._createLike();
|
|
204
|
+
let index = 0;
|
|
205
|
+
for (const [key, value] of this) out.set(key, callbackfn.call(thisArg, value, key, index++, this));
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
filter(predicate, thisArg) {
|
|
209
|
+
const out = this._createLike();
|
|
210
|
+
let index = 0;
|
|
211
|
+
for (const [key, value] of this) if (predicate.call(thisArg, value, key, index++, this)) out.set(key, value);
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
_createLike(entries = [], options) {
|
|
215
|
+
const Ctor = this.constructor;
|
|
216
|
+
return new Ctor(entries, options);
|
|
217
|
+
}
|
|
218
|
+
_rehashNoObj() {
|
|
219
|
+
const fresh = {};
|
|
220
|
+
for (const { key, value } of Object.values(this._store)) {
|
|
221
|
+
const sk = this._getNoObjKey(key);
|
|
222
|
+
fresh[sk] = { key, value };
|
|
223
|
+
}
|
|
224
|
+
this._store = fresh;
|
|
225
|
+
}
|
|
226
|
+
*_getIterator() {
|
|
227
|
+
for (const node of Object.values(this.store)) yield [node.key, node.value];
|
|
228
|
+
for (const node of this.objMap) yield node;
|
|
229
|
+
}
|
|
230
|
+
_isObjKey(key) {
|
|
231
|
+
const keyType = typeof key;
|
|
232
|
+
return (keyType === "object" || keyType === "function") && key !== null;
|
|
233
|
+
}
|
|
234
|
+
_getNoObjKey(key) {
|
|
235
|
+
const keyType = typeof key;
|
|
236
|
+
let strKey;
|
|
237
|
+
if (keyType !== "string" && keyType !== "number" && keyType !== "symbol") {
|
|
238
|
+
strKey = this._hashFn(key);
|
|
239
|
+
} else {
|
|
240
|
+
if (keyType === "number") {
|
|
241
|
+
strKey = key;
|
|
242
|
+
} else {
|
|
243
|
+
strKey = key;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return strKey;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
var LinkedHashMap = class extends IterableEntryBase {
|
|
250
|
+
static {
|
|
251
|
+
__name(this, "LinkedHashMap");
|
|
252
|
+
}
|
|
253
|
+
_sentinel;
|
|
254
|
+
constructor(entryOrRawElements = [], options) {
|
|
255
|
+
super();
|
|
256
|
+
this._sentinel = {};
|
|
257
|
+
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
258
|
+
if (options) {
|
|
259
|
+
const { hashFn, objHashFn, toEntryFn } = options;
|
|
260
|
+
if (hashFn) this._hashFn = hashFn;
|
|
261
|
+
if (objHashFn) this._objHashFn = objHashFn;
|
|
262
|
+
if (toEntryFn) this._toEntryFn = toEntryFn;
|
|
263
|
+
}
|
|
264
|
+
if (entryOrRawElements) this.setMany(entryOrRawElements);
|
|
265
|
+
}
|
|
266
|
+
_hashFn = __name((key) => String(key), "_hashFn");
|
|
267
|
+
get hashFn() {
|
|
268
|
+
return this._hashFn;
|
|
269
|
+
}
|
|
270
|
+
_objHashFn = __name((key) => key, "_objHashFn");
|
|
271
|
+
get objHashFn() {
|
|
272
|
+
return this._objHashFn;
|
|
273
|
+
}
|
|
274
|
+
_noObjMap = {};
|
|
275
|
+
get noObjMap() {
|
|
276
|
+
return this._noObjMap;
|
|
277
|
+
}
|
|
278
|
+
_objMap = new WeakMap();
|
|
279
|
+
get objMap() {
|
|
280
|
+
return this._objMap;
|
|
281
|
+
}
|
|
282
|
+
_head;
|
|
283
|
+
get head() {
|
|
284
|
+
return this._head;
|
|
285
|
+
}
|
|
286
|
+
_tail;
|
|
287
|
+
get tail() {
|
|
288
|
+
return this._tail;
|
|
289
|
+
}
|
|
290
|
+
_toEntryFn = __name((rawElement) => {
|
|
291
|
+
if (this.isEntry(rawElement)) {
|
|
292
|
+
return rawElement;
|
|
293
|
+
}
|
|
294
|
+
throw new Error(
|
|
295
|
+
"If `entryOrRawElements` does not adhere to [key,value], provide `options.toEntryFn` to transform raw records."
|
|
296
|
+
);
|
|
297
|
+
}, "_toEntryFn");
|
|
298
|
+
get toEntryFn() {
|
|
299
|
+
return this._toEntryFn;
|
|
300
|
+
}
|
|
301
|
+
_size = 0;
|
|
302
|
+
get size() {
|
|
303
|
+
return this._size;
|
|
304
|
+
}
|
|
305
|
+
get first() {
|
|
306
|
+
if (this._size === 0) return;
|
|
307
|
+
return [this.head.key, this.head.value];
|
|
308
|
+
}
|
|
309
|
+
get last() {
|
|
310
|
+
if (this._size === 0) return;
|
|
311
|
+
return [this.tail.key, this.tail.value];
|
|
312
|
+
}
|
|
313
|
+
*begin() {
|
|
314
|
+
let node = this.head;
|
|
315
|
+
while (node !== this._sentinel) {
|
|
316
|
+
yield [node.key, node.value];
|
|
317
|
+
node = node.next;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
*reverseBegin() {
|
|
321
|
+
let node = this.tail;
|
|
322
|
+
while (node !== this._sentinel) {
|
|
323
|
+
yield [node.key, node.value];
|
|
324
|
+
node = node.prev;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
set(key, value) {
|
|
328
|
+
let node;
|
|
329
|
+
const isNewKey = !this.has(key);
|
|
330
|
+
if (isWeakKey(key)) {
|
|
331
|
+
const hash = this._objHashFn(key);
|
|
332
|
+
node = this.objMap.get(hash);
|
|
333
|
+
if (!node && isNewKey) {
|
|
334
|
+
node = { key: hash, value, prev: this.tail, next: this._sentinel };
|
|
335
|
+
this.objMap.set(hash, node);
|
|
336
|
+
} else if (node) {
|
|
337
|
+
node.value = value;
|
|
338
|
+
}
|
|
339
|
+
} else {
|
|
340
|
+
const hash = this._hashFn(key);
|
|
341
|
+
node = this.noObjMap[hash];
|
|
342
|
+
if (!node && isNewKey) {
|
|
343
|
+
this.noObjMap[hash] = node = { key, value, prev: this.tail, next: this._sentinel };
|
|
344
|
+
} else if (node) {
|
|
345
|
+
node.value = value;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (node && isNewKey) {
|
|
349
|
+
if (this._size === 0) {
|
|
350
|
+
this._head = node;
|
|
351
|
+
this._sentinel.next = node;
|
|
352
|
+
} else {
|
|
353
|
+
this.tail.next = node;
|
|
354
|
+
node.prev = this.tail;
|
|
355
|
+
}
|
|
356
|
+
this._tail = node;
|
|
357
|
+
this._sentinel.prev = node;
|
|
358
|
+
this._size++;
|
|
359
|
+
}
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
setMany(entryOrRawElements) {
|
|
363
|
+
const results = [];
|
|
364
|
+
for (const rawEle of entryOrRawElements) {
|
|
365
|
+
let key, value;
|
|
366
|
+
if (this.isEntry(rawEle)) [key, value] = rawEle;
|
|
367
|
+
else if (this._toEntryFn) [key, value] = this._toEntryFn(rawEle);
|
|
368
|
+
if (key !== void 0 && value !== void 0) results.push(this.set(key, value));
|
|
369
|
+
}
|
|
370
|
+
return results;
|
|
371
|
+
}
|
|
372
|
+
has(key) {
|
|
373
|
+
if (isWeakKey(key)) {
|
|
374
|
+
const hash2 = this._objHashFn(key);
|
|
375
|
+
return this.objMap.has(hash2);
|
|
376
|
+
}
|
|
377
|
+
const hash = this._hashFn(key);
|
|
378
|
+
return hash in this.noObjMap;
|
|
379
|
+
}
|
|
380
|
+
get(key) {
|
|
381
|
+
if (isWeakKey(key)) {
|
|
382
|
+
const hash2 = this._objHashFn(key);
|
|
383
|
+
const node2 = this.objMap.get(hash2);
|
|
384
|
+
return node2 ? node2.value : void 0;
|
|
385
|
+
}
|
|
386
|
+
const hash = this._hashFn(key);
|
|
387
|
+
const node = this.noObjMap[hash];
|
|
388
|
+
return node ? node.value : void 0;
|
|
389
|
+
}
|
|
390
|
+
at(index) {
|
|
391
|
+
rangeCheck(index, 0, this._size - 1);
|
|
392
|
+
let node = this.head;
|
|
393
|
+
while (index--) node = node.next;
|
|
394
|
+
return node.value;
|
|
395
|
+
}
|
|
396
|
+
delete(key) {
|
|
397
|
+
let node;
|
|
398
|
+
if (isWeakKey(key)) {
|
|
399
|
+
const hash = this._objHashFn(key);
|
|
400
|
+
node = this.objMap.get(hash);
|
|
401
|
+
if (!node) return false;
|
|
402
|
+
this.objMap.delete(hash);
|
|
403
|
+
} else {
|
|
404
|
+
const hash = this._hashFn(key);
|
|
405
|
+
node = this.noObjMap[hash];
|
|
406
|
+
if (!node) return false;
|
|
407
|
+
delete this.noObjMap[hash];
|
|
408
|
+
}
|
|
409
|
+
return this._deleteNode(node);
|
|
410
|
+
}
|
|
411
|
+
deleteWhere(predicate) {
|
|
412
|
+
let node = this._head;
|
|
413
|
+
let i = 0;
|
|
414
|
+
while (node !== this._sentinel) {
|
|
415
|
+
const cur = node;
|
|
416
|
+
node = node.next;
|
|
417
|
+
if (predicate(cur.key, cur.value, i++, this)) {
|
|
418
|
+
if (isWeakKey(cur.key)) {
|
|
419
|
+
this._objMap.delete(cur.key);
|
|
420
|
+
} else {
|
|
421
|
+
const hash = this._hashFn(cur.key);
|
|
422
|
+
delete this._noObjMap[hash];
|
|
423
|
+
}
|
|
424
|
+
return this._deleteNode(cur);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
deleteAt(index) {
|
|
430
|
+
rangeCheck(index, 0, this._size - 1);
|
|
431
|
+
let node = this.head;
|
|
432
|
+
while (index--) node = node.next;
|
|
433
|
+
return this._deleteNode(node);
|
|
434
|
+
}
|
|
435
|
+
isEmpty() {
|
|
436
|
+
return this._size === 0;
|
|
437
|
+
}
|
|
438
|
+
isEntry(rawElement) {
|
|
439
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
440
|
+
}
|
|
441
|
+
clear() {
|
|
442
|
+
this._noObjMap = {};
|
|
443
|
+
this._size = 0;
|
|
444
|
+
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
445
|
+
}
|
|
446
|
+
clone() {
|
|
447
|
+
const opts = { hashFn: this._hashFn, objHashFn: this._objHashFn };
|
|
448
|
+
return this._createLike(this, opts);
|
|
449
|
+
}
|
|
450
|
+
filter(predicate, thisArg) {
|
|
451
|
+
const out = this._createLike();
|
|
452
|
+
let index = 0;
|
|
453
|
+
for (const [key, value] of this) {
|
|
454
|
+
if (predicate.call(thisArg, value, key, index, this)) out.set(key, value);
|
|
455
|
+
index++;
|
|
456
|
+
}
|
|
457
|
+
return out;
|
|
458
|
+
}
|
|
459
|
+
map(callback, thisArg) {
|
|
460
|
+
const out = this._createLike();
|
|
461
|
+
let index = 0;
|
|
462
|
+
for (const [key, value] of this) {
|
|
463
|
+
const [newKey, newValue] = callback.call(thisArg, value, key, index, this);
|
|
464
|
+
out.set(newKey, newValue);
|
|
465
|
+
index++;
|
|
466
|
+
}
|
|
467
|
+
return out;
|
|
468
|
+
}
|
|
469
|
+
*_getIterator() {
|
|
470
|
+
let node = this.head;
|
|
471
|
+
while (node !== this._sentinel) {
|
|
472
|
+
yield [node.key, node.value];
|
|
473
|
+
node = node.next;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
_deleteNode(node) {
|
|
477
|
+
const { prev, next } = node;
|
|
478
|
+
prev.next = next;
|
|
479
|
+
next.prev = prev;
|
|
480
|
+
if (node === this.head) this._head = next;
|
|
481
|
+
if (node === this.tail) this._tail = prev;
|
|
482
|
+
this._size -= 1;
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
_createLike(entries = [], options) {
|
|
486
|
+
const Ctor = this.constructor;
|
|
487
|
+
return new Ctor(entries, options);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
export {
|
|
491
|
+
HashMap,
|
|
492
|
+
LinkedHashMap
|
|
493
|
+
};
|