pub struct DenseElementBudget { /* private fields */ }Expand description
Element-count budget for one dense materialization.
A budget is checked against the element count of a dense object (or the total of several) before the allocation. Counts equal to the limit pass; only counts above it fail.
§Examples
use tensor4all_core::{DenseElementBudget, DEFAULT_MAX_DENSE_ELEMENTS};
assert_eq!(
DenseElementBudget::default().max_elements(),
DEFAULT_MAX_DENSE_ELEMENTS
);
assert_eq!(DenseElementBudget::unbounded().max_elements(), usize::MAX);Implementations§
Source§impl DenseElementBudget
impl DenseElementBudget
Sourcepub const fn new(max_elements: usize) -> Self
pub const fn new(max_elements: usize) -> Self
Create a budget that admits at most max_elements elements.
new(0) admits only empty objects (an extent of 0 or no objects at
all).
Sourcepub const fn unbounded() -> Self
pub const fn unbounded() -> Self
Create a budget that admits every object whose element count fits in
usize.
Sourcepub const fn max_elements(self) -> usize
pub const fn max_elements(self) -> usize
The maximum element count this budget admits.
Sourcepub fn check_dims(
self,
what: &'static str,
dims: &[usize],
) -> Result<usize, DenseBudgetError>
pub fn check_dims( self, what: &'static str, dims: &[usize], ) -> Result<usize, DenseBudgetError>
Element count of the dense object with dims, checked against the
budget.
dims is the extent of each axis; an empty slice is a scalar (1
element) and any extent of 0 is an empty object (0 elements).
§Errors
Returns DenseBudgetError::Overflow when the element count does not
fit in usize, and DenseBudgetError::Exceeded when it is above
Self::max_elements.
§Examples
use tensor4all_core::{DenseBudgetError, DenseElementBudget};
let budget = DenseElementBudget::new(4);
assert_eq!(budget.check_dims("density", &[2, 2]).unwrap(), 4);
assert_eq!(budget.check_dims("scalar", &[]).unwrap(), 1);
assert_eq!(budget.check_dims("empty", &[0, 9]).unwrap(), 0);
assert_eq!(
budget.check_dims("density", &[2, 3]),
Err(DenseBudgetError::Exceeded {
what: "density",
elements: 6,
max_elements: 4,
})
);Sourcepub fn check_total<'a>(
self,
what: &'static str,
objects: impl IntoIterator<Item = &'a [usize]>,
) -> Result<usize, DenseBudgetError>
pub fn check_total<'a>( self, what: &'static str, objects: impl IntoIterator<Item = &'a [usize]>, ) -> Result<usize, DenseBudgetError>
Total element count of several dense objects, checked against the budget once.
Use this when one operation allocates a list of dense objects whose sum, not any single object, is the cost.
§Errors
Returns DenseBudgetError::Overflow when the total does not fit in
usize, and DenseBudgetError::Exceeded when it is above
Self::max_elements.
§Examples
use tensor4all_core::DenseElementBudget;
let budget = DenseElementBudget::new(8);
let sites = [[1usize, 4, 1].as_slice(), [1, 4, 1].as_slice()];
assert_eq!(budget.check_total("mpo", sites).unwrap(), 8);
assert_eq!(
budget.check_total("mpo", std::iter::once(sites[0])).unwrap(),
4
);
assert!(DenseElementBudget::new(7).check_total("mpo", sites).is_err());Trait Implementations§
Source§impl Clone for DenseElementBudget
impl Clone for DenseElementBudget
impl Copy for DenseElementBudget
Source§impl Debug for DenseElementBudget
impl Debug for DenseElementBudget
Source§impl Default for DenseElementBudget
impl Default for DenseElementBudget
impl Eq for DenseElementBudget
Source§impl Hash for DenseElementBudget
impl Hash for DenseElementBudget
Source§impl PartialEq for DenseElementBudget
impl PartialEq for DenseElementBudget
impl StructuralPartialEq for DenseElementBudget
Auto Trait Implementations§
impl Freeze for DenseElementBudget
impl RefUnwindSafe for DenseElementBudget
impl Send for DenseElementBudget
impl Sync for DenseElementBudget
impl Unpin for DenseElementBudget
impl UnsafeUnpin for DenseElementBudget
impl UnwindSafe for DenseElementBudget
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
U: Sized,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreimpl<T> Boilerplate for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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