This commit is contained in:
Paul Masurel
2016-03-14 10:03:21 +09:00
parent c49da4c318
commit c26cd0e238
2 changed files with 63 additions and 0 deletions

62
src/core/fastfield.rs Normal file
View File

@@ -0,0 +1,62 @@
use std::io::Write;
use std::io;
use core::serialize::BinarySerializable;
struct IntFastFieldWriter {
vals: Vec<u64>,
}
impl IntFastFieldWriter {
pub fn new() -> IntFastFieldWriter {
IntFastFieldWriter {
vals: Vec::new()
}
}
pub fn add(&mut self, val: u64) {
self.vals.push(val);
}
pub fn compute_num_bits(&self, amplitude: u64) -> u8 {
if amplitude == 0 {
0
}
else {
1 + self.compute_num_bits(amplitude / 2)
}
}
pub fn close(&self, write: &mut Write) -> io::Result<()> {
try!((self.vals.len() as u32).serialize(write));
if self.vals.is_empty() {
return Ok(())
}
let min = self.vals.iter().min().unwrap();
let max = self.vals.iter().max().unwrap();
let amplitude: u64 = max - min;
let num_bits = self.compute_num_bits(amplitude);
for val in self.vals.iter() {
try!(val.serialize(write));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::IntFastFieldWriter;
#[test]
fn test_intfastfieldwriter() {
let mut write: Vec<u8> = Vec::new();
let mut int_fast_field_writer = IntFastFieldWriter::new();
int_fast_field_writer.add(4u64);
int_fast_field_writer.add(14u64);
int_fast_field_writer.add(2u64);
int_fast_field_writer.close(&mut write).unwrap();
assert_eq!(write.len(), 8 * 3 + 4);
}
}

View File

@@ -13,6 +13,7 @@ pub mod store;
pub mod simdcompression;
pub mod fstmap;
pub mod index;
pub mod fastfield;
use std::error;