pub struct SmallBitSet<Conf: SmallConfig>(/* private fields */);
Expand description

Same as BitSet, but sparsely populated hierarchy blocks 9 times smaller.

Which means that sparse sets virtually do not have indirection-related memory overhead!

§Memory

For _128bit each Level1 block consumes just 32 bytes, if less than 8 data blocks pointed from. 256+32 bytes otherwise.

§Performance

All operations still have O(1) complexity. But in terms of raw performance, (due to additional layer of indirection) this is x1.5 - x2 slower than BitSet. Which is still very fast.

§Implementation details

Level0          128bit SIMD                                        
                 [u8;128]                                          

            ┌   128bit SIMD    ┐    ╭─ ── ── ── ── ── ── ── ── ── ╮
            │ ╭──────────────╮ │      Act as SBO:                 │
Level1   Vec│ │   [u16;7]    │ │    │- Inline SparseBitMap, for    
            │ │ ──────────── │◁┼────┤small size.                  │
            │ │Box<[u16;128]>│ │     - Boxed full-size array with │
            └ ╰──────────────╯ ┘    │direct access for a big one.    
            ┌                  ┐    ╰ ── ── ── ── ── ── ── ── ── ─╯
Data     Vec│   128bit SIMD    │                                   
            └                  ┘                                   

SparseBitMap - bit_block acts as a sparse array:

                  0 1       2 3          ◁═ popcnt before element
                                            (dense_array indices)
 bit_block      0 1 1 0 0 0 1 1 0 0 ...                          
              └───┬─┬───────┬─┬─────────┘                        
               ┌──┘┌┘ ┌─────┘ │                                  
               │   │  │  ┌────┘                                  
               ▼   ▼  ▼  ▼                                       
dense_array    1, 32, 4, 5               len = bit_block popcnt  

As you can see, SparseBitMap has fast O(1) per-index access. Insert and remove - are O(N) operations, because dense_array must keep its element order.

Why not just use SparseBitMap for everything? Big-sized dense_array should be placed somewhere outside of LevelBlock struct to be memory efficient (e.g., in dynamic-sized heap). Hence - it will be accessed through the pointer (which is yet another layer of indirection). Due to this, and the O(N) insert/remove - SparseBitMap is used only for small-sized arrays. We aim for a 16-element array - as with this size, data blocks pointed from level1 block will have the same size in total, as a full-sized level1 block indirection array.

Implementations§

source§

impl<Conf> SmallBitSet<Conf>
where Conf: SmallConfig,

source

pub fn new() -> Self

source

pub const fn max_capacity() -> usize

Max usize, bitset with this Conf can hold.

source

pub fn insert(&mut self, index: usize)

§Safety

Will panic, if index is out of range.

source

pub fn remove(&mut self, index: usize) -> bool

Returns false if index is invalid/not in bitset.

source

pub unsafe fn remove_unchecked(&mut self, index: usize)

§Safety

index MUST exists in HiSparseBitset!

source§

impl<Conf> SmallBitSet<Conf>
where Conf: SmallConfig,

source

pub fn block_iter<'a>(&'a self) -> CachingBlockIter<&'a Self>

source

pub fn iter<'a>(&'a self) -> CachingIndexIter<&'a Self>

source

pub fn contains(&self, index: usize) -> bool

source

pub fn is_empty(&self) -> bool

Trait Implementations§

source§

impl<Conf, Rhs> BitAnd<Rhs> for &SmallBitSet<Conf>
where Rhs: BitSetInterface<Conf = <Self as BitSetBase>::Conf>, Conf: SmallConfig,

source§

fn bitand(self, rhs: Rhs) -> Self::Output

Returns intersection of self and rhs bitsets.

§

type Output = Apply<And, &SmallBitSet<Conf>, Rhs>

The resulting type after applying the & operator.
source§

impl<Conf, Rhs> BitOr<Rhs> for &SmallBitSet<Conf>
where Rhs: BitSetInterface<Conf = <Self as BitSetBase>::Conf>, Conf: SmallConfig,

source§

fn bitor(self, rhs: Rhs) -> Self::Output

Returns union of self and rhs bitsets.

§

type Output = Apply<Or, &SmallBitSet<Conf>, Rhs>

The resulting type after applying the | operator.
source§

impl<Conf: SmallConfig> BitSetBase for SmallBitSet<Conf>

§

type Conf = Conf

source§

const TRUSTED_HIERARCHY: bool = true

Does each raised bit in hierarchy bitblock correspond to non-empty data block? Read more
source§

impl<Conf> BitSetInterface for &SmallBitSet<Conf>
where Conf: SmallConfig,

source§

impl<Conf, Rhs> BitXor<Rhs> for &SmallBitSet<Conf>
where Rhs: BitSetInterface<Conf = <Self as BitSetBase>::Conf>, Conf: SmallConfig,

source§

fn bitxor(self, rhs: Rhs) -> Self::Output

Returns symmetric difference of self and rhs bitsets.

§

