Created
December 13, 2020 04:08
-
-
Save mocobeta/e42243a153f52fc267880531dbd07709 to your computer and use it in GitHub Desktop.
vByte 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
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