Skip to main content

sort_forward

Function sort_forward 

Source
pub fn sort_forward<T, V, R: RangeBounds<T>, S: RangeBounds<T>, C: IncDecCpCmp<T, V>>(
    x: &R,
    y: &S,
    rebound: &V,
    t: &C,
) -> Ordering
Expand description

Compares range a and b and returns the Forward Consolidation Order std::cmp::Ordering value.

The sort order is meant to represent Forward Consolidation Order not tradtional range sort order. Forward Consolidation Order is represented as earliest largest ranges first.

Put another way:

  • GetBeginEnd.get_begin() asc
  • GetBeginEnd.get_end() desc

ยงPanics!

If the RangeBounds cannot be converted to a computable range!

Examples found in repository?
examples/columns.rs (line 15)
3fn main() {
4    // We create all of our column data unsorted
5    let mut col_a = vec![0..=11, 2..=3, 7..=9, 22..=33, 34..=39];
6    let mut col_b = vec![6..=9, 6..=9, 6..=7, 11..=22, 7..=11, 9..=9];
7    let mut col_c = vec![3..=4, 3..=9, 4..=6, 30..=41];
8
9    // ** Full Sort Example here! **
10    // We will use this to drive the internals of the sort function
11    let t = NumberIncDecCpCmp::defaults();
12
13    // We create our sort function here
14    let sort_by = |a: &std::ops::RangeInclusive<i32>, b: &std::ops::RangeInclusive<i32>| {
15        sort_forward(a, b, &t.default_rebound(), &t)
16    };
17
18    // Sort all of our rows and force them to exist in the correct order!
19    col_a.sort_by(sort_by);
20    col_b.sort_by(sort_by);
21    col_c.sort_by(sort_by);
22    // ** End Full Sort Example **
23
24    // Create our Columns instance using number defaults.
25    let cols = Columns::num_defaults();
26
27    // give up if we fail to add a column!
28    assert!(cols.add_column(col_a.into_iter()).is_ok());
29    assert!(cols.add_column(col_b.into_iter()).is_ok());
30    assert!(cols.add_column(col_c.into_iter()).is_ok());
31
32    // Just pretty printing our text table border
33    println!(
34        "+---------+-----------+{:-<35}+{:-<61}+{:-<35}+",
35        "", "", ""
36    );
37
38    // Pretty preint our text table header
39    println!(
40        "| Overlap | State(id) |{:^35}|{:^61}|{:^35}|",
41        "Column(A)", "Column(B)", "Column(C)"
42    );
43
44    // In order to access the iter.get_column(column_id) method, the iter instance must remain in scope.
45    // If access to the causal ranges is not required, then a standard for lopp iterator will work.
46    let mut iter = cols.into_iter();
47    let mut id = 0;
48    loop {
49        let next = iter.next();
50        if next.is_none() {
51            // print out the last text bumper.
52            println!(
53                "+---------+-----------+{:-<35}+{:-<61}+{:-<35}+",
54                "", "", ""
55            );
56            return;
57        }
58        let (overlap, res, columns) = next.unwrap();
59        // print a bumper text row.
60        println!(
61            "+---------+-----------+{:-<35}+{:-<61}+{:-<35}+",
62            "", "", ""
63        );
64
65        // Print out the common intersecting range!
66        print!("|  {:^2}->{:^2} |", overlap.get_begin(), overlap.get_end());
67        let mut stop = false;
68        if res.is_err() {
69            print!("   Err({})  |", id);
70            // We still want to access the column or columns that error out before we stop
71            stop = true;
72        } else {
73            print!("   Ok({})   |", id);
74        }
75        for (column_id, col) in columns.iter().enumerate() {
76            let mut txt = Vec::new();
77            match col {
78                Ok(src) => {
79                    for row in src {
80                        // This range contains all of the ranges that were used to create it!
81                        let container = row.as_ref();
82                        txt.push(format!(
83                            "[{}->{}](",
84                            container.get_begin(),
85                            container.get_end()
86                        ));
87                        let mut r = Vec::new();
88
89                        // walk our raw source ranges that caused this larger range
90                        for (row_id, range) in container.src().iter() {
91                            r.push(format!("{}({}->{})", row_id, range.start(), range.end()));
92                        }
93                        txt.push(r.join(","));
94                        txt.push(String::from(")"));
95                    }
96                }
97                Err(msg) => {
98                    // This code exists but does not execute in this example.
99                    // The Err code block, exists to demonstrate how to gain access to the ranges that
100                    // caused a given error.
101
102                    // Save our error for output
103                    txt.push(String::from(*msg));
104
105                    // get our raw column and the original rows that caused the error!
106                    let col = iter.get_column(column_id).unwrap();
107
108                    // This Vec contains the rows that caused the error!
109                    let rows = col.get_rows();
110                    for row in rows {
111                        let result_range = row.as_ref();
112                        // The range that was generated from the raw ranges
113                        txt.push(format!(
114                            "Invalid Range: ({}->{})",
115                            result_range.get_begin(),
116                            result_range.get_end()
117                        ));
118                        for (row_id, range) in result_range.src().iter() {
119                            // One ore more of these ranges caused the error!
120                            txt.push(format!("({}){}->{}", row_id, range.start(), range.end()))
121                        }
122                    }
123                }
124            }
125            match column_id {
126                0 => print!("{:^35}|", txt.join("")),
127                1 => print!("{:^61}|", txt.join("")),
128                2 => print!("{:^35}|", txt.join("")),
129                _ => (),
130            }
131        }
132
133        println!();
134        if stop {
135            // stop here if we ran into an error processing an iterator.
136            break;
137        }
138        id += 1;
139    }
140}