Skip to main content

KeyBuilder

Struct KeyBuilder 

Source
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

Source

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());
Source

pub fn width_bits(&self) -> u64

Total bits appended so far.

Source

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());
Source

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

Source§

fn clone(&self) -> KeyBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>, U: Sized,

Casts 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 more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V