servicepoint-binding-c/servicepoint2/src/bit_vec.rs

159 lines
4.2 KiB
Rust
Raw Normal View History

2024-05-09 23:30:18 +02:00
/// A vector of bits
#[derive(Clone)]
2024-05-09 23:30:18 +02:00
pub struct BitVec {
data: Vec<u8>,
}
impl BitVec {
2024-05-12 01:30:55 +02:00
/// Create a new bit vector.
///
/// # Arguments
///
/// * `size`: size in bits. Must be dividable by 8.
///
/// returns: bit vector with all bits set to false.
2024-05-09 23:30:18 +02:00
pub fn new(size: usize) -> BitVec {
assert_eq!(size % 8, 0);
2024-05-11 23:28:08 +02:00
Self {
data: vec![0; size / 8],
}
2024-05-09 23:30:18 +02:00
}
2024-05-12 01:30:55 +02:00
/// Sets the value of a bit.
///
/// # Arguments
///
/// * `index`: the bit index to edit
/// * `value`: the value to set the bit to
///
/// returns: old value of the bit
2024-05-09 23:30:18 +02:00
pub fn set(&mut self, index: usize, value: bool) -> bool {
2024-05-10 12:24:07 +02:00
let (byte_index, bit_mask) = self.get_indexes(index);
2024-05-09 23:30:18 +02:00
let byte = self.data[byte_index];
let old_value = byte & bit_mask != 0;
self.data[byte_index] = if value {
byte | bit_mask
} else {
2024-05-10 12:24:07 +02:00
byte & (u8::MAX ^ bit_mask)
2024-05-09 23:30:18 +02:00
};
return old_value;
}
2024-05-12 01:30:55 +02:00
/// Gets the value of a bit.
///
/// # Arguments
///
/// * `index`: the bit index to read
///
/// returns: value of the bit
2024-05-09 23:30:18 +02:00
pub fn get(&self, index: usize) -> bool {
2024-05-10 12:24:07 +02:00
let (byte_index, bit_mask) = self.get_indexes(index);
return self.data[byte_index] & bit_mask != 0;
}
2024-05-12 01:30:55 +02:00
/// Sets all bits to the specified value
///
/// # Arguments
///
/// * `value`: the value to set all bits to
///
/// # Examples
/// ```
/// use servicepoint2::BitVec;
/// let mut vec = BitVec::new(8);
/// vec.fill(true);
/// ```
pub fn fill(&mut self, value: bool) {
let byte: u8 = if value { 0xFF } else { 0x00 };
2024-05-10 12:24:07 +02:00
self.data.fill(byte);
}
2024-05-11 00:36:27 +02:00
pub fn len(&self) -> usize {
self.data.len() * 8
2024-05-11 00:36:27 +02:00
}
2024-05-10 12:24:07 +02:00
fn get_indexes(&self, index: usize) -> (usize, u8) {
2024-05-09 23:30:18 +02:00
let byte_index = index / 8;
let bit_in_byte_index = 7 - index % 8;
2024-05-10 12:24:07 +02:00
let bit_mask: u8 = 1 << bit_in_byte_index;
return (byte_index, bit_mask);
2024-05-09 23:30:18 +02:00
}
}
2024-05-10 00:53:12 +02:00
impl Into<Vec<u8>> for BitVec {
fn into(self) -> Vec<u8> {
self.data
}
}
2024-05-12 01:30:55 +02:00
impl From<&[u8]> for BitVec {
fn from(value: &[u8]) -> Self {
2024-05-12 13:14:33 +02:00
Self {
data: Vec::from(value),
}
2024-05-12 01:30:55 +02:00
}
}
impl std::fmt::Debug for BitVec {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("BitVec")
.field("len", &self.len())
.field("data", &self.data)
.finish()
}
}
2024-05-12 17:15:30 +02:00
#[allow(unused)]
pub mod c_api {
use crate::BitVec;
/// Creates a new `BitVec` instance.
/// The returned instance has to be freed with `bit_vec_dealloc`.
pub unsafe extern "C" fn bit_vec_new(size: usize) -> *mut BitVec {
Box::into_raw(Box::new(BitVec::new(size)))
}
/// Loads a `BitVec` from the provided data.
/// The returned instance has to be freed with `bit_vec_dealloc`.
pub unsafe extern "C" fn byte_grid_load(data: *const u8, data_length: usize) -> *mut BitVec {
let data = std::slice::from_raw_parts(data, data_length);
Box::into_raw(Box::new(BitVec::from(data)))
}
/// Clones a `BitVec`.
/// The returned instance has to be freed with `bit_vec_dealloc`.
pub unsafe extern "C" fn byte_grid_clone(this: *const BitVec) -> *mut BitVec {
Box::into_raw(Box::new((*this).clone()))
}
/// Deallocates a `BitVec`.
///
/// Note: do not call this if the grid has been consumed in another way, e.g. to create a command.
pub unsafe extern "C" fn byte_grid_dealloc(this: *mut BitVec) {
_ = Box::from_raw(this);
}
/// Gets the value of a bit from the `BitVec`.
pub unsafe extern "C" fn byte_grid_get(this: *const BitVec, index: usize) -> bool {
(*this).get(index)
}
/// Sets the value of a bit in the `BitVec`.
pub unsafe extern "C" fn byte_grid_set(this: *mut BitVec, index: usize, value: bool) -> bool {
(*this).set(index, value)
}
/// Sets the value of all bits in the `BitVec`.
pub unsafe extern "C" fn byte_grid_fill(this: *mut BitVec, value: bool) {
(*this).fill(value)
}
/// Gets the length of the `BitVec` in bits.
pub unsafe extern "C" fn byte_grid_len(this: *const BitVec) -> usize{
(*this).len()
}
}