min-heap-typed 1.50.0 → 1.50.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +114 -9
  2. package/dist/data-structures/base/iterable-base.js +143 -7
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
  4. package/dist/data-structures/binary-tree/avl-tree.js +68 -71
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
  6. package/dist/data-structures/binary-tree/binary-tree.js +484 -376
  7. package/dist/data-structures/binary-tree/bst.d.ts +54 -74
  8. package/dist/data-structures/binary-tree/bst.js +30 -71
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
  10. package/dist/data-structures/binary-tree/rb-tree.js +84 -89
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
  12. package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
  13. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  14. package/dist/data-structures/graph/abstract-graph.js +3 -0
  15. package/dist/data-structures/graph/directed-graph.d.ts +14 -0
  16. package/dist/data-structures/graph/directed-graph.js +26 -0
  17. package/dist/data-structures/graph/map-graph.d.ts +8 -0
  18. package/dist/data-structures/graph/map-graph.js +14 -0
  19. package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
  20. package/dist/data-structures/graph/undirected-graph.js +25 -0
  21. package/dist/data-structures/hash/hash-map.d.ts +121 -15
  22. package/dist/data-structures/hash/hash-map.js +160 -25
  23. package/dist/data-structures/heap/heap.d.ts +66 -6
  24. package/dist/data-structures/heap/heap.js +66 -6
  25. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
  26. package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
  27. package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
  28. package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
  29. package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  30. package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
  31. package/dist/data-structures/matrix/matrix.d.ts +35 -4
  32. package/dist/data-structures/matrix/matrix.js +50 -11
  33. package/dist/data-structures/queue/deque.d.ts +49 -19
  34. package/dist/data-structures/queue/deque.js +101 -47
  35. package/dist/data-structures/queue/queue.d.ts +39 -5
  36. package/dist/data-structures/queue/queue.js +47 -5
  37. package/dist/data-structures/stack/stack.d.ts +16 -0
  38. package/dist/data-structures/stack/stack.js +22 -0
  39. package/dist/data-structures/trie/trie.d.ts +38 -1
  40. package/dist/data-structures/trie/trie.js +41 -0
  41. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  42. package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
  43. package/dist/types/utils/utils.d.ts +1 -0
  44. package/package.json +2 -2
  45. package/src/data-structures/base/iterable-base.ts +172 -19
  46. package/src/data-structures/binary-tree/avl-tree.ts +97 -97
  47. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  48. package/src/data-structures/binary-tree/bst.ts +89 -131
  49. package/src/data-structures/binary-tree/rb-tree.ts +127 -155
  50. package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
  51. package/src/data-structures/graph/abstract-graph.ts +4 -0
  52. package/src/data-structures/graph/directed-graph.ts +30 -0
  53. package/src/data-structures/graph/map-graph.ts +15 -0
  54. package/src/data-structures/graph/undirected-graph.ts +28 -0
  55. package/src/data-structures/hash/hash-map.ts +175 -34
  56. package/src/data-structures/heap/heap.ts +66 -6
  57. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
  58. package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
  59. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  60. package/src/data-structures/matrix/matrix.ts +52 -12
  61. package/src/data-structures/queue/deque.ts +108 -49
  62. package/src/data-structures/queue/queue.ts +51 -5
  63. package/src/data-structures/stack/stack.ts +24 -0
  64. package/src/data-structures/trie/trie.ts +45 -1
  65. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  66. package/src/types/data-structures/hash/hash-map.ts +4 -3
  67. package/src/types/utils/utils.ts +2 -0
@@ -20,6 +20,14 @@ export class SkipListNode<K, V> {
20
20
  }
21
21
 
