stack-typed 2.0.4 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  2. package/dist/data-structures/base/iterable-element-base.js +149 -107
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  4. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  5. package/dist/data-structures/base/linear-base.d.ts +250 -192
  6. package/dist/data-structures/base/linear-base.js +137 -274
  7. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  8. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  9. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  11. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  12. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  14. package/dist/data-structures/binary-tree/binary-tree.js +612 -879
  15. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  16. package/dist/data-structures/binary-tree/bst.js +505 -481
  17. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  18. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  19. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  20. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  21. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  22. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  23. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  24. package/dist/data-structures/graph/abstract-graph.js +267 -237
  25. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  26. package/dist/data-structures/graph/directed-graph.js +146 -233
  27. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  28. package/dist/data-structures/graph/map-graph.js +56 -59
  29. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  30. package/dist/data-structures/graph/undirected-graph.js +129 -149
  31. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  32. package/dist/data-structures/hash/hash-map.js +270 -457
  33. package/dist/data-structures/heap/heap.d.ts +214 -289
  34. package/dist/data-structures/heap/heap.js +340 -349
  35. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  36. package/dist/data-structures/heap/max-heap.js +11 -66
  37. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  38. package/dist/data-structures/heap/min-heap.js +11 -66
  39. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  40. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  41. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  42. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  43. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  44. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  45. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  46. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  47. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  48. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  49. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  50. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  51. package/dist/data-structures/queue/deque.d.ts +227 -254
  52. package/dist/data-structures/queue/deque.js +309 -348
  53. package/dist/data-structures/queue/queue.d.ts +180 -201
  54. package/dist/data-structures/queue/queue.js +265 -248
  55. package/dist/data-structures/stack/stack.d.ts +124 -102
  56. package/dist/data-structures/stack/stack.js +181 -125
  57. package/dist/data-structures/trie/trie.d.ts +164 -165
  58. package/dist/data-structures/trie/trie.js +189 -172
  59. package/dist/interfaces/binary-tree.d.ts +56 -6
  60. package/dist/interfaces/graph.d.ts +16 -0
  61. package/dist/types/data-structures/base/base.d.ts +1 -1
  62. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  63. package/dist/types/utils/utils.d.ts +6 -6
  64. package/dist/utils/utils.d.ts +110 -49
  65. package/dist/utils/utils.js +148 -73
  66. package/package.json +2 -2
  67. package/src/data-structures/base/iterable-element-base.ts +238 -115
  68. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  69. package/src/data-structures/base/linear-base.ts +271 -277
  70. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  71. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  72. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  73. package/src/data-structures/binary-tree/binary-tree.ts +681 -905
  74. package/src/data-structures/binary-tree/bst.ts +568 -570
  75. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  76. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  77. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  78. package/src/data-structures/graph/abstract-graph.ts +339 -264
  79. package/src/data-structures/graph/directed-graph.ts +146 -236
  80. package/src/data-structures/graph/map-graph.ts +63 -60
  81. package/src/data-structures/graph/undirected-graph.ts +129 -152
  82. package/src/data-structures/hash/hash-map.ts +274 -496
  83. package/src/data-structures/heap/heap.ts +389 -402
  84. package/src/data-structures/heap/max-heap.ts +12 -76
  85. package/src/data-structures/heap/min-heap.ts +13 -76
  86. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  87. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  88. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  89. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  90. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  91. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  92. package/src/data-structures/queue/deque.ts +381 -357
  93. package/src/data-structures/queue/queue.ts +310 -264
  94. package/src/data-structures/stack/stack.ts +217 -131
  95. package/src/data-structures/trie/trie.ts +240 -175
  96. package/src/interfaces/binary-tree.ts +240 -6
  97. package/src/interfaces/graph.ts +37 -0
  98. package/src/types/data-structures/base/base.ts +5 -5
  99. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  100. package/src/types/utils/utils.ts +9 -5
  101. package/src/utils/utils.ts +152 -86
@@ -5,23 +5,55 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
8
+ import type { DoublyLinkedListOptions, ElementCallback, LinearBaseOptions } from '../../types';
9
9
  import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
10
+ /**
11
+ * Node of a doubly linked list; stores value and prev/next links.
12
+ * @remarks Time O(1), Space O(1)
13
+ * @template E
14
+ */
10
15
  export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
11
16
  /**
12
- * The constructor function initializes the value, next, and previous properties of an object.
13
- * @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
14
- * is defined as a generic type "E".
17
+ * Create a node.
18
+ * @remarks Time O(1), Space O(1)
19
+ * @param value - Element value to store.
20
+ * @returns New node instance.
15
21
  */
16
22
  constructor(value: E);
17
23
  protected _next: DoublyLinkedListNode<E> | undefined;
