sorting-race 0.2.0

Terminal-based sorting algorithm race visualization tool with real-time metrics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Contract tests for operation highlighting system
//!
//! These tests verify the expected interface and behavior of the highlighting system
//! that integrates with the Telemetry markers to show algorithm operations.

use std::collections::HashMap;

/// Color types for different operation highlights
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HighlightColor {
    Red,    // For swaps/moves
    Blue,   // For comparisons
    Green,  // For sorted elements
    Yellow, // For pivot elements
    Purple, // For special operations
}

/// Priority levels for highlight colors when operations overlap
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum HighlightPriority {
    Low = 1,
    Medium = 2,
    High = 3,
    Critical = 4,
}

/// A single highlight with color and priority
#[derive(Debug, Clone)]
pub struct Highlight {
    pub color: HighlightColor,
    pub priority: HighlightPriority,
    pub operation_type: String,
}

impl Highlight {
    pub fn new(color: HighlightColor, priority: HighlightPriority, operation_type: &str) -> Self {
        Self {
            color,
            priority,
            operation_type: operation_type.to_string(),
        }
    }
}

/// Highlighting system that manages operation visualization
#[derive(Debug)]
pub struct HighlightSystem {
    /// Current highlights by array index
    highlights: HashMap<usize, Vec<Highlight>>,
    /// Default color mapping for operation types
    color_map: HashMap<String, (HighlightColor, HighlightPriority)>,
}

impl HighlightSystem {
    /// Create a new highlight system with default mappings
    pub fn new() -> Self {
        let mut color_map = HashMap::new();

        // Default operation -> (color, priority) mappings
        color_map.insert(
            "compare".to_string(),
            (HighlightColor::Blue, HighlightPriority::Medium),
        );
        color_map.insert(
            "swap".to_string(),
            (HighlightColor::Red, HighlightPriority::High),
        );
        color_map.insert(
            "move".to_string(),
            (HighlightColor::Red, HighlightPriority::High),
        );
        color_map.insert(
            "pivot".to_string(),
            (HighlightColor::Yellow, HighlightPriority::Critical),
        );
        color_map.insert(
            "sorted".to_string(),
            (HighlightColor::Green, HighlightPriority::Low),
        );
        color_map.insert(
            "cursor".to_string(),
            (HighlightColor::Purple, HighlightPriority::Medium),
        );

        Self {
            highlights: HashMap::new(),
            color_map,
        }
    }

    /// Clear all current highlights
    pub fn clear(&mut self) {
        self.highlights.clear();
    }

    /// Add highlight indices for comparisons
    pub fn highlight_comparison(&mut self, indices: &[usize]) {
        for &index in indices {
            let highlight =
                Highlight::new(HighlightColor::Blue, HighlightPriority::Medium, "compare");
            self.highlights
                .entry(index)
                .or_default()
                .push(highlight);
        }
    }

    /// Add highlight indices for swaps
    pub fn highlight_swap(&mut self, indices: &[usize]) {
        for &index in indices {
            let highlight = Highlight::new(HighlightColor::Red, HighlightPriority::High, "swap");
            self.highlights
                .entry(index)
                .or_default()
                .push(highlight);
        }
    }

    /// Add highlight for a specific operation type
    pub fn highlight_operation(&mut self, operation_type: &str, indices: &[usize]) {
        let (color, priority) = self
            .color_map
            .get(operation_type)
            .copied()
            .unwrap_or((HighlightColor::Purple, HighlightPriority::Medium));

        for &index in indices {
            let highlight = Highlight::new(color, priority, operation_type);
            self.highlights
                .entry(index)
                .or_default()
                .push(highlight);
        }
    }

    /// Get the effective highlight for an index (highest priority wins)
    pub fn get_highlight(&self, index: usize) -> Option<&Highlight> {
        if let Some(highlights) = self.highlights.get(&index) {
            // Return the highest priority highlight
            highlights.iter().max_by_key(|h| h.priority)
        } else {
            None
        }
    }

    /// Get all highlighted indices
    pub fn get_highlighted_indices(&self) -> Vec<usize> {
        let mut indices: Vec<usize> = self.highlights.keys().copied().collect();
        indices.sort();
        indices
    }

