use bitvec::order::Lsb0;
use byteorder::{LittleEndian, ReadBytesExt};
use rand::AsByteSliceMut;
use simple_sds_sbwt::serialize::Serialize;
use std::io::Read;
pub fn load_sdsl_bit_vector(input: &mut impl std::io::Read) -> std::io::Result<bitvec::vec::BitVec<u64, Lsb0>> {
let n_bits = input.read_u64::<LittleEndian>()?;
let n_bits_plus_pad = n_bits.div_ceil(64) * 64;
let n_bytes = n_bits_plus_pad / 8;
let n_words = n_bytes / 8;
let mut words: Vec<u64> = vec![0; n_words as usize];
input.read_exact(words.as_byte_slice_mut()).unwrap();
let mut vec = bitvec::vec::BitVec::<u64, Lsb0>::from_vec(words);
assert!(vec.len() >= n_bits as usize);
vec.truncate(n_bits as usize); Ok(vec)
}
pub fn load_runtime_width_sdsl_int_vector(input: &mut impl std::io::Read) -> std::io::Result<simple_sds_sbwt::int_vector::IntVector> {
let n_bits = input.read_u64::<LittleEndian>()?; let width = input.read_u8()? as u64;
assert!(n_bits % width == 0);
let n_elements = n_bits / width;
let n_bits_plus_pad = n_bits.div_ceil(64) * 64;
let n_bytes = n_bits_plus_pad / 8;
let n_words = n_bytes / 8;
let mut new_header = [n_elements, width, n_bits, n_words]; let mut modified_input = new_header.as_byte_slice_mut().chain(input);
simple_sds_sbwt::int_vector::IntVector::load(&mut modified_input)
}
pub fn load_known_width_sdsl_int_vector(input: &mut impl std::io::Read, width: u8) -> std::io::Result<simple_sds_sbwt::int_vector::IntVector> {
let n_bits = input.read_u64::<LittleEndian>()?; let mut new_header = [0u8; 9];
new_header[0..8].copy_from_slice(&n_bits.to_le_bytes());
new_header[8] = width;
let mut modified_input = new_header.as_byte_slice_mut().chain(input);
load_runtime_width_sdsl_int_vector(&mut modified_input)
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
use simple_sds_sbwt::ops::{Access, Vector};
#[test]
fn test_load_sdsl_bit_vector(){
let data = hex!("81 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
let v = load_sdsl_bit_vector(&mut std::io::Cursor::new(&data)).unwrap();
assert!(v.len() == 129);
for i in 0..129 {
print!("{}", v[i]);
if v[i] {
assert!(i == 8 || i == 9);
} else {
assert!(i != 8 && i != 9);
}
}
println!();
}
#[test]
fn test_load_empty_sdsl_bit_vector(){
let data = hex!("00 00 00 00 00 00 00 00");
let v = load_sdsl_bit_vector(&mut std::io::Cursor::new(&data)).unwrap();
assert!(v.is_empty());
}
#[test]
fn test_load_runtime_sdsl_int_vector(){
let data = hex!("64 00 00 00 00 00 00 00 05 00 80 03 00 00 00 00 00 00 00 00 00 00 00 00 00");
let v = load_runtime_width_sdsl_int_vector(&mut std::io::Cursor::new(&data)).unwrap();
assert!(v.len() == 20);
assert!(v.width() == 5);
for i in 0..v.len() {
print!("{} ", v.get(i));
if i == 3 {
assert!(v.get(i) == 7);
} else {
assert!(v.get(i) == 0);
}
}
println!();
}
#[test]
fn test_load_compile_time_sdsl_int_vector(){
let data = hex!("64 00 00 00 00 00 00 00 00 80 03 00 00 00 00 00 00 00 00 00 00 00 00 00");
let width = 5;
let v = load_known_width_sdsl_int_vector(&mut std::io::Cursor::new(&data), width).unwrap();
assert!(v.len() == 20);
assert!(v.width() == 5);
for i in 0..v.len() {
print!("{} ", v.get(i));
if i == 3 {
assert!(v.get(i) == 7);
} else {
assert!(v.get(i) == 0);
}
}
println!();
}
}