pub struct KeyBuilder { /* private fields */ }Expand description
Assembles one key by appending sub-keys at successive bit offsets.
This is the composition operation a tree needs: a node’s key is its own
local key followed by each child’s key, so
key(node) = local ++ key(c1) ++ key(c2) ++ .... Appending at a known bit
offset costs one pass over the pushed key’s limbs, so composing a tree is
linear in total width rather than quadratic as a multiply-based mixed-radix
composition would be.
The result equals what FlatIndexer::encode produces for the
concatenated multi-index, so a composed key and a directly encoded one are
interchangeable as map keys.
§Examples
use tensor4all_core::index_key::{FlatIndexer, KeyBuilder};
let local = FlatIndexer::try_new(&[3, 2]).unwrap();
let child = FlatIndexer::try_new(&[4]).unwrap();
let whole = FlatIndexer::try_new(&[3, 2, 4]).unwrap();
let mut builder = KeyBuilder::with_capacity_bits(whole.width_bits()).unwrap();
builder.push(&local.encode(&[2, 1]).unwrap()).unwrap();
builder.push(&child.encode(&[3]).unwrap()).unwrap();
assert_eq!(builder.finish(), whole.encode(&[2, 1, 3]).unwrap());Implementations§
Source§impl KeyBuilder
impl KeyBuilder
Sourcepub fn with_capacity_bits(width_bits: u64) -> Result<Self, IndexKeyError>
pub fn with_capacity_bits(width_bits: u64) -> Result<Self, IndexKeyError>
Creates a builder able to hold width_bits of appended sub-keys.
§Errors
Returns IndexKeyError::WidthOverflow when the width cannot be
allocated on this platform.
§Examples
use tensor4all_core::index_key::KeyBuilder;
assert!(KeyBuilder::with_capacity_bits(0).is_ok());
assert!(KeyBuilder::with_capacity_bits(4096).is_ok());Sourcepub fn width_bits(&self) -> u64
pub fn width_bits(&self) -> u64
Total bits appended so far.
Sourcepub fn push(&mut self, key: &IndexKey) -> Result<(), IndexKeyError>
pub fn push(&mut self, key: &IndexKey) -> Result<(), IndexKeyError>
Appends key at the current offset, advancing by the key’s own width.
The width comes from the key itself, so a sub-key can never be placed under a width that disagrees with its contents – which would overwrite the following field and silently break injectivity.
§Errors
Returns IndexKeyError::WidthOverflow when the append would exceed
the declared capacity, rather than truncating the key.
§Examples
use tensor4all_core::index_key::{FlatIndexer, KeyBuilder};
let indexer = FlatIndexer::try_new(&[2, 2]).unwrap();
let key = indexer.encode(&[1, 1]).unwrap();
let mut builder = KeyBuilder::with_capacity_bits(2).unwrap();
assert!(builder.push(&key).is_ok());
assert!(builder.push(&key).is_err());Sourcepub fn finish(self) -> IndexKey
pub fn finish(self) -> IndexKey
Produces the key for the bits actually appended.
The arm is selected from the appended width, not from the declared
capacity, so a composed key equals the same value from
FlatIndexer::encode even when the builder was given more capacity
than it ended up using.
§Examples
use tensor4all_core::index_key::{FlatIndexer, KeyBuilder};
let indexer = FlatIndexer::try_new(&[2, 2]).unwrap();
let key = indexer.encode(&[1, 0]).unwrap();
// Over-declared capacity does not change the composed key.
let mut tight = KeyBuilder::with_capacity_bits(2).unwrap();
tight.push(&key).unwrap();
let mut loose = KeyBuilder::with_capacity_bits(600).unwrap();
loose.push(&key).unwrap();
let composed = tight.finish();
assert_eq!(composed, loose.finish());
assert_eq!(composed, key);
let empty = KeyBuilder::with_capacity_bits(8).unwrap();
assert_eq!(empty.finish().width_bits(), 0);Trait Implementations§
Source§impl Clone for KeyBuilder
impl Clone for KeyBuilder
Source§fn clone(&self) -> KeyBuilder
fn clone(&self) -> KeyBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for KeyBuilder
impl RefUnwindSafe for KeyBuilder
impl Send for KeyBuilder
impl Sync for KeyBuilder
impl Unpin for KeyBuilder
impl UnsafeUnpin for KeyBuilder
impl UnwindSafe for KeyBuilder
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 moreSource§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<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