Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 12, 2022
1 parent 111a240 commit 6082798
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/encoding/bitpacked/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use std::convert::TryInto;

use super::{Packed, Unpackable, Unpacked};

/// Encodes `u32` values into a buffer using `num_bits`.
/// Encodes (packs) a slice of [`Unpackable`] into bitpacked bytes `packed`, using `num_bits` per value.
///
/// This function assumes that the maximum value in `unpacked` fits in `num_bits` bits
/// and saturates higher values.
///
/// Only the first `ceil8(unpacked.len() * num_bits)` of `packed` are populated.
pub fn encode<T: Unpackable>(unpacked: &[T], num_bits: usize, packed: &mut [u8]) {
let chunks = unpacked.chunks_exact(T::Unpacked::LENGTH);

Expand Down Expand Up @@ -30,12 +35,19 @@ pub fn encode<T: Unpackable>(unpacked: &[T], num_bits: usize, packed: &mut [u8])
}
}

/// Encodes (packs) a potentially incomplete pack of [`Unpackable`] into bitpacked
/// bytes `packed`, using `num_bits` per value.
///
/// This function assumes that the maximum value in `unpacked` fits in `num_bits` bits
/// and saturates higher values.
///
/// Only the first `ceil8(unpacked.len() * num_bits)` of `packed` are populated.
#[inline]
pub fn encode_pack<T: Unpackable>(unpacked: &[T], num_bits: usize, packed: &mut [u8]) {
if unpacked.len() < T::Packed::LENGTH {
let mut buf = T::Unpacked::zero();
buf.as_mut()[..unpacked.len()].copy_from_slice(unpacked);
T::pack(&buf, num_bits, packed)
let mut complete_unpacked = T::Unpacked::zero();
complete_unpacked.as_mut()[..unpacked.len()].copy_from_slice(unpacked);
T::pack(&complete_unpacked, num_bits, packed)
} else {
T::pack(&unpacked.try_into().unwrap(), num_bits, packed)
}
Expand Down

0 comments on commit 6082798

Please sign in to comment.