data-structure-typed 2.4.2 → 2.4.4

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 (48) hide show
  1. package/.github/workflows/release.yml +27 -0
  2. package/CHANGELOG.md +3 -1
  3. package/README.md +46 -50
  4. package/dist/cjs/index.cjs +57 -35
  5. package/dist/cjs-legacy/index.cjs +58 -35
  6. package/dist/esm/index.mjs +57 -35
  7. package/dist/esm-legacy/index.mjs +58 -35
  8. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  9. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -5
  10. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/tree-map.d.ts +10 -0
  12. package/dist/types/data-structures/binary-tree/tree-set.d.ts +10 -0
  13. package/dist/types/data-structures/graph/directed-graph.d.ts +2 -2
  14. package/dist/types/data-structures/graph/undirected-graph.d.ts +2 -2
  15. package/dist/types/data-structures/hash/hash-map.d.ts +2 -2
  16. package/dist/types/data-structures/heap/heap.d.ts +3 -7
  17. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +1 -1
  18. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
  19. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -1
  20. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
  21. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
  22. package/dist/types/types/data-structures/stack/stack.d.ts +1 -1
  23. package/dist/umd/data-structure-typed.js +58 -35
  24. package/dist/umd/data-structure-typed.min.js +4 -4
  25. package/package.json +2 -2
  26. package/src/data-structures/base/iterable-element-base.ts +2 -2
  27. package/src/data-structures/binary-tree/binary-tree.ts +8 -7
  28. package/src/data-structures/binary-tree/bst.ts +1 -1
  29. package/src/data-structures/binary-tree/tree-map.ts +16 -0
  30. package/src/data-structures/binary-tree/tree-multi-set.ts +5 -5
  31. package/src/data-structures/binary-tree/tree-set.ts +16 -0
  32. package/src/data-structures/graph/abstract-graph.ts +18 -18
  33. package/src/data-structures/graph/directed-graph.ts +4 -4
  34. package/src/data-structures/graph/map-graph.ts +1 -1
  35. package/src/data-structures/graph/undirected-graph.ts +4 -4
  36. package/src/data-structures/hash/hash-map.ts +6 -4
  37. package/src/data-structures/heap/heap.ts +17 -14
  38. package/src/data-structures/linked-list/doubly-linked-list.ts +4 -4
  39. package/src/data-structures/linked-list/singly-linked-list.ts +15 -9
  40. package/src/data-structures/queue/deque.ts +1 -1
  41. package/src/data-structures/stack/stack.ts +1 -1
  42. package/src/data-structures/trie/trie.ts +10 -5
  43. package/src/types/data-structures/binary-tree/avl-tree.ts +1 -1
  44. package/src/types/data-structures/binary-tree/red-black-tree.ts +1 -1
  45. package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -1
  46. package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -1
  47. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
  48. package/src/types/data-structures/stack/stack.ts +1 -1
@@ -1,2 +1,2 @@
1
1
  import { BSTOptions } from './bst';
2
- export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
2
+ export type AVLTreeOptions<K, V, R> = BSTOptions<K, V, R>;
@@ -1,3 +1,3 @@
1
1
  import type { BSTOptions } from './bst';
2
2
  export type RBTNColor = 'RED' | 'BLACK';
3
- export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
3
+ export type RedBlackTreeOptions<K, V, R> = BSTOptions<K, V, R>;
@@ -1,2 +1,2 @@
1
1
  import { LinearBaseOptions } from '../base';
2
- export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
2
+ export type DoublyLinkedListOptions<E, R> = LinearBaseOptions<E, R>;
@@ -1,2 +1,2 @@
1
1
  import { LinearBaseOptions } from '../base';
2
- export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R> & {};
2
+ export type SinglyLinkedListOptions<E, R> = LinearBaseOptions<E, R>;
@@ -1,2 +1,2 @@
1
1
  import { HeapOptions } from '../heap';
2
- export type PriorityQueueOptions<E, R> = HeapOptions<E, R> & {};
2
+ export type PriorityQueueOptions<E, R> = HeapOptions<E, R>;
@@ -1,2 +1,2 @@
1
1
  import { IterableElementBaseOptions } from '../base';
