Created
December 13, 2020 04:05
-
-
Save mocobeta/5d0b640d419646f941a855199d5e108c to your computer and use it in GitHub Desktop.
delta code in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://crates.io/crates/bit-vec | |
use bit_vec::BitVec; | |
pub fn encode_delta(li: &[usize]) -> BitVec { | |
fn encode(k: usize) -> BitVec { | |
let body_len: usize = ((k as f64).log2().floor() as usize) + 1; | |
let body = BitVec::from_bytes(&k.to_be_bytes()); | |
// set gamma encoded selector | |
let mut bv = encode_gamma(&[body_len]); | |
// set body bits | |
let mut pos = bv.len(); | |
bv.grow(body_len - 1, false); | |
let mut i = body.len() - body_len + 1; | |
while pos < bv.len() { | |
if let Some(b) = body.get(i) { | |
bv.set(pos, b); | |
} | |
pos += 1; | |
i += 1; | |
} | |
bv | |
} | |
let mut bv_all = BitVec::new(); | |
for &k in li { | |
let bv = encode(k); | |
let mut offset = bv_all.len(); | |
bv_all.grow(bv.len(), false); | |
for bit in bv.iter() { | |
bv_all.set(offset, bit); | |
offset += 1; | |
} | |
} | |
bv_all | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment