stack-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.
@@ -5,48 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types';
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, IterableWithSizeOrLength } from '../../types';
50
9
  export declare class HashMap<K = any, V = any> {
51
10
  readonly OBJ_KEY_INDEX: symbol;
52
11
  protected _nodes: HashMapLinkedNode<K, V>[];
@@ -63,41 +22,6 @@ export declare class HashMap<K = any, V = any> {
63
22
  constructor(elements?: IterableWithSizeOrLength<[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)[], 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)[], void, unknown>;
119
52
  /**
120
53
  * Time Complexity: O(1)
121
54
  * Space Complexity: O(1)
@@ -159,20 +92,6 @@ export declare class HashMap<K = any, V = any> {
159
92
  * where `K` is the key and `V` is the value.
160
93
  */
161
94
  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
95
  /**
177
96
  * Time Complexity: O(1)
178
97
  * Space Complexity: O(1)
@@ -7,113 +7,8 @@
7
7
  * @license MIT License
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.HashMap = exports.HashMapIterator = void 0;
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
14
  * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
@@ -136,49 +31,6 @@ class HashMap {
136
31
  get size() {
137
32
  return this._size;
138
33
  }
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
34
  /**
183
35
  * Time Complexity: O(1)
184
36
  * Space Complexity: O(1)
@@ -205,6 +57,27 @@ class HashMap {
205
57
  return;
206
58
  return [this._tail.key, this._tail.value];
207
59
  }
60
+ /**
61
+ * The `begin()` function in TypeScript iterates over a linked list and yields key-value pairs.
62
+ */
63
+ *begin() {
64
+ let node = this._head;
65
+ while (node !== this._sentinel) {
66
+ yield [node.key, node.value];
67
+ node = node.next;
68
+ }
69
+ }
70
+ /**
71
+ * The function `reverseBegin()` iterates over a linked list in reverse order, yielding each node's
72
+ * key and value.
73
+ */
74
+ *reverseBegin() {
75
+ let node = this._tail;
76
+ while (node !== this._sentinel) {
77
+ yield [node.key, node.value];
78
+ node = node.prev;
79
+ }
80
+ }
208
81
  /**
209
82
  * Time Complexity: O(1)
210
83
  * Space Complexity: O(1)
@@ -305,35 +178,6 @@ class HashMap {
305
178
  }
306
179
  return [node.key, node.value];
307
180
  }
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
181
  /**
338
182
  * Time Complexity: O(1)
339
183
  * Space Complexity: O(1)