2
- export type StackOptions<E, R> = IterableElementBaseOptions<E, R> & {};
2
+ export type StackOptions<E, R> = IterableElementBaseOptions<E, R>;
@@ -1105,8 +1105,9 @@ var dataStructureTyped = (() => {
1105
1105
  const cur = node;
1106
1106
  node = node.next;
1107
1107
  if (predicate(cur.key, cur.value, i++, this)) {
1108
- if (isWeakKey(cur.key)) {
1109
- this._objMap.delete(cur.key);
1108
+ const keyToCheck = cur.key;
1109
+ if (isWeakKey(keyToCheck)) {
1110
+ this._objMap.delete(keyToCheck);
1110
1111
  } else {
1111
1112
  const hash = this._hashFn(cur.key);
1112
1113
  delete this._noObjMap[hash];
@@ -1614,7 +1615,7 @@ var dataStructureTyped = (() => {
1614
1615
  */
1615
1616
  constructor(elements = [], options) {
1616
1617
  super(options);
1617
- __publicField(this, "_equals", Object.is);
1618
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
1618
1619
  __publicField(this, "_head");
1619
1620
  __publicField(this, "_tail");
1620
1621
  __publicField(this, "_length", 0);
@@ -1702,6 +1703,7 @@ var dataStructureTyped = (() => {
1702
1703
  * @returns Removed element or undefined.
1703
1704
  */
1704
1705
  pop() {
1706
+ var _a;
1705
1707
  if (!this.head) return void 0;
1706
1708
  if (this.head === this.tail) {
1707
1709
  const value2 = this.head.value;
@@ -1711,8 +1713,8 @@ var dataStructureTyped = (() => {
1711
1713
  return value2;
1712
1714
  }
1713
1715
  let current = this.head;
1714
- while (current.next !== this.tail) current = current.next;
1715
- const value = this.tail.value;
1716
+ while (current.next && current.next !== this.tail) current = current.next;
1717
+ const value = (_a = this.tail) == null ? void 0 : _a.value;
1716
1718
  current.next = void 0;
1717
1719
  this._tail = current;
1718
1720
  this._length--;
@@ -1800,8 +1802,8 @@ var dataStructureTyped = (() => {
1800
1802
  at(index) {
1801
1803
  if (index < 0 || index >= this._length) return void 0;
1802
1804
  let current = this.head;
1803
- for (let i = 0; i < index; i++) current = current.next;
1804
- return current.value;
1805
+ for (let i = 0; i < index && current; i++) current = current.next;
1806
+ return current == null ? void 0 : current.value;
1805
1807
  }
1806
1808
  /**
1807
1809
  * Type guard: check whether the input is a SinglyLinkedListNode.
@@ -1821,7 +1823,7 @@ var dataStructureTyped = (() => {
1821
1823
  getNodeAt(index) {
1822
1824
  if (index < 0 || index >= this._length) return void 0;
1823
1825
  let current = this.head;
1824
- for (let i = 0; i < index; i++) current = current.next;
1826
+ for (let i = 0; i < index && current; i++) current = current.next;
1825
1827
  return current;
1826
1828
  }
1827
1829
  /**
@@ -2332,7 +2334,7 @@ var dataStructureTyped = (() => {
2332
2334
  */
2333
2335
  constructor(elements = [], options) {
2334
2336
  super(options);
2335
- __publicField(this, "_equals", Object.is);
2337
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
2336
2338
  __publicField(this, "_head");
2337
2339
  __publicField(this, "_tail");
2338
2340
  __publicField(this, "_length", 0);
@@ -2520,8 +2522,8 @@ var dataStructureTyped = (() => {
2520
2522
  at(index) {
2521
2523
  if (index < 0 || index >= this._length) return void 0;
2522
2524
  let current = this.head;
2523
- for (let i = 0; i < index; i++) current = current.next;
2524
- return current.value;
2525
+ for (let i = 0; i < index && current; i++) current = current.next;
2526
+ return current == null ? void 0 : current.value;
2525
2527
  }
2526
2528
  /**
2527
2529
  * Get the node reference at a given index.
@@ -2532,7 +2534,7 @@ var dataStructureTyped = (() => {
2532
2534
  getNodeAt(index) {
2533
2535
  if (index < 0 || index >= this._length) return void 0;
2534
2536
  let current = this.head;
2535
- for (let i = 0; i < index; i++) current = current.next;
2537
+ for (let i = 0; i < index && current; i++) current = current.next;
2536
2538
  return current;
2537
2539
  }
2538
2540
  /**
@@ -3038,7 +3040,7 @@ var dataStructureTyped = (() => {
3038
3040
  */
3039
3041
  constructor(elements = [], options) {
3040
3042
  super(options);
3041
- __publicField(this, "_equals", Object.is);
3043
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
3042
3044
  __publicField(this, "_elements", []);
3043
3045
  this.pushMany(elements);
3044
3046
  }
@@ -3667,7 +3669,7 @@ var dataStructureTyped = (() => {
3667
3669
  */
3668
3670
  constructor(elements = [], options) {
3669
3671
  super(options);
3670
- __publicField(this, "_equals", Object.is);
3672
+ __publicField(this, "_equals", (a, b) => Object.is(a, b));
3671
3673
  __publicField(this, "_bucketSize", 1 << 12);
3672
3674
  __publicField(this, "_bucketFirst", 0);
3673
3675
  __publicField(this, "_firstInBucket", 0);
@@ -4737,11 +4739,6 @@ var dataStructureTyped = (() => {
4737
4739
  }
4738
4740
  return out;
4739
4741
  }
4740
- /**
4741
- * Get the comparator used to order elements.
4742
- * @remarks Time O(1), Space O(1)
4743
- * @returns Comparator function.
4744
- */
4745
4742
  /**
4746
4743
  * Get the comparator used to order elements.
4747
4744
  * @remarks Time O(1), Space O(1)
@@ -4790,8 +4787,7 @@ var dataStructureTyped = (() => {
4790
4787
  */
4791
4788
  _createInstance(options) {
4792
4789
  const Ctor = this.constructor;
4793
- const next = new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
4794
- return next;
4790
+ return new Ctor([], { comparator: this.comparator, toElementFn: this.toElementFn, ...options != null ? options : {} });
4795
4791
  }
4796
4792
  /**
4797
4793
  * (Protected) Create a like-kind instance seeded by elements.
@@ -5823,8 +5819,8 @@ var dataStructureTyped = (() => {
5823
5819
  const Ctor = this.constructor;
5824
5820
  const instance = new Ctor();
5825
5821
  const graph = _options == null ? void 0 : _options.graph;
5826
- if (graph) instance._options = { ...instance._options, ...graph };
5827
- else instance._options = { ...instance._options, ...this._options };
5822
+ if (graph) instance["_options"] = { ...instance["_options"], ...graph };
5823
+ else instance["_options"] = { ...instance["_options"], ...this._options };
5828
5824
  return instance;
5829
5825
  }
5830
5826
  /**
@@ -5857,12 +5853,10 @@ var dataStructureTyped = (() => {
5857
5853
  const [va, vb] = ends;
5858
5854
  const ka = va.key;
5859
5855
  const kb = vb.key;
5860
- const hasA = g.hasVertex ? g.hasVertex(ka) : false;
5861
- const hasB = g.hasVertex ? g.hasVertex(kb) : false;
5856
+ const hasA = typeof g.hasVertex === "function" ? g.hasVertex(ka) : false;
5857
+ const hasB = typeof g.hasVertex === "function" ? g.hasVertex(kb) : false;
5862
5858
  if (hasA && hasB) {
5863
- const w = e.weight;
5864
- const val = e.value;
5865
- const newEdge = g.createEdge(ka, kb, w, val);
5859
+ const newEdge = g.createEdge(ka, kb, e.weight, e.value);
5866
5860
  g._addEdge(newEdge);
5867
5861
  }
5868
5862
  }
@@ -7030,7 +7024,7 @@ var dataStructureTyped = (() => {
7030
7024
  * @param node - The node.
7031
7025
  * @returns The node's key or undefined.
7032
7026
  */
7033
- __publicField(this, "_DEFAULT_NODE_CALLBACK", (node) => node ? node.key : void 0);
7027
+ __publicField(this, "_DEFAULT_NODE_CALLBACK", (node) => node == null ? void 0 : node.key);
7034
7028
  if (options) {
7035
7029
  const { iterationType, toEntryFn, isMapMode, isDuplicate } = options;
7036
7030
  if (iterationType) this.iterationType = iterationType;
@@ -12160,6 +12154,21 @@ var dataStructureTyped = (() => {
12160
12154
  }
12161
12155
  return out;
12162
12156
  }
12157
+ /**
12158
+ * Creates a shallow clone of this set.
12159
+ * @remarks Time O(n log n), Space O(n)
12160
+ * @example
12161
+ * const original = new TreeSet([1, 2, 3]);
12162
+ * const copy = original.clone();
12163
+ * copy.add(4);
12164
+ * original.has(4); // false (original unchanged)
12165
+ */
12166
+ clone() {
12167
+ return new _TreeSet(this, {
12168
+ comparator: __privateGet(this, _isDefaultComparator) ? void 0 : __privateGet(this, _userComparator),
12169
+ isMapMode: __privateGet(this, _core).isMapMode
12170
+ });
12171
+ }
12163
12172
  };
12164
12173
  _core = new WeakMap();
12165
12174
  _isDefaultComparator = new WeakMap();
@@ -12977,6 +12986,21 @@ var dataStructureTyped = (() => {
12977
12986
  }
12978
12987
  return out;
12979
12988
  }
12989
+ /**
12990
+ * Creates a shallow clone of this map.
12991
+ * @remarks Time O(n log n), Space O(n)
12992
+ * @example
12993
+ * const original = new TreeMap([['a', 1], ['b', 2]]);
12994
+ * const copy = original.clone();
12995
+ * copy.set('c', 3);
12996
+ * original.has('c'); // false (original unchanged)
12997
+ */
12998
+ clone() {
12999
+ return new _TreeMap(this, {
13000
+ comparator: __privateGet(this, _isDefaultComparator3) ? void 0 : __privateGet(this, _userComparator2),
13001
+ isMapMode: __privateGet(this, _core3).isMapMode
13002
+ });
13003
+ }
12980
13004
  };
12981
13005
  _core3 = new WeakMap();
12982
13006
  _isDefaultComparator3 = new WeakMap();
@@ -13195,7 +13219,7 @@ var dataStructureTyped = (() => {
13195
13219
  * @remarks Time O(1), Space O(1)
13196
13220
  */
13197
13221
  get comparator() {
13198
- return __privateGet(this, _core4)._comparator;
13222
+ return __privateGet(this, _core4).comparator;
13199
13223
  }
13200
13224
  // ━━━ clear ━━━
13201
13225
  /**
@@ -13332,7 +13356,7 @@ var dataStructureTyped = (() => {
13332
13356
  filter(predicate) {
13333
13357
  const result = new _TreeMultiSet([], {
13334
13358
  comparator: __privateGet(this, _isDefaultComparator4) ? void 0 : this.comparator,
13335
- isMapMode: __privateGet(this, _core4)._isMapMode
13359
+ isMapMode: __privateGet(this, _core4).isMapMode
13336
13360
  });
13337
13361
  for (const [k, c] of this.entries()) {
13338
13362
  if (predicate(k, c)) {
@@ -13372,7 +13396,7 @@ var dataStructureTyped = (() => {
13372
13396
  map(mapper, options) {
13373
13397
  const result = new _TreeMultiSet([], {
13374
13398
  comparator: options == null ? void 0 : options.comparator,
13375
- isMapMode: __privateGet(this, _core4)._isMapMode
13399
+ isMapMode: __privateGet(this, _core4).isMapMode
13376
13400
  });
13377
13401
  for (const [k, c] of this.entries()) {
13378
13402
  const [newKey, newCount] = mapper(k, c);
@@ -13392,7 +13416,7 @@ var dataStructureTyped = (() => {
13392
13416
  clone() {
13393
13417
  const result = new _TreeMultiSet([], {
13394
13418
  comparator: __privateGet(this, _isDefaultComparator4) ? void 0 : this.comparator,
13395
- isMapMode: __privateGet(this, _core4)._isMapMode
13419
+ isMapMode: __privateGet(this, _core4).isMapMode
13396
13420
  });
13397
13421
  for (const [k, c] of this.entries()) {
13398
13422
  result.add(k, c);
@@ -14425,12 +14449,11 @@ var dataStructureTyped = (() => {
14425
14449
  */
14426
14450
  _createInstance(options) {
14427
14451
  const Ctor = this.constructor;
14428
- const next = new Ctor([], {
14452
+ return new Ctor([], {
14429
14453
  toElementFn: this.toElementFn,
14430
14454
  caseSensitive: this.caseSensitive,
14431
14455
  ...options != null ? options : {}
14432
14456
  });
14433
- return next;
14434
14457
  }
14435
14458
  /**
14436
14459
  * (Protected) Create a like-kind trie and seed it from an iterable.