data-structure-typed 1.46.4 → 1.46.6

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 (49) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +65 -79
  3. package/benchmark/report.html +46 -1
  4. package/benchmark/report.json +428 -5
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +29 -46
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/rb-tree.js +10 -5
  9. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  10. package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
  11. package/dist/cjs/data-structures/hash/hash-map.js +1 -2
  12. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  13. package/dist/cjs/types/{helpers.js → common.js} +1 -1
  14. package/dist/cjs/types/common.js.map +1 -0
  15. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -1
  16. package/dist/cjs/types/index.d.ts +1 -1
  17. package/dist/cjs/types/index.js +1 -1
  18. package/dist/cjs/types/index.js.map +1 -1
  19. package/dist/cjs/utils/utils.d.ts +1 -1
  20. package/dist/cjs/utils/utils.js.map +1 -1
  21. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
  22. package/dist/mjs/data-structures/binary-tree/binary-tree.js +29 -46
  23. package/dist/mjs/data-structures/binary-tree/rb-tree.js +10 -5
  24. package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
  25. package/dist/mjs/data-structures/hash/hash-map.js +1 -2
  26. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -1
  27. package/dist/mjs/types/index.d.ts +1 -1
  28. package/dist/mjs/types/index.js +1 -1
  29. package/dist/mjs/utils/utils.d.ts +1 -1
  30. package/dist/umd/data-structure-typed.js +33 -51
  31. package/dist/umd/data-structure-typed.min.js +10 -5
  32. package/dist/umd/data-structure-typed.min.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/data-structures/binary-tree/binary-tree.ts +33 -46
  35. package/src/data-structures/binary-tree/rb-tree.ts +9 -4
  36. package/src/data-structures/hash/hash-map.ts +4 -5
  37. package/src/types/data-structures/hash/hash-map.ts +1 -1
  38. package/src/types/index.ts +1 -1
  39. package/src/utils/utils.ts +1 -1
  40. package/test/integration/index.html +32 -23
  41. package/test/performance/data-structures/{comparison.test.ts → comparison/comparison.test.ts} +34 -28
  42. package/test/performance/data-structures/hash/hash-map.test.ts +3 -3
  43. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -0
  44. package/tsup.config.js +1 -1
  45. package/dist/cjs/types/helpers.js.map +0 -1
  46. /package/dist/cjs/types/{helpers.d.ts → common.d.ts} +0 -0
  47. /package/dist/mjs/types/{helpers.d.ts → common.d.ts} +0 -0
  48. /package/dist/mjs/types/{helpers.js → common.js} +0 -0
  49. /package/src/types/{helpers.ts → common.ts} +0 -0
