stack-typed 1.46.2 → 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/dist/data-structures/hash/hash-map.d.ts +31 -116
- package/dist/data-structures/hash/hash-map.js +126 -254
- package/dist/data-structures/hash/index.d.ts +0 -4
- package/dist/data-structures/hash/index.js +0 -4
- package/dist/data-structures/queue/deque.d.ts +6 -74
- package/dist/data-structures/queue/deque.js +20 -142
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -0
- package/dist/types/data-structures/hash/index.d.ts +0 -4
- package/dist/types/data-structures/hash/index.js +0 -4
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +3 -3
- package/package.json +2 -2
- package/src/data-structures/hash/hash-map.ts +153 -275
- package/src/data-structures/hash/index.ts +0 -4
- package/src/data-structures/queue/deque.ts +22 -161
- 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/dist/data-structures/hash/coordinate-map.d.ts +0 -44
- package/dist/data-structures/hash/coordinate-map.js +0 -62
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -36
- package/dist/data-structures/hash/coordinate-set.js +0 -52
- package/dist/data-structures/hash/tree-map.d.ts +0 -2
- package/dist/data-structures/hash/tree-map.js +0 -6
- package/dist/data-structures/hash/tree-set.d.ts +0 -2
- package/dist/data-structures/hash/tree-set.js +0 -6
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/types/data-structures/hash/coordinate-map.js +0 -2
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/types/data-structures/hash/coordinate-set.js +0 -2
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.js +0 -2
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.js +0 -2
- 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
|
@@ -5,99 +5,23 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { HashMapLinkedNode,
|
|
9
|
-
/**
|
|
10
|
-
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
11
|
-
* these underlying structures have already dealt with dynamic expansion and hash collisions.
|
|
12
|
-
* Therefore, there is no need for additional logic to handle these issues.
|
|
13
|
-
*/
|
|
14
|
-
export declare class HashMapIterator<K, V> {
|
|
15
|
-
readonly hashMap: HashMap<K, V>;
|
|
16
|
-
readonly iterateDirection: IterateDirection;
|
|
17
|
-
protected _node: HashMapLinkedNode<K, V>;
|
|
18
|
-
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
19
|
-
/**
|
|
20
|
-
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
21
|
-
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
|
|
22
|
-
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair
|
|
23
|
-
* and references to the previous and next nodes in the linked list.
|
|
24
|
-
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
|
|
25
|
-
* is used to mark the beginning or end of the list and is typically used in data structures like
|
|
26
|
-
* hash maps or linked lists to simplify operations and boundary checks.
|
|
27
|
-
* @param hashMap - A HashMap object that stores key-value pairs.
|
|
28
|
-
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
|
|
29
|
-
* parameter that specifies the direction in which the iterator should iterate over the elements of
|
|
30
|
-
* the HashMap. It can take one of the following values:
|
|
31
|
-
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
32
|
-
* methods of the object being created.
|
|
33
|
-
*/
|
|
34
|
-
constructor(node: HashMapLinkedNode<K, V>, sentinel: HashMapLinkedNode<K, V>, hashMap: HashMap<K, V>, iterateDirection?: IterateDirection);
|
|
35
|
-
/**
|
|
36
|
-
* The above function returns a Proxy object that allows access to the key and value of a node in a
|
|
37
|
-
* data structure.
|
|
38
|
-
* @returns The code is returning a Proxy object.
|
|
39
|
-
*/
|
|
40
|
-
get current(): [K, V];
|
|
41
|
-
/**
|
|
42
|
-
* The function checks if a node is accessible.
|
|
43
|
-
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
|
|
44
|
-
*/
|
|
45
|
-
isAccessible(): boolean;
|
|
46
|
-
prev(): this;
|
|
47
|
-
next(): this;
|
|
48
|
-
clone(): HashMapIterator<K, V>;
|
|
49
|
-
}
|
|
8
|
+
import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
50
9
|
export declare class HashMap<K = any, V = any> {
|
|
51
|
-
|
|
52
|
-
protected
|
|
53
|
-
protected
|
|
54
|
-
protected
|
|
55
|
-
protected
|
|
56
|
-
protected
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* `
|
|
62
|
-
*/
|
|
63
|
-
constructor(
|
|
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;
|
|
17
|
+
/**
|
|
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
|
+
*/
|
|
22
|
+
constructor(options?: HashMapOptions<K, V>);
|
|
64
23
|
protected _size: number;
|
|
65
24
|
get size(): number;
|
|
66
|
-
/**
|
|
67
|
-
* Time Complexity: O(1)
|
|
68
|
-
* Space Complexity: O(1)
|
|
69
|
-
*
|
|
70
|
-
* The function returns a new iterator object for a HashMap.
|
|
71
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
72
|
-
*/
|
|
73
|
-
get begin(): HashMapIterator<K, V>;
|
|
74
|
-
/**
|
|
75
|
-
* Time Complexity: O(1)
|
|
76
|
-
* Space Complexity: O(1)
|
|
77
|
-
*
|
|
78
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
79
|
-
* end values.
|
|
80
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
81
|
-
*/
|
|
82
|
-
get end(): HashMapIterator<K, V>;
|
|
83
|
-
/**
|
|
84
|
-
* Time Complexity: O(1)
|
|
85
|
-
* Space Complexity: O(1)
|
|
86
|
-
*
|
|
87
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
88
|
-
* a HashMap in reverse order.
|
|
89
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
90
|
-
*/
|
|
91
|
-
get reverseBegin(): HashMapIterator<K, V>;
|
|
92
|
-
/**
|
|
93
|
-
* Time Complexity: O(1)
|
|
94
|
-
* Space Complexity: O(1)
|
|
95
|
-
*
|
|
96
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
97
|
-
* HashMap in reverse order.
|
|
98
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
99
|
-
*/
|
|
100
|
-
get reverseEnd(): HashMapIterator<K, V>;
|
|
101
25
|
/**
|
|
102
26
|
* Time Complexity: O(1)
|
|
103
27
|
* Space Complexity: O(1)
|
|
@@ -116,6 +40,15 @@ export declare class HashMap<K = any, V = any> {
|
|
|
116
40
|
* data structure.
|
|
117
41
|
*/
|
|
118
42
|
get last(): [K, V] | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
45
|
+
*/
|
|
46
|
+
begin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
49
|
+
* key and value.
|
|
50
|
+
*/
|
|
51
|
+
reverseBegin(): Generator<(K | V | undefined)[], void, unknown>;
|
|
119
52
|
/**
|
|
120
53
|
* Time Complexity: O(1)
|
|
121
54
|
* Space Complexity: O(1)
|
|
@@ -126,10 +59,9 @@ export declare class HashMap<K = any, V = any> {
|
|
|
126
59
|
* type, but typically it is a string or symbol.
|
|
127
60
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
128
61
|
* value associated with the key being set in the data structure.
|
|
129
|
-
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
130
62
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
131
63
|
*/
|
|
132
|
-
set(key: K, value?: V
|
|
64
|
+
set(key: K, value?: V): number;
|
|
133
65
|
/**
|
|
134
66
|
* Time Complexity: O(1)
|
|
135
67
|
* Space Complexity: O(1)
|
|
@@ -138,15 +70,12 @@ export declare class HashMap<K = any, V = any> {
|
|
|
138
70
|
* key directly or by using an index stored in the key object.
|
|
139
71
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
140
72
|
* of any type, but typically it is a string or symbol.
|
|
141
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
142
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
143
|
-
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
144
73
|
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
145
74
|
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
146
|
-
* 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
|
|
147
76
|
* using the key itself. If the key is not found, `undefined` is
|
|
148
77
|
*/
|
|
149
|
-
get(key: K
|
|
78
|
+
get(key: K): V | undefined;
|
|
150
79
|
/**
|
|
151
80
|
* Time Complexity: O(n), where n is the index.
|
|
152
81
|
* Space Complexity: O(1)
|
|
@@ -159,20 +88,6 @@ export declare class HashMap<K = any, V = any> {
|
|
|
159
88
|
* where `K` is the key and `V` is the value.
|
|
160
89
|
*/
|
|
161
90
|
getAt(index: number): [K, V];
|
|
162
|
-
/**
|
|
163
|
-
* Time Complexity: O(1)
|
|
164
|
-
* Space Complexity: O(1)
|
|
165
|
-
*
|
|
166
|
-
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
|
|
167
|
-
* and whether it is an object key or not.
|
|
168
|
-
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
|
|
169
|
-
* can be of any type, depending on how the HashMap is implemented.
|
|
170
|
-
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
|
|
171
|
-
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
|
|
172
|
-
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
|
|
173
|
-
* @returns a new instance of the `HashMapIterator` class.
|
|
174
|
-
*/
|
|
175
|
-
getIterator(key: K, isObjectKey?: boolean): HashMapIterator<K, V>;
|
|
176
91
|
/**
|
|
177
92
|
* Time Complexity: O(1)
|
|
178
93
|
* Space Complexity: O(1)
|
|
@@ -180,13 +95,10 @@ export declare class HashMap<K = any, V = any> {
|
|
|
180
95
|
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
181
96
|
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
182
97
|
* It can be of any type, but typically it is a string or an object.
|
|
183
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
184
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
185
|
-
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
186
98
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
187
99
|
* was not found.
|
|
188
100
|
*/
|
|
189
|
-
delete(key: K
|
|
101
|
+
delete(key: K): boolean;
|
|
190
102
|
/**
|
|
191
103
|
* Time Complexity: O(n), where n is the index.
|
|
192
104
|
* Space Complexity: O(1)
|
|
@@ -223,6 +135,9 @@ export declare class HashMap<K = any, V = any> {
|
|
|
223
135
|
* HashMap. It takes three arguments:
|
|
224
136
|
*/
|
|
225
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;
|
|
226
141
|
/**
|
|
227
142
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
228
143
|
* Space Complexity: O(1)
|
|
@@ -240,5 +155,5 @@ export declare class HashMap<K = any, V = any> {
|
|
|
240
155
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
241
156
|
* and next nodes in the list.
|
|
242
157
|
*/
|
|
243
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V>): void;
|
|
158
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>): void;
|
|
244
159
|
}
|
|
@@ -7,178 +7,36 @@
|
|
|
7
7
|
* @license MIT License
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.HashMap =
|
|
10
|
+
exports.HashMap = void 0;
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
|
-
/**
|
|
13
|
-
* Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
|
|
14
|
-
* these underlying structures have already dealt with dynamic expansion and hash collisions.
|
|
15
|
-
* Therefore, there is no need for additional logic to handle these issues.
|
|
16
|
-
*/
|
|
17
|
-
class HashMapIterator {
|
|
18
|
-
/**
|
|
19
|
-
* This is a constructor function for a linked list iterator in a HashMap data structure.
|
|
20
|
-
* @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
|
|
21
|
-
* represents a node in a linked list used in a hash map data structure. It contains a key-value pair
|
|
22
|
-
* and references to the previous and next nodes in the linked list.
|
|
23
|
-
* @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
|
|
24
|
-
* is used to mark the beginning or end of the list and is typically used in data structures like
|
|
25
|
-
* hash maps or linked lists to simplify operations and boundary checks.
|
|
26
|
-
* @param hashMap - A HashMap object that stores key-value pairs.
|
|
27
|
-
* @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
|
|
28
|
-
* parameter that specifies the direction in which the iterator should iterate over the elements of
|
|
29
|
-
* the HashMap. It can take one of the following values:
|
|
30
|
-
* @returns The constructor does not return anything. It is used to initialize the properties and
|
|
31
|
-
* methods of the object being created.
|
|
32
|
-
*/
|
|
33
|
-
constructor(node, sentinel, hashMap, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
|
|
34
|
-
this._node = node;
|
|
35
|
-
this._sentinel = sentinel;
|
|
36
|
-
this.iterateDirection = iterateDirection;
|
|
37
|
-
if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
|
|
38
|
-
this.prev = function () {
|
|
39
|
-
if (this._node.prev === this._sentinel) {
|
|
40
|
-
(0, utils_1.throwRangeError)();
|
|
41
|
-
}
|
|
42
|
-
this._node = this._node.prev;
|
|
43
|
-
return this;
|
|
44
|
-
};
|
|
45
|
-
this.next = function () {
|
|
46
|
-
if (this._node === this._sentinel) {
|
|
47
|
-
(0, utils_1.throwRangeError)();
|
|
48
|
-
}
|
|
49
|
-
this._node = this._node.next;
|
|
50
|
-
return this;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
this.prev = function () {
|
|
55
|
-
if (this._node.next === this._sentinel) {
|
|
56
|
-
(0, utils_1.throwRangeError)();
|
|
57
|
-
}
|
|
58
|
-
this._node = this._node.next;
|
|
59
|
-
return this;
|
|
60
|
-
};
|
|
61
|
-
this.next = function () {
|
|
62
|
-
if (this._node === this._sentinel) {
|
|
63
|
-
(0, utils_1.throwRangeError)();
|
|
64
|
-
}
|
|
65
|
-
this._node = this._node.prev;
|
|
66
|
-
return this;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
this.hashMap = hashMap;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* The above function returns a Proxy object that allows access to the key and value of a node in a
|
|
73
|
-
* data structure.
|
|
74
|
-
* @returns The code is returning a Proxy object.
|
|
75
|
-
*/
|
|
76
|
-
get current() {
|
|
77
|
-
if (this._node === this._sentinel) {
|
|
78
|
-
(0, utils_1.throwRangeError)();
|
|
79
|
-
}
|
|
80
|
-
return new Proxy([], {
|
|
81
|
-
get: (target, prop) => {
|
|
82
|
-
if (prop === '0')
|
|
83
|
-
return this._node.key;
|
|
84
|
-
else if (prop === '1')
|
|
85
|
-
return this._node.value;
|
|
86
|
-
target[0] = this._node.key;
|
|
87
|
-
target[1] = this._node.value;
|
|
88
|
-
return target[prop];
|
|
89
|
-
},
|
|
90
|
-
set: (_, prop, newValue) => {
|
|
91
|
-
if (prop !== '1') {
|
|
92
|
-
throw new TypeError(`prop should be string '1'`);
|
|
93
|
-
}
|
|
94
|
-
this._node.value = newValue;
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* The function checks if a node is accessible.
|
|
101
|
-
* @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
|
|
102
|
-
*/
|
|
103
|
-
isAccessible() {
|
|
104
|
-
return this._node !== this._sentinel;
|
|
105
|
-
}
|
|
106
|
-
prev() {
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
next() {
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
clone() {
|
|
113
|
-
return new HashMapIterator(this._node, this._sentinel, this.hashMap, this.iterateDirection);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
exports.HashMapIterator = HashMapIterator;
|
|
117
12
|
class HashMap {
|
|
118
13
|
/**
|
|
119
|
-
* The constructor initializes a
|
|
120
|
-
* @param
|
|
121
|
-
*
|
|
122
|
-
* `K` represents the type of the key and `V` represents the
|
|
14
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
15
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
16
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
123
17
|
*/
|
|
124
|
-
constructor(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
18
|
+
constructor(options = {
|
|
19
|
+
elements: [],
|
|
20
|
+
hashFn: (key) => String(key),
|
|
21
|
+
objHashFn: (key) => key
|
|
22
|
+
}) {
|
|
23
|
+
this._noObjMap = {};
|
|
24
|
+
this._objMap = new WeakMap();
|
|
128
25
|
this._size = 0;
|
|
129
|
-
Object.setPrototypeOf(this._orgMap, null);
|
|
130
26
|
this._sentinel = {};
|
|
131
27
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
132
|
-
|
|
133
|
-
|
|
28
|
+
const { elements, hashFn, objHashFn } = options;
|
|
29
|
+
this._hashFn = hashFn;
|
|
30
|
+
this._objHashFn = objHashFn;
|
|
31
|
+
if (elements) {
|
|
32
|
+
for (const el of elements) {
|
|
33
|
+
this.set(el[0], el[1]);
|
|
34
|
+
}
|
|
134
35
|
}
|
|
135
36
|
}
|
|
136
37
|
get size() {
|
|
137
38
|
return this._size;
|
|
138
39
|
}
|
|
139
|
-
/**
|
|
140
|
-
* Time Complexity: O(1)
|
|
141
|
-
* Space Complexity: O(1)
|
|
142
|
-
*
|
|
143
|
-
* The function returns a new iterator object for a HashMap.
|
|
144
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
145
|
-
*/
|
|
146
|
-
get begin() {
|
|
147
|
-
return new HashMapIterator(this._head, this._sentinel, this);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Time Complexity: O(1)
|
|
151
|
-
* Space Complexity: O(1)
|
|
152
|
-
*
|
|
153
|
-
* The function returns a new HashMapIterator object with the _sentinel value as both the start and
|
|
154
|
-
* end values.
|
|
155
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
156
|
-
*/
|
|
157
|
-
get end() {
|
|
158
|
-
return new HashMapIterator(this._sentinel, this._sentinel, this);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Time Complexity: O(1)
|
|
162
|
-
* Space Complexity: O(1)
|
|
163
|
-
*
|
|
164
|
-
* The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
|
|
165
|
-
* a HashMap in reverse order.
|
|
166
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
167
|
-
*/
|
|
168
|
-
get reverseBegin() {
|
|
169
|
-
return new HashMapIterator(this._tail, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Time Complexity: O(1)
|
|
173
|
-
* Space Complexity: O(1)
|
|
174
|
-
*
|
|
175
|
-
* The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
|
|
176
|
-
* HashMap in reverse order.
|
|
177
|
-
* @returns A new instance of the HashMapIterator class is being returned.
|
|
178
|
-
*/
|
|
179
|
-
get reverseEnd() {
|
|
180
|
-
return new HashMapIterator(this._sentinel, this._sentinel, this, 1 /* IterateDirection.REVERSE */);
|
|
181
|
-
}
|
|
182
40
|
/**
|
|
183
41
|
* Time Complexity: O(1)
|
|
184
42
|
* Space Complexity: O(1)
|
|
@@ -205,6 +63,27 @@ class HashMap {
|
|
|
205
63
|
return;
|
|
206
64
|
return [this._tail.key, this._tail.value];
|
|
207
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
|
|
68
|
+
*/
|
|
69
|
+
*begin() {
|
|
70
|
+
let node = this._head;
|
|
71
|
+
while (node !== this._sentinel) {
|
|
72
|
+
yield [node.key, node.value];
|
|
73
|
+
node = node.next;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
|
|
78
|
+
* key and value.
|
|
79
|
+
*/
|
|
80
|
+
*reverseBegin() {
|
|
81
|
+
let node = this._tail;
|
|
82
|
+
while (node !== this._sentinel) {
|
|
83
|
+
yield [node.key, node.value];
|
|
84
|
+
node = node.prev;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
208
87
|
/**
|
|
209
88
|
* Time Complexity: O(1)
|
|
210
89
|
* Space Complexity: O(1)
|
|
@@ -215,52 +94,51 @@ class HashMap {
|
|
|
215
94
|
* type, but typically it is a string or symbol.
|
|
216
95
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
217
96
|
* value associated with the key being set in the data structure.
|
|
218
|
-
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
219
97
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
220
98
|
*/
|
|
221
|
-
set(key, value
|
|
222
|
-
let
|
|
223
|
-
if (
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
99
|
+
set(key, value) {
|
|
100
|
+
let node;
|
|
101
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
102
|
+
const hash = this._objHashFn(key);
|
|
103
|
+
node = this._objMap.get(hash);
|
|
104
|
+
if (node) {
|
|
105
|
+
// If the node already exists, update its value
|
|
106
|
+
node.value = value;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// Create new node
|
|
110
|
+
node = { key: hash, value, prev: this._tail, next: this._sentinel };
|
|
111
|
+
// Add new nodes to _objMap and linked list
|
|
112
|
+
this._objMap.set(hash, node);
|
|
228
113
|
}
|
|
229
|
-
Object.defineProperty(key, this.OBJ_KEY_INDEX, {
|
|
230
|
-
value: this._nodes.length,
|
|
231
|
-
configurable: true
|
|
232
|
-
});
|
|
233
|
-
newTail = {
|
|
234
|
-
key: key,
|
|
235
|
-
value: value,
|
|
236
|
-
prev: this._tail,
|
|
237
|
-
next: this._sentinel
|
|
238
|
-
};
|
|
239
|
-
this._nodes.push(newTail);
|
|
240
114
|
}
|
|
241
115
|
else {
|
|
242
|
-
const
|
|
116
|
+
const hash = this._hashFn(key);
|
|
117
|
+
// Non-object keys are handled in the same way as the original implementation
|
|
118
|
+
node = this._noObjMap[hash];
|
|
243
119
|
if (node) {
|
|
244
120
|
node.value = value;
|
|
245
|
-
return this._size;
|
|
246
121
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
122
|
+
else {
|
|
123
|
+
this._noObjMap[hash] = node = {
|
|
124
|
+
key,
|
|
125
|
+
value,
|
|
126
|
+
prev: this._tail,
|
|
127
|
+
next: this._sentinel
|
|
128
|
+
};
|
|
129
|
+
}
|
|
253
130
|
}
|
|
254
131
|
if (this._size === 0) {
|
|
255
|
-
this._head =
|
|
256
|
-
this._sentinel.next =
|
|
132
|
+
this._head = node;
|
|
133
|
+
this._sentinel.next = node;
|
|
257
134
|
}
|
|
258
135
|
else {
|
|
259
|
-
this._tail.next =
|
|
136
|
+
this._tail.next = node;
|
|
260
137
|
}
|
|
261
|
-
this._tail =
|
|
262
|
-
this._sentinel.prev =
|
|
263
|
-
|
|
138
|
+
this._tail = node;
|
|
139
|
+
this._sentinel.prev = node;
|
|
140
|
+
this._size++;
|
|
141
|
+
return this._size;
|
|
264
142
|
}
|
|
265
143
|
/**
|
|
266
144
|
* Time Complexity: O(1)
|
|
@@ -270,21 +148,22 @@ class HashMap {
|
|
|
270
148
|
* key directly or by using an index stored in the key object.
|
|
271
149
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
272
150
|
* of any type, but typically it is a string or symbol.
|
|
273
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
274
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
275
|
-
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
276
151
|
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
277
152
|
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
278
|
-
* property of the key. If the key is a string key, the value is retrieved from the `
|
|
153
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
279
154
|
* using the key itself. If the key is not found, `undefined` is
|
|
280
155
|
*/
|
|
281
|
-
get(key
|
|
282
|
-
if (
|
|
283
|
-
const
|
|
284
|
-
|
|
156
|
+
get(key) {
|
|
157
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
158
|
+
const hash = this._objHashFn(key);
|
|
159
|
+
const node = this._objMap.get(hash);
|
|
160
|
+
return node ? node.value : undefined;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const hash = this._hashFn(key);
|
|
164
|
+
const node = this._noObjMap[hash];
|
|
165
|
+
return node ? node.value : undefined;
|
|
285
166
|
}
|
|
286
|
-
const node = this._orgMap[key];
|
|
287
|
-
return node ? node.value : undefined;
|
|
288
167
|
}
|
|
289
168
|
/**
|
|
290
169
|
* Time Complexity: O(n), where n is the index.
|
|
@@ -305,35 +184,6 @@ class HashMap {
|
|
|
305
184
|
}
|
|
306
185
|
return [node.key, node.value];
|
|
307
186
|
}
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(1)
|
|
310
|
-
* Space Complexity: O(1)
|
|
311
|
-
*
|
|
312
|
-
* The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
|
|
313
|
-
* and whether it is an object key or not.
|
|
314
|
-
* @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
|
|
315
|
-
* can be of any type, depending on how the HashMap is implemented.
|
|
316
|
-
* @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
|
|
317
|
-
* indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
|
|
318
|
-
* the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
|
|
319
|
-
* @returns a new instance of the `HashMapIterator` class.
|
|
320
|
-
*/
|
|
321
|
-
getIterator(key, isObjectKey) {
|
|
322
|
-
let node;
|
|
323
|
-
if (isObjectKey) {
|
|
324
|
-
const index = key[this.OBJ_KEY_INDEX];
|
|
325
|
-
if (index === undefined) {
|
|
326
|
-
node = this._sentinel;
|
|
327
|
-
}
|
|
328
|
-
else {
|
|
329
|
-
node = this._nodes[index];
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
node = this._orgMap[key] || this._sentinel;
|
|
334
|
-
}
|
|
335
|
-
return new HashMapIterator(node, this._sentinel, this);
|
|
336
|
-
}
|
|
337
187
|
/**
|
|
338
188
|
* Time Complexity: O(1)
|
|
339
189
|
* Space Complexity: O(1)
|
|
@@ -341,28 +191,32 @@ class HashMap {
|
|
|
341
191
|
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
342
192
|
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
343
193
|
* It can be of any type, but typically it is a string or an object.
|
|
344
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
345
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
346
|
-
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
347
194
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
348
195
|
* was not found.
|
|
349
196
|
*/
|
|
350
|
-
delete(key
|
|
197
|
+
delete(key) {
|
|
351
198
|
let node;
|
|
352
|
-
if (
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
199
|
+
if ((0, utils_1.isWeakKey)(key)) {
|
|
200
|
+
const hash = this._objHashFn(key);
|
|
201
|
+
// Get nodes from WeakMap
|
|
202
|
+
node = this._objMap.get(hash);
|
|
203
|
+
if (!node) {
|
|
204
|
+
return false; // If the node does not exist, return false
|
|
205
|
+
}
|
|
206
|
+
// Remove nodes from WeakMap
|
|
207
|
+
this._objMap.delete(hash);
|
|
359
208
|
}
|
|
360
209
|
else {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
210
|
+
const hash = this._hashFn(key);
|
|
211
|
+
// Get nodes from noObjMap
|
|
212
|
+
node = this._noObjMap[hash];
|
|
213
|
+
if (!node) {
|
|
214
|
+
return false; // If the node does not exist, return false
|
|
215
|
+
}
|
|
216
|
+
// Remove nodes from orgMap
|
|
217
|
+
delete this._noObjMap[hash];
|
|
365
218
|
}
|
|
219
|
+
// Remove node from doubly linked list
|
|
366
220
|
this._deleteNode(node);
|
|
367
221
|
return true;
|
|
368
222
|
}
|
|
@@ -402,13 +256,7 @@ class HashMap {
|
|
|
402
256
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
403
257
|
*/
|
|
404
258
|
clear() {
|
|
405
|
-
|
|
406
|
-
// this._nodes.forEach(el => {
|
|
407
|
-
// delete (<Record<symbol, number>><unknown>el.key)[OBJ_KEY_INDEX];
|
|
408
|
-
// });
|
|
409
|
-
this._nodes = [];
|
|
410
|
-
this._orgMap = {};
|
|
411
|
-
Object.setPrototypeOf(this._orgMap, null);
|
|
259
|
+
this._noObjMap = {};
|
|
412
260
|
this._size = 0;
|
|
413
261
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
414
262
|
}
|
|
@@ -429,6 +277,30 @@ class HashMap {
|
|
|
429
277
|
node = node.next;
|
|
430
278
|
}
|
|
431
279
|
}
|
|
280
|
+
filter(predicate) {
|
|
281
|
+
const filteredMap = new HashMap();
|
|
282
|
+
for (const [key, value] of this) {
|
|
283
|
+
if (predicate([key, value], this)) {
|
|
284
|
+
filteredMap.set(key, value);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return filteredMap;
|
|
288
|
+
}
|
|
289
|
+
map(callback) {
|
|
290
|
+
const mappedMap = new HashMap();
|
|
291
|
+
for (const [key, value] of this) {
|
|
292
|
+
const newValue = callback([key, value], this);
|
|
293
|
+
mappedMap.set(key, newValue);
|
|
294
|
+
}
|
|
295
|
+
return mappedMap;
|
|
296
|
+
}
|
|
297
|
+
reduce(callback, initialValue) {
|
|
298
|
+
let accumulator = initialValue;
|
|
299
|
+
for (const element of this) {
|
|
300
|
+
accumulator = callback(accumulator, element, this);
|
|
301
|
+
}
|
|
302
|
+
return accumulator;
|
|
303
|
+
}
|
|
432
304
|
/**
|
|
433
305
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
434
306
|
* Space Complexity: O(1)
|