pub struct CachedVec<D, T, V, C = DefaultCache<D, T>>{ /* private fields */ }
Expand description
Wraps an NVec<D, T>
into a cached vector which maintains an internal cache.
Each time an element is requested, the vector first checks the cache:
- if the value is readily available in the cache, the vector returns it,
- otherwise, it computes its value, caches it for future use and returns it.
The cache is often a simple lookup or map, such as the HashMap. However, it might
be more advanced such as the least frequently used cache. Any internal structure
implementing the Cache
can be used.
The aim of the cached vector is to improve the execution time where computing an element is expensive. Consider for instance element (i, j) corresponds to the duration between two addresses i and j on a street network which requires running an algorithm to compute. Further assume that:
- pre-computing all elements is not justified as we will access only a small portion of the entire matrix, and
- we do not know ahead of time which indices that we will access.
In such scenarios, IntoCached
trait makes it very convenient to convert a functional
vector into a cached functional vector.
§Examples
use orx_v::*;
// assume an expensive api call to compute distance
fn api_call_to_get_distance(from: usize, to: usize) -> u64 {
match from > to {
true => (from - to) as u64,
false => (to - from) as u64,
}
}
let v2 = V.d2().fun(|[i, j]| api_call_to_get_distance(i, j)).into_cached();
assert_eq!(v2.cache_len(), 0);
// makes the api call; caches and returns the value
assert_eq!(v2.at([0, 3]), 3);
assert_eq!(v2.cache_len(), 1);
// does not repeat the api call; returns the value from the cache
assert_eq!(v2.at([0, 3]), 3);
Implementations§
Source§impl<D, T, V, C> CachedVec<D, T, V, C>
impl<D, T, V, C> CachedVec<D, T, V, C>
Sourcepub fn into_inner(self) -> (V, C)
pub fn into_inner(self) -> (V, C)
Destructs the cached vec and returns the tuple of the underlying NVec<D, T>
and cache.
Note that a new cached vector can be constructed by re-using the cache by
calling the into_cached_with
method on the vec.
Sourcepub fn clean_cache(&mut self)
pub fn clean_cache(&mut self)
Clears the internal cache of the cached vector; i.e., forgets all cached elements.
Trait Implementations§
Source§impl<T, V, C> NVec<D1, T> for CachedVec<D1, T, V, C>
impl<T, V, C> NVec<D1, T> for CachedVec<D1, T, V, C>
Source§fn at(&self, idx: impl IntoIdx<D1>) -> T
fn at(&self, idx: impl IntoIdx<D1>) -> T
idx
-th position of the vector. Read moreSource§fn child(&self, _: <D1 as Dim>::ChildIdx) -> impl NVec<<D1 as Dim>::PrevDim, T>
fn child(&self, _: <D1 as Dim>::ChildIdx) -> impl NVec<<D1 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 moreSource§impl<T, V, C> NVec<D2, T> for CachedVec<D2, T, V, C>
impl<T, V, C> NVec<D2, T> for CachedVec<D2, T, V, C>
Source§fn at(&self, idx: impl IntoIdx<D2>) -> T
fn at(&self, idx: impl IntoIdx<D2>) -> T
idx
-th position of the vector. Read moreSource§fn child(&self, i: <D2 as Dim>::ChildIdx) -> impl NVec<<D2 as Dim>::PrevDim, T>
fn child(&self, i: <D2 as Dim>::ChildIdx) -> impl NVec<<D2 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 moreSource§impl<T, V, C> NVec<D3, T> for CachedVec<D3, T, V, C>
impl<T, V, C> NVec<D3, T> for CachedVec<D3, T, V, C>
Source§fn at(&self, idx: impl IntoIdx<D3>) -> T
fn at(&self, idx: impl IntoIdx<D3>) -> T
idx
-th position of the vector. Read moreSource§fn child(&self, i: <D3 as Dim>::ChildIdx) -> impl NVec<<D3 as Dim>::PrevDim, T>
fn child(&self, i: <D3 as Dim>::ChildIdx) -> impl NVec<<D3 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 moreSource§impl<T, V, C> NVec<D4, T> for CachedVec<D4, T, V, C>
impl<T, V, C> NVec<D4, T> for CachedVec<D4, T, V, C>
Source§fn at(&self, idx: impl IntoIdx<D4>) -> T
fn at(&self, idx: impl IntoIdx<D4>) -> T
idx
-th position of the vector. Read moreSource§fn child(&self, i: <D4 as Dim>::ChildIdx) -> impl NVec<<D4 as Dim>::PrevDim, T>
fn child(&self, i: <D4 as Dim>::ChildIdx) -> impl NVec<<D4 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 moreAuto Trait Implementations§
impl<D, T, V, C = HashMap<<D as Dim>::Idx, T>> !Freeze for CachedVec<D, T, V, C>
impl<D, T, V, C = HashMap<<D as Dim>::Idx, T>> !RefUnwindSafe for CachedVec<D, T, V, C>
impl<D, T, V, C> Send for CachedVec<D, T, V, C>
impl<D, T, V, C = HashMap<<D as Dim>::Idx, T>> !Sync for CachedVec<D, T, V, C>
impl<D, T, V, C> Unpin for CachedVec<D, T, V, C>
impl<D, T, V, C> UnwindSafe for CachedVec<D, T, V, 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, 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