Skip to content

Instantly share code, notes, and snippets.

@mocobeta
Created December 13, 2020 04:08
Show Gist options
  • Save mocobeta/e42243a153f52fc267880531dbd07709 to your computer and use it in GitHub Desktop.
Save mocobeta/e42243a153f52fc267880531dbd07709 to your computer and use it in GitHub Desktop.
vByte code in Rust
pub fn encode_vbyte(li: &[usize]) -> Vec<u8> {
fn encode(k: usize) -> Vec<u8> {
let mut vbytes = Vec::new();
let mut tmp = k;
while tmp >= 128 {
vbytes.push(128 + (tmp & 127) as u8);
tmp >>= 7;
}
vbytes.push(tmp as u8);
vbytes
}
let mut bytes_all = Vec::new();
for &k in li {
let mut vbytes = encode(k);
bytes_all.append(&mut vbytes);
}
bytes_all
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment