undirected-graph-typed 1.54.3 → 2.0.1
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 +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +196 -63
- package/src/data-structures/stack/stack.ts +124 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LinearLinkedBase = exports.LinearBase = exports.LinkedListNode = void 0;
|
|
4
|
+
const iterable_element_base_1 = require("./iterable-element-base");
|
|
5
|
+
class LinkedListNode {
|
|
6
|
+
constructor(value) {
|
|
7
|
+
this._value = value;
|
|
8
|
+
this._next = undefined;
|
|
9
|
+
}
|
|
10
|
+
get value() {
|
|
11
|
+
return this._value;
|
|
12
|
+
}
|
|
13
|
+
set value(value) {
|
|
14
|
+
this._value = value;
|
|
15
|
+
}
|
|
16
|
+
get next() {
|
|
17
|
+
return this._next;
|
|
18
|
+
}
|
|
19
|
+
set next(value) {
|
|
20
|
+
this._next = value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.LinkedListNode = LinkedListNode;
|
|
24
|
+
class LinearBase extends iterable_element_base_1.IterableElementBase {
|
|
25
|
+
/**
|
|
26
|
+
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
27
|
+
* if provided.
|
|
28
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
29
|
+
* constructor. It is of type `LinearBaseOptions<E, R>`. The constructor checks if the `options`
|
|
30
|
+
* object is provided and then extracts the `maxLen` property from it. If `maxLen` is a
|
|
31
|
+
*/
|
|
32
|
+
constructor(options) {
|
|
33
|
+
super(options);
|
|
34
|
+
this._maxLen = -1;
|
|
35
|
+
if (options) {
|
|
36
|
+
const { maxLen } = options;
|
|
37
|
+
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
38
|
+
this._maxLen = maxLen;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
get maxLen() {
|
|
42
|
+
return this._maxLen;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(n)
|
|
46
|
+
* Space Complexity: O(1)
|
|
47
|
+
*
|
|
48
|
+
* The function indexOf searches for a specified element starting from a given index in an array-like
|
|
49
|
+
* object and returns the index of the first occurrence, or -1 if not found.
|
|
50
|
+
* @param {E} searchElement - The `searchElement` parameter in the `indexOf` function represents the
|
|
51
|
+
* element that you want to find within the array. The function will search for this element starting
|
|
52
|
+
* from the `fromIndex` (if provided) up to the end of the array. If the `searchElement` is found
|
|
53
|
+
* within the
|
|
54
|
+
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` function represents the
|
|
55
|
+
* index at which to start searching for the `searchElement` within the array. If provided, the
|
|
56
|
+
* search will begin at this index and continue to the end of the array. If `fromIndex` is not
|
|
57
|
+
* specified, the default
|
|
58
|
+
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
59
|
+
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
60
|
+
*/
|
|
61
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
62
|
+
// Boundary checks and adjustments
|
|
63
|
+
if (this.length === 0)
|
|
64
|
+
return -1;
|
|
65
|
+
if (fromIndex < 0)
|
|
66
|
+
fromIndex = this.length + fromIndex;
|
|
67
|
+
if (fromIndex < 0)
|
|
68
|
+
fromIndex = 0;
|
|
69
|
+
// Iterating from the specified index to the end
|
|
70
|
+
for (let i = fromIndex; i < this.length; i++) {
|
|
71
|
+
const element = this.at(i);
|
|
72
|
+
if (element === searchElement)
|
|
73
|
+
return i;
|
|
74
|
+
}
|
|
75
|
+
return -1; // Not found
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(n)
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*
|
|
81
|
+
* The function `lastIndexOf` in TypeScript returns the index of the last occurrence of a specified
|
|
82
|
+
* element in an array.
|
|
83
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find the
|
|
84
|
+
* last index of within the array. The `lastIndexOf` method will search the array starting from the
|
|
85
|
+
* `fromIndex` (or the end of the array if not specified) and return the index of the last occurrence
|
|
86
|
+
* of the
|
|
87
|
+
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
88
|
+
* index at which to start searching for the `searchElement` in the array. By default, it starts
|
|
89
|
+
* searching from the last element of the array (`this.length - 1`). If a specific `fromIndex` is
|
|
90
|
+
* provided
|
|
91
|
+
* @returns The last index of the `searchElement` in the array is being returned. If the
|
|
92
|
+
* `searchElement` is not found in the array, -1 is returned.
|
|
93
|
+
*/
|
|
94
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
95
|
+
if (this.length === 0)
|
|
96
|
+
return -1;
|
|
97
|
+
if (fromIndex >= this.length)
|
|
98
|
+
fromIndex = this.length - 1;
|
|
99
|
+
if (fromIndex < 0)
|
|
100
|
+
fromIndex = this.length + fromIndex;
|
|
101
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
102
|
+
const element = this.at(i);
|
|
103
|
+
if (element === searchElement)
|
|
104
|
+
return i;
|
|
105
|
+
}
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Time Complexity: O(n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
*
|
|
112
|
+
* The `findIndex` function iterates over an array and returns the index of the first element that
|
|
113
|
+
* satisfies the provided predicate function.
|
|
114
|
+
* @param predicate - The `predicate` parameter in the `findIndex` function is a callback function
|
|
115
|
+
* that takes three arguments: `item`, `index`, and the array `this`. It should return a boolean
|
|
116
|
+
* value indicating whether the current element satisfies the condition being checked for.
|
|
117
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `findIndex` function is an optional
|
|
118
|
+
* parameter that specifies the value to use as `this` when executing the `predicate` function. If
|
|
119
|
+
* provided, the `predicate` function will be called with `thisArg` as its `this` value. If `
|
|
120
|
+
* @returns The `findIndex` method is returning the index of the first element in the array that
|
|
121
|
+
* satisfies the provided predicate function. If no such element is found, it returns -1.
|
|
122
|
+
*/
|
|
123
|
+
findIndex(predicate, thisArg) {
|
|
124
|
+
for (let i = 0; i < this.length; i++) {
|
|
125
|
+
const item = this.at(i);
|
|
126
|
+
if (item !== undefined && predicate.call(thisArg, item, i, this))
|
|
127
|
+
return i;
|
|
128
|
+
}
|
|
129
|
+
return -1;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity: O(n + m)
|
|
133
|
+
* Space Complexity: O(n + m)
|
|
134
|
+
*
|
|
135
|
+
* The `concat` function in TypeScript concatenates multiple items into a new list, handling both
|
|
136
|
+
* individual elements and instances of `LinearBase`.
|
|
137
|
+
* @param {(E | this)[]} items - The `concat` method takes in an array of items, where
|
|
138
|
+
* each item can be either of type `E` or an instance of `LinearBase<E, R>`.
|
|
139
|
+
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
140
|
+
* items passed as arguments concatenated to it.
|
|
141
|
+
*/
|
|
142
|
+
concat(...items) {
|
|
143
|
+
const newList = this.clone();
|
|
144
|
+
for (const item of items) {
|
|
145
|
+
if (item instanceof LinearBase) {
|
|
146
|
+
newList.pushMany(item);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
newList.push(item);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return newList;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Time Complexity: O(n log n)
|
|
156
|
+
* Space Complexity: O(n)
|
|
157
|
+
*
|
|
158
|
+
* The `sort` function in TypeScript sorts the elements of a collection using a specified comparison
|
|
159
|
+
* function.
|
|
160
|
+
* @param [compareFn] - The `compareFn` parameter is a function that defines the sort order. It takes
|
|
161
|
+
* two elements `a` and `b` as input and returns a number indicating their relative order. If the
|
|
162
|
+
* returned value is negative, `a` comes before `b`. If the returned value is positive, `
|
|
163
|
+
* @returns The `sort` method is returning the instance of the object on which it is called (this),
|
|
164
|
+
* after sorting the elements based on the provided comparison function (compareFn).
|
|
165
|
+
*/
|
|
166
|
+
sort(compareFn) {
|
|
167
|
+
const arr = this.toArray();
|
|
168
|
+
arr.sort(compareFn);
|
|
169
|
+
this.clear();
|
|
170
|
+
for (const item of arr)
|
|
171
|
+
this.push(item);
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Time Complexity: O(n + m)
|
|
176
|
+
* Space Complexity: O(m)
|
|
177
|
+
*
|
|
178
|
+
* The `splice` function in TypeScript removes elements from an array and optionally inserts new
|
|
179
|
+
* elements at the specified index.
|
|
180
|
+
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
181
|
+
* to start modifying the array. If `start` is a negative number, it will count from the end of the
|
|
182
|
+
* array.
|
|
183
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
184
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
185
|
+
* `deleteCount` is not provided or is 0, no elements are removed, and only new elements are inserted
|
|
186
|
+
* at the `start`
|
|
187
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
188
|
+
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
189
|
+
* and you can pass multiple elements separated by commas. The `splice` method will insert these
|
|
190
|
+
* items into the array at the
|
|
191
|
+
* @returns The `splice` method returns a list of elements that were removed from the original list
|
|
192
|
+
* during the operation.
|
|
193
|
+
*/
|
|
194
|
+
splice(start, deleteCount = 0, ...items) {
|
|
195
|
+
const removedList = this._createInstance();
|
|
196
|
+
// Handling negative indexes and bounds
|
|
197
|
+
start = start < 0 ? this.length + start : start;
|
|
198
|
+
start = Math.max(0, Math.min(start, this.length));
|
|
199
|
+
deleteCount = Math.max(0, Math.min(deleteCount, this.length - start));
|
|
200
|
+
// Delete elements
|
|
201
|
+
for (let i = 0; i < deleteCount; i++) {
|
|
202
|
+
const removed = this.deleteAt(start); // Always delete the start position
|
|
203
|
+
if (removed !== undefined) {
|
|
204
|
+
removedList.push(removed); // Add removed elements to the returned list
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Insert new element
|
|
208
|
+
for (let i = 0; i < items.length; i++) {
|
|
209
|
+
this.addAt(start + i, items[i]); // Insert new elements one by one at the current position
|
|
210
|
+
}
|
|
211
|
+
return removedList; // Returns a list of removed elements
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Time Complexity: O(n)
|
|
215
|
+
* Space Complexity: O(1)
|
|
216
|
+
*
|
|
217
|
+
* The `join` function in TypeScript returns a string by joining the elements of an array with a
|
|
218
|
+
* specified separator.
|
|
219
|
+
* @param {string} [separator=,] - The `separator` parameter is a string that specifies the character
|
|
220
|
+
* or characters that will be used to separate each element when joining them into a single string.
|
|
221
|
+
* By default, the separator is set to a comma (`,`), but you can provide a different separator if
|
|
222
|
+
* needed.
|
|
223
|
+
* @returns The `join` method is being returned, which takes an optional `separator` parameter
|
|
224
|
+
* (defaulting to a comma) and returns a string created by joining all elements of the array after
|
|
225
|
+
* converting it to an array.
|
|
226
|
+
*/
|
|
227
|
+
join(separator = ',') {
|
|
228
|
+
return this.toArray().join(separator);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Time Complexity: O(n)
|
|
232
|
+
* Space Complexity: O(n)
|
|
233
|
+
*
|
|
234
|
+
* The function `toReversedArray` takes an array and returns a new array with its elements in reverse
|
|
235
|
+
* order.
|
|
236
|
+
* @returns The `toReversedArray()` function returns an array of elements of type `E` in reverse
|
|
237
|
+
* order.
|
|
238
|
+
*/
|
|
239
|
+
toReversedArray() {
|
|
240
|
+
const array = [];
|
|
241
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
242
|
+
array.push(this.at(i));
|
|
243
|
+
}
|
|
244
|
+
return array;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Time Complexity: O(n)
|
|
248
|
+
* Space Complexity: O(1)
|
|
249
|
+
*
|
|
250
|
+
* The `reduceRight` function in TypeScript iterates over an array from right to left and applies a
|
|
251
|
+
* callback function to each element, accumulating a single result.
|
|
252
|
+
* @param callbackfn - The `callbackfn` parameter in the `reduceRight` method is a function that will
|
|
253
|
+
* be called on each element in the array from right to left. It takes four arguments:
|
|
254
|
+
* @param {U} [initialValue] - The `initialValue` parameter in the `reduceRight` method is an
|
|
255
|
+
* optional parameter that specifies the initial value of the accumulator. If provided, the
|
|
256
|
+
* `accumulator` will start with this initial value before iterating over the elements of the array.
|
|
257
|
+
* If `initialValue` is not provided, the accumulator will
|
|
258
|
+
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
259
|
+
* callback function to each element in the array from right to left.
|
|
260
|
+
*/
|
|
261
|
+
reduceRight(callbackfn, initialValue) {
|
|
262
|
+
let accumulator = initialValue !== null && initialValue !== void 0 ? initialValue : 0;
|
|
263
|
+
for (let i = this.length - 1; i >= 0; i--) {
|
|
264
|
+
accumulator = callbackfn(accumulator, this.at(i), i, this);
|
|
265
|
+
}
|
|
266
|
+
return accumulator;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Time Complexity: O(m)
|
|
270
|
+
* Space Complexity: O(m)
|
|
271
|
+
*
|
|
272
|
+
* The `slice` function in TypeScript creates a new instance by extracting a portion of elements from
|
|
273
|
+
* the original instance based on the specified start and end indices.
|
|
274
|
+
* @param {number} [start=0] - The `start` parameter in the `slice` method represents the index at
|
|
275
|
+
* which to begin extracting elements from an array-like object. If no `start` parameter is provided,
|
|
276
|
+
* the default value is 0, meaning the extraction will start from the beginning of the array.
|
|
277
|
+
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
278
|
+
* end the slicing. By default, if no `end` parameter is provided, it will slice until the end of the
|
|
279
|
+
* array (i.e., `this.length`).
|
|
280
|
+
* @returns The `slice` method is returning a new instance of the object with elements sliced from
|
|
281
|
+
* the specified start index (default is 0) to the specified end index (default is the length of the
|
|
282
|
+
* object).
|
|
283
|
+
*/
|
|
284
|
+
slice(start = 0, end = this.length) {
|
|
285
|
+
start = start < 0 ? this.length + start : start;
|
|
286
|
+
end = end < 0 ? this.length + end : end;
|
|
287
|
+
const newList = this._createInstance();
|
|
288
|
+
for (let i = start; i < end; i++) {
|
|
289
|
+
newList.push(this.at(i));
|
|
290
|
+
}
|
|
291
|
+
return newList;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Time Complexity: O(n)
|
|
295
|
+
* Space Complexity: O(1)
|
|
296
|
+
*
|
|
297
|
+
* The `fill` function in TypeScript fills a specified range in an array-like object with a given
|
|
298
|
+
* value.
|
|
299
|
+
* @param {E} value - The `value` parameter in the `fill` method represents the element that will be
|
|
300
|
+
* used to fill the specified range in the array.
|
|
301
|
+
* @param [start=0] - The `start` parameter specifies the index at which to start filling the array
|
|
302
|
+
* with the specified value. If not provided, it defaults to 0, indicating the beginning of the
|
|
303
|
+
* array.
|
|
304
|
+
* @param end - The `end` parameter in the `fill` function represents the index at which the filling
|
|
305
|
+
* of values should stop. It specifies the end of the range within the array where the `value` should
|
|
306
|
+
* be filled.
|
|
307
|
+
* @returns The `fill` method is returning the modified object (`this`) after filling the specified
|
|
308
|
+
* range with the provided value.
|
|
309
|
+
*/
|
|
310
|
+
fill(value, start = 0, end = this.length) {
|
|
311
|
+
// Handling negative indexes
|
|
312
|
+
start = start < 0 ? this.length + start : start;
|
|
313
|
+
end = end < 0 ? this.length + end : end;
|
|
314
|
+
// Boundary processing
|
|
315
|
+
if (start < 0)
|
|
316
|
+
start = 0;
|
|
317
|
+
if (end > this.length)
|
|
318
|
+
end = this.length;
|
|
319
|
+
if (start >= end)
|
|
320
|
+
return this;
|
|
321
|
+
// Iterate through the specified range and fill in the values
|
|
322
|
+
for (let i = start; i < end; i++) {
|
|
323
|
+
this.setAt(i, value);
|
|
324
|
+
}
|
|
325
|
+
return this;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
exports.LinearBase = LinearBase;
|
|
329
|
+
class LinearLinkedBase extends LinearBase {
|
|
330
|
+
/**
|
|
331
|
+
* The constructor initializes the LinearBase class with optional options, setting the maximum length
|
|
332
|
+
* if provided and valid.
|
|
333
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
334
|
+
* constructor. It is of type `LinearBaseOptions<E, R>`. This object may contain properties such as
|
|
335
|
+
* `maxLen`, which is a number representing the maximum length. If `maxLen` is a positive integer,
|
|
336
|
+
*/
|
|
337
|
+
constructor(options) {
|
|
338
|
+
super(options);
|
|
339
|
+
if (options) {
|
|
340
|
+
const { maxLen } = options;
|
|
341
|
+
if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
|
|
342
|
+
this._maxLen = maxLen;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Time Complexity: O(n)
|
|
347
|
+
* Space Complexity: O(1)
|
|
348
|
+
*
|
|
349
|
+
* The function overrides the indexOf method to improve performance by searching for an element in a
|
|
350
|
+
* custom array implementation starting from a specified index.
|
|
351
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you are searching for
|
|
352
|
+
* within the array. The `indexOf` method will return the index of the first occurrence of this
|
|
353
|
+
* element within the array.
|
|
354
|
+
* @param {number} [fromIndex=0] - The `fromIndex` parameter in the `indexOf` method specifies the
|
|
355
|
+
* index in the array at which to start the search for the `searchElement`. If provided, the search
|
|
356
|
+
* will begin at the specified index and continue to the end of the array. If not provided, the
|
|
357
|
+
* search will start at index
|
|
358
|
+
* @returns The `indexOf` method is returning the index of the `searchElement` if it is found in the
|
|
359
|
+
* array starting from the `fromIndex`. If the `searchElement` is not found, it returns -1.
|
|
360
|
+
*/
|
|
361
|
+
indexOf(searchElement, fromIndex = 0) {
|
|
362
|
+
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
363
|
+
const iterator = this._getIterator();
|
|
364
|
+
let current = iterator.next();
|
|
365
|
+
let index = 0;
|
|
366
|
+
while (index < fromIndex) {
|
|
367
|
+
current = iterator.next();
|
|
368
|
+
index++;
|
|
369
|
+
}
|
|
370
|
+
while (!current.done) {
|
|
371
|
+
if (current.value === searchElement)
|
|
372
|
+
return index;
|
|
373
|
+
current = iterator.next();
|
|
374
|
+
index++;
|
|
375
|
+
}
|
|
376
|
+
return -1;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Time Complexity: O(n)
|
|
380
|
+
* Space Complexity: O(1)
|
|
381
|
+
*
|
|
382
|
+
* The function overrides the lastIndexOf method in TypeScript to improve performance by searching
|
|
383
|
+
* for an element in reverse order starting from a specified index.
|
|
384
|
+
* @param {E} searchElement - The `searchElement` parameter is the element that you want to find
|
|
385
|
+
* within the array. The `lastIndexOf` method searches the array for this element starting from the
|
|
386
|
+
* end of the array (or from the specified `fromIndex` if provided) and returns the index of the last
|
|
387
|
+
* occurrence of the element
|
|
388
|
+
* @param {number} fromIndex - The `fromIndex` parameter in the `lastIndexOf` method specifies the
|
|
389
|
+
* index at which to start searching for the `searchElement` in the array. If provided, the search
|
|
390
|
+
* will begin at this index and move towards the beginning of the array. If not provided, the search
|
|
391
|
+
* will start at the
|
|
392
|
+
* @returns The `lastIndexOf` method is being overridden to search for the `searchElement` starting
|
|
393
|
+
* from the specified `fromIndex` (defaulting to the end of the array). It iterates over the array in
|
|
394
|
+
* reverse order using a custom iterator `_getReverseIterator` and returns the index of the last
|
|
395
|
+
* occurrence of the `searchElement` if found, or -1 if not found.
|
|
396
|
+
*/
|
|
397
|
+
lastIndexOf(searchElement, fromIndex = this.length - 1) {
|
|
398
|
+
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
399
|
+
const iterator = this._getReverseIterator();
|
|
400
|
+
let current = iterator.next();
|
|
401
|
+
let index = this.length - 1;
|
|
402
|
+
while (index > fromIndex) {
|
|
403
|
+
current = iterator.next();
|
|
404
|
+
index--;
|
|
405
|
+
}
|
|
406
|
+
while (!current.done) {
|
|
407
|
+
if (current.value === searchElement)
|
|
408
|
+
return index;
|
|
409
|
+
current = iterator.next();
|
|
410
|
+
index--;
|
|
411
|
+
}
|
|
412
|
+
return -1;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Time Complexity: O(n + m)
|
|
416
|
+
* Space Complexity: O(n + m)
|
|
417
|
+
*
|
|
418
|
+
* The `concat` function in TypeScript overrides the default behavior to concatenate items into a new
|
|
419
|
+
* list, handling both individual elements and instances of `LinearBase`.
|
|
420
|
+
* @param {(E | LinearBase<E, R>)[]} items - The `concat` method you provided takes in a variable
|
|
421
|
+
* number of arguments of type `E` or `LinearBase<E, R>`. The method concatenates these items to the
|
|
422
|
+
* current list and returns a new list with the concatenated items.
|
|
423
|
+
* @returns The `concat` method is returning a new instance of the class that it belongs to, with the
|
|
424
|
+
* items passed as arguments concatenated to it.
|
|
425
|
+
*/
|
|
426
|
+
concat(...items) {
|
|
427
|
+
const newList = this.clone();
|
|
428
|
+
for (const item of items) {
|
|
429
|
+
if (item instanceof LinearBase) {
|
|
430
|
+
newList.pushMany(item);
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
newList.push(item);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return newList;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Time Complexity: O(m)
|
|
440
|
+
* Space Complexity: O(m)
|
|
441
|
+
*
|
|
442
|
+
* The `slice` method is overridden to improve performance by creating a new instance and iterating
|
|
443
|
+
* through the array to extract a subset based on the specified start and end indices.
|
|
444
|
+
* @param {number} [start=0] - The `start` parameter in the `slice` method specifies the index at
|
|
445
|
+
* which to begin extracting elements from the array. If no `start` parameter is provided, the
|
|
446
|
+
* default value is 0, indicating that extraction should start from the beginning of the array.
|
|
447
|
+
* @param {number} end - The `end` parameter in the `slice` method represents the index at which to
|
|
448
|
+
* end the slicing of the array. If not provided, it defaults to the length of the array.
|
|
449
|
+
* @returns The `slice` method is returning a new instance of the array implementation with elements
|
|
450
|
+
* sliced from the original array based on the `start` and `end` parameters.
|
|
451
|
+
*/
|
|
452
|
+
slice(start = 0, end = this.length) {
|
|
453
|
+
// In order to improve performance, it is best to override this method in the subclass of the array implementation
|
|
454
|
+
start = start < 0 ? this.length + start : start;
|
|
455
|
+
end = end < 0 ? this.length + end : end;
|
|
456
|
+
const newList = this._createInstance();
|
|
457
|
+
const iterator = this._getIterator();
|
|
458
|
+
let current = iterator.next();
|
|
459
|
+
let c = 0;
|
|
460
|
+
while (c < start) {
|
|
461
|
+
current = iterator.next();
|
|
462
|
+
c++;
|
|
463
|
+
}
|
|
464
|
+
for (let i = start; i < end; i++) {
|
|
465
|
+
newList.push(current.value);
|
|
466
|
+
current = iterator.next();
|
|
467
|
+
}
|
|
468
|
+
return newList;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Time Complexity: O(n + m)
|
|
472
|
+
* Space Complexity: O(m)
|
|
473
|
+
*
|
|
474
|
+
* The function overrides the splice method to handle deletion and insertion of elements in a data
|
|
475
|
+
* structure while returning the removed elements.
|
|
476
|
+
* @param {number} start - The `start` parameter in the `splice` method indicates the index at which
|
|
477
|
+
* to start modifying the array.
|
|
478
|
+
* @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
|
|
479
|
+
* number of elements to remove from the array starting at the specified `start` index. If
|
|
480
|
+
* `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
|
|
481
|
+
* elements can still be inserted at
|
|
482
|
+
* @param {E[]} items - The `items` parameter in the `splice` method represents the elements that
|
|
483
|
+
* will be inserted into the array at the specified `start` index. These elements can be of any type
|
|
484
|
+
* and there can be multiple elements passed as arguments to be inserted into the array.
|
|
485
|
+
* @returns The `splice` method is returning a new instance of the data structure that was modified
|
|
486
|
+
* by removing elements specified by the `start` and `deleteCount` parameters, and inserting new
|
|
487
|
+
* elements provided in the `items` array.
|
|
488
|
+
*/
|
|
489
|
+
splice(start, deleteCount = 0, ...items) {
|
|
490
|
+
const removedList = this._createInstance(); // Used to store deleted elements
|
|
491
|
+
// Handling negative indexes
|
|
492
|
+
start = start < 0 ? this.length + start : start;
|
|
493
|
+
start = Math.max(0, Math.min(start, this.length)); // Correct start range
|
|
494
|
+
deleteCount = Math.max(0, deleteCount); // Make sure deleteCount is non-negative
|
|
495
|
+
let currentIndex = 0;
|
|
496
|
+
let currentNode = undefined;
|
|
497
|
+
let previousNode = undefined;
|
|
498
|
+
// Find the starting point using an iterator
|
|
499
|
+
const iterator = this._getNodeIterator();
|
|
500
|
+
for (const node of iterator) {
|
|
501
|
+
if (currentIndex === start) {
|
|
502
|
+
currentNode = node; // Find the starting node
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
previousNode = node; // Update the previous node
|
|
506
|
+
currentIndex++;
|
|
507
|
+
}
|
|
508
|
+
// Delete nodes
|
|
509
|
+
for (let i = 0; i < deleteCount && currentNode; i++) {
|
|
510
|
+
removedList.push(currentNode.value); // Store the deleted value in removedList
|
|
511
|
+
const nextNode = currentNode.next; // Save next node
|
|
512
|
+
this.delete(currentNode); // Delete current node
|
|
513
|
+
currentNode = nextNode;
|
|
514
|
+
}
|
|
515
|
+
// Insert new value
|
|
516
|
+
for (let i = 0; i < items.length; i++) {
|
|
517
|
+
if (previousNode) {
|
|
518
|
+
this.addAfter(previousNode, items[i]); // Insert after previousNode
|
|
519
|
+
previousNode = previousNode.next; // Move to newly inserted node
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
this.addAt(0, items[i]); // Insert at the head of the linked list
|
|
523
|
+
previousNode = this._getNodeIterator().next().value; // Update the head node to be the first inserted node
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return removedList;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Time Complexity: O(n)
|
|
530
|
+
* Space Complexity: O(1)
|
|
531
|
+
*
|
|
532
|
+
* The function `reduceRight` iterates over an array in reverse order and applies a callback function
|
|
533
|
+
* to each element, accumulating a single result.
|
|
534
|
+
* @param callbackfn - The `callbackfn` parameter is a function that will be called on each element
|
|
535
|
+
* of the array from right to left. It takes four arguments:
|
|
536
|
+
* @param {U} [initialValue] - The `initialValue` parameter is an optional value that is used as the
|
|
537
|
+
* initial accumulator value in the reduce operation. If provided, the reduce operation starts with
|
|
538
|
+
* this initial value and iterates over the elements of the array, applying the callback function to
|
|
539
|
+
* each element and the current accumulator value. If `initial
|
|
540
|
+
* @returns The `reduceRight` method is returning the final accumulated value after applying the
|
|
541
|
+
* callback function to each element in the array from right to left.
|
|
542
|
+
*/
|
|
543
|
+
reduceRight(callbackfn, initialValue) {
|
|
544
|
+
let accumulator = initialValue !== null && initialValue !== void 0 ? initialValue : 0;
|
|
545
|
+
let index = this.length - 1;
|
|
546
|
+
for (const item of this._getReverseIterator()) {
|
|
547
|
+
accumulator = callbackfn(accumulator, item, index--, this);
|
|
548
|
+
}
|
|
549
|
+
return accumulator;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
exports.LinearLinkedBase = LinearLinkedBase;
|
|
@@ -60,15 +60,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV
|
|
|
60
60
|
* Time Complexity: O(1)
|
|
61
61
|
* Space Complexity: O(1)
|
|
62
62
|
*
|
|
63
|
-
* The
|
|
64
|
-
* specified key and
|
|
65
|
-
* @param {K} key - The `key` parameter
|
|
66
|
-
*
|
|
67
|
-
* @
|
|
68
|
-
*
|
|
63
|
+
* The `createNode` function in TypeScript overrides the default implementation to create a new
|
|
64
|
+
* AVLTreeMultiMapNode with a specified key and value array.
|
|
65
|
+
* @param {K} key - The `key` parameter represents the key of the node being created in the
|
|
66
|
+
* AVLTreeMultiMap.
|
|
67
|
+
* @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
|
|
68
|
+
* values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
|
|
69
|
+
* calling the method, an empty array `[]` is used as the default value.
|
|
70
|
+
* @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
|
|
71
|
+
* AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
|
|
72
|
+
* array is used.
|
|
69
73
|
*/
|
|
70
|
-
createNode(key: K): AVLTreeMultiMapNode<K, V>;
|
|
71
|
-
add(
|
|
74
|
+
createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
|
|
75
|
+
add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
72
76
|
add(key: K, value: V): boolean;
|
|
73
77
|
/**
|
|
74
78
|
* Time Complexity: O(log n)
|