@tomaszjarosz/react-visualizers 0.1.4 → 0.2.1
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.
- package/dist/index.cjs +142 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +142 -57
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3510,7 +3510,7 @@ const DPVisualizerComponent = ({
|
|
|
3510
3510
|
showCode = true,
|
|
3511
3511
|
className = ""
|
|
3512
3512
|
}) => {
|
|
3513
|
-
var _a;
|
|
3513
|
+
var _a, _b, _c;
|
|
3514
3514
|
const [items] = useState(DEFAULT_ITEMS);
|
|
3515
3515
|
const [capacity] = useState(DEFAULT_CAPACITY);
|
|
3516
3516
|
const [speed, setSpeed] = useState(25);
|
|
@@ -3634,33 +3634,83 @@ const DPVisualizerComponent = ({
|
|
|
3634
3634
|
] })
|
|
3635
3635
|
] }) }) }),
|
|
3636
3636
|
/* @__PURE__ */ jsx("div", { className: "p-4", children: /* @__PURE__ */ jsxs("div", { className: `flex gap-4 ${showCode ? "flex-col lg:flex-row" : ""}`, children: [
|
|
3637
|
-
/* @__PURE__ */ jsxs(VisualizationArea, { minHeight:
|
|
3637
|
+
/* @__PURE__ */ jsxs(VisualizationArea, { minHeight: 450, className: "flex-1 min-w-0", children: [
|
|
3638
|
+
/* @__PURE__ */ jsxs("div", { className: "mb-6 p-4 bg-gradient-to-r from-teal-50 to-cyan-50 rounded-xl border-2 border-teal-200", children: [
|
|
3639
|
+
/* @__PURE__ */ jsx("div", { className: "text-sm font-semibold text-teal-800 mb-3", children: "DP Recurrence Formula" }),
|
|
3640
|
+
/* @__PURE__ */ jsxs("div", { className: "font-mono text-sm bg-white rounded-lg p-3 border border-teal-200", children: [
|
|
3641
|
+
/* @__PURE__ */ jsxs("div", { className: "text-gray-600 mb-2", children: [
|
|
3642
|
+
"dp[i][w] = max(",
|
|
3643
|
+
/* @__PURE__ */ jsx("span", { className: "text-yellow-600 font-bold", children: " skip" }),
|
|
3644
|
+
",",
|
|
3645
|
+
/* @__PURE__ */ jsx("span", { className: "text-green-600 font-bold", children: " take" }),
|
|
3646
|
+
")"
|
|
3647
|
+
] }),
|
|
3648
|
+
/* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4 text-xs mt-3", children: [
|
|
3649
|
+
/* @__PURE__ */ jsxs("div", { className: "bg-yellow-50 p-2 rounded border border-yellow-200", children: [
|
|
3650
|
+
/* @__PURE__ */ jsx("span", { className: "text-yellow-700 font-bold", children: "skip" }),
|
|
3651
|
+
/* @__PURE__ */ jsx("span", { className: "text-gray-600", children: " = dp[i-1][w]" }),
|
|
3652
|
+
/* @__PURE__ */ jsx("div", { className: "text-gray-500 mt-1", children: "Don't take item i" })
|
|
3653
|
+
] }),
|
|
3654
|
+
/* @__PURE__ */ jsxs("div", { className: "bg-green-50 p-2 rounded border border-green-200", children: [
|
|
3655
|
+
/* @__PURE__ */ jsx("span", { className: "text-green-700 font-bold", children: "take" }),
|
|
3656
|
+
/* @__PURE__ */ jsx("span", { className: "text-gray-600", children: " = dp[i-1][w-weight] + value" }),
|
|
3657
|
+
/* @__PURE__ */ jsx("div", { className: "text-gray-500 mt-1", children: "Take item i" })
|
|
3658
|
+
] })
|
|
3659
|
+
] })
|
|
3660
|
+
] }),
|
|
3661
|
+
currentI > 0 && currentW > 0 && /* @__PURE__ */ jsxs("div", { className: "mt-4 p-3 bg-white rounded-lg border-2 border-purple-300", children: [
|
|
3662
|
+
/* @__PURE__ */ jsxs("div", { className: "text-sm font-semibold text-purple-800 mb-2", children: [
|
|
3663
|
+
"Current: dp[",
|
|
3664
|
+
currentI,
|
|
3665
|
+
"][",
|
|
3666
|
+
currentW,
|
|
3667
|
+
"]"
|
|
3668
|
+
] }),
|
|
3669
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-4 text-sm", children: [
|
|
3670
|
+
/* @__PURE__ */ jsxs("div", { className: "text-gray-600", children: [
|
|
3671
|
+
"Item ",
|
|
3672
|
+
currentI,
|
|
3673
|
+
": ",
|
|
3674
|
+
/* @__PURE__ */ jsxs("span", { className: "font-mono", children: [
|
|
3675
|
+
"w=",
|
|
3676
|
+
(_b = items[currentI - 1]) == null ? void 0 : _b.weight,
|
|
3677
|
+
", v=",
|
|
3678
|
+
(_c = items[currentI - 1]) == null ? void 0 : _c.value
|
|
3679
|
+
] })
|
|
3680
|
+
] }),
|
|
3681
|
+
decision && /* @__PURE__ */ jsx("div", { className: `px-3 py-1 rounded-full font-bold ${decision === "take" ? "bg-green-100 text-green-700" : "bg-yellow-100 text-yellow-700"}`, children: decision === "take" ? "✓ TAKE" : "✗ SKIP" })
|
|
3682
|
+
] })
|
|
3683
|
+
] })
|
|
3684
|
+
] }),
|
|
3638
3685
|
/* @__PURE__ */ jsxs("div", { className: "flex gap-6", children: [
|
|
3639
|
-
/* @__PURE__ */ jsxs("div", { className: "w-
|
|
3640
|
-
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-2", children: "Items:" }),
|
|
3686
|
+
/* @__PURE__ */ jsxs("div", { className: "w-40", children: [
|
|
3687
|
+
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-2", children: "Items (i = item index):" }),
|
|
3641
3688
|
/* @__PURE__ */ jsx("div", { className: "space-y-1", children: items.map((item, idx) => /* @__PURE__ */ jsxs(
|
|
3642
3689
|
"div",
|
|
3643
3690
|
{
|
|
3644
|
-
className: `px-2 py-1 rounded text-xs ${idx + 1 === currentI ? "bg-purple-
|
|
3691
|
+
className: `px-2 py-1.5 rounded text-xs transition-colors ${idx + 1 === currentI ? "bg-purple-200 text-purple-900 font-bold ring-2 ring-purple-400" : idx + 1 < currentI ? "bg-blue-50 text-blue-600" : "bg-gray-100 text-gray-500"}`,
|
|
3645
3692
|
children: [
|
|
3646
|
-
"
|
|
3647
|
-
|
|
3693
|
+
/* @__PURE__ */ jsxs("span", { className: "font-mono", children: [
|
|
3694
|
+
"i=",
|
|
3695
|
+
idx + 1
|
|
3696
|
+
] }),
|
|
3648
3697
|
": w=",
|
|
3649
3698
|
item.weight,
|
|
3650
3699
|
", v=",
|
|
3651
|
-
item.value
|
|
3700
|
+
item.value,
|
|
3701
|
+
idx + 1 === currentI && /* @__PURE__ */ jsx("span", { className: "ml-1", children: "← CURRENT" })
|
|
3652
3702
|
]
|
|
3653
3703
|
},
|
|
3654
3704
|
idx
|
|
3655
3705
|
)) }),
|
|
3656
|
-
/* @__PURE__ */ jsxs("div", { className: "mt-3 text-sm", children: [
|
|
3657
|
-
/* @__PURE__ */ jsx("span", { className: "font-medium text-gray-700", children: "Capacity:" }),
|
|
3706
|
+
/* @__PURE__ */ jsxs("div", { className: "mt-3 text-sm p-2 bg-gray-100 rounded", children: [
|
|
3707
|
+
/* @__PURE__ */ jsx("span", { className: "font-medium text-gray-700", children: "Max Capacity:" }),
|
|
3658
3708
|
" ",
|
|
3659
|
-
/* @__PURE__ */ jsx("span", { className: "text-gray-900", children: capacity })
|
|
3709
|
+
/* @__PURE__ */ jsx("span", { className: "text-gray-900 font-mono", children: capacity })
|
|
3660
3710
|
] })
|
|
3661
3711
|
] }),
|
|
3662
3712
|
/* @__PURE__ */ jsxs("div", { className: "flex-1 overflow-x-auto", children: [
|
|
3663
|
-
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-2", children: "DP Table:" }),
|
|
3713
|
+
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-2", children: "DP Table (w = current capacity):" }),
|
|
3664
3714
|
/* @__PURE__ */ jsxs("table", { className: "text-xs border-collapse", children: [
|
|
3665
3715
|
/* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
3666
3716
|
/* @__PURE__ */ jsx("th", { className: "w-8 p-1 text-gray-500 font-normal", children: "i\\w" }),
|
|
@@ -4780,20 +4830,6 @@ const HashTableVisualizerComponent = ({
|
|
|
4780
4830
|
key: ""
|
|
4781
4831
|
};
|
|
4782
4832
|
const { operation, buckets, bucketIndex, key } = currentStepData;
|
|
4783
|
-
const getBucketStyle = (index) => {
|
|
4784
|
-
if (index === bucketIndex) {
|
|
4785
|
-
if (operation === "collision") return "border-red-400 bg-red-50";
|
|
4786
|
-
if (operation === "placed") return "border-green-400 bg-green-50";
|
|
4787
|
-
if (operation === "insert") return "border-yellow-400 bg-yellow-50";
|
|
4788
|
-
}
|
|
4789
|
-
return "border-gray-300 bg-gray-50";
|
|
4790
|
-
};
|
|
4791
|
-
const getKeyStyle = (bucketKey) => {
|
|
4792
|
-
if (bucketKey === key && (operation === "placed" || operation === "insert")) {
|
|
4793
|
-
return "bg-yellow-200 text-yellow-900 border-yellow-400";
|
|
4794
|
-
}
|
|
4795
|
-
return "bg-blue-100 text-blue-800 border-blue-300";
|
|
4796
|
-
};
|
|
4797
4833
|
const currentDescription = ((_a = steps[currentStep]) == null ? void 0 : _a.description) || "";
|
|
4798
4834
|
return /* @__PURE__ */ jsxs(
|
|
4799
4835
|
"div",
|
|
@@ -4808,60 +4844,109 @@ const HashTableVisualizerComponent = ({
|
|
|
4808
4844
|
] })
|
|
4809
4845
|
] }) }) }),
|
|
4810
4846
|
/* @__PURE__ */ jsx("div", { className: "p-4", children: /* @__PURE__ */ jsxs("div", { className: `flex gap-4 ${showCode ? "flex-col lg:flex-row" : ""}`, children: [
|
|
4811
|
-
/* @__PURE__ */ jsxs(VisualizationArea, { minHeight:
|
|
4812
|
-
/* @__PURE__ */ jsxs("div", { className: "mb-
|
|
4847
|
+
/* @__PURE__ */ jsxs(VisualizationArea, { minHeight: 400, className: "flex-1 min-w-0", children: [
|
|
4848
|
+
/* @__PURE__ */ jsxs("div", { className: "mb-6", children: [
|
|
4813
4849
|
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-2", children: "Keys to insert:" }),
|
|
4814
4850
|
/* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: keys.map((k, idx) => {
|
|
4815
4851
|
const isInserted = steps.slice(0, currentStep + 1).some((s) => s.key === k && s.operation === "placed");
|
|
4816
4852
|
return /* @__PURE__ */ jsx(
|
|
4817
4853
|
"span",
|
|
4818
4854
|
{
|
|
4819
|
-
className: `px-
|
|
4855
|
+
className: `px-3 py-1.5 text-sm font-medium rounded-lg border-2 transition-colors ${isInserted ? "bg-green-100 text-green-700 border-green-400" : k === key ? "bg-yellow-100 text-yellow-700 border-yellow-400 ring-2 ring-yellow-300" : "bg-gray-50 text-gray-600 border-gray-300"}`,
|
|
4820
4856
|
children: k
|
|
4821
4857
|
},
|
|
4822
4858
|
idx
|
|
4823
4859
|
);
|
|
4824
4860
|
}) })
|
|
4825
4861
|
] }),
|
|
4826
|
-
/* @__PURE__ */ jsxs("div", { className: "mb-4", children: [
|
|
4827
|
-
/* @__PURE__ */
|
|
4862
|
+
/* @__PURE__ */ jsxs("div", { className: "mb-6 p-4 bg-gradient-to-r from-violet-50 to-purple-50 rounded-xl border-2 border-violet-200 min-h-[80px]", children: [
|
|
4863
|
+
/* @__PURE__ */ jsx("div", { className: "text-sm font-semibold text-violet-800 mb-2", children: "Hash Calculation" }),
|
|
4864
|
+
operation === "insert" && key ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2", children: [
|
|
4865
|
+
/* @__PURE__ */ jsxs("div", { className: "font-mono text-lg text-gray-800", children: [
|
|
4866
|
+
"hash(",
|
|
4867
|
+
/* @__PURE__ */ jsxs("span", { className: "text-violet-600 font-bold", children: [
|
|
4868
|
+
'"',
|
|
4869
|
+
key,
|
|
4870
|
+
'"'
|
|
4871
|
+
] }),
|
|
4872
|
+
") % ",
|
|
4873
|
+
buckets.length,
|
|
4874
|
+
" = ",
|
|
4875
|
+
/* @__PURE__ */ jsx("span", { className: "text-violet-600 font-bold text-xl", children: bucketIndex })
|
|
4876
|
+
] }),
|
|
4877
|
+
/* @__PURE__ */ jsx("div", { className: "text-violet-600 text-2xl animate-bounce", children: "↓" })
|
|
4878
|
+
] }) : operation === "collision" ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2", children: [
|
|
4879
|
+
/* @__PURE__ */ jsxs("div", { className: "font-mono text-lg text-red-700", children: [
|
|
4880
|
+
"Collision at bucket ",
|
|
4881
|
+
/* @__PURE__ */ jsxs("span", { className: "font-bold", children: [
|
|
4882
|
+
"[",
|
|
4883
|
+
bucketIndex,
|
|
4884
|
+
"]"
|
|
4885
|
+
] }),
|
|
4886
|
+
"! Adding to chain..."
|
|
4887
|
+
] }),
|
|
4888
|
+
/* @__PURE__ */ jsx("div", { className: "text-red-500 text-2xl", children: "⚠️" })
|
|
4889
|
+
] }) : operation === "placed" ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2", children: [
|
|
4890
|
+
/* @__PURE__ */ jsxs("div", { className: "font-mono text-lg text-green-700", children: [
|
|
4891
|
+
/* @__PURE__ */ jsxs("span", { className: "font-bold", children: [
|
|
4892
|
+
'"',
|
|
4893
|
+
key,
|
|
4894
|
+
'"'
|
|
4895
|
+
] }),
|
|
4896
|
+
" placed in bucket ",
|
|
4897
|
+
/* @__PURE__ */ jsxs("span", { className: "font-bold", children: [
|
|
4898
|
+
"[",
|
|
4899
|
+
bucketIndex,
|
|
4900
|
+
"]"
|
|
4901
|
+
] })
|
|
4902
|
+
] }),
|
|
4903
|
+
/* @__PURE__ */ jsx("div", { className: "text-green-500 text-2xl", children: "✓" })
|
|
4904
|
+
] }) : operation === "rehash" ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-2", children: [
|
|
4905
|
+
/* @__PURE__ */ jsx("div", { className: "font-mono text-lg text-orange-700", children: "Rehashing: expanding table..." }),
|
|
4906
|
+
/* @__PURE__ */ jsx("div", { className: "text-orange-500 text-2xl", children: "🔄" })
|
|
4907
|
+
] }) : /* @__PURE__ */ jsx("div", { className: "text-gray-400 text-center", children: "Click Play to start visualization" })
|
|
4908
|
+
] }),
|
|
4909
|
+
/* @__PURE__ */ jsxs("div", { className: "mb-6", children: [
|
|
4910
|
+
/* @__PURE__ */ jsxs("div", { className: "text-sm font-medium text-gray-700 mb-3", children: [
|
|
4828
4911
|
"Hash Table (",
|
|
4829
4912
|
buckets.length,
|
|
4830
4913
|
" buckets):"
|
|
4831
4914
|
] }),
|
|
4832
|
-
/* @__PURE__ */ jsx("div", { className: "
|
|
4915
|
+
/* @__PURE__ */ jsx("div", { className: "overflow-x-auto pb-2", children: /* @__PURE__ */ jsx("div", { className: "flex gap-1", style: { minWidth: "max-content" }, children: buckets.map((bucket, idx) => /* @__PURE__ */ jsxs(
|
|
4833
4916
|
"div",
|
|
4834
4917
|
{
|
|
4835
|
-
className:
|
|
4918
|
+
className: "flex flex-col items-center",
|
|
4919
|
+
style: { minWidth: "70px" },
|
|
4836
4920
|
children: [
|
|
4837
|
-
/* @__PURE__ */ jsxs(
|
|
4838
|
-
"
|
|
4839
|
-
idx,
|
|
4840
|
-
"]"
|
|
4841
|
-
] }),
|
|
4842
|
-
/* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1", children: bucket.length === 0 ? /* @__PURE__ */ jsx("span", { className: "text-xs text-gray-400 italic", children: "empty" }) : bucket.map((bucketKey, keyIdx) => /* @__PURE__ */ jsx(
|
|
4843
|
-
"span",
|
|
4921
|
+
/* @__PURE__ */ jsxs(
|
|
4922
|
+
"div",
|
|
4844
4923
|
{
|
|
4845
|
-
className: `
|
|
4846
|
-
children:
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4924
|
+
className: `w-full text-center py-1 px-2 rounded-t-lg font-mono text-sm font-bold transition-colors ${idx === bucketIndex ? operation === "collision" ? "bg-red-500 text-white" : operation === "placed" ? "bg-green-500 text-white" : "bg-yellow-400 text-yellow-900" : "bg-gray-200 text-gray-600"}`,
|
|
4925
|
+
children: [
|
|
4926
|
+
"[",
|
|
4927
|
+
idx,
|
|
4928
|
+
"]"
|
|
4929
|
+
]
|
|
4930
|
+
}
|
|
4931
|
+
),
|
|
4932
|
+
/* @__PURE__ */ jsx(
|
|
4933
|
+
"div",
|
|
4934
|
+
{
|
|
4935
|
+
className: `w-full border-2 rounded-b-lg min-h-[100px] p-1 transition-colors ${idx === bucketIndex ? operation === "collision" ? "border-red-400 bg-red-50" : operation === "placed" ? "border-green-400 bg-green-50" : "border-yellow-400 bg-yellow-50" : "border-gray-300 bg-gray-50"}`,
|
|
4936
|
+
children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-1", children: bucket.length === 0 ? /* @__PURE__ */ jsx("span", { className: "text-xs text-gray-400 italic text-center py-2", children: "empty" }) : bucket.map((bucketKey, keyIdx) => /* @__PURE__ */ jsx(
|
|
4937
|
+
"div",
|
|
4938
|
+
{
|
|
4939
|
+
className: `px-2 py-1 text-xs font-medium rounded border text-center transition-colors ${bucketKey === key && (operation === "placed" || operation === "insert") ? "bg-yellow-200 text-yellow-900 border-yellow-400" : "bg-blue-100 text-blue-800 border-blue-300"}`,
|
|
4940
|
+
children: bucketKey
|
|
4941
|
+
},
|
|
4942
|
+
keyIdx
|
|
4943
|
+
)) })
|
|
4944
|
+
}
|
|
4945
|
+
)
|
|
4850
4946
|
]
|
|
4851
4947
|
},
|
|
4852
4948
|
idx
|
|
4853
|
-
)) })
|
|
4854
|
-
] }),
|
|
4855
|
-
/* @__PURE__ */ jsxs("div", { className: "mb-4 p-3 bg-gray-100 rounded-lg min-h-[52px]", children: [
|
|
4856
|
-
/* @__PURE__ */ jsx("div", { className: "text-sm font-medium text-gray-700 mb-1", children: "Hash Calculation:" }),
|
|
4857
|
-
/* @__PURE__ */ jsx("div", { className: "font-mono text-sm", children: operation === "insert" && key ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
4858
|
-
'hash("',
|
|
4859
|
-
key,
|
|
4860
|
-
'") mod ',
|
|
4861
|
-
buckets.length,
|
|
4862
|
-
" = ",
|
|
4863
|
-
bucketIndex
|
|
4864
|
-
] }) : /* @__PURE__ */ jsx("span", { className: "text-gray-400", children: "—" }) })
|
|
4949
|
+
)) }) })
|
|
4865
4950
|
] }),
|
|
4866
4951
|
/* @__PURE__ */ jsx(
|
|
4867
4952
|
StatusPanel,
|