sorting_race/models/
algorithm.rs

1//! Algorithm state management
2
3/// Algorithm execution state
4#[derive(Debug, Clone, PartialEq, Default)]
5pub enum AlgorithmState {
6    /// Algorithm is ready to start
7    #[default]
8    Ready,
9    /// Algorithm is actively executing
10    Running,
11    /// Algorithm has completed successfully
12    Complete,
13    /// Algorithm encountered an error
14    Error(String),
15}
16
17/// Algorithm configuration and state tracking
18#[derive(Debug, Default)]
19pub struct Algorithm {
20    /// Current execution state
21    pub state: AlgorithmState,
22    /// Number of steps executed
23    pub steps_executed: usize,
24    /// Total runtime in milliseconds
25    pub runtime_ms: u64,
26    /// Whether algorithm is paused
27    pub is_paused: bool,
28}
29
30impl Algorithm {
31    /// Create a new algorithm state
32    pub fn new() -> Self {
33        Self::default()
34    }
35
36    /// Reset algorithm to initial state
37    pub fn reset(&mut self) {
38        self.state = AlgorithmState::Ready;
39        self.steps_executed = 0;
40        self.runtime_ms = 0;
41        self.is_paused = false;
42    }
43
44    /// Check if algorithm can execute a step
45    pub fn can_step(&self) -> bool {
46        matches!(self.state, AlgorithmState::Ready | AlgorithmState::Running) && !self.is_paused
47    }
48
49    /// Mark algorithm as complete
50    pub fn complete(&mut self) {
51        self.state = AlgorithmState::Complete;
52    }
53
54    /// Mark algorithm as running
55    pub fn start(&mut self) {
56        self.state = AlgorithmState::Running;
57    }
58
59    /// Set error state
60    pub fn set_error(&mut self, error: String) {
61        self.state = AlgorithmState::Error(error);
62    }
63
64    /// Increment step counter
65    pub fn increment_steps(&mut self) {
66        self.steps_executed += 1;
67    }
68
69    /// Add to runtime
70    pub fn add_runtime(&mut self, ms: u64) {
71        self.runtime_ms += ms;
72    }
73
74    /// Toggle pause state
75    pub fn toggle_pause(&mut self) {
76        self.is_paused = !self.is_paused;
77    }
78}