doubly-linked-list-typed 1.51.9 → 1.52.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.
Files changed (102) hide show
  1. package/dist/data-structures/base/index.d.ts +2 -1
  2. package/dist/data-structures/base/index.js +2 -1
  3. package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
  4. package/dist/data-structures/base/iterable-element-base.js +225 -0
  5. package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
  6. package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +13 -13
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +13 -13
  10. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +99 -99
  12. package/dist/data-structures/binary-tree/binary-tree.js +54 -52
  13. package/dist/data-structures/binary-tree/bst.d.ts +37 -45
  14. package/dist/data-structures/binary-tree/bst.js +17 -25
  15. package/dist/data-structures/binary-tree/rb-tree.d.ts +10 -10
  16. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  17. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +12 -12
  18. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  19. package/dist/data-structures/graph/directed-graph.js +2 -1
  20. package/dist/data-structures/hash/hash-map.d.ts +2 -2
  21. package/dist/data-structures/heap/heap.d.ts +43 -114
  22. package/dist/data-structures/heap/heap.js +59 -127
  23. package/dist/data-structures/heap/max-heap.d.ts +50 -4
  24. package/dist/data-structures/heap/max-heap.js +76 -10
  25. package/dist/data-structures/heap/min-heap.d.ts +51 -5
  26. package/dist/data-structures/heap/min-heap.js +68 -11
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
  30. package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
  31. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
  33. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
  34. package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
  35. package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
  36. package/dist/data-structures/priority-queue/priority-queue.js +70 -1
  37. package/dist/data-structures/queue/deque.d.ts +27 -18
  38. package/dist/data-structures/queue/deque.js +43 -21
  39. package/dist/data-structures/queue/queue.d.ts +8 -29
  40. package/dist/data-structures/queue/queue.js +15 -32
  41. package/dist/data-structures/stack/stack.d.ts +17 -22
  42. package/dist/data-structures/stack/stack.js +25 -24
  43. package/dist/data-structures/trie/trie.d.ts +18 -13
  44. package/dist/data-structures/trie/trie.js +26 -15
  45. package/dist/interfaces/binary-tree.d.ts +4 -4
  46. package/dist/types/common.d.ts +1 -22
  47. package/dist/types/data-structures/base/base.d.ts +5 -2
  48. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -3
  49. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
  50. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +20 -4
  51. package/dist/types/data-structures/binary-tree/bst.d.ts +5 -3
  52. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -3
  53. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -3
  54. package/dist/types/data-structures/heap/heap.d.ts +3 -2
  55. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
  56. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
  57. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  58. package/dist/types/data-structures/queue/deque.d.ts +4 -2
  59. package/dist/types/data-structures/queue/queue.d.ts +2 -1
  60. package/dist/types/data-structures/stack/stack.d.ts +2 -1
  61. package/dist/types/data-structures/trie/trie.d.ts +3 -2
  62. package/package.json +2 -2
  63. package/src/data-structures/base/index.ts +2 -1
  64. package/src/data-structures/base/iterable-element-base.ts +250 -0
  65. package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
  66. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +14 -15
  67. package/src/data-structures/binary-tree/avl-tree.ts +13 -14
  68. package/src/data-structures/binary-tree/binary-tree.ts +156 -152
  69. package/src/data-structures/binary-tree/bst.ts +52 -60
  70. package/src/data-structures/binary-tree/rb-tree.ts +12 -13
  71. package/src/data-structures/binary-tree/tree-multi-map.ts +12 -13
  72. package/src/data-structures/graph/directed-graph.ts +2 -1
  73. package/src/data-structures/hash/hash-map.ts +4 -4
  74. package/src/data-structures/heap/heap.ts +71 -152
  75. package/src/data-structures/heap/max-heap.ts +88 -13
  76. package/src/data-structures/heap/min-heap.ts +78 -15
  77. package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
  78. package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
  79. package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
  80. package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
  81. package/src/data-structures/priority-queue/priority-queue.ts +81 -4
  82. package/src/data-structures/queue/deque.ts +50 -25
  83. package/src/data-structures/queue/queue.ts +23 -37
  84. package/src/data-structures/stack/stack.ts +31 -26
  85. package/src/data-structures/trie/trie.ts +33 -18
  86. package/src/interfaces/binary-tree.ts +4 -5
  87. package/src/types/common.ts +2 -24
  88. package/src/types/data-structures/base/base.ts +14 -6
  89. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -3
  90. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -3
  91. package/src/types/data-structures/binary-tree/binary-tree.ts +24 -5
  92. package/src/types/data-structures/binary-tree/bst.ts +9 -3
  93. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -3
  94. package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -3
  95. package/src/types/data-structures/heap/heap.ts +4 -1
  96. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
  97. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
  98. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  99. package/src/types/data-structures/queue/deque.ts +6 -1
  100. package/src/types/data-structures/queue/queue.ts +3 -1
  101. package/src/types/data-structures/stack/stack.ts +3 -1
  102. package/src/types/data-structures/trie/trie.ts +3 -1
