pub struct Bitmap { /* private fields */ }
Expand description
An immutable container semantically equivalent to Arc<Vec<bool>>
but represented as Arc<Vec<u8>>
where
each boolean is represented as a single bit.
§Examples
use polars_arrow::bitmap::{Bitmap, MutableBitmap};
let bitmap = Bitmap::from([true, false, true]);
assert_eq!(bitmap.iter().collect::<Vec<_>>(), vec![true, false, true]);
// creation directly from bytes
let bitmap = Bitmap::try_new(vec![0b00001101], 5).unwrap();
// note: the first bit is the left-most of the first byte
assert_eq!(bitmap.iter().collect::<Vec<_>>(), vec![true, false, true, true, false]);
// we can also get the slice:
assert_eq!(bitmap.as_slice(), ([0b00001101u8].as_ref(), 0, 5));
// debug helps :)
assert_eq!(format!("{:?}", bitmap), "Bitmap { len: 5, offset: 0, bytes: [0b___01101] }");
// it supports copy-on-write semantics (to a `MutableBitmap`)
let bitmap: MutableBitmap = bitmap.into_mut().right().unwrap();
assert_eq!(bitmap, MutableBitmap::from([true, false, true, true, false]));
// slicing is 'O(1)' (data is shared)
let bitmap = Bitmap::try_new(vec![0b00001101], 5).unwrap();
let mut sliced = bitmap.clone();
sliced.slice(1, 4);
assert_eq!(sliced.as_slice(), ([0b00001101u8].as_ref(), 1, 4)); // 1 here is the offset:
assert_eq!(format!("{:?}", sliced), "Bitmap { len: 4, offset: 1, bytes: [0b___0110_] }");
// when sliced (or cloned), it is no longer possible to `into_mut`.
let same: Bitmap = sliced.into_mut().left().unwrap();
Implementations§
Source§impl Bitmap
impl Bitmap
Sourcepub fn iter(&self) -> BitmapIter<'_> ⓘ
pub fn iter(&self) -> BitmapIter<'_> ⓘ
Returns a new iterator of bool
over this bitmap
Sourcepub fn chunks<T: BitChunk>(&self) -> BitChunks<'_, T> ⓘ
pub fn chunks<T: BitChunk>(&self) -> BitChunks<'_, T> ⓘ
Returns an iterator over bits in bit chunks BitChunk
.
This iterator is useful to operate over multiple bits via e.g. bitwise.
Sourcepub fn fast_iter_u32(&self) -> FastU32BitmapIter<'_> ⓘ
pub fn fast_iter_u32(&self) -> FastU32BitmapIter<'_> ⓘ
Returns a fast iterator that gives 32 bits at a time. Has a remainder that must be handled separately.
Sourcepub fn fast_iter_u56(&self) -> FastU56BitmapIter<'_> ⓘ
pub fn fast_iter_u56(&self) -> FastU56BitmapIter<'_> ⓘ
Returns a fast iterator that gives 56 bits at a time. Has a remainder that must be handled separately.
Sourcepub fn fast_iter_u64(&self) -> FastU64BitmapIter<'_> ⓘ
pub fn fast_iter_u64(&self) -> FastU64BitmapIter<'_> ⓘ
Returns a fast iterator that gives 64 bits at a time. Has a remainder that must be handled separately.
Sourcepub fn true_idx_iter(&self) -> TrueIdxIter<'_> ⓘ
pub fn true_idx_iter(&self) -> TrueIdxIter<'_> ⓘ
Returns an iterator that only iterates over the set bits.
Sourcepub fn aligned<T: BitChunk>(&self) -> AlignedBitmapSlice<'_, T>
pub fn aligned<T: BitChunk>(&self) -> AlignedBitmapSlice<'_, T>
Returns the bits of this Bitmap
as a AlignedBitmapSlice
.
Sourcepub fn as_slice(&self) -> (&[u8], usize, usize)
pub fn as_slice(&self) -> (&[u8], usize, usize)
Returns the byte slice of this Bitmap
.
The returned tuple contains:
.1
: The byte slice, truncated to the start of the first bit. So the start of the slice is within the first 8 bits..2
: The start offset in bits on a range0 <= offsets < 8
..3
: The length in number of bits.
Sourcepub fn set_bits(&self) -> usize
pub fn set_bits(&self) -> usize
Returns the number of set bits on this Bitmap
.
See unset_bits
for details.
Sourcepub fn lazy_set_bits(&self) -> Option<usize>
pub fn lazy_set_bits(&self) -> Option<usize>
Returns the number of set bits on this Bitmap
if it is known.
See lazy_unset_bits
for details.
Sourcepub fn unset_bits(&self) -> usize
pub fn unset_bits(&self) -> usize
Sourcepub fn lazy_unset_bits(&self) -> Option<usize>
pub fn lazy_unset_bits(&self) -> Option<usize>
Returns the number of unset bits on this Bitmap
if it is known.
Guaranteed to be <= self.len()
.
Sourcepub unsafe fn update_bit_count(&mut self, bits_set: usize)
pub unsafe fn update_bit_count(&mut self, bits_set: usize)
Sourcepub fn slice(&mut self, offset: usize, length: usize)
pub fn slice(&mut self, offset: usize, length: usize)
Slices self
, offsetting by offset
and truncating up to length
bits.
§Panic
Panics iff offset + length > self.length
, i.e. if the offset and length
exceeds the allocated capacity of self
.
Sourcepub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize)
pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize)
Slices self
, offsetting by offset
and truncating up to length
bits.
§Safety
The caller must ensure that self.offset + offset + length <= self.len()
Sourcepub fn sliced(self, offset: usize, length: usize) -> Self
pub fn sliced(self, offset: usize, length: usize) -> Self
Slices self
, offsetting by offset
and truncating up to length
bits.
§Panic
Panics iff offset + length > self.length
, i.e. if the offset and length
exceeds the allocated capacity of self
.
Sourcepub unsafe fn sliced_unchecked(self, offset: usize, length: usize) -> Self
pub unsafe fn sliced_unchecked(self, offset: usize, length: usize) -> Self
Slices self
, offsetting by offset
and truncating up to length
bits.
§Safety
The caller must ensure that self.offset + offset + length <= self.len()
Sourcepub unsafe fn get_bit_unchecked(&self, i: usize) -> bool
pub unsafe fn get_bit_unchecked(&self, i: usize) -> bool
Sourcepub fn into_mut(self) -> Either<Self, MutableBitmap> ⓘ
pub fn into_mut(self) -> Either<Self, MutableBitmap> ⓘ
Converts this Bitmap
to MutableBitmap
, returning itself if the conversion
is not possible
This operation returns a MutableBitmap
iff:
Sourcepub fn make_mut(self) -> MutableBitmap
pub fn make_mut(self) -> MutableBitmap
Converts this Bitmap
into a MutableBitmap
, cloning its internal
buffer if required (clone-on-write).
Sourcepub fn new_zeroed(length: usize) -> Self
pub fn new_zeroed(length: usize) -> Self
Initializes an new Bitmap
filled with unset values.
Sourcepub fn new_with_value(value: bool, length: usize) -> Self
pub fn new_with_value(value: bool, length: usize) -> Self
Initializes an new Bitmap
filled with the given value.
Sourcepub fn null_count_range(&self, offset: usize, length: usize) -> usize
pub fn null_count_range(&self, offset: usize, length: usize) -> usize
Counts the nulls (unset bits) starting from offset
bits and for length
bits.
Sourcepub fn from_u8_slice<T: AsRef<[u8]>>(slice: T, length: usize) -> Self
pub fn from_u8_slice<T: AsRef<[u8]>>(slice: T, length: usize) -> Self
Sourcepub fn from_u8_vec(vec: Vec<u8>, length: usize) -> Self
pub fn from_u8_vec(vec: Vec<u8>, length: usize) -> Self
Alias for Bitmap::try_new().unwrap()
This function is O(1)
§Panic
This function panics iff length > bytes.len() * 8
Sourcepub unsafe fn from_inner_unchecked(
storage: SharedStorage<u8>,
offset: usize,
length: usize,
unset_bits: Option<usize>,
) -> Self
pub unsafe fn from_inner_unchecked( storage: SharedStorage<u8>, offset: usize, length: usize, unset_bits: Option<usize>, ) -> Self
Creates a [Bitmap]
from its internal representation.
This is the inverted from [Bitmap::into_inner]
§Safety
Callers must ensure all invariants of this struct are upheld.
Sourcepub fn intersects_with(&self, other: &Self) -> bool
pub fn intersects_with(&self, other: &Self) -> bool
Checks whether two Bitmap
s have shared set bits.
This is an optimized version of (self & other) != 0000..
.
Sourcepub fn num_intersections_with(&self, other: &Self) -> usize
pub fn num_intersections_with(&self, other: &Self) -> usize
Calculates the number of shared set bits between two Bitmap
s.
Sourcepub fn select(&self, truthy: &Self, falsy: &Self) -> Self
pub fn select(&self, truthy: &Self, falsy: &Self) -> Self
Select between truthy
and falsy
based on self
.
This essentially performs:
out[i] = if self[i] { truthy[i] } else { falsy[i] }
Sourcepub fn select_constant(&self, truthy: &Self, falsy: bool) -> Self
pub fn select_constant(&self, truthy: &Self, falsy: bool) -> Self
Select between truthy
and constant falsy
based on self
.
This essentially performs:
out[i] = if self[i] { truthy[i] } else { falsy }
Source§impl Bitmap
impl Bitmap
Sourcepub unsafe fn from_trusted_len_iter_unchecked<I: Iterator<Item = bool>>(
iterator: I,
) -> Self
pub unsafe fn from_trusted_len_iter_unchecked<I: Iterator<Item = bool>>( iterator: I, ) -> Self
Sourcepub fn from_trusted_len_iter<I: TrustedLen<Item = bool>>(iterator: I) -> Self
pub fn from_trusted_len_iter<I: TrustedLen<Item = bool>>(iterator: I) -> Self
Creates a new Bitmap
from an iterator of booleans.
Sourcepub fn try_from_trusted_len_iter<E, I: TrustedLen<Item = Result<bool, E>>>(
iterator: I,
) -> Result<Self, E>
pub fn try_from_trusted_len_iter<E, I: TrustedLen<Item = Result<bool, E>>>( iterator: I, ) -> Result<Self, E>
Creates a new Bitmap
from a fallible iterator of booleans.
Trait Implementations§
Source§impl<'a> BitAnd<&'a Bitmap> for MutableBitmap
impl<'a> BitAnd<&'a Bitmap> for MutableBitmap
Source§impl<'a> BitAndAssign<&'a Bitmap> for &mut MutableBitmap
impl<'a> BitAndAssign<&'a Bitmap> for &mut MutableBitmap
Source§fn bitand_assign(&mut self, rhs: &'a Bitmap)
fn bitand_assign(&mut self, rhs: &'a Bitmap)
&=
operation. Read moreSource§impl<'a> BitOr<&'a Bitmap> for MutableBitmap
impl<'a> BitOr<&'a Bitmap> for MutableBitmap
Source§impl<'a> BitOrAssign<&'a Bitmap> for &mut MutableBitmap
impl<'a> BitOrAssign<&'a Bitmap> for &mut MutableBitmap
Source§fn bitor_assign(&mut self, rhs: &'a Bitmap)
fn bitor_assign(&mut self, rhs: &'a Bitmap)
|=
operation. Read moreSource§impl<'a> BitXor<&'a Bitmap> for MutableBitmap
impl<'a> BitXor<&'a Bitmap> for MutableBitmap
Source§impl<'a> BitXorAssign<&'a Bitmap> for &mut MutableBitmap
impl<'a> BitXorAssign<&'a Bitmap> for &mut MutableBitmap
Source§fn bitxor_assign(&mut self, rhs: &'a Bitmap)
fn bitxor_assign(&mut self, rhs: &'a Bitmap)
^=
operation. Read moreSource§impl From<Bitmap> for BooleanArray
impl From<Bitmap> for BooleanArray
Source§impl From<MutableBitmap> for Bitmap
impl From<MutableBitmap> for Bitmap
Source§fn from(buffer: MutableBitmap) -> Self
fn from(buffer: MutableBitmap) -> Self
Source§impl FromData<Bitmap> for BooleanArray
impl FromData<Bitmap> for BooleanArray
fn from_data_default(values: Bitmap, validity: Option<Bitmap>) -> BooleanArray
Source§impl FromIterator<bool> for Bitmap
impl FromIterator<bool> for Bitmap
Source§impl FromTrustedLenIterator<bool> for Bitmap
impl FromTrustedLenIterator<bool> for Bitmap
fn from_iter_trusted_length<T: IntoIterator<Item = bool>>(iter: T) -> Selfwhere
T::IntoIter: TrustedLen,
Source§impl<'a> IntoIterator for &'a Bitmap
impl<'a> IntoIterator for &'a Bitmap
Source§impl IntoIterator for Bitmap
impl IntoIterator for Bitmap
Source§impl Splitable for Bitmap
impl Splitable for Bitmap
fn check_bound(&self, offset: usize) -> bool
Source§unsafe fn _split_at_unchecked(&self, offset: usize) -> (Self, Self)
unsafe fn _split_at_unchecked(&self, offset: usize) -> (Self, Self)
split_at_unchecked
. For any usage, prefer the using
split_at
or split_at_unchecked
. Read moreAuto Trait Implementations§
impl !Freeze for Bitmap
impl RefUnwindSafe for Bitmap
impl Send for Bitmap
impl Sync for Bitmap
impl Unpin for Bitmap
impl UnwindSafe for Bitmap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more