type Output = Apply<Xor, &SmallBitSet<Conf>, Rhs>

The resulting type after applying the ^ operator.
source§

impl<Conf> Clone for SmallBitSet<Conf>
where Conf: SmallConfig,

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<Conf> Debug for SmallBitSet<Conf>
where Conf: SmallConfig,

source§

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

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

impl<Conf> Default for SmallBitSet<Conf>
where Conf: SmallConfig,

source§

fn default() -> Self

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

impl<Conf, const N: usize> From<[usize; N]> for SmallBitSet<Conf>
where Conf: SmallConfig,

source§

fn from(value: [usize; N]) -> Self

Converts to this type from the input type.
source§

impl<Conf> FromIterator<usize> for SmallBitSet<Conf>
where Conf: SmallConfig,

source§

fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<Conf> IntoIterator for &SmallBitSet<Conf>
where Conf: SmallConfig,

§

type Item = usize

The type of the elements being iterated over.
§

type IntoIter = CachingIndexIter<&SmallBitSet<Conf>>

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<Conf> LevelMasks for SmallBitSet<Conf>
where Conf: SmallConfig,

source§

fn level0_mask(&self) -> <Self::Conf as Config>::Level0BitBlock

source§

unsafe fn level1_mask( &self, level0_index: usize ) -> <Self::Conf as Config>::Level1BitBlock

Safety Read more
source§

unsafe fn data_mask( &self, level0_index: usize, level1_index: usize ) -> <Self::Conf as Config>::DataBitBlock

Safety Read more
source§

impl<Conf> LevelMasksIterExt for SmallBitSet<Conf>
where Conf: SmallConfig,

§

type IterState = <RawBitSet<Conf, Block<<Conf as Config>::Level0BitBlock, <Conf as Config>::Level0BlockIndices>, CompactBlock<<Conf as Config>::Level1BitBlock, <Conf as SmallConfig>::Level1MaskU64Populations, <Conf as Config>::Level1BlockIndices, <Conf as SmallConfig>::Level1SmallBlockIndices>, Block<<Conf as Config>::DataBitBlock, [usize; 0]>> as LevelMasksIterExt>::IterState

Consists from child states (if any) + Self state. Read more
§

type Level1BlockData = <RawBitSet<Conf, Block<<Conf as Config>::Level0BitBlock, <Conf as Config>::Level0BlockIndices>, CompactBlock<<Conf as Config>::Level1BitBlock, <Conf as SmallConfig>::Level1MaskU64Populations, <Conf as Config>::Level1BlockIndices, <Conf as SmallConfig>::Level1SmallBlockIndices>, Block<<Conf as Config>::DataBitBlock, [usize; 0]>> as LevelMasksIterExt>::Level1BlockData

Level1 block related data, used to speed up data_mask access. Read more
source§

fn make_iter_state(&self) -> Self::IterState

source§

unsafe fn drop_iter_state(&self, state: &mut ManuallyDrop<Self::IterState>)

Having separate function for drop not strictly necessary, since IterState can actually drop itself. But! This allows not to store cache size within IterState. Which makes FixedCache CacheData ZST, if its childs are ZSTs, and which makes cache construction and destruction noop. Which is important for short iteration sessions. Read more
source§

unsafe fn init_level1_block_data( &self, state: &mut Self::IterState, level1_block_data: &mut MaybeUninit<Self::Level1BlockData>, level0_index: usize ) -> (<Self::Conf as Config>::Level1BitBlock, bool)

Init level1_block_data and return (Level1Mask, is_not_empty). Read more
source§

unsafe fn data_mask_from_block_data( level1_block_data: &Self::Level1BlockData, level1_index: usize ) -> <Self::Conf as Config>::DataBitBlock

Safety Read more
source§

impl<Conf, Rhs> PartialEq<Rhs> for SmallBitSet<Conf>
where Rhs: LevelMasksIterExt<Conf = <Self as BitSetBase>::Conf>, Conf: SmallConfig,

source§

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

Works faster with TRUSTED_HIERARCHY.

1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Conf, Rhs> Sub<Rhs> for &SmallBitSet<Conf>
where Rhs: BitSetInterface<Conf = <Self as BitSetBase>::Conf>, Conf: SmallConfig,

source§

fn sub(self, rhs: Rhs) -> Self::Output

Returns difference of self and rhs bitsets.

Or relative complement of rhs in self.

§

type Output = Apply<Sub, &SmallBitSet<Conf>, Rhs>

The resulting type after applying the - operator.
source§

impl<Conf> Eq for SmallBitSet<Conf>
where Conf: SmallConfig,

Auto Trait Implementations§

§

impl<Conf> RefUnwindSafe for SmallBitSet<Conf>

§

impl<Conf> Send for SmallBitSet<Conf>

§

impl<Conf> Sync for SmallBitSet<Conf>

§

impl<Conf> Unpin for SmallBitSet<Conf>

§

impl<Conf> UnwindSafe for SmallBitSet<Conf>

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> 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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.