doubly-linked-list-typed 2.0.5 → 2.1.0

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 (102) hide show
  1. package/README.md +14 -14
  2. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  3. package/dist/data-structures/base/iterable-element-base.js +149 -107
  4. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  5. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  6. package/dist/data-structures/base/linear-base.d.ts +250 -192
  7. package/dist/data-structures/base/linear-base.js +137 -274
  8. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  9. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  13. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  14. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  15. package/dist/data-structures/binary-tree/binary-tree.js +598 -869
  16. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  17. package/dist/data-structures/binary-tree/bst.js +505 -481
  18. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  19. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  20. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  21. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  22. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  23. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  24. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  25. package/dist/data-structures/graph/abstract-graph.js +267 -237
  26. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  27. package/dist/data-structures/graph/directed-graph.js +146 -233
  28. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  29. package/dist/data-structures/graph/map-graph.js +56 -59
  30. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  31. package/dist/data-structures/graph/undirected-graph.js +129 -149
  32. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  33. package/dist/data-structures/hash/hash-map.js +270 -457
  34. package/dist/data-structures/heap/heap.d.ts +214 -289
  35. package/dist/data-structures/heap/heap.js +340 -349
  36. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  37. package/dist/data-structures/heap/max-heap.js +11 -66
  38. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  39. package/dist/data-structures/heap/min-heap.js +11 -66
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  43. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  44. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  45. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  51. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  52. package/dist/data-structures/queue/deque.d.ts +227 -254
  53. package/dist/data-structures/queue/deque.js +309 -348
  54. package/dist/data-structures/queue/queue.d.ts +180 -201
  55. package/dist/data-structures/queue/queue.js +265 -248
  56. package/dist/data-structures/stack/stack.d.ts +124 -102
  57. package/dist/data-structures/stack/stack.js +181 -125
  58. package/dist/data-structures/trie/trie.d.ts +164 -165
  59. package/dist/data-structures/trie/trie.js +189 -172
  60. package/dist/interfaces/binary-tree.d.ts +56 -6
  61. package/dist/interfaces/graph.d.ts +16 -0
  62. package/dist/types/data-structures/base/base.d.ts +1 -1
  63. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  64. package/dist/types/utils/utils.d.ts +1 -0
  65. package/dist/utils/utils.d.ts +1 -1
  66. package/dist/utils/utils.js +2 -1
  67. package/package.json +2 -2
  68. package/src/data-structures/base/iterable-element-base.ts +238 -115
  69. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  70. package/src/data-structures/base/linear-base.ts +271 -277
  71. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  73. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  74. package/src/data-structures/binary-tree/binary-tree.ts +664 -893
  75. package/src/data-structures/binary-tree/bst.ts +568 -570
  76. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  77. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  78. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  79. package/src/data-structures/graph/abstract-graph.ts +339 -264
  80. package/src/data-structures/graph/directed-graph.ts +146 -236
  81. package/src/data-structures/graph/map-graph.ts +63 -60
  82. package/src/data-structures/graph/undirected-graph.ts +129 -152
  83. package/src/data-structures/hash/hash-map.ts +274 -496
  84. package/src/data-structures/heap/heap.ts +389 -402
  85. package/src/data-structures/heap/max-heap.ts +12 -76
  86. package/src/data-structures/heap/min-heap.ts +13 -76
  87. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  88. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  89. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  90. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  91. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  92. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  93. package/src/data-structures/queue/deque.ts +381 -357
  94. package/src/data-structures/queue/queue.ts +310 -264
  95. package/src/data-structures/stack/stack.ts +217 -131
  96. package/src/data-structures/trie/trie.ts +240 -175
  97. package/src/interfaces/binary-tree.ts +240 -6
  98. package/src/interfaces/graph.ts +37 -0
  99. package/src/types/data-structures/base/base.ts +5 -5
  100. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  101. package/src/types/utils/utils.ts +2 -0
  102. package/src/utils/utils.ts +9 -14
