data-structure-typed 1.49.8 → 1.50.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/CHANGELOG.md +1 -1
- package/benchmark/report.html +1 -37
- package/benchmark/report.json +22 -370
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/cjs/data-structures/hash/hash-map.js +36 -15
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/mjs/data-structures/hash/hash-map.d.ts +16 -12
- package/dist/mjs/data-structures/hash/hash-map.js +36 -15
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +176 -165
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +0 -10
- package/src/data-structures/binary-tree/binary-tree.ts +104 -141
- package/src/data-structures/binary-tree/bst.ts +153 -49
- package/src/data-structures/binary-tree/rb-tree.ts +1 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +2 -12
- package/src/data-structures/hash/hash-map.ts +42 -16
- package/src/types/data-structures/hash/hash-map.ts +2 -1
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/queue/deque.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +17 -1
|
@@ -79,13 +79,6 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
79
79
|
* Space Complexity: O(1)
|
|
80
80
|
*/
|
|
81
81
|
isRealNode(node: N | undefined): node is N;
|
|
82
|
-
/**
|
|
83
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
84
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
85
|
-
* data type.
|
|
86
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
87
|
-
*/
|
|
88
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
89
82
|
/**
|
|
90
83
|
* Time Complexity: O(log n)
|
|
91
84
|
* Space Complexity: O(1)
|
|
@@ -101,7 +101,7 @@ export class RedBlackTree extends BST {
|
|
|
101
101
|
node = this.createNode(key, value, RBTNColor.RED);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
else if (this.
|
|
104
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
105
105
|
node = this.createNode(keyOrNodeOrEntry, value, RBTNColor.RED);
|
|
106
106
|
}
|
|
107
107
|
else {
|
|
@@ -127,15 +127,6 @@ export class RedBlackTree extends BST {
|
|
|
127
127
|
return false;
|
|
128
128
|
return node instanceof RedBlackTreeNode;
|
|
129
129
|
}
|
|
130
|
-
/**
|
|
131
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
132
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
133
|
-
* data type.
|
|
134
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
135
|
-
*/
|
|
136
|
-
isNotNodeInstance(potentialKey) {
|
|
137
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
138
|
-
}
|
|
139
130
|
/**
|
|
140
131
|
* Time Complexity: O(log n)
|
|
141
132
|
* Space Complexity: O(1)
|
|
@@ -60,13 +60,6 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
60
60
|
* class.
|
|
61
61
|
*/
|
|
62
62
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
63
|
-
/**
|
|
64
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
-
* data type.
|
|
67
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
-
*/
|
|
69
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
70
63
|
/**
|
|
71
64
|
* Time Complexity: O(log n)
|
|
72
65
|
* Space Complexity: O(1)
|
|
@@ -30,7 +30,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
30
30
|
// TODO the _count is not accurate after nodes count modified
|
|
31
31
|
get count() {
|
|
32
32
|
let sum = 0;
|
|
33
|
-
this.
|
|
33
|
+
this.dfs(node => (sum += node.count));
|
|
34
34
|
return sum;
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
@@ -80,7 +80,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
80
80
|
node = this.createNode(key, value, count);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
else if (this.
|
|
83
|
+
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
84
84
|
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
85
85
|
}
|
|
86
86
|
else {
|
|
@@ -97,15 +97,6 @@ export class TreeMultimap extends AVLTree {
|
|
|
97
97
|
isNode(keyOrNodeOrEntry) {
|
|
98
98
|
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
99
99
|
}
|
|
100
|
-
/**
|
|
101
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
102
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
103
|
-
* data type.
|
|
104
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
105
|
-
*/
|
|
106
|
-
isNotNodeInstance(potentialKey) {
|
|
107
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
108
|
-
}
|
|
109
100
|
/**
|
|
110
101
|
* Time Complexity: O(log n)
|
|
111
102
|
* Space Complexity: O(1)
|
|
@@ -13,22 +13,24 @@ import { IterableEntryBase } from '../base';
|
|
|
13
13
|
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
|
14
14
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
15
15
|
*/
|
|
16
|
-
export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
16
|
+
export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
17
17
|
protected _store: {
|
|
18
18
|
[key: string]: HashMapStoreItem<K, V>;
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
21
|
/**
|
|
22
|
-
* The constructor function initializes a
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
27
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
22
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
23
|
+
* options.
|
|
24
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
25
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
26
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
28
27
|
*/
|
|
29
|
-
constructor(
|
|
28
|
+
constructor(rawCollection?: Iterable<R>, options?: HashMapOptions<K, V, R>);
|
|
29
|
+
protected _toEntryFn: (rawElement: R) => [K, V];
|
|
30
|
+
get toEntryFn(): (rawElement: R) => [K, V];
|
|
30
31
|
protected _size: number;
|
|
31
32
|
get size(): number;
|
|
33
|
+
isEntry(rawElement: any): rawElement is [K, V];
|
|
32
34
|
isEmpty(): boolean;
|
|
33
35
|
clear(): void;
|
|
34
36
|
/**
|
|
@@ -42,11 +44,13 @@ export declare class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|
|
42
44
|
*/
|
|
43
45
|
set(key: K, value: V): boolean;
|
|
44
46
|
/**
|
|
45
|
-
* The function
|
|
46
|
-
*
|
|
47
|
-
*
|
|
47
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
48
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
49
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
50
|
+
* `T`.
|
|
51
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
48
52
|
*/
|
|
49
|
-
setMany(
|
|
53
|
+
setMany(rawCollection: Iterable<R>): boolean[];
|
|
50
54
|
/**
|
|
51
55
|
* The `get` function retrieves a value from a map based on a given key, either from an object map or
|
|
52
56
|
* a string map.
|
|
@@ -10,29 +10,46 @@ export class HashMap extends IterableEntryBase {
|
|
|
10
10
|
_store = {};
|
|
11
11
|
_objMap = new Map();
|
|
12
12
|
/**
|
|
13
|
-
* The constructor function initializes a
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
18
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
13
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
14
|
+
* options.
|
|
15
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
16
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
17
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
19
18
|
*/
|
|
20
|
-
constructor(
|
|
19
|
+
constructor(rawCollection = [], options) {
|
|
21
20
|
super();
|
|
22
21
|
if (options) {
|
|
23
|
-
const { hashFn } = options;
|
|
22
|
+
const { hashFn, toEntryFn } = options;
|
|
24
23
|
if (hashFn) {
|
|
25
24
|
this._hashFn = hashFn;
|
|
26
25
|
}
|
|
26
|
+
if (toEntryFn) {
|
|
27
|
+
this._toEntryFn = toEntryFn;
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
|
-
if (
|
|
29
|
-
this.setMany(
|
|
30
|
+
if (rawCollection) {
|
|
31
|
+
this.setMany(rawCollection);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
34
|
+
_toEntryFn = (rawElement) => {
|
|
35
|
+
if (this.isEntry(rawElement)) {
|
|
36
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
37
|
+
return rawElement;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
throw new Error("If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified.");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
get toEntryFn() {
|
|
44
|
+
return this._toEntryFn;
|
|
45
|
+
}
|
|
32
46
|
_size = 0;
|
|
33
47
|
get size() {
|
|
34
48
|
return this._size;
|
|
35
49
|
}
|
|
50
|
+
isEntry(rawElement) {
|
|
51
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
52
|
+
}
|
|
36
53
|
isEmpty() {
|
|
37
54
|
return this.size === 0;
|
|
38
55
|
}
|
|
@@ -67,14 +84,18 @@ export class HashMap extends IterableEntryBase {
|
|
|
67
84
|
return true;
|
|
68
85
|
}
|
|
69
86
|
/**
|
|
70
|
-
* The function
|
|
71
|
-
*
|
|
72
|
-
*
|
|
87
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
88
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
89
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
90
|
+
* `T`.
|
|
91
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
73
92
|
*/
|
|
74
|
-
setMany(
|
|
93
|
+
setMany(rawCollection) {
|
|
75
94
|
const results = [];
|
|
76
|
-
for (const
|
|
95
|
+
for (const rawEle of rawCollection) {
|
|
96
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
77
97
|
results.push(this.set(key, value));
|
|
98
|
+
}
|
|
78
99
|
return results;
|
|
79
100
|
}
|
|
80
101
|
/**
|
|
@@ -8,8 +8,9 @@ export type LinkedHashMapOptions<K> = {
|
|
|
8
8
|
hashFn?: (key: K) => string;
|
|
9
9
|
objHashFn?: (key: K) => object;
|
|
10
10
|
};
|
|
11
|
-
export type HashMapOptions<K> = {
|
|
11
|
+
export type HashMapOptions<K, V, T> = {
|
|
12
12
|
hashFn?: (key: K) => string;
|
|
13
|
+
toEntryFn?: (rawElement: T) => [K, V];
|
|
13
14
|
};
|
|
14
15
|
export type HashMapStoreItem<K, V> = {
|
|
15
16
|
key: K;
|