Skip to main content

PagedBitSet

Struct PagedBitSet 

Source
pub struct PagedBitSet<K, const PAGE: usize = DEFAULT_BIT_PAGE, D: Directory = Flat> { /* private fields */ }
Expand description

A set of keys stored as bits at their slot indices.

  • PAGE is the number of 64-bit words per page. It must be non-zero.
  • D is the page directory: Flat (default), Radix for wide keys, or Tree.

The key’s DirectKey::Tag must be (). Use PagedSparseSet for generational keys. Set algebra and equality accept different directories with the same PAGE.

Implementations§

Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> PagedBitSet<K, PAGE, D>

Source

pub const PAGE_SIZE: usize = PAGE

The number of 64-bit words per page.

Source

pub const BITS_PER_PAGE: usize

The number of bits per page. The product must fit in usize.

Source

pub fn new() -> Self

Creates an empty set. The first insertion allocates the first page.

Source

pub fn len(&self) -> usize

Returns the number of keys in the set.

Source

pub fn is_empty(&self) -> bool

Returns whether the set holds no keys.

Source

pub fn allocated_pages(&self) -> usize

Returns the number of allocated pages, including empty ones.

Source

pub fn contains(&self, key: K) -> bool

Returns whether the set contains key.

Source

pub fn insert(&mut self, key: K) -> bool

Inserts key and returns whether it was newly added.

Source

pub fn remove(&mut self, key: K) -> bool

Removes key and returns whether it was present.

Source

pub fn toggle(&mut self, key: K) -> bool

Flips the bit for key and returns whether it is now present.

Source

pub fn insert_range(&mut self, keys: impl RangeBounds<K>) -> usize

Sets the bits in the range and returns the number of newly added keys.

The bounds refer to Key::index. Every slot in the range must represent a valid key of type K. Unbounded endpoints refer to slot zero and usize::MAX; use an explicit upper bound for narrow keys. An empty or inverted range adds zero keys.

§Panics

Panics if the set would contain more than usize::MAX keys or the directory cannot allocate a page.

use paged_collections::bit_set::PagedBitSet;

let mut set: PagedBitSet<u8, 1> = PagedBitSet::new();
assert_eq!(set.insert_range(63..=66), 4);
assert_eq!(set.insert_range(64..=66), 0);
assert_eq!(set.count_range(..=64), 2);
assert_eq!(set.remove_range(65..), 2);
assert_eq!(set.range(..).collect::<Vec<_>>(), [63, 64]);
Source

pub fn remove_range(&mut self, keys: impl RangeBounds<K>) -> usize

Clears the bits in the range and returns the number of removed keys.

The bounds refer to Key::index. An empty or inverted range removes zero keys. Pages stay allocated.

Source

pub fn count_range(&self, keys: impl RangeBounds<K>) -> usize

Counts the set bits in the range.

The bounds refer to Key::index. An empty or inverted range has count zero.

Source

pub fn range(&self, keys: impl RangeBounds<K>) -> Range<'_, K, PAGE, D> ⓘ

Iterates over the keys in the range in ascending slot-index order.

The bounds refer to Key::index. An empty or inverted range yields no keys.

Source

pub fn first(&self) -> Option<K>

Returns the key with the smallest slot index.

Source

pub fn last(&self) -> Option<K>

Returns the key with the largest slot index.

Source

pub fn pop_first(&mut self) -> Option<K>

Removes and returns the key with the smallest slot index.

Source

pub fn pop_last(&mut self) -> Option<K>

Removes and returns the key with the largest slot index.

Source

pub fn next_occupant(&self, index: usize) -> Option<K>

Returns the first key at slot index or after it.

Source

pub fn prev_occupant(&self, index: usize) -> Option<K>

Returns the last key at slot index or before it.

Source

pub fn retain(&mut self, keep: impl FnMut(K) -> bool)

Keeps keys for which keep returns true, visiting them in ascending slot-index order.

If keep panics, the current key and those still to be visited stay in the set.

Source

pub fn drain(&mut self) -> Drain<'_, K, PAGE, D> ⓘ

Removes every key and returns them in ascending slot-index order. Pages stay allocated, as with clear.

Keys are removed as the iterator yields them; dropping it removes the rest. If it is leaked instead (mem::forget), the keys not yet yielded stay in the set.

Source

pub fn extract_if<F: FnMut(K) -> bool>( &mut self, pred: F, ) -> ExtractIf<'_, K, F, PAGE, D> ⓘ

Removes the keys for which pred returns true and yields them, visiting the keys in ascending slot-index order.

Keys are tested and removed as the iterator advances: those it has not reached when it is dropped stay in the set. Use retain if the removed keys are not needed.

use paged_collections::bit_set::PagedBitSet;

let mut set: PagedBitSet<u32> = (0..6).collect();
assert_eq!(set.extract_if(|k| k % 2 == 1).collect::<Vec<_>>(), [1, 3, 5]);
assert_eq!(set.drain().collect::<Vec<_>>(), [0, 2, 4]);
assert!(set.is_empty());
Source

pub fn clear(&mut self)

Removes all keys, keeping the allocated pages for reuse.

Source

pub fn reset(&mut self)

Removes all keys and frees all pages.

Source

pub fn shrink_to_fit(&mut self)

Frees empty pages and shrinks the page directory.

Source

pub fn iter(&self) -> Iter<'_, K, PAGE, D> ⓘ

Iterates over all keys in ascending slot-index order.

Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> PagedBitSet<K, PAGE, D>

Source

pub fn is_disjoint<D2: Directory>( &self, other: &PagedBitSet<K, PAGE, D2>, ) -> bool

Returns whether the two sets have no key in common.

Source

pub fn is_subset<D2: Directory>(&self, other: &PagedBitSet<K, PAGE, D2>) -> bool

Returns whether every key in this set is also in other.

Source

pub fn is_superset<D2: Directory>( &self, other: &PagedBitSet<K, PAGE, D2>, ) -> bool

Returns whether every key in other is also in this set.

Source

pub fn union<'a, D2: Directory>( &'a self, other: &'a PagedBitSet<K, PAGE, D2>, ) -> Union<'a, K, PAGE, D, D2> ⓘ

Iterates over keys in either set, in ascending slot-index order.

use paged_collections::bit_set::PagedBitSet;
use paged_collections::page::Tree;

let a: PagedBitSet<usize, 2> = [1, 5].into_iter().collect();
let b: PagedBitSet<usize, 2, Tree> = [2, 5].into_iter().collect();
assert_eq!(a.union(&b).collect::<Vec<_>>(), [1, 2, 5]);
assert_eq!((&a & &b).iter().collect::<Vec<_>>(), [5]);
Source

pub fn intersection<'a, D2: Directory>( &'a self, other: &'a PagedBitSet<K, PAGE, D2>, ) -> Intersection<'a, K, PAGE, D, D2> ⓘ

Iterates over keys in both sets, in ascending slot-index order.

Source

pub fn difference<'a, D2: Directory>( &'a self, other: &'a PagedBitSet<K, PAGE, D2>, ) -> Difference<'a, K, PAGE, D, D2> ⓘ

Iterates over keys in this set that are absent from other, in ascending slot-index order.

Source

pub fn symmetric_difference<'a, D2: Directory>( &'a self, other: &'a PagedBitSet<K, PAGE, D2>, ) -> SymmetricDifference<'a, K, PAGE, D, D2> ⓘ

Iterates over keys in exactly one set, in ascending slot-index order.

Trait Implementations§

Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitAnd<&PagedBitSet<K, PAGE, D2>> for &PagedBitSet<K, PAGE, D>

Source§

type Output = PagedBitSet<K, PAGE, D>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: &PagedBitSet<K, PAGE, D2>) -> Self::Output

Performs the & operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitAndAssign<&PagedBitSet<K, PAGE, D2>> for PagedBitSet<K, PAGE, D>

Source§

fn bitand_assign(&mut self, other: &PagedBitSet<K, PAGE, D2>)

Performs the &= operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitOr<&PagedBitSet<K, PAGE, D2>> for &PagedBitSet<K, PAGE, D>

Source§

type Output = PagedBitSet<K, PAGE, D>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: &PagedBitSet<K, PAGE, D2>) -> Self::Output

Performs the | operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitOrAssign<&PagedBitSet<K, PAGE, D2>> for PagedBitSet<K, PAGE, D>

Source§

fn bitor_assign(&mut self, other: &PagedBitSet<K, PAGE, D2>)

Performs the |= operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitXor<&PagedBitSet<K, PAGE, D2>> for &PagedBitSet<K, PAGE, D>

Source§

type Output = PagedBitSet<K, PAGE, D>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: &PagedBitSet<K, PAGE, D2>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> BitXorAssign<&PagedBitSet<K, PAGE, D2>> for PagedBitSet<K, PAGE, D>

Source§

fn bitxor_assign(&mut self, other: &PagedBitSet<K, PAGE, D2>)

Performs the ^= operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> Clone for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: DirectKey<Tag = ()> + Debug, const PAGE: usize, D: Directory> Debug for PagedBitSet<K, PAGE, D>

Source§

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

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

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> Default for PagedBitSet<K, PAGE, D>

