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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::ops::{Add, Rem, Sub};
use std::ops::{Range, RangeInclusive};
/// A span of inclusive elements within an axis. In contrast to [`BoundSpan`], all
/// elements are guaranteed to be within axis bounds.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct UnboundSpan {
/// The first element of the span.
pub(crate) start: usize,
/// The last element of the span.
pub(crate) end: usize,
}
impl UnboundSpan {
/// Create a new `UnboundSpan` from the **inclusive** `start` and `end` indices.
pub(crate) fn new(start: usize, end: usize) -> Self {
debug_assert!(start <= end);
Self { start, end }
}
/// Create a new `UnboundSpan` from a `start` index and length.
pub(crate) fn from_len(start: usize, len: usize) -> Self {
debug_assert!(len > 0);
let end = start + len - 1;
UnboundSpan::new(start, end)
}
/// Get the number of elements within the span.
pub(crate) fn len(&self) -> usize {
self.end - self.start + 1
}
/// Get the index of the element `i` from `start`. Returns `None` if the index
/// exceeds the `end` of the span.
pub(crate) fn get_index(&self, i: usize) -> Option<usize> {
Some(self.start + i).filter(|i| *i <= self.end)
}
/// Consume the `UnboundSpan`, returning a `Range<usize>`. Offsets ranges
/// by the given value.
pub(crate) fn into_range(self, offset: usize) -> Range<usize> {
(self.start + offset)..(self.end + offset + 1)
}
}
impl From<usize> for UnboundSpan {
fn from(value: usize) -> Self {
UnboundSpan {
start: value,
end: value,
}
}
}
impl From<(usize, usize)> for UnboundSpan {
fn from((start, end): (usize, usize)) -> Self {
UnboundSpan { start, end }
}
}
impl From<Range<usize>> for UnboundSpan {
fn from(range: Range<usize>) -> Self {
UnboundSpan {
start: range.start,
end: range.end - 1,
}
}
}
impl From<RangeInclusive<usize>> for UnboundSpan {
fn from(range: RangeInclusive<usize>) -> Self {
let (start, end) = range.into_inner();
UnboundSpan { start, end }
}
}
/// A span of inclusive elements within an axis. In contrast to [`UnboundSpan`],
/// elements may wrap across axis bounds.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct BoundSpan {
/// The start index of the span.
start: usize,
/// The length of the span.
len: usize,
/// The upper (exclusive) bound of the span.
bound: usize,
}
impl BoundSpan {
/// Create a pair of inclusive `Bounds`. All `Span`s are assumed to have a
/// `len` less than, or equal to the upper bound of an axis.
pub(crate) fn new(start: usize, len: usize, bound: usize) -> Self {
debug_assert!(bound > start);
debug_assert!(len <= bound);
assert!(len > 0);
Self { start, bound, len }
}
/// Get the length of elements within the span.
pub(crate) fn len(&self) -> usize {
self.len
}
/// Get the upper bound of the span.
pub(crate) fn bound(&self) -> usize {
self.bound
}
/// Returns `true` if the span is exhaustive of the axis.
pub(crate) fn exhaustive(&self) -> bool {
self.start == 0 && self.len == self.bound
}
/// Returns `true` if the span wraps across the `bound`.
pub(crate) fn is_wrapping(&self) -> bool {
self.start + self.len > self.bound
}
/// Get the span of elements on either side of the axis bounds, or return `None`
/// if out of bounds.
pub(crate) fn get_span(&self, i: usize) -> Option<UnboundSpan> {
match i {
0 => Some(UnboundSpan::new(
self.start,
(self.start + self.len - 1).min(self.bound - 1),
)),
1 if self.is_wrapping() => Some(UnboundSpan::new(
0,
(self.start + self.len - 1) % self.bound,
)),
_ => None,
}
}
/// Get the index of the element `i` from `start`, wrapping over the `bound`,
/// if any. Returns `None` if the index exceeds the `len` of the span.
pub(crate) fn get_index(&self, i: usize) -> Option<usize> {
if i >= self.len {
None
} else {
Some((self.start + i) % self.bound)
}
}
/// Get the index of the element `i` of the wrapping elements (if any), followed
/// by the indices following `start`. Returns `None` if the index exceeds the
/// `len` of the span.
pub(crate) fn get_index_ordered(&self, i: usize) -> Option<usize> {
if i >= self.len {
None
} else if let Some(span_len) = self.get_span(1).map(|span| span.len()) {
if i < span_len {
Some(i)
} else {
Some(self.start + i - span_len)
}
} else {
self.get_index(i)
}
}
}
impl Add<usize> for BoundSpan {
type Output = BoundSpan;
fn add(self, rhs: usize) -> Self::Output {
BoundSpan {
start: self.start + rhs,
len: self.len,
bound: self.bound,
}
}
}
impl Sub<usize> for BoundSpan {
type Output = BoundSpan;
fn sub(self, rhs: usize) -> Self::Output {
BoundSpan {
start: self.start - rhs,
len: self.len,
bound: self.bound,
}
}
}
impl Rem<usize> for BoundSpan {
type Output = BoundSpan;
fn rem(self, rhs: usize) -> Self::Output {
BoundSpan {
start: self.start % rhs,
len: self.len,
bound: self.bound,
}
}
}