@@ -5,9 +5,13 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types';
8
+ import type { DequeOptions, ElementCallback, IterableElementBaseOptions, IterableWithSizeOrLength, LinearBaseOptions } from '../../types';
9
9
  import { LinearBase } from '../base/linear-base';
10
10
  /**
11
+ * Deque implemented with circular buckets allowing O(1) amortized push/pop at both ends.
12
+ * @remarks Time O(1), Space O(1)
13
+ * @template E
14
+ * @template R
11
15
  * 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
12
16
  * 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
13
17
  * 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
@@ -103,356 +107,325 @@ import { LinearBase } from '../base/linear-base';
103
107
  * console.log(maxSlidingWindow(nums, k)); // [3, 3, 5, 5, 6, 7]
104
108
  */
105
109
  export declare class Deque<E = any, R = any> extends LinearBase<E, R> {
110
+ protected _equals: (a: E, b: E) => boolean;
106
111
  /**
107
- * The constructor initializes a Deque object with optional iterable of elements and options.
108
- * @param elements - An iterable object (such as an array or a Set) that contains the initial
109
- * elements to be added to the deque. It can also be an object with a `length` or `size` property
110
- * that represents the number of elements in the iterable object. If no elements are provided, an
111
- * empty deque
112
- * @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
113
- * configuration options for the deque. In this code, it is used to set the `bucketSize` option,
114
- * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
115
- * or is not a number
112
+ * Create a Deque and optionally bulk-insert elements.
113
+ * @remarks Time O(N), Space O(N)
114
+ * @param [elements] - Iterable (or iterable-like) of elements/records to insert.
115
+ * @param [options] - Options such as bucketSize, toElementFn, and maxLen.
116
+ * @returns New Deque instance.
116
117
  */
117
118
  constructor(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
118
119
  protected _bucketSize: number;
120
+ /**
121
+ * Get the current bucket size.
122
+ * @remarks Time O(1), Space O(1)
123
+ * @returns Bucket capacity per bucket.
124
+ */
119
125
  get bucketSize(): number;
120
126
  protected _bucketFirst: number;
127
+ /**
128
+ * Get the index of the first bucket in use.
129
+ * @remarks Time O(1), Space O(1)
130
+ * @returns Zero-based bucket index.
131
+ */
121
132
  get bucketFirst(): number;
122
133
  protected _firstInBucket: number;
134
+ /**
135
+ * Get the index inside the first bucket.
136
+ * @remarks Time O(1), Space O(1)
137
+ * @returns Zero-based index within the first bucket.
138
+ */
123
139
  get firstInBucket(): number;
124
140
  protected _bucketLast: number;
141
+ /**
142
+ * Get the index of the last bucket in use.
143
+ * @remarks Time O(1), Space O(1)
144
+ * @returns Zero-based bucket index.
145
+ */
125
146
  get bucketLast(): number;
126
147
  protected _lastInBucket: number;
148
+ /**
149
+ * Get the index inside the last bucket.
150
+ * @remarks Time O(1), Space O(1)
151
+ * @returns Zero-based index within the last bucket.
152
+ */
127
153
  get lastInBucket(): number;
128
154
  protected _bucketCount: number;
155
+ /**
156
+ * Get the number of buckets allocated.
157
+ * @remarks Time O(1), Space O(1)
158
+ * @returns Bucket count.
159
+ */
129
160
  get bucketCount(): number;
130
161
  protected _buckets: E[][];
162
+ /**
163
+ * Get the internal buckets array.
164
+ * @remarks Time O(1), Space O(1)
165
+ * @returns Array of buckets storing values.
166
+ */
131
167
  get buckets(): E[][];
132
168
  protected _length: number;
169
+ /**
170
+ * Get the number of elements in the deque.
171
+ * @remarks Time O(1), Space O(1)
172
+ * @returns Current length.
173
+ */
133
174
  get length(): number;
134
175
  /**
135
- * The function returns the first element in a collection if it exists, otherwise it returns
136
- * undefined.
137
- * @returns The first element of the collection, of type E, is being returned.
176
+ * Get the first element without removing it.
177
+ * @remarks Time O(1), Space O(1)
178
+ * @returns First element or undefined.
138
179
  */
139
180
  get first(): E | undefined;
140
181
  /**
141
- * The last function returns the last element in the queue.
142
- * @return The last element in the array
182
+ * Get the last element without removing it.
183
+ * @remarks Time O(1), Space O(1)
184
+ * @returns Last element or undefined.
143
185
  */
144
186
  get last(): E | undefined;
145
187
  /**
146
- * Time Complexity - Amortized O(1) (possible reallocation),
147
- * Space Complexity - O(n) (due to potential resizing).
148
- *
149
- * The push function adds an element to a data structure and reallocates memory if necessary.
150
- * @param {E} element - The `element` parameter represents the value that you want to add to the data
151
- * structure.
152
- * @returns The size of the data structure after the element has been pushed.
188
+ * Create a Deque from an array of elements.
189
+ * @remarks Time O(N), Space O(N)
190
+ * @template E
191
+ * @template R
192
+ * @param this - Constructor (subclass) to instantiate.
193
+ * @param data - Array of elements to insert in order.
194
+ * @param [options] - Options forwarded to the constructor.
195
+ * @returns A new Deque populated from the array.
196
+ */
197
+ static fromArray<E, R = any>(this: new (elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>) => any, data: E[], options?: DequeOptions<E, R>): any;
198
+ /**
199
+ * Append one element at the back.
200
+ * @remarks Time O(1) amortized, Space O(1)
201
+ * @param element - Element to append.
202
+ * @returns True when appended.
153
203
  */
154
204
  push(element: E): boolean;
155
205
  /**
156
- * Time Complexity: O(1)
157
- * Space Complexity: O(1)
158
- *
159
- * The `pop()` function removes and returns the last element from a data structure, updating the
160
- * internal state variables accordingly.
161
- * @returns The element that was removed from the data structure is being returned.
206
+ * Remove and return the last element.
207
+ * @remarks Time O(1), Space O(1)
208
+ * @returns Removed element or undefined.
162
209
  */
163
210
  pop(): E | undefined;
164
211
  /**
165
- * Time Complexity: O(1)
166
- * Space Complexity: O(1)
167
- *
168
- * The `shift()` function removes and returns the first element from a data structure, updating the
169
- * internal state variables accordingly.
170
- * @returns The element that is being removed from the beginning of the data structure is being
171
- * returned.
212
+ * Remove and return the first element.
213
+ * @remarks Time O(1) amortized, Space O(1)
214
+ * @returns Removed element or undefined.
172
215
  */
173
216
  shift(): E | undefined;
174
217
  /**
175
- * Time Complexity: Amortized O(1)
176
- * Space Complexity: O(n)
177
- *
178
- * The `unshift` function adds an element to the beginning of an array-like data structure and
179
- * returns the new size of the structure.
180
- * @param {E} element - The `element` parameter represents the element that you want to add to the
181
- * beginning of the data structure.
182
- * @returns The size of the data structure after the element has been added.
218
+ * Prepend one element at the front.
219
+ * @remarks Time O(1) amortized, Space O(1)
220
+ * @param element - Element to prepend.
221
+ * @returns True when prepended.
183
222
  */
184
223
  unshift(element: E): boolean;
185
224
  /**
186
- * Time Complexity: O(k)
187
- * Space Complexity: O(k)
188
- *
189
- * The function `pushMany` iterates over elements and pushes them into an array after applying a
190
- * transformation function if provided.
191
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
192
- * parameter in the `pushMany` function is expected to be an iterable containing elements of type `E`
193
- * or `R`. It can be either an `IterableWithSizeOrLength<E>` or an `IterableWithSizeOrLength<R>`. The
194
- * function iterates over each element
195
- * @returns The `pushMany` function is returning an array of boolean values, where each value
196
- * represents the result of calling the `push` method on the current object instance with the
197
- * corresponding element from the input `elements` iterable.
225
+ * Append a sequence of elements.
226
+ * @remarks Time O(N), Space O(1)
227
+ * @param elements - Iterable (or iterable-like) of elements/records.
228
+ * @returns Array of per-element success flags.
198
229
  */
199
230
  pushMany(elements: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>): boolean[];
200
231
  /**
201
- * Time Complexity: O(k)
202
- * Space Complexity: O(k)
203
- *
204
- * The `unshiftMany` function in TypeScript iterates over elements and adds them to the beginning of
205
- * an array, optionally converting them using a provided function.
206
- * @param {IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>} elements - The `elements`
207
- * parameter in the `unshiftMany` function is an iterable containing elements of type `E` or `R`. It
208
- * can be an array or any other iterable data structure that has a known size or length. The function
209
- * iterates over each element in the `elements` iterable and
210
- * @returns The `unshiftMany` function returns an array of boolean values indicating whether each
211
- * element was successfully added to the beginning of the array.
232
+ * Prepend a sequence of elements.
233
+ * @remarks Time O(N), Space O(1)
234
+ * @param [elements] - Iterable (or iterable-like) of elements/records.
235
+ * @returns Array of per-element success flags.
212
236
  */
213
237
  unshiftMany(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>): boolean[];
214
238
  /**
215
- * Time Complexity: O(1)
216
- * Space Complexity: O(1)
217
- *
218
- * The function checks if the size of an object is equal to zero and returns a boolean value.
219
- * @returns A boolean value indicating whether the size of the object is 0 or not.
239
+ * Check whether the deque is empty.
240
+ * @remarks Time O(1), Space O(1)
241
+ * @returns True if length is 0.
220
242
  */
221
243
  isEmpty(): boolean;
222
244
  /**
223
- * Time Complexity: O(1)
224
- * Space Complexity: O(1)
225
- *
226
- * The clear() function resets the state of the object by initializing all variables to their default
227
- * values.
245
+ * Remove all elements and reset structure.
246
+ * @remarks Time O(1), Space O(1)
247
+ * @returns void
228
248
  */
229
249
  clear(): void;
230
250
  /**
231
- * Time Complexity: O(1)
232
- * Space Complexity: O(1)
233
- *
234
- * The `at` function retrieves an element at a specified position in an array-like data structure.
235
- * @param {number} pos - The `pos` parameter represents the position of the element that you want to
236
- * retrieve from the data structure. It is of type `number` and should be a valid index within the
237
- * range of the data structure.
238
- * @returns The element at the specified position in the data structure is being returned.
239
- */
240
- at(pos: number): E;
241
- /**
242
- * Time Complexity: O(1)
243
- * Space Complexity: O(1)
244
- *
245
- * The `setAt` function sets an element at a specific position in an array-like data structure.
246
- * @param {number} pos - The `pos` parameter represents the position at which the element needs to be
247
- * set. It is of type `number`.
248
- * @param {E} element - The `element` parameter is the value that you want to set at the specified
249
- * position in the data structure.
251
+ * Get the element at a given position.
252
+ * @remarks Time O(1), Space O(1)
253
+ * @param pos - Zero-based position from the front.
254
+ * @returns Element or undefined.
255
+ */
256
+ at(pos: number): E | undefined;
257
+ /**
258
+ * Replace the element at a given position.
259
+ * @remarks Time O(1), Space O(1)
260
+ * @param pos - Zero-based position from the front.
261
+ * @param element - New element value.
262
+ * @returns True if updated.
250
263
  */
251
264
  setAt(pos: number, element: E): boolean;
252
265
  /**
253
- * Time Complexity: O(n)
254
- * Space Complexity: O(n)
255
- *
256
- * The `addAt` function inserts one or more elements at a specified position in an array-like data
257
- * structure.
258
- * @param {number} pos - The `pos` parameter represents the position at which the element(s) should
259
- * be inserted. It is of type `number`.
260
- * @param {E} element - The `element` parameter represents the element that you want to insert into
261
- * the array at the specified position.
262
- * @param [num=1] - The `num` parameter represents the number of times the `element` should be
263
- * inserted at the specified position (`pos`). By default, it is set to 1, meaning that the `element`
264
- * will be inserted once. However, you can provide a different value for `num` if you want
265
- * @returns The size of the array after the insertion is being returned.
266
+ * Insert repeated copies of an element at a position.
267
+ * @remarks Time O(N), Space O(1)
268
+ * @param pos - Zero-based position from the front.
269
+ * @param element - Element to insert.
270
+ * @param [num] - Number of times to insert (default 1).
271
+ * @returns True if inserted.
266
272
  */
267
273
  addAt(pos: number, element: E, num?: number): boolean;
268
274
  /**
269
- * Time Complexity: O(1)
270
- * Space Complexity: O(1)
271
- *
272
- * The `cut` function updates the state of the object based on the given position and returns the
273
- * updated size.
274
- * @param {number} pos - The `pos` parameter represents the position at which the string should be
275
- * cut. It is a number that indicates the index of the character where the cut should be made.
276
- * @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
277
- * @returns The method is returning the updated size of the data structure.
275
+ * Cut the deque to keep items up to index; optionally mutate in-place.
276
+ * @remarks Time O(N), Space O(1)
277
+ * @param pos - Last index to keep.
278
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
279
+ * @returns This deque if in-place; otherwise a new deque of the prefix.
278
280
  */
279
281
  cut(pos: number, isCutSelf?: boolean): Deque<E>;
280
282
  /**
281
- * Time Complexity: O(n)
282
- * Space Complexity: O(1)
283
- *
284
- * The `splice` function in TypeScript overrides the default behavior to remove and insert elements
285
- * in a Deque data structure while ensuring the starting position and delete count are within bounds.
286
- * @param {number} start - The `start` parameter in the `splice` method represents the index at which
287
- * to start changing the array. Items will be removed or added starting from this index.
288
- * @param {number} deleteCount - The `deleteCount` parameter in the `splice` method represents the
289
- * number of elements to remove from the array starting at the specified `start` index. If
290
- * `deleteCount` is not provided, it defaults to the number of elements from the `start` index to the
291
- * end of the array (`
292
- * @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
293
- * will be inserted into the deque at the specified `start` index. These elements will be inserted in
294
- * place of the elements that are removed based on the `start` and `deleteCount` parameters.
295
- * @returns The `splice` method is returning the array `deletedElements` which contains the elements
296
- * that were removed from the Deque during the splice operation.
283
+ * Remove and/or insert elements at a position (array-like behavior).
284
+ * @remarks Time O(N + M), Space O(M)
285
+ * @param start - Start index (clamped to [0, length]).
286
+ * @param [deleteCount] - Number of elements to remove (default: length - start).
287
+ * @param [items] - Elements to insert after `start`.
288
+ * @returns A new deque containing the removed elements (typed as `this`).
297
289
  */
298
290
  splice(start: number, deleteCount?: number, ...items: E[]): this;
299
291
  /**
300
- * Time Complexity: O(1)
301
- * Space Complexity: O(1) or O(n)
302
- *
303
- * The `cutRest` function cuts the elements from a specified position in a deque and returns a new
304
- * deque with the cut elements.
305
- * @param {number} pos - The `pos` parameter represents the position from which to cut the Deque. It
306
- * is a number that indicates the index of the element in the Deque where the cut should start.
307
- * @param [isCutSelf=false] - isCutSelf is a boolean parameter that determines whether the original
308
- * Deque should be modified or a new Deque should be created. If isCutSelf is true, the original
309
- * Deque will be modified by cutting off elements starting from the specified position. If isCutSelf
310
- * is false, a new De
311
- * @returns The function `cutRest` returns either the modified original deque (`this`) or a new deque
312
- * (`newDeque`) depending on the value of the `isCutSelf` parameter.
292
+ * Cut the deque to keep items from index onward; optionally mutate in-place.
293
+ * @remarks Time O(N), Space O(1)
294
+ * @param pos - First index to keep.
295
+ * @param [isCutSelf] - When true, mutate this deque; otherwise return a new deque.
296
+ * @returns This deque if in-place; otherwise a new deque of the suffix.
313
297
  */
314
298
  cutRest(pos: number, isCutSelf?: boolean): Deque<E>;
315
299
  /**
316
- * Time Complexity: O(n)
317
- * Space Complexity: O(1) or O(n)
318
- *
319
- * The `deleteAt` function removes an element at a specified position in an array-like data
320
- * structure.
321
- * @param {number} pos - The `pos` parameter in the `deleteAt` function represents the position at
322
- * which an element needs to be deleted from the data structure. It is of type `number` and indicates
323
- * the index of the element to be deleted.
324
- * @returns The size of the data structure after the deletion operation is performed.
300
+ * Delete the element at a given position.
301
+ * @remarks Time O(N), Space O(1)
302
+ * @param pos - Zero-based position from the front.
303
+ * @returns Removed element or undefined.
325
304
  */
326
305
  deleteAt(pos: number): E | undefined;
327
306
  /**
328
- * Time Complexity: O(n)
329
- * Space Complexity: O(1)
330
- *
331
- * The `delete` function removes all occurrences of a specified element from an array-like data
332
- * structure.
333
- * @param {E} element - The `element` parameter represents the element that you want to delete from
334
- * the data structure.
335
- * @returns The size of the data structure after the element has been deleted.
307
+ * Delete the first occurrence of a value.
308
+ * @remarks Time O(N), Space O(1)
309
+ * @param element - Element to remove (using the configured equality).
310
+ * @returns True if an element was removed.
336
311
  */
337
312
  delete(element: E): boolean;
338
313
  /**
339
- * Time Complexity: O(n)
340
- * Space Complexity: O(1)
341
- *
342
- * The reverse() function reverses the order of the buckets and the elements within each bucket in a
343
- * data structure.
344
- * @returns The reverse() method is returning the object itself (this) after performing the reverse
345
- * operation on the buckets and updating the relevant properties.
314
+ * Delete the first element matching a predicate.
315
+ * @remarks Time O(N), Space O(1)
316
+ * @param predicate - Function (value, index, deque) → boolean.
317
+ * @returns True if a match was removed.
318
+ */
319
+ deleteWhere(predicate: (value: E, index: number, deque: this) => boolean): boolean;
320
+ /**
321
+ * Set the equality comparator used by delete operations.
322
+ * @remarks Time O(1), Space O(1)
323
+ * @param equals - Equality predicate (a, b) → boolean.
324
+ * @returns This deque.
325
+ */
326
+ setEquality(equals: (a: E, b: E) => boolean): this;
327
+ /**
328
+ * Reverse the deque by reversing buckets and pointers.
329
+ * @remarks Time O(N), Space O(N)
330
+ * @returns This deque.
346
331
  */
347
332
  reverse(): this;
348
333
  /**
349
- * Time Complexity: O(n)
350
- * Space Complexity: O(1)
351
- *
352
- * The `unique()` function removes duplicate elements from an array-like data structure and returns
353
- * the number of unique elements.
354
- * @returns The size of the modified array is being returned.
334
+ * Deduplicate consecutive equal elements in-place.
335
+ * @remarks Time O(N), Space O(1)
336
+ * @returns This deque.
355
337
  */
356
338
  unique(): this;
357
339
  /**
358
- * Time Complexity: O(n)
359
- * Space Complexity: O(n)
360
- *
361
- * The `shrinkToFit` function reorganizes the elements in an array-like data structure to minimize
362
- * memory usage.
363
- * @returns Nothing is being returned. The function is using the `return` statement to exit early if
364
- * `this._length` is 0, but it does not return any value.
340
+ * Trim unused buckets to fit exactly the active range.
341
+ * @remarks Time O(N), Space O(1)
342
+ * @returns void
365
343
  */
366
344
  shrinkToFit(): void;
367
345
  /**
368
- * Time Complexity: O(n)
369
- * Space Complexity: O(n)
370
- *
371
- * The `clone()` function returns a new instance of the `Deque` class with the same elements and
372
- * bucket size as the original instance.
373
- * @returns The `clone()` method is returning a new instance of the `Deque` class with the same
374
- * elements as the original deque (`this`) and the same bucket size.
346
+ * Deep clone this deque, preserving options.
347
+ * @remarks Time O(N), Space O(N)
348
+ * @returns A new deque with the same content and options.
375
349
  */
376
350
  clone(): this;
377
351
  /**
378
- * Time Complexity: O(n)
379
- * Space Complexity: O(n)
380
- *
381
- * The `filter` function creates a new deque containing elements from the original deque that satisfy
382
- * a given predicate function.
383
- * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
384
- * the current element being iterated over, the index of the current element, and the deque itself.
385
- * It should return a boolean value indicating whether the element should be included in the filtered
386
- * deque or not.
387
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
388
- * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
389
- * passed as the `this` value to the `predicate` function. If `thisArg` is
390
- * @returns The `filter` method is returning a new `Deque` object that contains the elements that
391
- * satisfy the given predicate function.
392
- */
393
- filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): Deque<E, R>;
394
- /**
395
- * Time Complexity: O(n)
396
- * Space Complexity: O(n)
397
- *
398
- * The `map` function takes a callback function and applies it to each element in the deque,
399
- * returning a new deque with the results.
400
- * @param callback - The callback parameter is a function that will be called for each element in the
401
- * deque. It takes three arguments: the current element, the index of the element, and the deque
402
- * itself. It should return a value of type EM.
403
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
404
- * transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
405
- * provided, this function will be called for each raw element in the original deque.
406
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
407
- * specify the value of `this` within the callback function. It is used to set the context or scope
408
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
409
- * value of
410
- * @returns a new Deque object with elements of type EM and raw elements of type RM.
411
- */
412
- map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Deque<EM, RM>;
413
- /**
414
- * Time Complexity: O(n)
415
- * Space Complexity: O(1)
416
- *
417
- * The above function is an implementation of the iterator protocol in TypeScript, allowing the
418
- * object to be iterated over using a for...of loop.
352
+ * Filter elements into a new deque of the same class.
353
+ * @remarks Time O(N), Space O(N)
354
+ * @param predicate - Predicate (value, index, deque) → boolean to keep element.
355
+ * @param [thisArg] - Value for `this` inside the predicate.
356
+ * @returns A new deque with kept elements.
357
+ */
358
+ filter(predicate: ElementCallback<E, R, boolean>, thisArg?: any): this;
359
+ /**
360
+ * Map elements into a new deque of the same element type.
361
+ * @remarks Time O(N), Space O(N)
362
+ * @param callback - Mapping function (value, index, deque) newValue.
363
+ * @param [thisArg] - Value for `this` inside the callback.
364
+ * @returns A new deque with mapped values.
365
+ */
366
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: any): this;
367
+ /**
368
+ * Map elements into a new deque (possibly different element type).
369
+ * @remarks Time O(N), Space O(N)
370
+ * @template EM
371
+ * @template RM
372
+ * @param callback - Mapping function (value, index, deque) newElement.
373
+ * @param [options] - Options for the output deque (e.g., bucketSize, toElementFn, maxLen).
374
+ * @param [thisArg] - Value for `this` inside the callback.
375
+ * @returns A new Deque with mapped elements.
376
+ */
377
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: IterableElementBaseOptions<EM, RM>, thisArg?: any): Deque<EM, RM>;
378
+ /**
379
+ * (Protected) Set the internal bucket size.
380
+ * @remarks Time O(1), Space O(1)
381
+ * @param size - Bucket capacity to assign.
382
+ * @returns void
383
+ */
384
+ protected _setBucketSize(size: number): void;
385
+ /**
386
+ * (Protected) Iterate elements from front to back.
387
+ * @remarks Time O(N), Space O(1)
388
+ * @returns Iterator of elements.
419
389
  */
