Skip to main content

example/
example.rs

1use common_range_tools::Intersector;
2use std::ops::RangeInclusive;
3
4fn main() {
5    // RangeInclusive used to make this more readable.
6    let src = [1..=4, 0..=3, 3..=11, 10..=22];
7
8    println!("Forwards");
9    for r in Intersector::num_from(&src) {
10        println!("Common Range: {}->{}", r.start(), r.end());
11    }
12    // Output will be
13    //  Forwards
14    //  Common Range: 0->0
15    //  Common Range: 1->2
16    //  Common Range: 3->3
17    //  Common Range: 4->4
18    //  Common Range: 5->9
19    //  Common Range: 10->11
20    //  Common Range: 12->22
21
22    // add a small bumper to the output
23    print!("\n");
24
25    // Backwards
26    println!("Backwards");
27    for r in Intersector::num_from(&src).rev() {
28        println!("Common Range: {}->{}", r.start(), r.end());
29    }
30    // Outout will be
31    //  Backwards
32    //  Common Range: 12->22
33    //  Common Range: 10->11
34    //  Common Range: 5->9
35    //  Common Range: 4->4
36    //  Common Range: 3->3
37    //  Common Range: 1->2
38    //  Common Range: 0->0
39
40    // add a small bumper to the output
41    print!("\n");
42
43    // This creates an iterator for both the intersection and a ref to the source range.
44    let mut iter = Intersector::num_from_ol(&src);
45    println!("Foward with source Ranges");
46    overlaps_info(&mut iter);
47    // Output will be:
48    //  Foward with source Ranges
49    //    Common Range:  0->0  Count: 1 Ranges: 0->3
50    //    Common Range:  1->2  Count: 2 Ranges: 1->4, 0->3
51    //    Common Range:  3->3  Count: 3 Ranges: 1->4, 0->3, 3->11
52    //    Common Range:  4->4  Count: 2 Ranges: 1->4, 3->11
53    //    Common Range:  5->9  Count: 1 Ranges: 3->11
54    //    Common Range: 10->11 Count: 2 Ranges: 3->11, 10->22
55    //    Common Range: 12->22 Count: 1 Ranges: 10->22
56
57    // add a small bumper to the output
58    print!("\n");
59
60    // we can just reset the iterator
61    iter.reset();
62    println!("Reverse, with source Ranges");
63    // now we set it to reverse
64    overlaps_info(&mut &mut iter.rev());
65    // Output will be:
66    //  Reverse, with source Ranges
67    //    Common Range: 12->22 Count: 1 Ranges: 10->22
68    //    Common Range: 10->11 Count: 2 Ranges: 3->11, 10->22
69    //    Common Range:  5->9  Count: 1 Ranges: 3->11
70    //    Common Range:  4->4  Count: 2 Ranges: 1->4, 3->11
71    //    Common Range:  3->3  Count: 3 Ranges: 1->4, 0->3, 3->11
72    //    Common Range:  1->2  Count: 2 Ranges: 1->4, 0->3
73    //    Common Range:  0->0  Count: 1 Ranges: 0->3
74}
75
76// Format the output for our  Intersections with the source Ranges!
77fn overlaps_info<'a, I: Iterator<Item = &'a RangeInclusive<i32>>>(
78    iter: &mut impl Iterator<Item = (RangeInclusive<i32>, I)>,
79) {
80    for (r, isec) in iter {
81        print!(
82            "  Common Range: {:^6}",
83            format!("{}->{}", r.start(), r.end())
84        );
85        let mut txt = Vec::new();
86        // grab all of our overlapping ranges!
87        for src_range in isec {
88            txt.push(format!("{}->{}", src_range.start(), src_range.end()));
89        }
90        print!(" Count: {}", txt.len());
91        println!(" Ranges: {}", txt.join(", "));
92    }
93}