data-structure-typed 1.46.3 → 1.46.5

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 (89) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +47 -61
  3. package/benchmark/report.html +1 -1
  4. package/benchmark/report.json +5 -17
  5. package/dist/cjs/data-structures/hash/hash-map.d.ts +22 -26
  6. package/dist/cjs/data-structures/hash/hash-map.js +104 -76
  7. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  8. package/dist/cjs/data-structures/hash/index.d.ts +0 -4
  9. package/dist/cjs/data-structures/hash/index.js +0 -4
  10. package/dist/cjs/data-structures/hash/index.js.map +1 -1
  11. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +5 -0
  12. package/dist/cjs/types/data-structures/hash/index.d.ts +0 -4
  13. package/dist/cjs/types/data-structures/hash/index.js +0 -4
  14. package/dist/cjs/types/data-structures/hash/index.js.map +1 -1
  15. package/dist/cjs/utils/utils.d.ts +1 -1
  16. package/dist/cjs/utils/utils.js +3 -3
  17. package/dist/cjs/utils/utils.js.map +1 -1
  18. package/dist/mjs/data-structures/hash/hash-map.d.ts +22 -26
  19. package/dist/mjs/data-structures/hash/hash-map.js +107 -77
  20. package/dist/mjs/data-structures/hash/index.d.ts +0 -4
  21. package/dist/mjs/data-structures/hash/index.js +0 -4
  22. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +5 -0
  23. package/dist/mjs/types/data-structures/hash/index.d.ts +0 -4
  24. package/dist/mjs/types/data-structures/hash/index.js +0 -4
  25. package/dist/mjs/utils/utils.d.ts +1 -1
  26. package/dist/mjs/utils/utils.js +1 -1
  27. package/dist/umd/data-structure-typed.js +96 -182
  28. package/dist/umd/data-structure-typed.min.js +1 -1
  29. package/dist/umd/data-structure-typed.min.js.map +1 -1
  30. package/package.json +1 -1
  31. package/src/data-structures/hash/hash-map.ts +130 -81
  32. package/src/data-structures/hash/index.ts +0 -4
  33. package/src/types/data-structures/hash/hash-map.ts +6 -0
  34. package/src/types/data-structures/hash/index.ts +0 -4
  35. package/src/utils/utils.ts +1 -1
  36. package/test/performance/data-structures/hash/hash-map.test.ts +75 -0
  37. package/test/unit/data-structures/hash/hash-map.test.ts +32 -0
  38. package/dist/cjs/data-structures/hash/coordinate-map.d.ts +0 -44
  39. package/dist/cjs/data-structures/hash/coordinate-map.js +0 -63
  40. package/dist/cjs/data-structures/hash/coordinate-map.js.map +0 -1
  41. package/dist/cjs/data-structures/hash/coordinate-set.d.ts +0 -36
  42. package/dist/cjs/data-structures/hash/coordinate-set.js +0 -53
  43. package/dist/cjs/data-structures/hash/coordinate-set.js.map +0 -1
  44. package/dist/cjs/data-structures/hash/tree-map.d.ts +0 -2
  45. package/dist/cjs/data-structures/hash/tree-map.js +0 -7
  46. package/dist/cjs/data-structures/hash/tree-map.js.map +0 -1
  47. package/dist/cjs/data-structures/hash/tree-set.d.ts +0 -2
  48. package/dist/cjs/data-structures/hash/tree-set.js +0 -7
  49. package/dist/cjs/data-structures/hash/tree-set.js.map +0 -1
  50. package/dist/cjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
  51. package/dist/cjs/types/data-structures/hash/coordinate-map.js +0 -3
  52. package/dist/cjs/types/data-structures/hash/coordinate-map.js.map +0 -1
  53. package/dist/cjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
  54. package/dist/cjs/types/data-structures/hash/coordinate-set.js +0 -3
  55. package/dist/cjs/types/data-structures/hash/coordinate-set.js.map +0 -1
  56. package/dist/cjs/types/data-structures/hash/tree-map.d.ts +0 -1
  57. package/dist/cjs/types/data-structures/hash/tree-map.js +0 -3
  58. package/dist/cjs/types/data-structures/hash/tree-map.js.map +0 -1
  59. package/dist/cjs/types/data-structures/hash/tree-set.d.ts +0 -1
  60. package/dist/cjs/types/data-structures/hash/tree-set.js +0 -3
  61. package/dist/cjs/types/data-structures/hash/tree-set.js.map +0 -1
  62. package/dist/mjs/data-structures/hash/coordinate-map.d.ts +0 -44
  63. package/dist/mjs/data-structures/hash/coordinate-map.js +0 -58
  64. package/dist/mjs/data-structures/hash/coordinate-set.d.ts +0 -36
  65. package/dist/mjs/data-structures/hash/coordinate-set.js +0 -48
  66. package/dist/mjs/data-structures/hash/tree-map.d.ts +0 -2
  67. package/dist/mjs/data-structures/hash/tree-map.js +0 -2
  68. package/dist/mjs/data-structures/hash/tree-set.d.ts +0 -2
  69. package/dist/mjs/data-structures/hash/tree-set.js +0 -2
  70. package/dist/mjs/types/data-structures/hash/coordinate-map.d.ts +0 -1
  71. package/dist/mjs/types/data-structures/hash/coordinate-map.js +0 -1
  72. package/dist/mjs/types/data-structures/hash/coordinate-set.d.ts +0 -1
  73. package/dist/mjs/types/data-structures/hash/coordinate-set.js +0 -1
  74. package/dist/mjs/types/data-structures/hash/tree-map.d.ts +0 -1
  75. package/dist/mjs/types/data-structures/hash/tree-map.js +0 -1
  76. package/dist/mjs/types/data-structures/hash/tree-set.d.ts +0 -1
  77. package/dist/mjs/types/data-structures/hash/tree-set.js +0 -1
  78. package/src/data-structures/hash/coordinate-map.ts +0 -63
  79. package/src/data-structures/hash/coordinate-set.ts +0 -52
  80. package/src/data-structures/hash/tree-map.ts +0 -2
  81. package/src/data-structures/hash/tree-set.ts +0 -2
  82. package/src/types/data-structures/hash/coordinate-map.ts +0 -1
  83. package/src/types/data-structures/hash/coordinate-set.ts +0 -1
  84. package/src/types/data-structures/hash/tree-map.ts +0 -1
  85. package/src/types/data-structures/hash/tree-set.ts +0 -1
  86. package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
  87. package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
  88. package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -74
  89. package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -66
