| 1 |
use std::cmp::{Ordering, PartialOrd}; |
| 2 |
use std::iter::IntoIterator; |
| 3 |
use std::ops::Range; |
| 4 |
|
| 5 |
use num_traits::{One, Zero}; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
pub fn fitting_range<'a, T, I: IntoIterator<Item = &'a T>>(iter: I) -> Range<T> |
| 20 |
where |
| 21 |
T: 'a + Zero + One + PartialOrd + Clone, |
| 22 |
{ |
| 23 |
let (mut lb, mut ub) = (None, None); |
| 24 |
|
| 25 |
for value in iter.into_iter() { |
| 26 |
if let Some(Ordering::Greater) = lb |
| 27 |
.as_ref() |
| 28 |
.map_or(Some(Ordering::Greater), |lbv: &T| lbv.partial_cmp(value)) |
| 29 |
{ |
| 30 |
lb = Some(value.clone()); |
| 31 |
} |
| 32 |
|
| 33 |
if let Some(Ordering::Less) = ub |
| 34 |
.as_ref() |
| 35 |
.map_or(Some(Ordering::Less), |ubv: &T| ubv.partial_cmp(value)) |
| 36 |
{ |
| 37 |
ub = Some(value.clone()); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
lb.unwrap_or_else(Zero::zero)..ub.unwrap_or_else(One::one) |
| 42 |
} |