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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::{array, fmt::Debug};
use crate::index::RawIndexSpan;
use crate::span::{BoundSpan, UnboundSpan};
use crate::span_iter::{BoundSpanIterator, SpanIterator, UnboundSpanIterator};
/// [`CircularArray`](crate::CircularArray) `N` dimensional index span iterator.
///
/// Derives contiguous indices from the Cartesian product of axis spans within.
/// Produces `N` dimensional [`RawIndexSpan`]s defining the bounds of contiguous
/// slices.
///
/// For unbound iteration, contiguous ranges are only derived from axis `0`, while
/// for bound spans, contiguous ranges will extend across axes if possible. As such,
/// unbound index ranges are applicable to any `N` dimensional array as long as spans
/// are in bounds.
#[derive(Debug, Clone, Copy)]
pub(crate) struct IndexIterator<const D: usize, S>([S; D]);
impl<const D: usize> IndexIterator<D, UnboundSpanIterator> {
/// Create a new iterator of unbound axis spans.
pub(crate) fn new_unbound(spans: [UnboundSpan; D]) -> Self {
let mut cont = true;
let bounds = spans.map(|span| {
let bounds = UnboundSpanIterator::new(span, cont);
cont = cont && bounds.exhaustive();
bounds
});
IndexIterator(bounds)
}
}
impl<const D: usize> IndexIterator<D, BoundSpanIterator> {
/// Create a new iterator for bound axis spans. Spans are **not** contiguous
/// across axes, and therefore only axis `0` will be contiguous. This should
/// be preffered if mapping slices from/to an array of a different shape.
pub(crate) fn new_bound(spans: [BoundSpan; D]) -> Self {
let mut cont = true;
let bounds = spans.map(|span| {
let bounds = BoundSpanIterator::new(span, false, cont);
cont = false;
bounds
});
IndexIterator(bounds)
}
/// Create a new iterator for bound axis spans. Spans are contiguous across
/// axes where possible. This should be preffered for destination arrays when
/// mapping elements from iterators or contiguous slices.
pub(crate) fn new_bound_contiguous(spans: [BoundSpan; D]) -> Self {
let mut cont = true;
let bounds = spans.map(|span| {
let bounds = BoundSpanIterator::new(span, false, cont);
cont = cont && bounds.exhaustive();
bounds
});
IndexIterator(bounds)
}
// TODO: This has the potential for improved cache locality for the destination
// array. Requires creating `BoundSpan`s for the source. Applicable to `push` and
// `push_fn` mutation methods.
/// Create a new iterator for bound axis spans. Spans are contiguous across
/// axes where possible and always contiguously ordered. Provides better cache
/// locality than [`IndexIterator::new_bound_contiguous`] where contiguous order
/// is acceptable.
#[allow(dead_code)]
pub(crate) fn new_bound_contiguous_ordered(spans: [BoundSpan; D]) -> Self {
let mut cont = true;
let bounds = spans.map(|mut span| {
// Mutate spans into exhaustive spans, if possible.
if span.len() == span.bound() {
span = BoundSpan::new(0, span.bound(), span.bound());
}
let bounds = BoundSpanIterator::new(span, true, cont);
cont = cont && bounds.exhaustive();
bounds
});
IndexIterator(bounds)
}
}
impl<const D: usize, S> IndexIterator<D, S> {
/// Get a reference to the inner span array.
fn inner(&self) -> &[S; D] {
&self.0
}
/// Get a mutable reference to the inner span array.
fn inner_mut(&mut self) -> &mut [S; D] {
&mut self.0
}
}
impl<const D: usize, S: SpanIterator> Iterator for IndexIterator<D, S> {
type Item = RawIndexSpan<D>;
fn next(&mut self) -> Option<Self::Item> {
if self.inner().iter().all(|bounds| bounds.is_finished()) {
None
} else {
let mut finished = true;
let span = array::from_fn(|i| {
let bounds = &mut self.inner_mut()[i];
let span = if finished {
match bounds.next() {
Some(bounds) => bounds,
None => {
bounds.reset();
bounds.next().expect("No bounds returned from iterator")
}
}
// Continue or reset and continue iteration.
} else {
// TODO: Should this ever be `None`?
// Why would the iterator be exhausted prior to calling current?
match bounds.get() {
Some(bounds) => bounds,
None => {
bounds.reset();
bounds.get().expect("No current bounds")
}
}
};
finished = finished && bounds.is_finished();
span
});
Some(span.into())
}
}
}
#[cfg(test)]
mod tests {
#[cfg(test)]
mod index_iterator {
#[cfg(test)]
mod unbound {
use crate::index_iter::IndexIterator;
use crate::span::UnboundSpan;
#[test]
fn iter() {
let iter = IndexIterator::new_unbound([
UnboundSpan::new(0, 1),
UnboundSpan::new(1, 2),
UnboundSpan::new(1, 1),
]);
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([0, 1, 1], [1, 1, 1]),
([0, 2, 1], [1, 2, 1])
]);
let iter = IndexIterator::new_unbound([
UnboundSpan::new(0, 2),
UnboundSpan::new(1, 3),
UnboundSpan::new(2, 3),
]);
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([0, 1, 2], [2, 1, 2]),
([0, 2, 2], [2, 2, 2]),
([0, 3, 2], [2, 3, 2]),
([0, 1, 3], [2, 1, 3]),
([0, 2, 3], [2, 2, 3]),
([0, 3, 3], [2, 3, 3]),
]);
}
}
mod bound {
use crate::index_iter::IndexIterator;
use crate::CircularArrayVec;
#[test]
fn iter() {
let shape = [4, 3, 2];
let mut array = CircularArrayVec::from_iter(shape, 0..shape.iter().product());
array.offset = [2, 2, 1];
let iter = IndexIterator::new_bound_contiguous(array.spans());
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([2, 2, 1], [3, 2, 1]),
([0, 2, 1], [1, 2, 1]),
([2, 0, 1], [3, 0, 1]),
([0, 0, 1], [1, 0, 1]),
([2, 1, 1], [3, 1, 1]),
([0, 1, 1], [1, 1, 1]),
([2, 2, 0], [3, 2, 0]),
([0, 2, 0], [1, 2, 0]),
([2, 0, 0], [3, 0, 0]),
([0, 0, 0], [1, 0, 0]),
([2, 1, 0], [3, 1, 0]),
([0, 1, 0], [1, 1, 0])
]);
array.offset = [0, 2, 1];
let iter = IndexIterator::new_bound_contiguous(array.spans());
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([0, 2, 1], [3, 2, 1]),
([0, 0, 1], [3, 1, 1]),
([0, 2, 0], [3, 2, 0]),
([0, 0, 0], [3, 1, 0])
]);
array.offset = [0, 0, 1];
let iter = IndexIterator::new_bound_contiguous(array.spans());
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([0, 0, 1], [3, 2, 1]),
([0, 0, 0], [3, 2, 0]),
]);
array.offset = [0, 0, 0];
let iter = IndexIterator::new_bound_contiguous(array.spans());
#[rustfmt::skip]
assert_eq!(iter.collect::<Vec<_>>(), [
([0, 0, 0], [3, 2, 1]),
]);
}
#[test]
fn iter_cont() {
let shape = [4, 3, 2];
let mut array = CircularArrayVec::from_iter(shape, 0..shape.iter().product());
array.offset = [2, 2, 1];
let iter = IndexIterator::new_bound_contiguous_ordered(array.spans());
assert_eq!(iter.collect::<Vec<_>>(), [([0, 0, 0], [3, 2, 1]),]);
array.offset = [0, 2, 1];
let iter = IndexIterator::new_bound_contiguous_ordered(array.spans());
assert_eq!(iter.collect::<Vec<_>>(), [([0, 0, 0], [3, 2, 1]),]);
array.offset = [0, 0, 1];
let iter = IndexIterator::new_bound_contiguous_ordered(array.spans());
assert_eq!(iter.collect::<Vec<_>>(), [([0, 0, 0], [3, 2, 1]),]);
array.offset = [0, 0, 0];
let iter = IndexIterator::new_bound_contiguous_ordered(array.spans());
assert_eq!(iter.collect::<Vec<_>>(), [([0, 0, 0], [3, 2, 1]),]);
}
}
}
}