22
22
  export class SkipList<K, V> {
23
+ /**
24
+ * The constructor function initializes a SkipLinkedList object with optional options and elements.
25
+ * @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
26
+ * is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
27
+ * provided, the SkipLinkedList will be empty.
28
+ * @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
29
+ * contain two properties:
30
+ */
23
31
  constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
24
32
  if (options) {
25
33
  const { maxLevel, probability } = options;
@@ -34,36 +42,52 @@ export class SkipList<K, V> {
34
42
 
35
43
  protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
36
44
 
45
+ /**
46
+ * The function returns the head node of a SkipList.
47
+ * @returns The method is returning a SkipListNode object with generic key type K and value type V.
48
+ */
37
49
  get head(): SkipListNode<K, V> {
38
50
  return this._head;
39
51
  }
40
52
 
41
53
  protected _level: number = 0;
42
54
 
55
+ /**
56
+ * The function returns the value of the private variable _level.
57
+ * @returns The level property of the object.
58
+ */
43
59
  get level(): number {
44
60
  return this._level;
45
61
  }
46
62
 
47
63
  protected _maxLevel: number = 16;
48
64
 
65
+ /**
66
+ * The function returns the maximum level.
67
+ * @returns The value of the variable `_maxLevel` is being returned.
68
+ */
49
69
  get maxLevel(): number {
50
70
  return this._maxLevel;
51
71
  }
52
72
 
53
73
  protected _probability: number = 0.5;
54
74
 
75
+ /**
76
+ * The function returns the probability value.
77
+ * @returns The probability value stored in the private variable `_probability` is being returned.
78
+ */
55
79
  get probability(): number {
56
80
  return this._probability;
57
81
  }
58
82
 
59
83
  /**
60
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
61
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
84
+ * Time Complexity: O(log n)
85
+ * Space Complexity: O(1)
62
86
  */
63
87
 
64
88
  /**
65
- * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
66
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
89
+ * Time Complexity: O(1)
90
+ * Space Complexity: O(1)
67
91
  *
68
92
  * Get the value of the first element (the smallest element) in the Skip List.
69
93
  * @returns The value of the first element, or undefined if the Skip List is empty.
@@ -74,13 +98,13 @@ export class SkipList<K, V> {
74
98
  }
75
99
 
76
100
  /**
77
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
78
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
101
+ * Time Complexity: O(log n)
102
+ * Space Complexity: O(1)
79
103
  */
80
104
 
81
105
  /**
82
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
83
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
106
+ * Time Complexity: O(log n)
107
+ * Space Complexity: O(1)
84
108
  *
85
109
  * Get the value of the last element (the largest element) in the Skip List.
86
110
  * @returns The value of the last element, or undefined if the Skip List is empty.
@@ -96,13 +120,13 @@ export class SkipList<K, V> {
96
120
  }
97
121
 
98
122
  /**
99
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
100
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
123
+ * Time Complexity: O(log n)
124
+ * Space Complexity: O(1)
101
125
  */
102
126
 
103
127
  /**
104
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
105
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
128
+ * Time Complexity: O(log n)
129
+ * Space Complexity: O(1)
106
130
  *
107
131
  * The add function adds a new node with a given key and value to a Skip List data structure.
108
132
  * @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
@@ -132,13 +156,13 @@ export class SkipList<K, V> {
132
156
  }
133
157
 
134
158
  /**
135
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
136
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
159
+ * Time Complexity: O(log n)
160
+ * Space Complexity: O(1)
137
161
  */
138
162
 
139
163
  /**
140
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
141
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
164
+ * Time Complexity: O(log n)
165
+ * Space Complexity: O(1)
142
166
  *
143
167
  * The function `get` retrieves the value associated with a given key from a skip list data structure.
144
168
  * @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
@@ -163,27 +187,28 @@ export class SkipList<K, V> {
163
187
  }
164
188
 
165
189
  /**
166
- * Time Complexity: O(1) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
167
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
190
+ * Time Complexity: O(log n)
191
+ * Space Complexity: O(1)
168
192
  */
169
193
 
170
194
  /**
171
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
172
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
195
+ * The function checks if a key exists in a data structure.
196
+ * @param {K} key - The parameter "key" is of type K, which represents the type of the key being
197
+ * checked.
198
+ * @returns a boolean value.
173
199
  */
174
-
175
200
  has(key: K): boolean {
176
201
  return this.get(key) !== undefined;
177
202
  }
178
203
 
179
204
  /**
180
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
181
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
205
+ * Time Complexity: O(log n)
206
+ * Space Complexity: O(1)
182
207
  */
183
208
 
184
209
  /**
185
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
186
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
210
+ * Time Complexity: O(log n)
211
+ * Space Complexity: O(1)
187
212
  *
188
213
  * The `delete` function removes a node with a specific key from a Skip List data structure.
189
214
  * @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
@@ -220,13 +245,13 @@ export class SkipList<K, V> {
220
245
  }
221
246
 
222
247
  /**
223
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
224
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
248
+ * Time Complexity: O(log n)
249
+ * Space Complexity: O(1)
225
250
  */
226
251
 
227
252
  /**
228
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
229
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
253
+ * Time Complexity: O(log n)
254
+ * Space Complexity: O(1)
230
255
  *
231
256
  * Get the value of the first element in the Skip List that is greater than the given key.
232
257
  * @param key - the given key.
@@ -244,13 +269,13 @@ export class SkipList<K, V> {
244
269
  }
245
270
 
246
271
  /**
247
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
248
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
272
+ * Time Complexity: O(log n)
273
+ * Space Complexity: O(1)
249
274
  */
250
275
 
251
276
  /**
252
- * Time Complexity: O(log n) - where n is the number of elements in the SkipList, as it traverses the levels of the SkipList.
253
- * Space Complexity: O(1) - constant space, as it uses a fixed amount of space regardless of the size of the SkipList.
277
+ * Time Complexity: O(log n)
278
+ * Space Complexity: O(1)
254
279
  *
255
280
  * Get the value of the last element in the Skip List that is less than the given key.
256
281
  * @param key - the given key.
@@ -273,13 +298,14 @@ export class SkipList<K, V> {
273
298
  }
274
299
 
275
300
  /**
276
- * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
277
- * Space Complexity: O(1) - constant space.
301
+ * Time Complexity: O(maxLevel)
302
+ * Space Complexity: O(1)
303
+ * where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
278
304
  */
279
305
 
280
306
  /**
281
- * Time Complexity: O(maxLevel) - where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
282
- * Space Complexity: O(1) - constant space.
307
+ * Time Complexity: O(maxLevel)
308
+ * Space Complexity: O(1)
283
309
  *
284
310
  * The function "_randomLevel" generates a random level based on a given probability and maximum level.
285
311
  * @returns the level, which is a number.
@@ -42,30 +42,54 @@ export class Matrix {
42
42
 
43
43
  protected _rows: number = 0;
44
44
 
45
+ /**
46
+ * The function returns the number of rows.
47
+ * @returns The number of rows.
48
+ */
45
49
  get rows(): number {
46
50
  return this._rows;
47
51
  }
48
52
 
49
53
  protected _cols: number = 0;
50
54
 
55
+ /**
56
+ * The function returns the value of the private variable _cols.
57
+ * @returns The number of columns.
58
+ */
51
59
  get cols(): number {
52
60
  return this._cols;
53
61
  }
54
62
 
55
63
  protected _data: number[][];
56
64
 
65
+ /**
66
+ * The function returns a two-dimensional array of numbers.
67
+ * @returns The data property, which is a two-dimensional array of numbers.
68
+ */
57
69
  get data(): number[][] {
58
70
  return this._data;
59
71
  }
60
72
 
73
+ /**
74
+ * The above function returns the value of the _addFn property.
75
+ * @returns The value of the property `_addFn` is being returned.
76
+ */
61
77
  get addFn() {
62
78
  return this._addFn;
63
79
  }
64
80
 
81
+ /**
82
+ * The function returns the value of the _subtractFn property.
83
+ * @returns The `_subtractFn` property is being returned.
84
+ */
65
85
  get subtractFn() {
66
86
  return this._subtractFn;
67
87
  }
68
88
 
89
+ /**
90
+ * The function returns the value of the _multiplyFn property.
91
+ * @returns The `_multiplyFn` property is being returned.
92
+ */
69
93
  get multiplyFn() {
70
94
  return this._multiplyFn;
71
95
  }
@@ -373,6 +397,34 @@ export class Matrix {
373
397
  });
374
398
  }
375
399
 
400
+ /**
401
+ * The function checks if a given row and column index is valid within a specified range.
402
+ * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
403
+ * matrix. It is a number that indicates the specific row in the matrix.
404
+ * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
405
+ * or grid. It is used to check if the given column index is valid within the bounds of the grid.
406
+ * @returns A boolean value is being returned.
407
+ */
408
+ isValidIndex(row: number, col: number): boolean {
409
+ return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
410
+ }
411
+
412
+ /**
413
+ * The `clone` function returns a new instance of the Matrix class with the same data and properties
414
+ * as the original instance.
415
+ * @returns The `clone()` method is returning a new instance of the `Matrix` class with the same data
416
+ * and properties as the current instance.
417
+ */
418
+ clone(): Matrix {
419
+ return new Matrix(this.data, {
420
+ rows: this.rows,
421
+ cols: this.cols,
422
+ addFn: this.addFn,
423
+ subtractFn: this.subtractFn,
424
+ multiplyFn: this.multiplyFn
425
+ });
426
+ }
427
+
376
428
  protected _addFn(a: number | undefined, b: number): number | undefined {
377
429
  if (a === undefined) return b;
378
430
  return a + b;
@@ -386,18 +438,6 @@ export class Matrix {
386
438
  return a * b;
387
439
  }
388
440
 
389
- /**
390
- * The function checks if a given row and column index is valid within a specified range.
391
- * @param {number} row - The `row` parameter represents the row index of a two-dimensional array or
392
- * matrix. It is a number that indicates the specific row in the matrix.
393
- * @param {number} col - The "col" parameter represents the column index in a two-dimensional array
394
- * or grid. It is used to check if the given column index is valid within the bounds of the grid.
395
- * @returns A boolean value is being returned.
396
- */
397
- protected isValidIndex(row: number, col: number): boolean {
398
- return row >= 0 && row < this.rows && col >= 0 && col < this.cols;
399
- }
400
-
401
441
  /**
402
442
  * The function `_swapRows` swaps the positions of two rows in an array.
403
443
  * @param {number} row1 - The `row1` parameter is the index of the first row that you want to swap.
@@ -24,6 +24,17 @@ export class Deque<E> extends IterableElementBase<E> {
24
24
  protected _bucketCount = 0;
25
25
  protected readonly _bucketSize: number = 1 << 12;
26
26
 
27
+ /**
28
+ * The constructor initializes a Deque object with an optional iterable of elements and options.
29
+ * @param elements - An iterable object (such as an array or a Set) that contains the initial
30
+ * elements to be added to the deque. It can also be an object with a `length` or `size` property
31
+ * that represents the number of elements in the iterable object. If no elements are provided, an
32
+ * empty deque
33
+ * @param {DequeOptions} [options] - The `options` parameter is an optional object that can contain
34
+ * configuration options for the deque. In this code, it is used to set the `bucketSize` option,
35
+ * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
36
+ * or is not a number
37
+ */
27
38
  constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) {
28
39
  super();
29
40
 
@@ -54,14 +65,32 @@ export class Deque<E> extends IterableElementBase<E> {
54
65
  }
55
66
  }
56
67
 
68
+ /**
69
+ * The bucketSize function returns the size of the bucket.
70
+ *
71
+ * @return The size of the bucket
72
+ */
73
+ get bucketSize() {
74
+ return this._bucketSize;
75
+ }
76
+
57
77
  protected _buckets: E[][] = [];
58
78
 
79
+ /**
80
+ * The buckets function returns the buckets property of the object.
81
+ *
82
+ * @return The buckets property
83
+ */
59
84
  get buckets() {
60
85
  return this._buckets;
61
86
  }
62
87
 
63
88
  protected _size = 0;
64
89
 
90
+ /**
91
+ * The size function returns the number of items in the stack.
92
+ * @return The number of values in the set
93
+ */
65
94
  get size() {
66
95
  return this._size;
67
96
  }
@@ -76,6 +105,10 @@ export class Deque<E> extends IterableElementBase<E> {
76
105
  return this._buckets[this._bucketFirst][this._firstInBucket];
77
106
  }
78
107
 
108
+ /**
109
+ * The last function returns the last element in the queue.
110
+ * @return The last element in the array
111
+ */
79
112
  get last(): E | undefined {
80
113
  if (this.size === 0) return;
81
114
  return this._buckets[this._bucketLast][this._lastInBucket];
@@ -235,7 +268,7 @@ export class Deque<E> extends IterableElementBase<E> {
235
268
  * begin(): Generator<E> {
236
269
  let index = 0;
237
270
  while (index < this.size) {
238
- yield this.getAt(index);
271
+ yield this.at(index);
239
272
  index++;
240
273
  }
241
274
  }
@@ -247,7 +280,7 @@ export class Deque<E> extends IterableElementBase<E> {
247
280
  * reverseBegin(): Generator<E> {
248
281
  let index = this.size - 1;
249
282
  while (index >= 0) {
250
- yield this.getAt(index);
283
+ yield this.at(index);
251
284
  index--;
252
285
  }
253
286
  }
@@ -261,13 +294,13 @@ export class Deque<E> extends IterableElementBase<E> {
261
294
  * Time Complexity: O(1)
262
295
  * Space Complexity: O(1)
263
296
  *
264
- * The `getAt` function retrieves an element at a specified position in an array-like data structure.
297
+ * The `at` function retrieves an element at a specified position in an array-like data structure.
265
298
  * @param {number} pos - The `pos` parameter represents the position of the element that you want to
266
299
  * retrieve from the data structure. It is of type `number` and should be a valid index within the
267
300
  * range of the data structure.
268
301
  * @returns The element at the specified position in the data structure is being returned.
269
302
  */
270
- getAt(pos: number): E {
303
+ at(pos: number): E {
271
304
  rangeCheck(pos, 0, this.size - 1);
272
305
  const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
273
306
  return this._buckets[bucketIndex][indexInBucket]!;
@@ -325,9 +358,9 @@ export class Deque<E> extends IterableElementBase<E> {
325
358
  } else {
326
359
  const arr: E[] = [];
327
360
  for (let i = pos; i < this.size; ++i) {
328
- arr.push(this.getAt(i));
361
+ arr.push(this.at(i));
329
362
  }
330
- this.cut(pos - 1);
363
+ this.cut(pos - 1, true);
331
364
  for (let i = 0; i < num; ++i) this.push(element);
332
365
  for (let i = 0; i < arr.length; ++i) this.push(arr[i]);
333
366
  }
@@ -347,18 +380,51 @@ export class Deque<E> extends IterableElementBase<E> {
347
380
  * updated size.
348
381
  * @param {number} pos - The `pos` parameter represents the position at which the string should be
349
382
  * cut. It is a number that indicates the index of the character where the cut should be made.
383
+ * @param {boolean} isCutSelf - If true, the original deque will not be cut, and return a new deque
350
384
  * @returns The method is returning the updated size of the data structure.
351
385
  */
352
- cut(pos: number): number {
353
- if (pos < 0) {
354
- this.clear();
355
- return 0;
386
+ cut(pos: number, isCutSelf = false): Deque<E> {
387
+ if (isCutSelf) {
388
+ if (pos < 0) {
389
+ this.clear();
390
+ return this;
391
+ }
392
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
393
+ this._bucketLast = bucketIndex;
394
+ this._lastInBucket = indexInBucket;
395
+ this._size = pos + 1;
396
+ return this;
397
+ } else {
398
+ const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
399
+
400
+ for (let i = 0; i <= pos; i++) {
401
+ newDeque.push(this.at(i));
402
+ }
403
+
404
+ return newDeque;
405
+ }
406
+ }
407
+
408
+ cutRest(pos: number, isCutSelf = false): Deque<E> {
409
+ if (isCutSelf) {
410
+ if (pos < 0) {
411
+ this.clear();
412
+ return this;
413
+ }
414
+ const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
415
+ this._bucketFirst = bucketIndex;
416
+ this._firstInBucket = indexInBucket;
417
+ this._size = this._size - pos;
418
+ return this;
419
+ } else {
420
+ const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
421
+
422
+ for (let i = pos; i < this.size; i++) {
423
+ newDeque.push(this.at(i));
424
+ }
425
+
426
+ return newDeque;
356
427
  }
357
- const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos);
358
- this._bucketLast = bucketIndex;
359
- this._lastInBucket = indexInBucket;
360
- this._size = pos + 1;
361
- return this.size;
362
428
  }
363
429
 
364
430
  /**
@@ -416,14 +482,14 @@ export class Deque<E> extends IterableElementBase<E> {
416
482
  let i = 0;
417
483
  let index = 0;
418
484
  while (i < size) {
419
- const oldElement = this.getAt(i);
485
+ const oldElement = this.at(i);
420
486
  if (oldElement !== element) {
421
487
  this.setAt(index, oldElement!);
422
488
  index += 1;
423
489
  }
424
490
  i += 1;
425
491
  }
426
- this.cut(index - 1);
492
+ this.cut(index - 1, true);
427
493
  return true;
428
494
  }
429
495
 
@@ -471,15 +537,15 @@ export class Deque<E> extends IterableElementBase<E> {
471
537
  return this;
472
538
  }
473
539
  let index = 1;
474
- let prev = this.getAt(0);
540
+ let prev = this.at(0);
475
541
  for (let i = 1; i < this.size; ++i) {
476
- const cur = this.getAt(i);
542
+ const cur = this.at(i);
477
543
  if (cur !== prev) {
478
544
  prev = cur;
479
545
  this.setAt(index++, cur);
480
546
  }
481
547
  }
482
- this.cut(index - 1);
548
+ this.cut(index - 1, true);
483
549
  return this;
484
550
  }
485
551
 
@@ -501,7 +567,7 @@ export class Deque<E> extends IterableElementBase<E> {
501
567
  sort(comparator?: (x: E, y: E) => number): this {
502
568
  const arr: E[] = [];
503
569
  for (let i = 0; i < this.size; ++i) {
504
- arr.push(this.getAt(i));
570
+ arr.push(this.at(i));
505
571
  }
506
572
  arr.sort(comparator);
507
573
  for (let i = 0; i < this.size; ++i) {
@@ -545,32 +611,6 @@ export class Deque<E> extends IterableElementBase<E> {
545
611
  this._buckets = newBuckets;
546
612
  }
547
613
 
548
- /**
549
- * Time Complexity: O(n)
550
- * Space Complexity: O(1)
551
- */
552
-
553
- /**
554
- * Time Complexity: O(n)
555
- * Space Complexity: O(1)
556
- *
557
- * The `find` function iterates over the elements in a deque and returns the first element for which
558
- * the callback function returns true, or undefined if no such element is found.
559
- * @param callback - A function that takes three parameters: element, index, and deque. It should
560
- * return a boolean value indicating whether the element satisfies a certain condition.
561
- * @returns The method `find` returns the first element in the deque that satisfies the condition
562
- * specified by the callback function. If no element satisfies the condition, it returns `undefined`.
563
- */
564
- find(callback: (element: E, index: number, deque: Deque<E>) => boolean): E | undefined {
565
- for (let i = 0; i < this.size; ++i) {
566
- const element = this.getAt(i);
567
- if (callback(element, i, this)) {
568
- return element;
569
- }
570
- }
571
- return;
572
- }
573
-
574
614
  /**
575
615
  * Time Complexity: O(n)
576
616
  * Space Complexity: O(1)
@@ -589,7 +629,7 @@ export class Deque<E> extends IterableElementBase<E> {
589
629
  */
590
630
  indexOf(element: E): number {
591
631
  for (let i = 0; i < this.size; ++i) {
592
- if (this.getAt(i) === element) {
632
+ if (this.at(i) === element) {
593
633
  return i;
594
634
  }
595
635
  }
@@ -616,6 +656,25 @@ export class Deque<E> extends IterableElementBase<E> {
616
656
  * Time Complexity: O(n)
617
657
  * Space Complexity: O(n)
618
658
  */
659
+
660
+ /**
661
+ * Time Complexity: O(n)
662
+ * Space Complexity: O(n)
663
+ *
664
+ * The `clone()` function returns a new instance of the `Deque` class with the same elements and
665
+ * bucket size as the original instance.
666
+ * @returns The `clone()` method is returning a new instance of the `Deque` class with the same
667
+ * elements as the original deque (`this`) and the same bucket size.
668
+ */
669
+ clone(): Deque<E> {
670
+ return new Deque<E>([...this], { bucketSize: this.bucketSize });
671
+ }
672
+
673
+ /**
674
+ * Time Complexity: O(n)
675
+ * Space Complexity: O(n)
676
+ */
677
+
619
678
  /**
620
679
  * Time Complexity: O(n)
621
680
  * Space Complexity: O(n)
@@ -737,7 +796,7 @@ export class Deque<E> extends IterableElementBase<E> {
737
796
  */
738
797
  protected* _getIterator(): IterableIterator<E> {
739
798
  for (let i = 0; i < this.size; ++i) {
740
- yield this.getAt(i);
799
+ yield this.at(i);
741
800
  }
742
801
  }
743
802