Skip to main content

Codebook

Struct Codebook 

Source
pub struct Codebook {
    pub version: u32,
    pub dimensionality: usize,
    pub basis_vectors: Vec<BasisVector>,
    pub semantic_markers: Vec<SparseVec>,
    pub statistics: CodebookStatistics,
    pub salt: Option<[u8; 32]>,
}
Expand description

The Codebook - acts as the private key for reconstruction

Fields§

§version: u32

Version for compatibility

§dimensionality: usize

Dimensionality of basis vectors

§basis_vectors: Vec<BasisVector>

The basis vectors forming the encoding dictionary Data is projected onto these bases

§semantic_markers: Vec<SparseVec>

Semantic marker vectors for outlier detection

§statistics: CodebookStatistics

Statistics for adaptive encoding

§salt: Option<[u8; 32]>

Cryptographic salt for key derivation (optional)

Implementations§

Source§

impl Codebook

Source

pub fn new(dimensionality: usize) -> Self

Create a new empty codebook

Source

pub fn with_salt(dimensionality: usize, salt: [u8; 32]) -> Self

Create a codebook with cryptographic salt for key derivation

Source

pub fn initialize_standard_basis(&mut self)

Initialize with common basis vectors for text/binary data

Source

pub fn initialize_byte_basis(&mut self)

Initialize with byte-level basis vectors (256 basis vectors for each byte value)

This creates a complete basis that can represent any byte data. Each byte value 0-255 gets its own basis vector.

Position basis vectors (64 vectors for positions 0-63) are also added by default. Use initialize_byte_basis_with_config to control this.

Source

pub fn initialize_byte_basis_with_config( &mut self, include_position_basis: bool, )

Initialize with byte-level basis vectors with optional position basis

§Arguments
  • include_position_basis - Whether to add position-aware basis vectors (64 vectors)
Source

pub fn train( &mut self, training_data: &[&[u8]], config: &CodebookTrainingConfig, ) -> usize

Train the codebook on representative data

This learns basis vectors by analyzing patterns in the training data. The algorithm:

  1. Chunk the data into blocks
  2. Find frequently occurring patterns (n-grams)
  3. Create basis vectors for the most common patterns
  4. Optionally add byte-level basis as fallback
§Arguments
  • training_data - Slice of training samples
  • config - Training configuration
§Returns

Number of basis vectors learned

§ID Allocation

Basis vector IDs are allocated in non-overlapping ranges:

  • Byte basis: 0-255
  • Position basis: 256-319
  • Learned patterns: 1000+
Source

pub fn train_from_files( &mut self, paths: &[&Path], config: &CodebookTrainingConfig, ) -> Result<usize>

Train codebook from files on disk

Convenience method that reads files and trains on their content.

Source

pub fn project(&self, data: &[u8]) -> ProjectionResult

Project data onto the codebook basis Returns coefficients, residual, and detected outliers

Source

pub fn project_with_config( &self, data: &[u8], config: &ProjectionConfig, ) -> ProjectionResult

Project data onto the codebook using custom configuration

Source

pub fn reconstruct( &self, projection: &ProjectionResult, expected_size: usize, ) -> Vec<u8>

Reconstruct original data from projection result

Trait Implementations§

Source§

impl Clone for Codebook

Source§

fn clone(&self) -> Codebook

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Codebook

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Codebook

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Codebook

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Codebook

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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
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
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.

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,