sorting_race/models/
algorithm.rs

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