graph-typed 1.46.1 → 1.46.3

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.
@@ -6,127 +6,8 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import { isObjOrFunc, rangeCheck, throwRangeError } from '../../utils';
10
- import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types';
11
-
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
- export class HashMapIterator<K, V> {
18
- readonly hashMap: HashMap<K, V>;
19
- readonly iterateDirection: IterateDirection;
20
- protected _node: HashMapLinkedNode<K, V>;
21
- protected readonly _sentinel: HashMapLinkedNode<K, V>;
22
-
23
- /**
24
- * This is a constructor function for a linked list iterator in a HashMap data structure.
25
- * @param node - The `node` parameter is a reference to a `HashMapLinkedNode` object. This object
26
- * represents a node in a linked list used in a hash map data structure. It contains a key-value pair
27
- * and references to the previous and next nodes in the linked list.
28
- * @param sentinel - The `sentinel` parameter is a reference to a special node in a linked list. It
29
- * is used to mark the beginning or end of the list and is typically used in data structures like
30
- * hash maps or linked lists to simplify operations and boundary checks.
31
- * @param hashMap - A HashMap object that stores key-value pairs.
32
- * @param {IterateDirection} iterateDirection - The `iterateDirection` parameter is an optional
33
- * parameter that specifies the direction in which the iterator should iterate over the elements of
34
- * the HashMap. It can take one of the following values:
35
- * @returns The constructor does not return anything. It is used to initialize the properties and
36
- * methods of the object being created.
37
- */
38
- constructor(
39
- node: HashMapLinkedNode<K, V>,
40
- sentinel: HashMapLinkedNode<K, V>,
41
- hashMap: HashMap<K, V>,
42
- iterateDirection: IterateDirection = IterateDirection.DEFAULT
43
- ) {
44
- this._node = node;
45
- this._sentinel = sentinel;
46
- this.iterateDirection = iterateDirection;
47
-
48
- if (this.iterateDirection === IterateDirection.DEFAULT) {
49
- this.prev = function () {
50
- if (this._node.prev === this._sentinel) {
51
- throwRangeError();
52
- }
53
- this._node = this._node.prev;
54
- return this;
55
- };
56
- this.next = function () {
57
- if (this._node === this._sentinel) {
58
- throwRangeError();
59
- }
60
- this._node = this._node.next;
61
- return this;
62
- };
63
- } else {
64
- this.prev = function () {
65
- if (this._node.next === this._sentinel) {
66
- throwRangeError();
67
- }
68
- this._node = this._node.next;
69
- return this;
70
- };
71
- this.next = function () {
72
- if (this._node === this._sentinel) {
73
- throwRangeError();
74
- }
75
- this._node = this._node.prev;
76
- return this;
77
- };
78
- }
79
- this.hashMap = hashMap;
80
- }
81
-
82
- /**
83
- * The above function returns a Proxy object that allows access to the key and value of a node in a
84
- * data structure.
85
- * @returns The code is returning a Proxy object.
86
- */
87
- get current() {
88
- if (this._node === this._sentinel) {
89
- throwRangeError();
90
- }
91
-
92
- return new Proxy(<[K, V]>(<unknown>[]), {
93
- get: (target, prop: '0' | '1') => {
94
- if (prop === '0') return this._node.key;
95
- else if (prop === '1') return this._node.value;
96
- target[0] = this._node.key;
97
- target[1] = this._node.value;
98
- return target[prop];
99
- },
100
- set: (_, prop: '1', newValue: V) => {
101
- if (prop !== '1') {
102
- throw new TypeError(`prop should be string '1'`);
103
- }
104
- this._node.value = newValue;
105
- return true;
106
- }
107
- });
108
- }
109
-
110
- /**
111
- * The function checks if a node is accessible.
112
- * @returns a boolean value indicating whether the `_node` is not equal to the `_sentinel`.
113
- */
114
- isAccessible() {
115
- return this._node !== this._sentinel;
116
- }
117
-
118
- prev() {
119
- return this;
120
- }
121
-
122
- next() {
123
- return this;
124
- }
125
-
126
- clone() {
127
- return new HashMapIterator<K, V>(this._node, this._sentinel, this.hashMap, this.iterateDirection)
128
- }
129
- }
9
+ import { isObjOrFunc, rangeCheck } from '../../utils';
10
+ import { HashMapLinkedNode, IterableWithSizeOrLength } from '../../types';
130
11
 
