polars_arrow::bitmap

Struct 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

Source

pub fn new() -> Self

Initializes an empty Bitmap.

Source

pub fn try_new(bytes: Vec<u8>, length: usize) -> PolarsResult<Self>

Initializes a new Bitmap from vector of bytes and a length.

§Errors

This function errors iff length > bytes.len() * 8

Source

pub fn len(&self) -> usize

Returns the length of the Bitmap.

Source

pub fn is_empty(&self) -> bool

Returns whether Bitmap is empty

Source

pub fn iter(&self) -> BitmapIter<'_>

Returns a new iterator of bool over this bitmap

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn true_idx_iter(&self) -> TrueIdxIter<'_>

Returns an iterator that only iterates over the set bits.

Source

pub fn aligned<T: BitChunk>(&self) -> AlignedBitmapSlice<'_, T>

Returns the bits of this Bitmap as a AlignedBitmapSlice.

Source

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 range 0 <= offsets < 8.
  • .3: The length in number of bits.
Source

pub fn set_bits(&self) -> usize

Returns the number of set bits on this Bitmap.

See unset_bits for details.

Source

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.

Source

pub fn unset_bits(&self) -> usize

Returns the number of unset bits on this Bitmap.

Guaranteed to be <= self.len().

§Implementation

This function counts the number of unset bits if it is not already computed. Repeated calls use the cached bitcount.

Source

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().

Source

pub unsafe fn update_bit_count(&mut self, bits_set: usize)

Updates the count of the number of set bits on this Bitmap.

§Safety

The number of set bits must be correct.

Source

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.

Source

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()

Source

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.

Source

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()

Source

pub fn get_bit(&self, i: usize) -> bool

Returns whether the bit at position i is set.

§Panics

Panics iff i >= self.len().

Source

pub unsafe fn get_bit_unchecked(&self, i: usize) -> bool

Unsafely returns whether the bit at position i is set.

§Safety

Unsound iff i >= self.len().

Source

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:

  • this Bitmap is not an offsetted slice of another Bitmap
  • this Bitmap has not been cloned (i.e. [Arc]::get_mut yields Some)
  • this Bitmap was not imported from the c data interface (FFI)
Source

pub fn make_mut(self) -> MutableBitmap

Converts this Bitmap into a MutableBitmap, cloning its internal buffer if required (clone-on-write).

Source

pub fn new_zeroed(length: usize) -> Self

Initializes an new Bitmap filled with unset values.

Source

pub fn new_with_value(value: bool, length: usize) -> Self

Initializes an new Bitmap filled with the given value.

Source

pub fn null_count_range(&self, offset: usize, length: usize) -> usize

Counts the nulls (unset bits) starting from offset bits and for length bits.

Source

pub fn from_u8_slice<T: AsRef<[u8]>>(slice: T, length: usize) -> Self

Creates a new Bitmap from a slice and length.

§Panic

Panics iff length <= bytes.len() * 8

Source

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

Source

pub fn get(&self, i: usize) -> Option<bool>

Returns whether the bit at position i is set.

Source

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.

Source

pub fn intersects_with(&self, other: &Self) -> bool

Checks whether two Bitmaps have shared set bits.

This is an optimized version of (self & other) != 0000...

Source

pub fn num_intersections_with(&self, other: &Self) -> usize

Calculates the number of shared set bits between two Bitmaps.

Source

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] }

Source

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

pub fn num_edges(&self) -> usize

Calculates the number of edges from 0 -> 1 and 1 -> 0.

Source§

impl Bitmap

Source

pub unsafe fn from_trusted_len_iter_unchecked<I: Iterator<Item = bool>>( iterator: I, ) -> Self

Creates a new Bitmap from an iterator of booleans.

§Safety

The iterator must report an accurate length.

Source

pub fn from_trusted_len_iter<I: TrustedLen<Item = bool>>(iterator: I) -> Self

Creates a new Bitmap from an iterator of booleans.

Source

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.

Source

pub unsafe fn try_from_trusted_len_iter_unchecked<E, I: Iterator<Item = Result<bool, E>>>( iterator: I, ) -> Result<Self, E>

Creates a new Bitmap from a fallible iterator of booleans.

§Safety

The iterator must report an accurate length.

Trait Implementations§

Source§

