bst-typed 2.0.4 → 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.
- package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/data-structures/base/iterable-element-base.js +149 -107
- package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/data-structures/base/linear-base.d.ts +250 -192
- package/dist/data-structures/base/linear-base.js +137 -274
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/data-structures/binary-tree/binary-tree.js +612 -879
- package/dist/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/data-structures/binary-tree/bst.js +505 -481
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/data-structures/graph/abstract-graph.js +267 -237
- package/dist/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/data-structures/graph/directed-graph.js +146 -233
- package/dist/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/data-structures/graph/map-graph.js +56 -59
- package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/data-structures/graph/undirected-graph.js +129 -149
- package/dist/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/data-structures/hash/hash-map.js +270 -457
- package/dist/data-structures/heap/heap.d.ts +214 -289
- package/dist/data-structures/heap/heap.js +340 -349
- package/dist/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/data-structures/heap/max-heap.js +11 -66
- package/dist/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/data-structures/heap/min-heap.js +11 -66
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/data-structures/queue/deque.d.ts +227 -254
- package/dist/data-structures/queue/deque.js +309 -348
- package/dist/data-structures/queue/queue.d.ts +180 -201
- package/dist/data-structures/queue/queue.js +265 -248
- package/dist/data-structures/stack/stack.d.ts +124 -102
- package/dist/data-structures/stack/stack.js +181 -125
- package/dist/data-structures/trie/trie.d.ts +164 -165
- package/dist/data-structures/trie/trie.js +189 -172
- package/dist/interfaces/binary-tree.d.ts +56 -6
- package/dist/interfaces/graph.d.ts +16 -0
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/types/utils/utils.d.ts +6 -6
- package/dist/utils/utils.d.ts +110 -49
- package/dist/utils/utils.js +148 -73
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +681 -905
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- package/src/utils/utils.ts +152 -86
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Pablo Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.Stack = void 0;
|
|
4
11
|
const base_1 = require("../base");
|
|
5
12
|
/**
|
|
13
|
+
* LIFO stack with array storage and optional record→element conversion.
|
|
14
|
+
* @remarks Time O(1), Space O(1)
|
|
15
|
+
* @template E
|
|
16
|
+
* @template R
|
|
6
17
|
* 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
|
|
7
18
|
* 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
|
|
8
19
|
* 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
|
|
@@ -132,214 +143,259 @@ const base_1 = require("../base");
|
|
|
132
143
|
* console.log(spans); // [1, 1, 1, 2, 1, 4, 6]
|
|
133
144
|
*/
|
|
134
145
|
class Stack extends base_1.IterableElementBase {
|
|
146
|
+
/**
|
|
147
|
+
* Create a Stack and optionally bulk-push elements.
|
|
148
|
+
* @remarks Time O(N), Space O(N)
|
|
149
|
+
* @param [elements] - Iterable of elements (or raw records if toElementFn is set).
|
|
150
|
+
* @param [options] - Options such as toElementFn and equality function.
|
|
151
|
+
* @returns New Stack instance.
|
|
152
|
+
*/
|
|
135
153
|
constructor(elements = [], options) {
|
|
136
154
|
super(options);
|
|
155
|
+
this._equals = Object.is;
|
|
137
156
|
this._elements = [];
|
|
138
157
|
this.pushMany(elements);
|
|
139
158
|
}
|
|
140
159
|
/**
|
|
141
|
-
*
|
|
142
|
-
* @
|
|
160
|
+
* Get the backing array of elements.
|
|
161
|
+
* @remarks Time O(1), Space O(1)
|
|
162
|
+
* @returns Internal elements array.
|
|
143
163
|
*/
|
|
144
164
|
get elements() {
|
|
145
165
|
return this._elements;
|
|
146
166
|
}
|
|
147
167
|
/**
|
|
148
|
-
*
|
|
149
|
-
* @
|
|
168
|
+
* Get the number of stored elements.
|
|
169
|
+
* @remarks Time O(1), Space O(1)
|
|
170
|
+
* @returns Current size.
|
|
150
171
|
*/
|
|
151
172
|
get size() {
|
|
152
173
|
return this.elements.length;
|
|
153
174
|
}
|
|
154
175
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* @param
|
|
160
|
-
* @
|
|
161
|
-
*
|
|
176
|
+
* Create a stack from an array of elements.
|
|
177
|
+
* @remarks Time O(N), Space O(N)
|
|
178
|
+
* @template E
|
|
179
|
+
* @template R
|
|
180
|
+
* @param this - The constructor (subclass) to instantiate.
|
|
181
|
+
* @param elements - Array of elements to push in order.
|
|
182
|
+
* @param [options] - Options forwarded to the constructor.
|
|
183
|
+
* @returns A new Stack populated from the array.
|
|
162
184
|
*/
|
|
163
|
-
static fromArray(elements) {
|
|
164
|
-
return new
|
|
185
|
+
static fromArray(elements, options) {
|
|
186
|
+
return new this(elements, options);
|
|
165
187
|
}
|
|
166
188
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* The function checks if an array is empty and returns a boolean value.
|
|
171
|
-
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
189
|
+
* Check whether the stack is empty.
|
|
190
|
+
* @remarks Time O(1), Space O(1)
|
|
191
|
+
* @returns True if size is 0.
|
|
172
192
|
*/
|
|
173
193
|
isEmpty() {
|
|
174
194
|
return this.elements.length === 0;
|
|
175
195
|
}
|
|
176
196
|
/**
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* The `peek` function returns the last element of an array, or undefined if the array is empty.
|
|
181
|
-
* @returns The `peek()` function returns the last element of the `_elements` array, or `undefined` if the array is empty.
|
|
197
|
+
* Get the top element without removing it.
|
|
198
|
+
* @remarks Time O(1), Space O(1)
|
|
199
|
+
* @returns Top element or undefined.
|
|
182
200
|
*/
|
|
183
201
|
peek() {
|
|
184
|
-
|
|
185
|
-
return undefined;
|
|
186
|
-
return this.elements[this.elements.length - 1];
|
|
202
|
+
return this.isEmpty() ? undefined : this.elements[this.elements.length - 1];
|
|
187
203
|
}
|
|
188
204
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* @param {E} element - The parameter "element" is of type E, which means it can be any data type.
|
|
194
|
-
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
205
|
+
* Push one element onto the top.
|
|
206
|
+
* @remarks Time O(1), Space O(1)
|
|
207
|
+
* @param element - Element to push.
|
|
208
|
+
* @returns True when pushed.
|
|
195
209
|
*/
|
|
196
210
|
push(element) {
|
|
197
211
|
this.elements.push(element);
|
|
198
212
|
return true;
|
|
199
213
|
}
|
|
200
214
|
/**
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
* The `pop` function removes and returns the last element from an array, or returns undefined if the array is empty.
|
|
205
|
-
* @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
|
|
206
|
-
* array is empty, it returns `undefined`.
|
|
215
|
+
* Pop and return the top element.
|
|
216
|
+
* @remarks Time O(1), Space O(1)
|
|
217
|
+
* @returns Removed element or undefined.
|
|
207
218
|
*/
|
|
208
219
|
pop() {
|
|
209
|
-
|
|
210
|
-
return;
|
|
211
|
-
return this.elements.pop();
|
|
220
|
+
return this.isEmpty() ? undefined : this.elements.pop();
|
|
212
221
|
}
|
|
213
222
|
/**
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* transformation function if provided.
|
|
219
|
-
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the `pushMany` function
|
|
220
|
-
* is an iterable containing elements of type `E` or `R`. The function iterates over each element in
|
|
221
|
-
* the iterable and pushes it into the data structure. If a transformation function `toElementFn` is
|
|
222
|
-
* provided, it is used to
|
|
223
|
-
* @returns The `pushMany` function is returning an array of boolean values indicating whether each
|
|
224
|
-
* element was successfully pushed into the data structure.
|
|
223
|
+
* Push many elements from an iterable.
|
|
224
|
+
* @remarks Time O(N), Space O(1)
|
|
225
|
+
* @param elements - Iterable of elements (or raw records if toElementFn is set).
|
|
226
|
+
* @returns Array of per-element success flags.
|
|
225
227
|
*/
|
|
226
228
|
pushMany(elements) {
|
|
227
229
|
const ans = [];
|
|
228
230
|
for (const el of elements) {
|
|
229
|
-
if (this.toElementFn)
|
|
231
|
+
if (this.toElementFn)
|
|
230
232
|
ans.push(this.push(this.toElementFn(el)));
|
|
231
|
-
|
|
232
|
-
else {
|
|
233
|
+
else
|
|
233
234
|
ans.push(this.push(el));
|
|
234
|
-
}
|
|
235
235
|
}
|
|
236
236
|
return ans;
|
|
237
237
|
}
|
|
238
238
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* @returns An array of type E.
|
|
239
|
+
* Delete the first occurrence of a specific element.
|
|
240
|
+
* @remarks Time O(N), Space O(1)
|
|
241
|
+
* @param element - Element to remove (using the configured equality).
|
|
242
|
+
* @returns True if an element was removed.
|
|
244
243
|
*/
|
|
245
244
|
delete(element) {
|
|
246
|
-
const
|
|
247
|
-
return this.deleteAt(
|
|
245
|
+
const idx = this._indexOfByEquals(element);
|
|
246
|
+
return this.deleteAt(idx);
|
|
248
247
|
}
|
|
249
248
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @returns An array of type E.
|
|
249
|
+
* Delete the element at an index.
|
|
250
|
+
* @remarks Time O(N), Space O(1)
|
|
251
|
+
* @param index - Zero-based index from the bottom.
|
|
252
|
+
* @returns True if removed.
|
|
255
253
|
*/
|
|
256
254
|
deleteAt(index) {
|
|
255
|
+
if (index < 0 || index >= this.elements.length)
|
|
256
|
+
return false;
|
|
257
257
|
const spliced = this.elements.splice(index, 1);
|
|
258
258
|
return spliced.length === 1;
|
|
259
259
|
}
|
|
260
260
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
261
|
+
* Delete the first element that satisfies a predicate.
|
|
262
|
+
* @remarks Time O(N), Space O(1)
|
|
263
|
+
* @param predicate - Function (value, index, stack) → boolean to decide deletion.
|
|
264
|
+
* @returns True if a match was removed.
|
|
265
|
+
*/
|
|
266
|
+
deleteWhere(predicate) {
|
|
267
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
268
|
+
if (predicate(this.elements[i], i, this)) {
|
|
269
|
+
this.elements.splice(i, 1);
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Remove all elements and reset storage.
|
|
277
|
+
* @remarks Time O(1), Space O(1)
|
|
278
|
+
* @returns void
|
|
265
279
|
*/
|
|
266
280
|
clear() {
|
|
267
281
|
this._elements = [];
|
|
268
282
|
}
|
|
269
283
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
* The `clone()` function returns a new `Stack` object with the same elements as the original stack.
|
|
274
|
-
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
284
|
+
* Deep clone this stack.
|
|
285
|
+
* @remarks Time O(N), Space O(N)
|
|
286
|
+
* @returns A new stack with the same content.
|
|
275
287
|
*/
|
|
276
288
|
clone() {
|
|
277
|
-
|
|
289
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
290
|
+
for (const v of this)
|
|
291
|
+
out.push(v);
|
|
292
|
+
return out;
|
|
278
293
|
}
|
|
279
294
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
286
|
-
* the current element being iterated over, the index of the current element, and the stack itself.
|
|
287
|
-
* It should return a boolean value indicating whether the element should be included in the filtered
|
|
288
|
-
* stack or not.
|
|
289
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
290
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
291
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
292
|
-
* @returns The `filter` method is returning a new `Stack` object that contains the elements that
|
|
293
|
-
* satisfy the given predicate function.
|
|
295
|
+
* Filter elements into a new stack of the same class.
|
|
296
|
+
* @remarks Time O(N), Space O(N)
|
|
297
|
+
* @param predicate - Predicate (value, index, stack) → boolean to keep value.
|
|
298
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
299
|
+
* @returns A new stack with kept values.
|
|
294
300
|
*/
|
|
295
301
|
filter(predicate, thisArg) {
|
|
296
|
-
const
|
|
302
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
297
303
|
let index = 0;
|
|
298
|
-
for (const
|
|
299
|
-
if (predicate.call(thisArg,
|
|
300
|
-
|
|
301
|
-
}
|
|
304
|
+
for (const v of this) {
|
|
305
|
+
if (predicate.call(thisArg, v, index, this))
|
|
306
|
+
out.push(v);
|
|
302
307
|
index++;
|
|
303
308
|
}
|
|
304
|
-
return
|
|
309
|
+
return out;
|
|
305
310
|
}
|
|
306
311
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
313
|
-
* stack. It takes three arguments: the current element, the index of the element, and the stack
|
|
314
|
-
* itself. It should return a new value that will be added to the new stack.
|
|
315
|
-
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
316
|
-
* transform the raw element (`RM`) into a new element (`EM`) before pushing it into the new stack.
|
|
317
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
318
|
-
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
319
|
-
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
320
|
-
* value of
|
|
321
|
-
* @returns a new Stack object with elements of type EM and raw elements of type RM.
|
|
312
|
+
* Map values into a new stack of the same element type.
|
|
313
|
+
* @remarks Time O(N), Space O(N)
|
|
314
|
+
* @param callback - Mapping function (value, index, stack) → newValue.
|
|
315
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
316
|
+
* @returns A new stack with mapped values.
|
|
322
317
|
*/
|
|
323
|
-
|
|
324
|
-
const
|
|
318
|
+
mapSame(callback, thisArg) {
|
|
319
|
+
const out = this._createInstance({ toElementFn: this.toElementFn });
|
|
325
320
|
let index = 0;
|
|
326
|
-
for (const
|
|
327
|
-
|
|
321
|
+
for (const v of this) {
|
|
322
|
+
const mv = thisArg === undefined ? callback(v, index++, this) : callback.call(thisArg, v, index++, this);
|
|
323
|
+
out.push(mv);
|
|
324
|
+
}
|
|
325
|
+
return out;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Map values into a new stack (possibly different element type).
|
|
329
|
+
* @remarks Time O(N), Space O(N)
|
|
330
|
+
* @template EM
|
|
331
|
+
* @template RM
|
|
332
|
+
* @param callback - Mapping function (value, index, stack) → newElement.
|
|
333
|
+
* @param [options] - Options for the output stack (e.g., toElementFn).
|
|
334
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
335
|
+
* @returns A new Stack with mapped elements.
|
|
336
|
+
*/
|
|
337
|
+
map(callback, options, thisArg) {
|
|
338
|
+
const out = this._createLike([], Object.assign({}, (options !== null && options !== void 0 ? options : {})));
|
|
339
|
+
let index = 0;
|
|
340
|
+
for (const v of this) {
|
|
341
|
+
out.push(thisArg === undefined ? callback(v, index, this) : callback.call(thisArg, v, index, this));
|
|
328
342
|
index++;
|
|
329
343
|
}
|
|
330
|
-
return
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Set the equality comparator used by delete/search operations.
|
|
348
|
+
* @remarks Time O(1), Space O(1)
|
|
349
|
+
* @param equals - Equality predicate (a, b) → boolean.
|
|
350
|
+
* @returns This stack.
|
|
351
|
+
*/
|
|
352
|
+
setEquality(equals) {
|
|
353
|
+
this._equals = equals;
|
|
354
|
+
return this;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* (Protected) Find the index of a target element using the equality function.
|
|
358
|
+
* @remarks Time O(N), Space O(1)
|
|
359
|
+
* @param target - Element to search for.
|
|
360
|
+
* @returns Index or -1 if not found.
|
|
361
|
+
*/
|
|
362
|
+
_indexOfByEquals(target) {
|
|
363
|
+
for (let i = 0; i < this.elements.length; i++)
|
|
364
|
+
if (this._equals(this.elements[i], target))
|
|
365
|
+
return i;
|
|
366
|
+
return -1;
|
|
331
367
|
}
|
|
332
368
|
/**
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
|
|
369
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
370
|
+
* @remarks Time O(1), Space O(1)
|
|
371
|
+
* @param [options] - Options forwarded to the constructor.
|
|
372
|
+
* @returns An empty like-kind stack instance.
|
|
373
|
+
*/
|
|
374
|
+
_createInstance(options) {
|
|
375
|
+
const Ctor = this.constructor;
|
|
376
|
+
return new Ctor([], options);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* (Protected) Create a like-kind stack and seed it from an iterable.
|
|
380
|
+
* @remarks Time O(N), Space O(N)
|
|
381
|
+
* @template T
|
|
382
|
+
* @template RR
|
|
383
|
+
* @param [elements] - Iterable used to seed the new stack.
|
|
384
|
+
* @param [options] - Options forwarded to the constructor.
|
|
385
|
+
* @returns A like-kind Stack instance.
|
|
386
|
+
*/
|
|
387
|
+
_createLike(elements = [], options) {
|
|
388
|
+
const Ctor = this.constructor;
|
|
389
|
+
return new Ctor(elements, options);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* (Protected) Iterate elements from bottom to top.
|
|
393
|
+
* @remarks Time O(N), Space O(1)
|
|
394
|
+
* @returns Iterator of elements.
|
|
338
395
|
*/
|
|
339
396
|
*_getIterator() {
|
|
340
|
-
for (let i = 0; i < this.elements.length; i++)
|
|
397
|
+
for (let i = 0; i < this.elements.length; i++)
|
|
341
398
|
yield this.elements[i];
|
|
342
|
-
}
|
|
343
399
|
}
|
|
344
400
|
}
|
|
345
401
|
exports.Stack = Stack;
|