pub struct RandomSlices { /* private fields */ }Expand description
Slice table indexed by u64 hash values.
A RandomSlices instance owns three parallel arrays: the
ascending interval lower bounds, the per-interval sizes
(kept for diagnostics), and the per-interval claimant names.
All builders maintain the parallel-array invariant; do not
construct this struct by hand.
Implementations§
Source§impl RandomSlices
impl RandomSlices
Sourcepub fn from_uniform(claimants: &[&str]) -> Result<Self, RandomSlicesError>
pub fn from_uniform(claimants: &[&str]) -> Result<Self, RandomSlicesError>
Build an equal-sized partition for claimants. Each
claimant gets an interval of u64::MAX / N values; the
last claimant absorbs the rounding remainder so coverage
is exact.
§Errors
Returns RandomSlicesError::EmptyClaimants when the
list is empty and
RandomSlicesError::DuplicateClaimant when two entries
share a name.
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
let s = RandomSlices::from_uniform(&["a", "b", "c"]).unwrap();
assert_eq!(s.len(), 3);Sourcepub fn from_weights(
weights: &[(String, f64)],
decimal_digits: usize,
) -> Result<Self, RandomSlicesError>
pub fn from_weights( weights: &[(String, f64)], decimal_digits: usize, ) -> Result<Self, RandomSlicesError>
Build a weighted partition. Each weight is rounded to
decimal_digits places before normalising; the sum is
then translated into per-claimant u64 sizes by
floor(u64::MAX * w / sum). The last claimant absorbs
the remainder so coverage is exact.
§Errors
Returns RandomSlicesError::EmptyClaimants,
RandomSlicesError::DuplicateClaimant, or
RandomSlicesError::InvalidWeight (for non-finite,
negative, or all-zero rounded weights).
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
let s = RandomSlices::from_weights(
&[("a".to_string(), 1.0), ("b".to_string(), 2.0)],
2,
).unwrap();
assert_eq!(s.len(), 2);Sourcepub fn from_sizes(sizes: &[(String, u64)]) -> Result<Self, RandomSlicesError>
pub fn from_sizes(sizes: &[(String, u64)]) -> Result<Self, RandomSlicesError>
Build a partition from explicit (name, size) pairs.
The supplied sizes are taken at face value; the only
transformation is bumping the tail interval up by the
remainder when the sum is below u64::MAX. Sums above
u64::MAX are rejected with
RandomSlicesError::OversizedSizeSum.
§Errors
Returns RandomSlicesError::EmptyClaimants,
RandomSlicesError::DuplicateClaimant,
RandomSlicesError::ZeroInterval, or
RandomSlicesError::OversizedSizeSum.
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
let s = RandomSlices::from_sizes(&[
("a".into(), 100),
("b".into(), 200),
]).unwrap();
assert_eq!(s.len(), 2);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of claimants / intervals in the table.
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
assert_eq!(RandomSlices::from_uniform(&["a", "b"]).unwrap().len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
True when the table holds no claimants. The builders
reject empty inputs, so this can only be observed via
the (private) raw constructor; included for completeness
behind clippy’s len lint.
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
assert!(!RandomSlices::from_uniform(&["a"]).unwrap().is_empty());Sourcepub fn claimant_for(&self, hash: u64) -> Option<&str>
pub fn claimant_for(&self, hash: u64) -> Option<&str>
Look up the claimant that owns hash.
Returns the borrowed claimant name. Returns None only
when the table is empty (which the builders prevent).
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
let s = RandomSlices::from_uniform(&["a", "b"]).unwrap();
assert!(s.claimant_for(0).is_some());Sourcepub fn index_for(&self, hash: u64) -> Option<usize>
pub fn index_for(&self, hash: u64) -> Option<usize>
Look up the index of the claimant that owns hash.
Returns None only when the table is empty.
Sourcepub fn claimants(&self) -> &[String]
pub fn claimants(&self) -> &[String]
Borrow the claimant list (parallel to
Self::lower_bounds and Self::interval_sizes).
Sourcepub fn lower_bounds(&self) -> &[u64]
pub fn lower_bounds(&self) -> &[u64]
Borrow the lower-bound array.
Sourcepub fn interval_sizes(&self) -> &[u64]
pub fn interval_sizes(&self) -> &[u64]
Borrow the per-interval size array.
Sourcepub fn intervals(&self) -> impl Iterator<Item = (&str, u64, u64)> + '_
pub fn intervals(&self) -> impl Iterator<Item = (&str, u64, u64)> + '_
Iterate (claimant, lower_bound, size) for diagnostics.
§Examples
use dynomite::hashkit::random_slicing::RandomSlices;
let s = RandomSlices::from_uniform(&["a", "b"]).unwrap();
let n = s.intervals().count();
assert_eq!(n, 2);Trait Implementations§
Source§impl Clone for RandomSlices
impl Clone for RandomSlices
Source§fn clone(&self) -> RandomSlices
fn clone(&self) -> RandomSlices
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RandomSlices
impl Debug for RandomSlices
Source§impl PartialEq for RandomSlices
impl PartialEq for RandomSlices
Source§fn eq(&self, other: &RandomSlices) -> bool
fn eq(&self, other: &RandomSlices) -> bool
self and other values to be equal, and is used by ==.impl Eq for RandomSlices
impl StructuralPartialEq for RandomSlices
Auto Trait Implementations§
impl Freeze for RandomSlices
impl RefUnwindSafe for RandomSlices
impl Send for RandomSlices
impl Sync for RandomSlices
impl Unpin for RandomSlices
impl UnsafeUnpin for RandomSlices
impl UnwindSafe for RandomSlices
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.