add an enumerated
method to DimensionedSlice
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use core::iter::Zip;
|
||||
use core::ops::{Index, IndexMut};
|
||||
|
||||
use crate::vec::Vec3u;
|
||||
@@ -18,6 +19,12 @@ impl<T> DimensionedSlice<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: IntoIterator> DimensionedSlice<T> {
|
||||
pub fn enumerated(self) -> Zip<Vec3uIter, T::IntoIter> {
|
||||
self.indices().zip(self.into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
fn index(loc: Vec3u, dim: Vec3u) -> usize {
|
||||
((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
|
||||
}
|
||||
@@ -242,5 +249,49 @@ mod test {
|
||||
assert!(s.indices().next() == None);
|
||||
assert!(s.indices().next() == None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimensioned_slice_enumerated() {
|
||||
let data = [
|
||||
10, 11,
|
||||
20, 21,
|
||||
30, 31,
|
||||
];
|
||||
let s = DimensionedSlice::new(Vec3u::new(1, 2, 3), &data);
|
||||
let mut i = s.enumerated();
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 0, 0), &10));
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 1, 0), &11));
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 0, 1), &20));
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 1, 1), &21));
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 0, 2), &30));
|
||||
assert!(i.next().unwrap() == (Vec3u::new(0, 1, 2), &31));
|
||||
assert!(i.next() == None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dimensioned_slice_enumerated_mut() {
|
||||
let mut data = [
|
||||
10, 11,
|
||||
20, 21,
|
||||
30, 31,
|
||||
];
|
||||
let s = DimensionedSlice::new(Vec3u::new(2, 1, 3), &mut data);
|
||||
let mut i = s.enumerated();
|
||||
|
||||
let (idx, v) = i.next().unwrap();
|
||||
assert!(idx == Vec3u::new(0, 0, 0));
|
||||
*v = 100;
|
||||
|
||||
let (idx, v) = i.next().unwrap();
|
||||
assert!(idx == Vec3u::new(1, 0, 0));
|
||||
*v = 110;
|
||||
i.next().unwrap();
|
||||
|
||||
let (idx, v) = i.next().unwrap();
|
||||
assert!(idx == Vec3u::new(1, 0, 1));
|
||||
*v = 210;
|
||||
|
||||
assert_eq!(data, [100, 110, 20, 210, 30, 31]);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user