24
+ /**
25
+ * Get the next node link.
26
+ * @remarks Time O(1), Space O(1)
27
+ * @returns Next node or undefined.
28
+ */
18
29
  get next(): DoublyLinkedListNode<E> | undefined;
30
+ /**
31
+ * Set the next node link.
32
+ * @remarks Time O(1), Space O(1)
33
+ * @param value - Next node or undefined.
34
+ * @returns void
35
+ */
19
36
  set next(value: DoublyLinkedListNode<E> | undefined);
20
37
  protected _prev: DoublyLinkedListNode<E> | undefined;
38
+ /**
39
+ * Get the previous node link.
40
+ * @remarks Time O(1), Space O(1)
41
+ * @returns Previous node or undefined.
42
+ */
21
43
  get prev(): DoublyLinkedListNode<E> | undefined;
44
+ /**
45
+ * Set the previous node link.
46
+ * @remarks Time O(1), Space O(1)
47
+ * @param value - Previous node or undefined.
48
+ * @returns void
49
+ */
22
50
  set prev(value: DoublyLinkedListNode<E> | undefined);
23
51
  }
24
52
  /**
53
+ * Doubly linked list with O(1) push/pop/unshift/shift and linear scans.
54
+ * @remarks Time O(1), Space O(1)
55
+ * @template E
56
+ * @template R
25
57
  * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
26
58
  * 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
27
59
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
@@ -196,6 +228,16 @@ export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
196
228
  * this.map = new Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>();
197
229
  * }
198
230
  *
231
+ * // Get the current cache length
232
+ * get length(): number {
233
+ * return this.list.length;
234
+ * }
235
+ *
236
+ * // Check if it is empty
237
+ * get isEmpty(): boolean {
238
+ * return this.list.isEmpty();
239
+ * }
240
+ *
199
241
  * // Get cached value
200
242
  * get(key: K): V | undefined {
201
243
  * const node = this.map.get(key);
@@ -241,12 +283,6 @@ export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
241
283
  * }
242
284
  * }
243
285
  *
244
- * // Move the node to the head of the linked list
245
- * private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
246
- * this.list.delete(node);
247
- * this.list.unshift(node.value);
248
- * }
249
- *
250
286
  * // Delete specific key
251
287
  * delete(key: K): boolean {
252
288
  * const node = this.map.get(key);
@@ -266,14 +302,10 @@ export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
266
302
  * this.map.clear();
267
303
  * }
268
304
  *
269
- * // Get the current cache length
270
- * get length(): number {
271
- * return this.list.length;
272
- * }
273
- *
274
- * // Check if it is empty
275
- * get isEmpty(): boolean {
276
- * return this.list.isEmpty();
305
+ * // Move the node to the head of the linked list
306
+ * private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
307
+ * this.list.delete(node);
308
+ * this.list.unshift(node.value);
277
309
  * }
278
310
  * }
279
311
  *
@@ -455,431 +487,283 @@ export declare class DoublyLinkedListNode<E = any> extends LinkedListNode<E> {
455
487
  * console.log(scheduler.listProcesses()); // []
456
488
  */
