undirected-graph-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
@@ -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 MinPriorityQueue<E = any> extends PriorityQueue<E> {
10
+ export declare class MinPriorityQueue<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,9 +15,55 @@ export declare class MinPriorityQueue<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. The `comparator` function takes two
20
- * parameters `a` and `b`,
20
+ * parameters `a` and `b`
21
21
  */
22
- constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
22
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
23
+ /**
24
+ * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
25
+ * comparator and toElementFn as the original instance.
26
+ * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
27
+ * properties as the current instance.
28
+ */
29
+ clone(): MinPriorityQueue<E, R>;
30
+ /**
31
+ * Time Complexity: O(n)
32
+ * Space Complexity: O(n)
33
+ *
34
+ * The `filter` function creates a new MinPriorityQueue object containing elements that pass a given callback
35
+ * function.
36
+ * @param callback - The `callback` parameter is a function that will be called for each element in
37
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
38
+ * heap itself. The callback function should return a boolean value indicating whether the current
39
+ * element should be included in the filtered list
40
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
41
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
42
+ * passed as the `this` value to the `callback` function. If `thisArg` is
43
+ * @returns The `filter` method is returning a new `MinPriorityQueue` object that contains the elements that pass
44
+ * the filter condition specified by the `callback` function.
45
+ */
46
+ filter(callback: ElementCallback<E, R, boolean, MinPriorityQueue<E, R>>, thisArg?: any): MinPriorityQueue<E, R>;
47
+ /**
48
+ * Time Complexity: O(n log n)
49
+ * Space Complexity: O(n)
50
+ *
51
+ * The `map` function creates a new heap by applying a callback function to each element of the
52
+ * original heap.
53
+ * @param callback - The `callback` parameter is a function that will be called for each element in
54
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
55
+ * element), and `this` (the heap itself). The callback function should return a value of
56
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
57
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
58
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
59
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
60
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
61
+ * returns a value of type `T`. This function is used to transform the elements of the original
62
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
63
+ * specify the value of `this` within the callback function. It is used to set the context or scope
64
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
65
+ * value of
66
+ * @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
67
+ */
68
+ map<EM, RM>(callback: ElementCallback<E, R, EM, MinPriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): MinPriorityQueue<EM, RM>;
23
69
  }
@@ -10,21 +10,81 @@ class MinPriorityQueue 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. The `comparator` function takes two
15
- * parameters `a` and `b`,
15
+ * parameters `a` and `b`
16
16
  */