impl<'b> BitAnd<&'b Bitmap> for &Bitmap

Source§

type Output = Bitmap

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &'b Bitmap) -> Bitmap

Performs the & operation. Read more
Source§

impl<'a> BitAnd<&'a Bitmap> for MutableBitmap

Source§

type Output = MutableBitmap

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &'a Bitmap) -> Self

Performs the & operation. Read more
Source§

impl<'a> BitAndAssign<&'a Bitmap> for &mut MutableBitmap

Source§

fn bitand_assign(&mut self, rhs: &'a Bitmap)

Performs the &= operation. Read more
Source§

impl<'b> BitOr<&'b Bitmap> for &Bitmap

Source§

type Output = Bitmap

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &'b Bitmap) -> Bitmap

Performs the | operation. Read more
Source§

impl<'a> BitOr<&'a Bitmap> for MutableBitmap

Source§

type Output = MutableBitmap

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &'a Bitmap) -> Self

Performs the | operation. Read more
Source§

impl<'a> BitOrAssign<&'a Bitmap> for &mut MutableBitmap

Source§

fn bitor_assign(&mut self, rhs: &'a Bitmap)

Performs the |= operation. Read more
Source§

impl<'b> BitXor<&'b Bitmap> for &Bitmap

Source§

type Output = Bitmap

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &'b Bitmap) -> Bitmap

Performs the ^ operation. Read more
Source§

impl<'a> BitXor<&'a Bitmap> for MutableBitmap

Source§

type Output = MutableBitmap

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &'a Bitmap) -> Self

Performs the ^ operation. Read more
Source§

impl<'a> BitXorAssign<&'a Bitmap> for &mut MutableBitmap

Source§

fn bitxor_assign(&mut self, rhs: &'a Bitmap)

Performs the ^= operation. Read more
Source§

impl Clone for Bitmap

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bitmap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Bitmap

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Bitmap> for BooleanArray

Source§

fn from(values: Bitmap) -> Self

Converts to this type from the input type.
Source§

impl From<MutableBitmap> for Bitmap

Source§

fn from(buffer: MutableBitmap) -> Self

Converts to this type from the input type.
Source§

impl<P: AsRef<[bool]>> From<P> for Bitmap

Source§

fn from(slice: P) -> Self

Converts to this type from the input type.
Source§

impl FromData<Bitmap> for BooleanArray

Source§

impl FromIterator<bool> for Bitmap

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = bool>,

Creates a value from an iterator. Read more
Source§

impl FromTrustedLenIterator<bool> for Bitmap

Source§

fn from_iter_trusted_length<T: IntoIterator<Item = bool>>(iter: T) -> Self
where T::IntoIter: TrustedLen,

Source§

impl<'a> IntoIterator for &'a Bitmap

Source§

type Item = bool

The type of the elements being iterated over.
Source§

type IntoIter = BitmapIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Bitmap

Source§

type Item = bool

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Not for &Bitmap

Source§

type Output = Bitmap

The resulting type after applying the ! operator.
Source§

fn not(self) -> Bitmap

Performs the unary ! operation. Read more
Source§

impl PartialEq for Bitmap

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Splitable for Bitmap

Source§

fn check_bound(&self, offset: usize) -> bool

Source§

unsafe fn _split_at_unchecked(&self, offset: usize) -> (Self, Self)

Internal implementation of split_at_unchecked. For any usage, prefer the using split_at or split_at_unchecked. Read more
Source§

fn split_at(&self, offset: usize) -> (Self, Self)

Split Self at offset where offset <= self.len().
Source§

unsafe fn split_at_unchecked(&self, offset: usize) -> (Self, Self)

Split Self at offset without checking offset <= self.len(). Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<I> IntoStreamingIterator for I
where I: IntoIterator,

Source§

fn into_streaming_iter(self) -> Convert<Self::IntoIter>

Source§

fn into_streaming_iter_ref<'a, T>(self) -> ConvertRef<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a T>, T: ?Sized,

Turns an IntoIterator of references into a StreamingIterator. Read more
Source§

fn into_streaming_iter_mut<'a, T>(self) -> ConvertMut<'a, Self::IntoIter, T>
where Self: IntoIterator<Item = &'a mut T>, T: ?Sized,

Turns an IntoIterator of mutable references into a StreamingIteratorMut. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.