457
489
  export declare class DoublyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, DoublyLinkedListNode<E>> {
490
+ protected _equals: (a: E, b: E) => boolean;
458
491
  /**
459
- * This TypeScript constructor initializes a DoublyLinkedList with optional elements and options.
460
- * @param {Iterable<E> | Iterable<R>} elements - The `elements` parameter in the constructor is an
461
- * iterable collection of elements of type `E` or `R`. It is used to initialize the DoublyLinkedList
462
- * with the elements provided in the iterable. If no elements are provided, the default value is an
463
- * empty iterable.
464
- * @param [options] - The `options` parameter in the constructor is of type
465
- * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
466
- * configuration options to customize the behavior of the DoublyLinkedList.
492
+ * Create a DoublyLinkedList and optionally bulk-insert elements.
493
+ * @remarks Time O(N), Space O(N)
494
+ * @param [elements] - Iterable of elements or nodes (or raw records if toElementFn is provided).
495
+ * @param [options] - Options such as maxLen and toElementFn.
496
+ * @returns New DoublyLinkedList instance.
467
497
  */
468
498
  constructor(elements?: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>, options?: DoublyLinkedListOptions<E, R>);
469
499
  protected _head: DoublyLinkedListNode<E> | undefined;
500
+ /**
501
+ * Get the head node.
502
+ * @remarks Time O(1), Space O(1)
503
+ * @returns Head node or undefined.
504
+ */
470
505
  get head(): DoublyLinkedListNode<E> | undefined;
471
506
  protected _tail: DoublyLinkedListNode<E> | undefined;
507
+ /**
508
+ * Get the tail node.
509
+ * @remarks Time O(1), Space O(1)
510
+ * @returns Tail node or undefined.
511
+ */
472
512
  get tail(): DoublyLinkedListNode<E> | undefined;
473
513
  protected _length: number;
514
+ /**
515
+ * Get the number of elements.
516
+ * @remarks Time O(1), Space O(1)
517
+ * @returns Current length.
518
+ */
474
519
  get length(): number;
475
520
  /**
476
- * Time Complexity: O(1)
477
- * Space Complexity: O(1)
478
- *
479
- * The `get first` function returns the first node in a doubly linked list, or undefined if the list is empty.
480
- * @returns The method `get first()` returns the first node of the doubly linked list, or `undefined` if the list is empty.
521
+ * Get the first element value.
522
+ * @remarks Time O(1), Space O(1)
523
+ * @returns First element or undefined.
481
524
  */
482
525
  get first(): E | undefined;
483
526
  /**
484
- * Time Complexity: O(1)
485
- * Space Complexity: O(1)
486
- *
487
- * The `get last` function returns the last node in a doubly linked list, or undefined if the list is empty.
488
- * @returns The method `get last()` returns the last node of the doubly linked list, or `undefined` if the list is empty.
527
+ * Get the last element value.
528
+ * @remarks Time O(1), Space O(1)
529
+ * @returns Last element or undefined.
489
530
  */
490
531
  get last(): E | undefined;
491
532
  /**
492
- * Time Complexity: O(n)
493
- * Space Complexity: O(n)
494
- *
495
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
496
- * given array.
497
- * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
498
- * @returns The `fromArray` function returns a DoublyLinkedList object.
499
- */
500
- static fromArray<E>(data: E[]): DoublyLinkedList<E, any>;
501
- /**
502
- * Time Complexity: O(1)
503
- * Space Complexity: O(1)
504
- *
505
- * The function `isNode` in TypeScript checks if a given input is an instance of
506
- * `DoublyLinkedListNode`.
507
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
508
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `isNode` function can
509
- * be one of the following types:
510
- * @returns The `isNode` function is checking if the `elementNodeOrPredicate` parameter is an
511
- * instance of `DoublyLinkedListNode<E>`. If it is, the function returns `true`, indicating that the
512
- * parameter is a `DoublyLinkedListNode<E>`. If it is not an instance of `DoublyLinkedListNode<E>`,
513
- * the function returns `false`.
533
+ * Create a new list from an array of elements.
534
+ * @remarks Time O(N), Space O(N)
535
+ * @template E
536
+ * @template R
537
+ * @param this - The constructor (subclass) to instantiate.
538
+ * @param data - Array of elements to insert.
539
+ * @returns A new list populated with the array's elements.
540
+ */
541
+ static fromArray<E, R = any>(this: new (elements?: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>, options?: DoublyLinkedListOptions<E, R>) => any, data: E[]): any;
542
+ /**
543
+ * Type guard: check whether the input is a DoublyLinkedListNode.
544
+ * @remarks Time O(1), Space O(1)
545
+ * @param elementNodeOrPredicate - Element, node, or predicate.
546
+ * @returns True if the value is a DoublyLinkedListNode.
514
547
  */
515
548
  isNode(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is DoublyLinkedListNode<E>;
516
549
  /**
517
- * Time Complexity: O(1)
518
- * Space Complexity: O(1)
519
- *
520
- * The `push` function adds a new element or node to the end of a doubly linked list.
521
- * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the `push`
522
- * method can accept either an element of type `E` or a `DoublyLinkedListNode<E>` object.
523
- * @returns The `push` method is returning a boolean value, specifically `true`.
550
+ * Append an element/node to the tail.
551
+ * @remarks Time O(1), Space O(1)
552
+ * @param elementOrNode - Element or node to append.
553
+ * @returns True when appended.
524
554
  */
525
555
  push(elementOrNode: E | DoublyLinkedListNode<E>): boolean;
526
556
  /**
527
- * Time Complexity: O(1)
528
- * Space Complexity: O(1)
529
- *
530
- * The `pop()` function removes and returns the value of the last element in a linked list.
531
- * @returns The method is returning the value of the removed node.
557
+ * Remove and return the tail element.
558
+ * @remarks Time O(1), Space O(1)
559
+ * @returns Removed element or undefined.
532
560
  */
533
561
  pop(): E | undefined;
534
562
  /**
535
- * Time Complexity: O(1)
536
- * Space Complexity: O(1)
537
- *
538
- * The `shift()` function removes and returns the value of the first element in a doubly linked list.
539
- * @returns The value of the removed node.
563
+ * Remove and return the head element.
564
+ * @remarks Time O(1), Space O(1)
565
+ * @returns Removed element or undefined.
540
566
  */
541
567
  shift(): E | undefined;
542
568
  /**
543
- * Time Complexity: O(1)
544
- * Space Complexity: O(1)
545
- *
546
- * The unshift function adds a new element or node to the beginning of a doubly linked list.
547
- * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter in the
548
- * `unshift` method can be either an element of type `E` or a `DoublyLinkedListNode` containing an
549
- * element of type `E`.
550
- * @returns The `unshift` method is returning a boolean value, specifically `true`.
569
+ * Prepend an element/node to the head.
570
+ * @remarks Time O(1), Space O(1)
571
+ * @param elementOrNode - Element or node to prepend.
572
+ * @returns True when prepended.
551
573
  */
552
574
  unshift(elementOrNode: E | DoublyLinkedListNode<E>): boolean;
553
575
  /**
554
- * Time Complexity: O(k)
555
- * Space Complexity: O(k)
556
- *
557
- * The function `pushMany` iterates over elements and pushes them into a data structure, applying a
558
- * transformation function if provided.
559
- * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
560
- * parameter in the `pushMany` function can accept an iterable containing elements of type `E`, `R`,
561
- * or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and pushes
562
- * it onto the linked list. If a transformation function `to
563
- * @returns The `pushMany` function is returning an array of boolean values (`ans`) which indicate
564
- * the success or failure of pushing each element into the data structure.
576
+ * Append a sequence of elements/nodes.
577
+ * @remarks Time O(N), Space O(1)
578
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
579
+ * @returns Array of per-element success flags.
565
580
  */
566
581
  pushMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[];
567
582
  /**
568
- * Time Complexity: O(k)
569
- * Space Complexity: O(k)
570
- *
571
- * The function `unshiftMany` iterates through a collection of elements and adds them to the
572
- * beginning of a Doubly Linked List, returning an array of boolean values indicating the success of
573
- * each insertion.
574
- * @param {Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>} elements - The `elements`
575
- * parameter in the `unshiftMany` function can accept an iterable containing elements of type `E`,
576
- * `R`, or `DoublyLinkedListNode<E>`. The function iterates over each element in the iterable and
577
- * performs an `unshift` operation on the doubly linked list
578
- * @returns The `unshiftMany` function returns an array of boolean values indicating the success of
579
- * each unshift operation performed on the elements passed as input.
583
+ * Prepend a sequence of elements/nodes.
584
+ * @remarks Time O(N), Space O(1)
585
+ * @param elements - Iterable of elements or nodes (or raw records if toElementFn is provided).
586
+ * @returns Array of per-element success flags.
580
587
  */
581
588
  unshiftMany(elements: Iterable<E> | Iterable<R> | Iterable<DoublyLinkedListNode<E>>): boolean[];
582
589
  /**
583
- * Time Complexity: O(n)
584
- * Space Complexity: O(1)
585
- *
586
- * The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds.
587
- * @param {number} index - The index parameter is a number that represents the position of the element we want to
588
- * retrieve from the list.
589
- * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
590
- * or the linked list is empty, it will return undefined.
590
+ * Get the element at a given index.
591
+ * @remarks Time O(N), Space O(1)
592
+ * @param index - Zero-based index.
593
+ * @returns Element or undefined.
591
594
  */
592
595
  at(index: number): E | undefined;
593
596
  /**
594
- * Time Complexity: O(n)
595
- * Space Complexity: O(1)
596
- *
597
- * The function `getNodeAt` returns the node at a given index in a doubly linked list, or undefined if the index is out of
598
- * range.
599
- * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
600
- * retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
601
- * @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<E>` object if the index is within the
602
- * valid range of the linked list, otherwise it returns `undefined`.
597
+ * Get the node reference at a given index.
598
+ * @remarks Time O(N), Space O(1)
599
+ * @param index - Zero-based index.
600
+ * @returns Node or undefined.
603
601
  */
604
602
  getNodeAt(index: number): DoublyLinkedListNode<E> | undefined;
605
603
  /**
606
- * Time Complexity: O(n)
607
- * Space Complexity: O(1)
608
- *
609
- * This TypeScript function searches for a node in a doubly linked list based on a given element node
610
- * or predicate.
611
- * @param {| E
612
- * | DoublyLinkedListNode<E>
613
- * | ((node: DoublyLinkedListNode<E>) => boolean)
614
- * | undefined} elementNodeOrPredicate - The `getNode` method you provided is used to find a
615
- * node in a doubly linked list based on a given element, node, or predicate function. The
616
- * `elementNodeOrPredicate` parameter can be one of the following:
617
- * @returns The `getNode` method returns a `DoublyLinkedListNode<E>` or `undefined` based on the
618
- * input `elementNodeOrPredicate`. If the input is `undefined`, the method returns `undefined`.
619
- * Otherwise, it iterates through the linked list starting from the head node and applies the
620
- * provided predicate function to each node. If a node satisfies the predicate, that node is
621
- * returned. If
604
+ * Find a node by value, reference, or predicate.
605
+ * @remarks Time O(N), Space O(1)
606
+ * @param [elementNodeOrPredicate] - Element, node, or predicate to match.
607
+ * @returns Matching node or undefined.
622
608
  */
623
609
  getNode(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean) | undefined): DoublyLinkedListNode<E> | undefined;
624
610
  /**
625
- * Time Complexity: O(n)
626
- * Space Complexity: O(1)
627
- *
628
- * The `addAt` function inserts a new element or node at a specified index in a doubly linked list.
629
- * @param {number} index - The `index` parameter in the `addAt` method represents the position at
630
- * which you want to add a new element or node in the doubly linked list. It indicates the location
631
- * where the new element or node should be inserted.
632
- * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
633
- * `addAt` method can be either a value of type `E` or a `DoublyLinkedListNode<E>` object.
634
- * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was
635
- * successfully added at the specified index, and `false` if the index is out of bounds (less than 0
636
- * or greater than the length of the list).
611
+ * Insert a new element/node at an index, shifting following nodes.
612
+ * @remarks Time O(N), Space O(1)
613
+ * @param index - Zero-based index.
614
+ * @param newElementOrNode - Element or node to insert.
615
+ * @returns True if inserted.
637
616
  */
638
617
  addAt(index: number, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
639
618
  /**
640
- * Time Complexity: O(1) or O(n)
641
- * Space Complexity: O(1)
642
- *
643
- * The `addBefore` function in TypeScript adds a new element or node before an existing element or
644
- * node in a doubly linked list.
645
- * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - The `existingElementOrNode` parameter
646
- * in the `addBefore` method can be either an element of type `E` or a `DoublyLinkedListNode<E>`.
647
- * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter
648
- * represents the element or node that you want to add before the `existingElementOrNode` in a doubly
649
- * linked list.
650
- * @returns The `addBefore` method returns a boolean value - `true` if the new element or node was
651
- * successfully added before the existing element or node, and `false` if the existing element or
652
- * node was not found.
619
+ * Insert a new element/node before an existing one.
620
+ * @remarks Time O(N), Space O(1)
621
+ * @param existingElementOrNode - Existing element or node.
622
+ * @param newElementOrNode - Element or node to insert.
623
+ * @returns True if inserted.
653
624
  */
654
625
  addBefore(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
655
626
  /**
656
- * Time Complexity: O(1) or O(n)
657
- * Space Complexity: O(1)
658
- *
659
- * The `addAfter` function in TypeScript adds a new element or node after an existing element or node
660
- * in a doubly linked list.
661
- * @param {E | DoublyLinkedListNode<E>} existingElementOrNode - existingElementOrNode represents the
662
- * element or node in the doubly linked list after which you want to add a new element or node.
663
- * @param {E | DoublyLinkedListNode<E>} newElementOrNode - The `newElementOrNode` parameter in the
664
- * `addAfter` method represents the element or node that you want to add after the existing element
665
- * or node in a doubly linked list. This parameter can be either an element value or a
666
- * `DoublyLinkedListNode` object that you want to insert
667
- * @returns The `addAfter` method returns a boolean value - `true` if the new element or node was
668
- * successfully added after the existing element or node, and `false` if the existing element or node
669
- * was not found in the linked list.
627
+ * Insert a new element/node after an existing one.
628
+ * @remarks Time O(N), Space O(1)
629
+ * @param existingElementOrNode - Existing element or node.
630
+ * @param newElementOrNode - Element or node to insert.
631
+ * @returns True if inserted.
670
632
  */
671
633
  addAfter(existingElementOrNode: E | DoublyLinkedListNode<E>, newElementOrNode: E | DoublyLinkedListNode<E>): boolean;
672
634
  /**
673
- * Time Complexity: O(n)
674
- * Space Complexity: O(1)
675
- *
676
- * The function `setAt` updates the value at a specified index in a data structure if the index
677
- * exists.
678
- * @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
679
- * data structure where you want to set a new value.
680
- * @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
681
- * want to set at the specified index in the data structure.
682
- * @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
683
- * is successfully updated, and `false` if the index is out of bounds.
635
+ * Set the element value at an index.
636
+ * @remarks Time O(N), Space O(1)
637
+ * @param index - Zero-based index.
638
+ * @param value - New value.
639
+ * @returns True if updated.
684
640
  */
685
641
  setAt(index: number, value: E): boolean;
686
642
  /**
687
- * Time Complexity: O(n)
688
- * Space Complexity: O(1)
689
- *
690
- * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
691
- * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
692
- * data structure. It is of type number.
693
- * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
694
- * bounds.
643
+ * Delete the element at an index.
644
+ * @remarks Time O(N), Space O(1)
645
+ * @param index - Zero-based index.
646
+ * @returns Removed element or undefined.
695
647
  */
696
648
  deleteAt(index: number): E | undefined;
697
649
  /**
698
- * Time Complexity: O(1) or O(n)
699
- * Space Complexity: O(1)
700
- *
701
- * The `delete` function removes a specified element or node from a doubly linked list if it exists.
702
- * @param {E | DoublyLinkedListNode<E> | undefined} elementOrNode - The `elementOrNode` parameter in
703
- * the `delete` method can accept an element of type `E`, a `DoublyLinkedListNode` of type `E`, or it
704
- * can be `undefined`. This parameter is used to identify the node that needs to be deleted from the
705
- * doubly linked list
706
- * @returns The `delete` method returns a boolean value - `true` if the element or node was
707
- * successfully deleted from the doubly linked list, and `false` if the element or node was not found
708
- * in the list.
650
+ * Delete the first match by value/node.
651
+ * @remarks Time O(N), Space O(1)
652
+ * @param [elementOrNode] - Element or node to remove.
653
+ * @returns True if removed.
709
654
  */
710
655
  delete(elementOrNode: E | DoublyLinkedListNode<E> | undefined): boolean;
711
656
  /**
712
- * Time Complexity: O(1)
713
- * Space Complexity: O(1)
714
- *
715
- * The function checks if a variable has a length greater than zero and returns a boolean value.
716
- * @returns A boolean value is being returned.
657
+ * Check whether the list is empty.
658
+ * @remarks Time O(1), Space O(1)
659
+ * @returns True if length is 0.
717
660
  */
718
661
  isEmpty(): boolean;
719
662
  /**
720
- * Time Complexity: O(1)
721
- * Space Complexity: O(1)
722
- *
723
- * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
663
+ * Remove all nodes and reset length.
664
+ * @remarks Time O(N), Space O(1)
665
+ * @returns void
724
666
  */
725
667
  clear(): void;
726
668
  /**
727
- * Time Complexity: O(n)
728
- * Space Complexity: O(1)
729
- *
730
- * This function retrieves an element from a doubly linked list based on a given element
731
- * node or predicate.
732
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
733
- * elementNodeOrPredicate - The `get` method takes in a parameter called `elementNodeOrPredicate`,
734
- * which can be one of the following types:
735
- * @returns The `get` method returns the value of the first node in the doubly linked list that
736
- * satisfies the provided predicate function. If no such node is found, it returns `undefined`.
669
+ * Find the first value matching a predicate scanning forward.
670
+ * @remarks Time O(N), Space O(1)
671
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
672
+ * @returns Matched value or undefined.
737
673
  */
738
674
  search(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
739
675
  /**
740
- * Time Complexity: O(n)
741
- * Space Complexity: O(1)
742
- *
743
- * The `getBackward` function searches for a specific element in a doubly linked list starting from
744
- * the tail and moving backwards.
745
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
746
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `getBackward`
747
- * function can be one of the following types:
748
- * @returns The `getBackward` method returns the value of the element node that matches the provided
749
- * predicate when traversing the doubly linked list backwards. If no matching element is found, it
750
- * returns `undefined`.
676
+ * Find the first value matching a predicate scanning backward.
677
+ * @remarks Time O(N), Space O(1)
678
+ * @param elementNodeOrPredicate - Element, node, or predicate to match.
679
+ * @returns Matched value or undefined.
751
680
  */
752
681
  getBackward(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): E | undefined;
753
682
  /**
754
- * Time Complexity: O(n)
755
- * Space Complexity: O(1)
756
- *
757
- * The `reverse` function reverses the order of the elements in a doubly linked list.
683
+ * Reverse the list in place.
684
+ * @remarks Time O(N), Space O(1)
685
+ * @returns This list.
758
686
  */
759
687
  reverse(): this;
760
688
  /**
761
- * Time Complexity: O(n)
762
- * Space Complexity: O(n)
763
- *
764
- * The `clone` function creates a new instance of the `DoublyLinkedList` class with the same values
765
- * as the original list.
766
- * @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
767
- * is a copy of the original list.
689
+ * Set the equality comparator used to compare values.
690
+ * @remarks Time O(1), Space O(1)
691
+ * @param equals - Equality predicate (a, b) → boolean.
692
+ * @returns This list.
768
693
  */
769
- clone(): this;
694
+ setEquality(equals: (a: E, b: E) => boolean): this;
770
695
  /**
771
- * Time Complexity: O(n)
772
- * Space Complexity: O(n)
773
- *
774
- * The `filter` function creates a new DoublyLinkedList by iterating over the elements of the current
775
- * list and applying a callback function to each element, returning only the elements for which the
776
- * callback function returns true.
777
- * @param callback - The `callback` parameter is a function that will be called for each element in
778
- * the DoublyLinkedList. It takes three arguments: the current element, the index of the current
779
- * element, and the DoublyLinkedList itself. The callback function should return a boolean value
780
- * indicating whether the current element should be included
781
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
782
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
783
- * passed as the `this` value to the `callback` function. If `thisArg` is
784
- * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
785
- * elements that pass the filter condition specified by the `callback` function.
786
- */
787
- filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): DoublyLinkedList<E, R>;
788
- /**
789
- * Time Complexity: O(n)
790
- * Space Complexity: O(n)
791
- *
792
- * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
793
- * of applying the callback to each element in the original list.
794
- * @param callback - The callback parameter is a function that will be called for each element in the
795
- * original DoublyLinkedList. It takes three arguments: current (the current element being
796
- * processed), index (the index of the current element), and this (the original DoublyLinkedList).
797
- * The callback function should return a value of type
798
- * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
799
- * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
800
- * input and returns the converted element. If this parameter is not provided, the raw element will
801
- * be used as is.
802
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
803
- * specify the value of `this` within the callback function. It is used to set the context or scope
804
- * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
805
- * value of
806
- * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
807
- */
808
- map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
809
- /**
810
- * Time Complexity: O(n)
811
- * Space Complexity: O(1)
812
- *
813
- * The function `countOccurrences` iterates through a doubly linked list and counts the occurrences
814
- * of a specified element or nodes that satisfy a given predicate.
815
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementOrNode
816
- * - The `elementOrNode` parameter in the `countOccurrences` method can accept three types of values:
817
- * @returns The `countOccurrences` method returns the number of occurrences of the specified element,
818
- * node, or predicate function in the doubly linked list.
819
- */
820
- countOccurrences(elementOrNode: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): number;
821
- /**
822
- * The function returns an iterator that iterates over the values of a linked list.
696
+ * Deep clone this list (values are copied by reference).
697
+ * @remarks Time O(N), Space O(N)
698
+ * @returns A new list with the same element sequence.
823
699
  */
824
- protected _getIterator(): IterableIterator<E>;
700
+ clone(): this;
825
701
  /**
826
- * The function returns an iterator that iterates over the elements of a data structure in reverse
827
- * order.
702
+ * Filter values into a new list of the same class.
703
+ * @remarks Time O(N), Space O(N)
704
+ * @param callback - Predicate (value, index, list) → boolean to keep value.
705
+ * @param [thisArg] - Value for `this` inside the callback.
706
+ * @returns A new list with kept values.
828
707
  */
829
- protected _getReverseIterator(): IterableIterator<E>;
708
+ filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): this;
830
709
  /**
831
- * The function returns an iterator that iterates over the nodes of a doubly linked list starting
832
- * from the head.
710
+ * Map values into a new list of the same class.
711
+ * @remarks Time O(N), Space O(N)
712
+ * @param callback - Mapping function (value, index, list) → newValue.
713
+ * @param [thisArg] - Value for `this` inside the callback.
714
+ * @returns A new list with mapped values.
833
715
  */
834
- protected _getNodeIterator(): IterableIterator<DoublyLinkedListNode<E>>;
716
+ mapSame(callback: ElementCallback<E, R, E>, thisArg?: any): this;
835
717
  /**
836
- * The function `_isPredicate` checks if the input is a function that takes a `DoublyLinkedListNode`
837
- * as an argument and returns a boolean.
838
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
839
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
840
- * types:
841
- * @returns The _isPredicate method is returning a boolean value indicating whether the
842
- * elementNodeOrPredicate parameter is a function or not. If the elementNodeOrPredicate is a
843
- * function, the method will return true, indicating that it is a predicate function.
718
+ * Map values into a new list (possibly different element type).
719
+ * @remarks Time O(N), Space O(N)
720
+ * @template EM
721
+ * @template RM
722
+ * @param callback - Mapping function (value, index, list) → newElement.
723
+ * @param [options] - Options for the output list (e.g., maxLen, toElementFn).
724
+ * @param [thisArg] - Value for `this` inside the callback.
725
+ * @returns A new DoublyLinkedList with mapped values.
844
726
  */
845
- protected _isPredicate(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): elementNodeOrPredicate is (node: DoublyLinkedListNode<E>) => boolean;
727
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, options?: DoublyLinkedListOptions<EM, RM>, thisArg?: any): DoublyLinkedList<EM, RM>;
846
728
  /**
847
- * The function `_ensureNode` ensures that the input is a valid node in a doubly linked list.
848
- * @param {E | DoublyLinkedListNode<E>} elementOrNode - The `elementOrNode` parameter can be either
849
- * an element of type `E` or a `DoublyLinkedListNode` containing an element of type `E`.
850
- * @returns If the `elementOrNode` parameter is already a `DoublyLinkedListNode`, it will be returned
851
- * as is. Otherwise, a new `DoublyLinkedListNode` instance will be created with the `elementOrNode`
852
- * value and returned.
729
+ * (Protected) Create or return a node for the given input (node or raw element).
730
+ * @remarks Time O(1), Space O(1)
731
+ * @param elementOrNode - Element value or node to normalize.
732
+ * @returns A DoublyLinkedListNode for the provided input.
853
733
  */
854
734
  protected _ensureNode(elementOrNode: E | DoublyLinkedListNode<E>): DoublyLinkedListNode<E>;
855
735
  /**
856
- * The function `_ensurePredicate` in TypeScript ensures that the input is either a node, a predicate
857
- * function, or a value to compare with the node's value.
858
- * @param {E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
859
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter can be one of the following
860
- * types:
861
- * @returns A function is being returned that takes a `DoublyLinkedListNode` as a parameter and
862
- * returns a boolean value based on the conditions specified in the code.
736
+ * (Protected) Normalize input into a predicate over nodes.
737
+ * @remarks Time O(1), Space O(1)
738
+ * @param elementNodeOrPredicate - Element, node, or node predicate.
739
+ * @returns A predicate function taking a node and returning true/false.
863
740
  */
864
741
  protected _ensurePredicate(elementNodeOrPredicate: E | DoublyLinkedListNode<E> | ((node: DoublyLinkedListNode<E>) => boolean)): (node: DoublyLinkedListNode<E>) => boolean;
865
742
  /**
866
- * The function `_createInstance` returns a new instance of `DoublyLinkedList` with the specified
867
- * options.
868
- * @param [options] - The `options` parameter in the `_createInstance` method is of type
869
- * `DoublyLinkedListOptions<E, R>`. It is an optional parameter that allows you to pass additional
870
- * configuration options when creating a new instance of the `DoublyLinkedList` class.
871
- * @returns An instance of the `DoublyLinkedList` class with an empty array and the provided options
872
- * is being returned, cast as the current class type.
743
+ * (Protected) Get the previous node of a given node.
744
+ * @remarks Time O(1), Space O(1)
745
+ * @param node - A node in the list.
746
+ * @returns Previous node or undefined.
873
747
  */
874
- protected _createInstance(options?: DoublyLinkedListOptions<E, R>): this;
748
+ protected _getPrevNode(node: DoublyLinkedListNode<E>): DoublyLinkedListNode<E> | undefined;
875
749
  /**
876
- * The function `_getPrevNode` returns the previous node of a given node in a doubly linked list.
877
- * @param node - The parameter `node` in the `_getPrevNode` method is of type
878
- * `DoublyLinkedListNode<E>`, which represents a node in a doubly linked list containing an element
879
- * of type `E`.
880
- * @returns The `_getPrevNode` method is returning the previous node of the input `node` in a doubly
881
- * linked list. If the input node has a previous node, it will return that node. Otherwise, it will
882
- * return `undefined`.
750
+ * (Protected) Create an empty instance of the same concrete class.
751
+ * @remarks Time O(1), Space O(1)
752
+ * @param [options] - Options forwarded to the constructor.
753
+ * @returns An empty like-kind list instance.
883
754
  */
884
- protected _getPrevNode(node: DoublyLinkedListNode<E>): DoublyLinkedListNode<E> | undefined;
755
+ protected _createInstance(options?: LinearBaseOptions<E, R>): this;
756
+ /**
757
+ * (Protected) Create a like-kind instance and seed it from an iterable.
758
+ * @remarks Time O(N), Space O(N)
759
+ * @template EM
760
+ * @template RM
761
+ * @param [elements] - Iterable used to seed the new list.
762
+ * @param [options] - Options forwarded to the constructor.
763
+ * @returns A like-kind DoublyLinkedList instance.
764
+ */
765
+ protected _createLike<EM = E, RM = R>(elements?: Iterable<EM> | Iterable<RM> | Iterable<DoublyLinkedListNode<EM>>, options?: DoublyLinkedListOptions<EM, RM>): DoublyLinkedList<EM, RM>;
766
+ protected _getIterator(): IterableIterator<E>;
767
+ protected _getReverseIterator(): IterableIterator<E>;
768
+ protected _getNodeIterator(): IterableIterator<DoublyLinkedListNode<E>>;
885
769
  }