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.
Files changed (54) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/benchmark/report.html +1 -37
  3. package/benchmark/report.json +22 -370
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
  11. package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
  12. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  19. package/dist/cjs/data-structures/hash/hash-map.d.ts +16 -12
  20. package/dist/cjs/data-structures/hash/hash-map.js +36 -15
  21. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  22. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
  23. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
  25. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
  27. package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
  28. package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
  29. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
  31. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
  32. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
  33. package/dist/mjs/data-structures/hash/hash-map.d.ts +16 -12
  34. package/dist/mjs/data-structures/hash/hash-map.js +36 -15
  35. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +2 -1
  36. package/dist/umd/data-structure-typed.js +176 -165
  37. package/dist/umd/data-structure-typed.min.js +2 -2
  38. package/dist/umd/data-structure-typed.min.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/data-structures/binary-tree/avl-tree.ts +0 -10
  41. package/src/data-structures/binary-tree/binary-tree.ts +104 -141
  42. package/src/data-structures/binary-tree/bst.ts +153 -49
  43. package/src/data-structures/binary-tree/rb-tree.ts +1 -11
  44. package/src/data-structures/binary-tree/tree-multimap.ts +2 -12
  45. package/src/data-structures/hash/hash-map.ts +42 -16
  46. package/src/types/data-structures/hash/hash-map.ts +2 -1
  47. package/test/integration/all-in-one.test.ts +1 -1
  48. package/test/integration/index.html +2 -2
  49. package/test/performance/data-structures/queue/deque.test.ts +8 -2
  50. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  51. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
  52. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
  53. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
  54. 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.isNotNodeInstance(keyOrNodeOrEntry)) {
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.subTreeTraverse(node => (sum += node.count));
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.isNotNodeInstance(keyOrNodeOrEntry)) {
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 new instance of a class with optional entries and options.
23
- * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
24
- * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
25
- * key-value pairs.
26
- * @param [options] - The `options` parameter is an optional object that can contain additional
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(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
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 "setMany" sets multiple key-value pairs in a map.
46
- * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
47
- * key-value pair is represented as an array with two entries: the key and the value.
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(entries: Iterable<[K, V]>): boolean[];
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 new instance of a class with optional entries and options.
14
- * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
15
- * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
16
- * key-value pairs.
17
- * @param [options] - The `options` parameter is an optional object that can contain additional
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(entries = [], options) {
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 (entries) {
29
- this.setMany(entries);
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 "setMany" sets multiple key-value pairs in a map.
71
- * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
72
- * key-value pair is represented as an array with two entries: the key and the value.
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(entries) {
93
+ setMany(rawCollection) {
75
94
  const results = [];
76
- for (const [key, value] of entries)
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;