doubly-linked-list-typed 1.51.8 → 1.52.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.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -226
- package/dist/data-structures/binary-tree/binary-tree.js +475 -363
- package/dist/data-structures/binary-tree/bst.d.ts +192 -202
- package/dist/data-structures/binary-tree/bst.js +207 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +21 -20
- package/dist/data-structures/queue/deque.js +29 -23
- package/dist/data-structures/queue/queue.d.ts +8 -28
- package/dist/data-structures/queue/queue.js +15 -31
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +144 -95
- package/src/data-structures/binary-tree/avl-tree.ts +96 -69
- package/src/data-structures/binary-tree/binary-tree.ts +535 -403
- package/src/data-structures/binary-tree/bst.ts +247 -277
- package/src/data-structures/binary-tree/rb-tree.ts +123 -103
- package/src/data-structures/binary-tree/tree-multi-map.ts +127 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +37 -26
- package/src/data-structures/queue/queue.ts +23 -36
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +9 -9
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/bst.ts +4 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +3 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { RBTreeOptions } from './rb-tree';
|
|
3
|
-
|
|
4
|
-
export type
|
|
5
|
-
export type
|
|
6
|
-
export type TreeMultiMapOptions<K> = RBTreeOptions<K> & {};
|
|
3
|
+
export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type TreeMultiMapNested<K, V, R, NODE extends TreeMultiMapNode<K, V, NODE>> = TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, TreeMultiMap<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type TreeMultiMapOptions<K, V, R> = RBTreeOptions<K, V, R> & {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { IterableElementBaseOptions } from '../base';
|
|
2
|
+
export type DoublyLinkedListOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { IterableElementBaseOptions } from '../base';
|
|
2
|
+
export type SinglyLinkedListOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { HeapOptions } from '../heap';
|
|
2
|
-
export type PriorityQueueOptions<
|
|
2
|
+
export type PriorityQueueOptions<E, R> = HeapOptions<E, R> & {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { IterableElementBaseOptions } from '../base';
|
|
2
|
+
export type QueueOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { IterableElementBaseOptions } from '../base';
|
|
2
|
+
export type StackOptions<E, R> = IterableElementBaseOptions<E, R> & {};
|
package/dist/utils/utils.js
CHANGED
|
@@ -95,7 +95,7 @@ exports.roundFixed = roundFixed;
|
|
|
95
95
|
function isComparable(key) {
|
|
96
96
|
const keyType = typeof key;
|
|
97
97
|
if (keyType === 'number')
|
|
98
|
-
return isNaN(key);
|
|
98
|
+
return !isNaN(key);
|
|
99
99
|
if (keyType === 'string')
|
|
100
100
|
return true;
|
|
101
101
|
if (keyType === 'bigint')
|
|
@@ -111,10 +111,8 @@ function isComparable(key) {
|
|
|
111
111
|
if (keyType === 'object') {
|
|
112
112
|
if (key === null)
|
|
113
113
|
return true;
|
|
114
|
-
if (typeof key.valueOf === 'function')
|
|
115
|
-
|
|
116
|
-
if (typeof key.toString === 'function')
|
|
117
|
-
return isComparable(key.toString());
|
|
114
|
+
// if (typeof key.valueOf === 'function') return isComparable(key.valueOf()); // This will keep recursing because every object has a valueOf method.
|
|
115
|
+
// if (typeof key.toString === 'function') return isComparable(key.toString()); // This will also keep recursing because every string type has a toString method.
|
|
118
116
|
return false;
|
|
119
117
|
}
|
|
120
118
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doubly-linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.52.0",
|
|
4
4
|
"description": "Doubly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -64,6 +64,6 @@
|
|
|
64
64
|
"typescript": "^4.9.5"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"data-structure-typed": "^1.
|
|
67
|
+
"data-structure-typed": "^1.52.0"
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './iterable-base';
|
|
1
|
+
export * from './iterable-entry-base';
|
|
2
|
+
export * from './iterable-element-base';
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
|
|
2
|
+
|
|
3
|
+
export abstract class IterableElementBase<E, R, C> {
|
|
4
|
+
/**
|
|
5
|
+
* The protected constructor initializes the options for the IterableElementBase class, including the
|
|
6
|
+
* toElementFn function.
|
|
7
|
+
* @param [options] - An optional object that contains the following properties:
|
|
8
|
+
*/
|
|
9
|
+
protected constructor(options?: IterableElementBaseOptions<E, R>) {
|
|
10
|
+
if (options) {
|
|
11
|
+
const { toElementFn } = options;
|
|
12
|
+
if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
|
|
13
|
+
else if (toElementFn) throw new TypeError('toElementFn must be a function type');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
abstract get size(): number;
|
|
18
|
+
|
|
19
|
+
protected _toElementFn?: (rawElement: R) => E;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The function returns the _toElementFn property, which is a function that converts a raw element to
|
|
23
|
+
* a specific type.
|
|
24
|
+
* @returns The function `get toElementFn()` is returning either a function that takes a raw element
|
|
25
|
+
* `rawElement` of type `R` and returns an element `E`, or `undefined` if no function is assigned to
|
|
26
|
+
* `_toElementFn`.
|
|
27
|
+
*/
|
|
28
|
+
get toElementFn(): ((rawElement: R) => E) | undefined {
|
|
29
|
+
return this._toElementFn;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Time Complexity: O(n)
|
|
34
|
+
* Space Complexity: O(1)
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* Time Complexity: O(n)
|
|
38
|
+
* Space Complexity: O(1)
|
|
39
|
+
*
|
|
40
|
+
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
41
|
+
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
42
|
+
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
43
|
+
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
44
|
+
*/
|
|
45
|
+
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
|
|
46
|
+
yield* this._getIterator(...args);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Time Complexity: O(n)
|
|
51
|
+
* Space Complexity: O(n)
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(n)
|
|
55
|
+
* Space Complexity: O(n)
|
|
56
|
+
*
|
|
57
|
+
* The function returns an iterator that yields all the values in the object.
|
|
58
|
+
*/
|
|
59
|
+
* values(): IterableIterator<E> {
|
|
60
|
+
for (const item of this) {
|
|
61
|
+
yield item;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Time Complexity: O(n)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*/
|
|
69
|
+
/**
|
|
70
|
+
* Time Complexity: O(n)
|
|
71
|
+
* Space Complexity: O(1)
|
|
72
|
+
*
|
|
73
|
+
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
74
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
75
|
+
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
76
|
+
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
77
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
78
|
+
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
79
|
+
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
80
|
+
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
81
|
+
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
82
|
+
*/
|
|
83
|
+
every(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
|
|
84
|
+
let index = 0;
|
|
85
|
+
for (const item of this) {
|
|
86
|
+
if (!predicate.call(thisArg, item, index++, this)) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Time Complexity: O(n)
|
|
95
|
+
* Space Complexity: O(1)
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Time Complexity: O(n)
|
|
100
|
+
* Space Complexity: O(1)
|
|
101
|
+
*/
|
|
102
|
+
/**
|
|
103
|
+
* Time Complexity: O(n)
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
106
|
+
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
107
|
+
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
108
|
+
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
109
|
+
* element satisfies the condition.
|
|
110
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
111
|
+
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
112
|
+
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
113
|
+
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
114
|
+
* in the collection, and false otherwise.
|
|
115
|
+
*/
|
|
116
|
+
some(predicate: ElementCallback<E, R, boolean, C>, thisArg?: any): boolean {
|
|
117
|
+
let index = 0;
|
|
118
|
+
for (const item of this) {
|
|
119
|
+
if (predicate.call(thisArg, item, index++, this)) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Time Complexity: O(n)
|
|
128
|
+
* Space Complexity: O(1)
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Time Complexity: O(n)
|
|
133
|
+
* Space Complexity: O(1)
|
|
134
|
+
*
|
|
135
|
+
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
136
|
+
* function for each element.
|
|
137
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
138
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
139
|
+
* element, and the array that forEach was called upon.
|
|
140
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
141
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
142
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
143
|
+
*/
|
|
144
|
+
forEach(callbackfn: ElementCallback<E, R, void, C>, thisArg?: any): void {
|
|
145
|
+
let index = 0;
|
|
146
|
+
for (const item of this) {
|
|
147
|
+
callbackfn.call(thisArg, item, index++, this);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Time Complexity: O(n)
|
|
153
|
+
* Space Complexity: O(1)
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(n)
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*
|
|
160
|
+
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
161
|
+
* element that satisfies the provided callback function.
|
|
162
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
163
|
+
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
164
|
+
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
165
|
+
* current element matches the desired condition.
|
|
166
|
+
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
167
|
+
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
168
|
+
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
169
|
+
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
170
|
+
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
171
|
+
*/
|
|
172
|
+
find(callbackfn: ElementCallback<E, R, boolean, C>, thisArg?: any): E | undefined {
|
|
173
|
+
let index = 0;
|
|
174
|
+
for (const item of this) {
|
|
175
|
+
if (callbackfn.call(thisArg, item, index++, this)) return item;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Time Complexity: O(n)
|
|
183
|
+
* Space Complexity: O(1)
|
|
184
|
+
*
|
|
185
|
+
* The function checks if a given element exists in a collection.
|
|
186
|
+
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
187
|
+
* represents the element that we want to check for existence in the collection.
|
|
188
|
+
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
189
|
+
* otherwise.
|
|
190
|
+
*/
|
|
191
|
+
has(element: E): boolean {
|
|
192
|
+
for (const ele of this) {
|
|
193
|
+
if (ele === element) return true;
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Time Complexity: O(n)
|
|
200
|
+
* Space Complexity: O(1)
|
|
201
|
+
*/
|
|
202
|
+
/**
|
|
203
|
+
* Time Complexity: O(n)
|
|
204
|
+
* Space Complexity: O(1)
|
|
205
|
+
*
|
|
206
|
+
* The `reduce` function iterates over the elements of an array-like object and applies a callback
|
|
207
|
+
* function to reduce them into a single value.
|
|
208
|
+
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
209
|
+
* the array. It takes four arguments:
|
|
210
|
+
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
211
|
+
* is the value that the accumulator starts with before the reduction operation begins.
|
|
212
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
213
|
+
* all the elements in the array and applying the callback function to each element.
|
|
214
|
+
*/
|
|
215
|
+
reduce<U>(callbackfn: ReduceElementCallback<E, R, U, C>, initialValue: U): U {
|
|
216
|
+
let accumulator = initialValue;
|
|
217
|
+
let index = 0;
|
|
218
|
+
for (const item of this) {
|
|
219
|
+
accumulator = callbackfn(accumulator, item as E, index++, this);
|
|
220
|
+
}
|
|
221
|
+
return accumulator;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Time Complexity: O(n)
|
|
226
|
+
* Space Complexity: O(n)
|
|
227
|
+
*/
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Time Complexity: O(n)
|
|
231
|
+
* Space Complexity: O(n)
|
|
232
|
+
*
|
|
233
|
+
* The print function logs the elements of an array to the console.
|
|
234
|
+
*/
|
|
235
|
+
print(): void {
|
|
236
|
+
console.log([...this]);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
abstract isEmpty(): boolean;
|
|
240
|
+
|
|
241
|
+
abstract clear(): void;
|
|
242
|
+
|
|
243
|
+
abstract clone(): C;
|
|
244
|
+
|
|
245
|
+
abstract map(...args: any[]): any;
|
|
246
|
+
|
|
247
|
+
abstract filter(...args: any[]): any;
|
|
248
|
+
|
|
249
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
|
|
250
|
+
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntryCallback, ReduceEntryCallback } from '../../types';
|
|
2
2
|
|
|
3
3
|
export abstract class IterableEntryBase<K = any, V = any> {
|
|
4
|
+
// protected constructor(options?: IterableEntryBaseOptions<K, V, R>) {
|
|
5
|
+
// if (options) {
|
|
6
|
+
// const { toEntryFn } = options;
|
|
7
|
+
// if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn
|
|
8
|
+
// else throw new TypeError('toEntryFn must be a function type');
|
|
9
|
+
// }
|
|
10
|
+
// }
|
|
11
|
+
|
|
4
12
|
/**
|
|
5
13
|
* Time Complexity: O(n)
|
|
6
14
|
* Space Complexity: O(1)
|
|
@@ -8,6 +16,16 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
8
16
|
|
|
9
17
|
abstract get size(): number;
|
|
10
18
|
|
|
19
|
+
// protected _toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
|
|
20
|
+
//
|
|
21
|
+
// /**
|
|
22
|
+
// * The function returns the value of the _toEntryFn property.
|
|
23
|
+
// * @returns The function being returned is `this._toEntryFn`.
|
|
24
|
+
// */
|
|
25
|
+
// get toEntryFn() {
|
|
26
|
+
// return this._toEntryFn;
|
|
27
|
+
// }
|
|
28
|
+
|
|
11
29
|
/**
|
|
12
30
|
* Time Complexity: O(n)
|
|
13
31
|
* Space Complexity: O(1)
|
|
@@ -285,221 +303,12 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
285
303
|
* Time Complexity: O(n)
|
|
286
304
|
* Space Complexity: O(n)
|
|
287
305
|
*/
|
|
288
|
-
print(): void {
|
|
289
|
-
console.log([...this]);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
abstract isEmpty(): boolean;
|
|
293
|
-
|
|
294
|
-
abstract clear(): void;
|
|
295
|
-
|
|
296
|
-
abstract clone(): any;
|
|
297
|
-
|
|
298
|
-
abstract map(...args: any[]): any;
|
|
299
|
-
|
|
300
|
-
abstract filter(...args: any[]): any;
|
|
301
|
-
|
|
302
|
-
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export abstract class IterableElementBase<E = any, C = any> {
|
|
306
|
-
abstract get size(): number;
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(n)
|
|
310
|
-
* Space Complexity: O(1)
|
|
311
|
-
*/
|
|
312
|
-
/**
|
|
313
|
-
* Time Complexity: O(n)
|
|
314
|
-
* Space Complexity: O(1)
|
|
315
|
-
*
|
|
316
|
-
* The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
|
|
317
|
-
* @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
|
|
318
|
-
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
319
|
-
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
320
|
-
*/
|
|
321
|
-
* [Symbol.iterator](...args: any[]): IterableIterator<E> {
|
|
322
|
-
yield* this._getIterator(...args);
|
|
323
|
-
}
|
|
324
306
|
|
|
325
307
|
/**
|
|
326
308
|
* Time Complexity: O(n)
|
|
327
309
|
* Space Complexity: O(n)
|
|
328
|
-
*/
|
|
329
|
-
/**
|
|
330
|
-
* Time Complexity: O(n)
|
|
331
|
-
* Space Complexity: O(n)
|
|
332
|
-
*
|
|
333
|
-
* The function returns an iterator that yields all the values in the object.
|
|
334
|
-
*/
|
|
335
|
-
* values(): IterableIterator<E> {
|
|
336
|
-
for (const item of this) {
|
|
337
|
-
yield item;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Time Complexity: O(n)
|
|
343
|
-
* Space Complexity: O(1)
|
|
344
|
-
*/
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(n)
|
|
347
|
-
* Space Complexity: O(1)
|
|
348
|
-
*
|
|
349
|
-
* The `every` function checks if every element in the array satisfies a given predicate.
|
|
350
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
351
|
-
* the current element being processed, its index, and the array it belongs to. It should return a
|
|
352
|
-
* boolean value indicating whether the element satisfies a certain condition or not.
|
|
353
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
354
|
-
* to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
|
|
355
|
-
* passed as the `this` value to the `predicate` function. If `thisArg` is
|
|
356
|
-
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
357
|
-
* the array satisfies the provided predicate function, and `false` otherwise.
|
|
358
|
-
*/
|
|
359
|
-
every(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
|
360
|
-
let index = 0;
|
|
361
|
-
for (const item of this) {
|
|
362
|
-
if (!predicate.call(thisArg, item, index++, this)) {
|
|
363
|
-
return false;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Time Complexity: O(n)
|
|
371
|
-
* Space Complexity: O(1)
|
|
372
|
-
*/
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* Time Complexity: O(n)
|
|
376
|
-
* Space Complexity: O(1)
|
|
377
|
-
*/
|
|
378
|
-
/**
|
|
379
|
-
* Time Complexity: O(n)
|
|
380
|
-
* Space Complexity: O(1)
|
|
381
|
-
*
|
|
382
|
-
* The "some" function checks if at least one element in a collection satisfies a given predicate.
|
|
383
|
-
* @param predicate - The `predicate` parameter is a callback function that takes three arguments:
|
|
384
|
-
* `value`, `index`, and `array`. It should return a boolean value indicating whether the current
|
|
385
|
-
* element satisfies the condition.
|
|
386
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
387
|
-
* to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
|
|
388
|
-
* it will be passed as the `this` value to the `predicate` function. If `thisArg
|
|
389
|
-
* @returns a boolean value. It returns true if the predicate function returns true for any element
|
|
390
|
-
* in the collection, and false otherwise.
|
|
391
|
-
*/
|
|
392
|
-
some(predicate: ElementCallback<E, boolean>, thisArg?: any): boolean {
|
|
393
|
-
let index = 0;
|
|
394
|
-
for (const item of this) {
|
|
395
|
-
if (predicate.call(thisArg, item, index++, this)) {
|
|
396
|
-
return true;
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return false;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(n)
|
|
404
|
-
* Space Complexity: O(1)
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* Time Complexity: O(n)
|
|
409
|
-
* Space Complexity: O(1)
|
|
410
|
-
*
|
|
411
|
-
* The `forEach` function iterates over each element in an array-like object and calls a callback
|
|
412
|
-
* function for each element.
|
|
413
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
414
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
415
|
-
* element, and the array that forEach was called upon.
|
|
416
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
417
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
418
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
419
|
-
*/
|
|
420
|
-
forEach(callbackfn: ElementCallback<E, void>, thisArg?: any): void {
|
|
421
|
-
let index = 0;
|
|
422
|
-
for (const item of this) {
|
|
423
|
-
callbackfn.call(thisArg, item, index++, this);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/**
|
|
428
|
-
* Time Complexity: O(n)
|
|
429
|
-
* Space Complexity: O(1)
|
|
430
|
-
*/
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Time Complexity: O(n)
|
|
434
|
-
* Space Complexity: O(1)
|
|
435
|
-
*
|
|
436
|
-
* The `find` function iterates over the elements of an array-like object and returns the first
|
|
437
|
-
* element that satisfies the provided callback function.
|
|
438
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
439
|
-
* the array. It takes three arguments: the current element being processed, the index of the current
|
|
440
|
-
* element, and the array itself. The function should return a boolean value indicating whether the
|
|
441
|
-
* current element matches the desired condition.
|
|
442
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
|
|
443
|
-
* to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
|
|
444
|
-
* be passed as the `this` value to the `callbackfn` function. If `thisArg
|
|
445
|
-
* @returns The `find` method returns the first element in the array that satisfies the provided
|
|
446
|
-
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
447
|
-
*/
|
|
448
|
-
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined {
|
|
449
|
-
let index = 0;
|
|
450
|
-
for (const item of this) {
|
|
451
|
-
if (callbackfn.call(thisArg, item, index++, this)) return item;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
/**
|
|
458
|
-
* Time Complexity: O(n)
|
|
459
|
-
* Space Complexity: O(1)
|
|
460
|
-
*
|
|
461
|
-
* The function checks if a given element exists in a collection.
|
|
462
|
-
* @param {E} element - The parameter "element" is of type E, which means it can be any type. It
|
|
463
|
-
* represents the element that we want to check for existence in the collection.
|
|
464
|
-
* @returns a boolean value. It returns true if the element is found in the collection, and false
|
|
465
|
-
* otherwise.
|
|
466
|
-
*/
|
|
467
|
-
has(element: E): boolean {
|
|
468
|
-
for (const ele of this) {
|
|
469
|
-
if (ele === element) return true;
|
|
470
|
-
}
|
|
471
|
-
return false;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Time Complexity: O(n)
|
|
476
|
-
* Space Complexity: O(1)
|
|
477
|
-
*/
|
|
478
|
-
/**
|
|
479
|
-
* Time Complexity: O(n)
|
|
480
|
-
* Space Complexity: O(1)
|
|
481
310
|
*
|
|
482
|
-
* The
|
|
483
|
-
* function to reduce them into a single value.
|
|
484
|
-
* @param callbackfn - The callbackfn parameter is a function that will be called for each element in
|
|
485
|
-
* the array. It takes four arguments:
|
|
486
|
-
* @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
|
|
487
|
-
* is the value that the accumulator starts with before the reduction operation begins.
|
|
488
|
-
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
489
|
-
* all the elements in the array and applying the callback function to each element.
|
|
490
|
-
*/
|
|
491
|
-
reduce<U>(callbackfn: ReduceElementCallback<E, U>, initialValue: U): U {
|
|
492
|
-
let accumulator = initialValue;
|
|
493
|
-
let index = 0;
|
|
494
|
-
for (const item of this) {
|
|
495
|
-
accumulator = callbackfn(accumulator, item as E, index++, this);
|
|
496
|
-
}
|
|
497
|
-
return accumulator;
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Time Complexity: O(n)
|
|
502
|
-
* Space Complexity: O(n)
|
|
311
|
+
* The print function logs the elements of an array to the console.
|
|
503
312
|
*/
|
|
504
313
|
print(): void {
|
|
505
314
|
console.log([...this]);
|
|
@@ -509,11 +318,11 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
509
318
|
|
|
510
319
|
abstract clear(): void;
|
|
511
320
|
|
|
512
|
-
abstract clone():
|
|
321
|
+
abstract clone(): any;
|
|
513
322
|
|
|
514
323
|
abstract map(...args: any[]): any;
|
|
515
324
|
|
|
516
325
|
abstract filter(...args: any[]): any;
|
|
517
326
|
|
|
518
|
-
protected abstract _getIterator(...args: any[]): IterableIterator<
|
|
327
|
+
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
519
328
|
}
|