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};
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub struct LowerBoundedRange<T> {
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> {
pub fn new(start: LowerBound<T>) -> Self {
Self { start }
}
pub fn contains(&self, t: T) -> bool {
match self.start.0 {
Bound::Excluded(x) => t > x,
Bound::Included(i) => t >= i,
}
}
}
#[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 {}