420
390
  protected _getIterator(): IterableIterator<E>;
421
391
  /**
422
- * Time Complexity: O(n)
423
- * Space Complexity: O(n)
424
- *
425
- * The `_reallocate` function reallocates the buckets in an array, adding new buckets if needed.
426
- * @param {number} [needBucketNum] - The `needBucketNum` parameter is an optional number that
427
- * specifies the number of new buckets needed. If not provided, it will default to half of the
428
- * current bucket count (`this._bucketCount >> 1`) or 1 if the current bucket count is less than 2.
392
+ * (Protected) Reallocate buckets to make room near the ends.
393
+ * @remarks Time O(N), Space O(N)
394
+ * @param [needBucketNum] - How many extra buckets to add; defaults to half of current.
395
+ * @returns void
429
396
  */
430
397
  protected _reallocate(needBucketNum?: number): void;
431
398
  /**
432
- * Time Complexity: O(1)
433
- * Space Complexity: O(1)
434
- *
435
- * The function calculates the bucket index and index within the bucket based on the given position.
436
- * @param {number} pos - The `pos` parameter represents the position within the data structure. It is
437
- * a number that indicates the index or position of an element within the structure.
438
- * @returns an object with two properties: "bucketIndex" and "indexInBucket".
399
+ * (Protected) Translate a logical position to bucket/offset.
400
+ * @remarks Time O(1), Space O(1)
401
+ * @param pos - Zero-based position.
402
+ * @returns An object containing bucketIndex and indexInBucket.
439
403
  */
