priority-queue-typed 1.46.3 → 1.46.4

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.
Files changed (39) hide show
  1. package/dist/data-structures/hash/hash-map.d.ts +22 -26
  2. package/dist/data-structures/hash/hash-map.js +105 -76
  3. package/dist/data-structures/hash/index.d.ts +0 -4
  4. package/dist/data-structures/hash/index.js +0 -4
  5. package/dist/types/data-structures/hash/hash-map.d.ts +5 -0
  6. package/dist/types/data-structures/hash/index.d.ts +0 -4
  7. package/dist/types/data-structures/hash/index.js +0 -4
  8. package/dist/utils/utils.d.ts +1 -1
  9. package/dist/utils/utils.js +3 -3
  10. package/package.json +2 -2
  11. package/src/data-structures/hash/hash-map.ts +131 -81
  12. package/src/data-structures/hash/index.ts +0 -4
  13. package/src/types/data-structures/hash/hash-map.ts +6 -0
  14. package/src/types/data-structures/hash/index.ts +0 -4
  15. package/src/utils/utils.ts +1 -1
  16. package/dist/data-structures/hash/coordinate-map.d.ts +0 -44
  17. package/dist/data-structures/hash/coordinate-map.js +0 -62
  18. package/dist/data-structures/hash/coordinate-set.d.ts +0 -36
  19. package/dist/data-structures/hash/coordinate-set.js +0 -52
  20. package/dist/data-structures/hash/tree-map.d.ts +0 -2
  21. package/dist/data-structures/hash/tree-map.js +0 -6
  22. package/dist/data-structures/hash/tree-set.d.ts +0 -2
  23. package/dist/data-structures/hash/tree-set.js +0 -6
  24. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -1
  25. package/dist/types/data-structures/hash/coordinate-map.js +0 -2
  26. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -1
  27. package/dist/types/data-structures/hash/coordinate-set.js +0 -2
  28. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  29. package/dist/types/data-structures/hash/tree-map.js +0 -2
  30. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  31. package/dist/types/data-structures/hash/tree-set.js +0 -2
  32. package/src/data-structures/hash/coordinate-map.ts +0 -63
  33. package/src/data-structures/hash/coordinate-set.ts +0 -52
  34. package/src/data-structures/hash/tree-map.ts +0 -2
  35. package/src/data-structures/hash/tree-set.ts +0 -2
  36. package/src/types/data-structures/hash/coordinate-map.ts +0 -1
  37. package/src/types/data-structures/hash/coordinate-set.ts +0 -1
  38. package/src/types/data-structures/hash/tree-map.ts +0 -1
  39. package/src/types/data-structures/hash/tree-set.ts +0 -1
@@ -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, IterableWithSizeOrLength } from '../../types';
8
+ import { HashMapLinkedNode, HashMapOptions } from '../../types';
9
9
  export declare class HashMap<K = any, V = any> {
10
- readonly OBJ_KEY_INDEX: symbol;
11
- protected _nodes: HashMapLinkedNode<K, V>[];
12
- protected _orgMap: Record<string, HashMapLinkedNode<K, V>>;
13
- protected _head: HashMapLinkedNode<K, V>;
14
- protected _tail: HashMapLinkedNode<K, V>;
15
- protected readonly _sentinel: HashMapLinkedNode<K, V>;
10
+ protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>>;
11
+ protected _objMap: WeakMap<WeakKey, 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) => WeakKey;
16
17
  /**
17
- * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
18
- * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
19
- * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
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(elements?: IterableWithSizeOrLength<[K, V]>);
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, isObjectKey?: boolean): number;
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 `_orgMap` object
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, isObjectKey?: boolean): V | undefined;
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, isObjectKey?: boolean): boolean;
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
  }
@@ -11,21 +11,27 @@ exports.HashMap = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  class HashMap {
13
13
  /**
14
- * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
15
- * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
16
- * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
17
- * `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]`.
18
17
  */
