Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bit Set

PagedBitSet<K, PAGE = 512, D = Flat> (Python BitSet) stores membership as one bit per slot in each allocated page. Pick it for integer keys that cluster in dense ranges, especially for ordered traversal, range updates or set algebra. A sparse set visits its dense key array in O(len), which can be faster when the occupied pages hold few keys. It also supports generational keys and contiguous access to the keys.

Pages and Costs

PAGE in Rust and page_size in Python count 64-bit words. The default of 512 words uses 4 KiB per allocated page and covers 32,768 keys. Pages are allocated as keys are inserted. Clearing the set or removing keys keeps the pages for reuse. In Rust, shrink_to_fit() frees empty pages and reset() frees all pages.

In a bit set with two 64-bit words per page, key 138 selects page 1, word 0, and bit 10. That bit is set.
Each word stores membership for 64 consecutive keys. In this example, page 1 starts at key 128, so key 138 is 10 bits into its first word.

The Python binding uses the Flat directory and accepts the sizes in PAGE_SIZES. Each directory slot holds one page pointer up to the highest allocated page. At the default size, the full Python key domain needs at most 131,072 directory slots (1 MiB of pointers on a 64-bit platform). Rust can use a different directory for keys spread over a wider domain.

OperationCost with Flat
Membership, insert, removeO(1) per key, plus allocation on first touch
LengthO(1)
Iteration, nearest-key queriesScan directory slots and words in the visited span, then visit set bits
Range addVisit words in the range and allocate newly touched pages
Range discard, countScan directory slots and words in allocated pages that overlap the range
ClearZero the words in all allocated pages
Same-size set algebraScan relevant pages word by word

Rust

Integer key types implement DirectKey<Tag = ()>. Ranges take Rust RangeBounds<K> and refer to the keys’ slot indices. Use explicit upper bounds for narrow integer key types when adding a range. Bit sets with the same PAGE support algebra across directory layouts.

#![allow(unused)]
fn main() {
use paged_collections::bit_set::PagedBitSet;

let mut bits: PagedBitSet<u32> = PagedBitSet::new();
assert_eq!(bits.insert_range(62..67), 5);
assert_eq!(bits.remove_range(64..66), 2);
assert_eq!(bits.range(63..).collect::<Vec<_>>(), [63, 66]);
assert_eq!(bits.next_occupant(64), Some(66));
assert_eq!(bits.pop_first(), Some(62));
}

Python

from paged_collections import BitSet

bits = BitSet([62, 65])
print(bits.add_range(63, 67))
print(list(bits.range(63, 67)))
print(bits.next_occupant(64))
print(bits.to_mask(61, 68).tolist())
print(BitSet.from_mask([True, False, True], offset=62).to_numpy().tolist())
3
[63, 64, 65, 66]
64
[False, True, True, True, True, True, False]
[62, 64]
  • Keys are integers in 0 <= key < 2**32. Membership and discard treat invalid keys as absent. add raises for invalid keys and remove raises KeyError for absent keys.
  • BitSet(keys=None, *, page_size=DEFAULT_BIT_PAGE_SIZE) accepts an initial one-dimensional integer array or sequence.
  • toggle(key) flips membership and returns whether the key is now present. It validates keys the same way as add.
  • first() and last() return the smallest and largest keys. pop_first() and pop_last() remove and return the respective keys. pop() is the same as pop_first(). These methods raise KeyError on an empty set. next_occupant(index) and prev_occupant(index) return the nearest key at or beyond the given key in the respective direction, or None if there is no match. The index must be a valid key.
  • add_range and discard_range return the number of keys changed. count_range counts keys and range iterates over them. Both endpoints of [start, stop) must be integers in 0 <= endpoint <= 2**32. With start >= stop the interval is empty.
  • Iteration is ascending, or descending with reversed(bits). Iterators raise RuntimeError if membership changes during iteration.
  • union, intersection, difference, symmetric_difference, their in-place methods (update for union) and the predicates accept iterable operands. Operators | & - ^, their in-place forms and comparisons take BitSet operands. Two bit sets with the same page size use word-wise algebra. With different sizes, algebra visits keys individually. New algebra results are base BitSet instances using the receiver’s page size. Pickling and copying preserve the page size.
  • add_many and discard_many return the number of keys changed. contains_many returns a boolean NumPy array. to_numpy() returns a new ascending uint32 array.
  • from_mask(mask, offset=0, *, page_size=DEFAULT_BIT_PAGE_SIZE) reads a one-dimensional boolean array or sequence. A true value at position i sets offset + i. The whole mask must fit in the key domain. The page size counts 64-bit words, as in the constructor.
  • to_mask(start, stop) returns a new boolean array with one element per key in the interval, using O(max(0, stop − start)) memory.

API PagedBitSet.