data-structure-typed 1.49.8 → 1.49.9
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/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.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/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
- 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 +36 -15
- 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/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +8 -7
- package/src/data-structures/binary-tree/binary-tree.ts +8 -7
- package/src/data-structures/binary-tree/bst.ts +7 -6
- package/src/data-structures/binary-tree/rb-tree.ts +7 -6
- package/src/data-structures/binary-tree/tree-multimap.ts +7 -6
- package/src/data-structures/graph/abstract-graph.ts +15 -14
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +45 -20
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/src/types/data-structures/hash/hash-map.ts +2 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +14 -0
- package/test/unit/data-structures/queue/deque.test.ts +1 -2
|
@@ -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;
|
|
@@ -13,20 +13,22 @@ 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
|
+
protected _toEntryFn: (rawElement: R) => [K, V];
|
|
22
|
+
get toEntryFn(): (rawElement: R) => [K, V];
|
|
23
|
+
isEntry(rawElement: any): rawElement is [K, V];
|
|
21
24
|
/**
|
|
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:
|
|
25
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
26
|
+
* options.
|
|
27
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
28
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
29
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
28
30
|
*/
|
|
29
|
-
constructor(
|
|
31
|
+
constructor(rawCollection?: Iterable<R>, options?: HashMapOptions<K, V, R>);
|
|
30
32
|
protected _size: number;
|
|
31
33
|
get size(): number;
|
|
32
34
|
isEmpty(): boolean;
|
|
@@ -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.
|
|
@@ -9,24 +9,41 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
9
9
|
export class HashMap extends IterableEntryBase {
|
|
10
10
|
_store = {};
|
|
11
11
|
_objMap = new Map();
|
|
12
|
+
_toEntryFn = (rawElement) => {
|
|
13
|
+
if (this.isEntry(rawElement)) {
|
|
14
|
+
// TODO, For performance optimization, it may be necessary to only inspect the first element traversed.
|
|
15
|
+
return rawElement;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
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.");
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
get toEntryFn() {
|
|
22
|
+
return this._toEntryFn;
|
|
23
|
+
}
|
|
24
|
+
isEntry(rawElement) {
|
|
25
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
26
|
+
}
|
|
12
27
|
/**
|
|
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:
|
|
28
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
29
|
+
* options.
|
|
30
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
31
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
32
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
19
33
|
*/
|
|
20
|
-
constructor(
|
|
34
|
+
constructor(rawCollection = [], options) {
|
|
21
35
|
super();
|
|
22
36
|
if (options) {
|
|
23
|
-
const { hashFn } = options;
|
|
37
|
+
const { hashFn, toEntryFn } = options;
|
|
24
38
|
if (hashFn) {
|
|
25
39
|
this._hashFn = hashFn;
|
|
26
40
|
}
|
|
41
|
+
if (toEntryFn) {
|
|
42
|
+
this._toEntryFn = toEntryFn;
|
|
43
|
+
}
|
|
27
44
|
}
|
|
28
|
-
if (
|
|
29
|
-
this.setMany(
|
|
45
|
+
if (rawCollection) {
|
|
46
|
+
this.setMany(rawCollection);
|
|
30
47
|
}
|
|
31
48
|
}
|
|
32
49
|
_size = 0;
|
|
@@ -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;
|
|
@@ -585,29 +585,46 @@ var dataStructureTyped = (() => {
|
|
|
585
585
|
// src/data-structures/hash/hash-map.ts
|
|
586
586
|
var HashMap = class _HashMap extends IterableEntryBase {
|
|
587
587
|
/**
|
|
588
|
-
* The constructor function initializes a
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
*
|
|
592
|
-
* @param [options] - The `options` parameter is an optional object that can contain
|
|
593
|
-
* configuration options for the constructor. In this case, it has one property:
|
|
588
|
+
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
589
|
+
* options.
|
|
590
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
591
|
+
* `T`. It is an optional parameter and its default value is an empty array `[]`.
|
|
592
|
+
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
594
593
|
*/
|
|
595
|
-
constructor(
|
|
594
|
+
constructor(rawCollection = [], options) {
|
|
596
595
|
super();
|
|
597
596
|
__publicField(this, "_store", {});
|
|
598
597
|
__publicField(this, "_objMap", /* @__PURE__ */ new Map());
|
|
598
|
+
__publicField(this, "_toEntryFn", (rawElement) => {
|
|
599
|
+
if (this.isEntry(rawElement)) {
|
|
600
|
+
return rawElement;
|
|
601
|
+
} else {
|
|
602
|
+
throw new Error(
|
|
603
|
+
"If the provided rawCollection does not adhere to the [key, value] type format, the toEntryFn in the constructor's options parameter needs to specified."
|
|
604
|
+
);
|
|
605
|
+
}
|
|
606
|
+
});
|
|
599
607
|
__publicField(this, "_size", 0);
|
|
600
608
|
__publicField(this, "_hashFn", (key) => String(key));
|
|
601
609
|
if (options) {
|
|
602
|
-
const { hashFn } = options;
|
|
610
|
+
const { hashFn, toEntryFn } = options;
|
|
603
611
|
if (hashFn) {
|
|
604
612
|
this._hashFn = hashFn;
|
|
605
613
|
}
|
|
614
|
+
if (toEntryFn) {
|
|
615
|
+
this._toEntryFn = toEntryFn;
|
|
616
|
+
}
|
|
606
617
|
}
|
|
607
|
-
if (
|
|
608
|
-
this.setMany(
|
|
618
|
+
if (rawCollection) {
|
|
619
|
+
this.setMany(rawCollection);
|
|
609
620
|
}
|
|
610
621
|
}
|
|
622
|
+
get toEntryFn() {
|
|
623
|
+
return this._toEntryFn;
|
|
624
|
+
}
|
|
625
|
+
isEntry(rawElement) {
|
|
626
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
627
|
+
}
|
|
611
628
|
get size() {
|
|
612
629
|
return this._size;
|
|
613
630
|
}
|
|
@@ -644,14 +661,18 @@ var dataStructureTyped = (() => {
|
|
|
644
661
|
return true;
|
|
645
662
|
}
|
|
646
663
|
/**
|
|
647
|
-
* The function
|
|
648
|
-
*
|
|
649
|
-
*
|
|
664
|
+
* The function `setMany` takes an iterable collection of objects, maps each object to a key-value
|
|
665
|
+
* pair using a mapping function, and sets each key-value pair in the current object.
|
|
666
|
+
* @param rawCollection - The `rawCollection` parameter is an iterable collection of elements of type
|
|
667
|
+
* `T`.
|
|
668
|
+
* @returns The `setMany` function is returning an array of booleans.
|
|
650
669
|
*/
|
|
651
|
-
setMany(
|
|
670
|
+
setMany(rawCollection) {
|
|
652
671
|
const results = [];
|
|
653
|
-
for (const
|
|
672
|
+
for (const rawEle of rawCollection) {
|
|
673
|
+
const [key, value] = this.toEntryFn(rawEle);
|
|
654
674
|
results.push(this.set(key, value));
|
|
675
|
+
}
|
|
655
676
|
return results;
|
|
656
677
|
}
|
|
657
678
|
/**
|