summaryrefslogtreecommitdiff
path: root/morphology/examples/test_sorting_directions.rs
blob: 30ea280b0bd8d05fcaf93abc2c247b26a708a267 (plain)
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
// Test program to demonstrate Forward vs Backward sorting directions

use meta_affine::{Sequence, Value};
use meta_affine::sequence::{sort_sequences, SequenceOptions, SortingMethod, SortingDirection};

fn print_sequences(label: &str, sequences: &[Sequence]) {
    println!("\n{}:", label);
    for (i, seq) in sequences.iter().enumerate() {
        print!("  {}: [", i);
        for (j, v) in seq.values().iter().enumerate() {
            if j > 0 { print!(", "); }
            print!("({},{})", v.outer, v.inner);
        }
        println!("]");
    }
}

fn main() {
    // Create test sequences where Forward and Backward will produce different results
    let sequences = vec![
        // Sequence A: (1,0), (0,1), (0,0) - differs at position 0
        Sequence::new(vec![Value::new(1, 0), Value::new(0, 1), Value::new(0, 0)]),
        // Sequence B: (0,1), (1,0), (0,0) - differs at position 0
        Sequence::new(vec![Value::new(0, 1), Value::new(1, 0), Value::new(0, 0)]),
        // Sequence C: (0,0)
        Sequence::new(vec![Value::new(0, 0)]),
    ];

    print_sequences("Original sequences", &sequences);

    // Test Forward DepthFirst (default)
    let mut forward_depth = sequences.clone();
    let opts = SequenceOptions::new()
        .sorting_method(SortingMethod::DepthFirst)
        .sorting_direction(SortingDirection::Forward);
    sort_sequences(&mut forward_depth, opts);

    print_sequences("Forward DepthFirst (compares from first position)", &forward_depth);
    println!("  Explanation: At position 0, compares (1,0) vs (0,1)");
    println!("               Outer values: 0 < 1, so B comes before A");

    // Test Backward DepthFirst
    let mut backward_depth = sequences.clone();
    let opts = SequenceOptions::new()
        .sorting_method(SortingMethod::DepthFirst)
        .sorting_direction(SortingDirection::Backward);
    sort_sequences(&mut backward_depth, opts);

    print_sequences("Backward DepthFirst (compares from last position)", &backward_depth);
    println!("  Explanation: Starting from last position (both end with (0,0))");
    println!("               Position -2 (second to last): compares (0,1) vs (1,0)");
    println!("               Outer values: 0 < 1, so A comes before B");

    // Test Forward BreadthFirst
    let mut forward_breadth = sequences.clone();
    let opts = SequenceOptions::new()
        .sorting_method(SortingMethod::BreadthFirst)
        .sorting_direction(SortingDirection::Forward);
    sort_sequences(&mut forward_breadth, opts);

    print_sequences("Forward BreadthFirst", &forward_breadth);
    println!("  Explanation: Compare all outer values [1,0,0] vs [0,1,0]");
    println!("               At position 0: 0 < 1, so B comes before A");

    // Test Backward BreadthFirst
    let mut backward_breadth = sequences.clone();
    let opts = SequenceOptions::new()
        .sorting_method(SortingMethod::BreadthFirst)
        .sorting_direction(SortingDirection::Backward);
    sort_sequences(&mut backward_breadth, opts);

    print_sequences("Backward BreadthFirst", &backward_breadth);
    println!("  Explanation: Compare all outer values reversed [0,0,1] vs [0,1,0]");
    println!("               At position 0 (from end): both are 0");
    println!("               At position 1 (from end): 0 < 1, so A comes before B");

    println!("\n=== Summary ===");
    println!("Forward: Compares sequences starting from the first position");
    println!("Backward: Compares sequences starting from the last position");
    println!("This can produce different orderings depending on where differences occur.");
}