vortex_buffer/bit/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Packed bitmaps that can be used to store boolean values.
5//!
6//! This module provides a wrapper on top of the `Buffer` type to store mutable and immutable
7//! bitsets. The bitsets are stored in little-endian order, meaning that the least significant bit
8//! of the first byte is the first bit in the bitset.
9#[cfg(feature = "arrow")]
10mod arrow;
11mod buf;
12mod buf_mut;
13mod macros;
14mod ops;
15mod view;
16
17pub use arrow_buffer::bit_chunk_iterator::{
18 BitChunkIterator, BitChunks, UnalignedBitChunk, UnalignedBitChunkIterator,
19};
20pub use arrow_buffer::bit_iterator::{BitIndexIterator, BitIterator, BitSliceIterator};
21pub use buf::*;
22pub use buf_mut::*;
23pub use view::*;
24
25/// Get the bit value at `index` out of `buf`.
26#[inline(always)]
27pub fn get_bit(buf: &[u8], index: usize) -> bool {
28 buf[index / 8] & (1 << (index % 8)) != 0
29}
30
31/// Get the bit value at `index` out of `buf` without bounds checking
32///
33/// # Safety
34///
35/// `index` must be between 0 and length of `buf`
36#[inline(always)]
37pub unsafe fn get_bit_unchecked(buf: *const u8, index: usize) -> bool {
38 (unsafe { *buf.add(index / 8) } & (1 << (index % 8))) != 0
39}
40
41/// Set the bit value at `index` in `buf` without bounds checking
42///
43/// # Safety
44///
45/// `index` must be between 0 and length of `buf`
46#[inline(always)]
47pub unsafe fn set_bit_unchecked(buf: *mut u8, index: usize) {
48 unsafe { *buf.add(index / 8) |= 1 << (index % 8) };
49}
50
51/// Unset the bit value at `index` in `buf` without bounds checking
52///
53/// # Safety
54///
55/// `index` must be between 0 and length of `buf`
56#[inline(always)]
57pub unsafe fn unset_bit_unchecked(buf: *mut u8, index: usize) {
58 unsafe { *buf.add(index / 8) &= !(1 << (index % 8)) };
59}