@@ -4,26 +4,83 @@ exports.MinHeap = void 0;
4
4
  const heap_1 = require("./heap");
5
5
  /**
6
6
  * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
7
- * 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
7
+ * 2. MinHeap Properties: The value of each parent node is less than or equal to the value of its children.
8
8
  * 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
9
9
  * 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
10
10
  * 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
11
11
  * 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
12
- * 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
12
+ * 7. Efficient Sorting Algorithms: For example, heap sort. MinHeap sort uses the properties of a heap to sort elements.
13
13
  * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
14
14
  */
15
15
  class MinHeap extends heap_1.Heap {
16
- constructor(elements = [], options = {
17
- comparator: (a, b) => {
18
- if (!(typeof a === 'number' && typeof b === 'number')) {
19
- throw new Error('The a, b params of compare function must be number');
20
- }
21
- else {
22
- return a - b;
16
+ constructor(elements = [], options) {
17
+ super(elements, options);
18
+ }
19
+ /**
20
+ * The `clone` function returns a new instance of the `MinHeap` class with the same comparator and
21
+ * toElementFn as the original instance.
22
+ * @returns The `clone()` method is returning a new instance of the `MinHeap` class with the same
23
+ * properties as the current instance.
24
+ */
25
+ clone() {
26
+ return new MinHeap(this, { comparator: this.comparator, toElementFn: this.toElementFn });
27
+ }
28
+ /**
29
+ * Time Complexity: O(n)
30
+ * Space Complexity: O(n)
31
+ *
32
+ * The `filter` function creates a new MinHeap object containing elements that pass a given callback
33
+ * function.
34
+ * @param callback - The `callback` parameter is a function that will be called for each element in
35
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
36
+ * heap itself. The callback function should return a boolean value indicating whether the current
37
+ * element should be included in the filtered list
38
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
39
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
40
+ * passed as the `this` value to the `callback` function. If `thisArg` is
41
+ * @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
42
+ * the filter condition specified by the `callback` function.
43
+ */
44
+ filter(callback, thisArg) {
45
+ const filteredList = new MinHeap([], { toElementFn: this.toElementFn, comparator: this.comparator });
46
+ let index = 0;
47
+ for (const current of this) {
48
+ if (callback.call(thisArg, current, index, this)) {
49
+ filteredList.add(current);
23
50
  }
51
+ index++;
24
52
  }
25
- }) {
26
- super(elements, options);
53
+ return filteredList;
54
+ }
55
+ /**
56
+ * Time Complexity: O(n log n)
57
+ * Space Complexity: O(n)
58
+ *
59
+ * The `map` function creates a new heap by applying a callback function to each element of the
60
+ * original heap.
61
+ * @param callback - The `callback` parameter is a function that will be called for each element in
62
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
63
+ * element), and `this` (the heap itself). The callback function should return a value of
64
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
65
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
66
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
67
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
68
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
69
+ * returns a value of type `T`. This function is used to transform the elements of the original
70
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
71
+ * specify the value of `this` within the callback function. It is used to set the context or scope
72
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
73
+ * value of
74
+ * @returns a new instance of the `MinHeap` class with the mapped elements.
75
+ */
76
+ map(callback, comparator, toElementFn, thisArg) {
77
+ const mappedHeap = new MinHeap([], { comparator, toElementFn });
78
+ let index = 0;
79
+ for (const el of this) {
80
+ mappedHeap.add(callback.call(thisArg, el, index, this));
81
+ index++;
82
+ }
83
+ return mappedHeap;
27
84
  }
28
85
  }
29
86
  exports.MinHeap = MinHeap;
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { DoublyLinkedListOptions, ElementCallback } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
  export declare class DoublyLinkedListNode<E = any> {
11
11
  /**
@@ -60,14 +60,8 @@ export declare class DoublyLinkedListNode<E = any> {
60
60
  * 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.
61
61
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
62
62
  */
63
- export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
64
- /**
65
- * The constructor initializes a linked list with optional elements.
66
- * @param elements - The `elements` parameter is an optional iterable object that contains the
67
- * initial elements to be added to the data structure. It defaults to an empty array if no elements
68
- * are provided.
69
- */
70
- constructor(elements?: Iterable<E>);
63
+ export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
64
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);
71
65
  protected _head: DoublyLinkedListNode<E> | undefined;
72
66
  /**
73
67
  * The `head` function returns the first node of a doubly linked list.
@@ -125,7 +119,7 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
125
119
  * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
126
120
  * @returns The `fromArray` function returns a DoublyLinkedList object.
127
121
  */
128
- static fromArray<E>(data: E[]): DoublyLinkedList<E>;
122
+ static fromArray<E>(data: E[]): DoublyLinkedList<E, any>;
129
123
  /**
130
124
  * Time Complexity: O(1)
131
125
  * Space Complexity: O(1)
@@ -393,7 +387,7 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
393
387
  * @returns The `clone()` method is returning a new instance of the `DoublyLinkedList` class, which
394
388
  * is a copy of the original list.
395
389
  */
396
- clone(): DoublyLinkedList<E>;
390
+ clone(): DoublyLinkedList<E, R>;
397
391
  /**
398
392
  * Time Complexity: O(n)
399
393
  * Space Complexity: O(n)
@@ -415,29 +409,29 @@ export declare class DoublyLinkedList<E = any> extends IterableElementBase<E> {
415
409
  * @returns The `filter` method is returning a new `DoublyLinkedList` object that contains the
416
410
  * elements that pass the filter condition specified by the `callback` function.
417
411
  */
418
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): DoublyLinkedList<E>;
412
+ filter(callback: ElementCallback<E, R, boolean, DoublyLinkedList<E, R>>, thisArg?: any): DoublyLinkedList<E, R>;
419
413
  /**
420
414
  * Time Complexity: O(n)
421
415
  * Space Complexity: O(n)
422
416
  */
423
417
  /**
424
- * Time Complexity: O(n)
425
- * Space Complexity: O(n)
426
- *
427
- * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
428
- * in the original list.
418
+ * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
419
+ * of applying the callback to each element in the original list.
429
420
  * @param callback - The callback parameter is a function that will be called for each element in the
430
- * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
431
- * and the DoublyLinkedList itself. The callback function should return a value that will be added to
432
- * the new DoublyLinkedList that
433
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
434
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
435
- * passed as the `this` value to the `callback` function. If `thisArg` is
436
- * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
437
- * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
438
- * object.
439
- */
440
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): DoublyLinkedList<T>;
421
+ * original DoublyLinkedList. It takes three arguments: current (the current element being
422
+ * processed), index (the index of the current element), and this (the original DoublyLinkedList).
423
+ * The callback function should return a value of type
424
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
425
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
426
+ * input and returns the converted element. If this parameter is not provided, the raw element will
427
+ * be used as is.
428
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
429
+ * specify the value of `this` within the callback function. It is used to set the context or scope
430
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
431
+ * value of
432
+ * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
433
+ */
434
+ map<EM, RM>(callback: ElementCallback<E, R, EM, DoublyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): DoublyLinkedList<EM, RM>;
441
435
  /**
442
436
  * The function returns an iterator that iterates over the values of a linked list.
443
437
  */
@@ -70,20 +70,18 @@ exports.DoublyLinkedListNode = DoublyLinkedListNode;
70
70
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
71
71
  */
72
72
  class DoublyLinkedList extends base_1.IterableElementBase {
73
- /**
74
- * The constructor initializes a linked list with optional elements.
75
- * @param elements - The `elements` parameter is an optional iterable object that contains the
76
- * initial elements to be added to the data structure. It defaults to an empty array if no elements
77
- * are provided.
78
- */
79
- constructor(elements = []) {
80
- super();
73
+ constructor(elements = [], options) {
74
+ super(options);
81
75
  this._head = undefined;
82
76
  this._tail = undefined;
83
77
  this._size = 0;
84
78
  if (elements) {
85
79
  for (const el of elements) {
86
- this.push(el);
80
+ if (this.toElementFn) {
81
+ this.push(this.toElementFn(el));
82
+ }
83
+ else
84
+ this.push(el);
87
85
  }
88
86
  }
89
87
  }
@@ -663,7 +661,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
663
661
  * is a copy of the original list.
664
662
  */
665
663
  clone() {
666
- return new DoublyLinkedList(this.values());
664
+ return new DoublyLinkedList(this);
667
665
  }
668
666
  /**
669
667
  * Time Complexity: O(n)
@@ -687,7 +685,7 @@ class DoublyLinkedList extends base_1.IterableElementBase {
687
685
  * elements that pass the filter condition specified by the `callback` function.
688
686
  */
689
687
  filter(callback, thisArg) {
690
- const filteredList = new DoublyLinkedList();
688
+ const filteredList = new DoublyLinkedList([], { toElementFn: this.toElementFn });
691
689
  let index = 0;
692
690
  for (const current of this) {
693
691
  if (callback.call(thisArg, current, index, this)) {
@@ -702,24 +700,24 @@ class DoublyLinkedList extends base_1.IterableElementBase {
702
700
  * Space Complexity: O(n)
703
701
  */
704
702
  /**
705
- * Time Complexity: O(n)
706
- * Space Complexity: O(n)
707
- *
708
- * The `map` function creates a new DoublyLinkedList by applying a callback function to each element
709
- * in the original list.
703
+ * The `map` function takes a callback function and returns a new DoublyLinkedList with the results
704
+ * of applying the callback to each element in the original list.
710
705
  * @param callback - The callback parameter is a function that will be called for each element in the
711
- * DoublyLinkedList. It takes three arguments: the current element, the index of the current element,
712
- * and the DoublyLinkedList itself. The callback function should return a value that will be added to
713
- * the new DoublyLinkedList that
714
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
715
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
716
- * passed as the `this` value to the `callback` function. If `thisArg` is
717
- * @returns The `map` function is returning a new `DoublyLinkedList` object that contains the results
718
- * of applying the provided `callback` function to each element in the original `DoublyLinkedList`
719
- * object.
720
- */
721
- map(callback, thisArg) {
722
- const mappedList = new DoublyLinkedList();
706
+ * original DoublyLinkedList. It takes three arguments: current (the current element being
707
+ * processed), index (the index of the current element), and this (the original DoublyLinkedList).
708
+ * The callback function should return a value of type
709
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
710
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
711
+ * input and returns the converted element. If this parameter is not provided, the raw element will
712
+ * be used as is.
713
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
714
+ * specify the value of `this` within the callback function. It is used to set the context or scope
715
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
716
+ * value of
717
+ * @returns a new instance of the `DoublyLinkedList` class with elements of type `T` and `RR`.
718
+ */
719
+ map(callback, toElementFn, thisArg) {
720
+ const mappedList = new DoublyLinkedList([], { toElementFn });
723
721
  let index = 0;
724
722
  for (const current of this) {
725
723
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { ElementCallback } from '../../types';
8
+ import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
9
  import { IterableElementBase } from '../base';
10
10
  export declare class SinglyLinkedListNode<E = any> {
11
11
  /**
@@ -40,14 +40,8 @@ export declare class SinglyLinkedListNode<E = any> {
40
40
  */
41
41
  set next(value: SinglyLinkedListNode<E> | undefined);
42
42
  }
43
- export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
44
- /**
45
- * The constructor initializes a new instance of a class with an optional iterable of elements.
46
- * @param elements - The `elements` parameter is an optional iterable object that contains the
47
- * initial elements to be added to the instance of the class. If no `elements` are provided, an empty
48
- * array will be used as the default value.
49
- */
50
- constructor(elements?: Iterable<E>);
43
+ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
44
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: SinglyLinkedListOptions<E, R>);
51
45
  protected _head: SinglyLinkedListNode<E> | undefined;
52
46
  /**
53
47
  * The `head` function returns the first node of a singly linked list.
@@ -93,7 +87,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
93
87
  * @param {E[]} data - The `data` parameter is an array of elements of type `E`.
94
88
  * @returns The `fromArray` function returns a `SinglyLinkedList` object.
95
89
  */
96
- static fromArray<E>(data: E[]): SinglyLinkedList<E>;
90
+ static fromArray<E>(data: E[]): SinglyLinkedList<E, any>;
97
91
  /**
98
92
  * Time Complexity: O(1)
99
93
  * Space Complexity: O(1)
@@ -348,7 +342,7 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
348
342
  * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
349
343
  * is a clone of the original list.
350
344
  */
351
- clone(): SinglyLinkedList<E>;
345
+ clone(): SinglyLinkedList<E, R>;
352
346
  /**
353
347
  * Time Complexity: O(n)
354
348
  * Space Complexity: O(n)
@@ -370,26 +364,29 @@ export declare class SinglyLinkedList<E = any> extends IterableElementBase<E> {
370
364
  * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
371
365
  * elements that pass the filter condition specified by the `callback` function.
372
366
  */
373
- filter(callback: ElementCallback<E, boolean>, thisArg?: any): SinglyLinkedList<E>;
367
+ filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R>;
374
368
  /**
375
369
  * Time Complexity: O(n)
376
370
  * Space Complexity: O(n)
377
371
  */
378
372
  /**
379
- * Time Complexity: O(n)
380
- * Space Complexity: O(n)
381
- *
382
- * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
383
- * of the original list.
373
+ * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
374
+ * of applying the callback to each element in the original list.
384
375
  * @param callback - The `callback` parameter is a function that will be called for each element in
385
- * the linked list. It takes three arguments:
386
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
387
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
388
- * passed as the `this` value to the `callback` function. If `thisArg` is
389
- * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
390
- * of applying the provided `callback` function to each element in the original list.
391
- */
392
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): SinglyLinkedList<T>;
376
+ * the original list. It takes three arguments: `current` (the current element being processed),
377
+ * `index` (the index of the current element), and `this` (the original list). It should return a
378
+ * value
379
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
380
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
381
+ * input and returns the converted element. If this parameter is not provided, the raw element will
382
+ * be used as is.
383
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
384
+ * specify the value of `this` within the callback function. It is used to set the context or scope
385
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
386
+ * value of
387
+ * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
388
+ */
389
+ map<EM, RM>(callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
393
390
  /**
394
391
  * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
395
392
  */
@@ -46,18 +46,18 @@ class SinglyLinkedListNode {
46
46
  }
47
47
  exports.SinglyLinkedListNode = SinglyLinkedListNode;
48
48
  class SinglyLinkedList extends base_1.IterableElementBase {
49
- /**
50
- * The constructor initializes a new instance of a class with an optional iterable of elements.
51
- * @param elements - The `elements` parameter is an optional iterable object that contains the
52
- * initial elements to be added to the instance of the class. If no `elements` are provided, an empty
53
- * array will be used as the default value.
54
- */
55
- constructor(elements = []) {
56
- super();
49
+ constructor(elements = [], options) {
50
+ super(options);
57
51
  this._size = 0;
58
52
  if (elements) {
59
- for (const el of elements)
60
- this.push(el);
53
+ for (const el of elements) {
54
+ if (this.toElementFn) {
55
+ this.push(this.toElementFn(el));
56
+ }
57
+ else {
58
+ this.push(el);
59
+ }
60
+ }
61
61
  }
62
62
  }
63
63
  /**
@@ -608,7 +608,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
608
608
  * is a clone of the original list.
609
609
  */
610
610
  clone() {
611
- return new SinglyLinkedList(this.values());
611
+ return new SinglyLinkedList(this, { toElementFn: this.toElementFn });
612
612
  }
613
613
  /**
614
614
  * Time Complexity: O(n)
@@ -632,7 +632,7 @@ class SinglyLinkedList extends base_1.IterableElementBase {
632
632
  * elements that pass the filter condition specified by the `callback` function.
633
633
  */
634
634
  filter(callback, thisArg) {
635
- const filteredList = new SinglyLinkedList();
635
+ const filteredList = new SinglyLinkedList([], { toElementFn: this.toElementFn });
636
636
  let index = 0;
637
637
  for (const current of this) {
638
638
  if (callback.call(thisArg, current, index, this)) {
@@ -647,21 +647,24 @@ class SinglyLinkedList extends base_1.IterableElementBase {
647
647
  * Space Complexity: O(n)
648
648
  */
649
649
  /**
650
- * Time Complexity: O(n)
651
- * Space Complexity: O(n)
652
- *
653
- * The `map` function creates a new SinglyLinkedList by applying a callback function to each element
654
- * of the original list.
650
+ * The `map` function takes a callback function and returns a new SinglyLinkedList with the results
651
+ * of applying the callback to each element in the original list.
655
652
  * @param callback - The `callback` parameter is a function that will be called for each element in
656
- * the linked list. It takes three arguments:
657
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
658
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
659
- * passed as the `this` value to the `callback` function. If `thisArg` is
660
- * @returns The `map` function is returning a new `SinglyLinkedList` object that contains the results
661
- * of applying the provided `callback` function to each element in the original list.
662
- */
663
- map(callback, thisArg) {
664
- const mappedList = new SinglyLinkedList();
653
+ * the original list. It takes three arguments: `current` (the current element being processed),
654
+ * `index` (the index of the current element), and `this` (the original list). It should return a
655
+ * value
656
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
657
+ * convert the raw element (`RR`) to the desired element type (`T`). It takes the raw element as
658
+ * input and returns the converted element. If this parameter is not provided, the raw element will
659
+ * be used as is.
660
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
661
+ * specify the value of `this` within the callback function. It is used to set the context or scope
662
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
663
+ * value of
664
+ * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
665
+ */
666
+ map(callback, toElementFn, thisArg) {
667
+ const mappedList = new SinglyLinkedList([], { toElementFn });
665
668
  let index = 0;
666
669
  for (const current of this) {
667
670
  mappedList.push(callback.call(thisArg, current, index, this));
@@ -5,9 +5,9 @@
5
5
  * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { PriorityQueueOptions } from '../../types';
8
+ import type { Comparator, ElementCallback, PriorityQueueOptions } from '../../types';
9
9
  import { PriorityQueue } from './priority-queue';
10
- export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
10
+ export declare class MaxPriorityQueue<E = any, R = any> extends PriorityQueue<E, R> {
11
11
  /**
12
12
  * The constructor initializes a PriorityQueue with optional elements and options, including a
13
13
  * comparator function.
@@ -15,8 +15,54 @@ export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
15
15
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
16
16
  * provided.
17
17
  * @param options - The `options` parameter is an object that contains additional configuration
18
- * options for the priority queue. In this case, it has a property called `comparator` which is a
18
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
19
19
  * function used to compare elements in the priority queue.
20
20
  */
21
- constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
21
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
22
+ /**
23
+ * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
24
+ * comparator and toElementFn as the current instance.
25
+ * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
26
+ * comparator and toElementFn as the current instance.
27
+ */
28
+ clone(): MaxPriorityQueue<E, R>;
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ *
33
+ * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
34
+ * function.
35
+ * @param callback - The `callback` parameter is a function that will be called for each element in
36
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
37
+ * heap itself. The callback function should return a boolean value indicating whether the current
38
+ * element should be included in the filtered list
39
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
40
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
41
+ * passed as the `this` value to the `callback` function. If `thisArg` is
42
+ * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
43
+ * the filter condition specified by the `callback` function.
44
+ */
45
+ filter(callback: ElementCallback<E, R, boolean, MaxPriorityQueue<E, R>>, thisArg?: any): MaxPriorityQueue<E, R>;
46
+ /**
47
+ * Time Complexity: O(n log n)
48
+ * Space Complexity: O(n)
49
+ *
50
+ * The `map` function creates a new heap by applying a callback function to each element of the
51
+ * original heap.
52
+ * @param callback - The `callback` parameter is a function that will be called for each element in
53
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
54
+ * element), and `this` (the heap itself). The callback function should return a value of
55
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
56
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
57
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
58
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
59
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
60
+ * returns a value of type `T`. This function is used to transform the elements of the original
61
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
62
+ * specify the value of `this` within the callback function. It is used to set the context or scope
63
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
64
+ * value of
65
+ * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
66
+ */
67
+ map<EM, RM>(callback: ElementCallback<E, R, EM, MaxPriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MaxPriorityQueue<EM, RM>;
22
68
  }
@@ -10,20 +10,89 @@ class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
10
10
  * elements to be added to the priority queue. It is optional and defaults to an empty array if not
11
11
  * provided.
12
12
  * @param options - The `options` parameter is an object that contains additional configuration
13
- * options for the priority queue. In this case, it has a property called `comparator` which is a
13
+ * options for the priority queue. In this case, it has a property called `comparator,` which is a
14
14
  * function used to compare elements in the priority queue.
15
15
  */
16
- constructor(elements = [], options = {
17
- comparator: (a, b) => {
18
- if (!(typeof a === 'number' && typeof b === 'number')) {
19
- throw new Error('The a, b params of compare function must be number');
20
- }
21
- else {
22
- return b - a;
16
+ constructor(elements = [], options) {
17
+ super(elements, Object.assign({ comparator: (a, b) => {
18
+ if (typeof a === 'object' || typeof b === 'object') {
19
+ throw TypeError(`When comparing object types, a custom comparator must be defined in the constructor's options parameter.`);
20
+ }
21
+ if (a < b)
22
+ return 1;
23
+ if (a > b)
24
+ return -1;
25
+ return 0;
26
+ } }, options));
27
+ }
28
+ /**
29
+ * The `clone` function returns a new instance of the `MaxPriorityQueue` class with the same
30
+ * comparator and toElementFn as the current instance.
31
+ * @returns The method is returning a new instance of the MaxPriorityQueue class with the same
32
+ * comparator and toElementFn as the current instance.
33
+ */
34
+ clone() {
35
+ return new MaxPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
36
+ }
37
+ /**
38
+ * Time Complexity: O(n)
39
+ * Space Complexity: O(n)
40
+ *
41
+ * The `filter` function creates a new MaxPriorityQueue object containing elements that pass a given callback
42
+ * function.
43
+ * @param callback - The `callback` parameter is a function that will be called for each element in
44
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
45
+ * heap itself. The callback function should return a boolean value indicating whether the current
46
+ * element should be included in the filtered list
47
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
48
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
49
+ * passed as the `this` value to the `callback` function. If `thisArg` is
50
+ * @returns The `filter` method is returning a new `MaxPriorityQueue` object that contains the elements that pass
51
+ * the filter condition specified by the `callback` function.
52
+ */
53
+ filter(callback, thisArg) {
54
+ const filteredPriorityQueue = new MaxPriorityQueue([], {
55
+ toElementFn: this.toElementFn,
56
+ comparator: this.comparator
57
+ });
58
+ let index = 0;
59
+ for (const current of this) {
60
+ if (callback.call(thisArg, current, index, this)) {
61
+ filteredPriorityQueue.add(current);
23
62
  }
63
+ index++;
64
+ }
65
+ return filteredPriorityQueue;
66
+ }
67
+ /**
68
+ * Time Complexity: O(n log n)
69
+ * Space Complexity: O(n)
70
+ *
71
+ * The `map` function creates a new heap by applying a callback function to each element of the
72
+ * original heap.
73
+ * @param callback - The `callback` parameter is a function that will be called for each element in
74
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
75
+ * element), and `this` (the heap itself). The callback function should return a value of
76
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
77
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
78
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
79
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
80
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
81
+ * returns a value of type `T`. This function is used to transform the elements of the original
82
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
83
+ * specify the value of `this` within the callback function. It is used to set the context or scope
84
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
85
+ * value of
86
+ * @returns a new instance of the `MaxPriorityQueue` class with the mapped elements.
87
+ */
88
+ map(callback, comparator, toElementFn, thisArg) {
89
+ const mappedPriorityQueue = new MaxPriorityQueue([], { comparator, toElementFn });
90
+ let index = 0;
91
+ for (const el of this) {
92
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
93
+ index++;
24
94
  }
25
- }) {
26
- super(elements, options);
95
+ return mappedPriorityQueue;
27
96
  }
28
97
  }
29
98
  exports.MaxPriorityQueue = MaxPriorityQueue;