Vol

Struct Vol 

Source
pub struct Vol<C, O = ZMaj> { /* private fields */ }
Expand description

Type for volume data stored in a slice, or for generating linear indexing.

  • C is some slice container type, e.g. &[T] or Box<[T]>. It may also be () to describe a linearization without actually storing data.
  • O specifies the choice of linearization. Currently, only one choice exists, ZMaj.

In addition to the data, each Vol stores the GridAab defining its size; the container’s length must be equal to the volume of that AAB. Whether that can be relied upon entirely depends on whether the specific container value produces the same length of slice every time it is Dereferenced without mutating it directly. For example, Vec<T> and Box<[T]> satisfy this criterion; the fact that Vec has length-mutating operations is irrelevant because no &mut Vec<T> is exposed.

A Vol whose volume exceeds usize::MAX cannot exist.

Implementations§

Source§

impl<O> Vol<(), O>

Source

pub fn with_elements<C, V>( self, elements: C, ) -> Result<Vol<C, O>, VolLengthError>
where C: Deref<Target = [V]>,

Attach some data to this dataless Vol.

Returns a VolLengthError if the number of elements does not match bounds.volume().

Source§

impl Vol<(), ZMaj>

Source

pub fn subdivide(self) -> Result<[Self; 2], Self>

Divide self into two approximately equal-sized parts which, if they had elements, would each be contiguous in the linear ordering.

Returns None if self does not have at least two cubes.

Note that this is one of several subdivide() methods for different container types; it is also implemented for immutable and mutable references. These are intended to be useful in executing parallel algorithms on volume data.

Source§

impl<C, O: Default, V> Vol<C, O>
where C: Deref<Target = [V]>,

Constructors from linear containers.

Source

pub fn from_elements( bounds: GridAab, elements: impl Into<C>, ) -> Result<Self, VolLengthError>

Constructs a Vol<C> containing the provided elements, which must be in the ordering specified by O.

Returns a VolLengthError if the number of elements does not match bounds.volume().

Source§

impl<C, V> Vol<C, ZMaj>
where C: Deref<Target = [V]> + FromIterator<V>,

Constructors from elements.

Source

pub fn from_fn<F>(bounds: GridAab, f: F) -> Self
where F: FnMut(Cube) -> V,

Constructs a Vol<C> by using the provided function to compute a value for each point.

Panics if bounds has a volume exceeding usize::MAX. (But there will likely be a memory allocation failure well below that point.)

Source

pub fn repeat(bounds: GridAab, value: V) -> Self
where V: Clone,

Constructs a Vol<C> by cloning the provided value for each point.

Source

pub fn from_element(value: V) -> Self

Constructs a Vol<C> with a single value, in bounds ORIGIN_CUBE.

If the single element should be at a different location, you can call .translate(offset), or use Vol::from_elements() instead.

Source§

impl<C, O> Vol<C, O>

Source

pub fn bounds(&self) -> GridAab

Returns the GridAab specifying the bounds of this volume data.

Source

pub fn volume(&self) -> usize

Returns the volume, also known as the number of elements.

Source

pub fn into_elements(self) -> C

Extracts the linear contents, discarding the bounds and ordering.

Source

pub fn without_elements(&self) -> Vol<(), O>
where O: Clone,

Returns a Vol with the same bounds and ordering but no data.

This is the inverse operation to Vol::with_elements().

Source

pub fn translate(self, offset: impl Into<GridVector>) -> Self

Translates the volume without affecting its contents.

Panics if this would cause numeric overflow.

TODO: example

Source§

impl<C> Vol<C, ZMaj>

Source

pub fn iter_cubes(&self) -> GridIter

Iterate over all cubes that this contains, in the order of the linearization, without including the stored data (if there is any).

Source

pub fn index(&self, cube: Cube) -> Option<usize>

Determines whether a unit cube lies within this volume and, if it does, returns the linearized slice index into it.

The linearized element order is defined by the O type.

use all_is_cubes::math::{Vol, GridAab};

let vol = GridAab::from_lower_size([0, 0, 0], [10, 10, 10]).to_vol().unwrap();

assert_eq!(vol.index([0, 0, 0].into()), Some(0));
assert_eq!(vol.index([1, 2, 3].into()), Some(123));
assert_eq!(vol.index([9, 9, 9].into()), Some(999));
assert_eq!(vol.index([0, 0, -1].into()), None);
assert_eq!(vol.index([0, 0, 10].into()), None);

TODO: more example, less unit-test

Source§

impl<C, O, V> Vol<C, O>
where C: Deref<Target = [V]>, O: Copy,

Linear data access.

Source

pub fn as_ref(&self) -> Vol<&[V], O>

Return a Vol that borrows the contents of this one.

Source

pub fn as_mut(&mut self) -> Vol<&mut [V], O>
where C: DerefMut,

Return a Vol that mutably borrows the contents of this one.

Source

pub fn as_linear(&self) -> &[V]