@@ -100,8 +100,6 @@ var dataStructureTyped = (() => {
100
100
  BinaryTreeNode: () => BinaryTreeNode,
101
101
  CP: () => CP,
102
102
  Character: () => Character,
103
- CoordinateMap: () => CoordinateMap,
104
- CoordinateSet: () => CoordinateSet,
105
103
  Deque: () => Deque,
106
104
  DirectedEdge: () => DirectedEdge,
107
105
  DirectedGraph: () => DirectedGraph,
@@ -142,11 +140,9 @@ var dataStructureTyped = (() => {
142
140
  SkipListNode: () => SkipListNode,
143
141
  Stack: () => Stack,
144
142
  THUNK_SYMBOL: () => THUNK_SYMBOL,
145
- TreeMap: () => TreeMap,
146
143
  TreeMultimap: () => TreeMultimap,
147
144
  TreeMultimapNode: () => TreeMultimapNode,
148
145
  TreeNode: () => TreeNode,
149
- TreeSet: () => TreeSet,
150
146
  Trie: () => Trie,
151
147
  TrieNode: () => TrieNode,
152
148
  UndirectedEdge: () => UndirectedEdge,
@@ -156,8 +152,8 @@ var dataStructureTyped = (() => {
156
152
  arrayRemove: () => arrayRemove,
157
153
  calcMinUnitsRequired: () => calcMinUnitsRequired,
158
154
  getMSB: () => getMSB,
159
- isObjOrFunc: () => isObjOrFunc,
160
155
  isThunk: () => isThunk,
156
+ isWeakKey: () => isWeakKey,
161
157
  rangeCheck: () => rangeCheck,
162
158
  throwRangeError: () => throwRangeError,
163
159
  toThunk: () => toThunk,
@@ -388,110 +384,6 @@ var dataStructureTyped = (() => {
388
384
  __publicField(_HashTable, "LOAD_FACTOR", 0.75);
389
385
  var HashTable = _HashTable;
390
386
 
391
- // src/data-structures/hash/coordinate-map.ts
392
- var CoordinateMap = class extends Map {
393
- constructor(joint) {
394
- super();
395
- __publicField(this, "_joint", "_");
396
- if (joint !== void 0)
397
- this._joint = joint;
398
- }
399
- get joint() {
400
- return this._joint;
401
- }
402
- /**
403
- * The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
404
- * key array with a specified delimiter.
405
- * @param {number[]} key - The parameter "key" is an array of numbers.
406
- * @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
407
- * (`super.has`) with the `key` array joined together using the `_joint` property.
408
- */
409
- has(key) {
410
- return super.has(key.join(this._joint));
411
- }
412
- /**
413
- * The function overrides the set method of a Map object to convert the key from an array to a string using a specified
414
- * delimiter before calling the original set method.
415
- * @param {number[]} key - The key parameter is an array of numbers.
416
- * @param {V} value - The value parameter is the value that you want to associate with the specified key.
417
- * @returns The `set` method is returning the result of calling the `set` method of the superclass
418
- * (`super.set(key.join(this._joint), value)`).
419
- */
420
- set(key, value) {
421
- return super.set(key.join(this._joint), value);
422
- }
423
- /**
424
- * The function overrides the get method to join the key array with a specified joint and then calls the super get
425
- * method.
426
- * @param {number[]} key - An array of numbers
427
- * @returns The code is returning the value associated with the specified key in the map.
428
- */
429
- get(key) {
430
- return super.get(key.join(this._joint));
431
- }
432
- /**
433
- * The function overrides the delete method and joins the key array using a specified joint character before calling
434
- * the super delete method.
435
- * @param {number[]} key - An array of numbers that represents the key to be deleted.
436
- * @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
437
- * `key` array joined together using the `_joint` property.
438
- */
439
- delete(key) {
440
- return super.delete(key.join(this._joint));
441
- }
442
- };
443
-
444
- // src/data-structures/hash/coordinate-set.ts
445
- var CoordinateSet = class extends Set {
446
- constructor(joint) {
447
- super();
448
- __publicField(this, "_joint", "_");
449
- if (joint !== void 0)
450
- this._joint = joint;
451
- }
452
- get joint() {
453
- return this._joint;
454
- }
455
- /**
456
- * The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
457
- * joining its elements with a specified separator.
458
- * @param {number[]} value - The parameter "value" is an array of numbers.
459
- * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
460
- * in the joined value as an argument.
461
- */
462
- has(value) {
463
- return super.has(value.join(this._joint));
464
- }
465
- /**
466
- * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
467
- * specified delimiter before calling the parent class's "add" function.
468
- * @param {number[]} value - An array of numbers
469
- * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
470
- * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
471
- */
472
- add(value) {
473
- return super.add(value.join(this._joint));
474
- }
475
- /**
476
- * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
477
- * array with a specified joint and then calling the delete method of the parent class.
478
- * @param {number[]} value - An array of numbers
479
- * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
480
- * `value` array joined together using the `_joint` property.
481
- */
482
- delete(value) {
483
- return super.delete(value.join(this._joint));
484
- }
485
- };
486
-
487
- // src/data-structures/hash/tree-map.ts
488
- var TreeMap = class {
489
- };
490
-
491
- // src/data-structures/hash/tree-set.ts
492
- var TreeSet = class {
493
- };
494
-
495
387
  // src/utils/utils.ts
496
388
  var uuidV4 = function() {
497
389
  return "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".replace(/[x]/g, function(c) {
@@ -560,33 +452,41 @@ var dataStructureTyped = (() => {
560
452
  var throwRangeError = (message = "The value is off-limits.") => {
561
453
  throw new RangeError(message);
562
454
  };
563
- var isObjOrFunc = (input) => {
455
+ var isWeakKey = (input) => {
564
456
  const inputType = typeof input;
565
457
  return inputType === "object" && input !== null || inputType === "function";
566
458
  };
567
459
  var calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
568
460
 
569
461
  // src/data-structures/hash/hash-map.ts
570
- var HashMap = class {
462
+ var HashMap = class _HashMap {
571
463
  /**
572
- * The constructor initializes a HashMap object with an optional initial set of key-value pairs.
573
- * @param {Iterable<[K, V]>} elements - The `hashMap` parameter is an optional parameter of type `HashMapOptions<[K,
574
- * V]>`. It is an array of key-value pairs, where each pair is represented as an array `[K, V]`. The
575
- * `K` represents the type of the key and `V` represents the
464
+ * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
465
+ * @param options - The `options` parameter is an object that contains the `elements` property. The
466
+ * `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
576
467
  */
577
- constructor(elements = []) {
578
- __publicField(this, "OBJ_KEY_INDEX", Symbol("OBJ_KEY_INDEX"));
579
- __publicField(this, "_nodes", []);
580
- __publicField(this, "_orgMap", {});
468
+ constructor(options = {
469
+ elements: [],
470
+ hashFn: (key) => String(key),
471
+ objHashFn: (key) => key
472
+ }) {
473
+ __publicField(this, "_noObjMap", {});
474
+ __publicField(this, "_objMap", /* @__PURE__ */ new WeakMap());
581
475
  __publicField(this, "_head");
582
476
  __publicField(this, "_tail");
583
477
  __publicField(this, "_sentinel");
478
+ __publicField(this, "_hashFn");
479
+ __publicField(this, "_objHashFn");
584
480
  __publicField(this, "_size", 0);
585
- Object.setPrototypeOf(this._orgMap, null);
586
481
  this._sentinel = {};
587
482
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
588
- for (const el of elements) {
589
- this.set(el[0], el[1]);
483
+ const { elements, hashFn, objHashFn } = options;
484
+ this._hashFn = hashFn;
485
+ this._objHashFn = objHashFn;
486
+ if (elements) {
487
+ for (const el of elements) {
488
+ this.set(el[0], el[1]);
489
+ }
590
490
  }
591
491
  }
592
492
  get size() {
@@ -649,50 +549,43 @@ var dataStructureTyped = (() => {
649
549
  * type, but typically it is a string or symbol.
650
550
  * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
651
551
  * value associated with the key being set in the data structure.
652
- * @param {boolean} isObjectKey - A boolean flag indicating whether the key is an object key or not.
653
552
  * @returns the size of the data structure after the key-value pair has been set.
654
553
  */
655
- set(key, value, isObjectKey = isObjOrFunc(key)) {
656
- let newTail;
657
- if (isObjectKey) {
658
- const index = key[this.OBJ_KEY_INDEX];
659
- if (index !== void 0) {
660
- this._nodes[index].value = value;
661
- return this._size;
662
- }
663
- Object.defineProperty(key, this.OBJ_KEY_INDEX, {
664
- value: this._nodes.length,
665
- configurable: true
666
- });
667
- newTail = {
668
- key,
669
- value,
670
- prev: this._tail,
671
- next: this._sentinel
672
- };
673
- this._nodes.push(newTail);
554
+ set(key, value) {
555
+ let node;
556
+ if (isWeakKey(key)) {
557
+ const hash = this._objHashFn(key);
558
+ node = this._objMap.get(hash);
559
+ if (node) {
560
+ node.value = value;
561
+ } else {
562
+ node = { key: hash, value, prev: this._tail, next: this._sentinel };
563
+ this._objMap.set(hash, node);
564
+ }
674
565
  } else {
675
- const node = this._orgMap[key];
566
+ const hash = this._hashFn(key);
567
+ node = this._noObjMap[hash];
676
568
  if (node) {
677
569
  node.value = value;
678
- return this._size;
570
+ } else {
571
+ this._noObjMap[hash] = node = {
572
+ key,
573
+ value,
574
+ prev: this._tail,
575
+ next: this._sentinel
576
+ };
679
577
  }
680
- this._orgMap[key] = newTail = {
681
- key,
682
- value,
683
- prev: this._tail,
684
- next: this._sentinel
685
- };
686
578
  }
687
579
  if (this._size === 0) {
688
- this._head = newTail;
689
- this._sentinel.next = newTail;
580
+ this._head = node;
581
+ this._sentinel.next = node;
690
582
  } else {
691
- this._tail.next = newTail;
583
+ this._tail.next = node;
692
584
  }
693
- this._tail = newTail;
694
- this._sentinel.prev = newTail;
695
- return ++this._size;
585
+ this._tail = node;
586
+ this._sentinel.prev = node;
587
+ this._size++;
588
+ return this._size;
696
589
  }
697
590
  /**
698
591
  * Time Complexity: O(1)
@@ -702,21 +595,21 @@ var dataStructureTyped = (() => {
702
595
  * key directly or by using an index stored in the key object.
703
596
  * @param {K} key - The `key` parameter is the key used to retrieve a value from the map. It can be
704
597
  * of any type, but typically it is a string or symbol.
705
- * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
706
- * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that
707
- * `key` is an object key. If `isObjectKey` is `false`, it means that `key`
708
598
  * @returns The value associated with the given key is being returned. If the key is an object key,
709
599
  * the value is retrieved from the `_nodes` array using the index stored in the `OBJ_KEY_INDEX`
710
- * property of the key. If the key is a string key, the value is retrieved from the `_orgMap` object
600
+ * property of the key. If the key is a string key, the value is retrieved from the `_noObjMap` object
711
601
  * using the key itself. If the key is not found, `undefined` is
712
602
  */
713
- get(key, isObjectKey = isObjOrFunc(key)) {
714
- if (isObjectKey) {
715
- const index = key[this.OBJ_KEY_INDEX];
716
- return index !== void 0 ? this._nodes[index].value : void 0;
603
+ get(key) {
604
+ if (isWeakKey(key)) {
605
+ const hash = this._objHashFn(key);
606
+ const node = this._objMap.get(hash);
607
+ return node ? node.value : void 0;
608
+ } else {
609
+ const hash = this._hashFn(key);
610
+ const node = this._noObjMap[hash];
611
+ return node ? node.value : void 0;
717
612
  }
718
- const node = this._orgMap[key];
719
- return node ? node.value : void 0;
720
613
  }
721
614
  /**
722
615
  * Time Complexity: O(n), where n is the index.
@@ -744,26 +637,25 @@ var dataStructureTyped = (() => {
744
637
  * The `delete` function removes a key-value pair from a map-like data structure.
745
638
  * @param {K} key - The `key` parameter is the key that you want to delete from the data structure.
746
639
  * It can be of any type, but typically it is a string or an object.
747
- * @param {boolean} isObjectKey - The `isObjectKey` parameter is a boolean flag that indicates
748
- * whether the `key` parameter is an object key or not. If `isObjectKey` is `true`, it means that the
749
- * `key` parameter is an object key. If `isObjectKey` is `false`, it means that the
750
640
  * @returns a boolean value. It returns `true` if the deletion was successful, and `false` if the key
751
641
  * was not found.
752
642
  */
753
- delete(key, isObjectKey = isObjOrFunc(key)) {
643
+ delete(key) {
754
644
  let node;
755
- if (isObjectKey) {
756
- const index = key[this.OBJ_KEY_INDEX];
757
- if (index === void 0)
645
+ if (isWeakKey(key)) {
646
+ const hash = this._objHashFn(key);
647
+ node = this._objMap.get(hash);
648
+ if (!node) {
758
649
  return false;
759
- delete key[this.OBJ_KEY_INDEX];
760
- node = this._nodes[index];
761
- delete this._nodes[index];
650
+ }
651
+ this._objMap.delete(hash);
762
652
  } else {
763
- node = this._orgMap[key];
764
- if (node === void 0)
653
+ const hash = this._hashFn(key);
654
+ node = this._noObjMap[hash];
655
+ if (!node) {
765
656
  return false;
766
- delete this._orgMap[key];
657
+ }
658
+ delete this._noObjMap[hash];
767
659
  }
768
660
  this._deleteNode(node);
769
661
  return true;
@@ -804,9 +696,7 @@ var dataStructureTyped = (() => {
804
696
  * The `clear` function clears all the elements in a data structure and resets its properties.
805
697
  */
806
698
  clear() {
807
- this._nodes = [];
808
- this._orgMap = {};
809
- Object.setPrototypeOf(this._orgMap, null);
699
+ this._noObjMap = {};
810
700
  this._size = 0;
811
701
  this._head = this._tail = this._sentinel.prev = this._sentinel.next = this._sentinel;
812
702
  }
@@ -827,6 +717,30 @@ var dataStructureTyped = (() => {
827
717
  node = node.next;
828
718
  }
829
719
  }
720
+ filter(predicate) {
721
+ const filteredMap = new _HashMap();
722
+ for (const [key, value] of this) {
723
+ if (predicate([key, value], this)) {
724
+ filteredMap.set(key, value);
725
+ }
726
+ }
727
+ return filteredMap;
728
+ }
729
+ map(callback) {
730
+ const mappedMap = new _HashMap();
731
+ for (const [key, value] of this) {
732
+ const newValue = callback([key, value], this);
733
+ mappedMap.set(key, newValue);
734
+ }
735
+ return mappedMap;
736
+ }
737
+ reduce(callback, initialValue) {
738
+ let accumulator = initialValue;
739
+ for (const element of this) {
740
+ accumulator = callback(accumulator, element, this);
741
+ }
742
+ return accumulator;
743
+ }
830
744
  /**
831
745
  * Time Complexity: O(n), where n is the number of elements in the HashMap.
832
746
  * Space Complexity: O(1)