rename DimensionedSlice -> DimSlice
This commit is contained in:
@@ -5,12 +5,12 @@ use crate::vec::Vec3u;
|
|||||||
|
|
||||||
/// use this to wrap a flat region of memory into something which can be indexed by coordinates in
|
/// use this to wrap a flat region of memory into something which can be indexed by coordinates in
|
||||||
/// 3d space.
|
/// 3d space.
|
||||||
pub struct DimensionedSlice<T> {
|
pub struct DimSlice<T> {
|
||||||
dim: Vec3u,
|
dim: Vec3u,
|
||||||
items: T,
|
items: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DimensionedSlice<T> {
|
impl<T> DimSlice<T> {
|
||||||
pub fn new(dim: Vec3u, items: T) -> Self {
|
pub fn new(dim: Vec3u, items: T) -> Self {
|
||||||
Self { dim, items }
|
Self { dim, items }
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,7 @@ impl<T> DimensionedSlice<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: IntoIterator> DimensionedSlice<T> {
|
impl<T: IntoIterator> DimSlice<T> {
|
||||||
pub fn enumerated(self) -> Zip<DimIter, T::IntoIter> {
|
pub fn enumerated(self) -> Zip<DimIter, T::IntoIter> {
|
||||||
self.indices().zip(self.into_iter())
|
self.indices().zip(self.into_iter())
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ fn index(loc: Vec3u, dim: Vec3u) -> usize {
|
|||||||
((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
|
((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a T> {
|
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimSlice<&'a T> {
|
||||||
type Output=T::Output;
|
type Output=T::Output;
|
||||||
|
|
||||||
fn index(&self, idx: Vec3u) -> &Self::Output {
|
fn index(&self, idx: Vec3u) -> &Self::Output {
|
||||||
@@ -38,7 +38,7 @@ impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a mut T> {
|
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimSlice<&'a mut T> {
|
||||||
type Output=T::Output;
|
type Output=T::Output;
|
||||||
|
|
||||||
fn index(&self, idx: Vec3u) -> &Self::Output {
|
fn index(&self, idx: Vec3u) -> &Self::Output {
|
||||||
@@ -47,14 +47,14 @@ impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a mut T>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: IndexMut<usize> + ?Sized> IndexMut<Vec3u> for DimensionedSlice<&'a mut T> {
|
impl<'a, T: IndexMut<usize> + ?Sized> IndexMut<Vec3u> for DimSlice<&'a mut T> {
|
||||||
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
|
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
|
||||||
let idx = index(idx, self.dim);
|
let idx = index(idx, self.dim);
|
||||||
&mut self.items[idx]
|
&mut self.items[idx]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: IntoIterator> IntoIterator for DimensionedSlice<T> {
|
impl<T: IntoIterator> IntoIterator for DimSlice<T> {
|
||||||
type Item = T::Item;
|
type Item = T::Item;
|
||||||
type IntoIter = T::IntoIter;
|
type IntoIter = T::IntoIter;
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
@@ -118,7 +118,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_index() {
|
fn dim_slice_index() {
|
||||||
let data = [
|
let data = [
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
3, 4, 5,
|
3, 4, 5,
|
||||||
@@ -126,7 +126,7 @@ mod test {
|
|||||||
0, 10,20,
|
0, 10,20,
|
||||||
30,40,50,
|
30,40,50,
|
||||||
];
|
];
|
||||||
let s = DimensionedSlice::new(Vec3u::new(3, 2, 2), &data);
|
let s = DimSlice::new(Vec3u::new(3, 2, 2), &data);
|
||||||
assert_eq!(s[Vec3u::new(0, 0, 0)], 0);
|
assert_eq!(s[Vec3u::new(0, 0, 0)], 0);
|
||||||
assert_eq!(s[Vec3u::new(1, 0, 0)], 1);
|
assert_eq!(s[Vec3u::new(1, 0, 0)], 1);
|
||||||
assert_eq!(s[Vec3u::new(1, 1, 0)], 4);
|
assert_eq!(s[Vec3u::new(1, 1, 0)], 4);
|
||||||
@@ -135,7 +135,7 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_index_mut() {
|
fn dim_slice_index_mut() {
|
||||||
let mut data = [
|
let mut data = [
|
||||||
0, 1, 2,
|
0, 1, 2,
|
||||||
3, 4, 5,
|
3, 4, 5,
|
||||||
@@ -143,7 +143,7 @@ mod test {
|
|||||||
0, 10,20,
|
0, 10,20,
|
||||||
30,40,50,
|
30,40,50,
|
||||||
];
|
];
|
||||||
let mut s = DimensionedSlice::new(Vec3u::new(3, 2, 2), &mut data);
|
let mut s = DimSlice::new(Vec3u::new(3, 2, 2), &mut data);
|
||||||
s[Vec3u::new(0, 0, 0)] = 100;
|
s[Vec3u::new(0, 0, 0)] = 100;
|
||||||
s[Vec3u::new(0, 1, 1)] = 300;
|
s[Vec3u::new(0, 1, 1)] = 300;
|
||||||
|
|
||||||
@@ -157,9 +157,9 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_into_iter() {
|
fn dim_slice_into_iter() {
|
||||||
let data = [1, 2, 3, 4, 5, 6];
|
let data = [1, 2, 3, 4, 5, 6];
|
||||||
let s = DimensionedSlice::new(Vec3u::new(3, 1, 2), &data);
|
let s = DimSlice::new(Vec3u::new(3, 1, 2), &data);
|
||||||
let mut i = s.into_iter();
|
let mut i = s.into_iter();
|
||||||
assert_eq!(*i.next().unwrap(), 1);
|
assert_eq!(*i.next().unwrap(), 1);
|
||||||
assert_eq!(*i.next().unwrap(), 2);
|
assert_eq!(*i.next().unwrap(), 2);
|
||||||
@@ -171,9 +171,9 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_into_iter_mut() {
|
fn dim_slice_into_iter_mut() {
|
||||||
let mut data = [1, 2, 3, 4, 5, 6];
|
let mut data = [1, 2, 3, 4, 5, 6];
|
||||||
let s = DimensionedSlice::new(Vec3u::new(3, 1, 2), &mut data);
|
let s = DimSlice::new(Vec3u::new(3, 1, 2), &mut data);
|
||||||
let mut i = s.into_iter();
|
let mut i = s.into_iter();
|
||||||
*i.next().unwrap() = 10;
|
*i.next().unwrap() = 10;
|
||||||
assert_eq!(*i.next().unwrap(), 2);
|
assert_eq!(*i.next().unwrap(), 2);
|
||||||
@@ -187,8 +187,8 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_indices() {
|
fn dim_slice_indices() {
|
||||||
let s = DimensionedSlice::new(Vec3u::new(4, 3, 2), &[()]);
|
let s = DimSlice::new(Vec3u::new(4, 3, 2), &[()]);
|
||||||
let mut i = s.indices();
|
let mut i = s.indices();
|
||||||
assert!(i.next().unwrap() == Vec3u::new(0, 0, 0));
|
assert!(i.next().unwrap() == Vec3u::new(0, 0, 0));
|
||||||
assert!(i.next().unwrap() == Vec3u::new(1, 0, 0));
|
assert!(i.next().unwrap() == Vec3u::new(1, 0, 0));
|
||||||
@@ -224,40 +224,40 @@ mod test {
|
|||||||
assert!(i.next() == None);
|
assert!(i.next() == None);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_indices_zero_dim() {
|
fn dim_slice_indices_zero_dim() {
|
||||||
let s = DimensionedSlice::new(Vec3u::new(4, 3, 0), &[()]);
|
let s = DimSlice::new(Vec3u::new(4, 3, 0), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
|
|
||||||
let s = DimensionedSlice::new(Vec3u::new(4, 0, 2), &[()]);
|
let s = DimSlice::new(Vec3u::new(4, 0, 2), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
|
|
||||||
let s = DimensionedSlice::new(Vec3u::new(0, 3, 2), &[()]);
|
let s = DimSlice::new(Vec3u::new(0, 3, 2), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
|
|
||||||
let s = DimensionedSlice::new(Vec3u::new(3, 0, 0), &[()]);
|
let s = DimSlice::new(Vec3u::new(3, 0, 0), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
|
|
||||||
let s = DimensionedSlice::new(Vec3u::new(0, 1, 0), &[()]);
|
let s = DimSlice::new(Vec3u::new(0, 1, 0), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
|
|
||||||
let s = DimensionedSlice::new(Vec3u::new(0, 0, 2), &[()]);
|
let s = DimSlice::new(Vec3u::new(0, 0, 2), &[()]);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
assert!(s.indices().next() == None);
|
assert!(s.indices().next() == None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_enumerated() {
|
fn dim_slice_enumerated() {
|
||||||
let data = [
|
let data = [
|
||||||
10, 11,
|
10, 11,
|
||||||
20, 21,
|
20, 21,
|
||||||
30, 31,
|
30, 31,
|
||||||
];
|
];
|
||||||
let s = DimensionedSlice::new(Vec3u::new(1, 2, 3), &data);
|
let s = DimSlice::new(Vec3u::new(1, 2, 3), &data);
|
||||||
let mut i = s.enumerated();
|
let mut i = s.enumerated();
|
||||||
assert!(i.next().unwrap() == (Vec3u::new(0, 0, 0), &10));
|
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, 1, 0), &11));
|
||||||
@@ -269,13 +269,13 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dimensioned_slice_enumerated_mut() {
|
fn dim_slice_enumerated_mut() {
|
||||||
let mut data = [
|
let mut data = [
|
||||||
10, 11,
|
10, 11,
|
||||||
20, 21,
|
20, 21,
|
||||||
30, 31,
|
30, 31,
|
||||||
];
|
];
|
||||||
let s = DimensionedSlice::new(Vec3u::new(2, 1, 3), &mut data);
|
let s = DimSlice::new(Vec3u::new(2, 1, 3), &mut data);
|
||||||
let mut i = s.enumerated();
|
let mut i = s.enumerated();
|
||||||
|
|
||||||
let (idx, v) = i.next().unwrap();
|
let (idx, v) = i.next().unwrap();
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
mod dim_slice;
|
mod dim_slice;
|
||||||
mod offset_dim_slice;
|
mod offset_dim_slice;
|
||||||
pub use dim_slice::{
|
pub use dim_slice::{
|
||||||
DimensionedSlice,
|
DimSlice,
|
||||||
DimIter,
|
DimIter,
|
||||||
};
|
};
|
||||||
pub use offset_dim_slice::OffsetDimensionedSlice;
|
pub use offset_dim_slice::OffsetDimSlice;
|
||||||
|
@@ -1,34 +1,34 @@
|
|||||||
use core::ops::{Index, IndexMut};
|
use core::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use crate::dim::DimensionedSlice;
|
use crate::dim::DimSlice;
|
||||||
use crate::vec::Vec3u;
|
use crate::vec::Vec3u;
|
||||||
|
|
||||||
|
|
||||||
pub struct OffsetDimensionedSlice<T> {
|
pub struct OffsetDimSlice<T> {
|
||||||
offset: Vec3u,
|
offset: Vec3u,
|
||||||
inner: DimensionedSlice<T>,
|
inner: DimSlice<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> OffsetDimensionedSlice<T> {
|
impl<T> OffsetDimSlice<T> {
|
||||||
pub fn new(offset: Vec3u, dim: Vec3u, items: T) -> Self {
|
pub fn new(offset: Vec3u, dim: Vec3u, items: T) -> Self {
|
||||||
Self { offset, inner: DimensionedSlice::new(dim, items) }
|
Self { offset, inner: DimSlice::new(dim, items) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Index<Vec3u> for OffsetDimensionedSlice<T>
|
impl<T> Index<Vec3u> for OffsetDimSlice<T>
|
||||||
where
|
where
|
||||||
DimensionedSlice<T>: Index<Vec3u>
|
DimSlice<T>: Index<Vec3u>
|
||||||
{
|
{
|
||||||
type Output=<DimensionedSlice<T> as Index<Vec3u>>::Output;
|
type Output=<DimSlice<T> as Index<Vec3u>>::Output;
|
||||||
|
|
||||||
fn index(&self, idx: Vec3u) -> &Self::Output {
|
fn index(&self, idx: Vec3u) -> &Self::Output {
|
||||||
&self.inner[idx - self.offset]
|
&self.inner[idx - self.offset]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> IndexMut<Vec3u> for OffsetDimensionedSlice<T>
|
impl<T> IndexMut<Vec3u> for OffsetDimSlice<T>
|
||||||
where
|
where
|
||||||
DimensionedSlice<T>: IndexMut<Vec3u>
|
DimSlice<T>: IndexMut<Vec3u>
|
||||||
{
|
{
|
||||||
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
|
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
|
||||||
&mut self.inner[idx - self.offset]
|
&mut self.inner[idx - self.offset]
|
||||||
@@ -48,7 +48,7 @@ mod test {
|
|||||||
0, 10,20,
|
0, 10,20,
|
||||||
30,40,50,
|
30,40,50,
|
||||||
];
|
];
|
||||||
let s = OffsetDimensionedSlice::new(Vec3u::new(1, 2, 3), Vec3u::new(3, 2, 2), &data);
|
let s = OffsetDimSlice::new(Vec3u::new(1, 2, 3), Vec3u::new(3, 2, 2), &data);
|
||||||
assert_eq!(s[Vec3u::new(1, 2, 3)], 0);
|
assert_eq!(s[Vec3u::new(1, 2, 3)], 0);
|
||||||
assert_eq!(s[Vec3u::new(2, 2, 3)], 1);
|
assert_eq!(s[Vec3u::new(2, 2, 3)], 1);
|
||||||
assert_eq!(s[Vec3u::new(2, 3, 3)], 4);
|
assert_eq!(s[Vec3u::new(2, 3, 3)], 4);
|
||||||
@@ -65,7 +65,7 @@ mod test {
|
|||||||
0, 10,20,
|
0, 10,20,
|
||||||
30,40,50,
|
30,40,50,
|
||||||
];
|
];
|
||||||
let mut s = OffsetDimensionedSlice::new(Vec3u::new(1, 2, 3), Vec3u::new(3, 2, 2), &mut data);
|
let mut s = OffsetDimSlice::new(Vec3u::new(1, 2, 3), Vec3u::new(3, 2, 2), &mut data);
|
||||||
s[Vec3u::new(1, 2, 3)] = 100;
|
s[Vec3u::new(1, 2, 3)] = 100;
|
||||||
s[Vec3u::new(1, 3, 4)] = 300;
|
s[Vec3u::new(1, 3, 4)] = 300;
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
use core::ops::{Index, IndexMut};
|
use core::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use crate::compound::Optional;
|
use crate::compound::Optional;
|
||||||
use crate::dim::DimensionedSlice;
|
use crate::dim::DimSlice;
|
||||||
use crate::mat::Material;
|
use crate::mat::Material;
|
||||||
use crate::real::Real;
|
use crate::real::Real;
|
||||||
use crate::vec::{Vec3, Vec3u};
|
use crate::vec::{Vec3, Vec3u};
|
||||||
@@ -274,10 +274,10 @@ impl<'a, R: Real, M: Material<R>> StepEContext<'a, R, M> {
|
|||||||
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
|
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
|
||||||
{
|
{
|
||||||
let dim = meta.dim;
|
let dim = meta.dim;
|
||||||
let stim_e_matrix = DimensionedSlice::new(dim, stim_e);
|
let stim_e_matrix = DimSlice::new(dim, stim_e);
|
||||||
let mat_matrix = DimensionedSlice::new(dim, mat);
|
let mat_matrix = DimSlice::new(dim, mat);
|
||||||
let mut e_matrix = DimensionedSlice::new(dim, e);
|
let mut e_matrix = DimSlice::new(dim, e);
|
||||||
let h_matrix = DimensionedSlice::new(dim, h);
|
let h_matrix = DimSlice::new(dim, h);
|
||||||
|
|
||||||
let stim_e = stim_e_matrix[idx];
|
let stim_e = stim_e_matrix[idx];
|
||||||
let mat = &mat_matrix[idx];
|
let mat = &mat_matrix[idx];
|
||||||
@@ -342,11 +342,11 @@ impl<'a, R: Real, M: Material<R>> StepHContext<'a, R, M> {
|
|||||||
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
|
WF: Index<usize, Output=Vec3<R>> + IndexMut<usize> + ?Sized,
|
||||||
{
|
{
|
||||||
let dim = meta.dim;
|
let dim = meta.dim;
|
||||||
let stim_h_matrix = DimensionedSlice::new(dim, stim_h);
|
let stim_h_matrix = DimSlice::new(dim, stim_h);
|
||||||
let mat_matrix = DimensionedSlice::new(dim, mat);
|
let mat_matrix = DimSlice::new(dim, mat);
|
||||||
let e_matrix = DimensionedSlice::new(dim, e);
|
let e_matrix = DimSlice::new(dim, e);
|
||||||
let mut h_matrix = DimensionedSlice::new(dim, h);
|
let mut h_matrix = DimSlice::new(dim, h);
|
||||||
let mut m_matrix = DimensionedSlice::new(dim, m);
|
let mut m_matrix = DimSlice::new(dim, m);
|
||||||
|
|
||||||
let stim_h = stim_h_matrix[idx];
|
let stim_h = stim_h_matrix[idx];
|
||||||
let mat = &mat_matrix[idx];
|
let mat = &mat_matrix[idx];
|
||||||
|
Reference in New Issue
Block a user