131
12
  export class HashMap<K = any, V = any> {
132
13
  readonly OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
@@ -158,53 +39,6 @@ export class HashMap<K = any, V = any> {
158
39
  return this._size;
159
40
  }
160
41
 
161
- /**
162
- * Time Complexity: O(1)
163
- * Space Complexity: O(1)
164
- *
165
- * The function returns a new iterator object for a HashMap.
166
- * @returns A new instance of the HashMapIterator class is being returned.
167
- */
168
- get begin() {
169
- return new HashMapIterator<K, V>(this._head, this._sentinel, this);
170
- }
171
-
172
- /**
173
- * Time Complexity: O(1)
174
- * Space Complexity: O(1)
175
- *
176
- * The function returns a new HashMapIterator object with the _sentinel value as both the start and
177
- * end values.
178
- * @returns A new instance of the HashMapIterator class is being returned.
179
- */
180
- get end() {
181
- return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this);
182
- }
183
-
184
- /**
185
- * Time Complexity: O(1)
186
- * Space Complexity: O(1)
187
- *
188
- * The reverseBegin function returns a new HashMapIterator object that iterates over the elements of
189
- * a HashMap in reverse order.
190
- * @returns A new instance of the HashMapIterator class is being returned.
191
- */
192
- get reverseBegin() {
193
- return new HashMapIterator<K, V>(this._tail, this._sentinel, this, IterateDirection.REVERSE);
194
- }
195
-
196
- /**
197
- * Time Complexity: O(1)
198
- * Space Complexity: O(1)
199
- *
200
- * The reverseEnd function returns a new HashMapIterator object that iterates over the elements of a
201
- * HashMap in reverse order.
202
- * @returns A new instance of the HashMapIterator class is being returned.
203
- */
204
- get reverseEnd() {
205
- return new HashMapIterator<K, V>(this._sentinel, this._sentinel, this, IterateDirection.REVERSE);
206
- }
207
-
208
42
  /**
209
43
  * Time Complexity: O(1)
210
44
  * Space Complexity: O(1)
@@ -231,6 +65,29 @@ export class HashMap<K = any, V = any> {
231
65
  return <[K, V]>[this._tail.key, this._tail.value];
232
66
  }
233
67
 
68
+ /**
69
+ * The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
70
+ */
71
+ * begin() {
72
+ let node = this._head;
73
+ while (node !== this._sentinel) {
74
+ yield [node.key, node.value];
75
+ node = node.next;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
81
+ * key and value.
82
+ */
83
+ * reverseBegin() {
84
+ let node = this._tail;
85
+ while (node !== this._sentinel) {
86
+ yield [node.key, node.value];
87
+ node = node.prev;
88
+ }
89
+ }
90
+
234
91
  /**
235
92
  * Time Complexity: O(1)
236
93
  * Space Complexity: O(1)
@@ -332,34 +189,6 @@ export class HashMap<K = any, V = any> {
332
189
  return <[K, V]>[node.key, node.value];
333
190
  }
334
191
 
335
- /**
336
- * Time Complexity: O(1)
337
- * Space Complexity: O(1)
338
- *
339
- * The function `getIterator` returns a new instance of `HashMapIterator` based on the provided key
340
- * and whether it is an object key or not.
341
- * @param {K} key - The `key` parameter is the key used to retrieve the iterator from the HashMap. It
342
- * can be of any type, depending on how the HashMap is implemented.
343
- * @param {boolean} [isObjectKey] - The `isObjectKey` parameter is an optional boolean parameter that
344
- * indicates whether the `key` parameter is an object key. If `isObjectKey` is `true`, it means that
345
- * the `key` parameter is an object and needs to be handled differently. If `isObjectKey` is `false`
346
- * @returns a new instance of the `HashMapIterator` class.
347
- */
348
- getIterator(key: K, isObjectKey?: boolean) {
349
- let node: HashMapLinkedNode<K, V>;
350
- if (isObjectKey) {
351
- const index = (<Record<symbol, number>>(<unknown>key))[this.OBJ_KEY_INDEX];
352
- if (index === undefined) {
353
- node = this._sentinel;
354
- } else {
355
- node = this._nodes[index];
356
- }
357
- } else {
358
- node = this._orgMap[<string>(<unknown>key)] || this._sentinel;
359
- }
360
- return new HashMapIterator<K, V>(node, this._sentinel, this);
361
- }
362
-
363
192
  /**
364
193
  * Time Complexity: O(1)
365
194
  * Space Complexity: O(1)