sorting_race/models/
markers.rs

1//! Visual markers for algorithm visualization
2//! 
3//! Re-exports the Markers struct from traits.rs for convenience
4//! and provides additional utilities for marker management.
5
6pub use crate::models::traits::Markers;
7
8/// Helper functions for working with visual markers
9impl Markers {
10    /// Create a new empty markers instance
11    pub fn new() -> Self {
12        Self::default()
13    }
14
15    /// Clear all markers
16    pub fn clear(&mut self) {
17        self.pivot = None;
18        self.heap_boundary = None;
19        self.merge_runs.clear();
20        self.cursors.clear();
21        self.gap = None;
22    }
23
24    /// Set pivot marker for Quick Sort
25    pub fn set_pivot(&mut self, index: usize) {
26        self.pivot = Some(index);
27    }
28
29    /// Clear pivot marker
30    pub fn clear_pivot(&mut self) {
31        self.pivot = None;
32    }
33
34    /// Set heap boundary for Heap Sort
35    pub fn set_heap_boundary(&mut self, boundary: usize) {
36        self.heap_boundary = Some(boundary);
37    }
38
39    /// Clear heap boundary
40    pub fn clear_heap_boundary(&mut self) {
41        self.heap_boundary = None;
42    }
43
44    /// Add a merge run for Merge Sort
45    pub fn add_merge_run(&mut self, start: usize, end: usize) {
46        self.merge_runs.push((start, end));
47    }
48
49    /// Clear all merge runs
50    pub fn clear_merge_runs(&mut self) {
51        self.merge_runs.clear();
52    }
53
54    /// Set cursor positions
55    pub fn set_cursors(&mut self, positions: Vec<usize>) {
56        self.cursors = positions;
57    }
58
59    /// Add a cursor position
60    pub fn add_cursor(&mut self, position: usize) {
61        self.cursors.push(position);
62    }
63
64    /// Clear all cursors
65    pub fn clear_cursors(&mut self) {
66        self.cursors.clear();
67    }
68
69    /// Set gap size for Shell Sort
70    pub fn set_gap(&mut self, gap: usize) {
71        self.gap = Some(gap);
72    }
73
74    /// Clear gap marker
75    pub fn clear_gap(&mut self) {
76        self.gap = None;
77    }
78
79    /// Check if any markers are active
80    pub fn has_active_markers(&self) -> bool {
81        self.pivot.is_some() 
82            || self.heap_boundary.is_some() 
83            || !self.merge_runs.is_empty() 
84            || !self.cursors.is_empty() 
85            || self.gap.is_some()
86    }
87
88    /// Get all active marker positions as a flat vector
89    pub fn get_all_positions(&self) -> Vec<usize> {
90        let mut positions = Vec::new();
91        
92        if let Some(pivot) = self.pivot {
93            positions.push(pivot);
94        }
95        
96        if let Some(boundary) = self.heap_boundary {
97            positions.push(boundary);
98        }
99        
100        positions.extend(self.cursors.iter().copied());
101        
102        for (start, end) in &self.merge_runs {
103            positions.push(*start);
104            positions.push(*end);
105        }
106        
107        positions.sort_unstable();
108        positions.dedup();
109        positions
110    }
111}