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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::iter::FusedIterator;

use crate::{Bound, LowerBound, Step};

/// A range only bounded below (either inclusive or exclusive).
///
/// Generalizes over [`std::ops::RangeFrom`] but also supports ranges with an exclusive lower bound.
///
/// While a `LowerBoundedRange` can be constructed directly, it will most likely
/// result from one or more range operations.
/// ```
/// use rangetools::{LowerBound, LowerBoundedRange, Rangetools};
///
/// let i = (5..).intersection(10..);
/// assert_eq!(i, LowerBoundedRange { start: LowerBound::included(10) });
/// ```
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct LowerBoundedRange<T> {
    /// The lower bound of the range (can be inclusive or exclusive).
    pub start: LowerBound<T>,
}

impl<T> From<std::ops::RangeFrom<T>> for LowerBoundedRange<T> {
    fn from(r: std::ops::RangeFrom<T>) -> Self {
        Self {
            start: LowerBound::included(r.start),
        }
    }
}

impl<T> IntoIterator for LowerBoundedRange<T>
where
    T: Copy + Step,
{
    type IntoIter = LowerBoundedRangeIter<T>;
    type Item = T;
    fn into_iter(self) -> Self::IntoIter {
        LowerBoundedRangeIter {
            current: match self.start {
                LowerBound(Bound::Excluded(t)) => Step::forward(t, 1),
                LowerBound(Bound::Included(t)) => t,
            },
        }
    }
}

impl<T: Copy + Ord> LowerBoundedRange<T> {
    /// Constructs a new `LowerBoundedRange` from a lower bound.
    ///
    /// # Example
    /// ```
    /// use rangetools::{LowerBound, LowerBoundedRange};
    ///
    /// let r = LowerBoundedRange::new(LowerBound::included(0));
    /// assert!(r.contains(5));
    /// ```
    pub fn new(start: LowerBound<T>) -> Self {
        Self { start }
    }

    /// Returns true if the range contains `t`.
    ///
    /// # Example
    /// ```
    /// use rangetools::Rangetools;
    ///
    /// let i = (5..).intersection(10..);
    /// assert!(i.contains(10));
    /// assert!(!i.contains(5));
    /// ```
    pub fn contains(&self, t: T) -> bool {
        match self.start.0 {
            Bound::Excluded(x) => t > x,
            Bound::Included(i) => t >= i,
        }
    }
}

/// An iterator over the values contained by a `LowerBoundedRange`.
///
/// Created by the `into_iter` method on `LowerBoundedRange` (provided by the [`std::iter::IntoIterator`] trait).
///
/// # Example
///
/// ```
/// # use rangetools::{LowerBoundedRange, LowerBoundedRangeIter};
/// let r: LowerBoundedRange<i32> = (0..).into();
/// let iter: LowerBoundedRangeIter<i32> = r.into_iter();
/// ```
#[derive(Clone, Debug)]
pub struct LowerBoundedRangeIter<T> {
    current: T,
}

impl<T> Iterator for LowerBoundedRangeIter<T>
where
    T: Copy + Step,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let t = self.current;
        self.current = Step::forward(self.current, 1);
        Some(t)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (usize::MAX, None)
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.current = Step::forward(self.current, n);
        self.next()
    }

    fn min(mut self) -> Option<Self::Item>
    where
        Self::Item: Ord,
    {
        self.next()
    }
}

impl<T> FusedIterator for LowerBoundedRangeIter<T> where T: Copy + Step {}