Struct all_is_cubes::math::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<()>

source

pub fn subdivide(self) -> Option<(Vol<()>, Vol<()>)>

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, V> Vol<C, O>
where O: Default, C: Deref<Target = [V]>,

Constructors from linear containers.

source

pub fn from_elements( bounds: GridAab, elements: impl Into<C>, ) -> Result<Vol<C, O>, 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>
where C: Deref<Target = [V]> + FromIterator<V>,

Constructors from elements.

source

pub fn from_fn<F>(bounds: GridAab, f: F) -> Vol<C>
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) -> Vol<C>
where V: Clone,

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

TODO: This feels like it should be called ‘filled’ or ‘cloned’, but if so, maybe FaceMap::repeat should also change?

source

pub fn from_element(value: V) -> Vol<C>

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<Vector3D<i32, Cube>>) -> Vol<C, O>

Translates the volume without affecting its contents.

Panics if this would cause numeric overflow.

TODO: example

source§

impl<C> Vol<C>

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]>

source

pub fn subdivide(self) -> Option<(Vol<&'a [V]>, Vol<&'a [V]>)>

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<'a, V> Vol<&'a mut [V]>

source

pub fn subdivide(self) -> Option<(Vol<&'a mut [V]>, Vol<&'a mut [V]>)>

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 immutable references and (). These are intended to be useful in executing parallel algorithms on volume data.

source§

impl<C, V> Vol<C>
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<'a, V, C> Arbitrary<'a> for Vol<C>
where V: Arbitrary<'a>, C: FromIterator<V> + Deref<Target = [V]>,

source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Vol<C>, Error>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

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

source§

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

Returns a copy 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, O> Debug for Vol<C, O>
where C: Debug, O: Debug,

source§

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

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

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

source§

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

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>
where P: Into<Cube>, C: Deref<Target = [V]>,

source§

fn index(&self, position: P) -> &<Vol<C> as Index<P>>::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.

§

type Output = V

The returned type after indexing.
source§

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

source§

fn index_mut(&mut self, position: P) -> &mut <Vol<C> as Index<P>>::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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

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

source§

impl SpaceBuilderBounds for Vol<()>

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: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

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

§

type EqTo = T

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,

§

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>,

§

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>,

§

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.