queue-typed 1.45.3 → 1.46.2

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,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { HashMapLinkedNode, HashMapOptions, IterateDirection } from '../../types';
8
+ import { HashMapLinkedNode, IterableWithSizeOrLength, IterateDirection } from '../../types';
9
9
  /**
10
10
  * Because the implementation of HashMap relies on JavaScript's built-in objects and arrays,
11
11
  * these underlying structures have already dealt with dynamic expansion and hash collisions.
@@ -45,6 +45,7 @@ export declare class HashMapIterator<K, V> {
45
45
  isAccessible(): boolean;
46
46
  prev(): this;
47
47
  next(): this;
48
+ clone(): HashMapIterator<K, V>;
48
49
  }
49
50
  export declare class HashMap<K = any, V = any> {
50
51
  readonly OBJ_KEY_INDEX: symbol;
@@ -55,11 +56,11 @@ export declare class HashMap<K = any, V = any> {
55
56
  protected readonly _sentinel: HashMapLinkedNode<K, V>;
56
57
  /**
57
58
  * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
58
- * @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
59
+ * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
59
60
  * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
60
61
  * `K` represents the type of the key and `V` represents the
61
62
  */
62
- constructor(hashMap?: HashMapOptions<[K, V]>);
63
+ constructor(elements?: IterableWithSizeOrLength<[K, V]>);
63
64
  protected _size: number;
64
65
  get size(): number;
65
66
  /**
@@ -105,7 +106,7 @@ export declare class HashMap<K = any, V = any> {
105
106
  * @returns The front element of the data structure, represented as a tuple with a key (K) and a
106
107
  * value (V).
107
108
  */
108
- get front(): [K, V] | undefined;
109
+ get first(): [K, V] | undefined;
109
110
  /**
110
111
  * Time Complexity: O(1)
111
112
  * Space Complexity: O(1)
@@ -114,7 +115,7 @@ export declare class HashMap<K = any, V = any> {
114
115
  * @returns The method is returning an array containing the key-value pair of the tail element in the
115
116
  * data structure.
116
117
  */
117
- get back(): [K, V] | undefined;
118
+ get last(): [K, V] | undefined;
118
119
  /**
119
120
  * Time Complexity: O(1)
120
121
  * Space Complexity: O(1)
@@ -109,16 +109,19 @@ class HashMapIterator {
109
109
  next() {
110
110
  return this;
111
111
  }
112
+ clone() {
113
+ return new HashMapIterator(this._node, this._sentinel, this.hashMap, this.iterateDirection);
114
+ }
112
115
  }
113
116
  exports.HashMapIterator = HashMapIterator;
114
117
  class HashMap {
115
118
  /**
116
119
  * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
117
- * @param hashMap - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
120
+ * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
118
121
  * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
119
122
  * `K` represents the type of the key and `V` represents the
120
123
  */
