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.
Files changed (50) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  4. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  8. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  10. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  11. package/dist/cjs/data-structures/hash/hash-map.d.ts +16 -12
  12. package/dist/cjs/data-structures/hash/hash-map.js +36 -15
  13. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  14. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  15. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  16. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  17. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  18. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  19. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  20. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  21. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +2 -1
  22. package/dist/mjs/data-structures/hash/hash-map.d.ts +16 -12
  23. package/dist/mjs/data-structures/hash/hash-map.js +36 -15
  24. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +2 -1
  25. package/dist/umd/data-structure-typed.js +36 -15
  26. package/dist/umd/data-structure-typed.min.js +2 -2
  27. package/dist/umd/data-structure-typed.min.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/data-structures/base/iterable-base.ts +6 -6
  30. package/src/data-structures/binary-tree/avl-tree.ts +8 -7
  31. package/src/data-structures/binary-tree/binary-tree.ts +8 -7
  32. package/src/data-structures/binary-tree/bst.ts +7 -6
  33. package/src/data-structures/binary-tree/rb-tree.ts +7 -6
  34. package/src/data-structures/binary-tree/tree-multimap.ts +7 -6
  35. package/src/data-structures/graph/abstract-graph.ts +15 -14
  36. package/src/data-structures/graph/directed-graph.ts +7 -6
  37. package/src/data-structures/graph/undirected-graph.ts +7 -6
  38. package/src/data-structures/hash/hash-map.ts +45 -20
  39. package/src/data-structures/heap/heap.ts +1 -1
  40. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  41. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  42. package/src/data-structures/queue/deque.ts +3 -3
  43. package/src/data-structures/queue/queue.ts +1 -1
  44. package/src/data-structures/stack/stack.ts +1 -1
  45. package/src/data-structures/trie/trie.ts +1 -1
  46. package/src/types/data-structures/graph/abstract-graph.ts +8 -8
  47. package/src/types/data-structures/hash/hash-map.ts +2 -1
  48. package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -2
  49. package/test/unit/data-structures/hash/hash-map.test.ts +14 -0
  50. 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 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:
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(entries?: Iterable<[K, V]>, options?: HashMapOptions<K>);
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 "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.
@@ -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 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:
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(entries = [], options) {
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 (entries) {
29
- this.setMany(entries);
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 "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;
@@ -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 new instance of a class with optional entries and options.
589
- * @param entries - The `entries` parameter is an iterable containing key-value pairs `[K, V]`. It
590
- * is optional and defaults to an empty array `[]`. This parameter is used to initialize the map with
591
- * key-value pairs.
592
- * @param [options] - The `options` parameter is an optional object that can contain additional
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(entries = [], options) {
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 (entries) {
608
- this.setMany(entries);
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 "setMany" sets multiple key-value pairs in a map.
648
- * @param entries - The `entries` parameter is an iterable containing key-value pairs. Each
649
- * key-value pair is represented as an array with two entries: the key and the value.
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(entries) {
670
+ setMany(rawCollection) {
652
671
  const results = [];
653
- for (const [key, value] of entries)
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
  /**