Returns the linear contents viewed as a slice.

Source

pub fn as_linear_mut(&mut self) -> &mut [V]
where C: DerefMut,

Returns the linear contents viewed as a mutable slice.

Source§

impl<'a, V> Vol<&'a [V], ZMaj>

Source

pub fn get_ref(&self, position: impl Into<Cube>) -> Option<&'a V>

Returns the element at position of this volume data, or None if position is out of bounds.

This differs from Self::get() in that it inherits the lifetime of the container reference, rather than reborrowing. It is therefore only available for Vol<&'a [V]>.

Source

pub fn subdivide(self) -> Result<[Self; 2], Self>

Divide self into two approximately equal-sized parts, each of which refers to the appropriate sub-slice of elements.

Returns None if self does not have at least two cubes.

Note that this is one of several subdivide() methods for different container types; it is also implemented for mutable references and (). These are intended to be useful in executing parallel algorithms on volume data.

Source§

impl<V> Vol<&mut [V], ZMaj>

Source

pub fn subdivide( self, filter: impl FnOnce([Vol<()>; 2]) -> bool, ) -> Result<[Self; 2], Self>

Divide self into two approximately equal-sized parts. each of which refers to the appropriate sub-slice of elements.

filter may be used to reject subdivisions that are too small or otherwise unsuitable; it is passed the bounds of the two slices that would be returned.

Returns Err containing self if self does not have at least two cubes, or if filter returns false.

Note that this is one of several subdivide() methods for different container types; it is also implemented for immutable references and (). These are intended to be useful in executing parallel algorithms on volume data.

Source§

impl<C, V> Vol<C, ZMaj>
where C: Deref<Target = [V]>,

Element lookup operations by 3D coordinates.

Source

pub fn get(&self, position: impl Into<Cube>) -> Option<&V>

Returns the element at position of this volume data, or None if position is out of bounds.

Source

pub fn get_mut(&mut self, position: impl Into<Cube>) -> Option<&mut V>
where C: DerefMut,

Returns a mutable reference to the element at position of this volume data, or None if position is out of bounds.

Source

pub fn iter<'s>(&'s self) -> impl Iterator<Item = (Cube, &'s V)> + Clone
where V: 's,

Iterates over all the cubes and values in this volume data, in the ordering specified by the O type parameter.

Source

pub fn iter_mut<'s>(&'s mut self) -> impl Iterator<Item = (Cube, &'s mut V)>
where C: DerefMut, V: 's,

Iterates by mutable reference over all the cubes and values in this volume data, in the ordering specified by the O type parameter.

Source§

impl<V, O> Vol<Box<[V]>, O>

Source

pub fn map<T, F>(self, f: F) -> Vol<Box<[T]>, O>
where F: FnMut(V) -> T,

Apply f to each element and collect the results into the same shape and ordering.

Trait Implementations§

Source§

impl<C: Clone, O: Clone> Clone for Vol<C, O>

Source§

fn clone(&self) -> Vol<C, O>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug, O: Debug> Debug for Vol<C, O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: Hash, O: Hash> Hash for Vol<C, O>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<P, C, V> Index<P> for Vol<C, ZMaj>
where P: Into<Cube>, C: Deref<Target = [V]>,

Source§

fn index(&self, position: P) -> &Self::Output

Returns the element at position of this volume data, or panics if position is out of bounds.

Use Vol::get() for a non-panicing alternative.

Source§

type Output = V

The returned type after indexing.
Source§

impl<P, C, V> IndexMut<P> for Vol<C, ZMaj>
where P: Into<Cube>, C: DerefMut<Target = [V]>,

Source§

fn index_mut(&mut self, position: P) -> &mut Self::Output

Returns the element at position of this volume data, or panics if position is out of bounds.

Source§

impl<O> PartialEq<GridAab> for Vol<(), O>

Source§

fn eq(&self, other: &GridAab) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<O> PartialEq<Vol<(), O>> for GridAab

Source§

fn eq(&self, other: &Vol<(), O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: PartialEq, O: PartialEq> PartialEq for Vol<C, O>

Source§

fn eq(&self, other: &Vol<C, O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: Copy, O: Copy> Copy for Vol<C, O>

Source§

impl<C: Eq, O: Eq> Eq for Vol<C, O>

Source§

impl<C, O> StructuralPartialEq for Vol<C, O>

Auto Trait Implementations§

§

impl<C, O> Freeze for Vol<C, O>
where O: Freeze, C: Freeze,

§

impl<C, O> RefUnwindSafe for Vol<C, O>

§

impl<C, O> Send for Vol<C, O>
where O: Send, C: Send,

§

impl<C, O> Sync for Vol<C, O>
where O: Sync, C: Sync,

§

impl<C, O> Unpin for Vol<C, O>
where O: Unpin, C: Unpin,

§

impl<C, O> UnwindSafe for Vol<C, O>
where O: UnwindSafe, C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.