17
- constructor(elements = [], options = {
18
- comparator: (a, b) => {
19
- if (!(typeof a === 'number' && typeof b === 'number')) {
20
- throw new Error('The a, b params of compare function must be number');
21
- }
22
- else {
23
- return a - b;
17
+ constructor(elements = [], options) {
18
+ super(elements, options);
19
+ }
20
+ /**
21
+ * The `clone` function returns a new instance of the `MinPriorityQueue` class with the same
22
+ * comparator and toElementFn as the original instance.
23
+ * @returns The method is returning a new instance of the `MinPriorityQueue` class with the same
24
+ * properties as the current instance.
25
+ */
26
+ clone() {
27
+ return new MinPriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
28
+ }
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ *
33
+ * The `filter` function creates a new MinPriorityQueue 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 `MinPriorityQueue` object that contains the elements that pass
43
+ * the filter condition specified by the `callback` function.
44
+ */
45
+ filter(callback, thisArg) {
46
+ const filteredPriorityQueue = new MinPriorityQueue([], {
47
+ toElementFn: this.toElementFn,
48
+ comparator: this.comparator
49
+ });
50
+ let index = 0;
51
+ for (const current of this) {
52
+ if (callback.call(thisArg, current, index, this)) {
53
+ filteredPriorityQueue.add(current);
24
54
  }
55
+ index++;
25
56
  }
26
- }) {
27
- super(elements, options);
57
+ return filteredPriorityQueue;
58
+ }
59
+ /**
60
+ * Time Complexity: O(n log n)
61
+ * Space Complexity: O(n)
62
+ *
63
+ * The `map` function creates a new heap by applying a callback function to each element of the
64
+ * original heap.
65
+ * @param callback - The `callback` parameter is a function that will be called for each element in
66
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
67
+ * element), and `this` (the heap itself). The callback function should return a value of
68
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
69
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
70
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
71
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
72
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
73
+ * returns a value of type `T`. This function is used to transform the elements of the original
74
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
75
+ * specify the value of `this` within the callback function. It is used to set the context or scope
76
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
77
+ * value of
78
+ * @returns a new instance of the `MinPriorityQueue` class with the mapped elements.
79
+ */
80
+ map(callback, comparator, toElementFn, thisArg) {
81
+ const mappedPriorityQueue = new MinPriorityQueue([], { comparator, toElementFn });
82
+ let index = 0;
83
+ for (const el of this) {
84
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
85
+ index++;
86
+ }
87
+ return mappedPriorityQueue;
28
88
  }
29
89
  }
30
90
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -5,7 +5,7 @@
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 { Heap } from '../heap';
10
10
  /**
11
11
  * 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
@@ -15,14 +15,60 @@ import { Heap } from '../heap';
15
15
  * 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
16
16
  * 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
17
17
  */
18
- export declare class PriorityQueue<E = any> extends Heap<E> {
18
+ export declare class PriorityQueue<E = any, R = any> extends Heap<E, R> {
19
19
  /**
20
20
  * The constructor initializes a priority queue with optional elements and options.
21
21
  * @param elements - The `elements` parameter is an iterable object that contains the initial
22
- * elements to be added to the priority queue. It is an optional parameter and if not provided, the
22
+ * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
23
23
  * priority queue will be initialized as empty.
24
24
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
25
25
  * behavior of the priority queue. It can contain the following properties:
26
26
  */
27
- constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
27
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: PriorityQueueOptions<E, R>);
28
+ /**
29
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
30
+ * and toElementFn as the original instance.
31
+ * @returns The method is returning a new instance of the `PriorityQueue` class with the same
32
+ * elements and properties as the current instance.
33
+ */
34
+ clone(): PriorityQueue<E, R>;
35
+ /**
36
+ * Time Complexity: O(n)
37
+ * Space Complexity: O(n)
38
+ *
39
+ * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
40
+ * function.
41
+ * @param callback - The `callback` parameter is a function that will be called for each element in
42
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
43
+ * heap itself. The callback function should return a boolean value indicating whether the current
44
+ * element should be included in the filtered list
45
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
46
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
47
+ * passed as the `this` value to the `callback` function. If `thisArg` is
48
+ * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
49
+ * the filter condition specified by the `callback` function.
50
+ */
51
+ filter(callback: ElementCallback<E, R, boolean, PriorityQueue<E, R>>, thisArg?: any): PriorityQueue<E, R>;
52
+ /**
53
+ * Time Complexity: O(n log n)
54
+ * Space Complexity: O(n)
55
+ *
56
+ * The `map` function creates a new heap by applying a callback function to each element of the
57
+ * original heap.
58
+ * @param callback - The `callback` parameter is a function that will be called for each element in
59
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
60
+ * element), and `this` (the heap itself). The callback function should return a value of
61
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
62
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
63
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
64
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
65
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
66
+ * returns a value of type `T`. This function is used to transform the elements of the original
67
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
68
+ * specify the value of `this` within the callback function. It is used to set the context or scope
69
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
70
+ * value of
71
+ * @returns a new instance of the `PriorityQueue` class with the mapped elements.
72
+ */
73
+ map<EM, RM>(callback: ElementCallback<E, R, EM, PriorityQueue<E, R>>, comparator: Comparator<EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): PriorityQueue<EM, RM>;
28
74
  }
@@ -14,7 +14,7 @@ class PriorityQueue extends heap_1.Heap {
14
14
  /**
15
15
  * The constructor initializes a priority queue with optional elements and options.
16
16
  * @param elements - The `elements` parameter is an iterable object that contains the initial
17
- * elements to be added to the priority queue. It is an optional parameter and if not provided, the
17
+ * elements to be added to the priority queue. It is an optional parameter, and if not provided, the
18
18
  * priority queue will be initialized as empty.
19
19
  * @param [options] - The `options` parameter is an optional object that can be used to customize the
20
20
  * behavior of the priority queue. It can contain the following properties:
@@ -22,5 +22,74 @@ class PriorityQueue extends heap_1.Heap {
22
22
  constructor(elements = [], options) {
23
23
  super(elements, options);
24
24
  }
25
+ /**
26
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same comparator
27
+ * and toElementFn as the original instance.
28
+ * @returns The method is returning a new instance of the `PriorityQueue` class with the same
29
+ * elements and properties as the current instance.
30
+ */
31
+ clone() {
32
+ return new PriorityQueue(this, { comparator: this.comparator, toElementFn: this.toElementFn });
33
+ }
34
+ /**
35
+ * Time Complexity: O(n)
36
+ * Space Complexity: O(n)
37
+ *
38
+ * The `filter` function creates a new PriorityQueue object containing elements that pass a given callback
39
+ * function.
40
+ * @param callback - The `callback` parameter is a function that will be called for each element in
41
+ * the heap. It takes three arguments: the current element, the index of the current element, and the
42
+ * heap itself. The callback function should return a boolean value indicating whether the current
43
+ * element should be included in the filtered list
44
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
45
+ * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
46
+ * passed as the `this` value to the `callback` function. If `thisArg` is
47
+ * @returns The `filter` method is returning a new `PriorityQueue` object that contains the elements that pass
48
+ * the filter condition specified by the `callback` function.
49
+ */
50
+ filter(callback, thisArg) {
51
+ const filteredPriorityQueue = new PriorityQueue([], {
52
+ toElementFn: this.toElementFn,
53
+ comparator: this.comparator
54
+ });
55
+ let index = 0;
56
+ for (const current of this) {
57
+ if (callback.call(thisArg, current, index, this)) {
58
+ filteredPriorityQueue.add(current);
59
+ }
60
+ index++;
61
+ }
62
+ return filteredPriorityQueue;
63
+ }
64
+ /**
65
+ * Time Complexity: O(n log n)
66
+ * Space Complexity: O(n)
67
+ *
68
+ * The `map` function creates a new heap by applying a callback function to each element of the
69
+ * original heap.
70
+ * @param callback - The `callback` parameter is a function that will be called for each element in
71
+ * the heap. It takes three arguments: `el` (the current element), `index` (the index of the current
72
+ * element), and `this` (the heap itself). The callback function should return a value of
73
+ * @param comparator - The `comparator` parameter is a function that defines the order of the
74
+ * elements in the heap. It takes two elements `a` and `b` as arguments and returns a negative number
75
+ * if `a` should be placed before `b`, a positive number if `a` should be placed after
76
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that converts the raw
77
+ * element `RR` to the desired type `T`. It takes a single argument `rawElement` of type `RR` and
78
+ * returns a value of type `T`. This function is used to transform the elements of the original
79
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
80
+ * specify the value of `this` within the callback function. It is used to set the context or scope
81
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
82
+ * value of
83
+ * @returns a new instance of the `PriorityQueue` class with the mapped elements.
84
+ */
85
+ map(callback, comparator, toElementFn, thisArg) {
86
+ const mappedPriorityQueue = new PriorityQueue([], { comparator, toElementFn });
87
+ let index = 0;
88
+ for (const el of this) {
89
+ mappedPriorityQueue.add(callback.call(thisArg, el, index, this));
90
+ index++;
91
+ }
92
+ return mappedPriorityQueue;
93
+ }
25
94
  }
26
95
  exports.PriorityQueue = PriorityQueue;
@@ -14,9 +14,9 @@ import { IterableElementBase } from '../base';
14
14
  * 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
15
15
  * 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
16
16
  */
17
- export declare class Deque<E> extends IterableElementBase<E> {
17
+ export declare class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E, R>> {
18
18
  /**
19
- * The constructor initializes a Deque object with an optional iterable of elements and options.
19
+ * The constructor initializes a Deque object with optional iterable of elements and options.
20
20
  * @param elements - An iterable object (such as an array or a Set) that contains the initial
21
21
  * elements to be added to the deque. It can also be an object with a `length` or `size` property
22
22
  * that represents the number of elements in the iterable object. If no elements are provided, an
@@ -26,7 +26,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
26
26
  * which determines the size of each bucket in the deque. If the `bucketSize` option is not provided
27
27
  * or is not a number
28
28
  */
29
- constructor(elements?: IterableWithSizeOrLength<E>, options?: DequeOptions);
29
+ constructor(elements?: IterableWithSizeOrLength<E> | IterableWithSizeOrLength<R>, options?: DequeOptions<E, R>);
30
30
  protected _bucketSize: number;
31
31
  /**
32
32
  * The bucketSize function returns the size of the bucket.
@@ -34,6 +34,13 @@ export declare class Deque<E> extends IterableElementBase<E> {
34
34
  * @return The size of the bucket
35
35
  */
36
36
  get bucketSize(): number;
37
+ protected _maxLen: number;
38
+ /**
39
+ * The maxLen function returns the max length of the deque.
40
+ *
41
+ * @return The max length of the deque
42
+ */
43
+ get maxLen(): number;
37
44
  protected _bucketFirst: number;
38
45
  /**
39
46
  * The function returns the value of the protected variable `_bucketFirst`.
@@ -392,7 +399,7 @@ export declare class Deque<E> extends IterableElementBase<E> {
392
399
  * @returns The `clone()` method is returning a new instance of the `Deque` class with the same
393
400
  * elements as the original deque (`this`) and the same bucket size.
394
401
  */
395
- clone(): Deque<E>;
402
+ clone(): Deque<E, R>;
396
403
  /**
397
404
  * Time Complexity: O(n)
398
405
  * Space Complexity: O(n)
@@ -413,25 +420,27 @@ export declare class Deque<E> extends IterableElementBase<E> {
413
420
  * @returns The `filter` method is returning a new `Deque` object that contains the elements that
414
421
  * satisfy the given predicate function.
415
422
  */
416
- filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E>;
423
+ filter(predicate: ElementCallback<E, R, boolean, Deque<E, R>>, thisArg?: any): Deque<E, R>;
417
424
  /**
418
425
  * Time Complexity: O(n)
419
426
  * Space Complexity: O(n)
420
427
  */
421
428
  /**
422
- * Time Complexity: O(n)
423
- * Space Complexity: O(n)
424
- *
425
- * The `map` function creates a new Deque by applying a callback function to each element of the
426
- * original Deque.
427
- * @param callback - The `callback` parameter is a function that will be called for each element in
428
- * the deque. It takes three arguments:
429
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
430
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
431
- * passed as the `this` value to the `callback` function. If `thisArg` is
432
- * @returns a new Deque object with the mapped values.
433
- */
434
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T>;
429
+ * The `map` function takes a callback function and applies it to each element in the deque,
430
+ * returning a new deque with the results.
431
+ * @param callback - The callback parameter is a function that will be called for each element in the
432
+ * deque. It takes three arguments: the current element, the index of the element, and the deque
433
+ * itself. It should return a value of type EM.
434
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
435
+ * transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
436
+ * provided, this function will be called for each raw element in the original deque.
437
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
438
+ * specify the value of `this` within the callback function. It is used to set the context or scope
439
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
440
+ * value of
441
+ * @returns a new Deque object with elements of type EM and raw elements of type RM.
442
+ */
443
+ map<EM, RM>(callback: ElementCallback<E, R, EM, Deque<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Deque<EM, RM>;
435
444
  /**
436
445
  * Time Complexity: O(n)
437
446
  * Space Complexity: O(1)
@@ -12,7 +12,7 @@ const utils_1 = require("../../utils");
12
12
  */
13
13
  class Deque extends base_1.IterableElementBase {
14
14
  /**
15
- * The constructor initializes a Deque object with an optional iterable of elements and options.
15
+ * The constructor initializes a Deque object with optional iterable of elements and options.
16
16
  * @param elements - An iterable object (such as an array or a Set) that contains the initial
17
17
  * elements to be added to the deque. It can also be an object with a `length` or `size` property
18
18
  * that represents the number of elements in the iterable object. If no elements are provided, an
@@ -23,8 +23,9 @@ class Deque extends base_1.IterableElementBase {
23
23
  * or is not a number
24
24
  */
25
25
  constructor(elements = [], options) {
26
- super();
26
+ super(options);
27
27
  this._bucketSize = 1 << 12;
28
+ this._maxLen = -1;
28
29
  this._bucketFirst = 0;
29
30
  this._firstInBucket = 0;
30
31
  this._bucketLast = 0;
@@ -33,9 +34,11 @@ class Deque extends base_1.IterableElementBase {
33
34
  this._buckets = [];
34
35
  this._size = 0;
35
36
  if (options) {
36
- const { bucketSize } = options;
37
+ const { bucketSize, maxLen } = options;
37
38
  if (typeof bucketSize === 'number')
38
39
  this._bucketSize = bucketSize;
40
+ if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0)
41
+ this._maxLen = maxLen;
39
42
  }
40
43
  let _size;
41
44
  if ('length' in elements) {
@@ -57,8 +60,13 @@ class Deque extends base_1.IterableElementBase {
57
60
  const needBucketNum = (0, utils_1.calcMinUnitsRequired)(_size, this._bucketSize);
58
61
  this._bucketFirst = this._bucketLast = (this._bucketCount >> 1) - (needBucketNum >> 1);
59
62
  this._firstInBucket = this._lastInBucket = (this._bucketSize - (_size % this._bucketSize)) >> 1;
60
- for (const element of elements) {
61
- this.push(element);
63
+ for (const el of elements) {
64
+ if (this.toElementFn) {
65
+ this.push(this.toElementFn(el));
66
+ }
67
+ else {
68
+ this.push(el);
69
+ }
62
70
  }
63
71
  }
64
72
  /**
@@ -69,6 +77,14 @@ class Deque extends base_1.IterableElementBase {
69
77
  get bucketSize() {
70
78
  return this._bucketSize;
71
79
  }
80
+ /**
81
+ * The maxLen function returns the max length of the deque.
82
+ *
83
+ * @return The max length of the deque
84
+ */
85
+ get maxLen() {
86
+ return this._maxLen;
87
+ }
72
88
  /**
73
89
  * The function returns the value of the protected variable `_bucketFirst`.
74
90
  * @returns The value of the `_bucketFirst` property.
@@ -170,6 +186,8 @@ class Deque extends base_1.IterableElementBase {
170
186
  }
171
187
  this._size += 1;
172
188
  this._buckets[this._bucketLast][this._lastInBucket] = element;
189
+ if (this._maxLen > 0 && this._size > this._maxLen)
190
+ this.shift();
173
191
  return true;
174
192
  }
175
193
  /**
@@ -236,6 +254,8 @@ class Deque extends base_1.IterableElementBase {
236
254
  }
237
255
  this._size += 1;
238
256
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
257
+ if (this._maxLen > 0 && this._size > this._maxLen)
258
+ this.pop();
239
259
  return true;
240
260
  }
241
261
  /**
@@ -708,7 +728,7 @@ class Deque extends base_1.IterableElementBase {
708
728
  * elements as the original deque (`this`) and the same bucket size.
709
729
  */
710
730
  clone() {
711
- return new Deque([...this], { bucketSize: this.bucketSize });
731
+ return new Deque(this, { bucketSize: this.bucketSize, toElementFn: this.toElementFn });
712
732
  }
713
733
  /**
714
734
  * Time Complexity: O(n)
@@ -731,7 +751,7 @@ class Deque extends base_1.IterableElementBase {
731
751
  * satisfy the given predicate function.
732
752
  */
733
753
  filter(predicate, thisArg) {
734
- const newDeque = new Deque([], { bucketSize: this._bucketSize });
754
+ const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn: this.toElementFn });
735
755
  let index = 0;
736
756
  for (const el of this) {
737
757
  if (predicate.call(thisArg, el, index, this)) {
@@ -746,20 +766,22 @@ class Deque extends base_1.IterableElementBase {
746
766
  * Space Complexity: O(n)
747
767
  */
748
768
  /**
749
- * Time Complexity: O(n)
750
- * Space Complexity: O(n)
751
- *
752
- * The `map` function creates a new Deque by applying a callback function to each element of the
753
- * original Deque.
754
- * @param callback - The `callback` parameter is a function that will be called for each element in
755
- * the deque. It takes three arguments:
756
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
757
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
758
- * passed as the `this` value to the `callback` function. If `thisArg` is
759
- * @returns a new Deque object with the mapped values.
760
- */
761
- map(callback, thisArg) {
762
- const newDeque = new Deque([], { bucketSize: this._bucketSize });
769
+ * The `map` function takes a callback function and applies it to each element in the deque,
770
+ * returning a new deque with the results.
771
+ * @param callback - The callback parameter is a function that will be called for each element in the
772
+ * deque. It takes three arguments: the current element, the index of the element, and the deque
773
+ * itself. It should return a value of type EM.
774
+ * @param [toElementFn] - The `toElementFn` parameter is an optional function that can be used to
775
+ * transform the raw element (`RM`) into a new element (`EM`) before adding it to the new deque. If
776
+ * provided, this function will be called for each raw element in the original deque.
777
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
778
+ * specify the value of `this` within the callback function. It is used to set the context or scope
779
+ * in which the callback function will be executed. If `thisArg` is provided, it will be used as the
780
+ * value of
781
+ * @returns a new Deque object with elements of type EM and raw elements of type RM.
782
+ */
783
+ map(callback, toElementFn, thisArg) {
784
+ const newDeque = new Deque([], { bucketSize: this._bucketSize, toElementFn });
763
785
  let index = 0;
764
786
  for (const el of this) {
765
787
  newDeque.push(callback.call(thisArg, el, index, this));
@@ -3,7 +3,7 @@
3
3
  * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
- import type { ElementCallback } from '../../types';
6
+ import type { ElementCallback, QueueOptions } from '../../types';
7
7
  import { IterableElementBase } from '../base';
8
8
  import { SinglyLinkedList } from '../linked-list';
9
9
  /**
@@ -15,14 +15,8 @@ import { SinglyLinkedList } from '../linked-list';
15
15
  * 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
16
16
  * 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
17
17
  */
18
- export declare class Queue<E = any> extends IterableElementBase<E> {
19
- /**
20
- * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
21
- * @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
22
- * will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be
23
- * initialized as an empty array.
24
- */
25
- constructor(elements?: Iterable<E>);
18
+ export declare class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
19
+ constructor(elements?: Iterable<E> | Iterable<R>, options?: QueueOptions<E, R>);
26
20
  protected _elements: E[];
27
21
  /**
28
22
  * The elements function returns the elements of this set.
@@ -76,7 +70,6 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
76
70
  *
77
71
  * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
78
72
  * @public
79
- * @static
80
73
  * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
81
74
  * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
82
75
  * array.
@@ -178,7 +171,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
178
171
  * The `clone()` function returns a new Queue object with the same elements as the original Queue.
179
172
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
180
173
  */
181
- clone(): Queue<E>;
174
+ clone(): Queue<E, R>;
182
175
  /**
183
176
  * Time Complexity: O(n)
184
177
  * Space Complexity: O(n)
@@ -199,26 +192,12 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
199
192
  * @returns The `filter` method is returning a new `Queue` object that contains the elements that
200
193
  * satisfy the given predicate function.
201
194
  */
202
- filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Queue<E>;
203
- /**
204
- * Time Complexity: O(n)
205
- * Space Complexity: O(n)
206
- */
195
+ filter(predicate: ElementCallback<E, R, boolean, Queue<E, R>>, thisArg?: any): Queue<E, R>;
207
196
  /**
208
197
  * Time Complexity: O(n)
209
198
  * Space Complexity: O(n)
210
- *
211
- * The `map` function takes a callback function and applies it to each element in the queue,
212
- * returning a new queue with the results.
213
- * @param callback - The callback parameter is a function that will be called for each element in the
214
- * queue. It takes three arguments: the current element, the index of the current element, and the
215
- * queue itself. The callback function should return a new value that will be added to the new queue.
216
- * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
217
- * to be used as `this` when executing the `callback` function. If `thisArg` is provided, it will be
218
- * passed as the `this` value to the `callback` function. If `thisArg` is
219
- * @returns The `map` function is returning a new `Queue` object with the transformed elements.
220
199
  */
221
- map<T>(callback: ElementCallback<E, T>, thisArg?: any): Queue<T>;
200
+ map<EM, RM>(callback: ElementCallback<E, R, EM, Queue<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): Queue<EM, RM>;
222
201
  /**
223
202
  * Time Complexity: O(n)
224
203
  * Space Complexity: O(n)
@@ -237,7 +216,7 @@ export declare class Queue<E = any> extends IterableElementBase<E> {
237
216
  * 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
238
217
  * 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
239
218
  */
240
- export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
219
+ export declare class LinkedListQueue<E = any, R = any> extends SinglyLinkedList<E, R> {
241
220
  /**
242
221
  * Time Complexity: O(n)
243
222
  * Space Complexity: O(n)
@@ -250,5 +229,5 @@ export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
250
229
  * @returns The `clone()` method is returning a new instance of `LinkedListQueue` with the same
251
230
  * values as the original `LinkedListQueue`.
252
231
  */
253
- clone(): LinkedListQueue<E>;
232
+ clone(): LinkedListQueue<E, R>;
254
233
  }