Skip to main content

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::BitChunkIterator;
18pub use arrow_buffer::bit_chunk_iterator::BitChunks;
19pub use arrow_buffer::bit_chunk_iterator::UnalignedBitChunk;
20pub use arrow_buffer::bit_chunk_iterator::UnalignedBitChunkIterator;
21pub use arrow_buffer::bit_iterator::BitIndexIterator;
22pub use arrow_buffer::bit_iterator::BitIterator;
23pub use arrow_buffer::bit_iterator::BitSliceIterator;
24pub use buf::*;
25pub use buf_mut::*;
26pub use view::*;
27
28/// Get the bit value at `index` out of `buf`.
29///
30/// # Panics
31///
32/// Panics if `index` is not between 0 and length of `buf * 8`.
33#[inline(always)]
34pub fn get_bit(buf: &[u8], index: usize) -> bool {
35    buf[index / 8] & (1 << (index % 8)) != 0
36}
37
38/// Get the bit value at `index` out of `buf` without bounds checking.
39///
40/// # Safety
41///
42/// `index` must be between 0 and length of `buf * 8`.
43#[inline(always)]
44pub unsafe fn get_bit_unchecked(buf: *const u8, index: usize) -> bool {
45    (unsafe { *buf.add(index / 8) } & (1 << (index % 8))) != 0
46}
47
48/// Set the bit value at `index` in `buf` without bounds checking.
49///
50/// # Safety
51///
52/// `index` must be between 0 and length of `buf * 8`.
53#[inline(always)]
54pub unsafe fn set_bit_unchecked(buf: *mut u8, index: usize) {
55    unsafe { *buf.add(index / 8) |= 1 << (index % 8) };
56}
57
58/// Unset the bit value at `index` in `buf` without bounds checking.
59///
60/// # Safety
61///
62/// `index` must be between 0 and length of `buf * 8`.
63#[inline(always)]
64pub unsafe fn unset_bit_unchecked(buf: *mut u8, index: usize) {
65    unsafe { *buf.add(index / 8) &= !(1 << (index % 8)) };
66}