feat: add iteration and aggregation to compact int vec

Implemented `sum()`, `count_nonzero()`, and `iter()` to complete the numeric vector interface. The builder now computes aggregate values across memory-mapped regions and overflow entries, while the reader delegates these operations to its inherent methods. The iterator provides zero-copy access to underlying `u32` elements.
This commit is contained in:
Eric Coissac
2026-06-17 09:48:09 +02:00
parent 497d250d8a
commit 26de90f18d
3 changed files with 11 additions and 1 deletions
+7 -1
View File
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
use memmap2::MmapMut; use memmap2::MmapMut;
use crate::format::{HEADER_SIZE, finalize_pciv, parse_overflow_entry}; use crate::format::{byte_count_nonzero, byte_sum, HEADER_SIZE, finalize_pciv, parse_overflow_entry};
use crate::reader::PersistentCompactIntVec; use crate::reader::PersistentCompactIntVec;
pub struct PersistentCompactIntVecBuilder { pub struct PersistentCompactIntVecBuilder {
@@ -148,6 +148,12 @@ impl IntSlice for PersistentCompactIntVecBuilder {
fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ { fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ {
self.overflow.iter().map(|(&k, &v)| (k, v)) self.overflow.iter().map(|(&k, &v)| (k, v))
} }
fn sum(&self) -> u64 {
byte_sum(&self.mmap[HEADER_SIZE..HEADER_SIZE + self.n], self.overflow.values().copied())
}
fn count_nonzero(&self) -> u64 {
byte_count_nonzero(&self.mmap[HEADER_SIZE..HEADER_SIZE + self.n])
}
} }
impl IntSliceMut for PersistentCompactIntVecBuilder { impl IntSliceMut for PersistentCompactIntVecBuilder {
+1
View File
@@ -80,6 +80,7 @@ impl IntSlice for MemoryIntVec {
fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ { fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ {
self.overflow.iter().map(|(&k, &v)| (k, v)) self.overflow.iter().map(|(&k, &v)| (k, v))
} }
fn iter(&self) -> impl Iterator<Item = u32> + '_ { self.iter() }
fn sum(&self) -> u64 { self.sum() } fn sum(&self) -> u64 { self.sum() }
fn count_nonzero(&self) -> u64 { self.count_nonzero() } fn count_nonzero(&self) -> u64 { self.count_nonzero() }
} }
+3
View File
@@ -363,6 +363,9 @@ impl IntSlice for PersistentCompactIntVec {
fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ { fn overflow_entries(&self) -> impl Iterator<Item = (usize, u32)> + '_ {
(0..self.n_overflow).map(|i| (self.data_slot(i), self.data_value(i))) (0..self.n_overflow).map(|i| (self.data_slot(i), self.data_value(i)))
} }
fn iter(&self) -> impl Iterator<Item = u32> + '_ { self.iter() }
fn sum(&self) -> u64 { self.sum() }
fn count_nonzero(&self) -> u64 { self.count_nonzero() }
} }
impl<'a> IntoIterator for &'a PersistentCompactIntVec { impl<'a> IntoIterator for &'a PersistentCompactIntVec {