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), "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(
bytes: Arc<Bytes<u8>>,
offset: usize,
length: usize,
unset_bits: Option<usize>,
) -> Self
pub unsafe fn from_inner_unchecked( bytes: Arc<Bytes<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.
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 moresource§impl From<Bitmap> for BooleanArray
impl From<Bitmap> for BooleanArray
source§impl From<Bitmap> for NullBuffer
impl From<Bitmap> for NullBuffer
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 PartialEq for Bitmap
impl PartialEq 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 moresource§fn split_at(&self, offset: usize) -> (Self, Self)
fn split_at(&self, offset: usize) -> (Self, Self)
source§unsafe fn split_at_unchecked(&self, offset: usize) -> (Self, Self)
unsafe fn split_at_unchecked(&self, offset: usize) -> (Self, Self)
Auto 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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<A, T, E> FromFallibleIterator<A, E> for Twhere
T: FromIterator<A>,
E: Error,
impl<A, T, E> FromFallibleIterator<A, E> for Twhere
T: FromIterator<A>,
E: Error,
fn from_fallible_iter<F>(iter: F) -> Result<T, E>where
F: FallibleIterator<E, Item = A>,
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