Source§

fn default() -> Self

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

impl<'de, K: DirectKey<Tag = ()> + Deserialize<'de>, const PAGE: usize, D: Directory> Deserialize<'de> for PagedBitSet<K, PAGE, D>

Available on crate feature serde only.

Deserializes from a sequence of keys, skipping duplicates.

Memory follows the key indices. With the Flat directory one large index allocates a directory entry for every page below it. For untrusted input, use Radix or Tree, or validate the keys first.

Source§

fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> Eq for PagedBitSet<K, PAGE, D>

Source§

impl<'a, K: DirectKey<Tag = ()> + 'a, const PAGE: usize, D: Directory> Extend<&'a K> for PagedBitSet<K, PAGE, D>

Source§

fn extend<I: IntoIterator<Item = &'a K>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> Extend<K> for PagedBitSet<K, PAGE, D>

Source§

fn extend<I: IntoIterator<Item = K>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> FromIterator<K> for PagedBitSet<K, PAGE, D>

Source§

fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> Hash for PagedBitSet<K, PAGE, D>

Hashes the length and the nonzero words in ascending word-index order.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> IntoIterator for &'a PagedBitSet<K, PAGE, D>

Source§

type Item = K

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, PAGE, D>

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<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory> IntoIterator for PagedBitSet<K, PAGE, D>

Source§

type Item = K

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, PAGE, D>

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<'a, K: DirectKey<Tag = ()> + Send, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedBitSet<K, PAGE, D>

Available on crate feature rayon only.
Source§

type Item = K

The type of item that the parallel iterator will produce.
Source§

type Iter = ParIter<'a, K, PAGE>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> PartialEq<PagedBitSet<K, PAGE, D2>> for PagedBitSet<K, PAGE, D>

Compares the set bits, ignoring empty pages and the directory layout.

Source§

fn eq(&self, other: &PagedBitSet<K, PAGE, D2>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<K: DirectKey<Tag = ()> + Serialize, const PAGE: usize, D: Directory> Serialize for PagedBitSet<K, PAGE, D>

Available on crate feature serde only.

Serializes as a sequence of keys in ascending slot-index order.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> Sub<&PagedBitSet<K, PAGE, D2>> for &PagedBitSet<K, PAGE, D>

Source§

type Output = PagedBitSet<K, PAGE, D>

The resulting type after applying the - operator.
Source§

fn sub(self, other: &PagedBitSet<K, PAGE, D2>) -> Self::Output

Performs the - operation. Read more
Source§

impl<K: DirectKey<Tag = ()>, const PAGE: usize, D: Directory, D2: Directory> SubAssign<&PagedBitSet<K, PAGE, D2>> for PagedBitSet<K, PAGE, D>

Source§

fn sub_assign(&mut self, other: &PagedBitSet<K, PAGE, D2>)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<K, const PAGE: usize, D> Forget for PagedBitSet<K, PAGE, D>

§

impl<K, const PAGE: usize, D> Freeze for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: Freeze, PhantomData<fn() -> K>: Freeze,

§

impl<K, const PAGE: usize, D> Move for PagedBitSet<K, PAGE, D>

§

impl<K, const PAGE: usize, D> RefUnwindSafe for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: RefUnwindSafe, PhantomData<fn() -> K>: RefUnwindSafe,

§

impl<K, const PAGE: usize, D> Send for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: Send, PhantomData<fn() -> K>: Send,

§

impl<K, const PAGE: usize, D> Sync for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: Sync, PhantomData<fn() -> K>: Sync,

§

impl<K, const PAGE: usize, D> Unpin for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: Unpin, PhantomData<fn() -> K>: Unpin,

§

impl<K, const PAGE: usize, D> UnsafeUnpin for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: UnsafeUnpin, PhantomData<fn() -> K>: UnsafeUnpin,

§

impl<K, const PAGE: usize, D> UnwindSafe for PagedBitSet<K, PAGE, D>
where <D as Directory>::Store<u64, PAGE>: UnwindSafe, PhantomData<fn() -> K>: UnwindSafe,

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, dest: *mut u8)

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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<'data, I> IntoParallelRefIterator<'data> for I
where I: 'data + ?Sized, &'data I: IntoParallelIterator,

Source§

type Iter = <&'data I as IntoParallelIterator>::Iter

The type of the parallel iterator that will be returned.
Source§

type Item = <&'data I as IntoParallelIterator>::Item

The type of item that the parallel iterator will produce. This will typically be an &'data T reference type.
Source§

fn par_iter(&'data self) -> <I as IntoParallelRefIterator<'data>>::Iter

Converts self into a parallel iterator. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.