Skip to content

Commit 6b6d140

Browse files
committed
SortAnimation 수정
1 parent eb63e66 commit 6b6d140

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

src/components/ide/animations/SortAnimation.jsx

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ const SortAnimation = ({
1111
const svgRef = useRef(null);
1212
const [intervalId, setIntervalId] = useState(null);
1313

14-
// 🎯 알고리즘 추론 함수
15-
const inferAlgorithm = (steps) => {
16-
if (!steps) return 'unknown';
17-
if (steps.some(step => step.condition?.expression?.includes('pivot'))) return 'quick';
18-
if (steps.some(step => step.condition?.expression?.includes('merge') || step.description?.includes('merge'))) return 'merge';
19-
if (steps.some(step => step.changes?.some(c => c.variable === 'min'))) return 'selection';
20-
if (steps.some(step => step.condition?.expression?.includes('list[i] < list[j-1]'))) return 'insertion';
21-
if (steps.some(step => step.condition?.expression?.includes('list[i] > list[j]'))) return 'bubble';
22-
return 'unknown';
14+
// 🎯 함수 이름에서 알고리즘 매핑
15+
const getAlgorithmFromFunction = (functions) => {
16+
if (!functions || !functions[0]?.name) return 'unknown';
17+
const functionName = functions[0].name.toLowerCase();
18+
const algorithmMap = {
19+
'bubble_sort': 'bubble',
20+
'selection_sort': 'selection',
21+
'insertion_sort': 'insertion',
22+
'merge_sort': 'merge',
23+
'quick_sort': 'quick',
24+
'heap_sort': 'heap'
25+
};
26+
return algorithmMap[functionName] || 'unknown';
2327
};
2428

29+
const algorithm = getAlgorithmFromFunction(data?.functions);
30+
2531
console.log('🔄 SortAnimation 렌더링:', {
26-
inferredAlgorithm: inferAlgorithm(data?.steps),
32+
functionName: data?.functions?.[0]?.name || 'none',
33+
algorithm,
2734
currentStep,
2835
totalSteps,
2936
hasData: !!data,
@@ -32,7 +39,6 @@ const SortAnimation = ({
3239

3340
// 🎯 Generic step conversion function
3441
const convertSteps = (rawSteps) => {
35-
const algorithm = inferAlgorithm(rawSteps);
3642
const result = [];
3743
let currentList = [];
3844

@@ -44,44 +50,44 @@ const SortAnimation = ({
4450
const change = step.changes?.find(c => c.variable === 'list');
4551
if (change?.after) currentList = change.after;
4652

53+
// 기본적으로 JSON의 description 사용
54+
let description = step.description || '단계 처리 중';
55+
4756
if (step.condition) {
48-
let description = '';
4957
const match = step.condition.expression?.match(/list\[(\d+)\](?:.*list\[(\d+)\])?/) || [];
5058
const indices = match ? [parseInt(match[1]), parseInt(match[2])].filter(i => !isNaN(i)) : [];
5159

5260
if (algorithm === 'bubble' || algorithm === 'selection' || algorithm === 'insertion') {
5361
const resultStr = step.condition.result ? 'true' : 'false';
54-
const isSwap = next?.description?.includes('list') ?? false;
62+
const isSwap = next?.changes?.some(c => c.variable === 'list') ?? false;
5563
const action = isSwap ? '→ 교환' : '→ 교환 없음';
5664
if (indices.length === 2) {
57-
description = `list[${indices[0]}]=${currentList[indices[0]]}, list[${indices[1]}]=${currentList[indices[1]]} 비교 → ${resultStr} ${action}`;
65+
description = `${description} (list[${indices[0]}]=${currentList[indices[0]]}, list[${indices[1]}]=${currentList[indices[1]]} 비교 → ${resultStr} ${action})`;
5866
} else if (indices.length === 1) {
59-
description = `list[${indices[0]}]=${currentList[indices[0]]} 처리 → ${resultStr} ${action}`;
67+
description = `${description} (list[${indices[0]}]=${currentList[indices[0]]} 처리 → ${resultStr} ${action})`;
6068
}
6169
} else if (algorithm === 'quick') {
62-
description = step.condition?.expression?.includes('pivot')
70+
const quickDesc = step.condition?.expression?.includes('pivot')
6371
? `피벗 list[${indices[0]}]=${currentList[indices[0]]} 처리`
6472
: indices.length > 0
6573
? `list[${indices.join(', ')}] 파티션 처리`
6674
: '파티션 처리 중';
75+
description = `${description} (${quickDesc})`;
6776
} else if (algorithm === 'merge') {
68-
description = step.condition?.expression?.includes('merge') || step.description?.includes('merge')
77+
const mergeDesc = step.condition?.expression?.includes('merge') || step.description?.includes('merge')
6978
? `구간 [${indices.join(', ')}] 병합`
7079
: indices.length > 0
7180
? `list[${indices.join(', ')}] 분할`
7281
: '분할/병합 중';
73-
} else {
74-
description = step.description ||
75-
(indices.length > 0
76-
? `list[${indices.join(', ')}] 처리`
77-
: '배열 처리 중');
82+
description = `${description} (${mergeDesc})`;
83+
} else if (indices.length > 0) {
84+
description = `${description} (list[${indices.join(', ')}] 처리)`;
7885
}
79-
80-
newStep.description = description;
8186
} else if (step.description?.includes('정렬된 배열 출력')) {
82-
newStep.description = '정렬 완료: 최종 배열 출력';
87+
description = '정렬 완료: 최종 배열 출력';
8388
}
8489

90+
newStep.description = description;
8591
result.push(newStep);
8692
}
8793

@@ -93,7 +99,7 @@ const SortAnimation = ({
9399

94100
if (data?.variables) {
95101
data.variables.forEach(v => {
96-
vars[v.name] = v.initialValue;
102+
vars[v.name] = v.currentValue ?? v.initialValue;
97103
});
98104
}
99105

@@ -121,7 +127,6 @@ const SortAnimation = ({
121127
if (!data?.steps) return;
122128

123129
const steps = convertSteps(data.steps);
124-
const algorithm = inferAlgorithm(data.steps);
125130
const step = steps[stepIndex];
126131
if (!step) return;
127132

@@ -137,7 +142,6 @@ const SortAnimation = ({
137142
const comparingIndices = match ? [parseInt(match[1]), parseInt(match[2])].filter(i => !isNaN(i)) : [];
138143
const isFinalStep = step.description?.includes('정렬 완료');
139144

140-
// Handle pivot for quicksort
141145
const pivotIndex = step.condition?.expression?.includes('pivot') && comparingIndices.length > 0
142146
? comparingIndices[0]
143147
: null;
@@ -153,7 +157,7 @@ const SortAnimation = ({
153157
if (isFinalStep) fill = '#2ecc71';
154158
else if (changedIndices.includes(i)) fill = '#845ef7';
155159
else if (comparingIndices.includes(i)) fill = '#f1c40f';
156-
else if (i === pivotIndex) fill = '#e74c3c'; // Red for pivot
160+
else if (i === pivotIndex) fill = '#e74c3c';
157161

158162
const rect = group
159163
.append('rect')
@@ -248,7 +252,6 @@ const SortAnimation = ({
248252
}
249253
}
250254

251-
// Draw merge sort partitions
252255
if (algorithm === 'merge' && step.condition?.expression?.includes('merge')) {
253256
const match = step.condition.expression.match(/\[(\d+):(\d+)\]/);
254257
if (match) {
@@ -295,7 +298,6 @@ const SortAnimation = ({
295298
const steps = data?.steps ? convertSteps(data.steps) : [];
296299
const currentVariables = data?.steps ? getVariableStateAt(currentStep, steps) : {};
297300
const stepData = steps[currentStep];
298-
const algorithm = inferAlgorithm(data?.steps);
299301

300302
return (
301303
<div style={{
@@ -471,7 +473,8 @@ const SortAnimation = ({
471473
maxHeight: '150px'
472474
}}>
473475
{JSON.stringify({
474-
inferredAlgorithm: algorithm,
476+
functionName: data?.functions?.[0]?.name || 'none',
477+
algorithm,
475478
currentStep,
476479
totalSteps,
477480
currentVariables,

0 commit comments

Comments
 (0)