2024-05-18 11:11:15 +02:00
|
|
|
use crate::DataRef;
|
|
|
|
|
2024-05-09 23:30:18 +02:00
|
|
|
/// A vector of bits
|
2024-05-17 18:36:18 +02:00
|
|
|
#[derive(Clone, PartialEq)]
|
2024-05-09 23:30:18 +02:00
|
|
|
pub struct BitVec {
|
2024-05-17 23:48:30 +02:00
|
|
|
size: usize,
|
2024-05-09 23:30:18 +02:00
|
|
|
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 {
|
2024-05-17 23:48:30 +02:00
|
|
|
size,
|
2024-05-11 23:28:08 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-05-15 23:08:54 +02:00
|
|
|
old_value
|
2024-05-09 23:30:18 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2024-05-15 23:08:54 +02:00
|
|
|
self.data[byte_index] & bit_mask != 0
|
2024-05-10 12:24:07 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
/// ```
|
2024-05-10 16:14:53 +02:00
|
|
|
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-15 20:34:51 +02:00
|
|
|
/// Gets the length in bits
|
2024-05-11 00:36:27 +02:00
|
|
|
pub fn len(&self) -> usize {
|
2024-05-17 23:48:30 +02:00
|
|
|
self.size
|
2024-05-11 00:36:27 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:14:38 +02:00
|
|
|
/// returns true if length is 0.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.data.is_empty()
|
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Calculates the byte index and bitmask for a specific bit in the vector
|
2024-05-17 23:48:30 +02:00
|
|
|
fn get_indexes(&self, bit_index: usize) -> (usize, u8) {
|
|
|
|
if bit_index >= self.size {
|
2024-05-17 23:56:20 +02:00
|
|
|
panic!(
|
|
|
|
"bit index {bit_index} is outside of range 0..<{}",
|
|
|
|
self.size
|
|
|
|
)
|
2024-05-17 23:48:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let byte_index = bit_index / 8;
|
|
|
|
let bit_in_byte_index = 7 - bit_index % 8;
|
2024-05-10 12:24:07 +02:00
|
|
|
let bit_mask: u8 = 1 << bit_in_byte_index;
|
2024-05-15 23:08:54 +02:00
|
|
|
(byte_index, bit_mask)
|
2024-05-09 23:30:18 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-10 00:53:12 +02:00
|
|
|
|
2024-05-18 11:11:15 +02:00
|
|
|
impl DataRef for BitVec {
|
|
|
|
fn data_ref_mut(&mut self) -> &mut [u8] {
|
|
|
|
self.data.as_mut_slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_ref(&self) -> &[u8] {
|
|
|
|
self.data.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
impl From<BitVec> for Vec<u8> {
|
|
|
|
/// Turns the `BitVec` into the underlying `Vec<u8>`
|
|
|
|
fn from(value: BitVec) -> Self {
|
|
|
|
value.data
|
2024-05-10 00:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-11 18:28:57 +02:00
|
|
|
|
2024-05-12 01:30:55 +02:00
|
|
|
impl From<&[u8]> for BitVec {
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Interpret the data as a series of bits and load then into a new `BitVec` instance.
|
2024-05-12 01:30:55 +02:00
|
|
|
fn from(value: &[u8]) -> Self {
|
2024-05-12 13:14:33 +02:00
|
|
|
Self {
|
2024-05-17 23:48:30 +02:00
|
|
|
size: value.len() * 8,
|
2024-05-12 13:14:33 +02:00
|
|
|
data: Vec::from(value),
|
|
|
|
}
|
2024-05-12 01:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-11 18:28:57 +02:00
|
|
|
impl std::fmt::Debug for BitVec {
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Formats a `BitVec` for debug. The manual implementation includes the length of the instance.
|
2024-05-11 18:28:57 +02:00
|
|
|
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
|
|
|
|
2024-05-16 23:03:39 +02:00
|
|
|
#[cfg(feature = "c_api")]
|
2024-05-12 17:15:30 +02:00
|
|
|
pub mod c_api {
|
2024-05-18 11:11:15 +02:00
|
|
|
use crate::{BitVec, CByteSlice, DataRef};
|
2024-05-12 17:15:30 +02:00
|
|
|
|
|
|
|
/// Creates a new `BitVec` instance.
|
|
|
|
/// The returned instance has to be freed with `bit_vec_dealloc`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_new(size: usize) -> *mut BitVec {
|
2024-05-12 17:15:30 +02:00
|
|
|
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`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_load(
|
|
|
|
data: *const u8,
|
|
|
|
data_length: usize,
|
|
|
|
) -> *mut BitVec {
|
2024-05-12 17:15:30 +02:00
|
|
|
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`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_clone(
|
|
|
|
this: *const BitVec,
|
|
|
|
) -> *mut BitVec {
|
2024-05-12 17:15:30 +02:00
|
|
|
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.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_dealloc(this: *mut BitVec) {
|
2024-05-12 17:15:30 +02:00
|
|
|
_ = Box::from_raw(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the value of a bit from the `BitVec`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_get(
|
|
|
|
this: *const BitVec,
|
|
|
|
index: usize,
|
|
|
|
) -> bool {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).get(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of a bit in the `BitVec`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_set(
|
|
|
|
this: *mut BitVec,
|
|
|
|
index: usize,
|
|
|
|
value: bool,
|
|
|
|
) -> bool {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).set(index, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the value of all bits in the `BitVec`.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_fill(this: *mut BitVec, value: bool) {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).fill(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the length of the `BitVec` in bits.
|
2024-05-12 18:28:53 +02:00
|
|
|
#[no_mangle]
|
2024-05-13 01:26:44 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize {
|
2024-05-12 17:15:30 +02:00
|
|
|
(*this).len()
|
|
|
|
}
|
2024-05-15 20:34:51 +02:00
|
|
|
|
2024-05-15 23:14:38 +02:00
|
|
|
/// Returns true if length is 0.
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_is_empty(this: *const BitVec) -> bool {
|
|
|
|
(*this).is_empty()
|
|
|
|
}
|
|
|
|
|
2024-05-15 20:34:51 +02:00
|
|
|
/// Gets an unsafe reference to the data of the `BitVec` instance.
|
|
|
|
///
|
|
|
|
/// ## Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure to never access the returned memory after the `BitVec`
|
|
|
|
/// instance has been consumed or manually deallocated.
|
|
|
|
///
|
|
|
|
/// Reading and writing concurrently to either the original instance or the returned data will
|
|
|
|
/// result in undefined behavior.
|
|
|
|
#[no_mangle]
|
2024-05-16 23:18:43 +02:00
|
|
|
pub unsafe extern "C" fn sp2_bit_vec_unsafe_data_ref(
|
|
|
|
this: *mut BitVec,
|
|
|
|
) -> CByteSlice {
|
2024-05-18 11:11:15 +02:00
|
|
|
let data = (*this).data_ref_mut();
|
2024-05-15 20:34:51 +02:00
|
|
|
CByteSlice {
|
|
|
|
start: data.as_mut_ptr_range().start,
|
|
|
|
length: data.len(),
|
|
|
|
}
|
|
|
|
}
|
2024-05-12 17:15:30 +02:00
|
|
|
}
|
2024-05-17 17:38:51 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-05-18 11:11:15 +02:00
|
|
|
use crate::{BitVec, DataRef};
|
2024-05-17 17:38:51 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fill() {
|
|
|
|
let mut vec = BitVec::new(8 * 3);
|
|
|
|
assert_eq!(vec.data, [0x00, 0x00, 0x00]);
|
|
|
|
|
|
|
|
vec.fill(true);
|
|
|
|
assert_eq!(vec.data, [0xFF, 0xFF, 0xFF]);
|
|
|
|
|
|
|
|
vec.fill(false);
|
|
|
|
assert_eq!(vec.data, [0x00, 0x00, 0x00]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_set() {
|
|
|
|
let mut vec = BitVec::new(8 * 3);
|
2024-05-17 18:44:31 +02:00
|
|
|
assert!(!vec.get(1));
|
|
|
|
assert!(!vec.get(11));
|
2024-05-17 17:38:51 +02:00
|
|
|
|
|
|
|
vec.set(1, true);
|
|
|
|
vec.set(11, true);
|
|
|
|
assert_eq!(vec.data, [0x40, 0x10, 0x00]);
|
2024-05-17 18:44:31 +02:00
|
|
|
assert!(!vec.get(0));
|
|
|
|
assert!(vec.get(1));
|
|
|
|
assert!(vec.get(11));
|
2024-05-17 17:38:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn load() {
|
|
|
|
let mut vec = BitVec::new(8 * 3);
|
|
|
|
vec.set(6, true);
|
|
|
|
vec.set(7, true);
|
|
|
|
vec.set(8, true);
|
|
|
|
vec.set(9, true);
|
|
|
|
vec.set(10, true);
|
|
|
|
vec.set(vec.len() - 1, true);
|
|
|
|
|
|
|
|
assert_eq!(vec.data, [0x03, 0xE0, 0x01]);
|
|
|
|
|
|
|
|
let data: Vec<u8> = vec.into();
|
|
|
|
|
|
|
|
let vec = BitVec::from(&*data);
|
|
|
|
assert_eq!(vec.data, [0x03, 0xE0, 0x01]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-05-17 18:44:31 +02:00
|
|
|
fn mut_data_ref() {
|
2024-05-17 17:38:51 +02:00
|
|
|
let mut vec = BitVec::new(8 * 3);
|
|
|
|
|
2024-05-18 11:11:15 +02:00
|
|
|
let data_ref = vec.data_ref_mut();
|
2024-05-17 17:38:51 +02:00
|
|
|
data_ref.copy_from_slice(&[0x40, 0x10, 0x00]);
|
|
|
|
|
|
|
|
assert_eq!(vec.data, [0x40, 0x10, 0x00]);
|
2024-05-17 18:44:31 +02:00
|
|
|
assert!(vec.get(1));
|
2024-05-17 17:38:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn is_empty() {
|
|
|
|
let vec = BitVec::new(8 * 3);
|
2024-05-17 18:44:31 +02:00
|
|
|
assert!(!vec.is_empty());
|
2024-05-17 17:38:51 +02:00
|
|
|
|
|
|
|
let vec = BitVec::new(0);
|
2024-05-17 18:44:31 +02:00
|
|
|
assert!(vec.is_empty());
|
2024-05-17 17:38:51 +02:00
|
|
|
}
|
2024-05-17 21:02:50 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_returns_old() {
|
|
|
|
let mut vec = BitVec::new(8);
|
2024-05-17 23:56:20 +02:00
|
|
|
assert!(!vec.set(1, true));
|
|
|
|
assert!(vec.set(1, true));
|
|
|
|
assert!(vec.set(1, false));
|
|
|
|
assert!(!vec.set(1, false));
|
2024-05-17 21:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn debug_print() {
|
|
|
|
let vec = BitVec::new(8 * 3);
|
|
|
|
format!("{vec:?}");
|
|
|
|
}
|
2024-05-17 18:44:31 +02:00
|
|
|
}
|