121
- constructor(hashMap = []) {
124
+ constructor(elements = []) {
122
125
  this.OBJ_KEY_INDEX = Symbol('OBJ_KEY_INDEX');
123
126
  this._nodes = [];
124
127
  this._orgMap = {};
@@ -126,9 +129,9 @@ class HashMap {
126
129
  Object.setPrototypeOf(this._orgMap, null);
127
130
  this._sentinel = {};
128
131
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
129
- hashMap.forEach(el => {
132
+ for (const el of elements) {
130
133
  this.set(el[0], el[1]);
131
- });
134
+ }
132
135
  }
133
136
  get size() {
134
137
  return this._size;
@@ -184,7 +187,7 @@ class HashMap {
184
187
  * @returns The front element of the data structure, represented as a tuple with a key (K) and a
185
188
  * value (V).
186
189
  */
187
- get front() {
190
+ get first() {
188
191
  if (this._size === 0)
189
192
  return;
190
193
  return [this._head.key, this._head.value];
@@ -197,7 +200,7 @@ class HashMap {
197
200
  * @returns The method is returning an array containing the key-value pair of the tail element in the
198
201
  * data structure.
199
202
  */
200
- get back() {
203
+ get last() {
201
204
  if (this._size === 0)
202
205
  return;
203
206
  return [this._tail.key, this._tail.value];
@@ -8,10 +8,10 @@ import type { Comparator, DFSOrderPattern } from '../../types';
8
8
  export declare class Heap<E = any> {
9
9
  constructor(options: {
10
10
  comparator: Comparator<E>;
11
- nodes?: E[];
11
+ elements?: E[];
12
12
  });
13
- protected _nodes: E[];
14
- get nodes(): E[];
13
+ protected _elements: E[];
14
+ get elements(): E[];
15
15
  protected _comparator: Comparator<E>;
16
16
  get comparator(): Comparator<E>;
17
17
  /**
@@ -24,20 +24,20 @@ export declare class Heap<E = any> {
24
24
  */
25
25
  get leaf(): E | undefined;
26
26
  /**
27
- * Static method that creates a binary heap from an array of nodes and a comparison function.
27
+ * Static method that creates a binary heap from an array of elements and a comparison function.
28
28
  * @returns A new Heap instance.
29
29
  * @param options
30
30
  */
31
31
  static heapify<E>(options: {
32
- nodes: E[];
32
+ elements: E[];
33
33
  comparator: Comparator<E>;
34
34
  }): Heap<E>;
35
35
  /**
36
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
36
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
37
37
  * Space Complexity: O(1)
38
38
  */
39
39
  /**
40
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
40
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
41
41
  * Space Complexity: O(1)
42
42
  *
43
43
  * Insert an element into the heap and maintain the heap properties.
@@ -45,11 +45,11 @@ export declare class Heap<E = any> {
45
45
  */
46
46
  add(element: E): Heap<E>;
47
47
  /**
48
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
48
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
49
49
  * Space Complexity: O(1)
50
50
  */
51
51
  /**
52
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
52
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
53
53
  * Space Complexity: O(1)
54
54
  *
55
55
  * Insert an element into the heap and maintain the heap properties.
@@ -57,11 +57,11 @@ export declare class Heap<E = any> {
57
57
  */
58
58
  push(element: E): Heap<E>;
59
59
  /**
60
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
60
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
61
61
  * Space Complexity: O(1)
62
62
  */
63
63
  /**
64
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
64
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
65
65
  * Space Complexity: O(1)
66
66
  *
67
67
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -69,11 +69,11 @@ export declare class Heap<E = any> {
69
69
  */
70
70
  poll(): E | undefined;
71
71
  /**
72
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
72
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
73
73
  * Space Complexity: O(1)
74
74
  */
75
75
  /**
76
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
76
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
77
77
  * Space Complexity: O(1)
78
78
  *
79
79
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -91,27 +91,27 @@ export declare class Heap<E = any> {
91
91
  */
92
92
  isEmpty(): boolean;
93
93
  /**
94
- * Reset the nodes of the heap. Make the nodes empty.
94
+ * Reset the elements of the heap. Make the elements empty.
95
95
  */
96
96
  clear(): void;
97
97
  /**
98
- * Time Complexity: O(n), where n is the number of elements in the nodes array.
98
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
99
99
  * Space Complexity: O(n)
100
100
  */
101
101
  /**
102
- * Time Complexity: O(n), where n is the number of elements in the nodes array.
102
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
103
103
  * Space Complexity: O(n)
104
104
  *
105
- * Clear and add nodes of the heap
106
- * @param nodes
105
+ * Clear and add elements of the heap
106
+ * @param elements
107
107
  */
108
- refill(nodes: E[]): void;
108
+ refill(elements: E[]): void;
109
109
  /**
110
- * Time Complexity: O(n), where n is the number of nodes in the heap.
110
+ * Time Complexity: O(n), where n is the number of elements in the heap.
111
111
  * Space Complexity: O(1)
112
112
  */
113
113
  /**
114
- * Time Complexity: O(n), where n is the number of nodes in the heap.
114
+ * Time Complexity: O(n), where n is the number of elements in the heap.
115
115
  * Space Complexity: O(1)
116
116
  *
117
117
  * Use a comparison function to check whether a binary heap contains a specific element.
@@ -120,11 +120,27 @@ export declare class Heap<E = any> {
120
120
  */
121
121
  has(element: E): boolean;
122
122
  /**
123
- * Time Complexity: O(n), where n is the number of nodes in the heap.
123
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
124
+ * Space Complexity: O(1)
125
+ */
126
+ /**
127
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
128
+ * Space Complexity: O(1)
129
+ *
130
+ * The `delete` function removes an element from an array-like data structure, maintaining the order
131
+ * and structure of the remaining elements.
132
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
133
+ * the array `this.elements`.
134
+ * @returns The `delete` function is returning a boolean value. It returns `true` if the element was
135
+ * successfully deleted from the array, and `false` if the element was not found in the array.
136
+ */
137
+ delete(element: E): boolean;
138
+ /**
139
+ * Time Complexity: O(n), where n is the number of elements in the heap.
124
140
  * Space Complexity: O(h), where h is the height of the heap.
125
141
  */
126
142
  /**
127
- * Time Complexity: O(n), where n is the number of nodes in the heap.
143
+ * Time Complexity: O(n), where n is the number of elements in the heap.
128
144
  * Space Complexity: O(h), where h is the height of the heap.
129
145
  *
130
146
  * Depth-first search (DFS) method, different traversal orders can be selected。
@@ -144,11 +160,6 @@ export declare class Heap<E = any> {
144
160
  * @returns An array containing the elements of the heap.
145
161
  */
146
162
  toArray(): E[];
147
- /**
148
- * Time Complexity: O(1)
149
- * Space Complexity: O(1)
150
- */
151
- getNodes(): E[];
152
163
  /**
153
164
  * Time Complexity: O(n)
154
165
  * Space Complexity: O(n)
@@ -178,13 +189,12 @@ export declare class Heap<E = any> {
178
189
  * Space Complexity: O(1)
179
190
  */
180
191
  /**
181
- * Time Complexity: O(log n)
192
+ * Time Complexity: O(n)
182
193
  * Space Complexity: O(1)
183
194
  *
184
- * Float operation to maintain heap properties after adding an element.
185
- * @param index - The index of the newly added element.
195
+ * Fix the entire heap to maintain heap properties.
186
196
  */
187
- protected bubbleUp(index: number): void;
197
+ fix(): void;
188
198
  /**
189
199
  * Time Complexity: O(log n)
190
200
  * Space Complexity: O(1)
@@ -193,21 +203,23 @@ export declare class Heap<E = any> {
193
203
  * Time Complexity: O(log n)
194
204
  * Space Complexity: O(1)
195
205
  *
196
- * Sinking operation to maintain heap properties after removing the top element.
197
- * @param index - The index from which to start sinking.
206
+ * Float operation to maintain heap properties after adding an element.
207
+ * @param index - The index of the newly added element.
198
208
  */
199
- protected sinkDown(index: number): void;
209
+ protected _bubbleUp(index: number): void;
200
210
  /**
201
211
  * Time Complexity: O(n)
202
212
  * Space Complexity: O(1)
203
213
  */
204
214
  /**
205
- * Time Complexity: O(n)
215
+ * Time Complexity: O(log n)
206
216
  * Space Complexity: O(1)
207
217
  *
208
- * Fix the entire heap to maintain heap properties.
218
+ * Sinking operation to maintain heap properties after removing the top element.
219
+ * @param index - The index from which to start sinking.
220
+ * @param halfLength
209
221
  */
210
- protected fix(): void;
222
+ protected _sinkDown(index: number, halfLength: number): void;
211
223
  }
212
224
  export declare class FibonacciHeapNode<E> {
213
225
  element: E;
@@ -274,17 +286,17 @@ export declare class FibonacciHeap<E> {
274
286
  */
275
287
  peek(): E | undefined;
276
288
  /**
277
- * Time Complexity: O(n), where n is the number of nodes in the linked list.
289
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
278
290
  * Space Complexity: O(1)
279
291
  */
280
292
  /**
281
- * Time Complexity: O(n), where n is the number of nodes in the linked list.
293
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
282
294
  * Space Complexity: O(1)
283
295
  *
284
296
  * Get the size (number of elements) of the heap.
285
297
  * @param {FibonacciHeapNode<E>} head - The head of the linked list.
286
298
  * @protected
287
- * @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
299
+ * @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
288
300
  */
289
301
  consumeLinkedList(head?: FibonacciHeapNode<E>): FibonacciHeapNode<E>[];
290
302
  /**
@@ -296,11 +308,11 @@ export declare class FibonacciHeap<E> {
296
308
  */
297
309
  mergeWithChild(parent: FibonacciHeapNode<E>, node: FibonacciHeapNode<E>): void;
298
310
  /**
299
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
311
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
300
312
  * Space Complexity: O(1)
301
313
  */
302
314
  /**
303
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
315
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
304
316
  * Space Complexity: O(1)
305
317
  *
306
318
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -308,11 +320,11 @@ export declare class FibonacciHeap<E> {
308
320
  */
309
321
  poll(): E | undefined;
310
322
  /**
311
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
323
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
312
324
  * Space Complexity: O(1)
313
325
  */
314
326
  /**
315
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
327
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
316
328
  * Space Complexity: O(1)
317
329
  *
318
330
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -384,11 +396,11 @@ export declare class FibonacciHeap<E> {
384
396
  */
385
397
  protected link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
386
398
  /**
387
- * Time Complexity: O(n log n), where n is the number of nodes in the heap.
399
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
388
400
  * Space Complexity: O(n)
389
401
  */
390
402
  /**
391
- * Time Complexity: O(n log n), where n is the number of nodes in the heap.
403
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
392
404
  * Space Complexity: O(n)
393
405
  *
394
406
  * Remove and return the top element (smallest or largest element) from the heap.