2026-08-25 18:17:32 +02:00
|
|
|
use std::fs::{self, OpenOptions};
|
2026-05-13 21:39:08 +08:00
|
|
|
use std::io::{self, Seek, SeekFrom, Write as _};
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
|
|
use memmap2::{Mmap, MmapMut};
|
|
|
|
|
|
2026-10-14 18:03:40 +02:00
|
|
|
use crate::mmap_file::{building_path, create_mmap_file, open_mmap_file, publish_built};
|
2026-05-13 21:39:08 +08:00
|
|
|
use crate::reader::PersistentCompactIntVec;
|
2026-06-19 09:12:07 +02:00
|
|
|
use crate::views::{BitSliceIter, BitSliceView, IntSliceView};
|
2026-05-13 21:39:08 +08:00
|
|
|
|
2026-11-17 13:20:08 +01:00
|
|
|
pub(crate) const MAGIC: [u8; 4] = *b"PBIV";
|
2026-05-13 21:39:08 +08:00
|
|
|
|
|
|
|
|
// Header: magic(4) + _pad(4) + n(8) = 16 bytes.
|
2026-06-17 23:07:19 +02:00
|
|
|
// Data starts at offset 16, u64-aligned (mmap base is page-aligned, 16 % 8 == 0).
|
2026-05-13 21:39:08 +08:00
|
|
|
const HEADER_SIZE: usize = 16;
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub(crate) fn n_words(n: usize) -> usize {
|
|
|
|
|
n.div_ceil(64)
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
|
|
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
fn n_bytes_for_words(n: usize) -> usize {
|
|
|
|
|
n_words(n) * 8
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
|
2026-06-17 23:07:19 +02:00
|
|
|
// ── PersistentBitVec ──────────────────────────────────────────────────────────
|
2026-05-13 21:39:08 +08:00
|
|
|
|
|
|
|
|
pub struct PersistentBitVec {
|
|
|
|
|
mmap: Mmap,
|
2026-08-14 21:29:38 +02:00
|
|
|
n: usize,
|
2026-05-13 21:39:08 +08:00
|
|
|
path: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PersistentBitVec {
|
|
|
|
|
pub fn open(path: &Path) -> io::Result<Self> {
|
2026-08-25 18:17:32 +02:00
|
|
|
let (mmap, n) = open_mmap_file(path, MAGIC, HEADER_SIZE)?;
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
|
&self.path
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.n
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.n == 0
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-05-13 21:39:08 +08:00
|
|
|
pub fn get(&self, slot: usize) -> bool {
|
|
|
|
|
(self.mmap[HEADER_SIZE + (slot >> 3)] >> (slot & 7)) & 1 != 0
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 12:06:35 +02:00
|
|
|
/// Batch lookup: read bits at `slots` in an order that minimizes
|
|
|
|
|
/// cache misses.
|
|
|
|
|
///
|
|
|
|
|
/// The slots are sorted internally before reading so that accesses to
|
|
|
|
|
/// the underlying mmap are as sequential as possible, then the results
|
|
|
|
|
/// are reordered to match the input order.
|
2026-08-20 05:31:22 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn collect_slots_values(&self, slots: &[usize]) -> Vec<bool> {
|
|
|
|
|
self.view().collect_slots_values(slots)
|
2026-08-14 12:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-20 05:31:22 +02:00
|
|
|
/// Like [`collect_slots_values`](Self::collect_slots_values), but fills a caller-provided buffer.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn fill_slots_values(&self, slots: &[usize], out: &mut [bool]) {
|
|
|
|
|
self.view().fill_slots_values(slots, out)
|
2026-08-14 12:06:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-17 23:07:19 +02:00
|
|
|
// SAFETY: mmap is page-aligned, HEADER_SIZE=16 divisible by 8 → u64-aligned.
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-05-13 21:39:08 +08:00
|
|
|
fn data_words(&self) -> &[u64] {
|
2026-08-14 21:29:38 +02:00
|
|
|
let nw = n_words(self.n);
|
2026-05-13 21:39:08 +08:00
|
|
|
let ptr = self.mmap[HEADER_SIZE..].as_ptr() as *const u64;
|
|
|
|
|
unsafe { std::slice::from_raw_parts(ptr, nw) }
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn view(&self) -> BitSliceView<'_> {
|
|
|
|
|
BitSliceView::new(self.data_words(), self.n)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn words(&self) -> &[u64] {
|
|
|
|
|
self.data_words()
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn count_ones(&self) -> u64 {
|
|
|
|
|
self.view().count_ones()
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn count_zeros(&self) -> u64 {
|
|
|
|
|
self.view().count_zeros()
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn partial_jaccard_dist(&self, other: &PersistentBitVec) -> (u64, u64) {
|
|
|
|
|
self.view().partial_jaccard_dist(other.view())
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn jaccard_dist(&self, other: &PersistentBitVec) -> f64 {
|
|
|
|
|
self.view().jaccard_dist(other.view())
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn hamming_dist(&self, other: &PersistentBitVec) -> u64 {
|
|
|
|
|
self.view().hamming_dist(other.view())
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-05-13 21:39:08 +08:00
|
|
|
pub fn iter(&self) -> BitIter<'_> {
|
2026-08-14 21:29:38 +02:00
|
|
|
BitIter {
|
|
|
|
|
words: self.data_words(),
|
|
|
|
|
slot: 0,
|
|
|
|
|
n: self.n,
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> IntoIterator for &'a PersistentBitVec {
|
|
|
|
|
type Item = bool;
|
|
|
|
|
type IntoIter = BitIter<'a>;
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
fn into_iter(self) -> BitIter<'a> {
|
|
|
|
|
self.iter()
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-17 23:07:19 +02:00
|
|
|
// ── BitIter ───────────────────────────────────────────────────────────────────
|
|
|
|
|
|
2026-05-13 21:39:08 +08:00
|
|
|
pub struct BitIter<'a> {
|
2026-06-19 09:55:41 +02:00
|
|
|
words: &'a [u64],
|
2026-08-14 21:29:38 +02:00
|
|
|
slot: usize,
|
|
|
|
|
n: usize,
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ExactSizeIterator for BitIter<'_> {}
|
|
|
|
|
|
|
|
|
|
impl Iterator for BitIter<'_> {
|
|
|
|
|
type Item = bool;
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-05-13 21:39:08 +08:00
|
|
|
fn next(&mut self) -> Option<bool> {
|
2026-08-14 21:29:38 +02:00
|
|
|
if self.slot >= self.n {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2026-06-17 09:32:12 +02:00
|
|
|
let v = (self.words[self.slot >> 6] >> (self.slot & 63)) & 1 != 0;
|
2026-05-13 21:39:08 +08:00
|
|
|
self.slot += 1;
|
|
|
|
|
Some(v)
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-05-13 21:39:08 +08:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
let rem = self.n - self.slot;
|
|
|
|
|
(rem, Some(rem))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 23:07:19 +02:00
|
|
|
// ── PersistentBitVecBuilder ───────────────────────────────────────────────────
|
2026-05-13 21:39:08 +08:00
|
|
|
|
|
|
|
|
pub struct PersistentBitVecBuilder {
|
|
|
|
|
mmap: MmapMut,
|
2026-08-14 21:29:38 +02:00
|
|
|
n: usize,
|
2026-06-17 09:19:30 +02:00
|
|
|
path: PathBuf,
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PersistentBitVecBuilder {
|
|
|
|
|
pub fn new(n: usize, path: &Path) -> io::Result<Self> {
|
|
|
|
|
let file_size = HEADER_SIZE + n_bytes_for_words(n);
|
2026-08-25 18:17:32 +02:00
|
|
|
let mmap = create_mmap_file(path, MAGIC, [0u8; 4], n, 0, file_size)?;
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-19 09:55:41 +02:00
|
|
|
pub fn from_raw_bytes(bytes: &[u8], n: usize, path: &Path) -> io::Result<Self> {
|
2026-06-16 23:18:10 +02:00
|
|
|
let file_size = HEADER_SIZE + n_bytes_for_words(n);
|
2026-08-25 18:17:32 +02:00
|
|
|
let mut mmap = create_mmap_file(path, MAGIC, [0u8; 4], n, 0, file_size)?;
|
2026-06-16 23:18:10 +02:00
|
|
|
mmap[HEADER_SIZE..HEADER_SIZE + bytes.len()].copy_from_slice(bytes);
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-06-16 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-19 09:23:44 +02:00
|
|
|
/// Create an all-ones bit vector of length `n` at `path`.
|
|
|
|
|
///
|
|
|
|
|
/// More efficient than `new(n, path)` + `not()`: the data is written as
|
|
|
|
|
/// 0xFF bytes in a single sequential pass, with no intermediate all-zeros state.
|
|
|
|
|
pub fn new_ones(n: usize, path: &Path) -> io::Result<Self> {
|
2026-08-14 21:29:38 +02:00
|
|
|
let nw = n_words(n);
|
2026-06-19 09:23:44 +02:00
|
|
|
let file_size = HEADER_SIZE + nw * 8;
|
2026-08-14 21:29:38 +02:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
|
.read(true)
|
|
|
|
|
.write(true)
|
|
|
|
|
.create(true)
|
|
|
|
|
.truncate(true)
|
2026-10-14 18:03:40 +02:00
|
|
|
.open(building_path(path))?;
|
2026-06-19 09:23:44 +02:00
|
|
|
file.write_all(&MAGIC)?;
|
|
|
|
|
file.write_all(&[0u8; 4])?;
|
|
|
|
|
file.write_all(&(n as u64).to_le_bytes())?;
|
|
|
|
|
file.write_all(&vec![0xFFu8; nw * 8])?;
|
|
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
file.set_len(file_size as u64)?;
|
|
|
|
|
let mut mmap = unsafe { MmapMut::map_mut(&file)? };
|
|
|
|
|
// Clear padding bits in the last word so trailing bits are always 0.
|
|
|
|
|
let rem = n % 64;
|
|
|
|
|
if rem != 0 {
|
2026-08-14 21:29:38 +02:00
|
|
|
let ptr = mmap[HEADER_SIZE..].as_mut_ptr() as *mut u64;
|
2026-06-19 09:23:44 +02:00
|
|
|
let words = unsafe { std::slice::from_raw_parts_mut(ptr, nw) };
|
|
|
|
|
words[nw - 1] &= (1u64 << rem) - 1;
|
|
|
|
|
}
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-06-19 09:23:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-13 21:39:08 +08:00
|
|
|
pub fn build_from(source: &PersistentBitVec, path: &Path) -> io::Result<Self> {
|
2026-10-14 18:03:40 +02:00
|
|
|
fs::copy(source.path(), building_path(path))?;
|
|
|
|
|
let file = OpenOptions::new().read(true).write(true).open(building_path(path))?;
|
2026-05-13 21:39:08 +08:00
|
|
|
let mmap = unsafe { MmapMut::map_mut(&file)? };
|
|
|
|
|
let n = source.len();
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn build_from_counts(
|
|
|
|
|
source: &PersistentCompactIntVec,
|
|
|
|
|
threshold: u32,
|
|
|
|
|
path: &Path,
|
|
|
|
|
) -> io::Result<Self> {
|
2026-05-13 21:39:08 +08:00
|
|
|
let n = source.len();
|
|
|
|
|
let file_size = HEADER_SIZE + n_bytes_for_words(n);
|
2026-08-25 18:17:32 +02:00
|
|
|
let mut mmap = create_mmap_file(path, MAGIC, [0u8; 4], n, 0, file_size)?;
|
2026-05-13 21:39:08 +08:00
|
|
|
{
|
2026-08-14 21:29:38 +02:00
|
|
|
let nw = n_words(n);
|
2026-05-13 21:39:08 +08:00
|
|
|
let ptr = mmap[HEADER_SIZE..].as_mut_ptr() as *mut u64;
|
|
|
|
|
let words = unsafe { std::slice::from_raw_parts_mut(ptr, nw) };
|
|
|
|
|
for (slot, count) in source.iter().enumerate() {
|
2026-08-14 21:29:38 +02:00
|
|
|
if count >= threshold {
|
|
|
|
|
words[slot >> 6] |= 1u64 << (slot & 63);
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-14 21:29:38 +02:00
|
|
|
Ok(Self {
|
|
|
|
|
mmap,
|
|
|
|
|
n,
|
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
|
})
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn build_from_presence(source: &PersistentCompactIntVec, path: &Path) -> io::Result<Self> {
|
|
|
|
|
Self::build_from_counts(source, 1, path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.n
|
|
|
|
|
}
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.n == 0
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn get(&self, slot: usize) -> bool {
|
|
|
|
|
(self.mmap[HEADER_SIZE + (slot >> 3)] >> (slot & 7)) & 1 != 0
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|
2026-06-17 09:19:30 +02:00
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn set(&mut self, slot: usize, value: bool) {
|
|
|
|
|
let bit = 1u64 << (slot & 63);
|
2026-08-14 21:29:38 +02:00
|
|
|
if value {
|
|
|
|
|
self.data_words_mut()[slot >> 6] |= bit;
|
|
|
|
|
} else {
|
|
|
|
|
self.data_words_mut()[slot >> 6] &= !bit;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
fn data_words(&self) -> &[u64] {
|
2026-08-14 21:29:38 +02:00
|
|
|
let nw = n_words(self.n);
|
2026-06-17 23:07:19 +02:00
|
|
|
let ptr = self.mmap[HEADER_SIZE..].as_ptr() as *const u64;
|
|
|
|
|
unsafe { std::slice::from_raw_parts(ptr, nw) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SAFETY: same alignment argument as PersistentBitVec::data_words.
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
fn data_words_mut(&mut self) -> &mut [u64] {
|
2026-08-14 21:29:38 +02:00
|
|
|
let nw = n_words(self.n);
|
2026-06-17 23:07:19 +02:00
|
|
|
let ptr = self.mmap[HEADER_SIZE..].as_mut_ptr() as *mut u64;
|
|
|
|
|
unsafe { std::slice::from_raw_parts_mut(ptr, nw) }
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn view(&self) -> BitSliceView<'_> {
|
|
|
|
|
BitSliceView::new(self.data_words(), self.n)
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn words(&self) -> &[u64] {
|
|
|
|
|
self.data_words()
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
|
|
|
|
|
pub fn copy_from(&mut self, src: BitSliceView<'_>) {
|
|
|
|
|
assert_eq!(self.n, src.len(), "BitSliceView length mismatch");
|
|
|
|
|
self.data_words_mut().copy_from_slice(src.words());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn and(&mut self, other: BitSliceView<'_>) {
|
|
|
|
|
assert_eq!(self.n, other.len(), "BitSliceView length mismatch");
|
2026-08-14 21:29:38 +02:00
|
|
|
for (w, &o) in self.data_words_mut().iter_mut().zip(other.words()) {
|
|
|
|
|
*w &= o;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn or(&mut self, other: BitSliceView<'_>) {
|
|
|
|
|
assert_eq!(self.n, other.len(), "BitSliceView length mismatch");
|
2026-08-14 21:29:38 +02:00
|
|
|
for (w, &o) in self.data_words_mut().iter_mut().zip(other.words()) {
|
|
|
|
|
*w |= o;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn xor(&mut self, other: BitSliceView<'_>) {
|
|
|
|
|
assert_eq!(self.n, other.len(), "BitSliceView length mismatch");
|
2026-08-14 21:29:38 +02:00
|
|
|
for (w, &o) in self.data_words_mut().iter_mut().zip(other.words()) {
|
|
|
|
|
*w ^= o;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn not(&mut self) {
|
2026-08-14 21:29:38 +02:00
|
|
|
let rem = self.n % 64;
|
2026-06-17 23:07:19 +02:00
|
|
|
let words = self.data_words_mut();
|
2026-08-14 21:29:38 +02:00
|
|
|
for w in words.iter_mut() {
|
|
|
|
|
*w ^= u64::MAX;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
if rem != 0 {
|
2026-08-14 21:29:38 +02:00
|
|
|
if let Some(last) = words.last_mut() {
|
|
|
|
|
*last &= (1u64 << rem) - 1;
|
|
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-19 09:12:07 +02:00
|
|
|
/// OR in bits at slots where `pred(col[slot])` is true.
|
|
|
|
|
pub fn or_where(&mut self, col: IntSliceView<'_>, pred: impl Fn(u32) -> bool) {
|
|
|
|
|
assert_eq!(self.n, col.len(), "IntSliceView length mismatch");
|
|
|
|
|
let n = self.n;
|
|
|
|
|
let primary = col.primary_bytes();
|
|
|
|
|
let words = self.data_words_mut();
|
|
|
|
|
let nw = n_words(n);
|
|
|
|
|
for wi in 0..nw {
|
2026-08-14 21:29:38 +02:00
|
|
|
let base = wi * 64;
|
2026-06-19 09:12:07 +02:00
|
|
|
let limit = (base + 64).min(n);
|
|
|
|
|
let mut mask = 0u64;
|
|
|
|
|
for bit in 0..(limit - base) {
|
|
|
|
|
let b = primary[base + bit];
|
2026-08-14 21:29:38 +02:00
|
|
|
if b < 255 && pred(b as u32) {
|
|
|
|
|
mask |= 1u64 << bit;
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
words[wi] |= mask;
|
|
|
|
|
}
|
|
|
|
|
for (slot, val) in col.overflow_entries() {
|
2026-08-14 21:29:38 +02:00
|
|
|
if pred(val) {
|
|
|
|
|
words[slot >> 6] |= 1u64 << (slot & 63);
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clear bits at slots where `pred(col[slot])` is false.
|
|
|
|
|
pub fn and_where(&mut self, col: IntSliceView<'_>, pred: impl Fn(u32) -> bool) {
|
|
|
|
|
assert_eq!(self.n, col.len(), "IntSliceView length mismatch");
|
|
|
|
|
let n = self.n;
|
|
|
|
|
let primary = col.primary_bytes();
|
|
|
|
|
let words = self.data_words_mut();
|
|
|
|
|
let nw = n_words(n);
|
|
|
|
|
for wi in 0..nw {
|
2026-08-14 21:29:38 +02:00
|
|
|
let base = wi * 64;
|
2026-06-19 09:12:07 +02:00
|
|
|
let limit = (base + 64).min(n);
|
|
|
|
|
let mut mask = 0u64;
|
|
|
|
|
for bit in 0..(limit - base) {
|
|
|
|
|
let b = primary[base + bit];
|
2026-08-14 21:29:38 +02:00
|
|
|
if b < 255 && !pred(b as u32) {
|
|
|
|
|
mask |= 1u64 << bit;
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
words[wi] &= !mask;
|
|
|
|
|
}
|
|
|
|
|
for (slot, val) in col.overflow_entries() {
|
2026-08-14 21:29:38 +02:00
|
|
|
if !pred(val) {
|
|
|
|
|
words[slot >> 6] &= !(1u64 << (slot & 63));
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Toggle bits at slots where `pred(col[slot])` is true.
|
|
|
|
|
pub fn xor_where(&mut self, col: IntSliceView<'_>, pred: impl Fn(u32) -> bool) {
|
|
|
|
|
assert_eq!(self.n, col.len(), "IntSliceView length mismatch");
|
|
|
|
|
let n = self.n;
|
|
|
|
|
let primary = col.primary_bytes();
|
|
|
|
|
let words = self.data_words_mut();
|
|
|
|
|
let nw = n_words(n);
|
|
|
|
|
for wi in 0..nw {
|
2026-08-14 21:29:38 +02:00
|
|
|
let base = wi * 64;
|
2026-06-19 09:12:07 +02:00
|
|
|
let limit = (base + 64).min(n);
|
|
|
|
|
let mut mask = 0u64;
|
|
|
|
|
for bit in 0..(limit - base) {
|
|
|
|
|
let b = primary[base + bit];
|
2026-08-14 21:29:38 +02:00
|
|
|
if b < 255 && pred(b as u32) {
|
|
|
|
|
mask |= 1u64 << bit;
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
words[wi] ^= mask;
|
|
|
|
|
}
|
|
|
|
|
for (slot, val) in col.overflow_entries() {
|
2026-08-14 21:29:38 +02:00
|
|
|
if pred(val) {
|
|
|
|
|
words[slot >> 6] ^= 1u64 << (slot & 63);
|
|
|
|
|
}
|
2026-06-19 09:12:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 22:29:46 +02:00
|
|
|
#[inline]
|
2026-06-17 23:07:19 +02:00
|
|
|
pub fn iter(&self) -> BitSliceIter<'_> {
|
|
|
|
|
self.view().iter()
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 21:29:38 +02:00
|
|
|
pub fn close(self) -> io::Result<()> {
|
2026-10-14 18:03:40 +02:00
|
|
|
self.mmap.flush()?;
|
|
|
|
|
publish_built(&self.path)
|
2026-08-14 21:29:38 +02:00
|
|
|
}
|
2026-06-17 23:07:19 +02:00
|
|
|
|
2026-06-17 09:19:30 +02:00
|
|
|
pub fn finish(self) -> io::Result<PersistentBitVec> {
|
|
|
|
|
let path = self.path.clone();
|
|
|
|
|
self.close()?;
|
|
|
|
|
PersistentBitVec::open(&path)
|
|
|
|
|
}
|
2026-05-13 21:39:08 +08:00
|
|
|
}
|