Created
February 9, 2018 05:44
-
-
Save djg/ba2af324e9d0be65274c5baf8c88480b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Samples are delta-encoded | |
pub fn unpack_sample<T: Default + ops::AddAssign + Clone>(input: &[u8]) | |
-> Result<Vec<T>, &'static str> { | |
if input.len() % mem::size_of::<T>() != 0 { | |
return Err("packed sample data is not aligned"); | |
} | |
let packed_sample: &[T] = unsafe { | |
slice::from_raw_parts(input.as_ptr() as *const T, | |
input.len() / mem::size_of::<T>()) | |
}; | |
let mut unpacked_sample = Vec::<T>::with_capacity(packed_sample.len()); | |
let mut last_sample: T = T::default(); | |
for delta in packed_sample { | |
last_sample += *delta; | |
unpacked_sample.push(last_sample); | |
} | |
Ok(unpacked_sample) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment