queue-typed 1.46.2 → 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)
@@ -5,39 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { IterableWithSizeOrLength, IterateDirection } from "../../types";
8
+ import { IterableWithSizeOrLength } from "../../types";
9
9
  /**
10
10
  * Deque can provide random access with O(1) time complexity
11
11
  * Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
12
12
  * Deque may experience performance jitter, but DoublyLinkedList will not
13
13
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
14
14
  */
15
- export declare class DequeIterator<E> {
16
- iterateDirection: IterateDirection;
17
- index: number;
18
- readonly deque: Deque<E>;
19
- /**
20
- * The constructor initializes the index, iterate direction, and prev/next functions for a
21
- * DequeIterator object.
22
- * @param {number} index - The index parameter represents the current index position of the iterator
23
- * within the deque. It is a number that indicates the position of the element that the iterator is
24
- * currently pointing to.
25
- * @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
26
- * double-ended queue data structure, which allows elements to be added or removed from both ends.
27
- * @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
28
- * the direction in which the iterator should iterate over the elements of the `deque`. It has a
29
- * default value of `IterateDirection.DEFAULT`.
30
- * @returns The constructor is not returning anything. It is used to initialize the properties of the
31
- * object being created.
32
- */
33
- constructor(index: number, deque: Deque<E>, iterateDirection?: IterateDirection);
34
- get current(): E;
35
- set current(newElement: E);
36
- isAccessible(): boolean;
37
- prev(): DequeIterator<E>;
38
- next(): DequeIterator<E>;
39
- clone(): DequeIterator<E>;
40
- }
41
15
  export declare class Deque<E> {
42
16
  protected _bucketFirst: number;
43
17
  protected _firstInBucket: number;
@@ -120,28 +94,14 @@ export declare class Deque<E> {
120
94
  */
121
95
  clear(): void;
122
96
  /**
123
- * The `begin()` function returns a new iterator for a deque starting from the first element.
124
- * @returns A new instance of the DequeIterator class is being returned.
125
- */
126
- begin(): DequeIterator<E>;
127
- /**
128
- * The `end()` function returns a new `DequeIterator` object with the size and reference to the
129
- * current deque.
130
- * @returns A new instance of the DequeIterator class is being returned.
97
+ * The below function is a generator that yields elements from a collection one by one.
131
98
  */
132
- end(): DequeIterator<E>;
99
+ begin(): Generator<E>;
133
100
  /**
134
- * The reverseBegin function returns a new DequeIterator object that starts at the last element of
135
- * the deque and iterates in reverse direction.
136
- * @returns A new instance of the DequeIterator class is being returned.
101
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
102
+ * the last element.
137
103
  */
138
- reverseBegin(): DequeIterator<E>;
139
- /**
140
- * The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
141
- * Deque in reverse order.
142
- * @returns A new instance of the DequeIterator class is being returned.
143
- */
144
- reverseEnd(): DequeIterator<E>;
104
+ reverseBegin(): Generator<E>;
145
105
  /**
146
106
  * Time Complexity - Amortized O(1) (possible reallocation)
147
107
  * Space Complexity - O(n) (due to potential resizing).
@@ -294,34 +254,6 @@ export declare class Deque<E> {
294
254
  * @returns The size of the data structure after the element has been deleted.
295
255
  */
296
256
  delete(element: E): number;
297
- /**
298
- * Time Complexity: O(n)
299
- * Space Complexity: O(1)
300
- */
301
- /**
302
- * Time Complexity: O(n)
303
- * Space Complexity: O(1)
304
- *
305
- * The function deletes an element from a deque using an iterator and returns the next iterator.
306
- * @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
307
- * that is used to iterate over elements in a deque (double-ended queue).
308
- * @returns the updated iterator after deleting an element from the deque.
309
- */
310
- deleteByIterator(iter: DequeIterator<E>): DequeIterator<E>;
311
- /**
312
- * Time Complexity: O(n)
313
- * Space Complexity: O(1)
314
- */
315
- /**
316
- * Time Complexity: O(n)
317
- * Space Complexity: O(1)
318
- *
319
- * The function `findIterator` searches for an element in a deque and returns an iterator pointing to
320
- * the element if found, otherwise it returns an iterator pointing to the end of the deque.
321
- * @param {E} element - The `element` parameter is the element that you want to find in the deque.
322
- * @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
323
- */
324
- findIterator(element: E): DequeIterator<E>;
325
257
  /**
326
258
  * Time Complexity: O(n)
327
259
  * Space Complexity: O(1)
@@ -7,7 +7,7 @@
7
7
  * @license MIT License
8
8
  */
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.ObjectDeque = exports.Deque = exports.DequeIterator = void 0;
10
+ exports.ObjectDeque = exports.Deque = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  /**
13
13
  * Deque can provide random access with O(1) time complexity
@@ -15,78 +15,6 @@ const utils_1 = require("../../utils");
15
15
  * Deque may experience performance jitter, but DoublyLinkedList will not
16
16
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
17
17
  */
18
- class DequeIterator {
19
- /**
20
- * The constructor initializes the index, iterate direction, and prev/next functions for a
21
- * DequeIterator object.
22
- * @param {number} index - The index parameter represents the current index position of the iterator
23
- * within the deque. It is a number that indicates the position of the element that the iterator is
24
- * currently pointing to.
25
- * @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
26
- * double-ended queue data structure, which allows elements to be added or removed from both ends.
27
- * @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
28
- * the direction in which the iterator should iterate over the elements of the `deque`. It has a
29
- * default value of `IterateDirection.DEFAULT`.
30
- * @returns The constructor is not returning anything. It is used to initialize the properties of the
31
- * object being created.
32
- */
33
- constructor(index, deque, iterateDirection = 0 /* IterateDirection.DEFAULT */) {
34
- this.index = index;
35
- this.iterateDirection = iterateDirection;
36
- if (this.iterateDirection === 0 /* IterateDirection.DEFAULT */) {
37
- this.prev = function () {
38
- if (this.index === 0) {
39
- (0, utils_1.throwRangeError)();
40
- }
41
- this.index -= 1;
42
- return this;
43
- };
44
- this.next = function () {
45
- if (this.index === this.deque.size) {
46
- (0, utils_1.throwRangeError)();
47
- }
48
- this.index += 1;
49
- return this;
50
- };
51
- }
52
- else {
53
- this.prev = function () {
54
- if (this.index === this.deque.size - 1) {
55
- (0, utils_1.throwRangeError)();
56
- }
57
- this.index += 1;
58
- return this;
59
- };
60
- this.next = function () {
61
- if (this.index === -1) {
62
- (0, utils_1.throwRangeError)();
63
- }
64
- this.index -= 1;
65
- return this;
66
- };
67
- }
68
- this.deque = deque;
69
- }
70
- get current() {
71
- return this.deque.getAt(this.index);
72
- }
73
- set current(newElement) {
74
- this.deque.setAt(this.index, newElement);
75
- }
76
- isAccessible() {
77
- return this.index !== this.deque.size;
78
- }
79
- prev() {
80
- return this;
81
- }
82
- next() {
83
- return this;
84
- }
85
- clone() {
86
- return new DequeIterator(this.index, this.deque, this.iterateDirection);
87
- }
88
- }
89
- exports.DequeIterator = DequeIterator;
90
18
  class Deque {
91
19
  /**
92
20
  * The constructor initializes a data structure with a specified bucket size and populates it with
@@ -220,35 +148,25 @@ class Deque {
220
148
  this._firstInBucket = this._lastInBucket = this._bucketSize >> 1;
221
149
  }
222
150
  /**
223
- * The `begin()` function returns a new iterator for a deque starting from the first element.
224
- * @returns A new instance of the DequeIterator class is being returned.
225
- */
226
- begin() {
227
- return new DequeIterator(0, this);
228
- }
229
- /**
230
- * The `end()` function returns a new `DequeIterator` object with the size and reference to the
231
- * current deque.
232
- * @returns A new instance of the DequeIterator class is being returned.
151
+ * The below function is a generator that yields elements from a collection one by one.
233
152
  */
234
- end() {
235
- return new DequeIterator(this.size, this);
236
- }
237
- /**
238
- * The reverseBegin function returns a new DequeIterator object that starts at the last element of
239
- * the deque and iterates in reverse direction.
240
- * @returns A new instance of the DequeIterator class is being returned.
241
- */
242
- reverseBegin() {
243
- return new DequeIterator(this.size - 1, this, 1 /* IterateDirection.REVERSE */);
153
+ *begin() {
154
+ let index = 0;
155
+ while (index < this.size) {
156
+ yield this.getAt(index);
157
+ index++;
158
+ }
244
159
  }
245
160
  /**
246
- * The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
247
- * Deque in reverse order.
248
- * @returns A new instance of the DequeIterator class is being returned.
161
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
162
+ * the last element.
249
163
  */
250
- reverseEnd() {
251
- return new DequeIterator(-1, this, 1 /* IterateDirection.REVERSE */);
164
+ *reverseBegin() {
165
+ let index = this.size - 1;
166
+ while (index >= 0) {
167
+ yield this.getAt(index);
168
+ index--;
169
+ }
252
170
  }
253
171
  /**
254
172
  * Time Complexity - Amortized O(1) (possible reallocation)
@@ -399,7 +317,7 @@ class Deque {
399
317
  * @returns The element at the specified position in the data structure is being returned.
400
318
  */
401
319
  getAt(pos) {
402
- utils_1.rangeCheck(pos, 0, this.size - 1);
320
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
403
321
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
404
322
  return this._buckets[bucketIndex][indexInBucket];
405
323
  }
@@ -418,7 +336,7 @@ class Deque {
418
336
  * position in the data structure.
419
337
  */
420
338
  setAt(pos, element) {
421
- utils_1.rangeCheck(pos, 0, this.size - 1);
339
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
422
340
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
423
341
  this._buckets[bucketIndex][indexInBucket] = element;
424
342
  }
@@ -443,7 +361,7 @@ class Deque {
443
361
  */
444
362
  insertAt(pos, element, num = 1) {
445
363
  const length = this.size;
446
- utils_1.rangeCheck(pos, 0, length);
364
+ (0, utils_1.rangeCheck)(pos, 0, length);
447
365
  if (pos === 0) {
448
366
  while (num--)
449
367
  this.unshift(element);
@@ -506,7 +424,7 @@ class Deque {
506
424
  * @returns The size of the data structure after the deletion operation is performed.
507
425
  */
508
426
  deleteAt(pos) {
509
- utils_1.rangeCheck(pos, 0, this.size - 1);
427
+ (0, utils_1.rangeCheck)(pos, 0, this.size - 1);
510
428
  if (pos === 0)
511
429
  this.shift();
512
430
  else if (pos === this.size - 1)
@@ -555,46 +473,6 @@ class Deque {
555
473
  this.cut(index - 1);
556
474
  return this.size;
557
475
  }
558
- /**
559
- * Time Complexity: O(n)
560
- * Space Complexity: O(1)
561
- */
562
- /**
563
- * Time Complexity: O(n)
564
- * Space Complexity: O(1)
565
- *
566
- * The function deletes an element from a deque using an iterator and returns the next iterator.
567
- * @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
568
- * that is used to iterate over elements in a deque (double-ended queue).
569
- * @returns the updated iterator after deleting an element from the deque.
570
- */
571
- deleteByIterator(iter) {
572
- const index = iter.index;
573
- this.deleteAt(index);
574
- iter = iter.next();
575
- return iter;
576
- }
577
- /**
578
- * Time Complexity: O(n)
579
- * Space Complexity: O(1)
580
- */
581
- /**
582
- * Time Complexity: O(n)
583
- * Space Complexity: O(1)
584
- *
585
- * The function `findIterator` searches for an element in a deque and returns an iterator pointing to
586
- * the element if found, otherwise it returns an iterator pointing to the end of the deque.
587
- * @param {E} element - The `element` parameter is the element that you want to find in the deque.
588
- * @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
589
- */
590
- findIterator(element) {
591
- for (let i = 0; i < this.size; ++i) {
592
- if (this.getAt(i) === element) {
593
- return new DequeIterator(i, this);
594
- }
595
- }
596
- return this.end();
597
- }
598
476
  /**
599
477
  * Time Complexity: O(n)
600
478
  * Space Complexity: O(1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "queue-typed",
3
- "version": "1.46.2",
3
+ "version": "1.46.3",
4
4
  "description": "Queue, ArrayQueue. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -115,6 +115,6 @@
115
115
  "typescript": "^4.9.5"
116
116
  },
117
117
  "dependencies": {
118
- "data-structure-typed": "^1.46.2"
118
+ "data-structure-typed": "^1.46.3"
119
119
  }
120
120
  }
@@ -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)
@@ -7,8 +7,8 @@
7
7
  */
8
8
 
9
9
 
10
- import { IterableWithSizeOrLength, IterateDirection } from "../../types";
11
- import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
10
+ import { IterableWithSizeOrLength } from "../../types";
11
+ import { calcMinUnitsRequired, rangeCheck } from "../../utils";
12
12
 
13
13
  /**
14
14
  * Deque can provide random access with O(1) time complexity
@@ -17,89 +17,6 @@ import { calcMinUnitsRequired, rangeCheck, throwRangeError } from "../../utils";
17
17
  * Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
18
18
  */
19
19
 
20
- export class DequeIterator<E> {
21
- iterateDirection: IterateDirection;
22
-
23
- index: number;
24
- readonly deque: Deque<E>;
25
-
26
- /**
27
- * The constructor initializes the index, iterate direction, and prev/next functions for a
28
- * DequeIterator object.
29
- * @param {number} index - The index parameter represents the current index position of the iterator
30
- * within the deque. It is a number that indicates the position of the element that the iterator is
31
- * currently pointing to.
32
- * @param deque - The `deque` parameter is an instance of the `Deque` class. It represents a
33
- * double-ended queue data structure, which allows elements to be added or removed from both ends.
34
- * @param iterateDirection - The `iterateDirection` parameter is an optional parameter that specifies
35
- * the direction in which the iterator should iterate over the elements of the `deque`. It has a
36
- * default value of `IterateDirection.DEFAULT`.
37
- * @returns The constructor is not returning anything. It is used to initialize the properties of the
38
- * object being created.
39
- */
40
- constructor(index: number, deque: Deque<E>, iterateDirection = IterateDirection.DEFAULT) {
41
- this.index = index;
42
- this.iterateDirection = iterateDirection;
43
- if (this.iterateDirection === IterateDirection.DEFAULT) {
44
- this.prev = function () {
45
- if (this.index === 0) {
46
- throwRangeError();
47
- }
48
- this.index -= 1;
49
- return this;
50
- };
51
- this.next = function () {
52
- if (this.index === this.deque.size) {
53
- throwRangeError();
54
- }
55
- this.index += 1;
56
- return this;
57
- };
58
- } else {
59
- this.prev = function () {
60
- if (this.index === this.deque.size - 1) {
61
- throwRangeError();
62
- }
63
- this.index += 1;
64
- return this;
65
- };
66
- this.next = function () {
67
- if (this.index === -1) {
68
- throwRangeError();
69
- }
70
- this.index -= 1;
71
- return this;
72
- };
73
- }
74
- this.deque = deque;
75
- }
76
-
77
- get current() {
78
- return this.deque.getAt(this.index);
79
- }
80
-
81
- set current(newElement: E) {
82
- this.deque.setAt(this.index, newElement);
83
- }
84
-
85
- isAccessible() {
86
- return this.index !== this.deque.size;
87
- }
88
-
89
- prev(): DequeIterator<E> {
90
- return this;
91
- }
92
-
93
- next(): DequeIterator<E> {
94
- return this;
95
- }
96
-
97
- clone() {
98
- return new DequeIterator<E>(this.index, this.deque, this.iterateDirection);
99
- }
100
-
101
- }
102
-
103
20
  export class Deque<E> {
104
21
  protected _bucketFirst = 0;
105
22
  protected _firstInBucket = 0;
@@ -123,7 +40,7 @@ export class Deque<E> {
123
40
  if ('length' in elements) {
124
41
  if (elements.length instanceof Function) _size = elements.length(); else _size = elements.length;
125
42
  } else {
126
- if (elements.size instanceof Function) _size = elements.size();else _size = elements.size;
43
+ if (elements.size instanceof Function) _size = elements.size(); else _size = elements.size;
127
44
  }
128
45
 
129
46
  this._bucketSize = bucketSize;
@@ -245,38 +162,26 @@ export class Deque<E> {
245
162
  }
246
163
 
247
164
  /**
248
- * The `begin()` function returns a new iterator for a deque starting from the first element.
249
- * @returns A new instance of the DequeIterator class is being returned.
250
- */
251
- begin() {
252
- return new DequeIterator<E>(0, this);
253
- }
254
-
255
- /**
256
- * The `end()` function returns a new `DequeIterator` object with the size and reference to the
257
- * current deque.
258
- * @returns A new instance of the DequeIterator class is being returned.
165
+ * The below function is a generator that yields elements from a collection one by one.
259
166
  */
260
- end() {
261
- return new DequeIterator<E>(this.size, this);
262
- }
263
-
264
- /**
265
- * The reverseBegin function returns a new DequeIterator object that starts at the last element of
266
- * the deque and iterates in reverse direction.
267
- * @returns A new instance of the DequeIterator class is being returned.
268
- */
269
- reverseBegin() {
270
- return new DequeIterator<E>(this.size - 1, this, IterateDirection.REVERSE);
167
+ * begin(): Generator<E> {
168
+ let index = 0;
169
+ while (index < this.size) {
170
+ yield this.getAt(index);
171
+ index++;
172
+ }
271
173
  }
272
174
 
273
175
  /**
274
- * The reverseEnd() function returns a new DequeIterator object that iterates over the elements of a
275
- * Deque in reverse order.
276
- * @returns A new instance of the DequeIterator class is being returned.
176
+ * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
177
+ * the last element.
277
178
  */
278
- reverseEnd() {
279
- return new DequeIterator<E>(-1, this, IterateDirection.REVERSE);
179
+ * reverseBegin(): Generator<E> {
180
+ let index = this.size - 1;
181
+ while (index >= 0) {
182
+ yield this.getAt(index);
183
+ index--;
184
+ }
280
185
  }
281
186
 
282
187
  /**
@@ -431,7 +336,7 @@ export class Deque<E> {
431
336
  * @returns The element at the specified position in the data structure is being returned.
432
337
  */
433
338
  getAt(pos: number): E {
434
- rangeCheck!(pos, 0, this.size - 1);
339
+ rangeCheck(pos, 0, this.size - 1);
435
340
  const {
436
341
  bucketIndex,
437
342
  indexInBucket
@@ -456,7 +361,7 @@ export class Deque<E> {
456
361
  * position in the data structure.
457
362
  */
458
363
  setAt(pos: number, element: E) {
459
- rangeCheck!(pos, 0, this.size - 1);
364
+ rangeCheck(pos, 0, this.size - 1);
460
365
  const {
461
366
  bucketIndex,
462
367
  indexInBucket
@@ -486,7 +391,7 @@ export class Deque<E> {
486
391
  */
487
392
  insertAt(pos: number, element: E, num = 1) {
488
393
  const length = this.size;
489
- rangeCheck!(pos, 0, length);
394
+ rangeCheck(pos, 0, length);
490
395
  if (pos === 0) {
491
396
  while (num--) this.unshift(element);
492
397
  } else if (pos === this.size) {
@@ -550,7 +455,7 @@ export class Deque<E> {
550
455
  * @returns The size of the data structure after the deletion operation is performed.
551
456
  */
552
457
  deleteAt(pos: number) {
553
- rangeCheck!(pos, 0, this.size - 1);
458
+ rangeCheck(pos, 0, this.size - 1);
554
459
  if (pos === 0) this.shift();
555
460
  else if (pos === this.size - 1) this.pop();
556
461
  else {
@@ -605,50 +510,6 @@ export class Deque<E> {
605
510
  return this.size;
606
511
  }
607
512
 
608
- /**
609
- * Time Complexity: O(n)
610
- * Space Complexity: O(1)
611
- */
612
-
613
- /**
614
- * Time Complexity: O(n)
615
- * Space Complexity: O(1)
616
- *
617
- * The function deletes an element from a deque using an iterator and returns the next iterator.
618
- * @param iter - The parameter `iter` is of type `DequeIterator<E>`. It represents an iterator object
619
- * that is used to iterate over elements in a deque (double-ended queue).
620
- * @returns the updated iterator after deleting an element from the deque.
621
- */
622
- deleteByIterator(iter: DequeIterator<E>) {
623
- const index = iter.index;
624
- this.deleteAt(index);
625
- iter = iter.next();
626
- return iter;
627
- }
628
-
629
- /**
630
- * Time Complexity: O(n)
631
- * Space Complexity: O(1)
632
- */
633
-
634
- /**
635
- * Time Complexity: O(n)
636
- * Space Complexity: O(1)
637
- *
638
- * The function `findIterator` searches for an element in a deque and returns an iterator pointing to
639
- * the element if found, otherwise it returns an iterator pointing to the end of the deque.
640
- * @param {E} element - The `element` parameter is the element that you want to find in the deque.
641
- * @returns The method `findIterator(element: E)` returns a `DequeIterator<E>` object.
642
- */
643
- findIterator(element: E) {
644
- for (let i = 0; i < this.size; ++i) {
645
- if (this.getAt(i) === element) {
646
- return new DequeIterator<E>(i, this);
647
- }
648
- }
649
- return this.end();
650
- }
651
-
652
513
  /**
653
514
  * Time Complexity: O(n)
654
515
  * Space Complexity: O(1)