    /// Get highlights for multiple indices at once
    pub fn get_highlights_for_indices(
        &self,
        indices: &[usize],
    ) -> HashMap<usize, Option<&Highlight>> {
        indices
            .iter()
            .map(|&index| (index, self.get_highlight(index)))
            .collect()
    }

    /// Check if an index has any highlights
    pub fn is_highlighted(&self, index: usize) -> bool {
        self.highlights.contains_key(&index) && !self.highlights[&index].is_empty()
    }

    /// Add multiple simultaneous highlights for different operations
    pub fn add_simultaneous_highlights(&mut self, operations: &[(String, Vec<usize>)]) {
        for (operation_type, indices) in operations {
            self.highlight_operation(operation_type, indices);
        }
    }

    /// Update color mapping for operation types
    pub fn set_color_mapping(
        &mut self,
        operation_type: &str,
        color: HighlightColor,
        priority: HighlightPriority,
    ) {
        self.color_map
            .insert(operation_type.to_string(), (color, priority));
    }

    /// Get color mapping for operation type
    pub fn get_color_mapping(
        &self,
        operation_type: &str,
    ) -> Option<(HighlightColor, HighlightPriority)> {
        self.color_map.get(operation_type).copied()
    }

    /// Get total number of highlighted positions
    pub fn highlight_count(&self) -> usize {
        self.highlights.len()
    }

    /// Get all unique operation types currently highlighted
    pub fn get_active_operation_types(&self) -> Vec<String> {
        let mut types = std::collections::HashSet::new();

        for highlights in self.highlights.values() {
            for highlight in highlights {
                types.insert(highlight.operation_type.clone());
            }
        }

        let mut result: Vec<String> = types.into_iter().collect();
        result.sort();
        result
    }

    /// Create highlights from telemetry data
    pub fn from_telemetry_highlights(
        &mut self,
        telemetry_highlights: &[usize],
        operation_type: &str,
    ) {
        self.highlight_operation(operation_type, telemetry_highlights);
    }
}

impl Default for HighlightSystem {
    fn default() -> Self {
        Self::new()
    }
}

/// Integration with Telemetry markers for operation highlighting
pub struct TelemetryHighlightIntegration {
    highlight_system: HighlightSystem,
}

impl Default for TelemetryHighlightIntegration {
    fn default() -> Self {
        Self::new()
    }
}

impl TelemetryHighlightIntegration {
    pub fn new() -> Self {
        Self {
            highlight_system: HighlightSystem::new(),
        }
    }

    /// Update highlights based on telemetry markers
    pub fn update_from_telemetry(&mut self, telemetry: &sorting_race::models::traits::Telemetry) {
        // Clear previous highlights
        self.highlight_system.clear();

        // Add highlights from telemetry
        if !telemetry.highlights.is_empty() {
            // Determine operation type from context (this is simplified)
            let operation_type = if telemetry.status_text.contains("comparing") {
                "compare"
            } else if telemetry.status_text.contains("swapping") {
                "swap"
            } else {
                "operation"
            };

            self.highlight_system
                .from_telemetry_highlights(&telemetry.highlights, operation_type);
        }

        // Add markers-specific highlights
        if let Some(pivot) = telemetry.markers.pivot {
            self.highlight_system.highlight_operation("pivot", &[pivot]);
        }

        for &cursor in &telemetry.markers.cursors {
            self.highlight_system
                .highlight_operation("cursor", &[cursor]);
        }

        // Add merge runs
        for &(start, end) in &telemetry.markers.merge_runs {
            let indices: Vec<usize> = (start..=end).collect();
            self.highlight_system.highlight_operation("merge", &indices);
        }
    }

    pub fn get_highlight_system(&self) -> &HighlightSystem {
        &self.highlight_system
    }

