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
impl SparseVector
Sourcepub fn new(indices: Vec<u32>, values: Vec<f32>) -> Result<Self, SparseError>
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),indicesstrictly 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).
Sourcepub fn from_pairs(pairs: Vec<(u32, f32)>) -> Result<Self, SparseError>
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).
Sourcepub fn values(&self) -> &[f32]
pub fn values(&self) -> &[f32]
The weights, parallel to SparseVector::indices.
Trait Implementations§
Source§impl Clone for SparseVector
impl Clone for SparseVector
Source§fn clone(&self) -> SparseVector
fn clone(&self) -> SparseVector
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 SparseVector
impl Debug for SparseVector
Source§impl<'de> Deserialize<'de> for SparseVector
impl<'de> Deserialize<'de> for SparseVector
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for SparseVector
impl PartialEq for SparseVector
Source§fn eq(&self, other: &SparseVector) -> bool
fn eq(&self, other: &SparseVector) -> bool
self and other values to be equal, and is used by ==.