440
404
  protected _getBucketAndPosition(pos: number): {
441
405
  bucketIndex: number;
442
406
  indexInBucket: number;
443
407
  };
444
408
  /**
445
- * The function `_createInstance` returns a new instance of the `Deque` class with the specified
446
- * options.
447
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
448
- * `DequeOptions<E, R>`, which is an optional parameter that allows you to pass additional
449
- * configuration options when creating a new instance of the `Deque` class.
450
- * @returns An instance of the `Deque` class with an empty array and the provided options, casted as
451
- * `this`.
409
+ * (Protected) Create an empty instance of the same concrete class.
410
+ * @remarks Time O(1), Space O(1)
411
+ * @param [options] - Options forwarded to the constructor.
412
+ * @returns An empty like-kind deque instance.
413
+ */
414
+ protected _createInstance(options?: LinearBaseOptions<E, R>): this;
415
+ /**
416
+ * (Protected) Create a like-kind deque seeded by elements.
417
+ * @remarks Time O(N), Space O(N)
418
+ * @template T
419
+ * @template RR
420
+ * @param [elements] - Iterable used to seed the new deque.
421
+ * @param [options] - Options forwarded to the constructor.
422
+ * @returns A like-kind Deque instance.
452
423
  */
453
- protected _createInstance(options?: DequeOptions<E, R>): this;
424
+ protected _createLike<T = E, RR = R>(elements?: IterableWithSizeOrLength<T> | IterableWithSizeOrLength<RR>, options?: DequeOptions<T, RR>): any;
454
425
  /**
455
- * This function returns an iterator that iterates over elements in reverse order.
426
+ * (Protected) Iterate elements from back to front.
427
+ * @remarks Time O(N), Space O(1)
428
+ * @returns Iterator of elements.
456
429
  */
457
430
  protected _getReverseIterator(): IterableIterator<E>;
458
431
  }