data-structure-typed 1.46.3 → 1.46.5
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/README.md +47 -61
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +5 -17
- package/dist/cjs/data-structures/hash/hash-map.d.ts +22 -26
- package/dist/cjs/data-structures/hash/hash-map.js +104 -76
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/index.d.ts +0 -4
- package/dist/cjs/data-structures/hash/index.js +0 -4
- package/dist/cjs/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -0
- package/dist/cjs/types/data-structures/hash/index.d.ts +0 -4
- package/dist/cjs/types/data-structures/hash/index.js +0 -4
- package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -1
- package/dist/cjs/utils/utils.js +3 -3
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +22 -26
- package/dist/mjs/data-structures/hash/hash-map.js +107 -77
- package/dist/mjs/data-structures/hash/index.d.ts +0 -4
- package/dist/mjs/data-structures/hash/index.js +0 -4
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -0
- package/dist/mjs/types/data-structures/hash/index.d.ts +0 -4
- package/dist/mjs/types/data-structures/hash/index.js +0 -4
- package/dist/mjs/utils/utils.d.ts +1 -1
- package/dist/mjs/utils/utils.js +1 -1
- package/dist/umd/data-structure-typed.js +96 -182
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/hash/hash-map.ts +130 -81
- package/src/data-structures/hash/index.ts +0 -4
- package/src/types/data-structures/hash/hash-map.ts +6 -0
- package/src/types/data-structures/hash/index.ts +0 -4
- package/src/utils/utils.ts +1 -1
- package/test/performance/data-structures/hash/hash-map.test.ts +75 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +32 -0
- package/dist/cjs/data-structures/hash/coordinate-map.d.ts +0 -44
- package/dist/cjs/data-structures/hash/coordinate-map.js +0 -63
- package/dist/cjs/data-structures/hash/coordinate-map.js.map +0 -1
- package/dist/cjs/data-structures/hash/coordinate-set.d.ts +0 -36
- package/dist/cjs/data-structures/hash/coordinate-set.js +0 -53
- package/dist/cjs/data-structures/hash/coordinate-set.js.map +0 -1
- package/dist/cjs/data-structures/hash/tree-map.d.ts +0 -2
- package/dist/cjs/data-structures/hash/tree-map.js +0 -7
- package/dist/cjs/data-structures/hash/tree-map.js.map +0 -1
- package/dist/cjs/data-structures/hash/tree-set.d.ts +0 -2
- package/dist/cjs/data-structures/hash/tree-set.js +0 -7
- package/dist/cjs/data-structures/hash/tree-set.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-map.js +0 -3
- package/dist/cjs/types/data-structures/hash/coordinate-map.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/coordinate-set.js +0 -3
- package/dist/cjs/types/data-structures/hash/coordinate-set.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/tree-map.js +0 -3
- package/dist/cjs/types/data-structures/hash/tree-map.js.map +0 -1
- package/dist/cjs/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/cjs/types/data-structures/hash/tree-set.js +0 -3
- package/dist/cjs/types/data-structures/hash/tree-set.js.map +0 -1
- package/dist/mjs/data-structures/hash/coordinate-map.d.ts +0 -44
- package/dist/mjs/data-structures/hash/coordinate-map.js +0 -58
- package/dist/mjs/data-structures/hash/coordinate-set.d.ts +0 -36
- package/dist/mjs/data-structures/hash/coordinate-set.js +0 -48
- package/dist/mjs/data-structures/hash/tree-map.d.ts +0 -2
- package/dist/mjs/data-structures/hash/tree-map.js +0 -2
- package/dist/mjs/data-structures/hash/tree-set.d.ts +0 -2
- package/dist/mjs/data-structures/hash/tree-set.js +0 -2
- package/dist/mjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-map.js +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/coordinate-set.js +0 -1
- package/dist/mjs/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/tree-map.js +0 -1
- package/dist/mjs/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/mjs/types/data-structures/hash/tree-set.js +0 -1
- package/src/data-structures/hash/coordinate-map.ts +0 -63
- package/src/data-structures/hash/coordinate-set.ts +0 -52
- package/src/data-structures/hash/tree-map.ts +0 -2
- package/src/data-structures/hash/tree-set.ts +0 -2
- package/src/types/data-structures/hash/coordinate-map.ts +0 -1
- package/src/types/data-structures/hash/coordinate-set.ts +0 -1
- package/src/types/data-structures/hash/tree-map.ts +0 -1
- package/src/types/data-structures/hash/tree-set.ts +0 -1
- package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -74
- package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -66
package/dist/cjs/utils/utils.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.calcMinUnitsRequired = exports.
|
|
12
|
+
exports.calcMinUnitsRequired = exports.isWeakKey = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
13
13
|
const uuidV4 = function () {
|
|
14
14
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
15
15
|
const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
@@ -80,11 +80,11 @@ const throwRangeError = (message = 'The value is off-limits.') => {
|
|
|
80
80
|
throw new RangeError(message);
|
|
81
81
|
};
|
|
82
82
|
exports.throwRangeError = throwRangeError;
|
|
83
|
-
const
|
|
83
|
+
const isWeakKey = (input) => {
|
|
84
84
|
const inputType = typeof input;
|
|
85
85
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
86
86
|
};
|
|
87
|
-
exports.
|
|
87
|
+
exports.isWeakKey = isWeakKey;
|
|
88
88
|
const calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
|
|
89
89
|
exports.calcMinUnitsRequired = calcMinUnitsRequired;
|
|
90
90
|
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/utils/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;AASO,MAAM,MAAM,GAAG;IACpB,OAAO,sCAAsC,CAAC,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC;QACvE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAChC,CAAC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;QACrC,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AANW,QAAA,MAAM,UAMjB;AAEK,MAAM,WAAW,GAAG,UAAa,KAAU,EAAE,SAA0D;IAC5G,IA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KAAc,EAAmB,EAAE;IAC3D,MAAM,SAAS,GAAG,OAAO,KAAK,CAAC;IAC/B,OAAO,CAAC,SAAS,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC,IAAI,SAAS,KAAK,UAAU,CAAC;AAChF,CAAC,CAAC;AAHW,QAAA,SAAS,aAGpB;AAEK,MAAM,oBAAoB,GAAG,CAAC,aAAqB,EAAE,QAAgB,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,QAAQ,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAA;AAAzH,QAAA,oBAAoB,wBAAqG"}
|
|
@@ -5,21 +5,21 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode,
|
|
8
|
+
import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
9
9
|
export declare class HashMap<K = any, V = any> {
|
|
10
|
-
|
|
11
|
-
protected
|
|
12
|
-
protected
|
|
13
|
-
protected
|
|
14
|
-
protected
|
|
15
|
-
protected
|
|
10
|
+
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
|
|
11
|
+
protected _objMap: WeakMap<object, HashMapLinkedNode<K, V | undefined>>;
|
|
12
|
+
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
13
|
+
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
14
|
+
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
|
+
protected _hashFn: (key: K) => string;
|
|
16
|
+
protected _objHashFn: (key: K) => object;
|
|
16
17
|
/**
|
|
17
|
-
* The constructor initializes a
|
|
18
|
-
* @param
|
|
19
|
-
*
|
|
20
|
-
* `K` represents the type of the key and `V` represents the
|
|
18
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
21
|
*/
|
|
22
|
-
constructor(
|
|
22
|
+
constructor(options?: HashMapOptions<K, V>);
|
|
23
23
|
protected _size: number;
|
|
24
24
|
get size(): number;
|
|
25
25
|
/**
|
|
@@ -43,12 +43,12 @@ export declare class HashMap<K = any, V = any> {
|
|
|
43
43
|
/**
|
|
44
44
|
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
45
45
|
*/
|
|
46
|
-
begin(): Generator<(K | V)[], void, unknown>;
|
|
46
|
+
begin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
47
47
|
/**
|
|
48
48
|
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
49
49
|
* key and value.
|
|
50
50
|
*/
|
|
51
|
-
reverseBegin(): Generator<(K | V)[], void, unknown>;
|
|
51
|
+
reverseBegin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
52
52
|
/**
|
|
53
53
|
* Time Complexity: O(1)
|
|
54
54
|
* Space Complexity: O(1)
|
|
@@ -59,10 +59,9 @@ export declare class HashMap<K = any, V = any> {
|
|
|
59
59
|
* type, but typically it is a string or symbol.
|
|
60
60
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
61
61
|
* value associated with the key being set in the data structure.
|
|
62
|
-
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
63
62
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
64
63
|
*/
|
|
65
|
-
set(key: K, value?: V
|
|
64
|
+
set(key: K, value?: V): number;
|
|
66
65
|
/**
|
|
67
66
|
* Time Complexity: O(1)
|
|
68
67
|
* Space Complexity: O(1)
|
|
@@ -71,15 +70,12 @@ export declare class HashMap<K = any, V = any> {
|
|
|
71
70
|
* key directly or by using an index stored in the key object.
|
|
72
71
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
73
72
|
* of any type, but typically it is a string or symbol.
|
|
74
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
75
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
76
|
-
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
77
73
|
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
78
74
|
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
79
|
-
* property of the key. If the key is a string key, the value is retrieved from the `
|
|
75
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
80
76
|
* using the key itself. If the key is not found, `undefined` is
|
|
81
77
|
*/
|
|
82
|
-
get(key: K
|
|
78
|
+
get(key: K): V | undefined;
|
|
83
79
|
/**
|
|
84
80
|
* Time Complexity: O(n), where n is the index.
|
|
85
81
|
* Space Complexity: O(1)
|
|
@@ -99,13 +95,10 @@ export declare class HashMap<K = any, V = any> {
|
|
|
99
95
|
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
100
96
|
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
101
97
|
* It can be of any type, but typically it is a string or an object.
|
|
102
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
103
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
104
|
-
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
105
98
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
106
99
|
* was not found.
|
|
107
100
|
*/
|
|
108
|
-
delete(key: K
|
|
101
|
+
delete(key: K): boolean;
|
|
109
102
|
/**
|
|
110
103
|
* Time Complexity: O(n), where n is the index.
|
|
111
104
|
* Space Complexity: O(1)
|
|
@@ -142,6 +135,9 @@ export declare class HashMap<K = any, V = any> {
|
|
|
142
135
|
* HashMap. It takes three arguments:
|
|
143
136
|
*/
|
|
144
137
|
forEach(callback: (element: [K, V], index: number, hashMap: HashMap<K, V>) => void): void;
|
|
138
|
+
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V>;
|
|
139
|
+
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV>;
|
|
140
|
+
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A;
|
|
145
141
|
/**
|
|
146
142
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
147
143
|
* Space Complexity: O(1)
|
|
@@ -159,5 +155,5 @@ export declare class HashMap<K = any, V = any> {
|
|
|
159
155
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
160
156
|
* and next nodes in the list.
|
|
161
157
|
*/
|
|
162
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V>): void;
|
|
158
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
|
|
163
159
|
}
|
|
@@ -5,26 +5,34 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { isWeakKey, rangeCheck } from '../../utils';
|
|
9
9
|
export class HashMap {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_orgMap = {};
|
|
10
|
+
_noObjMap = {};
|
|
11
|
+
_objMap = new WeakMap();
|
|
13
12
|
_head;
|
|
14
13
|
_tail;
|
|
15
14
|
_sentinel;
|
|
15
|
+
_hashFn;
|
|
16
|
+
_objHashFn;
|
|
16
17
|
/**
|
|
17
|
-
* The constructor initializes a
|
|
18
|
-
* @param
|
|
19
|
-
*
|
|
20
|
-
* `K` represents the type of the key and `V` represents the
|
|
18
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
21
|
*/
|
|
22
|
-
constructor(
|
|
23
|
-
|
|
22
|
+
constructor(options = {
|
|
23
|
+
elements: [],
|
|
24
|
+
hashFn: (key) => String(key),
|
|
25
|
+
objHashFn: (key) => key
|
|
26
|
+
}) {
|
|
24
27
|
this._sentinel = {};
|
|
25
28
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
const { elements, hashFn, objHashFn } = options;
|
|
30
|
+
this._hashFn = hashFn;
|
|
31
|
+
this._objHashFn = objHashFn;
|
|
32
|
+
if (elements) {
|
|
33
|
+
for (const el of elements) {
|
|
34
|
+
this.set(el[0], el[1]);
|
|
35
|
+
}
|
|
28
36
|
}
|
|
29
37
|
}
|
|
30
38
|
_size = 0;
|
|
@@ -88,52 +96,51 @@ export class HashMap {
|
|
|
88
96
|
* type, but typically it is a string or symbol.
|
|
89
97
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
90
98
|
* value associated with the key being set in the data structure.
|
|
91
|
-
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
92
99
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
93
100
|
*/
|
|
94
|
-
set(key, value
|
|
95
|
-
let
|
|
96
|
-
if (
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
+
set(key, value) {
|
|
102
|
+
let node;
|
|
103
|
+
if (isWeakKey(key)) {
|
|
104
|
+
const hash = this._objHashFn(key);
|
|
105
|
+
node = this._objMap.get(hash);
|
|
106
|
+
if (node) {
|
|
107
|
+
// If the node already exists, update its value
|
|
108
|
+
node.value = value;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// Create new node
|
|
112
|
+
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
113
|
+
// Add new nodes to _objMap and linked list
|
|
114
|
+
this._objMap.set(hash, node);
|
|
101
115
|
}
|
|
102
|
-
Object.defineProperty(key, this.OBJ_KEY_INDEX, {
|
|
103
|
-
value: this._nodes.length,
|
|
104
|
-
configurable: true
|
|
105
|
-
});
|
|
106
|
-
newTail = {
|
|
107
|
-
key: key,
|
|
108
|
-
value: value,
|
|
109
|
-
prev: this._tail,
|
|
110
|
-
next: this._sentinel
|
|
111
|
-
};
|
|
112
|
-
this._nodes.push(newTail);
|
|
113
116
|
}
|
|
114
117
|
else {
|
|
115
|
-
const
|
|
118
|
+
const hash = this._hashFn(key);
|
|
119
|
+
// Non-object keys are handled in the same way as the original implementation
|
|
120
|
+
node = this._noObjMap[hash];
|
|
116
121
|
if (node) {
|
|
117
122
|
node.value = value;
|
|
118
|
-
return this._size;
|
|
119
123
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
else {
|
|
125
|
+
this._noObjMap[hash] = node = {
|
|
126
|
+
key,
|
|
127
|
+
value,
|
|
128
|
+
prev: this._tail,
|
|
129
|
+
next: this._sentinel
|
|
130
|
+
};
|
|
131
|
+
}
|
|
126
132
|
}
|
|
127
133
|
if (this._size === 0) {
|
|
128
|
-
this._head =
|
|
129
|
-
this._sentinel.next =
|
|
134
|
+
this._head = node;
|
|
135
|
+
this._sentinel.next = node;
|
|
130
136
|
}
|
|
131
137
|
else {
|
|
132
|
-
this._tail.next =
|
|
138
|
+
this._tail.next = node;
|
|
133
139
|
}
|
|
134
|
-
this._tail =
|
|
135
|
-
this._sentinel.prev =
|
|
136
|
-
|
|
140
|
+
this._tail = node;
|
|
141
|
+
this._sentinel.prev = node;
|
|
142
|
+
this._size++;
|
|
143
|
+
return this._size;
|
|
137
144
|
}
|
|
138
145
|
/**
|
|
139
146
|
* Time Complexity: O(1)
|
|
@@ -143,21 +150,22 @@ export class HashMap {
|
|
|
143
150
|
* key directly or by using an index stored in the key object.
|
|
144
151
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
145
152
|
* of any type, but typically it is a string or symbol.
|
|
146
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
147
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
148
|
-
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
149
153
|
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
150
154
|
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
151
|
-
* property of the key. If the key is a string key, the value is retrieved from the `
|
|
155
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
152
156
|
* using the key itself. If the key is not found, `undefined` is
|
|
153
157
|
*/
|
|
154
|
-
get(key
|
|
155
|
-
if (
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
+
get(key) {
|
|
159
|
+
if (isWeakKey(key)) {
|
|
160
|
+
const hash = this._objHashFn(key);
|
|
161
|
+
const node = this._objMap.get(hash);
|
|
162
|
+
return node ? node.value : undefined;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const hash = this._hashFn(key);
|
|
166
|
+
const node = this._noObjMap[hash];
|
|
167
|
+
return node ? node.value : undefined;
|
|
158
168
|
}
|
|
159
|
-
const node = this._orgMap[key];
|
|
160
|
-
return node ? node.value : undefined;
|
|
161
169
|
}
|
|
162
170
|
/**
|
|
163
171
|
* Time Complexity: O(n), where n is the index.
|
|
@@ -185,28 +193,32 @@ export class HashMap {
|
|
|
185
193
|
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
186
194
|
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
187
195
|
* It can be of any type, but typically it is a string or an object.
|
|
188
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
189
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
190
|
-
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
191
196
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
192
197
|
* was not found.
|
|
193
198
|
*/
|
|
194
|
-
delete(key
|
|
199
|
+
delete(key) {
|
|
195
200
|
let node;
|
|
196
|
-
if (
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
if (isWeakKey(key)) {
|
|
202
|
+
const hash = this._objHashFn(key);
|
|
203
|
+
// Get nodes from WeakMap
|
|
204
|
+
node = this._objMap.get(hash);
|
|
205
|
+
if (!node) {
|
|
206
|
+
return false; // If the node does not exist, return false
|
|
207
|
+
}
|
|
208
|
+
// Remove nodes from WeakMap
|
|
209
|
+
this._objMap.delete(hash);
|
|
203
210
|
}
|
|
204
211
|
else {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
212
|
+
const hash = this._hashFn(key);
|
|
213
|
+
// Get nodes from noObjMap
|
|
214
|
+
node = this._noObjMap[hash];
|
|
215
|
+
if (!node) {
|
|
216
|
+
return false; // If the node does not exist, return false
|
|
217
|
+
}
|
|
218
|
+
// Remove nodes from orgMap
|
|
219
|
+
delete this._noObjMap[hash];
|
|
209
220
|
}
|
|
221
|
+
// Remove node from doubly linked list
|
|
210
222
|
this._deleteNode(node);
|
|
211
223
|
return true;
|
|
212
224
|
}
|
|
@@ -246,13 +258,7 @@ export class HashMap {
|
|
|
246
258
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
247
259
|
*/
|
|
248
260
|
clear() {
|
|
249
|
-
|
|
250
|
-
// this._nodes.forEach(el => {
|
|
251
|
-
// delete (<Record<symbol, number>><unknown>el.key)[OBJ_KEY_INDEX];
|
|
252
|
-
// });
|
|
253
|
-
this._nodes = [];
|
|
254
|
-
this._orgMap = {};
|
|
255
|
-
Object.setPrototypeOf(this._orgMap, null);
|
|
261
|
+
this._noObjMap = {};
|
|
256
262
|
this._size = 0;
|
|
257
263
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
258
264
|
}
|
|
@@ -273,6 +279,30 @@ export class HashMap {
|
|
|
273
279
|
node = node.next;
|
|
274
280
|
}
|
|
275
281
|
}
|
|
282
|
+
filter(predicate) {
|
|
283
|
+
const filteredMap = new HashMap();
|
|
284
|
+
for (const [key, value] of this) {
|
|
285
|
+
if (predicate([key, value], this)) {
|
|
286
|
+
filteredMap.set(key, value);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return filteredMap;
|
|
290
|
+
}
|
|
291
|
+
map(callback) {
|
|
292
|
+
const mappedMap = new HashMap();
|
|
293
|
+
for (const [key, value] of this) {
|
|
294
|
+
const newValue = callback([key, value], this);
|
|
295
|
+
mappedMap.set(key, newValue);
|
|
296
|
+
}
|
|
297
|
+
return mappedMap;
|
|
298
|
+
}
|
|
299
|
+
reduce(callback, initialValue) {
|
|
300
|
+
let accumulator = initialValue;
|
|
301
|
+
for (const element of this) {
|
|
302
|
+
accumulator = callback(accumulator, element, this);
|
|
303
|
+
}
|
|
304
|
+
return accumulator;
|
|
305
|
+
}
|
|
276
306
|
/**
|
|
277
307
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
278
308
|
* Space Complexity: O(1)
|
|
@@ -20,5 +20,5 @@ export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Paramet
|
|
|
20
20
|
export declare const getMSB: (value: number) => number;
|
|
21
21
|
export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
|
|
22
22
|
export declare const throwRangeError: (message?: string) => void;
|
|
23
|
-
export declare const
|
|
23
|
+
export declare const isWeakKey: (input: unknown) => input is object;
|
|
24
24
|
export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
|
package/dist/mjs/utils/utils.js
CHANGED
|
@@ -59,7 +59,7 @@ export const rangeCheck = (index, min, max, message = 'Index out of bounds.') =>
|
|
|
59
59
|
export const throwRangeError = (message = 'The value is off-limits.') => {
|
|
60
60
|
throw new RangeError(message);
|
|
61
61
|
};
|
|
62
|
-
export const
|
|
62
|
+
export const isWeakKey = (input) => {
|
|
63
63
|
const inputType = typeof input;
|
|
64
64
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
65
65
|
};
|