19
- constructor(elements = []) {
20
- this.OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
21
- this._nodes = [];
22
- this._orgMap = {};
18
+ constructor(options = {
19
+ elements: [],
20
+ hashFn: (key) => String(key),
21
+ objHashFn: (key) => key
22
+ }) {
23
+ this._noObjMap = {};
24
+ this._objMap = new WeakMap();
23
25
  this._size = 0;
24
- Object.setPrototypeOf(this._orgMap, null);
25
26
  this._sentinel = {};
26
27
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
27
- for (const el of elements) {
28
- this.set(el[0], el[1]);
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
+ }
29
35
  }
30
36
  }
31
37
  get size() {
@@ -88,52 +94,52 @@ class HashMap {
88
94
  * type, but typically it is a string or symbol.
89
95
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
90
96
  * 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
97
  * @returns the size of the data structure after the key-value pair has been set.
93
98
  */
94
- set(key, value, isObjectKey = (0, utils_1.isObjOrFunc)(key)) {
95
- let newTail;
96
- if (isObjectKey) {
97
- const index = key[this.OBJ_KEY_INDEX];
98
- if (index !== undefined) {
99
- this._nodes[index].value = value;
100
- return this._size;
99
+ set(key, value) {
100
+ let node;
101
+ if ((0, utils_1.isWeakKey)(key)) {
102
+ // const hash = this._objHashFn(key);
103
+ const hash = key;
104
+ node = this._objMap.get(hash);
105
+ if (node) {
106
+ // If the node already exists, update its value
107
+ node.value = value;
108
+ }
109
+ else {
110
+ // Create new node
111
+ node = { key: hash, value, prev: this._tail, next: this._sentinel };
112
+ // Add new nodes to _objMap and linked list
113
+ this._objMap.set(hash, node);
101
114
  }
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
115
  }
114
116
  else {
115
- const node = this._orgMap[key];
117
+ const hash = this._hashFn(key);
118
+ // Non-object keys are handled in the same way as the original implementation
119
+ node = this._noObjMap[hash];
116
120
  if (node) {
117
121
  node.value = value;
118
- return this._size;
119
122
  }
120
- this._orgMap[key] = newTail = {
121
- key: key,
122
- value: value,
123
- prev: this._tail,
124
- next: this._sentinel
125
- };
123
+ else {
124
+ this._noObjMap[hash] = node = {
125
+ key,
126
+ value,
127
+ prev: this._tail,
128
+ next: this._sentinel
129
+ };
130
+ }
126
131
  }
127
132
  if (this._size === 0) {
128
- this._head = newTail;
129
- this._sentinel.next = newTail;
133
+ this._head = node;
134
+ this._sentinel.next = node;
130
135
  }
131
136
  else {
132
- this._tail.next = newTail;
137
+ this._tail.next = node;
133
138
  }
134
- this._tail = newTail;
135
- this._sentinel.prev = newTail;
136
- return ++this._size;
139
+ this._tail = node;
140
+ this._sentinel.prev = node;
141
+ this._size++;
142
+ return this._size;
137
143
  }
138
144
  /**
139
145
  * Time Complexity: O(1)
@@ -143,21 +149,22 @@ class HashMap {
143
149
  * key directly or by using an index stored in the key object.
144
150
  * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
145
151
  * 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
152
  * @returns The value associated with the given key is being returned. If the key is an object key,
150
153
  * 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 `_orgMap` object
154
+ * property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
152
155
  * using the key itself. If the key is not found, `undefined` is
153
156
  */
154
- get(key, isObjectKey = (0, utils_1.isObjOrFunc)(key)) {
155
- if (isObjectKey) {
156
- const index = key[this.OBJ_KEY_INDEX];
157
- return index !== undefined ? this._nodes[index].value : undefined;
157
+ get(key) {
158
+ if ((0, utils_1.isWeakKey)(key)) {
159
+ const hash = this._objHashFn(key);
160
+ const node = this._objMap.get(hash);
161
+ return node ? node.value : undefined;
162
+ }
163
+ else {
164
+ const hash = this._hashFn(key);
165
+ const node = this._noObjMap[hash];
166
+ return node ? node.value : undefined;
158
167
  }
159
- const node = this._orgMap[key];
160
- return node ? node.value : undefined;
161
168
  }
162
169
  /**
163
170
  * Time Complexity: O(n), where n is the index.
@@ -185,28 +192,32 @@ class HashMap {
185
192
  * The `delete` function removes a key-value pair from a map-like data structure.
186
193
  * @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
187
194
  * 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
195
  * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
192
196
  * was not found.
193
197
  */
194
- delete(key, isObjectKey = (0, utils_1.isObjOrFunc)(key)) {
198
+ delete(key) {
195
199
  let node;
196
- if (isObjectKey) {
197
- const index = key[this.OBJ_KEY_INDEX];
198
- if (index === undefined)
199
- return false;
200
- delete key[this.OBJ_KEY_INDEX];
201
- node = this._nodes[index];
202
- delete this._nodes[index];
200
+ if ((0, utils_1.isWeakKey)(key)) {
201
+ const hash = this._objHashFn(key);
202
+ // Get nodes from WeakMap
203
+ node = this._objMap.get(hash);
204
+ if (!node) {
205
+ return false; // If the node does not exist, return false
206
+ }
207
+ // Remove nodes from WeakMap
208
+ this._objMap.delete(hash);
203
209
  }
204
210
  else {
205
- node = this._orgMap[key];
206
- if (node === undefined)
207
- return false;
208
- delete this._orgMap[key];
211
+ const hash = this._hashFn(key);
212
+ // Get nodes from noObjMap
213
+ node = this._noObjMap[hash];
214
+ if (!node) {
215
+ return false; // If the node does not exist, return false
216
+ }
217
+ // Remove nodes from orgMap
218
+ delete this._noObjMap[hash];
209
219
  }
220
+ // Remove node from doubly linked list
210
221
  this._deleteNode(node);
211
222
  return true;
212
223
  }
@@ -246,13 +257,7 @@ class HashMap {
246
257
  * The `clear` function clears all the elements in a data structure and resets its properties.
247
258
  */
248
259
  clear() {
249
- // const OBJ_KEY_INDEX = this.OBJ_KEY_INDEX;
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);
260
+ this._noObjMap = {};
256
261
  this._size = 0;
257
262
  this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
258
263
  }
@@ -273,6 +278,30 @@ class HashMap {
273
278
  node = node.next;
274
279
  }
275
280
  }
281
+ filter(predicate) {
282
+ const filteredMap = new HashMap();
283
+ for (const [key, value] of this) {
284
+ if (predicate([key, value], this)) {
285
+ filteredMap.set(key, value);
286
+ }
287
+ }
288
+ return filteredMap;
289
+ }
290
+ map(callback) {
291
+ const mappedMap = new HashMap();
292
+ for (const [key, value] of this) {
293
+ const newValue = callback([key, value], this);
294
+ mappedMap.set(key, newValue);
295
+ }
296
+ return mappedMap;
297
+ }
298
+ reduce(callback, initialValue) {
299
+ let accumulator = initialValue;
300
+ for (const element of this) {
301
+ accumulator = callback(accumulator, element, this);
302
+ }
303
+ return accumulator;
304
+ }
276
305
  /**
277
306
  * Time Complexity: O(n), where n is the number of elements in the HashMap.
278
307
  * Space Complexity: O(1)
@@ -1,6 +1,2 @@
1
1
  export * from './hash-table';
2
- export * from './coordinate-map';
3
- export * from './coordinate-set';
4
- export * from './tree-map';
5
- export * from './tree-set';
6
2
  export * from './hash-map';
@@ -15,8 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./hash-table"), exports);
18
- __exportStar(require("./coordinate-map"), exports);
19
- __exportStar(require("./coordinate-set"), exports);
20
- __exportStar(require("./tree-map"), exports);
21
- __exportStar(require("./tree-set"), exports);
22
18
  __exportStar(require("./hash-map"), exports);
@@ -4,3 +4,8 @@ export type HashMapLinkedNode<K, V> = {
4
4
  next: HashMapLinkedNode<K, V>;
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
+ export type HashMapOptions<K, V> = {
8
+ elements: Iterable<[K, V]>;
9
+ hashFn: (key: K) => string;
10
+ objHashFn: (key: K) => WeakKey;
11
+ };
@@ -1,7 +1,3 @@
1
- export * from './coordinate-map';
2
- export * from './coordinate-set';
3
1
  export * from './hash-map';
4
2
  export * from './hash-table';
5
- export * from './tree-map';
6
- export * from './tree-set';
7
3
  export type HashFunction<K> = (key: K) => number;
@@ -14,9 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./coordinate-map"), exports);
18
- __exportStar(require("./coordinate-set"), exports);
19
17
  __exportStar(require("./hash-map"), exports);
20
18
  __exportStar(require("./hash-table"), exports);
21
- __exportStar(require("./tree-map"), exports);
22
- __exportStar(require("./tree-set"), exports);
@@ -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 isObjOrFunc: (input: unknown) => input is Record<string, unknown> | ((...args: any[]) => any);
23
+ export declare const isWeakKey: (input: unknown) => input is WeakKey;
24
24
  export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
@@ -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.isObjOrFunc = exports.throwRangeError = exports.rangeCheck = exports.getMSB = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
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,10 +80,10 @@ const throwRangeError = (message = 'The value is off-limits.') => {
80
80
  throw new RangeError(message);
81
81
  };
82
82
  exports.throwRangeError = throwRangeError;
83
- const isObjOrFunc = (input) => {
83
+ const isWeakKey = (input) => {
84
84
  const inputType = typeof input;
85
85
  return (inputType === 'object' && input !== null) || inputType === 'function';
86
86
  };
87
- exports.isObjOrFunc = isObjOrFunc;
87
+ exports.isWeakKey = isWeakKey;
88
88
  const calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
89
89
  exports.calcMinUnitsRequired = calcMinUnitsRequired;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "priority-queue-typed",
3
- "version": "1.46.3",
3
+ "version": "1.46.4",
4
4
  "description": "Priority Queue, Min Priority Queue, Max Priority Queue. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -120,6 +120,6 @@
120
120
  "typedoc": "^0.25.1"
121
121
  },
122
122
  "dependencies": {
123
- "data-structure-typed": "^1.46.3"
123
+ "data-structure-typed": "^1.46.4"
124
124
  }
125
125
  }
@@ -6,31 +6,41 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import { isObjOrFunc, rangeCheck } from '../../utils';
10
- import { HashMapLinkedNode, IterableWithSizeOrLength } from '../../types';
9
+ import { isWeakKey, rangeCheck } from '../../utils';
10
+ import { HashMapLinkedNode, HashMapOptions } from '../../types';
11
11
 
12
12
  export class HashMap<K = any, V = any> {
13
- readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
14
- protected _nodes: HashMapLinkedNode<K, V>[] = [];
15
- protected _orgMap: Record<string, HashMapLinkedNode<K, V>> = {};
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<WeakKey, 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) => WeakKey;
19
21
 
20
22
  /**
21
- * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
22
- * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
23
- * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
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(elements: IterableWithSizeOrLength<[K, V]> = []) {
27
- Object.setPrototypeOf(this._orgMap, null);
27
+ constructor(options: HashMapOptions<K, V> = {
28
+ elements: [],
29
+ hashFn: (key: K) => String(key),
30
+ objHashFn: (key: K) => (<WeakKey>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
- for (const el of elements) {
32
- this.set(el[0], el[1]);
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,54 @@ 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, isObjectKey: boolean = isObjOrFunc(key)) {
105
- let newTail;
106
- if (isObjectKey) {
107
- const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
108
- if (index !== undefined) {
109
- this._nodes[<number>index].value = <V>value;
110
- return this._size;
113
+ set(key: K, value?: V) {
114
+ let node;
115
+
116
+ if (isWeakKey(key)) {
117
+ // const hash = this._objHashFn(key);
118
+ const hash = key;
119
+ node = this._objMap.get(hash);
120
+
121
+ if (node) {
122
+ // If the node already exists, update its value
123
+ node.value = value;
124
+ } else {
125
+ // Create new node
126
+ node = { key: <K>hash, value, prev: this._tail, next: this._sentinel };
127
+
128
+ // Add new nodes to _objMap and linked list
129
+ this._objMap.set(hash, node);
111
130
  }
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
131
  } else {
124
- const node = this._orgMap[<string>(<unknown>key)];
132
+ const hash = this._hashFn(key);
133
+ // Non-object keys are handled in the same way as the original implementation
134
+ node = this._noObjMap[hash];
125
135
  if (node) {
126
- node.value = <V>value;
127
- return this._size;
136
+ node.value = value;
137
+ } else {
138
+ this._noObjMap[hash] = node = {
139
+ key,
140
+ value,
141
+ prev: this._tail,
142
+ next: this._sentinel
143
+ };
128
144
  }
129
- this._orgMap[<string>(<unknown>key)] = newTail = {
130
- key: key,
131
- value: <V>value,
132
- prev: this._tail,
133
- next: this._sentinel
134
- };
135
145
  }
146
+
136
147
  if (this._size === 0) {
137
- this._head = newTail;
138
- this._sentinel.next = newTail;
148
+ this._head = node;
149
+ this._sentinel.next = node;
139
150
  } else {
140
- this._tail.next = newTail;
151
+ this._tail.next = node;
141
152
  }
142
- this._tail = newTail;
143
- this._sentinel.prev = newTail;
144
- return ++this._size;
153
+
154
+ this._tail = node;
155
+ this._sentinel.prev = node;
156
+ this._size++;
157
+
158
+ return this._size;
145
159
  }
146
160
 
147
161
  /**
@@ -152,21 +166,21 @@ export class HashMap<K = any, V = any> {
152
166
  * key directly or by using an index stored in the key object.
153
167
  * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
154
168
  * 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
169
  * @returns The value associated with the given key is being returned. If the key is an object key,
159
170
  * 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 `_orgMap` object
171
+ * property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
161
172
  * using the key itself. If the key is not found, `undefined` is
162
173
  */
163
- get(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
164
- if (isObjectKey) {
165
- const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
166
- return index !== undefined ? this._nodes[index].value : undefined;
174
+ get(key: K): V | undefined {
175
+ if (isWeakKey(key)) {
176
+ const hash = this._objHashFn(key);
177
+ const node = this._objMap.get(hash);
178
+ return node ? node.value : undefined;
179
+ } else {
180
+ const hash = this._hashFn(key);
181
+ const node = this._noObjMap[hash];
182
+ return node ? node.value : undefined;
167
183
  }
168
- const node = this._orgMap[<string>(<unknown>key)];
169
- return node ? node.value : undefined;
170
184
  }
171
185
 
172
186
  /**
@@ -196,25 +210,37 @@ export class HashMap<K = any, V = any> {
196
210
  * The `delete` function removes a key-value pair from a map-like data structure.
197
211
  * @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
198
212
  * 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
213
  * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
203
214
  * was not found.
204
215
  */
205
- delete(key: K, isObjectKey: boolean = isObjOrFunc(key)) {
216
+ delete(key: K) {
206
217
  let node;
207
- if (isObjectKey) {
208
- const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
209
- if (index === undefined) return false;
210
- delete (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
211
- node = this._nodes[index];
212
- delete this._nodes[index];
218
+
219
+ if (isWeakKey(key)) {
220
+ const hash = this._objHashFn(key);
221
+ // Get nodes from WeakMap
222
+ node = this._objMap.get(hash);
223
+
224
+ if (!node) {
225
+ return false; // If the node does not exist, return false
226
+ }
227
+
228
+ // Remove nodes from WeakMap
229
+ this._objMap.delete(hash);
213
230
  } else {
214
- node = this._orgMap[<string>(<unknown>key)];
215
- if (node === undefined) return false;
216
- delete this._orgMap[<string>(<unknown>key)];
231
+ const hash = this._hashFn(key);
232
+ // Get nodes from noObjMap
233
+ node = this._noObjMap[hash];
234
+
235
+ if (!node) {
236
+ return false; // If the node does not exist, return false
237
+ }
238
+
239
+ // Remove nodes from orgMap
240
+ delete this._noObjMap[hash];
217
241
  }
242
+
243
+ // Remove node from doubly linked list
218
244
  this._deleteNode(node);
219
245
  return true;
220
246
  }
@@ -257,13 +283,7 @@ export class HashMap<K = any, V = any> {
257
283
  * The `clear` function clears all the elements in a data structure and resets its properties.
258
284
  */
259
285
  clear() {
260
- // const OBJ_KEY_INDEX = this.OBJ_KEY_INDEX;
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);
286
+ this._noObjMap = {};
267
287
  this._size = 0;
268
288
  this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
269
289
  }
@@ -286,6 +306,33 @@ export class HashMap<K = any, V = any> {
286
306
  }
287
307
  }
288
308
 
309
+ filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V> {
310
+ const filteredMap = new HashMap<K, V>();
311
+ for (const [key, value] of this) {
312
+ if (predicate([key, value], this)) {
313
+ filteredMap.set(key, value);
314
+ }
315
+ }
316
+ return filteredMap;
317
+ }
318
+
319
+ map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV> {
320
+ const mappedMap = new HashMap<K, NV>();
321
+ for (const [key, value] of this) {
322
+ const newValue = callback([key, value], this);
323
+ mappedMap.set(key, newValue);
324
+ }
325
+ return mappedMap;
326
+ }
327
+
328
+ reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A {
329
+ let accumulator = initialValue;
330
+ for (const element of this) {
331
+ accumulator = callback(accumulator, element, this);
332
+ }
333
+ return accumulator;
334
+ }
335
+
289
336
  /**
290
337
  * Time Complexity: O(n), where n is the number of elements in the HashMap.
291
338
  * Space Complexity: O(1)
@@ -310,16 +357,19 @@ export class HashMap<K = any, V = any> {
310
357
  * represents a node in a linked list. It contains a key-value pair and references to the previous
311
358
  * and next nodes in the list.
312
359
  */
313
- protected _deleteNode(node: HashMapLinkedNode<K, V>) {
360
+ protected _deleteNode(node: HashMapLinkedNode<K, V | undefined>) {
314
361
  const { prev, next } = node;
315
362
  prev.next = next;
316
363
  next.prev = prev;
364
+
317
365
  if (node === this._head) {
318
366
  this._head = next;
319
367
  }
368
+
320
369
  if (node === this._tail) {
321
370
  this._tail = prev;
322
371
  }
372
+
323
373
  this._size -= 1;
324
374
  }
325
375
  }
@@ -1,6 +1,2 @@
1
1
  export * from './hash-table';
2
- export * from './coordinate-map';
3
- export * from './coordinate-set';
4
- export * from './tree-map';
5
- export * from './tree-set';
6
2
  export * from './hash-map';
@@ -4,3 +4,9 @@ export type HashMapLinkedNode<K, V> = {
4
4
  next: HashMapLinkedNode<K, V>;
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
+
8
+ export type HashMapOptions<K, V> = {
9
+ elements: Iterable<[K, V]>;
10
+ hashFn: (key: K) => string;
11
+ objHashFn: (key: K) => WeakKey
12
+ }
@@ -1,8 +1,4 @@
1
- export * from './coordinate-map';
2
- export * from './coordinate-set';
3
1
  export * from './hash-map';
4
2
  export * from './hash-table';
5
- export * from './tree-map';
6
- export * from './tree-set';
7
3
 
8
4
  export type HashFunction<K> = (key: K) => number;
@@ -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 isObjOrFunc = (input: unknown): input is Record<string, unknown> | ((...args: any[]) => any) => {
96
+ export const isWeakKey = (input: unknown): input is WeakKey => {
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
- }
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CoordinateSet = 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 CoordinateSet extends Set {
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 "has" method of the superclass and checks if a value exists in an array after
23
- * joining its elements with a specified separator.
24
- * @param {number[]} value - The parameter "value" is an array of numbers.
25
- * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
26
- * in the joined value as an argument.
27
- */
28
- has(value) {
29
- return super.has(value.join(this._joint));
30
- }
31
- /**
32
- * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
33
- * specified delimiter before calling the parent class's "add" function.
34
- * @param {number[]} value - An array of numbers
35
- * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
36
- * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
37
- */
38
- add(value) {
39
- return super.add(value.join(this._joint));
40
- }
41
- /**
42
- * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
43
- * array with a specified joint and then calling the delete method of the parent class.
44
- * @param {number[]} value - An array of numbers
45
- * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
46
- * `value` array joined together using the `_joint` property.
47
- */
48
- delete(value) {
49
- return super.delete(value.join(this._joint));
50
- }
51
- }
52
- exports.CoordinateSet = CoordinateSet;
@@ -1,2 +0,0 @@
1
- export declare class TreeMap {
2
- }
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TreeMap = void 0;
4
- class TreeMap {
5
- }
6
- exports.TreeMap = TreeMap;
@@ -1,2 +0,0 @@
1
- export declare class TreeSet {
2
- }
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TreeSet = void 0;
4
- class TreeSet {
5
- }
6
- exports.TreeSet = TreeSet;
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,63 +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 class CoordinateMap<V> extends Map<any, V> {
9
- constructor(joint?: string) {
10
- super();
11
- if (joint !== undefined) this._joint = joint;
12
- }
13
-
14
- protected _joint = '_';
15
-
16
- get joint(): string {
17
- return this._joint;
18
- }
19
-
20
- /**
21
- * The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
22
- * key array with a specified delimiter.
23
- * @param {number[]} key - The parameter "key" is an array of numbers.
24
- * @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
25
- * (`super.has`) with the `key` array joined together using the `_joint` property.
26
- */
27
- override has(key: number[]) {
28
- return super.has(key.join(this._joint));
29
- }
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
- override set(key: number[], value: V) {
40
- return super.set(key.join(this._joint), value);
41
- }
42
-
43
- /**
44
- * The function overrides the get method to join the key array with a specified joint and then calls the super get
45
- * method.
46
- * @param {number[]} key - An array of numbers
47
- * @returns The code is returning the value associated with the specified key in the map.
48
- */
49
- override get(key: number[]) {
50
- return super.get(key.join(this._joint));
51
- }
52
-
53
- /**
54
- * The function overrides the delete method and joins the key array using a specified joint character before calling
55
- * the super delete method.
56
- * @param {number[]} key - An array of numbers that represents the key to be deleted.
57
- * @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
58
- * `key` array joined together using the `_joint` property.
59
- */
60
- override delete(key: number[]) {
61
- return super.delete(key.join(this._joint));
62
- }
63
- }
@@ -1,52 +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 class CoordinateSet extends Set<any> {
9
- constructor(joint?: string) {
10
- super();
11
- if (joint !== undefined) this._joint = joint;
12
- }
13
-
14
- protected _joint = '_';
15
-
16
- get joint(): string {
17
- return this._joint;
18
- }
19
-
20
- /**
21
- * The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
22
- * joining its elements with a specified separator.
23
- * @param {number[]} value - The parameter "value" is an array of numbers.
24
- * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
25
- * in the joined value as an argument.
26
- */
27
- override has(value: number[]) {
28
- return super.has(value.join(this._joint));
29
- }
30
-
31
- /**
32
- * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
33
- * specified delimiter before calling the parent class's "add" function.
34
- * @param {number[]} value - An array of numbers
35
- * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
36
- * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
37
- */
38
- override add(value: number[]) {
39
- return super.add(value.join(this._joint));
40
- }
41
-
42
- /**
43
- * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
44
- * array with a specified joint and then calling the delete method of the parent class.
45
- * @param {number[]} value - An array of numbers
46
- * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
47
- * `value` array joined together using the `_joint` property.
48
- */
49
- override delete(value: number[]) {
50
- return super.delete(value.join(this._joint));
51
- }
52
- }
@@ -1,2 +0,0 @@
1
- export class TreeMap {
2
- }
@@ -1,2 +0,0 @@
1
- export class TreeSet {
2
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};