graph-typed 1.46.3 → 1.46.6
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/binary-tree/binary-tree.d.ts +2 -1
- package/dist/data-structures/binary-tree/binary-tree.js +29 -46
- package/dist/data-structures/binary-tree/rb-tree.js +10 -5
- package/dist/data-structures/hash/hash-map.d.ts +22 -26
- package/dist/data-structures/hash/hash-map.js +104 -76
- package/dist/data-structures/hash/index.d.ts +0 -4
- package/dist/data-structures/hash/index.js +0 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- 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/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/dist/utils/utils.d.ts +1 -1
- package/dist/utils/utils.js +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +33 -46
- package/src/data-structures/binary-tree/rb-tree.ts +9 -4
- package/src/data-structures/hash/hash-map.ts +130 -81
- package/src/data-structures/hash/index.ts +0 -4
- package/src/index.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -0
- package/src/types/data-structures/hash/index.ts +0 -4
- package/src/types/index.ts +1 -1
- 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
- /package/dist/types/{helpers.d.ts → common.d.ts} +0 -0
- /package/dist/types/{helpers.js → common.js} +0 -0
- /package/src/types/{helpers.ts → common.ts} +0 -0
|
@@ -1727,7 +1727,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1727
1727
|
|
|
1728
1728
|
/**
|
|
1729
1729
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
1730
|
-
* @param {N | null | undefined}
|
|
1730
|
+
* @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
|
|
1731
1731
|
* undefined`. It represents the root node of a binary tree. The root node can have one of the
|
|
1732
1732
|
* following types:
|
|
1733
1733
|
*/
|
|
@@ -1736,61 +1736,48 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1736
1736
|
if (!beginRoot) return;
|
|
1737
1737
|
|
|
1738
1738
|
const display = (root: N | null | undefined): void => {
|
|
1739
|
-
const [lines, , ,] = _displayAux(root);
|
|
1739
|
+
const [lines, , ,] = this._displayAux(root);
|
|
1740
1740
|
for (const line of lines) {
|
|
1741
1741
|
console.log(line);
|
|
1742
1742
|
}
|
|
1743
1743
|
};
|
|
1744
1744
|
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
return [[], 0, 0, 0];
|
|
1748
|
-
}
|
|
1745
|
+
display(beginRoot);
|
|
1746
|
+
}
|
|
1749
1747
|
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
const middle = Math.floor(width / 2);
|
|
1755
|
-
return [[line], width, height, middle];
|
|
1756
|
-
}
|
|
1748
|
+
protected _displayAux(node: N | null | undefined): [string[], number, number, number] {
|
|
1749
|
+
if (!node) {
|
|
1750
|
+
return [['─'], 1, 0, 0];
|
|
1751
|
+
}
|
|
1757
1752
|
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
const s = `${node.key}`;
|
|
1761
|
-
const u = s.length;
|
|
1762
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s;
|
|
1763
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u);
|
|
1764
|
-
const shifted_lines = lines.map(line => line + ' '.repeat(u));
|
|
1765
|
-
return [[first_line, second_line, ...shifted_lines], n + u, p + 2, n + Math.floor(u / 2)];
|
|
1766
|
-
}
|
|
1753
|
+
const line = node.key.toString();
|
|
1754
|
+
const width = line.length;
|
|
1767
1755
|
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
const x = s.length;
|
|
1772
|
-
const first_line = s + '_'.repeat(x) + ' '.repeat(n - x);
|
|
1773
|
-
const second_line = ' '.repeat(u + x) + '\\' + ' '.repeat(n - x - 1);
|
|
1774
|
-
const shifted_lines = lines.map(line => ' '.repeat(u) + line);
|
|
1775
|
-
return [[first_line, second_line, ...shifted_lines], n + x, p + 2, Math.floor(u / 2)];
|
|
1776
|
-
}
|
|
1756
|
+
if (!node.left && !node.right) {
|
|
1757
|
+
return [[line], width, 1, Math.floor(width / 2)];
|
|
1758
|
+
}
|
|
1777
1759
|
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
const s = `${node.key}`;
|
|
1781
|
-
const u = s.length;
|
|
1782
|
-
const first_line = ' '.repeat(x + 1) + '_'.repeat(n - x - 1) + s + '_'.repeat(y) + ' '.repeat(m - y);
|
|
1783
|
-
const second_line = ' '.repeat(x) + '/' + ' '.repeat(n - x - 1 + u + y) + '\\' + ' '.repeat(m - y - 1);
|
|
1784
|
-
if (p < q) {
|
|
1785
|
-
left.push(...new Array(q - p).fill(' '.repeat(n)));
|
|
1786
|
-
} else if (q < p) {
|
|
1787
|
-
right.push(...new Array(p - q).fill(' '.repeat(m)));
|
|
1788
|
-
}
|
|
1789
|
-
const zipped_lines = left.map((a, i) => a + ' '.repeat(u) + right[i]);
|
|
1790
|
-
return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
|
|
1791
|
-
};
|
|
1760
|
+
const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[''], 0, 0, 0];
|
|
1761
|
+
const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[''], 0, 0, 0];
|
|
1792
1762
|
|
|
1793
|
-
|
|
1763
|
+
const firstLine = ' '.repeat(Math.max(0, leftMiddle + 1))
|
|
1764
|
+
+ '_'.repeat(Math.max(0, leftWidth - leftMiddle - 1))
|
|
1765
|
+
+ line
|
|
1766
|
+
+ '_'.repeat(Math.max(0, rightMiddle))
|
|
1767
|
+
+ ' '.repeat(Math.max(0, rightWidth - rightMiddle));
|
|
1768
|
+
|
|
1769
|
+
const secondLine = (leftHeight > 0 ? ' '.repeat(leftMiddle) + '/' + ' '.repeat(leftWidth - leftMiddle - 1) : ' '.repeat(leftWidth))
|
|
1770
|
+
+ ' '.repeat(width)
|
|
1771
|
+
+ (rightHeight > 0 ? ' '.repeat(rightMiddle) + '\\' + ' '.repeat(rightWidth - rightMiddle - 1) : ' '.repeat(rightWidth));
|
|
1772
|
+
|
|
1773
|
+
const mergedLines = [firstLine, secondLine];
|
|
1774
|
+
for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
|
|
1775
|
+
const leftLine = i < leftHeight ? leftLines[i] : ' '.repeat(leftWidth);
|
|
1776
|
+
const rightLine = i < rightHeight ? rightLines[i] : ' '.repeat(rightWidth);
|
|
1777
|
+
mergedLines.push(leftLine + ' '.repeat(width) + rightLine);
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
|
|
1794
1781
|
}
|
|
1795
1782
|
|
|
1796
1783
|
protected _defaultOneParamCallback = (node: N) => node.key;
|
|
@@ -103,11 +103,16 @@ export class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTr
|
|
|
103
103
|
|
|
104
104
|
while (x !== this.NIL) {
|
|
105
105
|
y = x;
|
|
106
|
-
if (x
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
if (x) {
|
|
107
|
+
if (node.key < x.key) {
|
|
108
|
+
x = x.left;
|
|
109
|
+
} else if (node.key > x.key) {
|
|
110
|
+
x = x?.right;
|
|
111
|
+
} else {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
110
114
|
}
|
|
115
|
+
|
|
111
116
|
}
|
|
112
117
|
|
|
113
118
|
node.parent = y;
|
|
@@ -6,31 +6,41 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
import { HashMapLinkedNode,
|
|
9
|
+
import { isWeakKey, rangeCheck } from '../../utils';
|
|
10
|
+
import { HashMapLinkedNode, HashMapOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class HashMap<K = any, V = any> {
|
|
13
|
-
|
|
14
|
-
protected
|
|
15
|
-
protected
|
|
16
|
-
protected _head: HashMapLinkedNode<K, V>;
|
|
17
|
-
protected _tail: HashMapLinkedNode<K, V>;
|
|
18
|
-
protected readonly _sentinel: HashMapLinkedNode<K, V>;
|
|
13
|
+
|
|
14
|
+
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
|
15
|
+
protected _objMap = new WeakMap<object, HashMapLinkedNode<K, V | undefined>>();
|
|
16
|
+
protected _head: HashMapLinkedNode<K, V | undefined>;
|
|
17
|
+
protected _tail: HashMapLinkedNode<K, V | undefined>;
|
|
18
|
+
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
19
|
+
protected _hashFn: (key: K) => string;
|
|
20
|
+
protected _objHashFn: (key: K) => object;
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
|
-
* The constructor initializes a
|
|
22
|
-
* @param
|
|
23
|
-
*
|
|
24
|
-
* `K` represents the type of the key and `V` represents the
|
|
23
|
+
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
24
|
+
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
25
|
+
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
25
26
|
*/
|
|
26
|
-
constructor(
|
|
27
|
-
|
|
27
|
+
constructor(options: HashMapOptions<K, V> = {
|
|
28
|
+
elements: [],
|
|
29
|
+
hashFn: (key: K) => String(key),
|
|
30
|
+
objHashFn: (key: K) => (<object>key)
|
|
31
|
+
}) {
|
|
28
32
|
this._sentinel = <HashMapLinkedNode<K, V>>{};
|
|
29
33
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
const { elements, hashFn, objHashFn } = options;
|
|
36
|
+
this._hashFn = hashFn;
|
|
37
|
+
this._objHashFn = objHashFn;
|
|
38
|
+
if (elements) {
|
|
39
|
+
for (const el of elements) {
|
|
40
|
+
this.set(el[0], el[1]);
|
|
41
|
+
}
|
|
33
42
|
}
|
|
43
|
+
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
protected _size = 0;
|
|
@@ -98,50 +108,53 @@ export class HashMap<K = any, V = any> {
|
|
|
98
108
|
* type, but typically it is a string or symbol.
|
|
99
109
|
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
100
110
|
* value associated with the key being set in the data structure.
|
|
101
|
-
* @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
|
|
102
111
|
* @returns the size of the data structure after the key-value pair has been set.
|
|
103
112
|
*/
|
|
104
|
-
set(key: K, value?: V
|
|
105
|
-
let
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
113
|
+
set(key: K, value?: V) {
|
|
114
|
+
let node;
|
|
115
|
+
|
|
116
|
+
if (isWeakKey(key)) {
|
|
117
|
+
const hash = this._objHashFn(key);
|
|
118
|
+
node = this._objMap.get(hash);
|
|
119
|
+
|
|
120
|
+
if (node) {
|
|
121
|
+
// If the node already exists, update its value
|
|
122
|
+
node.value = value;
|
|
123
|
+
} else {
|
|
124
|
+
// Create new node
|
|
125
|
+
node = { key: <K>hash, value, prev: this._tail, next: this._sentinel };
|
|
126
|
+
|
|
127
|
+
// Add new nodes to _objMap and linked list
|
|
128
|
+
this._objMap.set(hash, node);
|
|
111
129
|
}
|
|
112
|
-
Object.defineProperty(key, this.OBJ_KEY_INDEX, {
|
|
113
|
-
value: this._nodes.length,
|
|
114
|
-
configurable: true
|
|
115
|
-
});
|
|
116
|
-
newTail = {
|
|
117
|
-
key: key,
|
|
118
|
-
value: <V>value,
|
|
119
|
-
prev: this._tail,
|
|
120
|
-
next: this._sentinel
|
|
121
|
-
};
|
|
122
|
-
this._nodes.push(newTail);
|
|
123
130
|
} else {
|
|
124
|
-
const
|
|
131
|
+
const hash = this._hashFn(key);
|
|
132
|
+
// Non-object keys are handled in the same way as the original implementation
|
|
133
|
+
node = this._noObjMap[hash];
|
|
125
134
|
if (node) {
|
|
126
|
-
node.value =
|
|
127
|
-
|
|
135
|
+
node.value = value;
|
|
136
|
+
} else {
|
|
137
|
+
this._noObjMap[hash] = node = {
|
|
138
|
+
key,
|
|
139
|
+
value,
|
|
140
|
+
prev: this._tail,
|
|
141
|
+
next: this._sentinel
|
|
142
|
+
};
|
|
128
143
|
}
|
|
129
|
-
this._orgMap[<string>(<unknown>key)] = newTail = {
|
|
130
|
-
key: key,
|
|
131
|
-
value: <V>value,
|
|
132
|
-
prev: this._tail,
|
|
133
|
-
next: this._sentinel
|
|
134
|
-
};
|
|
135
144
|
}
|
|
145
|
+
|
|
136
146
|
if (this._size === 0) {
|
|
137
|
-
this._head =
|
|
138
|
-
this._sentinel.next =
|
|
147
|
+
this._head = node;
|
|
148
|
+
this._sentinel.next = node;
|
|
139
149
|
} else {
|
|
140
|
-
this._tail.next =
|
|
150
|
+
this._tail.next = node;
|
|
141
151
|
}
|
|
142
|
-
|
|
143
|
-
this.
|
|
144
|
-
|
|
152
|
+
|
|
153
|
+
this._tail = node;
|
|
154
|
+
this._sentinel.prev = node;
|
|
155
|
+
this._size++;
|
|
156
|
+
|
|
157
|
+
return this._size;
|
|
145
158
|
}
|
|
146
159
|
|
|
147
160
|
/**
|
|
@@ -152,21 +165,21 @@ export class HashMap<K = any, V = any> {
|
|
|
152
165
|
* key directly or by using an index stored in the key object.
|
|
153
166
|
* @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
|
|
154
167
|
* of any type, but typically it is a string or symbol.
|
|
155
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
156
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
|
|
157
|
-
* `key` is an object key. If `isObjectKey` is `false`, it means that `key`
|
|
158
168
|
* @returns The value associated with the given key is being returned. If the key is an object key,
|
|
159
169
|
* the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
|
|
160
|
-
* property of the key. If the key is a string key, the value is retrieved from the `
|
|
170
|
+
* property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
|
|
161
171
|
* using the key itself. If the key is not found, `undefined` is
|
|
162
172
|
*/
|
|
163
|
-
get(key: K
|
|
164
|
-
if (
|
|
165
|
-
const
|
|
166
|
-
|
|
173
|
+
get(key: K): V | undefined {
|
|
174
|
+
if (isWeakKey(key)) {
|
|
175
|
+
const hash = this._objHashFn(key);
|
|
176
|
+
const node = this._objMap.get(hash);
|
|
177
|
+
return node ? node.value : undefined;
|
|
178
|
+
} else {
|
|
179
|
+
const hash = this._hashFn(key);
|
|
180
|
+
const node = this._noObjMap[hash];
|
|
181
|
+
return node ? node.value : undefined;
|
|
167
182
|
}
|
|
168
|
-
const node = this._orgMap[<string>(<unknown>key)];
|
|
169
|
-
return node ? node.value : undefined;
|
|
170
183
|
}
|
|
171
184
|
|
|
172
185
|
/**
|
|
@@ -196,25 +209,37 @@ export class HashMap<K = any, V = any> {
|
|
|
196
209
|
* The `delete` function removes a key-value pair from a map-like data structure.
|
|
197
210
|
* @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
|
|
198
211
|
* It can be of any type, but typically it is a string or an object.
|
|
199
|
-
* @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
|
|
200
|
-
* whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
|
|
201
|
-
* `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
|
|
202
212
|
* @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
|
|
203
213
|
* was not found.
|
|
204
214
|
*/
|
|
205
|
-
delete(key: K
|
|
215
|
+
delete(key: K) {
|
|
206
216
|
let node;
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
node = this.
|
|
212
|
-
|
|
217
|
+
|
|
218
|
+
if (isWeakKey(key)) {
|
|
219
|
+
const hash = this._objHashFn(key);
|
|
220
|
+
// Get nodes from WeakMap
|
|
221
|
+
node = this._objMap.get(hash);
|
|
222
|
+
|
|
223
|
+
if (!node) {
|
|
224
|
+
return false; // If the node does not exist, return false
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Remove nodes from WeakMap
|
|
228
|
+
this._objMap.delete(hash);
|
|
213
229
|
} else {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
230
|
+
const hash = this._hashFn(key);
|
|
231
|
+
// Get nodes from noObjMap
|
|
232
|
+
node = this._noObjMap[hash];
|
|
233
|
+
|
|
234
|
+
if (!node) {
|
|
235
|
+
return false; // If the node does not exist, return false
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Remove nodes from orgMap
|
|
239
|
+
delete this._noObjMap[hash];
|
|
217
240
|
}
|
|
241
|
+
|
|
242
|
+
// Remove node from doubly linked list
|
|
218
243
|
this._deleteNode(node);
|
|
219
244
|
return true;
|
|
220
245
|
}
|
|
@@ -257,13 +282,7 @@ export class HashMap<K = any, V = any> {
|
|
|
257
282
|
* The `clear` function clears all the elements in a data structure and resets its properties.
|
|
258
283
|
*/
|
|
259
284
|
clear() {
|
|
260
|
-
|
|
261
|
-
// this._nodes.forEach(el => {
|
|
262
|
-
// delete (<Record<symbol, number>><unknown>el.key)[OBJ_KEY_INDEX];
|
|
263
|
-
// });
|
|
264
|
-
this._nodes = [];
|
|
265
|
-
this._orgMap = {};
|
|
266
|
-
Object.setPrototypeOf(this._orgMap, null);
|
|
285
|
+
this._noObjMap = {};
|
|
267
286
|
this._size = 0;
|
|
268
287
|
this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
|
|
269
288
|
}
|
|
@@ -286,6 +305,33 @@ export class HashMap<K = any, V = any> {
|
|
|
286
305
|
}
|
|
287
306
|
}
|
|
288
307
|
|
|
308
|
+
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V> {
|
|
309
|
+
const filteredMap = new HashMap<K, V>();
|
|
310
|
+
for (const [key, value] of this) {
|
|
311
|
+
if (predicate([key, value], this)) {
|
|
312
|
+
filteredMap.set(key, value);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return filteredMap;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV> {
|
|
319
|
+
const mappedMap = new HashMap<K, NV>();
|
|
320
|
+
for (const [key, value] of this) {
|
|
321
|
+
const newValue = callback([key, value], this);
|
|
322
|
+
mappedMap.set(key, newValue);
|
|
323
|
+
}
|
|
324
|
+
return mappedMap;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A {
|
|
328
|
+
let accumulator = initialValue;
|
|
329
|
+
for (const element of this) {
|
|
330
|
+
accumulator = callback(accumulator, element, this);
|
|
331
|
+
}
|
|
332
|
+
return accumulator;
|
|
333
|
+
}
|
|
334
|
+
|
|
289
335
|
/**
|
|
290
336
|
* Time Complexity: O(n), where n is the number of elements in the HashMap.
|
|
291
337
|
* Space Complexity: O(1)
|
|
@@ -310,16 +356,19 @@ export class HashMap<K = any, V = any> {
|
|
|
310
356
|
* represents a node in a linked list. It contains a key-value pair and references to the previous
|
|
311
357
|
* and next nodes in the list.
|
|
312
358
|
*/
|
|
313
|
-
protected _deleteNode(node: HashMapLinkedNode<K, V>) {
|
|
359
|
+
protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>) {
|
|
314
360
|
const { prev, next } = node;
|
|
315
361
|
prev.next = next;
|
|
316
362
|
next.prev = prev;
|
|
363
|
+
|
|
317
364
|
if (node === this._head) {
|
|
318
365
|
this._head = next;
|
|
319
366
|
}
|
|
367
|
+
|
|
320
368
|
if (node === this._tail) {
|
|
321
369
|
this._tail = prev;
|
|
322
370
|
}
|
|
371
|
+
|
|
323
372
|
this._size -= 1;
|
|
324
373
|
}
|
|
325
374
|
}
|
package/src/index.ts
CHANGED
package/src/types/index.ts
CHANGED
package/src/utils/utils.ts
CHANGED
|
@@ -93,7 +93,7 @@ export const throwRangeError = (message = 'The value is off-limits.'): void => {
|
|
|
93
93
|
throw new RangeError(message);
|
|
94
94
|
};
|
|
95
95
|
|
|
96
|
-
export const
|
|
96
|
+
export const isWeakKey = (input: unknown): input is object => {
|
|
97
97
|
const inputType = typeof input;
|
|
98
98
|
return (inputType === 'object' && input !== null) || inputType === 'function';
|
|
99
99
|
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export declare class CoordinateMap<V> extends Map<any, V> {
|
|
9
|
-
constructor(joint?: string);
|
|
10
|
-
protected _joint: string;
|
|
11
|
-
get joint(): string;
|
|
12
|
-
/**
|
|
13
|
-
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
14
|
-
* key array with a specified delimiter.
|
|
15
|
-
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
16
|
-
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
17
|
-
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
18
|
-
*/
|
|
19
|
-
has(key: number[]): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
22
|
-
* delimiter before calling the original set method.
|
|
23
|
-
* @param {number[]} key - The key parameter is an array of numbers.
|
|
24
|
-
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
25
|
-
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
26
|
-
* (`super.set(key.join(this._joint), value)`).
|
|
27
|
-
*/
|
|
28
|
-
set(key: number[], value: V): this;
|
|
29
|
-
/**
|
|
30
|
-
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
31
|
-
* method.
|
|
32
|
-
* @param {number[]} key - An array of numbers
|
|
33
|
-
* @returns The code is returning the value associated with the specified key in the map.
|
|
34
|
-
*/
|
|
35
|
-
get(key: number[]): V | undefined;
|
|
36
|
-
/**
|
|
37
|
-
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
38
|
-
* the super delete method.
|
|
39
|
-
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
40
|
-
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
41
|
-
* `key` array joined together using the `_joint` property.
|
|
42
|
-
*/
|
|
43
|
-
delete(key: number[]): boolean;
|
|
44
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CoordinateMap = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* data-structure-typed
|
|
6
|
-
*
|
|
7
|
-
* @author Tyler Zeng
|
|
8
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
-
* @license MIT License
|
|
10
|
-
*/
|
|
11
|
-
class CoordinateMap extends Map {
|
|
12
|
-
constructor(joint) {
|
|
13
|
-
super();
|
|
14
|
-
this._joint = '_';
|
|
15
|
-
if (joint !== undefined)
|
|
16
|
-
this._joint = joint;
|
|
17
|
-
}
|
|
18
|
-
get joint() {
|
|
19
|
-
return this._joint;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
|
|
23
|
-
* key array with a specified delimiter.
|
|
24
|
-
* @param {number[]} key - The parameter "key" is an array of numbers.
|
|
25
|
-
* @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
|
|
26
|
-
* (`super.has`) with the `key` array joined together using the `_joint` property.
|
|
27
|
-
*/
|
|
28
|
-
has(key) {
|
|
29
|
-
return super.has(key.join(this._joint));
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* The function overrides the set method of a Map object to convert the key from an array to a string using a specified
|
|
33
|
-
* delimiter before calling the original set method.
|
|
34
|
-
* @param {number[]} key - The key parameter is an array of numbers.
|
|
35
|
-
* @param {V} value - The value parameter is the value that you want to associate with the specified key.
|
|
36
|
-
* @returns The `set` method is returning the result of calling the `set` method of the superclass
|
|
37
|
-
* (`super.set(key.join(this._joint), value)`).
|
|
38
|
-
*/
|
|
39
|
-
set(key, value) {
|
|
40
|
-
return super.set(key.join(this._joint), value);
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The function overrides the get method to join the key array with a specified joint and then calls the super get
|
|
44
|
-
* method.
|
|
45
|
-
* @param {number[]} key - An array of numbers
|
|
46
|
-
* @returns The code is returning the value associated with the specified key in the map.
|
|
47
|
-
*/
|
|
48
|
-
get(key) {
|
|
49
|
-
return super.get(key.join(this._joint));
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* The function overrides the delete method and joins the key array using a specified joint character before calling
|
|
53
|
-
* the super delete method.
|
|
54
|
-
* @param {number[]} key - An array of numbers that represents the key to be deleted.
|
|
55
|
-
* @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
|
|
56
|
-
* `key` array joined together using the `_joint` property.
|
|
57
|
-
*/
|
|
58
|
-
delete(key) {
|
|
59
|
-
return super.delete(key.join(this._joint));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
exports.CoordinateMap = CoordinateMap;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export declare class CoordinateSet extends Set<any> {
|
|
9
|
-
constructor(joint?: string);
|
|
10
|
-
protected _joint: string;
|
|
11
|
-
get joint(): string;
|
|
12
|
-
/**
|
|
13
|
-
* The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
|
|
14
|
-
* joining its elements with a specified separator.
|
|
15
|
-
* @param {number[]} value - The parameter "value" is an array of numbers.
|
|
16
|
-
* @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
|
|
17
|
-
* in the joined value as an argument.
|
|
18
|
-
*/
|
|
19
|
-
has(value: number[]): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
|
|
22
|
-
* specified delimiter before calling the parent class's "add" function.
|
|
23
|
-
* @param {number[]} value - An array of numbers
|
|
24
|
-
* @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
|
|
25
|
-
* (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
|
|
26
|
-
*/
|
|
27
|
-
add(value: number[]): this;
|
|
28
|
-
/**
|
|
29
|
-
* The function overrides the delete method and deletes an element from a Set by joining the elements of the input
|
|
30
|
-
* array with a specified joint and then calling the delete method of the parent class.
|
|
31
|
-
* @param {number[]} value - An array of numbers
|
|
32
|
-
* @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
|
|
33
|
-
* `value` array joined together using the `_joint` property.
|
|
34
|
-
*/
|
|
35
|
-
delete(value: number[]): boolean;
|
|
36
|
-
}
|