|
|
""" |
|
|
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
β Models: step.py β |
|
|
β Classes for representing algorithm execution steps β |
|
|
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
|
|
|
|
|
This module contains: |
|
|
β’ StepType (Enum) - Types of operations (compare, swap, merge, etc.) |
|
|
β’ Step (dataclass) - A single step in algorithm execution |
|
|
|
|
|
π WHY RECORD STEPS? |
|
|
To visualize algorithms step-by-step, we need to RECORD what happens. |
|
|
Each Step captures: |
|
|
- WHAT operation occurred (compare, swap, etc.) |
|
|
- WHERE it happened (which indices) |
|
|
- WHEN in the process (depth for recursive algorithms) |
|
|
- Additional context (metadata) |
|
|
|
|
|
This is the "data" that visualization will render. |
|
|
""" |
|
|
|
|
|
from dataclasses import dataclass, field |
|
|
from enum import Enum, auto |
|
|
from typing import List, TYPE_CHECKING |
|
|
|
|
|
|
|
|
if TYPE_CHECKING: |
|
|
from oop_sorting_teaching.models.gesture import GestureImage |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StepType(Enum): |
|
|
""" |
|
|
Types of algorithm steps that can be visualized. |
|
|
|
|
|
Each step in our sorting/searching algorithms will have a type |
|
|
that determines how it's displayed in the visualization. |
|
|
|
|
|
π CONCEPT: auto() |
|
|
|
|
|
The auto() function automatically assigns incrementing values. |
|
|
We don't care what the actual numbers are - we just need |
|
|
unique identifiers for each step type. |
|
|
""" |
|
|
|
|
|
COMPARE = auto() |
|
|
|
|
|
|
|
|
SWAP = auto() |
|
|
MOVE = auto() |
|
|
|
|
|
|
|
|
SPLIT = auto() |
|
|
MERGE = auto() |
|
|
|
|
|
|
|
|
PIVOT_SELECT = auto() |
|
|
PARTITION = auto() |
|
|
|
|
|
|
|
|
SEARCH_RANGE = auto() |
|
|
NARROW_LEFT = auto() |
|
|
NARROW_RIGHT = auto() |
|
|
FOUND = auto() |
|
|
NOT_FOUND = auto() |
|
|
|
|
|
|
|
|
PASS_COMPLETE = auto() |
|
|
COMPLETE = auto() |
|
|
MARK_SORTED = auto() |
|
|
|
|
|
|
|
|
INSTABILITY = auto() |
|
|
INSTABILITY_WARNING = auto() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
class Step: |
|
|
""" |
|
|
Represents a single step in an algorithm's execution. |
|
|
|
|
|
This is used to record what the algorithm is doing at each point, |
|
|
so we can visualize it step by step. |
|
|
|
|
|
Think of it like frames in a movie: |
|
|
- Each Step is one frame |
|
|
- Together they tell the story of the algorithm |
|
|
|
|
|
Attributes: |
|
|
step_type: What kind of operation (compare, swap, merge, etc.) |
|
|
indices: Which array positions are involved |
|
|
description: Human-readable explanation |
|
|
depth: Recursion depth (for merge sort / quick sort) |
|
|
array_state: Copy of the array at this step |
|
|
highlight_indices: Extra indices to highlight (e.g., sorted region) |
|
|
metadata: Additional algorithm-specific data |
|
|
|
|
|
Example: |
|
|
step = Step( |
|
|
step_type=StepType.COMPARE, |
|
|
indices=[3, 4], |
|
|
description="Comparing elements at positions 3 and 4", |
|
|
depth=0, |
|
|
array_state=[...], |
|
|
metadata={"comparison_count": 5} |
|
|
) |
|
|
""" |
|
|
step_type: StepType |
|
|
indices: List[int] |
|
|
description: str |
|
|
depth: int = 0 |
|
|
array_state: List['GestureImage'] = field(default_factory=list) |
|
|
highlight_indices: List[int] = field(default_factory=list) |
|
|
metadata: dict = field(default_factory=dict) |
|
|
|
|
|
@property |
|
|
def type(self) -> StepType: |
|
|
""" |
|
|
Alias for step_type for cleaner access in renderers. |
|
|
|
|
|
This allows: step.type instead of step.step_type |
|
|
Makes the code more readable in visualization code. |
|
|
""" |
|
|
return self.step_type |
|
|
|
|
|
def __str__(self) -> str: |
|
|
"""Human-readable string for debugging.""" |
|
|
indices_str = ', '.join(str(i) for i in self.indices) |
|
|
return f"[{self.step_type.name}] indices=[{indices_str}] depth={self.depth}: {self.description}" |
|
|
|