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};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct Range<C: Component> {
range: IRange<isize>,
phanton: PhantomData<C>,
}
impl<C: Component> Range<C> {
pub fn bounded(start: C, end: C) -> Self {
Range {
phanton: PhantomData,
range: start.value()..end.value(),
}
}
#[inline]
pub fn span(start: C, size: C::Distance) -> Self {
Self::bounded(start, start.add(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)
}
}
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> {}
pub type RowRange = Range<Row>;
pub type ColumnRange = Range<Column>;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum RangeError<T: Component> {
TooLow(T),
TooHigh(T),
}
pub type RowRangeError = RangeError<Row>;
pub type ColumnRangeError = RangeError<Column>;