summaryrefslogtreecommitdiffstats
path: root/vendor/plotters/src/data/data_range.rs
blob: 445260b970d3bb313758099c8096f178fc6c7888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::cmp::{Ordering, PartialOrd};
use std::iter::IntoIterator;
use std::ops::Range;

use num_traits::{One, Zero};

/// Build a range that fits the data
///
/// - `iter`: the iterator over the data
/// - **returns** The resulting range
///
/// ```rust
/// use plotters::data::fitting_range;
///
/// let data = [4, 14, -2, 2, 5];
/// let range = fitting_range(&data);
/// assert_eq!(range, std::ops::Range { start: -2, end: 14 });
/// ```
pub fn fitting_range<'a, T: 'a, I: IntoIterator<Item = &'a T>>(iter: I) -> Range<T>
where
    T: Zero + One + PartialOrd + Clone,
{
    let (mut lb, mut ub) = (None, None);

    for value in iter.into_iter() {
        if let Some(Ordering::Greater) = lb
            .as_ref()
            .map_or(Some(Ordering::Greater), |lbv: &T| lbv.partial_cmp(value))
        {
            lb = Some(value.clone());
        }

        if let Some(Ordering::Less) = ub
            .as_ref()
            .map_or(Some(Ordering::Less), |ubv: &T| ubv.partial_cmp(value))
        {
            ub = Some(value.clone());
        }
    }

    lb.unwrap_or_else(Zero::zero)..ub.unwrap_or_else(One::one)
}