Struct polars_arrow::bitmap::Bitmap
source · 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), "[0b___01101]".to_string());
// 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), "[0b___0110_]".to_string());
// 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 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
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.
sourcepub unsafe fn try_from_trusted_len_iter_unchecked<E, I: Iterator<Item = Result<bool, E>>>(
iterator: I
) -> Result<Self, E>
pub unsafe fn try_from_trusted_len_iter_unchecked<E, I: Iterator<Item = Result<bool, E>>>( iterator: I ) -> Result<Self, E>
sourcepub fn from_null_buffer(value: NullBuffer) -> Self
pub fn from_null_buffer(value: NullBuffer) -> Self
Create a new Bitmap
from an arrow NullBuffer
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 more