Skip to main content

SparseVector

Struct SparseVector 

Source
pub struct SparseVector { /* private fields */ }
Expand description

A learned-sparse vector: {term_id -> weight} over a high-cardinality vocabulary (e.g. SPLADE-v3 / BGE-M3 sparse head over a ~30k-term BERT vocab).

Stored as two parallel arrays — indices (term ids) and values (weights) — with indices kept strictly ascending so that the canonical scoring kernel (crate::ops::sparse_dot) is a linear merge-join. The invariant is enforced at construction by SparseVector::new.

The in-memory and binary forms keep weights as lossless f32. Weight quantization (8-bit, etc.) is a storage-engine concern applied at the index postings boundary, never in this type, so a brute-force scorer over SparseVector is always an exact ground truth.

Implementations§

Source§

impl SparseVector

Source

pub fn new(indices: Vec<u32>, values: Vec<f32>) -> Result<Self, SparseError>

Construct a sparse vector, validating its invariants:

  • indices.len() == values.len() (SV-1),
  • indices strictly ascending — sorted and unique (SV-2),
  • every weight finite — no NaN / ±inf (SV-3).

Use SparseVector::from_pairs for unsorted input with duplicate term ids (the typical embedding-producer shape).

Source

pub fn from_pairs(pairs: Vec<(u32, f32)>) -> Result<Self, SparseError>

Build from arbitrary (term_id, weight) pairs: sorts by term id and sums the weights of duplicate term ids, then validates. This is the ingestion-friendly constructor for embedding-model output and for dict[int, float] from the Python surface.

Non-finite input weights are still rejected (after summation).

Source

pub fn indices(&self) -> &[u32]

The (strictly ascending) term ids.

Source

pub fn values(&self) -> &[f32]

The weights, parallel to SparseVector::indices.

Source

pub fn len(&self) -> usize

Number of non-zero terms.

Source

pub fn is_empty(&self) -> bool

Whether the vector has no non-zero terms.

Source

pub fn iter(&self) -> impl Iterator<Item = (u32, f32)> + '_

Iterate over (term_id, weight) pairs in ascending term-id order.

Source

pub fn into_parts(self) -> (Vec<u32>, Vec<f32>)

Consume the vector into its parallel arrays.

Trait Implementations§

Source§

impl Clone for SparseVector

Source§

fn clone(&self) -> SparseVector

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 SparseVector

Source§

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

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

impl<'de> Deserialize<'de> for SparseVector

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 PartialEq for SparseVector

Source§

fn eq(&self, other: &SparseVector) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for SparseVector

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

impl StructuralPartialEq for SparseVector

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> 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.