pub struct Intersector<T, V, C: IncDecCpCmp<T, V>, R, F> { /* private fields */ }Expand description
This acts as a general OverlapIter factory.
The self.add_ methods*:
The various add_* methods return the index of the column that was added and the generated instance of GetBeginEnd. The index can be used to update that column during the iteration process of the returned OverlapIter object instance. See OverlapIter::update_column for more details.
Implementations§
Source§impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, B: GetBeginEndOption<T, R>> Intersector<T, V, C, R, B>
impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, B: GetBeginEndOption<T, R>> Intersector<T, V, C, R, B>
Sourcepub fn new(list: Vec<R>, step: V, rebound: V, cmp: C, factory: B) -> Self
pub fn new(list: Vec<R>, step: V, rebound: V, cmp: C, factory: B) -> Self
Constructs a new instance of Intersector.
Examples found in repository?
55fn main() {
56 let t = CustomIncDecCpCmp {};
57
58 let mut isec = Intersector::new(
59 Vec::new(), // Container for our internal ranges
60 Point { p: 1 }, // step
61 Point { p: 1 }, // Rebound value
62 t, // our compare instance
63 RiFactory::new(), // Factory used to construct new ranges
64 );
65
66 // Note: an internal RangeInclusive instance is
67 // generated for every range that is successfully added.
68 // This means it is safe to drop the orginal range values
69 // after they are loaded into the Intersector instance.
70 isec.add_range(&(..Point { p: 2 }));
71 isec.add_range(&(Point { p: 1 }..Point { p: 3 }));
72 isec.add_range(&(Point { p: 3 }..=Point { p: 4 }));
73 isec.add_range(&(Point { p: 3 }..));
74 for r in isec.into_iter() {
75 println!("X: {:?}, Y: {:?}", r.start(), r.end());
76 }
77 // Output will be:
78 // X: Point { p: 0 }, Y: Point { p: 0 }
79 // X: Point { p: 1 }, Y: Point { p: 1 }
80 // X: Point { p: 2 }, Y: Point { p: 2 }
81 // X: Point { p: 3 }, Y: Point { p: 4 }
82 // X: Point { p: 5 }, Y: Point { p: 8 }
83}Source§impl<T, V> Intersector<T, V, AnyIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>
impl<T, V> Intersector<T, V, AnyIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>
Sourcepub fn any(
step: V,
rebound: V,
min: T,
max: T,
) -> Intersector<T, V, AnyIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>
pub fn any( step: V, rebound: V, min: T, max: T, ) -> Intersector<T, V, AnyIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>
Creates a new Intersector instance that works with any data type.
Examples found in repository?
4fn main() {
5 let min = UNIX_EPOCH;
6 let max = UNIX_EPOCH + Duration::from_millis(u64::MAX);
7 let step = Duration::from_secs(1);
8 let rebound = Duration::from_secs(1);
9
10 let mut isec = Intersector::any(step, rebound, min, max);
11
12 let mut pos = 0;
13 for _ in 1..=5 {
14 let start = UNIX_EPOCH + Duration::from_secs(pos);
15 let end = UNIX_EPOCH + Duration::from_secs(pos + 10);
16 pos += 5;
17 let range = start..=end;
18 isec.add_raw_range(range);
19 }
20 for r in isec.into_iter() {
21 let start = r.start().duration_since(UNIX_EPOCH).unwrap().as_secs();
22 let end = r.end().duration_since(UNIX_EPOCH).unwrap().as_secs();
23 println!("Start: {}, End: {}", start, end);
24 }
25}pub fn any_from( step: V, rebound: V, min: T, max: T, src: &[impl RangeBounds<T>], ) -> OverlapIter<T, V, AnyIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>> ⓘ
Source§impl<T> Intersector<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>where
T: PartialOrd + Copy + Add<T, Output = T> + Sub<T, Output = T>,
NumberIncDecCpCmp<T>: DefaultValues<T, T>,
impl<T> Intersector<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>>where
T: PartialOrd + Copy + Add<T, Output = T> + Sub<T, Output = T>,
NumberIncDecCpCmp<T>: DefaultValues<T, T>,
Sourcepub fn num_defaults() -> Self
pub fn num_defaults() -> Self
Returns a new instance of Intersector configured to work with any primitive number type using the default values.
Examples found in repository?
4fn main() {
5 let mut isec = Intersector::num_defaults();
6
7 // 1 to 3
8 let range: std::ops::Range<i32> = 1..4;
9 isec.add_range(&range);
10
11 // 3 to 5
12 let range_inclusive: std::ops::RangeInclusive<i32> = 3..=5;
13 isec.add_range(&range_inclusive);
14
15 // -2147483648 to 7
16 let min_to_end: std::ops::RangeToInclusive<i32> = ..=7;
17 isec.add_range(&min_to_end);
18
19 // 7 to 2147483647
20 let begin_to_max: std::ops::RangeFrom<i32> = 7..;
21 isec.add_range(&begin_to_max);
22
23 // Note 7.. and ..7 include our min and max all ready!
24 let min_to_max: std::ops::RangeFull = ..;
25 isec.add_range(&min_to_max);
26
27 for i in isec.into_iter() {
28 println!("Common Range: {:^14}->{:^14}", i.start(), i.end());
29 }
30
31 // The Output will be:
32 // Common Range: -2147483648 -> 0
33 // Common Range: 1 -> 2
34 // Common Range: 3 -> 3
35 // Common Range: 4 -> 5
36 // Common Range: 6 -> 6
37 // Common Range: 7 -> 7
38 // Common Range: 8 -> 2147483647
39}Sourcepub fn num(step: T, rebound: T, min: T, max: T) -> Self
pub fn num(step: T, rebound: T, min: T, max: T) -> Self
Returns a new instance of Intersector configured to work with numbers based on the arguments passed in.
Examples found in repository?
4fn main() {
5 let mut isec = Intersector::num(
6 1, // step
7 1, // rebound
8 0, // min
9 8, // max
10 );
11 // 1 to 3
12 let range: std::ops::Range<i32> = 1..4;
13 isec.add_range(&range);
14
15 // 3 to 5
16 let range_inclusive: std::ops::RangeInclusive<i32> = 3..=5;
17 isec.add_range(&range_inclusive);
18
19 // 0 to 7
20 let min_to_end: std::ops::RangeToInclusive<i32> = ..=7;
21 isec.add_range(&min_to_end);
22
23 // 7 to 8
24 let begin_to_max: std::ops::RangeFrom<i32> = 7..;
25 isec.add_range(&begin_to_max);
26
27 // Note 7.. and ..7 include our min and max all ready!
28 let min_to_max: std::ops::RangeFull = ..;
29 isec.add_range(&min_to_max);
30 for i in isec.into_iter() {
31 println!(" Common Range {:^3}->{:^3}", i.start(), i.end());
32 }
33 // The output will be:
34 // Common Range 0 -> 0
35 // Common Range 1 -> 2
36 // Common Range 3 -> 3
37 // Common Range 4 -> 5
38 // Common Range 6 -> 6
39 // Common Range 7 -> 7
40 // Common Range 8 -> 8
41}Sourcepub fn num_sr(sr: T) -> Self
pub fn num_sr(sr: T) -> Self
Returns a new instance of Intersector configured to work with numbers. The nteral step and rebound values are set to sr.
Sourcepub fn num_from(
src: &[impl RangeBounds<T>],
) -> OverlapIter<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>> ⓘ
pub fn num_from( src: &[impl RangeBounds<T>], ) -> OverlapIter<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>> ⓘ
Takes any list of range of numbers and converts them to an instance of OverlapIter.
Examples found in repository?
3fn main() {
4 // RangeInclusive used to make this more readable.
5 let src = [1..=4, 0..=3, 3..=11, 10..=22];
6 // Forwards
7 println!("Forwards");
8 for r in Intersector::num_from(&src) {
9 println!("Common Range: {}->{}", r.start(), r.end());
10 }
11 // Output will be
12 // Forwards
13 // Common Range: 0->0
14 // Common Range: 1->2
15 // Common Range: 3->3
16 // Common Range: 4->4
17 // Common Range: 5->9
18 // Common Range: 10->11
19 // Common Range: 12->22
20
21 // add a small bumper to the output
22 print!("\n\n");
23 // Backwards
24 println!("Backwards");
25 for r in Intersector::num_from(&src).rev() {
26 println!("Common Range: {}->{}", r.start(), r.end());
27 }
28 // Outout will be
29 // Backwards
30 // Common Range: 12->22
31 // Common Range: 10->11
32 // Common Range: 5->9
33 // Common Range: 4->4
34 // Common Range: 3->3
35 // Common Range: 1->2
36 // Common Range: 0->0
37}Sourcepub fn num_sr_from(
sr: T,
src: &[impl RangeBounds<T>],
) -> OverlapIter<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>> ⓘ
pub fn num_sr_from( sr: T, src: &[impl RangeBounds<T>], ) -> OverlapIter<T, T, NumberIncDecCpCmp<T>, RangeInclusive<T>, RiFactory<T>> ⓘ
Takes any list of range of numbers and converts them to an instance of OverlapIter, with the step and rebound value set to sr.
Examples found in repository?
3fn main() {
4 let l = NumberIncDecCpCmp::defaults();
5 // f32 Increment examples
6 assert_eq!(l.inc(&0.2, &0.5), Some(0.7));
7 assert_eq!(l.inc(&1.7, &-0.5), None);
8 assert_eq!(l.inc(&f32::INFINITY, &0.5), None);
9 assert_eq!(l.inc(&f32::INFINITY, &f32::INFINITY), None);
10 assert_eq!(l.inc(&1.0, &f32::INFINITY), Some(f32::INFINITY));
11 assert_eq!(l.inc(&1.0, &f32::NEG_INFINITY), None);
12
13 // f32 Decrement examples
14 assert_eq!(l.dec(&0.5, &0.5), Some(-0.0));
15 assert_eq!(l.dec(&1.7, &-0.5), None);
16 assert_eq!(l.dec(&f32::INFINITY, &0.5), None);
17 assert_eq!(l.dec(&f32::INFINITY, &f32::INFINITY), None);
18 assert_eq!(l.dec(&1.0, &f32::INFINITY), Some(f32::NEG_INFINITY));
19 assert_eq!(l.dec(&1.0, &f32::NEG_INFINITY), None);
20
21 // This sets the step and rebound value to 0.1
22 for r in Intersector::num_sr_from(0.1, &[1.0..=3.1, 2.5..=4.1, 1.9..=7.64]) {
23 print!("Common Range: {}->{}\n", r.start(), r.end());
24 }
25
26 // The resulting output will be:
27 // Common Range: 1->1.7999999999999998
28 // Common Range: 1.9->2.4
29 // Common Range: 2.5->3.1
30 // Common Range: 3.2->4.1
31 // Common Range: 4.199999999999999->7.64
32}Source§impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, F: GetBeginEndOption<T, R>> Intersector<T, V, C, R, F>
impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, F: GetBeginEndOption<T, R>> Intersector<T, V, C, R, F>
Sourcepub fn add_raw_range(&mut self, src: R) -> Option<(usize, &R)>
pub fn add_raw_range(&mut self, src: R) -> Option<(usize, &R)>
Tries to add an instance of GetBeginEnd to this instance returns None if src is invalid.
Examples found in repository?
4fn main() {
5 let min = UNIX_EPOCH;
6 let max = UNIX_EPOCH + Duration::from_millis(u64::MAX);
7 let step = Duration::from_secs(1);
8 let rebound = Duration::from_secs(1);
9
10 let mut isec = Intersector::any(step, rebound, min, max);
11
12 let mut pos = 0;
13 for _ in 1..=5 {
14 let start = UNIX_EPOCH + Duration::from_secs(pos);
15 let end = UNIX_EPOCH + Duration::from_secs(pos + 10);
16 pos += 5;
17 let range = start..=end;
18 isec.add_raw_range(range);
19 }
20 for r in isec.into_iter() {
21 let start = r.start().duration_since(UNIX_EPOCH).unwrap().as_secs();
22 let end = r.end().duration_since(UNIX_EPOCH).unwrap().as_secs();
23 println!("Start: {}, End: {}", start, end);
24 }
25}Sourcepub fn add_from_tuple_ref(&mut self, src: (&T, &T)) -> Option<(usize, &R)>
pub fn add_from_tuple_ref(&mut self, src: (&T, &T)) -> Option<(usize, &R)>
Tries to create and add a valid internal range from the tuple of refs.
Sourcepub fn add_from_tuple(&mut self, src: (T, T)) -> Option<(usize, &R)>
pub fn add_from_tuple(&mut self, src: (T, T)) -> Option<(usize, &R)>
Tries to add a tuple to the instance, returns None if it fails.
Sourcepub fn rebound(&self, r: &impl RangeBounds<T>) -> Option<(T, T)>
pub fn rebound(&self, r: &impl RangeBounds<T>) -> Option<(T, T)>
This is really a wrapper for crate::range_bounds_to_values.
Sourcepub fn add_range(&mut self, r: &impl RangeBounds<T>) -> Option<(usize, &R)>
pub fn add_range(&mut self, r: &impl RangeBounds<T>) -> Option<(usize, &R)>
Tries to convert a given RangeBounds instance to the internal range type. Returns None if the conversion process fails or the range produced is invalid.
Examples found in repository?
4fn main() {
5 let mut isec = Intersector::num(
6 1, // step
7 1, // rebound
8 0, // min
9 8, // max
10 );
11 // 1 to 3
12 let range: std::ops::Range<i32> = 1..4;
13 isec.add_range(&range);
14
15 // 3 to 5
16 let range_inclusive: std::ops::RangeInclusive<i32> = 3..=5;
17 isec.add_range(&range_inclusive);
18
19 // 0 to 7
20 let min_to_end: std::ops::RangeToInclusive<i32> = ..=7;
21 isec.add_range(&min_to_end);
22
23 // 7 to 8
24 let begin_to_max: std::ops::RangeFrom<i32> = 7..;
25 isec.add_range(&begin_to_max);
26
27 // Note 7.. and ..7 include our min and max all ready!
28 let min_to_max: std::ops::RangeFull = ..;
29 isec.add_range(&min_to_max);
30 for i in isec.into_iter() {
31 println!(" Common Range {:^3}->{:^3}", i.start(), i.end());
32 }
33 // The output will be:
34 // Common Range 0 -> 0
35 // Common Range 1 -> 2
36 // Common Range 3 -> 3
37 // Common Range 4 -> 5
38 // Common Range 6 -> 6
39 // Common Range 7 -> 7
40 // Common Range 8 -> 8
41}More examples
4fn main() {
5 let mut isec = Intersector::num_defaults();
6
7 // 1 to 3
8 let range: std::ops::Range<i32> = 1..4;
9 isec.add_range(&range);
10
11 // 3 to 5
12 let range_inclusive: std::ops::RangeInclusive<i32> = 3..=5;
13 isec.add_range(&range_inclusive);
14
15 // -2147483648 to 7
16 let min_to_end: std::ops::RangeToInclusive<i32> = ..=7;
17 isec.add_range(&min_to_end);
18
19 // 7 to 2147483647
20 let begin_to_max: std::ops::RangeFrom<i32> = 7..;
21 isec.add_range(&begin_to_max);
22
23 // Note 7.. and ..7 include our min and max all ready!
24 let min_to_max: std::ops::RangeFull = ..;
25 isec.add_range(&min_to_max);
26
27 for i in isec.into_iter() {
28 println!("Common Range: {:^14}->{:^14}", i.start(), i.end());
29 }
30
31 // The Output will be:
32 // Common Range: -2147483648 -> 0
33 // Common Range: 1 -> 2
34 // Common Range: 3 -> 3
35 // Common Range: 4 -> 5
36 // Common Range: 6 -> 6
37 // Common Range: 7 -> 7
38 // Common Range: 8 -> 2147483647
39}55fn main() {
56 let t = CustomIncDecCpCmp {};
57
58 let mut isec = Intersector::new(
59 Vec::new(), // Container for our internal ranges
60 Point { p: 1 }, // step
61 Point { p: 1 }, // Rebound value
62 t, // our compare instance
63 RiFactory::new(), // Factory used to construct new ranges
64 );
65
66 // Note: an internal RangeInclusive instance is
67 // generated for every range that is successfully added.
68 // This means it is safe to drop the orginal range values
69 // after they are loaded into the Intersector instance.
70 isec.add_range(&(..Point { p: 2 }));
71 isec.add_range(&(Point { p: 1 }..Point { p: 3 }));
72 isec.add_range(&(Point { p: 3 }..=Point { p: 4 }));
73 isec.add_range(&(Point { p: 3 }..));
74 for r in isec.into_iter() {
75 println!("X: {:?}, Y: {:?}", r.start(), r.end());
76 }
77 // Output will be:
78 // X: Point { p: 0 }, Y: Point { p: 0 }
79 // X: Point { p: 1 }, Y: Point { p: 1 }
80 // X: Point { p: 2 }, Y: Point { p: 2 }
81 // X: Point { p: 3 }, Y: Point { p: 4 }
82 // X: Point { p: 5 }, Y: Point { p: 8 }
83}Sourcepub fn add_tuple(&mut self, src: (T, T)) -> Option<(usize, &R)>
pub fn add_tuple(&mut self, src: (T, T)) -> Option<(usize, &R)>
Tries to convert a tuple to the internal range type and add it. Returns None if the conversion process fails or the resulting range is invalid.
Sourcepub fn get_cmp_mut(&mut self) -> &mut C
pub fn get_cmp_mut(&mut self) -> &mut C
Returns a mutable ref to the internal instance of IncDecCpCmp.
Sourcepub fn get_cmp(&self) -> &C
pub fn get_cmp(&self) -> &C
Returns a ref to the internal instance of IncDecCpCmp.
Sourcepub fn get_rebound(&self) -> &V
pub fn get_rebound(&self) -> &V
Returns a ref to the internal rebound value.
Sourcepub fn set_rebound(&mut self, rebound: V)
pub fn set_rebound(&mut self, rebound: V)
Updates the internal rebound value.
Trait Implementations§
Source§impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, F: GetBeginEndOption<T, R>> IntoIterator for Intersector<T, V, C, R, F>
impl<T, V, C: IncDecCpCmp<T, V>, R: GetBeginEnd<T>, F: GetBeginEndOption<T, R>> IntoIterator for Intersector<T, V, C, R, F>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Converts the current instance of Intersector into an instance of OverlapIter.