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
125
126
127
128
129
130
131
132
133
134
use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::ops::Range as IRange;

use crate::location::{self, Column, Component, Row};

// TODO: replace this with Range<C> once Step is stabilized
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Range<C: Component> {
    range: IRange<isize>,
    phanton: PhantomData<C>,
}

impl<C: Component> Range<C> {
    /// Create a range bounded by `[start .. end)`
    pub fn bounded(start: C, end: C) -> Self {
        Range {
            phanton: PhantomData,
            range: start.value()..end.value(),
        }
    }

    /// Create a range starting at `start` with length `size`
    #[inline]
    pub fn span(start: C, size: C::Distance) -> Self {
        Self::bounded(start, start.add(size))
    }

    /// Create a range starting at Row or Column 0 with length `size`
    #[inline]
    pub fn range(size: C::Distance) -> Self {
        Self::span(0.into(), size)
    }

    #[inline]
    pub fn start(&self) -> C {
        self.range.start.into()
    }

    #[inline]
    pub fn end(&self) -> C {
        self.range.end.into()
    }

    #[inline]
    pub fn size(&self) -> C::Distance {
        self.start().distance_to(self.end())
    }

    pub fn check(&self, idx: impl Into<C>) -> Result<C, RangeError<C>> {
        let idx = idx.into();

        let min = self.start();
        let max = self.end();

        if idx < min {
            Err(RangeError::TooLow(min))
        } else if idx >= max {
            Err(RangeError::TooHigh(max))
        } else {
            Ok(idx)
        }
    }

    #[inline]
    pub fn in_bounds(&self, loc: C) -> bool {
        self.check(loc).is_ok()
    }

    pub fn combine(self, index: C::Converse) -> location::Range<C::Converse> {
        location::Range::new(index, self)
    }
}

// TODO: add a bunch more iterator methods that forward to self.range.
impl<C: Component> Iterator for Range<C> {
    type Item = C;

    #[inline]
    fn next(&mut self) -> Option<C> {
        self.range.next().map(C::from)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<C> {
        self.range.nth(n).map(C::from)
    }

    #[inline]
    fn last(mut self) -> Option<C> {
        self.next_back()
    }
}

impl<C: Component> DoubleEndedIterator for Range<C> {
    fn next_back(&mut self) -> Option<C> {
        self.range.next_back().map(C::from)
    }
}

impl<C: Component> ExactSizeIterator for Range<C> {}
impl<C: Component> FusedIterator for Range<C> {}
// TODO: TrustedLen

pub type RowRange = Range<Row>;
pub type ColumnRange = Range<Column>;

// TODO: Error implementation

/// Error indicating that a Row or Column was out of bounds.
///
/// Note that the bounds expressed in this error are half inclusive; that is,
/// the lower bound in TooLow is an inclusive lower bound, but the upper bound
/// in TooHigh is an exclusive upper bound. This is consistent with the
/// conventional range representation of `low..high`
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RangeError<T: Component> {
    /// The given row or column was too low. The value in the error is the
    /// minimum row or column, inclusive.
    TooLow(T),

    /// The given row or column was too high. The given value in the error is
    /// the maximum row or column, exclusive (that is, a value *equal* to the
    /// error value is considered too high).
    TooHigh(T),
}

pub type RowRangeError = RangeError<Row>;
pub type ColumnRangeError = RangeError<Column>;