@@ -554,7 +554,7 @@ var dataStructureTyped = (() => {
554
554
  set(key, value) {
555
555
  let node;
556
556
  if (isWeakKey(key)) {
557
- const hash = key;
557
+ const hash = this._objHashFn(key);
558
558
  node = this._objMap.get(hash);
559
559
  if (node) {
560
560
  node.value = value;
@@ -6534,7 +6534,7 @@ var dataStructureTyped = (() => {
6534
6534
  return RBTNColor2;
6535
6535
  })(RBTNColor || {});
6536
6536
 
6537
- // src/types/helpers.ts
6537
+ // src/types/common.ts
6538
6538
  var CP = /* @__PURE__ */ ((CP2) => {
6539
6539
  CP2["lt"] = "lt";
6540
6540
  CP2["eq"] = "eq";
@@ -7941,7 +7941,7 @@ var dataStructureTyped = (() => {
7941
7941
  }
7942
7942
  /**
7943
7943
  * The `print` function is used to display a binary tree structure in a visually appealing way.
7944
- * @param {N | null | undefined} root - The `root` parameter is of type `BTNKey | N | null |
7944
+ * @param {N | null | undefined} beginRoot - The `root` parameter is of type `BTNKey | N | null |
7945
7945
  * undefined`. It represents the root node of a binary tree. The root node can have one of the
7946
7946
  * following types:
7947
7947
  */
@@ -7950,56 +7950,34 @@ var dataStructureTyped = (() => {
7950
7950
  if (!beginRoot)
7951
7951
  return;
7952
7952
  const display = (root) => {
7953
- const [lines, , ,] = _displayAux(root);
7953
+ const [lines, , ,] = this._displayAux(root);
7954
7954
  for (const line of lines) {
7955
7955
  console.log(line);
7956
7956
  }
7957
7957
  };
7958
- const _displayAux = (node) => {
7959
- if (!this.isRealNode(node)) {
7960
- return [[], 0, 0, 0];
7961
- }
7962
- if (this.isRealNode(node) && !this.isRealNode(node.right) && !this.isRealNode(node.left)) {
7963
- const line = `${node.key}`;
7964
- const width = line.length;
7965
- const height = 1;
7966
- const middle = Math.floor(width / 2);
7967
- return [[line], width, height, middle];
7968
- }
7969
- if (this.isRealNode(node) && !this.isRealNode(node.right)) {
7970
- const [lines, n2, p2, x2] = _displayAux(node.left);
7971
- const s2 = `${node.key}`;
7972
- const u2 = s2.length;
7973
- const first_line2 = " ".repeat(x2 + 1) + "_".repeat(n2 - x2 - 1) + s2;
7974
- const second_line2 = " ".repeat(x2) + "/" + " ".repeat(n2 - x2 - 1 + u2);
7975
- const shifted_lines = lines.map((line) => line + " ".repeat(u2));
7976
- return [[first_line2, second_line2, ...shifted_lines], n2 + u2, p2 + 2, n2 + Math.floor(u2 / 2)];
7977
- }
7978
- if (this.isRealNode(node) && !this.isRealNode(node.left)) {
7979
- const [lines, n2, p2, u2] = _displayAux(node.right);
7980
- const s2 = `${node.key}`;
7981
- const x2 = s2.length;
7982
- const first_line2 = s2 + "_".repeat(x2) + " ".repeat(n2 - x2);
7983
- const second_line2 = " ".repeat(u2 + x2) + "\\" + " ".repeat(n2 - x2 - 1);
7984
- const shifted_lines = lines.map((line) => " ".repeat(u2) + line);
7985
- return [[first_line2, second_line2, ...shifted_lines], n2 + x2, p2 + 2, Math.floor(u2 / 2)];
7986
- }
7987
- const [left, n, p, x] = _displayAux(node.left);
7988
- const [right, m, q, y] = _displayAux(node.right);
7989
- const s = `${node.key}`;
7990
- const u = s.length;
7991
- const first_line = " ".repeat(x + 1) + "_".repeat(n - x - 1) + s + "_".repeat(y) + " ".repeat(m - y);
7992
- const second_line = " ".repeat(x) + "/" + " ".repeat(n - x - 1 + u + y) + "\\" + " ".repeat(m - y - 1);
7993
- if (p < q) {
7994
- left.push(...new Array(q - p).fill(" ".repeat(n)));
7995
- } else if (q < p) {
7996
- right.push(...new Array(p - q).fill(" ".repeat(m)));
7997
- }
7998
- const zipped_lines = left.map((a, i) => a + " ".repeat(u) + right[i]);
7999
- return [[first_line, second_line, ...zipped_lines], n + m + u, Math.max(p, q) + 2, n + Math.floor(u / 2)];
8000
- };
8001
7958
  display(beginRoot);
8002
7959
  }
7960
+ _displayAux(node) {
7961
+ if (!node) {
7962
+ return [["\u2500"], 1, 0, 0];
7963
+ }
7964
+ const line = node.key.toString();
7965
+ const width = line.length;
7966
+ if (!node.left && !node.right) {
7967
+ return [[line], width, 1, Math.floor(width / 2)];
7968
+ }
7969
+ const [leftLines, leftWidth, leftHeight, leftMiddle] = node.left ? this._displayAux(node.left) : [[""], 0, 0, 0];
7970
+ const [rightLines, rightWidth, rightHeight, rightMiddle] = node.right ? this._displayAux(node.right) : [[""], 0, 0, 0];
7971
+ const firstLine = " ".repeat(Math.max(0, leftMiddle + 1)) + "_".repeat(Math.max(0, leftWidth - leftMiddle - 1)) + line + "_".repeat(Math.max(0, rightMiddle)) + " ".repeat(Math.max(0, rightWidth - rightMiddle));
7972
+ const secondLine = (leftHeight > 0 ? " ".repeat(leftMiddle) + "/" + " ".repeat(leftWidth - leftMiddle - 1) : " ".repeat(leftWidth)) + " ".repeat(width) + (rightHeight > 0 ? " ".repeat(rightMiddle) + "\\" + " ".repeat(rightWidth - rightMiddle - 1) : " ".repeat(rightWidth));
7973
+ const mergedLines = [firstLine, secondLine];
7974
+ for (let i = 0; i < Math.max(leftHeight, rightHeight); i++) {
7975
+ const leftLine = i < leftHeight ? leftLines[i] : " ".repeat(leftWidth);
7976
+ const rightLine = i < rightHeight ? rightLines[i] : " ".repeat(rightWidth);
7977
+ mergedLines.push(leftLine + " ".repeat(width) + rightLine);
7978
+ }
7979
+ return [mergedLines, leftWidth + width + rightWidth, Math.max(leftHeight, rightHeight) + 2, leftWidth + Math.floor(width / 2)];
7980
+ }
8003
7981
  /**
8004
7982
  * Swap the data of two nodes in the binary tree.
8005
7983
  * @param {N} srcNode - The source node to swap.
@@ -9560,10 +9538,14 @@ var dataStructureTyped = (() => {
9560
9538
  let x = this.root;
9561
9539
  while (x !== this.NIL) {
9562
9540
  y = x;
9563
- if (x && node.key < x.key) {
9564
- x = x.left;
9565
- } else {
9566
- x = x == null ? void 0 : x.right;
9541
+ if (x) {
9542
+ if (node.key < x.key) {
9543
+ x = x.left;
9544
+ } else if (node.key > x.key) {
9545
+ x = x == null ? void 0 : x.right;
9546
+ } else {
9547
+ return;
9548
+ }
9567
9549
  }
9568
9550
  }
9569
9551
  node.parent = y;