columns/columns.rs
1use common_range_tools::{Columns, DefaultValues, GetBeginEnd, NumberIncDecCpCmp, sort_forward};
2
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}
141
142// The resulting output will be
143// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
144// | Overlap | State(id) | Column(A) | Column(B) | Column(C) |
145// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
146// | 0 ->2 | Ok(0) | [0->11](0(0->11),1(2->3),2(7->9)) | | |
147// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
148// | 3 ->5 | Ok(1) | [0->11](0(0->11),1(2->3),2(7->9)) | | [3->9](0(3->9),1(3->4),2(4->6)) |
149// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
150// | 6 ->9 | Ok(2) | [0->11](0(0->11),1(2->3),2(7->9)) | [6->22](0(6->9),1(6->9),2(6->7),3(7->11),4(9->9),5(11->22)) | [3->9](0(3->9),1(3->4),2(4->6)) |
151// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
152// | 10->11 | Ok(3) | [0->11](0(0->11),1(2->3),2(7->9)) | [6->22](0(6->9),1(6->9),2(6->7),3(7->11),4(9->9),5(11->22)) | |
153// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
154// | 12->21 | Ok(4) | | [6->22](0(6->9),1(6->9),2(6->7),3(7->11),4(9->9),5(11->22)) | |
155// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
156// | 22->22 | Ok(5) | [22->33](3(22->33)) | [6->22](0(6->9),1(6->9),2(6->7),3(7->11),4(9->9),5(11->22)) | |
157// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
158// | 23->29 | Ok(6) | [22->33](3(22->33)) | | |
159// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
160// | 30->33 | Ok(7) | [22->33](3(22->33)) | | [30->41](3(30->41)) |
161// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
162// | 34->39 | Ok(8) | [34->39](4(34->39)) | | [30->41](3(30->41)) |
163// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+
164// | 40->41 | Ok(9) | | | [30->41](3(30->41)) |
165// +---------+-----------+-----------------------------------+-------------------------------------------------------------+-----------------------------------+