data-structure-typed 2.0.1 → 2.0.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.
- package/CHANGELOG.md +1 -1
- package/package.json +1 -1
- package/src/data-structures/queue/queue.ts +1 -1
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/utils/utils.test.ts +3 -2
- package/dist/individuals/binary-tree/avl-tree-counter.mjs +0 -4701
- package/dist/individuals/binary-tree/avl-tree-multi-map.mjs +0 -4514
- package/dist/individuals/binary-tree/avl-tree.mjs +0 -4321
- package/dist/individuals/binary-tree/binary-tree.mjs +0 -3097
- package/dist/individuals/binary-tree/bst.mjs +0 -3858
- package/dist/individuals/binary-tree/red-black-tree.mjs +0 -4391
- package/dist/individuals/binary-tree/tree-counter.mjs +0 -4806
- package/dist/individuals/binary-tree/tree-multi-map.mjs +0 -4582
- package/dist/individuals/graph/directed-graph.mjs +0 -2910
- package/dist/individuals/graph/undirected-graph.mjs +0 -2745
- package/dist/individuals/hash/hash-map.mjs +0 -1040
- package/dist/individuals/heap/heap.mjs +0 -909
- package/dist/individuals/heap/max-heap.mjs +0 -671
- package/dist/individuals/heap/min-heap.mjs +0 -659
- package/dist/individuals/linked-list/doubly-linked-list.mjs +0 -1495
- package/dist/individuals/linked-list/singly-linked-list.mjs +0 -1479
- package/dist/individuals/priority-queue/max-priority-queue.mjs +0 -768
- package/dist/individuals/priority-queue/min-priority-queue.mjs +0 -757
- package/dist/individuals/priority-queue/priority-queue.mjs +0 -670
- package/dist/individuals/queue/deque.mjs +0 -1262
- package/dist/individuals/queue/queue.mjs +0 -1865
- package/dist/individuals/stack/stack.mjs +0 -415
- package/dist/individuals/trie/trie.mjs +0 -687
|
@@ -1,1495 +0,0 @@
|
|
|
1
|
-
// src/data-structures/base/iterable-element-base.ts
|
|
2
|
-
var IterableElementBase = class {
|
|
3
|
-
/**
|
|
4
|
-
* The protected constructor initializes the options for the IterableElementBase class, including the
|
|
5
|
-
* toElementFn function.
|
|
6
|
-
* @param [options] - An optional object that contains the following properties:
|
|
7
|
-
*/
|
|
8
|
-
constructor(options) {
|
|
9
|
-
if (options) {
|
|
10
|
-
const { toElementFn } = options;
|
|
11
|
-
if (typeof toElementFn === "function") this._toElementFn = toElementFn;
|
|
12
|
-
else if (toElementFn) throw new TypeError("toElementFn must be a function type");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
_toElementFn;
|
|
16
|
-
get toElementFn() {
|
|
17
|
-
return this._toElementFn;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Time Complexity: O(n)
|
|
21
|
-
* Space Complexity: O(1)
|
|
22
|
-
*
|
|
23
|
-
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
24
|
-
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
25
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
26
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
27
|
-
*/
|
|
28
|
-
*[Symbol.iterator](...args) {
|
|
29
|
-
yield* this._getIterator(...args);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Time Complexity: O(n)
|
|
33
|
-
* Space Complexity: O(n)
|
|
34
|
-
*
|
|
35
|
-
* The function returns an iterator that yields all the values in the object.
|
|
36
|
-
*/
|
|
37
|
-
*values() {
|
|
38
|
-
for (const item of this) {
|
|
39
|
-
yield item;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Time Complexity: O(n)
|
|
44
|
-
* Space Complexity: O(1)
|
|
45
|
-
*
|
|
46
|
-
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
47
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
48
|
-
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
49
|
-
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
50
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
51
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
52
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
53
|
-
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
54
|
-
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
55
|
-
*/
|
|
56
|
-
every(predicate, thisArg) {
|
|
57
|
-
let index = 0;
|
|
58
|
-
for (const item of this) {
|
|
59
|
-
if (!predicate.call(thisArg, item, index++, this)) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Time Complexity: O(n)
|
|
67
|
-
* Space Complexity: O(1)
|
|
68
|
-
*
|
|
69
|
-
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
70
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
71
|
-
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
72
|
-
* element satisfies the condition.
|
|
73
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
74
|
-
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
75
|
-
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
76
|
-
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
77
|
-
* in the collection, and false otherwise.
|
|
78
|
-
*/
|
|
79
|
-
some(predicate, thisArg) {
|
|
80
|
-
let index = 0;
|
|
81
|
-
for (const item of this) {
|
|
82
|
-
if (predicate.call(thisArg, item, index++, this)) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Time Complexity: O(n)
|
|
90
|
-
* Space Complexity: O(1)
|
|
91
|
-
*
|
|
92
|
-
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
93
|
-
* function for each element.
|
|
94
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
95
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
96
|
-
* element, and the array that forEach was called upon.
|
|
97
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
98
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
99
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
100
|
-
*/
|
|
101
|
-
forEach(callbackfn, thisArg) {
|
|
102
|
-
let index = 0;
|
|
103
|
-
for (const item of this) {
|
|
104
|
-
callbackfn.call(thisArg, item, index++, this);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Time Complexity: O(n)
|
|
109
|
-
* Space Complexity: O(1)
|
|
110
|
-
*
|
|
111
|
-
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
112
|
-
* element that satisfies the provided callback function.
|
|
113
|
-
* @param predicate - The predicate parameter is a function that will be called for each element in
|
|
114
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
115
|
-
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
116
|
-
* current element matches the desired condition.
|
|
117
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
118
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
119
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
120
|
-
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
121
|
-
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
122
|
-
*/
|
|
123
|
-
find(predicate, thisArg) {
|
|
124
|
-
let index = 0;
|
|
125
|
-
for (const item of this) {
|
|
126
|
-
if (predicate.call(thisArg, item, index++, this)) return item;
|
|
127
|
-
}
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Time Complexity: O(n)
|
|
132
|
-
* Space Complexity: O(1)
|
|
133
|
-
*
|
|
134
|
-
* The function checks if a given element exists in a collection.
|
|
135
|
-
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
136
|
-
* represents the element that we want to check for existence in the collection.
|
|
137
|
-
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
138
|
-
* otherwise.
|
|
139
|
-
*/
|
|
140
|
-
has(element) {
|
|
141
|
-
for (const ele of this) {
|
|
142
|
-
if (ele === element) return true;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Time Complexity: O(n)
|
|
148
|
-
* Space Complexity: O(1)
|
|
149
|
-
*
|
|
150
|
-
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
151
|
-
* function to reduce them into a single value.
|
|
152
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
153
|
-
* the array. It takes four arguments:
|
|
154
|
-
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
155
|
-
* is the value that the accumulator starts with before the reduction operation begins.
|
|
156
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
157
|
-
* all the elements in the array and applying the callback function to each element.
|
|
158
|
-
*/
|
|
159
|
-
reduce(callbackfn, initialValue) {
|
|
160
|
-
let accumulator = initialValue ?? 0;
|
|
161
|
-
let index = 0;
|
|
162
|
-
for (const item of this) {
|
|
163
|
-
accumulator = callbackfn(accumulator, item, index++, this);
|
|
164
|
-
}
|
|
165
|
-
return accumulator;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Time Complexity: O(n)
|
|
169
|
-
* Space Complexity: O(n)
|
|
170
|
-
*
|
|
171
|
-
* The `toArray` function converts a linked list into an array.
|
|
172
|
-
* @returns The `toArray()` method is returning an array of type `E[]`.
|
|
173
|
-
*/
|
|
174
|
-
toArray() {
|
|
175
|
-
return [...this];
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Time Complexity: O(n)
|
|
179
|
-
* Space Complexity: O(n)
|
|
180
|
-
*
|
|
181
|
-
* The print function logs the elements of an array to the console.
|
|
182
|
-
*/
|
|
183
|
-
toVisual() {
|
|
184
|
-
return [...this];
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Time Complexity: O(n)
|
|
188
|
-
* Space Complexity: O(n)
|
|
189
|
-
*
|
|
190
|
-
* The print function logs the elements of an array to the console.
|
|
191
|
-
*/
|
|
192
|
-
print() {
|
|
193
|
-
console.log(this.toVisual());
|
|
194
|
-
}
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
// src/data-structures/base/linear-base.ts
|
|
198
|
-
var LinkedListNode = class {
|
|
199
|
-
constructor(value) {
|
|
200
|
-
this._value = value;
|
|
201
|
-
this._next = void 0;
|
|
202
|
-
}
|
|
203
|
-
_value;
|
|
204
|
-
get value() {
|
|
205
|
-
return this._value;
|
|
206
|
-
}
|
|
207
|
-
set value(value) {
|
|
208
|
-
this._value = value;
|
|
209
|
-
}
|
|
210
|
-
_next;
|
|
211
|
-
get next() {
|
|
212
|
-
return this._next;
|
|
213
|
-
}
|
|
214
|
-
set next(value) {
|
|
215
|
-
this._next = value;
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
var LinearBase = class _LinearBase extends IterableElementBase {
|
|
219
|
-
/**
|
|
220
|
-
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
221
|
-
* if provided.
|
|
222
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
223
|
-
* constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
|
|
224
|
-
* object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
|
|
225
|
-
*/
|
|
226
|
-
constructor(options) {
|
|
227
|
-
super(options);
|
|
228
|
-
if (options) {
|
|
229
|
-
const { maxLen } = options;
|
|
230
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
_maxLen = -1;
|
|
234
|
-
get maxLen() {
|
|
235
|
-
return this._maxLen;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Time Complexity: O(n)
|
|
239
|
-
* Space Complexity: O(1)
|
|
240
|
-
*
|
|
241
|
-
* The function indexOf searches for a specified element starting from a given index in an array-like
|
|
242
|
-
* object and returns the index of the first occurrence, or -1 if not found.
|
|
243
|
-
* @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
|
|
244
|
-
* element that you want to find within the array. The function will search for this element starting
|
|
245
|
-
* from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
|
|
246
|
-
* within the
|
|
247
|
-
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
|
|
248
|
-
* index at which to start searching for the `searchElement` within the array. If provided, the
|
|
249
|
-
* search will begin at this index and continue to the end of the array. If `fromIndex` is not
|
|
250
|
-
* specified, the default
|
|
251
|
-
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
252
|
-
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
253
|
-
*/
|
|
254
|
-
indexOf(searchElement, fromIndex = 0) {
|
|
255
|
-
if (this.length === 0) return -1;
|
|
256
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
257
|
-
if (fromIndex < 0) fromIndex = 0;
|
|
258
|
-
for (let i = fromIndex; i < this.length; i++) {
|
|
259
|
-
const element = this.at(i);
|
|
260
|
-
if (element === searchElement) return i;
|
|
261
|
-
}
|
|
262
|
-
return -1;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Time Complexity: O(n)
|
|
266
|
-
* Space Complexity: O(1)
|
|
267
|
-
*
|
|
268
|
-
* The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
|
|
269
|
-
* element in an array.
|
|
270
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
|
|
271
|
-
* last index of within the array. The `lastIndexOf` method will search the array starting from the
|
|
272
|
-
* `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
|
|
273
|
-
* of the
|
|
274
|
-
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
275
|
-
* index at which to start searching for the `searchElement` in the array. By default, it starts
|
|
276
|
-
* searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
|
|
277
|
-
* provided
|
|
278
|
-
* @returns The last index of the `searchElement` in the array is being returned. If the
|
|
279
|
-
* `searchElement` is not found in the array, -1 is returned.
|
|
280
|
-
*/
|
|
281
|
-
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
282
|
-
if (this.length === 0) return -1;
|
|
283
|
-
if (fromIndex >= this.length) fromIndex = this.length - 1;
|
|
284
|
-
if (fromIndex < 0) fromIndex = this.length + fromIndex;
|
|
285
|
-
for (let i = fromIndex; i >= 0; i--) {
|
|
286
|
-
const element = this.at(i);
|
|
287
|
-
if (element === searchElement) return i;
|
|
288
|
-
}
|
|
289
|
-
return -1;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Time Complexity: O(n)
|
|
293
|
-
* Space Complexity: O(1)
|
|
294
|
-
*
|
|
295
|
-
* The `findIndex` function iterates over an array and returns the index of the first element that
|
|
296
|
-
* satisfies the provided predicate function.
|
|
297
|
-
* @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
|
|
298
|
-
* that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
|
|
299
|
-
* value indicating whether the current element satisfies the condition being checked for.
|
|
300
|
-
* @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
|
|
301
|
-
* parameter that specifies the value to use as `this` when executing the `predicate` function. If
|
|
302
|
-
* provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
|
|
303
|
-
* @returns The `findIndex` method is returning the index of the first element in the array that
|
|
304
|
-
* satisfies the provided predicate function. If no such element is found, it returns -1.
|
|
305
|
-
*/
|
|
306
|
-
findIndex(predicate, thisArg) {
|
|
307
|
-
for (let i = 0; i < this.length; i++) {
|
|
308
|
-
const item = this.at(i);
|
|
309
|
-
if (item !== void 0 && predicate.call(thisArg, item, i, this)) return i;
|
|
310
|
-
}
|
|
311
|
-
return -1;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Time Complexity: O(n + m)
|
|
315
|
-
* Space Complexity: O(n + m)
|
|
316
|
-
*
|
|
317
|
-
* The `concat` function in TypeScript concatenates multiple items into a new list, handling both
|
|
318
|
-
* individual elements and instances of `LinearBase`.
|
|
319
|
-
* @param {(E | this)[]} items - The `concat` method takes in an array of items, where
|
|
320
|
-
* each item can be either of type `E` or an instance of `LinearBase<E, R>`.
|
|
321
|
-
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
322
|
-
* items passed as arguments concatenated to it.
|
|
323
|
-
*/
|
|
324
|
-
concat(...items) {
|
|
325
|
-
const newList = this.clone();
|
|
326
|
-
for (const item of items) {
|
|
327
|
-
if (item instanceof _LinearBase) {
|
|
328
|
-
newList.pushMany(item);
|
|
329
|
-
} else {
|
|
330
|
-
newList.push(item);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
return newList;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Time Complexity: O(n log n)
|
|
337
|
-
* Space Complexity: O(n)
|
|
338
|
-
*
|
|
339
|
-
* The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
|
|
340
|
-
* function.
|
|
341
|
-
* @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
|
|
342
|
-
* two elements `a` and `b` as input and returns a number indicating their relative order. If the
|
|
343
|
-
* returned value is negative, `a` comes before `b`. If the returned value is positive, `
|
|
344
|
-
* @returns The `sort` method is returning the instance of the object on which it is called (this),
|
|
345
|
-
* after sorting the elements based on the provided comparison function (compareFn).
|
|
346
|
-
*/
|
|
347
|
-
sort(compareFn) {
|
|
348
|
-
const arr = this.toArray();
|
|
349
|
-
arr.sort(compareFn);
|
|
350
|
-
this.clear();
|
|
351
|
-
for (const item of arr) this.push(item);
|
|
352
|
-
return this;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Time Complexity: O(n + m)
|
|
356
|
-
* Space Complexity: O(m)
|
|
357
|
-
*
|
|
358
|
-
* The `splice` function in TypeScript removes elements from an array and optionally inserts new
|
|
359
|
-
* elements at the specified index.
|
|
360
|
-
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
361
|
-
* to start modifying the array. If `start` is a negative number, it will count from the end of the
|
|
362
|
-
* array.
|
|
363
|
-
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
364
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
365
|
-
* `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
|
|
366
|
-
* at the `start`
|
|
367
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
368
|
-
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
369
|
-
* and you can pass multiple elements separated by commas. The `splice` method will insert these
|
|
370
|
-
* items into the array at the
|
|
371
|
-
* @returns The `splice` method returns a list of elements that were removed from the original list
|
|
372
|
-
* during the operation.
|
|
373
|
-
*/
|
|
374
|
-
splice(start, deleteCount = 0, ...items) {
|
|
375
|
-
const removedList = this._createInstance();
|
|
376
|
-
start = start < 0 ? this.length + start : start;
|
|
377
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
378
|
-
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
379
|
-
for (let i = 0; i < deleteCount; i++) {
|
|
380
|
-
const removed = this.deleteAt(start);
|
|
381
|
-
if (removed !== void 0) {
|
|
382
|
-
removedList.push(removed);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
for (let i = 0; i < items.length; i++) {
|
|
386
|
-
this.addAt(start + i, items[i]);
|
|
387
|
-
}
|
|
388
|
-
return removedList;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Time Complexity: O(n)
|
|
392
|
-
* Space Complexity: O(1)
|
|
393
|
-
*
|
|
394
|
-
* The `join` function in TypeScript returns a string by joining the elements of an array with a
|
|
395
|
-
* specified separator.
|
|
396
|
-
* @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
|
|
397
|
-
* or characters that will be used to separate each element when joining them into a single string.
|
|
398
|
-
* By default, the separator is set to a comma (`,`), but you can provide a different separator if
|
|
399
|
-
* needed.
|
|
400
|
-
* @returns The `join` method is being returned, which takes an optional `separator` parameter
|
|
401
|
-
* (defaulting to a comma) and returns a string created by joining all elements of the array after
|
|
402
|
-
* converting it to an array.
|
|
403
|
-
*/
|
|
404
|
-
join(separator = ",") {
|
|
405
|
-
return this.toArray().join(separator);
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity: O(n)
|
|
409
|
-
* Space Complexity: O(n)
|
|
410
|
-
*
|
|
411
|
-
* The function `toReversedArray` takes an array and returns a new array with its elements in reverse
|
|
412
|
-
* order.
|
|
413
|
-
* @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
|
|
414
|
-
* order.
|
|
415
|
-
*/
|
|
416
|
-
toReversedArray() {
|
|
417
|
-
const array = [];
|
|
418
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
419
|
-
array.push(this.at(i));
|
|
420
|
-
}
|
|
421
|
-
return array;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Time Complexity: O(n)
|
|
425
|
-
* Space Complexity: O(1)
|
|
426
|
-
*
|
|
427
|
-
* The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
|
|
428
|
-
* callback function to each element, accumulating a single result.
|
|
429
|
-
* @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
|
|
430
|
-
* be called on each element in the array from right to left. It takes four arguments:
|
|
431
|
-
* @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
|
|
432
|
-
* optional parameter that specifies the initial value of the accumulator. If provided, the
|
|
433
|
-
* `accumulator` will start with this initial value before iterating over the elements of the array.
|
|
434
|
-
* If `initialValue` is not provided, the accumulator will
|
|
435
|
-
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
436
|
-
* callback function to each element in the array from right to left.
|
|
437
|
-
*/
|
|
438
|
-
reduceRight(callbackfn, initialValue) {
|
|
439
|
-
let accumulator = initialValue ?? 0;
|
|
440
|
-
for (let i = this.length - 1; i >= 0; i--) {
|
|
441
|
-
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
442
|
-
}
|
|
443
|
-
return accumulator;
|
|
444
|
-
}
|
|
445
|
-
/**
|
|
446
|
-
* Time Complexity: O(m)
|
|
447
|
-
* Space Complexity: O(m)
|
|
448
|
-
*
|
|
449
|
-
* The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
|
|
450
|
-
* the original instance based on the specified start and end indices.
|
|
451
|
-
* @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
|
|
452
|
-
* which to begin extracting elements from an array-like object. If no `start` parameter is provided,
|
|
453
|
-
* the default value is 0, meaning the extraction will start from the beginning of the array.
|
|
454
|
-
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
455
|
-
* end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
|
|
456
|
-
* array (i.e., `this.length`).
|
|
457
|
-
* @returns The `slice` method is returning a new instance of the object with elements sliced from
|
|
458
|
-
* the specified start index (default is 0) to the specified end index (default is the length of the
|
|
459
|
-
* object).
|
|
460
|
-
*/
|
|
461
|
-
slice(start = 0, end = this.length) {
|
|
462
|
-
start = start < 0 ? this.length + start : start;
|
|
463
|
-
end = end < 0 ? this.length + end : end;
|
|
464
|
-
const newList = this._createInstance();
|
|
465
|
-
for (let i = start; i < end; i++) {
|
|
466
|
-
newList.push(this.at(i));
|
|
467
|
-
}
|
|
468
|
-
return newList;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Time Complexity: O(n)
|
|
472
|
-
* Space Complexity: O(1)
|
|
473
|
-
*
|
|
474
|
-
* The `fill` function in TypeScript fills a specified range in an array-like object with a given
|
|
475
|
-
* value.
|
|
476
|
-
* @param {E} value - The `value` parameter in the `fill` method represents the element that will be
|
|
477
|
-
* used to fill the specified range in the array.
|
|
478
|
-
* @param [start=0] - The `start` parameter specifies the index at which to start filling the array
|
|
479
|
-
* with the specified value. If not provided, it defaults to 0, indicating the beginning of the
|
|
480
|
-
* array.
|
|
481
|
-
* @param end - The `end` parameter in the `fill` function represents the index at which the filling
|
|
482
|
-
* of values should stop. It specifies the end of the range within the array where the `value` should
|
|
483
|
-
* be filled.
|
|
484
|
-
* @returns The `fill` method is returning the modified object (`this`) after filling the specified
|
|
485
|
-
* range with the provided value.
|
|
486
|
-
*/
|
|
487
|
-
fill(value, start = 0, end = this.length) {
|
|
488
|
-
start = start < 0 ? this.length + start : start;
|
|
489
|
-
end = end < 0 ? this.length + end : end;
|
|
490
|
-
if (start < 0) start = 0;
|
|
491
|
-
if (end > this.length) end = this.length;
|
|
492
|
-
if (start >= end) return this;
|
|
493
|
-
for (let i = start; i < end; i++) {
|
|
494
|
-
this.setAt(i, value);
|
|
495
|
-
}
|
|
496
|
-
return this;
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
var LinearLinkedBase = class extends LinearBase {
|
|
500
|
-
/**
|
|
501
|
-
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
502
|
-
* if provided and valid.
|
|
503
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
504
|
-
* constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
|
|
505
|
-
* `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
|
|
506
|
-
*/
|
|
507
|
-
constructor(options) {
|
|
508
|
-
super(options);
|
|
509
|
-
if (options) {
|
|
510
|
-
const { maxLen } = options;
|
|
511
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
/**
|
|
515
|
-
* Time Complexity: O(n)
|
|
516
|
-
* Space Complexity: O(1)
|
|
517
|
-
*
|
|
518
|
-
* The function overrides the indexOf method to improve performance by searching for an element in a
|
|
519
|
-
* custom array implementation starting from a specified index.
|
|
520
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
521
|
-
* within the array. The `indexOf` method will return the index of the first occurrence of this
|
|
522
|
-
* element within the array.
|
|
523
|
-
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
524
|
-
* index in the array at which to start the search for the `searchElement`. If provided, the search
|
|
525
|
-
* will begin at the specified index and continue to the end of the array. If not provided, the
|
|
526
|
-
* search will start at index
|
|
527
|
-
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
528
|
-
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
529
|
-
*/
|
|
530
|
-
indexOf(searchElement, fromIndex = 0) {
|
|
531
|
-
const iterator = this._getIterator();
|
|
532
|
-
let current = iterator.next();
|
|
533
|
-
let index = 0;
|
|
534
|
-
while (index < fromIndex) {
|
|
535
|
-
current = iterator.next();
|
|
536
|
-
index++;
|
|
537
|
-
}
|
|
538
|
-
while (!current.done) {
|
|
539
|
-
if (current.value === searchElement) return index;
|
|
540
|
-
current = iterator.next();
|
|
541
|
-
index++;
|
|
542
|
-
}
|
|
543
|
-
return -1;
|
|
544
|
-
}
|
|
545
|
-
/**
|
|
546
|
-
* Time Complexity: O(n)
|
|
547
|
-
* Space Complexity: O(1)
|
|
548
|
-
*
|
|
549
|
-
* The function overrides the lastIndexOf method in TypeScript to improve performance by searching
|
|
550
|
-
* for an element in reverse order starting from a specified index.
|
|
551
|
-
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find
|
|
552
|
-
* within the array. The `lastIndexOf` method searches the array for this element starting from the
|
|
553
|
-
* end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
|
|
554
|
-
* occurrence of the element
|
|
555
|
-
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
556
|
-
* index at which to start searching for the `searchElement` in the array. If provided, the search
|
|
557
|
-
* will begin at this index and move towards the beginning of the array. If not provided, the search
|
|
558
|
-
* will start at the
|
|
559
|
-
* @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
|
|
560
|
-
* from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
|
|
561
|
-
* reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
|
|
562
|
-
* occurrence of the `searchElement` if found, or -1 if not found.
|
|
563
|
-
*/
|
|
564
|
-
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
565
|
-
const iterator = this._getReverseIterator();
|
|
566
|
-
let current = iterator.next();
|
|
567
|
-
let index = this.length - 1;
|
|
568
|
-
while (index > fromIndex) {
|
|
569
|
-
current = iterator.next();
|
|
570
|
-
index--;
|
|
571
|
-
}
|
|
572
|
-
while (!current.done) {
|
|
573
|
-
if (current.value === searchElement) return index;
|
|
574
|
-
current = iterator.next();
|
|
575
|
-
index--;
|
|
576
|
-
}
|
|
577
|
-
return -1;
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Time Complexity: O(n + m)
|
|
581
|
-
* Space Complexity: O(n + m)
|
|
582
|
-
*
|
|
583
|
-
* The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
|
|
584
|
-
* list, handling both individual elements and instances of `LinearBase`.
|
|
585
|
-
* @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
|
|
586
|
-
* number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
|
|
587
|
-
* current list and returns a new list with the concatenated items.
|
|
588
|
-
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
589
|
-
* items passed as arguments concatenated to it.
|
|
590
|
-
*/
|
|
591
|
-
concat(...items) {
|
|
592
|
-
const newList = this.clone();
|
|
593
|
-
for (const item of items) {
|
|
594
|
-
if (item instanceof LinearBase) {
|
|
595
|
-
newList.pushMany(item);
|
|
596
|
-
} else {
|
|
597
|
-
newList.push(item);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
return newList;
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Time Complexity: O(m)
|
|
604
|
-
* Space Complexity: O(m)
|
|
605
|
-
*
|
|
606
|
-
* The `slice` method is overridden to improve performance by creating a new instance and iterating
|
|
607
|
-
* through the array to extract a subset based on the specified start and end indices.
|
|
608
|
-
* @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
|
|
609
|
-
* which to begin extracting elements from the array. If no `start` parameter is provided, the
|
|
610
|
-
* default value is 0, indicating that extraction should start from the beginning of the array.
|
|
611
|
-
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
612
|
-
* end the slicing of the array. If not provided, it defaults to the length of the array.
|
|
613
|
-
* @returns The `slice` method is returning a new instance of the array implementation with elements
|
|
614
|
-
* sliced from the original array based on the `start` and `end` parameters.
|
|
615
|
-
*/
|
|
616
|
-
slice(start = 0, end = this.length) {
|
|
617
|
-
start = start < 0 ? this.length + start : start;
|
|
618
|
-
end = end < 0 ? this.length + end : end;
|
|
619
|
-
const newList = this._createInstance();
|
|
620
|
-
const iterator = this._getIterator();
|
|
621
|
-
let current = iterator.next();
|
|
622
|
-
let c = 0;
|
|
623
|
-
while (c < start) {
|
|
624
|
-
current = iterator.next();
|
|
625
|
-
c++;
|
|
626
|
-
}
|
|
627
|
-
for (let i = start; i < end; i++) {
|
|
628
|
-
newList.push(current.value);
|
|
629
|
-
current = iterator.next();
|
|
630
|
-
}
|
|
631
|
-
return newList;
|
|
632
|
-
}
|
|
633
|
-
/**
|
|
634
|
-
* Time Complexity: O(n + m)
|
|
635
|
-
* Space Complexity: O(m)
|
|
636
|
-
*
|
|
637
|
-
* The function overrides the splice method to handle deletion and insertion of elements in a data
|
|
638
|
-
* structure while returning the removed elements.
|
|
639
|
-
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
640
|
-
* to start modifying the array.
|
|
641
|
-
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
642
|
-
* number of elements to remove from the array starting at the specified `start` index. If
|
|
643
|
-
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
644
|
-
* elements can still be inserted at
|
|
645
|
-
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
646
|
-
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
647
|
-
* and there can be multiple elements passed as arguments to be inserted into the array.
|
|
648
|
-
* @returns The `splice` method is returning a new instance of the data structure that was modified
|
|
649
|
-
* by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
|
|
650
|
-
* elements provided in the `items` array.
|
|
651
|
-
*/
|
|
652
|
-
splice(start, deleteCount = 0, ...items) {
|
|
653
|
-
const removedList = this._createInstance();
|
|
654
|
-
start = start < 0 ? this.length + start : start;
|
|
655
|
-
start = Math.max(0, Math.min(start, this.length));
|
|
656
|
-
deleteCount = Math.max(0, deleteCount);
|
|
657
|
-
let currentIndex = 0;
|
|
658
|
-
let currentNode = void 0;
|
|
659
|
-
let previousNode = void 0;
|
|
660
|
-
const iterator = this._getNodeIterator();
|
|
661
|
-
for (const node of iterator) {
|
|
662
|
-
if (currentIndex === start) {
|
|
663
|
-
currentNode = node;
|
|
664
|
-
break;
|
|
665
|
-
}
|
|
666
|
-
previousNode = node;
|
|
667
|
-
currentIndex++;
|
|
668
|
-
}
|
|
669
|
-
for (let i = 0; i < deleteCount && currentNode; i++) {
|
|
670
|
-
removedList.push(currentNode.value);
|
|
671
|
-
const nextNode = currentNode.next;
|
|
672
|
-
this.delete(currentNode);
|
|
673
|
-
currentNode = nextNode;
|
|
674
|
-
}
|
|
675
|
-
for (let i = 0; i < items.length; i++) {
|
|
676
|
-
if (previousNode) {
|
|
677
|
-
this.addAfter(previousNode, items[i]);
|
|
678
|
-
previousNode = previousNode.next;
|
|
679
|
-
} else {
|
|
680
|
-
this.addAt(0, items[i]);
|
|
681
|
-
previousNode = this._getNodeIterator().next().value;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
return removedList;
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Time Complexity: O(n)
|
|
688
|
-
* Space Complexity: O(1)
|
|
689
|
-
*
|
|
690
|
-
* The function `reduceRight` iterates over an array in reverse order and applies a callback function
|
|
691
|
-
* to each element, accumulating a single result.
|
|
692
|
-
* @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
|
|
693
|
-
* of the array from right to left. It takes four arguments:
|
|
694
|
-
* @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
|
|
695
|
-
* initial accumulator value in the reduce operation. If provided, the reduce operation starts with
|
|
696
|
-
* this initial value and iterates over the elements of the array, applying the callback function to
|
|
697
|
-
* each element and the current accumulator value. If `initial
|
|
698
|
-
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
699
|
-
* callback function to each element in the array from right to left.
|
|
700
|
-
*/
|
|
701
|
-
reduceRight(callbackfn, initialValue) {
|
|
702
|
-
let accumulator = initialValue ?? 0;
|
|
703
|
-
let index = this.length - 1;
|
|
704
|
-
for (const item of this._getReverseIterator()) {
|
|
705
|
-
accumulator = callbackfn(accumulator, item, index--, this);
|
|
706
|
-
}
|
|
707
|
-
return accumulator;
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
|
|
711
|
-
// src/data-structures/linked-list/doubly-linked-list.ts
|
|
712
|
-
var DoublyLinkedListNode = class extends LinkedListNode {
|
|
713
|
-
/**
|
|
714
|
-
* The constructor function initializes the value, next, and previous properties of an object.
|
|
715
|
-
* @param {E} value - The "value" parameter is the value that will be stored in the node. It can be of any data type, as it
|
|
716
|
-
* is defined as a generic type "E".
|
|
717
|
-
*/
|
|
718
|
-
constructor(value) {
|
|
719
|
-
super(value);
|
|
720
|
-
this._value = value;
|
|
721
|
-
this._next = void 0;
|
|
722
|
-
this._prev = void 0;
|
|
723
|
-
}
|
|
724
|
-
_next;
|
|
725
|
-
get next() {
|
|
726
|
-
return this._next;
|
|
727
|
-
}
|
|
728
|
-
set next(value) {
|
|
729
|
-
this._next = value;
|
|
730
|
-
}
|
|
731
|
-
_prev;
|
|
732
|
-
get prev() {
|
|
733
|
-
return this._prev;
|
|
734
|
-
}
|
|
735
|
-
set prev(value) {
|
|
736
|
-
this._prev = value;
|
|
737
|
-
}
|
|
738
|
-
};
|
|
739
|
-
var DoublyLinkedList = class _DoublyLinkedList extends LinearLinkedBase {
|
|
740
|
-
/**
|
|
741
|
-
* This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
|
|
742
|
-
* @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
|
|
743
|
-
* iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
|
|
744
|
-
* with the elements provided in the iterable. If no elements are provided, the default value is an
|
|
745
|
-
* empty iterable.
|
|
746
|
-
* @param [options] - The `options` parameter in the constructor is of type
|
|
747
|
-
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
748
|
-
* configuration options to customize the behavior of the DoublyLinkedList.
|
|
749
|
-
*/
|
|
750
|
-
constructor(elements = [], options) {
|
|
751
|
-
super(options);
|
|
752
|
-
this._head = void 0;
|
|
753
|
-
this._tail = void 0;
|
|
754
|
-
this._length = 0;
|
|
755
|
-
if (options) {
|
|
756
|
-
const { maxLen } = options;
|
|
757
|
-
if (typeof maxLen === "number" && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
|
|
758
|
-
}
|
|
759
|
-
this.pushMany(elements);
|
|
760
|
-
}
|
|
761
|
-
_head;
|
|
762
|
-
get head() {
|
|
763
|
-
return this._head;
|
|
764
|
-
}
|
|
765
|
-
_tail;
|
|
766
|
-
get tail() {
|
|
767
|
-
return this._tail;
|
|
768
|
-
}
|
|
769
|
-
_length;
|
|
770
|
-
get length() {
|
|
771
|
-
return this._length;
|
|
772
|
-
}
|
|
773
|
-
/**
|
|
774
|
-
* Time Complexity: O(1)
|
|
775
|
-
* Space Complexity: O(1)
|
|
776
|
-
*
|
|
777
|
-
* The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
|
|
778
|
-
* @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
|
|
779
|
-
*/
|
|
780
|
-
get first() {
|
|
781
|
-
return this.head?.value;
|
|
782
|
-
}
|
|
783
|
-
/**
|
|
784
|
-
* Time Complexity: O(1)
|
|
785
|
-
* Space Complexity: O(1)
|
|
786
|
-
*
|
|
787
|
-
* The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
|
|
788
|
-
* @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
|
|
789
|
-
*/
|
|
790
|
-
get last() {
|
|
791
|
-
return this.tail?.value;
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* Time Complexity: O(n)
|
|
795
|
-
* Space Complexity: O(n)
|
|
796
|
-
*
|
|
797
|
-
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
|
|
798
|
-
* given array.
|
|
799
|
-
* @param {E[]} data - The `data` parameter is an array of elements of type `E`.
|
|
800
|
-
* @returns The `fromArray` function returns a DoublyLinkedList object.
|
|
801
|
-
*/
|
|
802
|
-
static fromArray(data) {
|
|
803
|
-
return new _DoublyLinkedList(data);
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Time Complexity: O(1)
|
|
807
|
-
* Space Complexity: O(1)
|
|
808
|
-
*
|
|
809
|
-
* The function `isNode` in TypeScript checks if a given input is an instance of
|
|
810
|
-
* `DoublyLinkedListNode`.
|
|
811
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
812
|
-
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
|
|
813
|
-
* be one of the following types:
|
|
814
|
-
* @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
|
|
815
|
-
* instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
|
|
816
|
-
* parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
|
|
817
|
-
* the function returns `false`.
|
|
818
|
-
*/
|
|
819
|
-
isNode(elementNodeOrPredicate) {
|
|
820
|
-
return elementNodeOrPredicate instanceof DoublyLinkedListNode;
|
|
821
|
-
}
|
|
822
|
-
/**
|
|
823
|
-
* Time Complexity: O(1)
|
|
824
|
-
* Space Complexity: O(1)
|
|
825
|
-
*
|
|
826
|
-
* The `push` function adds a new element or node to the end of a doubly linked list.
|
|
827
|
-
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
|
|
828
|
-
* method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
829
|
-
* @returns The `push` method is returning a boolean value, specifically `true`.
|
|
830
|
-
*/
|
|
831
|
-
push(elementOrNode) {
|
|
832
|
-
const newNode = this._ensureNode(elementOrNode);
|
|
833
|
-
if (!this.head) {
|
|
834
|
-
this._head = newNode;
|
|
835
|
-
this._tail = newNode;
|
|
836
|
-
} else {
|
|
837
|
-
newNode.prev = this.tail;
|
|
838
|
-
this.tail.next = newNode;
|
|
839
|
-
this._tail = newNode;
|
|
840
|
-
}
|
|
841
|
-
this._length++;
|
|
842
|
-
if (this._maxLen > 0 && this.length > this._maxLen) this.shift();
|
|
843
|
-
return true;
|
|
844
|
-
}
|
|
845
|
-
/**
|
|
846
|
-
* Time Complexity: O(1)
|
|
847
|
-
* Space Complexity: O(1)
|
|
848
|
-
*
|
|
849
|
-
* The `pop()` function removes and returns the value of the last element in a linked list.
|
|
850
|
-
* @returns The method is returning the value of the removed node.
|
|
851
|
-
*/
|
|
852
|
-
pop() {
|
|
853
|
-
if (!this.tail) return void 0;
|
|
854
|
-
const removedNode = this.tail;
|
|
855
|
-
if (this.head === this.tail) {
|
|
856
|
-
this._head = void 0;
|
|
857
|
-
this._tail = void 0;
|
|
858
|
-
} else {
|
|
859
|
-
this._tail = removedNode.prev;
|
|
860
|
-
this.tail.next = void 0;
|
|
861
|
-
}
|
|
862
|
-
this._length--;
|
|
863
|
-
return removedNode.value;
|
|
864
|
-
}
|
|
865
|
-
/**
|
|
866
|
-
* Time Complexity: O(1)
|
|
867
|
-
* Space Complexity: O(1)
|
|
868
|
-
*
|
|
869
|
-
* The `shift()` function removes and returns the value of the first element in a doubly linked list.
|
|
870
|
-
* @returns The value of the removed node.
|
|
871
|
-
*/
|
|
872
|
-
shift() {
|
|
873
|
-
if (!this.head) return void 0;
|
|
874
|
-
const removedNode = this.head;
|
|
875
|
-
if (this.head === this.tail) {
|
|
876
|
-
this._head = void 0;
|
|
877
|
-
this._tail = void 0;
|
|
878
|
-
} else {
|
|
879
|
-
this._head = removedNode.next;
|
|
880
|
-
this.head.prev = void 0;
|
|
881
|
-
}
|
|
882
|
-
this._length--;
|
|
883
|
-
return removedNode.value;
|
|
884
|
-
}
|
|
885
|
-
/**
|
|
886
|
-
* Time Complexity: O(1)
|
|
887
|
-
* Space Complexity: O(1)
|
|
888
|
-
*
|
|
889
|
-
* The unshift function adds a new element or node to the beginning of a doubly linked list.
|
|
890
|
-
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
|
|
891
|
-
* `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
|
|
892
|
-
* element of type `E`.
|
|
893
|
-
* @returns The `unshift` method is returning a boolean value, specifically `true`.
|
|
894
|
-
*/
|
|
895
|
-
unshift(elementOrNode) {
|
|
896
|
-
const newNode = this._ensureNode(elementOrNode);
|
|
897
|
-
if (!this.head) {
|
|
898
|
-
this._head = newNode;
|
|
899
|
-
this._tail = newNode;
|
|
900
|
-
} else {
|
|
901
|
-
newNode.next = this.head;
|
|
902
|
-
this.head.prev = newNode;
|
|
903
|
-
this._head = newNode;
|
|
904
|
-
}
|
|
905
|
-
this._length++;
|
|
906
|
-
if (this._maxLen > 0 && this._length > this._maxLen) this.pop();
|
|
907
|
-
return true;
|
|
908
|
-
}
|
|
909
|
-
/**
|
|
910
|
-
* Time Complexity: O(k)
|
|
911
|
-
* Space Complexity: O(k)
|
|
912
|
-
*
|
|
913
|
-
* The function `pushMany` iterates over elements and pushes them into a data structure, applying a
|
|
914
|
-
* transformation function if provided.
|
|
915
|
-
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
916
|
-
* parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
|
|
917
|
-
* or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
|
|
918
|
-
* it onto the linked list. If a transformation function `to
|
|
919
|
-
* @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
|
|
920
|
-
* the success or failure of pushing each element into the data structure.
|
|
921
|
-
*/
|
|
922
|
-
pushMany(elements) {
|
|
923
|
-
const ans = [];
|
|
924
|
-
for (const el of elements) {
|
|
925
|
-
if (this.toElementFn) {
|
|
926
|
-
ans.push(this.push(this.toElementFn(el)));
|
|
927
|
-
continue;
|
|
928
|
-
}
|
|
929
|
-
ans.push(this.push(el));
|
|
930
|
-
}
|
|
931
|
-
return ans;
|
|
932
|
-
}
|
|
933
|
-
/**
|
|
934
|
-
* Time Complexity: O(k)
|
|
935
|
-
* Space Complexity: O(k)
|
|
936
|
-
*
|
|
937
|
-
* The function `unshiftMany` iterates through a collection of elements and adds them to the
|
|
938
|
-
* beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
|
|
939
|
-
* each insertion.
|
|
940
|
-
* @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
|
|
941
|
-
* parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
|
|
942
|
-
* `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
|
|
943
|
-
* performs an `unshift` operation on the doubly linked list
|
|
944
|
-
* @returns The `unshiftMany` function returns an array of boolean values indicating the success of
|
|
945
|
-
* each unshift operation performed on the elements passed as input.
|
|
946
|
-
*/
|
|
947
|
-
unshiftMany(elements) {
|
|
948
|
-
const ans = [];
|
|
949
|
-
for (const el of elements) {
|
|
950
|
-
if (this.toElementFn) {
|
|
951
|
-
ans.push(this.unshift(this.toElementFn(el)));
|
|
952
|
-
continue;
|
|
953
|
-
}
|
|
954
|
-
ans.push(this.unshift(el));
|
|
955
|
-
}
|
|
956
|
-
return ans;
|
|
957
|
-
}
|
|
958
|
-
/**
|
|
959
|
-
* Time Complexity: O(n)
|
|
960
|
-
* Space Complexity: O(1)
|
|
961
|
-
*
|
|
962
|
-
* The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
|
|
963
|
-
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
964
|
-
* retrieve from the list.
|
|
965
|
-
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
|
|
966
|
-
* or the linked list is empty, it will return undefined.
|
|
967
|
-
*/
|
|
968
|
-
at(index) {
|
|
969
|
-
if (index < 0 || index >= this._length) return void 0;
|
|
970
|
-
let current = this.head;
|
|
971
|
-
for (let i = 0; i < index; i++) {
|
|
972
|
-
current = current.next;
|
|
973
|
-
}
|
|
974
|
-
return current.value;
|
|
975
|
-
}
|
|
976
|
-
/**
|
|
977
|
-
* Time Complexity: O(n)
|
|
978
|
-
* Space Complexity: O(1)
|
|
979
|
-
*
|
|
980
|
-
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
|
|
981
|
-
* range.
|
|
982
|
-
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
|
|
983
|
-
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
|
|
984
|
-
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
|
|
985
|
-
* valid range of the linked list, otherwise it returns `undefined`.
|
|
986
|
-
*/
|
|
987
|
-
getNodeAt(index) {
|
|
988
|
-
if (index < 0 || index >= this._length) return void 0;
|
|
989
|
-
let current = this.head;
|
|
990
|
-
for (let i = 0; i < index; i++) {
|
|
991
|
-
current = current.next;
|
|
992
|
-
}
|
|
993
|
-
return current;
|
|
994
|
-
}
|
|
995
|
-
/**
|
|
996
|
-
* Time Complexity: O(n)
|
|
997
|
-
* Space Complexity: O(1)
|
|
998
|
-
*
|
|
999
|
-
* This TypeScript function searches for a node in a doubly linked list based on a given element node
|
|
1000
|
-
* or predicate.
|
|
1001
|
-
* @param {| E
|
|
1002
|
-
* | DoublyLinkedListNode<E>
|
|
1003
|
-
* | ((node: DoublyLinkedListNode<E>) => boolean)
|
|
1004
|
-
* | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
|
|
1005
|
-
* node in a doubly linked list based on a given element, node, or predicate function. The
|
|
1006
|
-
* `elementNodeOrPredicate` parameter can be one of the following:
|
|
1007
|
-
* @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
|
|
1008
|
-
* input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
|
|
1009
|
-
* Otherwise, it iterates through the linked list starting from the head node and applies the
|
|
1010
|
-
* provided predicate function to each node. If a node satisfies the predicate, that node is
|
|
1011
|
-
* returned. If
|
|
1012
|
-
*/
|
|
1013
|
-
getNode(elementNodeOrPredicate) {
|
|
1014
|
-
if (elementNodeOrPredicate === void 0) return;
|
|
1015
|
-
if (this.isNode(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
1016
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1017
|
-
let current = this.head;
|
|
1018
|
-
while (current) {
|
|
1019
|
-
if (predicate(current)) {
|
|
1020
|
-
return current;
|
|
1021
|
-
}
|
|
1022
|
-
current = current.next;
|
|
1023
|
-
}
|
|
1024
|
-
return void 0;
|
|
1025
|
-
}
|
|
1026
|
-
/**
|
|
1027
|
-
* Time Complexity: O(n)
|
|
1028
|
-
* Space Complexity: O(1)
|
|
1029
|
-
*
|
|
1030
|
-
* The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
|
|
1031
|
-
* @param {number} index - The `index` parameter in the `addAt` method represents the position at
|
|
1032
|
-
* which you want to add a new element or node in the doubly linked list. It indicates the location
|
|
1033
|
-
* where the new element or node should be inserted.
|
|
1034
|
-
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
1035
|
-
* `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
|
|
1036
|
-
* @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
|
|
1037
|
-
* successfully added at the specified index, and `false` if the index is out of bounds (less than 0
|
|
1038
|
-
* or greater than the length of the list).
|
|
1039
|
-
*/
|
|
1040
|
-
addAt(index, newElementOrNode) {
|
|
1041
|
-
if (index < 0 || index > this._length) return false;
|
|
1042
|
-
if (index === 0) {
|
|
1043
|
-
this.unshift(newElementOrNode);
|
|
1044
|
-
return true;
|
|
1045
|
-
}
|
|
1046
|
-
if (index === this._length) {
|
|
1047
|
-
this.push(newElementOrNode);
|
|
1048
|
-
return true;
|
|
1049
|
-
}
|
|
1050
|
-
const newNode = this._ensureNode(newElementOrNode);
|
|
1051
|
-
const prevNode = this.getNodeAt(index - 1);
|
|
1052
|
-
const nextNode = prevNode.next;
|
|
1053
|
-
newNode.prev = prevNode;
|
|
1054
|
-
newNode.next = nextNode;
|
|
1055
|
-
prevNode.next = newNode;
|
|
1056
|
-
nextNode.prev = newNode;
|
|
1057
|
-
this._length++;
|
|
1058
|
-
return true;
|
|
1059
|
-
}
|
|
1060
|
-
/**
|
|
1061
|
-
* Time Complexity: O(1) or O(n)
|
|
1062
|
-
* Space Complexity: O(1)
|
|
1063
|
-
*
|
|
1064
|
-
* The `addBefore` function in TypeScript adds a new element or node before an existing element or
|
|
1065
|
-
* node in a doubly linked list.
|
|
1066
|
-
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
|
|
1067
|
-
* in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
|
|
1068
|
-
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
|
|
1069
|
-
* represents the element or node that you want to add before the `existingElementOrNode` in a doubly
|
|
1070
|
-
* linked list.
|
|
1071
|
-
* @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
|
|
1072
|
-
* successfully added before the existing element or node, and `false` if the existing element or
|
|
1073
|
-
* node was not found.
|
|
1074
|
-
*/
|
|
1075
|
-
addBefore(existingElementOrNode, newElementOrNode) {
|
|
1076
|
-
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
1077
|
-
if (existingNode) {
|
|
1078
|
-
const newNode = this._ensureNode(newElementOrNode);
|
|
1079
|
-
newNode.prev = existingNode.prev;
|
|
1080
|
-
if (existingNode.prev) {
|
|
1081
|
-
existingNode.prev.next = newNode;
|
|
1082
|
-
}
|
|
1083
|
-
newNode.next = existingNode;
|
|
1084
|
-
existingNode.prev = newNode;
|
|
1085
|
-
if (existingNode === this.head) {
|
|
1086
|
-
this._head = newNode;
|
|
1087
|
-
}
|
|
1088
|
-
this._length++;
|
|
1089
|
-
return true;
|
|
1090
|
-
}
|
|
1091
|
-
return false;
|
|
1092
|
-
}
|
|
1093
|
-
/**
|
|
1094
|
-
* Time Complexity: O(1) or O(n)
|
|
1095
|
-
* Space Complexity: O(1)
|
|
1096
|
-
*
|
|
1097
|
-
* The `addAfter` function in TypeScript adds a new element or node after an existing element or node
|
|
1098
|
-
* in a doubly linked list.
|
|
1099
|
-
* @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
|
|
1100
|
-
* element or node in the doubly linked list after which you want to add a new element or node.
|
|
1101
|
-
* @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
|
|
1102
|
-
* `addAfter` method represents the element or node that you want to add after the existing element
|
|
1103
|
-
* or node in a doubly linked list. This parameter can be either an element value or a
|
|
1104
|
-
* `DoublyLinkedListNode` object that you want to insert
|
|
1105
|
-
* @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
|
|
1106
|
-
* successfully added after the existing element or node, and `false` if the existing element or node
|
|
1107
|
-
* was not found in the linked list.
|
|
1108
|
-
*/
|
|
1109
|
-
addAfter(existingElementOrNode, newElementOrNode) {
|
|
1110
|
-
const existingNode = this.isNode(existingElementOrNode) ? existingElementOrNode : this.getNode(existingElementOrNode);
|
|
1111
|
-
if (existingNode) {
|
|
1112
|
-
const newNode = this._ensureNode(newElementOrNode);
|
|
1113
|
-
newNode.next = existingNode.next;
|
|
1114
|
-
if (existingNode.next) {
|
|
1115
|
-
existingNode.next.prev = newNode;
|
|
1116
|
-
}
|
|
1117
|
-
newNode.prev = existingNode;
|
|
1118
|
-
existingNode.next = newNode;
|
|
1119
|
-
if (existingNode === this.tail) {
|
|
1120
|
-
this._tail = newNode;
|
|
1121
|
-
}
|
|
1122
|
-
this._length++;
|
|
1123
|
-
return true;
|
|
1124
|
-
}
|
|
1125
|
-
return false;
|
|
1126
|
-
}
|
|
1127
|
-
/**
|
|
1128
|
-
* Time Complexity: O(n)
|
|
1129
|
-
* Space Complexity: O(1)
|
|
1130
|
-
*
|
|
1131
|
-
* The function `setAt` updates the value at a specified index in a data structure if the index
|
|
1132
|
-
* exists.
|
|
1133
|
-
* @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
|
|
1134
|
-
* data structure where you want to set a new value.
|
|
1135
|
-
* @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
|
|
1136
|
-
* want to set at the specified index in the data structure.
|
|
1137
|
-
* @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
|
|
1138
|
-
* is successfully updated, and `false` if the index is out of bounds.
|
|
1139
|
-
*/
|
|
1140
|
-
setAt(index, value) {
|
|
1141
|
-
const node = this.getNodeAt(index);
|
|
1142
|
-
if (node) {
|
|
1143
|
-
node.value = value;
|
|
1144
|
-
return true;
|
|
1145
|
-
}
|
|
1146
|
-
return false;
|
|
1147
|
-
}
|
|
1148
|
-
/**
|
|
1149
|
-
* Time Complexity: O(n)
|
|
1150
|
-
* Space Complexity: O(1)
|
|
1151
|
-
*
|
|
1152
|
-
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
1153
|
-
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
1154
|
-
* data structure. It is of type number.
|
|
1155
|
-
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
1156
|
-
* bounds.
|
|
1157
|
-
*/
|
|
1158
|
-
deleteAt(index) {
|
|
1159
|
-
if (index < 0 || index >= this._length) return;
|
|
1160
|
-
let deleted;
|
|
1161
|
-
if (index === 0) {
|
|
1162
|
-
deleted = this.first;
|
|
1163
|
-
this.shift();
|
|
1164
|
-
return deleted;
|
|
1165
|
-
}
|
|
1166
|
-
if (index === this._length - 1) {
|
|
1167
|
-
deleted = this.last;
|
|
1168
|
-
this.pop();
|
|
1169
|
-
return deleted;
|
|
1170
|
-
}
|
|
1171
|
-
const removedNode = this.getNodeAt(index);
|
|
1172
|
-
const prevNode = removedNode.prev;
|
|
1173
|
-
const nextNode = removedNode.next;
|
|
1174
|
-
prevNode.next = nextNode;
|
|
1175
|
-
nextNode.prev = prevNode;
|
|
1176
|
-
this._length--;
|
|
1177
|
-
return removedNode?.value;
|
|
1178
|
-
}
|
|
1179
|
-
/**
|
|
1180
|
-
* Time Complexity: O(1) or O(n)
|
|
1181
|
-
* Space Complexity: O(1)
|
|
1182
|
-
*
|
|
1183
|
-
* The `delete` function removes a specified element or node from a doubly linked list if it exists.
|
|
1184
|
-
* @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
|
|
1185
|
-
* the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
|
|
1186
|
-
* can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
|
|
1187
|
-
* doubly linked list
|
|
1188
|
-
* @returns The `delete` method returns a boolean value - `true` if the element or node was
|
|
1189
|
-
* successfully deleted from the doubly linked list, and `false` if the element or node was not found
|
|
1190
|
-
* in the list.
|
|
1191
|
-
*/
|
|
1192
|
-
delete(elementOrNode) {
|
|
1193
|
-
const node = this.getNode(elementOrNode);
|
|
1194
|
-
if (node) {
|
|
1195
|
-
if (node === this.head) {
|
|
1196
|
-
this.shift();
|
|
1197
|
-
} else if (node === this.tail) {
|
|
1198
|
-
this.pop();
|
|
1199
|
-
} else {
|
|
1200
|
-
const prevNode = node.prev;
|
|
1201
|
-
const nextNode = node.next;
|
|
1202
|
-
if (prevNode) prevNode.next = nextNode;
|
|
1203
|
-
if (nextNode) nextNode.prev = prevNode;
|
|
1204
|
-
this._length--;
|
|
1205
|
-
}
|
|
1206
|
-
return true;
|
|
1207
|
-
}
|
|
1208
|
-
return false;
|
|
1209
|
-
}
|
|
1210
|
-
/**
|
|
1211
|
-
* Time Complexity: O(1)
|
|
1212
|
-
* Space Complexity: O(1)
|
|
1213
|
-
*
|
|
1214
|
-
* The function checks if a variable has a length greater than zero and returns a boolean value.
|
|
1215
|
-
* @returns A boolean value is being returned.
|
|
1216
|
-
*/
|
|
1217
|
-
isEmpty() {
|
|
1218
|
-
return this._length === 0;
|
|
1219
|
-
}
|
|
1220
|
-
/**
|
|
1221
|
-
* Time Complexity: O(1)
|
|
1222
|
-
* Space Complexity: O(1)
|
|
1223
|
-
*
|
|
1224
|
-
* The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
|
|
1225
|
-
*/
|
|
1226
|
-
clear() {
|
|
1227
|
-
this._head = void 0;
|
|
1228
|
-
this._tail = void 0;
|
|
1229
|
-
this._length = 0;
|
|
1230
|
-
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Time Complexity: O(n)
|
|
1233
|
-
* Space Complexity: O(1)
|
|
1234
|
-
*
|
|
1235
|
-
* This function retrieves an element from a doubly linked list based on a given element
|
|
1236
|
-
* node or predicate.
|
|
1237
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1238
|
-
* elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
|
|
1239
|
-
* which can be one of the following types:
|
|
1240
|
-
* @returns The `get` method returns the value of the first node in the doubly linked list that
|
|
1241
|
-
* satisfies the provided predicate function. If no such node is found, it returns `undefined`.
|
|
1242
|
-
*/
|
|
1243
|
-
search(elementNodeOrPredicate) {
|
|
1244
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1245
|
-
let current = this.head;
|
|
1246
|
-
while (current) {
|
|
1247
|
-
if (predicate(current)) return current.value;
|
|
1248
|
-
current = current.next;
|
|
1249
|
-
}
|
|
1250
|
-
return void 0;
|
|
1251
|
-
}
|
|
1252
|
-
/**
|
|
1253
|
-
* Time Complexity: O(n)
|
|
1254
|
-
* Space Complexity: O(1)
|
|
1255
|
-
*
|
|
1256
|
-
* The `getBackward` function searches for a specific element in a doubly linked list starting from
|
|
1257
|
-
* the tail and moving backwards.
|
|
1258
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1259
|
-
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
|
|
1260
|
-
* function can be one of the following types:
|
|
1261
|
-
* @returns The `getBackward` method returns the value of the element node that matches the provided
|
|
1262
|
-
* predicate when traversing the doubly linked list backwards. If no matching element is found, it
|
|
1263
|
-
* returns `undefined`.
|
|
1264
|
-
*/
|
|
1265
|
-
getBackward(elementNodeOrPredicate) {
|
|
1266
|
-
const predicate = this._ensurePredicate(elementNodeOrPredicate);
|
|
1267
|
-
let current = this.tail;
|
|
1268
|
-
while (current) {
|
|
1269
|
-
if (predicate(current)) return current.value;
|
|
1270
|
-
current = current.prev;
|
|
1271
|
-
}
|
|
1272
|
-
return void 0;
|
|
1273
|
-
}
|
|
1274
|
-
/**
|
|
1275
|
-
* Time Complexity: O(n)
|
|
1276
|
-
* Space Complexity: O(1)
|
|
1277
|
-
*
|
|
1278
|
-
* The `reverse` function reverses the order of the elements in a doubly linked list.
|
|
1279
|
-
*/
|
|
1280
|
-
reverse() {
|
|
1281
|
-
let current = this.head;
|
|
1282
|
-
[this._head, this._tail] = [this.tail, this.head];
|
|
1283
|
-
while (current) {
|
|
1284
|
-
const next = current.next;
|
|
1285
|
-
[current.prev, current.next] = [current.next, current.prev];
|
|
1286
|
-
current = next;
|
|
1287
|
-
}
|
|
1288
|
-
return this;
|
|
1289
|
-
}
|
|
1290
|
-
/**
|
|
1291
|
-
* Time Complexity: O(n)
|
|
1292
|
-
* Space Complexity: O(n)
|
|
1293
|
-
*
|
|
1294
|
-
* The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values
|
|
1295
|
-
* as the original list.
|
|
1296
|
-
* @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
|
|
1297
|
-
* is a copy of the original list.
|
|
1298
|
-
*/
|
|
1299
|
-
clone() {
|
|
1300
|
-
return new _DoublyLinkedList(this, { toElementFn: this._toElementFn, maxLen: this._maxLen });
|
|
1301
|
-
}
|
|
1302
|
-
/**
|
|
1303
|
-
* Time Complexity: O(n)
|
|
1304
|
-
* Space Complexity: O(n)
|
|
1305
|
-
*
|
|
1306
|
-
* The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
|
|
1307
|
-
* list and applying a callback function to each element, returning only the elements for which the
|
|
1308
|
-
* callback function returns true.
|
|
1309
|
-
* @param callback - The `callback` parameter is a function that will be called for each element in
|
|
1310
|
-
* the DoublyLinkedList. It takes three arguments: the current element, the index of the current
|
|
1311
|
-
* element, and the DoublyLinkedList itself. The callback function should return a boolean value
|
|
1312
|
-
* indicating whether the current element should be included
|
|
1313
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
1314
|
-
* to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
|
|
1315
|
-
* passed as the `this` value to the `callback` function. If `thisArg` is
|
|
1316
|
-
* @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
|
|
1317
|
-
* elements that pass the filter condition specified by the `callback` function.
|
|
1318
|
-
*/
|
|
1319
|
-
filter(callback, thisArg) {
|
|
1320
|
-
const filteredList = this._createInstance({ toElementFn: this.toElementFn, maxLen: this._maxLen });
|
|
1321
|
-
let index = 0;
|
|
1322
|
-
for (const current of this) {
|
|
1323
|
-
if (callback.call(thisArg, current, index, this)) {
|
|
1324
|
-
filteredList.push(current);
|
|
1325
|
-
}
|
|
1326
|
-
index++;
|
|
1327
|
-
}
|
|
1328
|
-
return filteredList;
|
|
1329
|
-
}
|
|
1330
|
-
/**
|
|
1331
|
-
* Time Complexity: O(n)
|
|
1332
|
-
* Space Complexity: O(n)
|
|
1333
|
-
*
|
|
1334
|
-
* The `map` function takes a callback function and returns a new DoublyLinkedList with the results
|
|
1335
|
-
* of applying the callback to each element in the original list.
|
|
1336
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
1337
|
-
* original DoublyLinkedList. It takes three arguments: current (the current element being
|
|
1338
|
-
* processed), index (the index of the current element), and this (the original DoublyLinkedList).
|
|
1339
|
-
* The callback function should return a value of type
|
|
1340
|
-
* @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
|
|
1341
|
-
* convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
|
|
1342
|
-
* input and returns the converted element. If this parameter is not provided, the raw element will
|
|
1343
|
-
* be used as is.
|
|
1344
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
1345
|
-
* specify the value of `this` within the callback function. It is used to set the context or scope
|
|
1346
|
-
* in which the callback function will be executed. If `thisArg` is provided, it will be used as the
|
|
1347
|
-
* value of
|
|
1348
|
-
* @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
|
|
1349
|
-
*/
|
|
1350
|
-
map(callback, toElementFn, thisArg) {
|
|
1351
|
-
const mappedList = new _DoublyLinkedList([], { toElementFn, maxLen: this._maxLen });
|
|
1352
|
-
let index = 0;
|
|
1353
|
-
for (const current of this) {
|
|
1354
|
-
mappedList.push(callback.call(thisArg, current, index, this));
|
|
1355
|
-
index++;
|
|
1356
|
-
}
|
|
1357
|
-
return mappedList;
|
|
1358
|
-
}
|
|
1359
|
-
/**
|
|
1360
|
-
* Time Complexity: O(n)
|
|
1361
|
-
* Space Complexity: O(1)
|
|
1362
|
-
*
|
|
1363
|
-
* The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
|
|
1364
|
-
* of a specified element or nodes that satisfy a given predicate.
|
|
1365
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
|
|
1366
|
-
* - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
|
|
1367
|
-
* @returns The `countOccurrences` method returns the number of occurrences of the specified element,
|
|
1368
|
-
* node, or predicate function in the doubly linked list.
|
|
1369
|
-
*/
|
|
1370
|
-
countOccurrences(elementOrNode) {
|
|
1371
|
-
const predicate = this._ensurePredicate(elementOrNode);
|
|
1372
|
-
let count = 0;
|
|
1373
|
-
let current = this.head;
|
|
1374
|
-
while (current) {
|
|
1375
|
-
if (predicate(current)) {
|
|
1376
|
-
count++;
|
|
1377
|
-
}
|
|
1378
|
-
current = current.next;
|
|
1379
|
-
}
|
|
1380
|
-
return count;
|
|
1381
|
-
}
|
|
1382
|
-
/**
|
|
1383
|
-
* The function returns an iterator that iterates over the values of a linked list.
|
|
1384
|
-
*/
|
|
1385
|
-
*_getIterator() {
|
|
1386
|
-
let current = this.head;
|
|
1387
|
-
while (current) {
|
|
1388
|
-
yield current.value;
|
|
1389
|
-
current = current.next;
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
/**
|
|
1393
|
-
* The function returns an iterator that iterates over the elements of a data structure in reverse
|
|
1394
|
-
* order.
|
|
1395
|
-
*/
|
|
1396
|
-
*_getReverseIterator() {
|
|
1397
|
-
let current = this.tail;
|
|
1398
|
-
while (current) {
|
|
1399
|
-
yield current.value;
|
|
1400
|
-
current = current.prev;
|
|
1401
|
-
}
|
|
1402
|
-
}
|
|
1403
|
-
/**
|
|
1404
|
-
* The function returns an iterator that iterates over the nodes of a doubly linked list starting
|
|
1405
|
-
* from the head.
|
|
1406
|
-
*/
|
|
1407
|
-
*_getNodeIterator() {
|
|
1408
|
-
let current = this.head;
|
|
1409
|
-
while (current) {
|
|
1410
|
-
yield current;
|
|
1411
|
-
current = current.next;
|
|
1412
|
-
}
|
|
1413
|
-
}
|
|
1414
|
-
// protected *_getReverseNodeIterator(): IterableIterator<DoublyLinkedListNode<E>> {
|
|
1415
|
-
// const reversedArr = [...this._getNodeIterator()].reverse();
|
|
1416
|
-
//
|
|
1417
|
-
// for (const item of reversedArr) {
|
|
1418
|
-
// yield item;
|
|
1419
|
-
// }
|
|
1420
|
-
// }
|
|
1421
|
-
/**
|
|
1422
|
-
* The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
|
|
1423
|
-
* as an argument and returns a boolean.
|
|
1424
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1425
|
-
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1426
|
-
* types:
|
|
1427
|
-
* @returns The _isPredicate method is returning a boolean value indicating whether the
|
|
1428
|
-
* elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
|
|
1429
|
-
* function, the method will return true, indicating that it is a predicate function.
|
|
1430
|
-
*/
|
|
1431
|
-
_isPredicate(elementNodeOrPredicate) {
|
|
1432
|
-
return typeof elementNodeOrPredicate === "function";
|
|
1433
|
-
}
|
|
1434
|
-
/**
|
|
1435
|
-
* The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
|
|
1436
|
-
* @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
|
|
1437
|
-
* an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
|
|
1438
|
-
* @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
|
|
1439
|
-
* as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
|
|
1440
|
-
* value and returned.
|
|
1441
|
-
*/
|
|
1442
|
-
_ensureNode(elementOrNode) {
|
|
1443
|
-
if (this.isNode(elementOrNode)) return elementOrNode;
|
|
1444
|
-
return new DoublyLinkedListNode(elementOrNode);
|
|
1445
|
-
}
|
|
1446
|
-
/**
|
|
1447
|
-
* The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
|
|
1448
|
-
* function, or a value to compare with the node's value.
|
|
1449
|
-
* @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
|
|
1450
|
-
* elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
|
|
1451
|
-
* types:
|
|
1452
|
-
* @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
|
|
1453
|
-
* returns a boolean value based on the conditions specified in the code.
|
|
1454
|
-
*/
|
|
1455
|
-
_ensurePredicate(elementNodeOrPredicate) {
|
|
1456
|
-
if (this.isNode(elementNodeOrPredicate)) return (node) => node === elementNodeOrPredicate;
|
|
1457
|
-
if (this._isPredicate(elementNodeOrPredicate)) return elementNodeOrPredicate;
|
|
1458
|
-
return (node) => node.value === elementNodeOrPredicate;
|
|
1459
|
-
}
|
|
1460
|
-
/**
|
|
1461
|
-
* The function `_createInstance` returns a new instance of `DoublyLinkedList` with the specified
|
|
1462
|
-
* options.
|
|
1463
|
-
* @param [options] - The `options` parameter in the `_createInstance` method is of type
|
|
1464
|
-
* `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
|
|
1465
|
-
* configuration options when creating a new instance of the `DoublyLinkedList` class.
|
|
1466
|
-
* @returns An instance of the `DoublyLinkedList` class with an empty array and the provided options
|
|
1467
|
-
* is being returned, cast as the current class type.
|
|
1468
|
-
*/
|
|
1469
|
-
_createInstance(options) {
|
|
1470
|
-
return new _DoublyLinkedList([], options);
|
|
1471
|
-
}
|
|
1472
|
-
/**
|
|
1473
|
-
* The function `_getPrevNode` returns the previous node of a given node in a doubly linked list.
|
|
1474
|
-
* @param node - The parameter `node` in the `_getPrevNode` method is of type
|
|
1475
|
-
* `DoublyLinkedListNode<E>`, which represents a node in a doubly linked list containing an element
|
|
1476
|
-
* of type `E`.
|
|
1477
|
-
* @returns The `_getPrevNode` method is returning the previous node of the input `node` in a doubly
|
|
1478
|
-
* linked list. If the input node has a previous node, it will return that node. Otherwise, it will
|
|
1479
|
-
* return `undefined`.
|
|
1480
|
-
*/
|
|
1481
|
-
_getPrevNode(node) {
|
|
1482
|
-
return node.prev;
|
|
1483
|
-
}
|
|
1484
|
-
};
|
|
1485
|
-
export {
|
|
1486
|
-
DoublyLinkedList,
|
|
1487
|
-
DoublyLinkedListNode
|
|
1488
|
-
};
|
|
1489
|
-
/**
|
|
1490
|
-
* data-structure-typed
|
|
1491
|
-
*
|
|
1492
|
-
* @author Pablo Zeng
|
|
1493
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
1494
|
-
* @license MIT License
|
|
1495
|
-
*/
|