    pub fn get_highlight_system_mut(&mut self) -> &mut HighlightSystem {
        &mut self.highlight_system
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sorting_race::models::traits::{Markers, Telemetry};

    #[test]
    fn test_highlight_indices_for_comparisons() {
        let mut system = HighlightSystem::new();

        // Test single comparison
        system.highlight_comparison(&[3, 7]);

        assert!(system.is_highlighted(3));
        assert!(system.is_highlighted(7));
        assert!(!system.is_highlighted(5));

        let highlight_3 = system.get_highlight(3).unwrap();
        assert_eq!(highlight_3.color, HighlightColor::Blue);
        assert_eq!(highlight_3.operation_type, "compare");
    }

    #[test]
    fn test_highlight_indices_for_swaps() {
        let mut system = HighlightSystem::new();

        system.highlight_swap(&[2, 8]);

        assert!(system.is_highlighted(2));
        assert!(system.is_highlighted(8));

        let highlight_2 = system.get_highlight(2).unwrap();
        assert_eq!(highlight_2.color, HighlightColor::Red);
        assert_eq!(highlight_2.operation_type, "swap");
        assert_eq!(highlight_2.priority, HighlightPriority::High);
    }

    #[test]
    fn test_multiple_simultaneous_highlights() {
        let mut system = HighlightSystem::new();

        // Add multiple operations simultaneously
        let operations = vec![
            ("compare".to_string(), vec![1, 2]),
            ("pivot".to_string(), vec![5]),
            ("sorted".to_string(), vec![8, 9, 10]),
        ];

        system.add_simultaneous_highlights(&operations);

        assert_eq!(system.highlight_count(), 6); // 2 + 1 + 3 unique indices

        // Check specific highlights
        let compare_highlight = system.get_highlight(1).unwrap();
        assert_eq!(compare_highlight.color, HighlightColor::Blue);

        let pivot_highlight = system.get_highlight(5).unwrap();
        assert_eq!(pivot_highlight.color, HighlightColor::Yellow);

        let sorted_highlight = system.get_highlight(8).unwrap();
        assert_eq!(sorted_highlight.color, HighlightColor::Green);
    }

    #[test]
    fn test_color_priorities_when_operations_overlap() {
        let mut system = HighlightSystem::new();

        // Add overlapping operations at index 3
        system.highlight_comparison(&[3]); // Medium priority, blue
        system.highlight_swap(&[3]); // High priority, red
        system.highlight_operation("sorted", &[3]); // Low priority, green

        // High priority swap should win
        let effective_highlight = system.get_highlight(3).unwrap();
        assert_eq!(effective_highlight.color, HighlightColor::Red);
        assert_eq!(effective_highlight.priority, HighlightPriority::High);
        assert_eq!(effective_highlight.operation_type, "swap");
    }

    #[test]
    fn test_critical_priority_overrides_all() {
        let mut system = HighlightSystem::new();

        // Add multiple operations including critical priority
        system.highlight_comparison(&[5]);
        system.highlight_swap(&[5]);
        system.highlight_operation("pivot", &[5]); // Critical priority

        let effective_highlight = system.get_highlight(5).unwrap();
        assert_eq!(effective_highlight.color, HighlightColor::Yellow);
        assert_eq!(effective_highlight.priority, HighlightPriority::Critical);
        assert_eq!(effective_highlight.operation_type, "pivot");
    }

    #[test]
    fn test_get_highlighted_indices() {
        let mut system = HighlightSystem::new();

        system.highlight_comparison(&[1, 5]);
        system.highlight_swap(&[3, 7]);
        system.highlight_operation("pivot", &[2]);

        let indices = system.get_highlighted_indices();
        assert_eq!(indices, vec![1, 2, 3, 5, 7]); // Should be sorted
    }

    #[test]
    fn test_clear_highlights() {
        let mut system = HighlightSystem::new();

        system.highlight_comparison(&[1, 2, 3]);
        system.highlight_swap(&[4, 5]);

        assert_eq!(system.highlight_count(), 5);

        system.clear();

        assert_eq!(system.highlight_count(), 0);
        assert!(!system.is_highlighted(1));
        assert!(!system.is_highlighted(4));
    }

    #[test]
    fn test_custom_color_mapping() {
        let mut system = HighlightSystem::new();

        // Add custom operation type
        system.set_color_mapping("custom_op", HighlightColor::Purple, HighlightPriority::High);

        system.highlight_operation("custom_op", &[10]);

        let highlight = system.get_highlight(10).unwrap();
        assert_eq!(highlight.color, HighlightColor::Purple);
        assert_eq!(highlight.priority, HighlightPriority::High);
        assert_eq!(highlight.operation_type, "custom_op");
    }

    #[test]
    fn test_get_highlights_for_multiple_indices() {
        let mut system = HighlightSystem::new();

        system.highlight_comparison(&[1, 3]);
        system.highlight_swap(&[2]);

        let query_indices = vec![1, 2, 3, 4];
        let results = system.get_highlights_for_indices(&query_indices);

        assert!(results[&1].is_some());
        assert!(results[&2].is_some());
        assert!(results[&3].is_some());
        assert!(results[&4].is_none());

        assert_eq!(results[&1].unwrap().color, HighlightColor::Blue);
        assert_eq!(results[&2].unwrap().color, HighlightColor::Red);
    }

    #[test]
    fn test_active_operation_types() {
        let mut system = HighlightSystem::new();

        system.highlight_comparison(&[1]);
        system.highlight_swap(&[2]);
        system.highlight_operation("pivot", &[3]);
        system.highlight_operation("pivot", &[4]); // Duplicate type

        let active_types = system.get_active_operation_types();
        assert_eq!(active_types, vec!["compare", "pivot", "swap"]); // Should be sorted and unique
    }

    #[test]
    fn test_integration_with_telemetry_markers() {
        let mut integration = TelemetryHighlightIntegration::new();

        let telemetry = Telemetry {
            total_comparisons: 10,
            total_moves: 5,
            memory_current: 1024,
            memory_peak: 2048,
            highlights: vec![2, 5, 8], // Telemetry highlights
            markers: Markers {
                pivot: Some(3),
                heap_boundary: None,
                merge_runs: vec![(6, 9)],
                cursors: vec![1, 7],
                gap: None,
            },
            status_text: "comparing elements".to_string(),
            progress_hint: 0.5,
        };

        integration.update_from_telemetry(&telemetry);
        let system = integration.get_highlight_system();

        // Should have highlights from telemetry
        assert!(system.is_highlighted(2));
        assert!(system.is_highlighted(5));
        assert!(system.is_highlighted(8));

        // Should have pivot highlight
        assert!(system.is_highlighted(3));
        let pivot_highlight = system.get_highlight(3).unwrap();
        assert_eq!(pivot_highlight.color, HighlightColor::Yellow);

        // Should have cursor highlights
        assert!(system.is_highlighted(1));
        assert!(system.is_highlighted(7));

        // Should have merge run highlights
        for i in 6..=9 {
            assert!(system.is_highlighted(i));
        }
    }

    #[test]
    fn test_telemetry_operation_detection() {
        let mut integration = TelemetryHighlightIntegration::new();

        // Test swap detection
        let swap_telemetry = Telemetry {
            total_comparisons: 5,
            total_moves: 3,
            memory_current: 512,
            memory_peak: 1024,
            highlights: vec![4, 7],
            markers: Markers::default(),
            status_text: "swapping elements at positions 4 and 7".to_string(),
            progress_hint: 0.3,
        };

        integration.update_from_telemetry(&swap_telemetry);
        let system = integration.get_highlight_system();

        let highlight_4 = system.get_highlight(4).unwrap();
        assert_eq!(highlight_4.color, HighlightColor::Red); // Should be red for swap
    }

    #[test]
    fn test_no_highlights_scenario() {
        let system = HighlightSystem::new();

        assert_eq!(system.highlight_count(), 0);
        assert!(system.get_highlighted_indices().is_empty());
        assert!(system.get_active_operation_types().is_empty());
        assert!(!system.is_highlighted(0));
        assert!(system.get_highlight(0).is_none());
    }

    #[test]
    fn test_same_index_multiple_operations_different_priority() {
        let mut system = HighlightSystem::new();

        // Add operations in order of increasing priority
        system.highlight_operation("sorted", &[5]); // Low
        system.highlight_comparison(&[5]); // Medium  
        system.highlight_swap(&[5]); // High

        // Should show the highest priority (swap)
        let highlight = system.get_highlight(5).unwrap();
        assert_eq!(highlight.priority, HighlightPriority::High);
        assert_eq!(highlight.operation_type, "swap");

        // Now add critical priority
        system.highlight_operation("pivot", &[5]); // Critical

        let highlight = system.get_highlight(5).unwrap();
        assert_eq!(highlight.priority, HighlightPriority::Critical);
        assert_eq!(highlight.operation_type, "pivot");
    }

    #[test]
    fn test_large_index_values() {
        let mut system = HighlightSystem::new();

        let large_indices = vec![1000, 5000, 10000];
        system.highlight_comparison(&large_indices);

        for &index in &large_indices {
            assert!(system.is_highlighted(index));
        }

        let highlighted = system.get_highlighted_indices();
        assert_eq!(highlighted, large_indices);
    }
}