Skip to main content

hermes_core/structures/postings/sparse/
config.rs

1//! Configuration types for sparse vector posting lists
2
3use serde::{Deserialize, Serialize};
4
5/// Sparse vector index format
6///
7/// Determines the on-disk layout and query execution strategy:
8/// - **MaxScore**: Per-dimension variable-size blocks (DAAT — document-at-a-time).
9///   Default, optimal for general sparse retrieval with block-max pruning.
10/// - **Bmp**: Fixed doc_id range blocks (BAAT — block-at-a-time).
11///   Based on Mallia, Suel & Tonellotto (SIGIR 2024). Divides the document
12///   space into fixed-size blocks and processes them in decreasing upper-bound
13///   order, enabling aggressive early termination.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
15pub enum SparseFormat {
16    /// Per-dimension variable-size blocks (existing format, DAAT MaxScore)
17    #[default]
18    MaxScore,
19    /// Fixed doc_id range blocks (BMP, BAAT block-at-a-time)
20    Bmp,
21}
22
23impl SparseFormat {
24    fn is_default(&self) -> bool {
25        *self == Self::MaxScore
26    }
27}
28
29/// Size of the index (term/dimension ID) in sparse vectors
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
31#[repr(u8)]
32pub enum IndexSize {
33    /// 16-bit index (0-65535), ideal for SPLADE vocabularies
34    U16 = 0,
35    /// 32-bit index (0-4B), for large vocabularies
36    #[default]
37    U32 = 1,
38}
39
40impl IndexSize {
41    /// Bytes per index
42    pub fn bytes(&self) -> usize {
43        match self {
44            IndexSize::U16 => 2,
45            IndexSize::U32 => 4,
46        }
47    }
48
49    /// Maximum value representable
50    pub fn max_value(&self) -> u32 {
51        match self {
52            IndexSize::U16 => u16::MAX as u32,
53            IndexSize::U32 => u32::MAX,
54        }
55    }
56
57    pub(crate) fn from_u8(v: u8) -> Option<Self> {
58        match v {
59            0 => Some(IndexSize::U16),
60            1 => Some(IndexSize::U32),
61            _ => None,
62        }
63    }
64}
65
66/// Quantization format for sparse vector weights
67///
68/// Research-validated compression/effectiveness trade-offs (Pati, 2025):
69/// - **UInt8**: 4x compression, ~1-2% nDCG@10 loss (RECOMMENDED for production)
70/// - **Float16**: 2x compression, <1% nDCG@10 loss
71/// - **Float32**: No compression, baseline effectiveness
72/// - **UInt4**: 8x compression, ~3-5% nDCG@10 loss (experimental)
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
74#[repr(u8)]
75pub enum WeightQuantization {
76    /// Full 32-bit float precision
77    #[default]
78    Float32 = 0,
79    /// 16-bit float (half precision) - 2x compression, <1% effectiveness loss
80    Float16 = 1,
81    /// 8-bit unsigned integer with scale factor - 4x compression, ~1-2% effectiveness loss (RECOMMENDED)
82    UInt8 = 2,
83    /// 4-bit unsigned integer with scale factor (packed, 2 per byte) - 8x compression, ~3-5% effectiveness loss
84    UInt4 = 3,
85}
86
87impl WeightQuantization {
88    /// Bytes per weight (approximate for UInt4)
89    pub fn bytes_per_weight(&self) -> f32 {
90        match self {
91            WeightQuantization::Float32 => 4.0,
92            WeightQuantization::Float16 => 2.0,
93            WeightQuantization::UInt8 => 1.0,
94            WeightQuantization::UInt4 => 0.5,
95        }
96    }
97
98    pub(crate) fn from_u8(v: u8) -> Option<Self> {
99        match v {
100            0 => Some(WeightQuantization::Float32),
101            1 => Some(WeightQuantization::Float16),
102            2 => Some(WeightQuantization::UInt8),
103            3 => Some(WeightQuantization::UInt4),
104            _ => None,
105        }
106    }
107}
108
109/// Query-time weighting strategy for sparse vector queries
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
111#[serde(rename_all = "snake_case")]
112pub enum QueryWeighting {
113    /// All terms get weight 1.0
114    #[default]
115    One,
116    /// Terms weighted by IDF (inverse document frequency) from global index statistics
117    /// Uses ln(N/df) where N = total docs, df = docs containing dimension
118    Idf,
119    /// Terms weighted by pre-computed IDF from model's idf.json file
120    /// Loaded from HuggingFace model repo. No fallback to global stats.
121    IdfFile,
122}
123
124/// Query-time configuration for sparse vectors
125///
126/// Research-validated query optimization strategies:
127/// - **weight_threshold (0.01-0.05)**: Drop query dimensions with weight below threshold
128///   - Filters low-IDF tokens that add latency without improving relevance
129/// - **max_query_dims (10-20)**: Process only top-k dimensions by weight
130///   - 30-50% latency reduction with <2% nDCG loss (Qiao et al., 2023)
131/// - **heap_factor (0.8)**: Skip blocks with low max score contribution
132///   - ~20% speedup with minor recall loss (SEISMIC-style)
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
134pub struct SparseQueryConfig {
135    /// HuggingFace tokenizer path/name for query-time tokenization
136    /// Example: "Alibaba-NLP/gte-Qwen2-1.5B-instruct"
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    pub tokenizer: Option<String>,
139    /// Weighting strategy for tokenized query terms
140    #[serde(default)]
141    pub weighting: QueryWeighting,
142    /// Heap factor for approximate search (SEISMIC-style optimization)
143    /// A block is skipped if its max possible score < heap_factor * threshold
144    ///
145    /// Research recommendation:
146    /// - 1.0 = exact search (default)
147    /// - 0.8 = approximate, ~20% faster with minor recall loss (RECOMMENDED for production)
148    /// - 0.5 = very approximate, much faster but higher recall loss
149    #[serde(default = "default_heap_factor")]
150    pub heap_factor: f32,
151    /// Minimum weight for query dimensions (query-time pruning)
152    /// Dimensions with abs(weight) below this threshold are dropped before search.
153    /// Useful for filtering low-IDF tokens that add latency without improving relevance.
154    ///
155    /// - 0.0 = no filtering (default)
156    /// - 0.01-0.05 = recommended for SPLADE/learned sparse models
157    #[serde(default)]
158    pub weight_threshold: f32,
159    /// Maximum number of query dimensions to process (query pruning)
160    /// Processes only the top-k dimensions by weight
161    ///
162    /// Research recommendation (Multiple papers 2022-2024):
163    /// - None = process all dimensions (default, exact)
164    /// - Some(10-20) = process top 10-20 dimensions only (RECOMMENDED for SPLADE)
165    ///   - 30-50% latency reduction
166    ///   - <2% nDCG@10 loss
167    #[serde(default, skip_serializing_if = "Option::is_none")]
168    pub max_query_dims: Option<usize>,
169    /// Fraction of query dimensions to keep (0.0-1.0), same semantics as
170    /// indexing-time `pruning`: sort by abs(weight) descending,
171    /// keep top fraction. None or 1.0 = no pruning.
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub pruning: Option<f32>,
174    /// Minimum number of query dimensions before pruning and weight_threshold
175    /// filtering are applied. Protects short queries from losing most signal.
176    ///
177    /// Default: 4. Set to 0 to always apply pruning/filtering.
178    #[serde(default = "default_min_terms")]
179    pub min_query_dims: usize,
180}
181
182fn default_heap_factor() -> f32 {
183    1.0
184}
185
186impl Default for SparseQueryConfig {
187    fn default() -> Self {
188        Self {
189            tokenizer: None,
190            weighting: QueryWeighting::One,
191            heap_factor: 1.0,
192            weight_threshold: 0.0,
193            max_query_dims: None,
194            pruning: None,
195            min_query_dims: 4,
196        }
197    }
198}
199
200/// Configuration for sparse vector storage
201///
202/// Research-validated optimizations for learned sparse retrieval (SPLADE, uniCOIL, etc.):
203/// - **Weight threshold (0.01-0.05)**: Removes ~30-50% of postings with minimal nDCG impact
204/// - **Posting list pruning (0.1)**: Keeps top 10% per dimension, 50-70% index reduction, <1% nDCG loss
205/// - **Query pruning (top 10-20 dims)**: 30-50% latency reduction, <2% nDCG loss
206/// - **UInt8 quantization**: 4x compression, 1-2% nDCG loss (optimal trade-off)
207#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
208pub struct SparseVectorConfig {
209    /// Index format: MaxScore (DAAT) or BMP (BAAT)
210    #[serde(default, skip_serializing_if = "SparseFormat::is_default")]
211    pub format: SparseFormat,
212    /// Size of dimension/term indices
213    pub index_size: IndexSize,
214    /// Quantization for weights (see WeightQuantization docs for trade-offs)
215    pub weight_quantization: WeightQuantization,
216    /// Minimum weight threshold - weights below this value are not indexed
217    ///
218    /// Research recommendation (Guo et al., 2022; SPLADE v2):
219    /// - 0.01-0.05 for SPLADE models removes ~30-50% of postings
220    /// - Minimal impact on nDCG@10 (<1% loss)
221    /// - Major reduction in index size and query latency
222    #[serde(default)]
223    pub weight_threshold: f32,
224    /// Block size for posting lists (must be power of 2, default 128 for SIMD)
225    /// Larger blocks = better compression, smaller blocks = faster seeks.
226    /// Used by MaxScore format only.
227    #[serde(default = "default_block_size")]
228    pub block_size: usize,
229    /// BMP block size: number of consecutive doc_ids per block (must be power of 2).
230    /// Default 64. Only used when format = Bmp.
231    /// Smaller = better pruning granularity, larger = less overhead.
232    #[serde(default = "default_bmp_block_size")]
233    pub bmp_block_size: u32,
234    /// Maximum BMP grid memory in bytes. If the grid (num_dims × num_blocks)
235    /// would exceed this, bmp_block_size is automatically increased to cap memory.
236    /// Default: 256MB. Set to 0 to disable the cap.
237    #[serde(default = "default_max_bmp_grid_bytes")]
238    pub max_bmp_grid_bytes: u64,
239    /// BMP superblock size: number of consecutive blocks grouped for hierarchical
240    /// pruning (Carlson et al., SIGIR 2025). Must be power of 2.
241    /// Default 64. Set to 0 to disable superblock pruning (flat BMP scoring).
242    /// Only used when format = Bmp.
243    #[serde(default = "default_bmp_superblock_size")]
244    pub bmp_superblock_size: u32,
245    /// Static pruning: fraction of postings to keep per inverted list (SEISMIC-style)
246    /// Lists are sorted by weight descending and truncated to top fraction.
247    ///
248    /// Research recommendation (SPLADE v2, Formal et al., 2021):
249    /// - None = keep all postings (default, exact)
250    /// - Some(0.1) = keep top 10% of postings per dimension
251    ///   - 50-70% index size reduction
252    ///   - <1% nDCG@10 loss
253    ///   - Exploits "concentration of importance" in learned representations
254    ///
255    /// Applied only during initial segment build, not during merge.
256    #[serde(default, skip_serializing_if = "Option::is_none")]
257    pub pruning: Option<f32>,
258    /// Query-time configuration (tokenizer, weighting)
259    #[serde(default, skip_serializing_if = "Option::is_none")]
260    pub query_config: Option<SparseQueryConfig>,
261    /// Fixed vocabulary size (number of dimensions) for BMP format.
262    ///
263    /// When set, all BMP segments use the same grid dimensions (rows = dims),
264    /// enabling zero-copy block-copy merge. The grid is indexed by dim_id directly
265    /// (no dim_ids Section C needed).
266    ///
267    /// Required for BMP V12 format. Typical values:
268    /// - SPLADE/BERT: 30522 or 105879 (WordPiece / Unigram vocabulary)
269    /// - uniCOIL: 30522
270    /// - Custom models: set to vocabulary size
271    ///
272    /// If None, BMP builder derives dims from observed data (V10 behavior).
273    #[serde(default, skip_serializing_if = "Option::is_none")]
274    pub dims: Option<u32>,
275    /// Fixed max weight scale for BMP format.
276    ///
277    /// When set, all BMP segments use the same quantization scale
278    /// (`max_weight_scale = max_weight`), eliminating rescaling during merge.
279    ///
280    /// For SPLADE models: 5.0 (covers typical weight range 0-5).
281    /// If None, BMP builder derives scale from data (V10 behavior).
282    #[serde(default, skip_serializing_if = "Option::is_none")]
283    pub max_weight: Option<f32>,
284    /// Minimum number of postings in a dimension before pruning and
285    /// weight_threshold filtering are applied. Protects dimensions with
286    /// very few postings from losing most of their signal.
287    ///
288    /// Default: 4. Set to 0 to always apply pruning/filtering.
289    #[serde(default = "default_min_terms")]
290    pub min_terms: usize,
291}
292
293fn default_block_size() -> usize {
294    128
295}
296
297fn default_bmp_block_size() -> u32 {
298    64
299}
300
301fn default_max_bmp_grid_bytes() -> u64 {
302    0 // disabled by default — masks eliminate DRAM stalls during scoring
303}
304
305fn default_bmp_superblock_size() -> u32 {
306    64
307}
308
309fn default_min_terms() -> usize {
310    4
311}
312
313impl Default for SparseVectorConfig {
314    fn default() -> Self {
315        Self {
316            format: SparseFormat::MaxScore,
317            index_size: IndexSize::U32,
318            weight_quantization: WeightQuantization::Float32,
319            weight_threshold: 0.0,
320            block_size: 128,
321            bmp_block_size: 64,
322            max_bmp_grid_bytes: 0,
323            bmp_superblock_size: 64,
324            pruning: None,
325            query_config: None,
326
327            dims: None,
328            max_weight: None,
329            min_terms: 4,
330        }
331    }
332}
333
334impl SparseVectorConfig {
335    /// SPLADE-optimized config with research-validated defaults
336    ///
337    /// Optimized for SPLADE, uniCOIL, and similar learned sparse retrieval models.
338    /// Based on research findings from:
339    /// - Pati (2025): UInt8 quantization = 4x compression, 1-2% nDCG loss
340    /// - Formal et al. (2021): SPLADE v2 posting list pruning
341    /// - Qiao et al. (2023): Query dimension pruning and approximate search
342    /// - Guo et al. (2022): Weight thresholding for efficiency
343    ///
344    /// Expected performance vs. full precision baseline:
345    /// - Index size: ~15-25% of original (combined effect of all optimizations)
346    /// - Query latency: 40-60% faster
347    /// - Effectiveness: 2-4% nDCG@10 loss (typically acceptable for production)
348    ///
349    /// Vocabulary: ~30K dimensions (fits in u16)
350    pub fn splade() -> Self {
351        Self {
352            format: SparseFormat::MaxScore,
353            index_size: IndexSize::U16,
354            weight_quantization: WeightQuantization::UInt8,
355            weight_threshold: 0.01, // Remove ~30-50% of low-weight postings
356            block_size: 128,
357            bmp_block_size: 64,
358            max_bmp_grid_bytes: 0,
359            bmp_superblock_size: 64,
360            pruning: Some(0.1), // Keep top 10% per dimension
361            query_config: Some(SparseQueryConfig {
362                tokenizer: None,
363                weighting: QueryWeighting::One,
364                heap_factor: 0.8,         // 20% faster approximate search
365                weight_threshold: 0.01,   // Drop low-IDF query tokens
366                max_query_dims: Some(20), // Process top 20 query dimensions
367                pruning: Some(0.1),       // Keep top 10% of query dims
368                min_query_dims: 4,
369            }),
370
371            dims: None,
372            max_weight: None,
373            min_terms: 4,
374        }
375    }
376
377    /// SPLADE-optimized config with BMP (Block-Max Pruning) format
378    ///
379    /// Same optimization settings as `splade()` but uses the BMP block-at-a-time
380    /// format (Mallia, Suel & Tonellotto, SIGIR 2024) instead of MaxScore.
381    /// BMP divides the document space into fixed-size blocks and processes them
382    /// in decreasing upper-bound order, enabling aggressive early termination.
383    pub fn splade_bmp() -> Self {
384        Self {
385            format: SparseFormat::Bmp,
386            index_size: IndexSize::U16,
387            weight_quantization: WeightQuantization::UInt8,
388            weight_threshold: 0.01,
389            block_size: 128,
390            bmp_block_size: 64,
391            max_bmp_grid_bytes: 0,
392            bmp_superblock_size: 64,
393            pruning: Some(0.1),
394            query_config: Some(SparseQueryConfig {
395                tokenizer: None,
396                weighting: QueryWeighting::One,
397                heap_factor: 0.8,
398                weight_threshold: 0.01,
399                max_query_dims: Some(20),
400                pruning: Some(0.1),
401                min_query_dims: 4,
402            }),
403
404            dims: Some(105879),
405            max_weight: Some(5.0),
406            min_terms: 4,
407        }
408    }
409
410    /// Compact config: Maximum compression (experimental)
411    ///
412    /// Uses aggressive UInt4 quantization for smallest possible index size.
413    /// Expected trade-offs:
414    /// - Index size: ~10-15% of Float32 baseline
415    /// - Effectiveness: ~3-5% nDCG@10 loss
416    ///
417    /// Recommended for: Memory-constrained environments, cache-heavy workloads
418    pub fn compact() -> Self {
419        Self {
420            format: SparseFormat::MaxScore,
421            index_size: IndexSize::U16,
422            weight_quantization: WeightQuantization::UInt4,
423            weight_threshold: 0.02, // Slightly higher threshold for UInt4
424            block_size: 128,
425            bmp_block_size: 64,
426            max_bmp_grid_bytes: 0,
427            bmp_superblock_size: 64,
428            pruning: Some(0.15), // Keep top 15% per dimension
429            query_config: Some(SparseQueryConfig {
430                tokenizer: None,
431                weighting: QueryWeighting::One,
432                heap_factor: 0.7,         // More aggressive approximate search
433                weight_threshold: 0.02,   // Drop low-IDF query tokens
434                max_query_dims: Some(15), // Fewer query dimensions
435                pruning: Some(0.15),      // Keep top 15% of query dims
436                min_query_dims: 4,
437            }),
438
439            dims: None,
440            max_weight: None,
441            min_terms: 4,
442        }
443    }
444
445    /// Full precision config: No compression, baseline effectiveness
446    ///
447    /// Use for: Research baselines, when effectiveness is critical
448    pub fn full_precision() -> Self {
449        Self {
450            format: SparseFormat::MaxScore,
451            index_size: IndexSize::U32,
452            weight_quantization: WeightQuantization::Float32,
453            weight_threshold: 0.0,
454            block_size: 128,
455            bmp_block_size: 64,
456            max_bmp_grid_bytes: 0,
457            bmp_superblock_size: 64,
458            pruning: None,
459            query_config: None,
460
461            dims: None,
462            max_weight: None,
463            min_terms: 4,
464        }
465    }
466
467    /// Conservative config: Mild optimizations, minimal effectiveness loss
468    ///
469    /// Balances compression and effectiveness with conservative defaults.
470    /// Expected trade-offs:
471    /// - Index size: ~40-50% of Float32 baseline
472    /// - Query latency: ~20-30% faster
473    /// - Effectiveness: <1% nDCG@10 loss
474    ///
475    /// Recommended for: Production deployments prioritizing effectiveness
476    pub fn conservative() -> Self {
477        Self {
478            format: SparseFormat::MaxScore,
479            index_size: IndexSize::U32,
480            weight_quantization: WeightQuantization::Float16,
481            weight_threshold: 0.005, // Minimal pruning
482            block_size: 128,
483            bmp_block_size: 64,
484            max_bmp_grid_bytes: 0,
485            bmp_superblock_size: 64,
486            pruning: None, // No posting list pruning
487            query_config: Some(SparseQueryConfig {
488                tokenizer: None,
489                weighting: QueryWeighting::One,
490                heap_factor: 0.9,         // Nearly exact search
491                weight_threshold: 0.005,  // Minimal query pruning
492                max_query_dims: Some(50), // Process more dimensions
493                pruning: None,            // No fraction-based pruning
494                min_query_dims: 4,
495            }),
496
497            dims: None,
498            max_weight: None,
499            min_terms: 4,
500        }
501    }
502
503    /// Set weight threshold (builder pattern)
504    pub fn with_weight_threshold(mut self, threshold: f32) -> Self {
505        self.weight_threshold = threshold;
506        self
507    }
508
509    /// Set posting list pruning fraction (builder pattern)
510    /// e.g., 0.1 = keep top 10% of postings per dimension
511    pub fn with_pruning(mut self, fraction: f32) -> Self {
512        self.pruning = Some(fraction.clamp(0.0, 1.0));
513        self
514    }
515
516    /// Bytes per entry (index + weight)
517    pub fn bytes_per_entry(&self) -> f32 {
518        self.index_size.bytes() as f32 + self.weight_quantization.bytes_per_weight()
519    }
520
521    /// Serialize config to a single byte.
522    ///
523    /// Layout: bits 7-4 = IndexSize, bit 3 = format (0=MaxScore, 1=BMP), bits 2-0 = WeightQuantization
524    pub fn to_byte(&self) -> u8 {
525        let format_bit = if self.format == SparseFormat::Bmp {
526            0x08
527        } else {
528            0
529        };
530        ((self.index_size as u8) << 4) | format_bit | (self.weight_quantization as u8)
531    }
532
533    /// Deserialize config from a single byte.
534    ///
535    /// Note: weight_threshold, block_size, bmp_block_size, and query_config are not
536    /// serialized in the byte — they come from the schema.
537    pub fn from_byte(b: u8) -> Option<Self> {
538        let index_size = IndexSize::from_u8((b >> 4) & 0x03)?;
539        let format = if b & 0x08 != 0 {
540            SparseFormat::Bmp
541        } else {
542            SparseFormat::MaxScore
543        };
544        let weight_quantization = WeightQuantization::from_u8(b & 0x07)?;
545        Some(Self {
546            format,
547            index_size,
548            weight_quantization,
549            weight_threshold: 0.0,
550            block_size: 128,
551            bmp_block_size: 64,
552            max_bmp_grid_bytes: 0,
553            bmp_superblock_size: 64,
554            pruning: None,
555            query_config: None,
556
557            dims: None,
558            max_weight: None,
559            min_terms: 4,
560        })
561    }
562
563    /// Set block size (builder pattern)
564    /// Must be power of 2, recommended: 64, 128, 256
565    pub fn with_block_size(mut self, size: usize) -> Self {
566        self.block_size = size.next_power_of_two();
567        self
568    }
569
570    /// Set query configuration (builder pattern)
571    pub fn with_query_config(mut self, config: SparseQueryConfig) -> Self {
572        self.query_config = Some(config);
573        self
574    }
575}
576
577/// A sparse vector entry: (dimension_id, weight)
578#[derive(Debug, Clone, Copy, PartialEq)]
579pub struct SparseEntry {
580    pub dim_id: u32,
581    pub weight: f32,
582}
583
584/// Sparse vector representation
585#[derive(Debug, Clone, Default)]
586pub struct SparseVector {
587    pub(super) entries: Vec<SparseEntry>,
588}
589
590impl SparseVector {
591    /// Create a new sparse vector
592    pub fn new() -> Self {
593        Self {
594            entries: Vec::new(),
595        }
596    }
597
598    /// Create with pre-allocated capacity
599    pub fn with_capacity(capacity: usize) -> Self {
600        Self {
601            entries: Vec::with_capacity(capacity),
602        }
603    }
604
605    /// Create from dimension IDs and weights
606    pub fn from_entries(dim_ids: &[u32], weights: &[f32]) -> Self {
607        assert_eq!(dim_ids.len(), weights.len());
608        let mut entries: Vec<SparseEntry> = dim_ids
609            .iter()
610            .zip(weights.iter())
611            .map(|(&dim_id, &weight)| SparseEntry { dim_id, weight })
612            .collect();
613        // Sort by dimension ID for efficient intersection
614        entries.sort_by_key(|e| e.dim_id);
615        Self { entries }
616    }
617
618    /// Add an entry (must maintain sorted order by dim_id)
619    pub fn push(&mut self, dim_id: u32, weight: f32) {
620        debug_assert!(
621            self.entries.is_empty() || self.entries.last().unwrap().dim_id < dim_id,
622            "Entries must be added in sorted order by dim_id"
623        );
624        self.entries.push(SparseEntry { dim_id, weight });
625    }
626
627    /// Number of non-zero entries
628    pub fn len(&self) -> usize {
629        self.entries.len()
630    }
631
632    /// Check if empty
633    pub fn is_empty(&self) -> bool {
634        self.entries.is_empty()
635    }
636
637    /// Iterate over entries
638    pub fn iter(&self) -> impl Iterator<Item = &SparseEntry> {
639        self.entries.iter()
640    }
641
642    /// Sort by dimension ID (required for posting list encoding)
643    pub fn sort_by_dim(&mut self) {
644        self.entries.sort_by_key(|e| e.dim_id);
645    }
646
647    /// Sort by weight descending
648    pub fn sort_by_weight_desc(&mut self) {
649        self.entries.sort_by(|a, b| {
650            b.weight
651                .partial_cmp(&a.weight)
652                .unwrap_or(std::cmp::Ordering::Equal)
653        });
654    }
655
656    /// Get top-k entries by weight
657    pub fn top_k(&self, k: usize) -> Vec<SparseEntry> {
658        let mut sorted = self.entries.clone();
659        sorted.sort_by(|a, b| {
660            b.weight
661                .partial_cmp(&a.weight)
662                .unwrap_or(std::cmp::Ordering::Equal)
663        });
664        sorted.truncate(k);
665        sorted
666    }
667
668    /// Compute dot product with another sparse vector
669    pub fn dot(&self, other: &SparseVector) -> f32 {
670        let mut result = 0.0f32;
671        let mut i = 0;
672        let mut j = 0;
673
674        while i < self.entries.len() && j < other.entries.len() {
675            let a = &self.entries[i];
676            let b = &other.entries[j];
677
678            match a.dim_id.cmp(&b.dim_id) {
679                std::cmp::Ordering::Less => i += 1,
680                std::cmp::Ordering::Greater => j += 1,
681                std::cmp::Ordering::Equal => {
682                    result += a.weight * b.weight;
683                    i += 1;
684                    j += 1;
685                }
686            }
687        }
688
689        result
690    }
691
692    /// L2 norm squared
693    pub fn norm_squared(&self) -> f32 {
694        self.entries.iter().map(|e| e.weight * e.weight).sum()
695    }
696
697    /// L2 norm
698    pub fn norm(&self) -> f32 {
699        self.norm_squared().sqrt()
700    }
701
702    /// Prune dimensions below a weight threshold
703    pub fn filter_by_weight(&self, min_weight: f32) -> Self {
704        let entries: Vec<SparseEntry> = self
705            .entries
706            .iter()
707            .filter(|e| e.weight.abs() >= min_weight)
708            .cloned()
709            .collect();
710        Self { entries }
711    }
712}
713
714impl From<Vec<(u32, f32)>> for SparseVector {
715    fn from(pairs: Vec<(u32, f32)>) -> Self {
716        Self {
717            entries: pairs
718                .into_iter()
719                .map(|(dim_id, weight)| SparseEntry { dim_id, weight })
720                .collect(),
721        }
722    }
723}
724
725impl From<SparseVector> for Vec<(u32, f32)> {
726    fn from(vec: SparseVector) -> Self {
727        vec.entries
728            .into_iter()
729            .map(|e| (e.dim_id, e.weight))
730            .collect()
731    }
732}