pub struct ConstantVec<D, T, C = UnboundedCard<D>>{ /* private fields */ }
Expand description
A constant vector which returns the same value for any input index.
§Example
use orx_v::*;
let v1 = V.d1().constant(42).bounded(4);
assert_eq!(v1.at([2]), 42);
assert_eq!(
v1.equality(&[42, 42, 42, 42]),
Equality::Equal
);
let v2 = V.d2().constant(42).with_rectangular_bounds([2, 3]);
assert_eq!(v2.at([0, 2]), 42);
assert_eq!(v2.at([1, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42, 42], vec![42, 42, 42]]),
Equality::Equal
);
Implementations§
Source§impl<D, T, C> ConstantVec<D, T, C>
impl<D, T, C> ConstantVec<D, T, C>
Sourcepub fn new(value: T, card: C) -> Self
pub fn new(value: T, card: C) -> Self
Creates a new constant vector with the given card
where all elements are
equal to value
.
Alternatively, unbounded constant vectors of different dimensions can be
created by V.d1().constant(42)
, V.d2().constant(42)
, etc., which can
later be transformed into bounded vectors by applying bounded
,
with_rectangular_bounds
or with_variable_bounds
transformations.
Source§impl<T> ConstantVec<D1, T, UnboundedCard<D1>>where
T: Copy,
impl<T> ConstantVec<D1, T, UnboundedCard<D1>>where
T: Copy,
Sourcepub fn bounded(self, num_elements: usize) -> ConstantVec<D1, T, CardD1>
pub fn bounded(self, num_elements: usize) -> ConstantVec<D1, T, CardD1>
Transforms an unbounded constant vector into one with a fixed length.
§Example
use orx_v::*;
let v1 = V.d1().constant(42);
assert_eq!(v1.card([]), usize::MAX);
assert_eq!(v1.at([2]), 42);
assert_eq!(v1.at([42]), 42);
let v1 = v1.bounded(4);
// or
let v1 = V.d1().constant(42).bounded(4);
assert_eq!(v1.card([]), 4);
assert_eq!(v1.at([2]), 42);
assert_eq!(v1.try_at([2]), Some(42));
assert_eq!(v1.try_at([42]), None);
assert_eq!(v1.all().collect::<Vec<_>>(), vec![42, 42, 42, 42]);
assert_eq!(v1.at([42]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Source§impl<T> ConstantVec<D2, T, UnboundedCard<D2>>where
T: Copy,
impl<T> ConstantVec<D2, T, UnboundedCard<D2>>where
T: Copy,
Sourcepub fn with_rectangular_bounds(
self,
dimensions: [usize; 2],
) -> ConstantVec<D2, T, RectangularCardD2>
pub fn with_rectangular_bounds( self, dimensions: [usize; 2], ) -> ConstantVec<D2, T, RectangularCardD2>
Transforms an unbounded constant vector into one with rectangular bounds as in matrices:
- the vector has
dimensions[0]
children, and - each children has
dimensions[1]
elements.
§Example
use orx_v::*;
let v2 = V.d2().constant(42).with_rectangular_bounds([2, 3]);
assert_eq!(v2.card([]), 2);
assert_eq!(v2.card([0]), 3);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.at([0, 2]), 42);
assert_eq!(v2.at([1, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42, 42], vec![42, 42, 42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Sourcepub fn with_variable_bounds<C>(
self,
cardinality: C,
) -> ConstantVec<D2, T, VariableCardD2<C>>
pub fn with_variable_bounds<C>( self, cardinality: C, ) -> ConstantVec<D2, T, VariableCardD2<C>>
Transforms an unbounded constant vector into one with variable bounds as in jagged arrays:
- the vector has
cardinality.card([])
children, and - i-th child has
cardinality.at([i])
elements.
§Example
use orx_v::*;
// jagged => [ [42, 42], [42, 42, 42], [42] ]
let num_cols = vec![2, 3, 1];
let v2 = V.d2().constant(42).with_variable_bounds(&num_cols);
assert_eq!(v2.card([]), 3);
assert_eq!(v2.card([0]), 2);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.card([2]), 1);
assert_eq!(v2.at([1, 2]), 42);
assert_eq!(v2.at([2, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42], vec![42, 42, 42], vec![42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Source§impl<T> ConstantVec<D3, T, UnboundedCard<D3>>where
T: Copy,
impl<T> ConstantVec<D3, T, UnboundedCard<D3>>where
T: Copy,
Sourcepub fn with_rectangular_bounds(
self,
dimensions: [usize; 3],
) -> ConstantVec<D3, T, RectangularCardD3>
pub fn with_rectangular_bounds( self, dimensions: [usize; 3], ) -> ConstantVec<D3, T, RectangularCardD3>
Transforms an unbounded constant vector into one with rectangular bounds as in
multi-dimensional matrices. The matrix has
dimensions[0]
x dimensions[1]
x dimensions[2]
elements.
§Example
use orx_v::*;
let v2 = V.d2().constant(42).with_rectangular_bounds([2, 3]);
assert_eq!(v2.card([]), 2);
assert_eq!(v2.card([0]), 3);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.at([0, 2]), 42);
assert_eq!(v2.at([1, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42, 42], vec![42, 42, 42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Sourcepub fn with_variable_bounds<C>(
self,
cardinality: C,
) -> ConstantVec<D3, T, VariableCardD3<C>>
pub fn with_variable_bounds<C>( self, cardinality: C, ) -> ConstantVec<D3, T, VariableCardD3<C>>
Transforms an unbounded constant vector into one with variable bounds as in jagged arrays:
- the vector has
cardinality.card([])
children, and - i-th child has
cardinality.at([i])
elements.
§Example
use orx_v::*;
// jagged => [ [42, 42], [42, 42, 42], [42] ]
let num_cols = vec![2, 3, 1];
let v2 = V.d2().constant(42).with_variable_bounds(&num_cols);
assert_eq!(v2.card([]), 3);
assert_eq!(v2.card([0]), 2);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.card([2]), 1);
assert_eq!(v2.at([1, 2]), 42);
assert_eq!(v2.at([2, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42], vec![42, 42, 42], vec![42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Source§impl<T> ConstantVec<D4, T, UnboundedCard<D4>>where
T: Copy,
impl<T> ConstantVec<D4, T, UnboundedCard<D4>>where
T: Copy,
Sourcepub fn with_rectangular_bounds(
self,
dimensions: [usize; 4],
) -> ConstantVec<D4, T, RectangularCardD4>
pub fn with_rectangular_bounds( self, dimensions: [usize; 4], ) -> ConstantVec<D4, T, RectangularCardD4>
Transforms an unbounded constant vector into one with rectangular bounds as in
multi-dimensional matrices. The matrix has
dimensions[0]
x dimensions[1]
x dimensions[2]
x dimensions[3]
elements.
§Example
use orx_v::*;
let v2 = V.d2().constant(42).with_rectangular_bounds([2, 3]);
assert_eq!(v2.card([]), 2);
assert_eq!(v2.card([0]), 3);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.at([0, 2]), 42);
assert_eq!(v2.at([1, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42, 42], vec![42, 42, 42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Sourcepub fn with_variable_bounds<C>(
self,
cardinality: C,
) -> ConstantVec<D4, T, VariableCardD4<C>>
pub fn with_variable_bounds<C>( self, cardinality: C, ) -> ConstantVec<D4, T, VariableCardD4<C>>
Transforms an unbounded constant vector into one with variable bounds as in jagged arrays:
- the vector has
cardinality.card([])
children, and - i-th child has
cardinality.at([i])
elements.
§Example
use orx_v::*;
// jagged => [ [42, 42], [42, 42, 42], [42] ]
let num_cols = vec![2, 3, 1];
let v2 = V.d2().constant(42).with_variable_bounds(&num_cols);
assert_eq!(v2.card([]), 3);
assert_eq!(v2.card([0]), 2);
assert_eq!(v2.card([1]), 3);
assert_eq!(v2.card([2]), 1);
assert_eq!(v2.at([1, 2]), 42);
assert_eq!(v2.at([2, 0]), 42);
assert_eq!(
v2.equality(&vec![vec![42, 42], vec![42, 42, 42], vec![42]]),
Equality::Equal
);
assert_eq!(v2.try_at([42, 113]), None);
assert_eq!(v2.at([42, 113]), 42); // (!) un-compromised performance
(!) un-compromised performance
Main reason to add bounds to a functional vector is to set its domain.
However, calling at
with an out-of-bounds index can still produce a valid
element in order not to compromise performance.
If we want to check whether or not an index is in bounds or not, we can
use the in_bounds
or card
methods, or use try_at
instead which would
return None
if the index is out of bounds.
Trait Implementations§
Source§impl<D, T, C> Clone for ConstantVec<D, T, C>
impl<D, T, C> Clone for ConstantVec<D, T, C>
Source§fn clone(&self) -> ConstantVec<D, T, C>
fn clone(&self) -> ConstantVec<D, T, C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T, C> Debug for ConstantVec<D1, T, C>
impl<T, C> Debug for ConstantVec<D1, T, C>
Source§impl<T, C> Debug for ConstantVec<D2, T, C>
impl<T, C> Debug for ConstantVec<D2, T, C>
Source§impl<T, C> Debug for ConstantVec<D3, T, C>
impl<T, C> Debug for ConstantVec<D3, T, C>
Source§impl<T, C> Debug for ConstantVec<D4, T, C>
impl<T, C> Debug for ConstantVec<D4, T, C>
Source§impl<D, T, C> NVec<D, T> for ConstantVec<D, T, C>
impl<D, T, C> NVec<D, T> for ConstantVec<D, T, C>
Source§fn at(&self, _: impl IntoIdx<D>) -> T
fn at(&self, _: impl IntoIdx<D>) -> T
idx
-th position of the vector. Read moreSource§fn child(&self, i: <D as Dim>::ChildIdx) -> impl NVec<<D as Dim>::PrevDim, T>
fn child(&self, i: <D as Dim>::ChildIdx) -> impl NVec<<D as Dim>::PrevDim, T>
i
-th child of the vector. Read moreSource§fn all(&self) -> impl Iterator<Item = T>
fn all(&self) -> impl Iterator<Item = T>
Source§fn num_children(&self) -> usize
fn num_children(&self) -> usize
Source§fn card(&self, idx: impl Into<D::CardIdx>) -> usize
fn card(&self, idx: impl Into<D::CardIdx>) -> usize
Source§fn is_bounded(&self) -> bool
fn is_bounded(&self) -> bool
Source§fn is_rectangular(&self) -> bool
fn is_rectangular(&self) -> bool
D
has the same number of children
at a given lower dimension for all indices. Read moreSource§fn is_unbounded(&self) -> bool
fn is_unbounded(&self) -> bool
Source§fn in_bounds(&self, idx: impl Into<D::LeqIdx>) -> bool
fn in_bounds(&self, idx: impl Into<D::LeqIdx>) -> bool
idx
is in bounds. Read moreSource§fn card_equality(&self, other: &impl NVec<D, T>) -> CardEquality<D>
fn card_equality(&self, other: &impl NVec<D, T>) -> CardEquality<D>
other
: Read moreSource§fn try_at(&self, idx: impl IntoIdx<D>) -> Option<T>
fn try_at(&self, idx: impl IntoIdx<D>) -> Option<T>
idx
-th position of the vector if the
index is in_bounds
; returns None otherwise. Read moreSource§fn equality(&self, other: &impl NVec<D, T>) -> Equality<D>where
T: PartialEq,
fn equality(&self, other: &impl NVec<D, T>) -> Equality<D>where
T: PartialEq,
other
: Read moreimpl<D, T, C> Copy for ConstantVec<D, T, C>
Auto Trait Implementations§
impl<D, T, C> Freeze for ConstantVec<D, T, C>
impl<D, T, C> RefUnwindSafe for ConstantVec<D, T, C>
impl<D, T, C> Send for ConstantVec<D, T, C>
impl<D, T, C> Sync for ConstantVec<D, T, C>
impl<D, T, C> Unpin for ConstantVec<D, T, C>
impl<D, T, C> UnwindSafe for ConstantVec<D, T, C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, V> IntoJagged<T> for V
impl<T, V> IntoJagged<T> for V
Source§fn into_jagged<I>(self, row_end_indices: I) -> FlatJagged<Self, I, T>
fn into_jagged<I>(self, row_end_indices: I) -> FlatJagged<Self, I, T>
D1
vector into a jagged D2
vector where each row is
identified by row_end_indices
. Read moreSource§fn as_jagged<I>(&self, row_end_indices: I) -> FlatJagged<&Self, I, T>
fn as_jagged<I>(&self, row_end_indices: I) -> FlatJagged<&Self, I, T>
D1
vector, creates a jagged D2
vector view where each row is
identified by row_end_indices
. Read moreSource§fn as_jagged_mut<I>(
&mut self,
row_end_indices: I,
) -> FlatJagged<&mut Self, I, T>
fn as_jagged_mut<I>( &mut self, row_end_indices: I, ) -> FlatJagged<&mut Self, I, T>
D1
vector, creates a mutable jagged D2
vector view where each row is
identified by row_end_indices
. Read moreSource§fn into_jagged_from_row_lengths<I>(
self,
row_lengths: &I,
) -> FlatJagged<Self, Vec<usize>, T>
fn into_jagged_from_row_lengths<I>( self, row_lengths: &I, ) -> FlatJagged<Self, Vec<usize>, T>
Source§fn as_jagged_from_row_lengths<I>(
&self,
row_lengths: &I,
) -> FlatJagged<&Self, Vec<usize>, T>
fn as_jagged_from_row_lengths<I>( &self, row_lengths: &I, ) -> FlatJagged<&Self, Vec<usize>, T>
D1
vector, creates a jagged D2
vector view where each row is
identified by row_lengths
. Read moreSource§fn as_jagged_mut_from_row_lengths<I>(
&mut self,
row_lengths: &I,
) -> FlatJagged<&mut Self, Vec<usize>, T>
fn as_jagged_mut_from_row_lengths<I>( &mut self, row_lengths: &I, ) -> FlatJagged<&mut Self, Vec<usize>, T>
D1
vector, creates a mutable jagged D2
vector view where each row is
identified by row_lengths
. Read moreSource§fn into_jagged_with_uniform_lengths(
self,
uniform_length: usize,
) -> FlatJagged<Self, UniformEndIndices, T>
fn into_jagged_with_uniform_lengths( self, uniform_length: usize, ) -> FlatJagged<Self, UniformEndIndices, T>
D1
vector into a jagged D2
vector where each row has the given
uniform_length
, except that the last row might have fewer elements if the
cardinality of the D1
vector is not divisible by the uniform length. Read moreSource§fn as_jagged_with_uniform_lengths(
&self,
uniform_length: usize,
) -> FlatJagged<&Self, UniformEndIndices, T>
fn as_jagged_with_uniform_lengths( &self, uniform_length: usize, ) -> FlatJagged<&Self, UniformEndIndices, T>
D1
vector, creates a jagged D2
vector view where each row has
the given uniform_length
, except that the last row might have fewer elements
if the cardinality of the D1
vector is not divisible by the uniform length. Read moreSource§fn as_jagged_mut_with_uniform_lengths(
&mut self,
uniform_length: usize,
) -> FlatJagged<&mut Self, UniformEndIndices, T>
fn as_jagged_mut_with_uniform_lengths( &mut self, uniform_length: usize, ) -> FlatJagged<&mut Self, UniformEndIndices, T>
D1
vector, creates a mutable jagged D2
vector view where each row has
the given uniform_length
, except that the last row might have fewer elements
if the cardinality of the D1
vector is not divisible by the uniform length. Read moreSource§impl<T, V> V1AsMatrix<T> for V
impl<T, V> V1AsMatrix<T> for V
Source§fn v1_into_matrix(
self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, Self, V1LayoutRowMajor>
fn v1_into_matrix( self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, Self, V1LayoutRowMajor>
D1
vector into a row-major matrix. Read moreSource§fn v1_as_matrix(
&self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, &Self, V1LayoutRowMajor>
fn v1_as_matrix( &self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, &Self, V1LayoutRowMajor>
D1
vector. Read moreSource§fn v1_as_matrix_mut(
&mut self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, &mut Self, V1LayoutRowMajor>
fn v1_as_matrix_mut( &mut self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, &mut Self, V1LayoutRowMajor>
D1
vector. Read moreSource§fn v1_into_matrix_col_major(
self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, Self, V1LayoutColMajor>
fn v1_into_matrix_col_major( self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, Self, V1LayoutColMajor>
D1
vector into a column-major matrix. Read moreSource§fn v1_as_matrix_col_major(
&self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, &Self, V1LayoutColMajor>
fn v1_as_matrix_col_major( &self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, &Self, V1LayoutColMajor>
D1
vector. Read moreSource§fn v1_as_matrix_col_major_mut(
&mut self,
num_rows: usize,
num_cols: usize,
) -> V1Matrix<T, &mut Self, V1LayoutColMajor>
fn v1_as_matrix_col_major_mut( &mut self, num_rows: usize, num_cols: usize, ) -> V1Matrix<T, &mut Self, V1LayoutColMajor>
D1
vector. Read moreSource§impl<T, V> V2AsMatrix<T> for V
impl<T, V> V2AsMatrix<T> for V
Source§fn into_matrix(self) -> V2MatrixRowMajor<T, Self>
fn into_matrix(self) -> V2MatrixRowMajor<T, Self>
D2
vector into a row-major matrix. Read moreSource§fn as_matrix(&self) -> V2MatrixRowMajor<T, &Self>
fn as_matrix(&self) -> V2MatrixRowMajor<T, &Self>
D2
vector. Read moreSource§fn as_matrix_mut(&mut self) -> V2MatrixRowMajor<T, &mut Self>
fn as_matrix_mut(&mut self) -> V2MatrixRowMajor<T, &mut Self>
D2
vector. Read moreSource§fn into_matrix_col_major(self) -> V2MatrixColMajor<T, Self>
fn into_matrix_col_major(self) -> V2MatrixColMajor<T, Self>
D2
vector into a column-major matrix. Read moreSource§fn as_matrix_col_major(&self) -> V2MatrixColMajor<T, &Self>
fn as_matrix_col_major(&self) -> V2MatrixColMajor<T, &Self>
D2
vector. Read moreSource§fn as_matrix_col_major_mut(&mut self) -> V2MatrixColMajor<T, &mut Self>
fn as_matrix_col_major_mut(&mut self) -> V2MatrixColMajor<T, &mut Self>
D2
vector. Read more