pub struct SmolBitmap { /* private fields */ }Expand description
A compact bitmap that stores bits either inline or on the heap.
§Overview
SmolBitmap provides efficient storage for bit flags with automatic
promotion from inline storage to heap allocation when needed. It’s designed
for scenarios where you frequently need small bitmaps but occasionally
require larger ones.
§Storage Strategy
- Inline: Up to
SmolBitmap::inline_capacity()bits stored directly in the struct - Heap: Automatically switches to heap allocation for larger bitmaps
- The highest bit of the last word indicates storage mode (0 = inline, 1 = heap)
§Capacity Model
Unlike typical collections, SmolBitmap unifies length and capacity. All
bits up to the capacity are accessible, with unset bits returning false.
§Internal Representation
The array field has dual purpose:
- Inline mode: Contains the actual bit data
- External mode: Contains [pointer, -length] where length is negative to set MSB
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
// Set some bits
bitmap.insert(10);
bitmap.insert(100);
// Check bits
assert!(bitmap.get(10));
assert!(bitmap.get(100));
assert!(!bitmap.get(50)); // Unset bits are false
// Force heap allocation
bitmap.insert(200);
assert!(bitmap.is_spilled()); // Now using heap storageImplementations§
Source§impl SmolBitmap
impl SmolBitmap
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new empty bitmap with inline storage.
The bitmap starts with inline storage capacity (127 bits) and will automatically promote to heap storage if needed.
§Examples
let bitmap = SmolBitmap::new();
assert_eq!(bitmap.capacity(), SmolBitmap::inline_capacity());
assert!(!bitmap.is_spilled());Sourcepub const fn inline_capacity() -> usize
pub const fn inline_capacity() -> usize
Returns the maximum number of bits that can be stored in inline storage.
This is 127 bits (2 × 64 - 1), as the highest bit is reserved for storage mode indication.
§Examples
assert_eq!(SmolBitmap::inline_capacity(), 127);Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
Removes all bits bit such that f(bit) returns false. This
operation is performed in-place and efficiently processes bits word
by word.
§Arguments
f- A predicate function that takes a bit index and returnstrueif the bit should be retained
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(2);
bitmap.insert(5);
bitmap.insert(8);
// Retain only even indices
bitmap.retain(|bit| bit % 2 == 0);
assert!(!bitmap.get(1));
assert!(bitmap.get(2));
assert!(!bitmap.get(5));
assert!(bitmap.get(8));Sourcepub fn with_capacity(bits: usize) -> Self
pub fn with_capacity(bits: usize) -> Self
Creates a new bitmap with at least the specified bit capacity.
If the capacity is within the inline limit
(inline_capacity), the bitmap will use
inline storage. Otherwise, it will allocate heap storage.
§Examples
// Small capacity uses inline storage
let bitmap = SmolBitmap::with_capacity(100);
assert!(!bitmap.is_spilled());
// Large capacity uses heap storage
let bitmap = SmolBitmap::with_capacity(1000);
assert!(bitmap.is_spilled());Sourcepub const fn is_spilled(&self) -> bool
pub const fn is_spilled(&self) -> bool
Returns true if the bitmap is using heap storage.
§Examples
let mut bitmap = SmolBitmap::new();
assert!(!bitmap.is_spilled());
bitmap.insert(200); // Beyond inline capacity
assert!(bitmap.is_spilled());Sourcepub const fn as_mut_ptr(&mut self) -> *mut u64
pub const fn as_mut_ptr(&mut self) -> *mut u64
Get a mutable pointer to the storage
Sourcepub const unsafe fn as_mut_slice(&mut self) -> &mut [u64]
pub const unsafe fn as_mut_slice(&mut self) -> &mut [u64]
Get the storage as a mutable slice
§Safety
The caller must ensure that:
- If the storage is inline, the MSB of the last word must not be set
- Modifications must maintain the storage invariants
- The slice must not be extended beyond its capacity
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the total number of bits that can be stored without reallocation.
For inline storage, this is always
inline_capacity. For heap storage, this is
the number of allocated bits.
§Examples
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.capacity(), SmolBitmap::inline_capacity());Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more bits.
This method ensures that the bitmap can store at least additional
more bits beyond its current capacity. It may reserve more space
to avoid frequent reallocations.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.reserve(300);
assert!(bitmap.capacity() >= 300);
assert!(bitmap.is_spilled());Sourcepub fn get(&self, i: usize) -> bool
pub fn get(&self, i: usize) -> bool
Returns the value of the bit at the given index.
Returns false if the index is out of bounds.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(42);
assert!(bitmap.get(42));
assert!(!bitmap.get(43));
assert!(!bitmap.get(1000)); // Out of bounds returns falseSourcepub fn replace(&mut self, i: usize, v: bool) -> bool
pub fn replace(&mut self, i: usize, v: bool) -> bool
Sets the bit at the given index to the specified value and returns the previous value.
If the index is beyond the current capacity and the value is true,
the bitmap will be resized to accommodate it. If the value is false
and the index is out of bounds, no resize occurs and false is
returned.
§Examples
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.replace(10, true), false);
assert_eq!(bitmap.replace(10, false), true);
assert_eq!(bitmap.replace(10, true), false);Sourcepub fn set(&mut self, i: usize, v: bool)
pub fn set(&mut self, i: usize, v: bool)
Sets the bit at the given index to the specified value.
If the index is beyond the current capacity and the value is true,
the bitmap will be resized to accommodate it.
§Performance
- O(1) for bits within current capacity
- O(n) if reallocation is needed (when index > capacity)
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(10);
bitmap.insert(20);
assert!(bitmap.get(10));
assert!(bitmap.get(20));Sourcepub fn insert(&mut self, i: usize) -> bool
pub fn insert(&mut self, i: usize) -> bool
Inserts a bit at the given index, setting it to true.
Returns true if the bit was previously unset (insertion occurred),
or false if it was already set.
§Examples
let mut bitmap = SmolBitmap::new();
assert!(bitmap.insert(10)); // First insertion
assert!(!bitmap.insert(10)); // Already setSourcepub fn remove(&mut self, i: usize) -> bool
pub fn remove(&mut self, i: usize) -> bool
Removes a bit at the given index, setting it to false.
Returns true if the bit was previously set (removal occurred),
or false if it was already unset.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(10);
assert!(bitmap.remove(10)); // Bit was set
assert!(!bitmap.remove(10)); // Already unsetSourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all bits in the bitmap.
This is equivalent to creating a new empty bitmap but it keeps the existing allocation if possible.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(100);
bitmap.insert(200);
bitmap.clear();
assert!(!bitmap.get(100));
assert!(bitmap.is_spilled());Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the bitmap as much as possible.
This will remove trailing zero words and potentially move the data back to inline storage if it fits.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(300);
bitmap.remove(300); // Clear the bit
bitmap.shrink_to_fit();
// May move back to inline storage if all set bits fitSourcepub const fn as_slice_rtrim(&self) -> &[u64]
pub const fn as_slice_rtrim(&self) -> &[u64]
Returns a trimmed slice of the bitmap, removing any trailing zero words.
This method provides a view of the bitmap’s underlying storage without trailing zeros, which can be useful for operations that require a compact representation of the set bits.
§Returns
A slice of type &[u64] representing the bitmap’s storage without
trailing zeros.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(64);
let trimmed_slice = bitmap.as_slice_rtrim();
assert_eq!(trimmed_slice.len(), 2); // Only two words are neededSourcepub fn eq_rtrim(&self, b: &[u64]) -> bool
pub fn eq_rtrim(&self, b: &[u64]) -> bool
Compares two slices for equivalency, ignoring trailing zero words.
This function checks if two slices are equivalent by comparing their elements, ignoring any trailing zero words. This is useful for comparing bitmaps where trailing zeros do not affect the logical equivalence of the data.
§Arguments
a- The first slice to compare.b- The second slice to compare.
§Returns
true if the slices are equivalent when trailing zeros are ignored,
false otherwise.
§Examples
use smol_bitmap::SmolBitmap;
let a = SmolBitmap::from(&[1u64, 2, 0, 0]);
let b = &[1u64, 2];
assert!(a.eq_rtrim(b));
let c = &[1u64, 2, 3];
assert!(!a.eq_rtrim(c));Sourcepub fn cmp_rtrim(&self, b: &[u64]) -> Ordering
pub fn cmp_rtrim(&self, b: &[u64]) -> Ordering
Compares two slices for ordering, ignoring trailing zero words.
This function compares two slices by their elements, ignoring any trailing zero words. This is useful for comparing bitmaps where trailing zeros do not affect the logical ordering of the data.
§Arguments
a- The first slice to compare.b- The second slice to compare.
§Returns
An Ordering value that indicates whether a is less than, equal to,
or greater than b when trailing zeros are ignored.
§Examples
use smol_bitmap::SmolBitmap;
let a = SmolBitmap::from(&[1u64, 2, 0, 0]);
let b = &[1u64, 2];
assert_eq!(a.cmp_rtrim(b), core::cmp::Ordering::Equal);
let c = &[1u64, 2, 3];
assert_eq!(a.cmp_rtrim(c), core::cmp::Ordering::Less);Sourcepub fn remove_prefix(&mut self, bit: bool) -> usize
pub fn remove_prefix(&mut self, bit: bool) -> usize
Removes a prefix of consecutive bits with the specified value.
Efficiently removes all consecutive bits of the given value from the beginning of the bitmap by shifting the remaining bits left. This is useful for maintaining compact bitmap representations.
§Algorithm
- Skip whole words that match the pattern (all 0s or all 1s)
- Find consecutive bits in the first non-matching word
- Shift all remaining bits left by the total skip amount
§Arguments
bit- The bit value to remove from the prefix (truefor 1s,falsefor 0s)
§Returns
The number of bits removed from the prefix.
§Examples
let mut bitmap = SmolBitmap::new();
// Set bits: 111100101
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(2);
bitmap.insert(3);
bitmap.insert(5);
bitmap.insert(7);
// Remove prefix of 1s
let removed = bitmap.remove_prefix(true);
assert_eq!(removed, 4); // Removed four leading 1s
// Bitmap is now: 0101 (shifted left)Sourcepub const fn iter(&self) -> Iter<'_>
pub const fn iter(&self) -> Iter<'_>
Returns an iterator over the set bits.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(10);
bitmap.insert(20);
let mut iter = bitmap.iter();
assert_eq!(iter.next(), Some(10));
assert_eq!(iter.next(), Some(20));
assert_eq!(iter.next(), None);Sourcepub fn select<I: IntoIterator>(&self, iter: I) -> SelectIter<'_, I::IntoIter> ⓘ
pub fn select<I: IntoIterator>(&self, iter: I) -> SelectIter<'_, I::IntoIter> ⓘ
Creates an iterator that selects elements from the provided iterator based on set bits.
This method returns a SelectIter that filters the input iterator,
yielding only elements at positions corresponding to set bits in the
bitmap. This is useful for creating a subset of a collection based on
a bitmap mask.
§Arguments
iter- An iterator whose elements will be selected based on set bits
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(2);
bitmap.insert(4);
let items = vec!["a", "b", "c", "d", "e"];
let selected: Vec<_> = bitmap.select(items.iter().copied()).collect();
assert_eq!(selected, vec!["a", "c", "e"]);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of set bits in the bitmap.
Time complexity: O(n) where n is the number of words.
§Examples
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.len(), 0);
bitmap.insert(10);
bitmap.insert(20);
assert_eq!(bitmap.len(), 2);Sourcepub fn rank(&self, i: usize) -> usize
pub fn rank(&self, i: usize) -> usize
Returns the number of set bits below the given index.
Counts all set bits at positions less than idx. This is useful for
determining the position of an element in a compressed representation.
Time complexity: O(w) where w is the number of words up to the target index.
§Arguments
idx- The index to count below (exclusive upper bound)
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
bitmap.insert(15);
bitmap.insert(20);
assert_eq!(bitmap.rank(0), 0); // No bits below index 0
assert_eq!(bitmap.rank(6), 1); // Bit at index 5
assert_eq!(bitmap.rank(12), 2); // Bits at indices 5, 10
assert_eq!(bitmap.rank(25), 4); // All bits below index 25Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the bitmap contains no set bits.
Time complexity: O(n) where n is the number of words.
§Examples
let mut bitmap = SmolBitmap::new();
assert!(bitmap.is_empty());
bitmap.insert(10);
assert!(!bitmap.is_empty());Sourcepub fn first(&self) -> Option<usize>
pub fn first(&self) -> Option<usize>
Returns the minimum (first) set bit in the bitmap, or None if the
bitmap is empty.
Time complexity: O(n) where n is the number of words.
§Examples
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.first(), None);
bitmap.insert(10);
bitmap.insert(5);
bitmap.insert(20);
assert_eq!(bitmap.first(), Some(5));Sourcepub fn last(&self) -> Option<usize>
pub fn last(&self) -> Option<usize>
Returns the maximum (last) set bit in the bitmap, or None if the
bitmap is empty.
Returns None if the bitmap is empty.
Time complexity: O(n) where n is the number of words.
§Examples
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.last(), None);
bitmap.insert(10);
bitmap.insert(5);
bitmap.insert(20);
assert_eq!(bitmap.last(), Some(20));Sourcepub fn count_ones(&self) -> usize
pub fn count_ones(&self) -> usize
Counts the number of set bits in the bitmap.
This method efficiently counts all set bits using the built-in
count_ones method on each word.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
bitmap.insert(15);
assert_eq!(bitmap.count_ones(), 3);Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
Counts the number of unset bits up to the capacity of the bitmap.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
let zeros = bitmap.count_zeros();
assert_eq!(zeros, bitmap.capacity() - 2);Sourcepub fn complement(&mut self, i: usize) -> bool
pub fn complement(&mut self, i: usize) -> bool
Complements the bit at the specified index.
If the bit was set, it becomes unset. If it was unset, it becomes set.
§Examples
let mut bitmap = SmolBitmap::new();
assert!(!bitmap.get(5));
bitmap.complement(5);
assert!(bitmap.get(5));
bitmap.complement(5);
assert!(!bitmap.get(5));Sourcepub fn next_set_bit(&self, beg: usize) -> Option<usize>
pub fn next_set_bit(&self, beg: usize) -> Option<usize>
Returns the index of the next set bit at or after the given index.
If no set bit is found at or after the given index, returns None.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
bitmap.insert(15);
assert_eq!(bitmap.next_set_bit(0), Some(5));
assert_eq!(bitmap.next_set_bit(6), Some(10));
assert_eq!(bitmap.next_set_bit(11), Some(15));
assert_eq!(bitmap.next_set_bit(16), None);Sourcepub fn prev_set_bit(&self, from: usize) -> Option<usize>
pub fn prev_set_bit(&self, from: usize) -> Option<usize>
Returns the index of the previous set bit at or before the given index.
If no set bit is found at or before the given index, returns None.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
bitmap.insert(15);
assert_eq!(bitmap.prev_set_bit(20), Some(15));
assert_eq!(bitmap.prev_set_bit(14), Some(10));
assert_eq!(bitmap.prev_set_bit(9), Some(5));
assert_eq!(bitmap.prev_set_bit(4), None);Sourcepub fn shl(&self, n: usize) -> Self
pub fn shl(&self, n: usize) -> Self
Shifts all bits left by n positions.
Bits that would be shifted beyond the capacity are lost.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(2);
let shifted = bitmap.shl(2);
assert!(!shifted.get(0));
assert!(!shifted.get(1));
assert!(shifted.get(2));
assert!(shifted.get(3));
assert!(shifted.get(4));Sourcepub fn shr(&self, n: usize) -> Self
pub fn shr(&self, n: usize) -> Self
Shifts all bits right by n positions.
Bits that would be shifted below 0 are lost.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(2);
bitmap.insert(3);
bitmap.insert(4);
let shifted = bitmap.shr(2);
assert!(shifted.get(0));
assert!(shifted.get(1));
assert!(shifted.get(2));
assert!(!shifted.get(3));
assert!(!shifted.get(4));Sourcepub fn leading_zeros(&self) -> Option<usize>
pub fn leading_zeros(&self) -> Option<usize>
Counts the number of leading zero bits (from bit 0 upward).
Returns None if the bitmap is empty.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.leading_zeros(), None);
bitmap.insert(3);
assert_eq!(bitmap.leading_zeros(), Some(3));
bitmap.insert(0);
assert_eq!(bitmap.leading_zeros(), Some(0));Sourcepub fn trailing_zeros(&self) -> Option<usize>
pub fn trailing_zeros(&self) -> Option<usize>
Counts the number of trailing zero bits (from the highest set bit downward).
Returns None if the bitmap is empty.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(3);
bitmap.insert(5);
bitmap.insert(10);
// 4 trailing zeros from bit 10 down to bit 6 (bit 5 is set)
assert_eq!(bitmap.trailing_zeros(), Some(4));Sourcepub fn leading_ones(&self) -> usize
pub fn leading_ones(&self) -> usize
Counts the number of leading one bits (consecutive ones from bit 0).
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(2);
bitmap.insert(4);
assert_eq!(bitmap.leading_ones(), 3);Sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
Counts the number of trailing one bits (consecutive ones from the highest set bit).
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(2);
bitmap.insert(4);
bitmap.insert(5);
bitmap.insert(6);
assert_eq!(bitmap.trailing_ones(), 3);Sourcepub fn find_first_zero(&self) -> Option<usize>
pub fn find_first_zero(&self) -> Option<usize>
Finds the first unset bit in the bitmap.
Returns None if all bits up to capacity are set.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
assert_eq!(bitmap.find_first_zero(), Some(0));
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(3);
assert_eq!(bitmap.find_first_zero(), Some(2));Sourcepub fn find_last_zero(&self) -> Option<usize>
pub fn find_last_zero(&self) -> Option<usize>
Finds the last unset bit in the bitmap.
Returns None if all bits are set or the bitmap is empty.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(2);
bitmap.insert(5);
// The last unset bit before bit 5
assert_eq!(bitmap.find_last_zero(), Some(4));Sourcepub fn find_next_zero(&self, beg: usize) -> Option<usize>
pub fn find_next_zero(&self, beg: usize) -> Option<usize>
Finds the next unset bit at or after the given position.
Returns None if no unset bit is found within capacity.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(3);
assert_eq!(bitmap.find_next_zero(0), Some(2));
assert_eq!(bitmap.find_next_zero(2), Some(2));
assert_eq!(bitmap.find_next_zero(3), Some(4));Sourcepub fn find_prev_zero(&self, from: usize) -> Option<usize>
pub fn find_prev_zero(&self, from: usize) -> Option<usize>
Finds the previous unset bit at or before the given position.
Returns None if no unset bit is found.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(3);
bitmap.insert(5);
assert_eq!(bitmap.find_prev_zero(5), Some(4));
assert_eq!(bitmap.find_prev_zero(3), Some(2));
assert_eq!(bitmap.find_prev_zero(1), Some(0));Sourcepub fn count_ones_range(&self, beg: usize, end: usize) -> usize
pub fn count_ones_range(&self, beg: usize, end: usize) -> usize
Counts the number of set bits in the range [beg, end).
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(3);
bitmap.insert(5);
bitmap.insert(7);
assert_eq!(bitmap.count_ones_range(0, 4), 2); // bits 1 and 3
assert_eq!(bitmap.count_ones_range(2, 8), 3); // bits 3, 5, and 7Sourcepub fn count_zeros_range(&self, beg: usize, end: usize) -> usize
pub fn count_zeros_range(&self, beg: usize, end: usize) -> usize
Counts the number of unset bits in the range [beg, end).
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(3);
assert_eq!(bitmap.count_zeros_range(0, 4), 2); // bits 0 and 2
assert_eq!(bitmap.count_zeros_range(0, 8), 6); // 8 - 2 set bitsSourcepub fn set_all(&mut self)
pub fn set_all(&mut self)
Sets all bits up to the current highest set bit to true.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(5);
bitmap.set_all();
assert!(bitmap.get(0));
assert!(bitmap.get(1));
assert!(bitmap.get(2));
assert!(bitmap.get(3));
assert!(bitmap.get(4));
assert!(bitmap.get(5));Sourcepub fn clear_range(&mut self, beg: usize, end: usize)
pub fn clear_range(&mut self, beg: usize, end: usize)
Clears bits in the range [beg, end).
§Panics
Panics if beg > end.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
for i in 0..10 {
bitmap.insert(i);
}
bitmap.clear_range(3, 7);
assert!(bitmap.get(2));
assert!(!bitmap.get(3));
assert!(!bitmap.get(6));
assert!(bitmap.get(7));Sourcepub fn complement_range(&mut self, beg: usize, end: usize)
pub fn complement_range(&mut self, beg: usize, end: usize)
Complements all bits in the range [beg, end).
§Panics
Panics if beg > end.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(2);
bitmap.insert(4);
bitmap.complement_range(1, 6);
assert!(bitmap.get(1));
assert!(!bitmap.get(2));
assert!(bitmap.get(3));
assert!(!bitmap.get(4));
assert!(bitmap.get(5));Sourcepub fn set_range_value(&mut self, beg: usize, end: usize, value: bool)
pub fn set_range_value(&mut self, beg: usize, end: usize, value: bool)
Sets all bits in the range [beg, end) to the specified value.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.set_range_value(5, 10, true);
assert!(!bitmap.get(4));
assert!(bitmap.get(5));
assert!(bitmap.get(9));
assert!(!bitmap.get(10));
bitmap.set_range_value(7, 9, false);
assert!(bitmap.get(6));
assert!(!bitmap.get(7));
assert!(!bitmap.get(8));
assert!(bitmap.get(9));Sourcepub fn fill(&mut self, value: bool)
pub fn fill(&mut self, value: bool)
Sets all bits up to capacity to the specified value.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(10); // This determines a capacity
bitmap.fill(true);
// All bits from 0 to 10 are now true
for i in 0..=10 {
assert!(bitmap.get(i));
}Sourcepub fn get_range(&self, beg: usize, end: usize) -> u64
pub fn get_range(&self, beg: usize, end: usize) -> u64
Extracts bits in the range [beg, end) as a u64 value.
Returns up to 64 bits. If the range is larger than 64 bits, only the first 64 bits are returned.
§Panics
Panics if beg > end.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(2);
bitmap.insert(3);
// Extract bits 0-4: 1101 in binary = 13 in decimal
assert_eq!(bitmap.get_range(0, 4), 0b1101);Sourcepub fn extract_word(&self, word_idx: usize) -> u64
pub fn extract_word(&self, word_idx: usize) -> u64
Extracts a 64-bit word at the given word index.
This method returns the word at the specified index. For inline storage, if reading the last word (index 1), bit 127 (the MSB) is cleared to hide the storage mode indicator.
§Arguments
word_idx- The word index to extract (0-based)
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(63);
bitmap.insert(64);
bitmap.insert(126);
assert_eq!(bitmap.extract_word(0), (1u64 << 63) | 1);
assert_eq!(bitmap.extract_word(1), (1u64 << 62) | 1); // bit 127 is maskedSourcepub fn put_word(&mut self, word_idx: usize, word: u64)
pub fn put_word(&mut self, word_idx: usize, word: u64)
Inserts a 64-bit word at the given word index.
This method sets the word at the specified index. If the bitmap is using inline storage and bit 127 would be set (MSB of the last word), the bitmap is automatically promoted to heap storage.
§Arguments
word_idx- The word index to write to (0-based)word- The 64-bit word to insert
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
// Set word 0
bitmap.put_word(0, 0xFFFF_FFFF_FFFF_FFFF);
assert!(!bitmap.is_spilled());
// Setting MSB of word 1 causes promotion to heap
bitmap.put_word(1, 0x8000_0000_0000_0000);
assert!(bitmap.is_spilled());Sourcepub fn resize(&mut self, new_bits: usize)
pub fn resize(&mut self, new_bits: usize)
Resizes the bitmap to contain exactly new_bits bits.
If new_bits is greater than the current capacity, the bitmap is
extended with zeros. If new_bits is less than the current
capacity, the bitmap is truncated.
§Arguments
new_bits- The new size in bits
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(50);
bitmap.insert(100);
// Resize to 75 bits - bit 100 will be removed
bitmap.resize(75);
assert!(bitmap.get(50));
assert!(!bitmap.get(100));
// Resize to 200 bits - expands with zeros
bitmap.resize(200);
assert!(bitmap.is_spilled()); // Beyond inline capacitySourcepub fn truncate(&mut self, max_bits: usize)
pub fn truncate(&mut self, max_bits: usize)
Truncates the bitmap to contain at most max_bits bits.
If max_bits is greater than or equal to the current highest set bit,
this has no effect. Otherwise, all bits at positions >= max_bits are
cleared.
This method may also shrink the storage if the truncation allows the bitmap to fit back into inline storage.
§Arguments
max_bits- The maximum number of bits to keep
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(50);
bitmap.insert(100);
bitmap.insert(150);
// Truncate to 120 bits - bit 150 will be removed
bitmap.truncate(120);
assert!(bitmap.get(50));
assert!(bitmap.get(100));
assert!(!bitmap.get(150));
// Further truncation
bitmap.truncate(60);
assert!(bitmap.get(50));
assert!(!bitmap.get(100));Sourcepub fn bitslice(&self, beg: usize, end: usize) -> Self
pub fn bitslice(&self, beg: usize, end: usize) -> Self
Extracts a range of bits as a new bitmap.
The bits in the range [start, end) are copied to a new bitmap,
with bit start becoming bit 0 in the new bitmap.
§Panics
This function will not panic under normal circumstances. The unwrap call in the implementation is safe because the function returns early when the slice would be empty, ensuring there’s always at least one element when we access the last element.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(2);
bitmap.insert(3);
bitmap.insert(5);
let sub = bitmap.bitslice(2, 6);
assert!(sub.get(0)); // bit 2 -> 0
assert!(sub.get(1)); // bit 3 -> 1
assert!(!sub.get(2)); // bit 4 -> 2
assert!(sub.get(3)); // bit 5 -> 3Sourcepub fn skip(&self, beg: usize) -> Self
pub fn skip(&self, beg: usize) -> Self
Extracts bits from the given position to the end as a new bitmap.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(2);
bitmap.insert(3);
bitmap.insert(5);
let sub = bitmap.skip(2);
assert!(sub.get(0)); // bit 2 -> 0
assert!(sub.get(1)); // bit 3 -> 1
assert!(sub.get(3)); // bit 5 -> 3Sourcepub fn take(&self, end: usize) -> Self
pub fn take(&self, end: usize) -> Self
Extracts bits from the beginning up to (but not including) the given position.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(3);
bitmap.insert(5);
let sub = bitmap.take(4);
assert!(sub.get(1));
assert!(sub.get(3));
assert!(!sub.get(5)); // Not includedSource§impl SmolBitmap
impl SmolBitmap
Sourcepub const fn to_ne_bytes(&self) -> &[u8] ⓘ
pub const fn to_ne_bytes(&self) -> &[u8] ⓘ
Converts the bitmap to a byte slice in native-endian order.
This method provides a view of the bitmap’s internal representation as a slice of bytes. The byte order is native-endian, meaning it matches the endianness of the host system.
§Safety
The returned slice is valid as long as the bitmap is not modified. Modifying the bitmap while holding a reference to this slice may lead to undefined behavior.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
let bytes = bitmap.to_ne_bytes();
assert_eq!(bytes[0], 0b00000001);Sourcepub fn from_ne_bytes(bytes: &[u8]) -> (Self, &[u8])
pub fn from_ne_bytes(bytes: &[u8]) -> (Self, &[u8])
Constructs a bitmap from a byte slice in native-endian order.
This method reads bytes in native-endian order and constructs a
SmolBitmap from them. Any remaining bytes that do not form a
complete word are returned as a separate slice.
§Safety
The input byte slice must be properly aligned and sized to match the internal representation of the bitmap. Misalignment or incorrect sizing may lead to undefined behavior.
Source§impl SmolBitmap
impl SmolBitmap
Sourcepub const fn to_le_bytes(&self) -> Cow<'_, [u8]>
pub const fn to_le_bytes(&self) -> Cow<'_, [u8]>
Converts the bitmap to little-endian bytes.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(7);
bitmap.insert(8);
let bytes = bitmap.to_le_bytes();
assert_eq!(bytes[0], 0b10000001); // bits 0 and 7
assert_eq!(bytes[1], 0b00000001); // bit 8Sourcepub fn from_le_bytes(bytes: &[u8]) -> (Self, &[u8])
pub fn from_le_bytes(bytes: &[u8]) -> (Self, &[u8])
Constructs a bitmap from little-endian bytes.
§Examples
use smol_bitmap::SmolBitmap;
let bytes = [0b10000001, 0b00000001, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
let (bitmap, _rest) = SmolBitmap::from_le_bytes(&bytes);
assert!(bitmap.get(0)); // bit 0 from byte 0
assert!(bitmap.get(7)); // bit 7 from byte 0
assert!(bitmap.get(8)); // bit 0 from byte 1Sourcepub fn to_be_bytes(&self) -> Cow<'_, [u8]>
pub fn to_be_bytes(&self) -> Cow<'_, [u8]>
Converts the bitmap to big-endian bytes.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(7);
let bytes = bitmap.to_be_bytes();
// Big-endian representationSourcepub fn from_be_bytes(bytes: &[u8]) -> (Self, &[u8])
pub fn from_be_bytes(bytes: &[u8]) -> (Self, &[u8])
Constructs a bitmap from big-endian bytes.
§Examples
use smol_bitmap::SmolBitmap;
let bytes = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00]; // Big-endian representation
let (bitmap, _rest) = SmolBitmap::from_be_bytes(&bytes);
assert!(bitmap.get(8)); // The high bit of the first byteSource§impl SmolBitmap
impl SmolBitmap
Sourcepub fn contains(&self, index: usize) -> bool
pub fn contains(&self, index: usize) -> bool
Checks if the bit at the specified index is set.
This is an alias for the SmolBitmap::get method.
§Arguments
index- The index of the bit to check.
§Returns
Returns true if the bit at the specified index is set, otherwise
false.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(10);
assert!(bitmap.contains(10));
assert!(!bitmap.contains(5));Sourcepub fn pop_first(&mut self) -> Option<usize>
pub fn pop_first(&mut self) -> Option<usize>
Removes and returns the index of the first set bit in the bitmap, or
None if the bitmap is empty.
This operation modifies the bitmap by clearing the first set bit.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
assert_eq!(bitmap.pop_first(), Some(5));
assert_eq!(bitmap.pop_first(), Some(10));
assert_eq!(bitmap.pop_first(), None);Sourcepub fn pop_last(&mut self) -> Option<usize>
pub fn pop_last(&mut self) -> Option<usize>
Removes and returns the index of the last set bit in the bitmap, or
None if the bitmap is empty.
This operation modifies the bitmap by clearing the last set bit.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
bitmap.insert(10);
assert_eq!(bitmap.pop_last(), Some(10));
assert_eq!(bitmap.pop_last(), Some(5));
assert_eq!(bitmap.pop_last(), None);Sourcepub fn cardinality(&self) -> usize
pub fn cardinality(&self) -> usize
Returns the number of set bits in the bitmap.
This is an alias for the SmolBitmap::count_ones method.
§Examples
let mut bitmap = SmolBitmap::new();
bitmap.insert(10);
bitmap.insert(20);
assert_eq!(bitmap.cardinality(), 2);Sourcepub fn add(&mut self, index: usize) -> bool
pub fn add(&mut self, index: usize) -> bool
Inserts a bit at the given index, setting it to true.
Returns true if the bit was previously unset (insertion occurred),
or false if it was already set.
This is an alias for the SmolBitmap::insert method.
§Examples
let mut bitmap = SmolBitmap::new();
assert!(bitmap.insert(10)); // First insertion
assert!(!bitmap.insert(10)); // Already setSourcepub fn union_with(&mut self, other: impl AsRef<[u64]>)
pub fn union_with(&mut self, other: impl AsRef<[u64]>)
Performs a bitwise OR operation with another bitmap.
This sets each bit in self to self[i] | other[i]. If other
is larger, the bitmap is extended to accommodate all bits.
§Examples
let mut a = SmolBitmap::new();
let mut b = SmolBitmap::new();
a.insert(10);
b.insert(20);
a.union_with(&b);
assert!(a.get(10));
assert!(a.get(20));Sourcepub fn union(&self, other: impl AsRef<[u64]>) -> Self
pub fn union(&self, other: impl AsRef<[u64]>) -> Self
Creates a new set that is the union of this set and another set.
The union contains elements present in either this set or the other set.
Time complexity: O(n) where n is the max number of bytes in either set.
Sourcepub fn intersection_with(&mut self, other: impl AsRef<[u64]>)
pub fn intersection_with(&mut self, other: impl AsRef<[u64]>)
Performs a bitwise AND operation with another bitmap.
This sets each bit in self to self[i] & other[i]. The result
is truncated to the length of the shorter bitmap. Trailing zeros
are removed.
§Examples
let mut a = SmolBitmap::new();
let mut b = SmolBitmap::new();
a.insert(10);
a.insert(20);
b.insert(10);
a.intersection_with(&b);
assert!(a.get(10));
assert!(!a.get(20));Sourcepub fn intersection(&self, other: impl AsRef<[u64]>) -> Self
pub fn intersection(&self, other: impl AsRef<[u64]>) -> Self
Creates a new set that is the intersection of this set and another set.
The intersection contains only elements present in both sets.
Time complexity: O(n) where n is the min number of bytes in either set.
Sourcepub fn difference(&self, other: impl AsRef<[u64]>) -> Self
pub fn difference(&self, other: impl AsRef<[u64]>) -> Self
Creates a new set that is the difference of this set and another set.
The difference contains elements present in this set but not in the other set.
Time complexity: O(n) where n is the number of bytes in self.
Sourcepub fn difference_with(&mut self, other: impl AsRef<[u64]>)
pub fn difference_with(&mut self, other: impl AsRef<[u64]>)
Updates this set to be the difference of itself and another set.
Time complexity: O(n) where n is the min number of bytes in either set.
Sourcepub fn symmetric_difference(&self, other: impl AsRef<[u64]>) -> Self
pub fn symmetric_difference(&self, other: impl AsRef<[u64]>) -> Self
Creates a new set that is the symmetric difference of this set and another set.
The symmetric difference contains elements present in either this set or the other set, but not in both.
Time complexity: O(n) where n is the max number of words in either set.
Sourcepub fn symmetric_difference_with(&mut self, other: impl AsRef<[u64]>)
pub fn symmetric_difference_with(&mut self, other: impl AsRef<[u64]>)
Updates this set to be the symmetric difference of itself and another set.
Time complexity: O(n) where n is the max number of words in either set.
Sourcepub fn is_subset(&self, other: impl AsRef<[u64]>) -> bool
pub fn is_subset(&self, other: impl AsRef<[u64]>) -> bool
Checks if this set is a subset of another set.
A set is a subset if all its elements are contained in the other set.
Time complexity: O(n) where n is the number of words in self.
Sourcepub fn is_superset(&self, other: impl AsRef<[u64]>) -> bool
pub fn is_superset(&self, other: impl AsRef<[u64]>) -> bool
Checks if this set is a superset of another set.
A set is a superset if it contains all elements of the other set.
Time complexity: O(n) where n is the number of words in other.
Sourcepub fn is_disjoint(&self, other: impl AsRef<[u64]>) -> bool
pub fn is_disjoint(&self, other: impl AsRef<[u64]>) -> bool
Checks if this set is disjoint from another set.
Two sets are disjoint if they have no elements in common.
Time complexity: O(n) where n is the min number of words in either set.
Sourcepub fn all(&self) -> bool
pub fn all(&self) -> bool
Returns true if all bits up to the maximum set bit are set.
Returns true for an empty bitmap.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
assert!(bitmap.all()); // Empty bitmap
bitmap.insert(0);
bitmap.insert(1);
bitmap.insert(2);
assert!(bitmap.all()); // All bits 0-2 are set
bitmap.insert(4);
assert!(!bitmap.all()); // Bit 3 is not setTrait Implementations§
Source§impl Archive for SmolBitmap
impl Archive for SmolBitmap
Source§type Archived = ArchivedVec<<u64 as Archive>::Archived>
type Archived = ArchivedVec<<u64 as Archive>::Archived>
Source§type Resolver = VecResolver
type Resolver = VecResolver
Source§fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>)
fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>)
Source§const COPY_OPTIMIZATION: CopyOptimization<Self> = _
const COPY_OPTIMIZATION: CopyOptimization<Self> = _
serialize. Read moreSource§impl AsRef<[u64]> for SmolBitmap
impl AsRef<[u64]> for SmolBitmap
Source§impl Binary for SmolBitmap
impl Binary for SmolBitmap
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the bitmap as a binary string representation.
The binary representation shows bits from least significant (rightmost)
to most significant (leftmost), with the 0b prefix. Trailing zeros
are omitted for compact representation.
§Examples
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(0);
bitmap.insert(2);
bitmap.insert(65);
// Shows compact binary representation
println!("{:#b}", bitmap); // 0b10000000000000000000000000000000000000000000000000000000000000000101Source§impl BitAnd for &SmolBitmap
impl BitAnd for &SmolBitmap
Source§impl BitAnd for SmolBitmap
impl BitAnd for SmolBitmap
Source§fn bitand(self, rhs: Self) -> Self::Output
fn bitand(self, rhs: Self) -> Self::Output
Performs bitwise AND operation between two bitmaps.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(1);
let mut b = SmolBitmap::new();
b.insert(1);
b.insert(2);
let c = a & b;
assert!(c.get(1)); // Only bit 1 is set in both
assert!(!c.get(0));
assert!(!c.get(2));Source§type Output = SmolBitmap
type Output = SmolBitmap
& operator.Source§impl BitAndAssign<&SmolBitmap> for SmolBitmap
impl BitAndAssign<&SmolBitmap> for SmolBitmap
Source§fn bitand_assign(&mut self, rhs: &Self)
fn bitand_assign(&mut self, rhs: &Self)
&= operation. Read moreSource§impl BitAndAssign for SmolBitmap
impl BitAndAssign for SmolBitmap
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
Performs in-place bitwise AND operation.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(1);
let mut b = SmolBitmap::new();
b.insert(1);
b.insert(2);
a &= b;
assert!(a.get(1)); // Only bit 1 is set in both
assert!(!a.get(0));Source§impl BitOr for &SmolBitmap
impl BitOr for &SmolBitmap
Source§impl BitOr for SmolBitmap
impl BitOr for SmolBitmap
Source§fn bitor(self, rhs: Self) -> Self::Output
fn bitor(self, rhs: Self) -> Self::Output
Performs bitwise OR operation between two bitmaps.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(1);
let mut b = SmolBitmap::new();
b.insert(1);
b.insert(2);
let c = a | b;
assert!(c.get(0));
assert!(c.get(1));
assert!(c.get(2));Source§type Output = SmolBitmap
type Output = SmolBitmap
| operator.Source§impl BitOrAssign<&SmolBitmap> for SmolBitmap
impl BitOrAssign<&SmolBitmap> for SmolBitmap
Source§fn bitor_assign(&mut self, rhs: &Self)
fn bitor_assign(&mut self, rhs: &Self)
|= operation. Read moreSource§impl BitOrAssign for SmolBitmap
impl BitOrAssign for SmolBitmap
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Performs in-place bitwise OR operation.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
let mut b = SmolBitmap::new();
b.insert(1);
a |= b;
assert!(a.get(0));
assert!(a.get(1));Source§impl BitXor for &SmolBitmap
impl BitXor for &SmolBitmap
Source§impl BitXor for SmolBitmap
impl BitXor for SmolBitmap
Source§fn bitxor(self, rhs: Self) -> Self::Output
fn bitxor(self, rhs: Self) -> Self::Output
Performs bitwise XOR operation between two bitmaps.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(1);
let mut b = SmolBitmap::new();
b.insert(1);
b.insert(2);
let c = a ^ b;
assert!(c.get(0)); // Only in a
assert!(!c.get(1)); // In both, so not in XOR
assert!(c.get(2)); // Only in bSource§type Output = SmolBitmap
type Output = SmolBitmap
^ operator.Source§impl BitXorAssign<&SmolBitmap> for SmolBitmap
impl BitXorAssign<&SmolBitmap> for SmolBitmap
Source§fn bitxor_assign(&mut self, rhs: &Self)
fn bitxor_assign(&mut self, rhs: &Self)
^= operation. Read moreSource§impl BitXorAssign for SmolBitmap
impl BitXorAssign for SmolBitmap
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
Performs in-place bitwise XOR operation.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(1);
let mut b = SmolBitmap::new();
b.insert(1);
b.insert(2);
a ^= b;
assert!(a.get(0)); // Only in original a
assert!(!a.get(1)); // In both, so removed
assert!(a.get(2)); // Only in bSource§impl Borrow<[u64]> for SmolBitmap
impl Borrow<[u64]> for SmolBitmap
Source§impl Clone for SmolBitmap
impl Clone for SmolBitmap
Source§impl Debug for SmolBitmap
impl Debug for SmolBitmap
Source§impl Default for SmolBitmap
impl Default for SmolBitmap
Source§impl<'de> Deserialize<'de> for SmolBitmap
impl<'de> Deserialize<'de> for SmolBitmap
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize a SmolBitmap using little-endian byte order.
§Errors
Returns an error if deserialization fails.
Source§impl<D: Fallible + ?Sized> Deserialize<SmolBitmap, D> for ArchivedSmolBitmap
impl<D: Fallible + ?Sized> Deserialize<SmolBitmap, D> for ArchivedSmolBitmap
Source§fn deserialize(&self, _deserializer: &mut D) -> Result<SmolBitmap, D::Error>
fn deserialize(&self, _deserializer: &mut D) -> Result<SmolBitmap, D::Error>
Source§impl Display for SmolBitmap
impl Display for SmolBitmap
Source§impl Drop for SmolBitmap
impl Drop for SmolBitmap
Source§impl Extend<usize> for SmolBitmap
impl Extend<usize> for SmolBitmap
Source§fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl From<&[u64]> for SmolBitmap
impl From<&[u64]> for SmolBitmap
Source§impl From<SmolBitmap> for SmolBitmapBuilder
impl From<SmolBitmap> for SmolBitmapBuilder
Source§fn from(bitmap: SmolBitmap) -> Self
fn from(bitmap: SmolBitmap) -> Self
Source§impl From<SmolBitmap> for Vec<u64>
impl From<SmolBitmap> for Vec<u64>
Source§fn from(bitmap: SmolBitmap) -> Self
fn from(bitmap: SmolBitmap) -> Self
Source§impl From<SmolBitmapBuilder> for SmolBitmap
impl From<SmolBitmapBuilder> for SmolBitmap
Source§fn from(builder: SmolBitmapBuilder) -> Self
fn from(builder: SmolBitmapBuilder) -> Self
Source§impl From<u128> for SmolBitmap
impl From<u128> for SmolBitmap
Source§impl From<u16> for SmolBitmap
impl From<u16> for SmolBitmap
Source§impl From<u32> for SmolBitmap
impl From<u32> for SmolBitmap
Source§impl From<u64> for SmolBitmap
impl From<u64> for SmolBitmap
Source§impl From<u8> for SmolBitmap
impl From<u8> for SmolBitmap
Source§impl FromIterator<usize> for SmolBitmap
impl FromIterator<usize> for SmolBitmap
Source§impl FromStr for SmolBitmap
impl FromStr for SmolBitmap
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses a binary string representation into a SmolBitmap.
The input string can optionally start with 0b and contain only ‘0’ and
‘1’ characters. The string is interpreted with the least significant bit
on the right.
§Errors
Returns a ParseBitmapError if the string contains invalid characters
or cannot be parsed as binary.
§Examples
use core::str::FromStr;
use smol_bitmap::SmolBitmap;
let bitmap = SmolBitmap::from_str("0b101")?;
assert!(bitmap.get(0)); // bit 0 set
assert!(!bitmap.get(1)); // bit 1 unset
assert!(bitmap.get(2)); // bit 2 set
Source§type Err = ParseBitmapError
type Err = ParseBitmapError
Source§impl Hash for SmolBitmap
impl Hash for SmolBitmap
Source§impl Index<usize> for SmolBitmap
impl Index<usize> for SmolBitmap
Source§fn index(&self, index: usize) -> &Self::Output
fn index(&self, index: usize) -> &Self::Output
Returns a reference to a static bool representing the bit at the given index.
Note: Since we can’t return a reference to a bit directly, this returns
a reference to a static true or false value based on the bit state.
Returns false for indices beyond the capacity, consistent with
get().
§Examples
use core::ops::Index;
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(5);
assert_eq!(*bitmap.index(5), true);
assert_eq!(*bitmap.index(0), false);
assert_eq!(*bitmap.index(1000), false); // Out of bounds returns falseSource§impl<'a> IntoIterator for &'a SmolBitmap
impl<'a> IntoIterator for &'a SmolBitmap
Source§impl IntoIterator for SmolBitmap
impl IntoIterator for SmolBitmap
Source§impl LowerHex for SmolBitmap
impl LowerHex for SmolBitmap
Source§impl Not for &SmolBitmap
impl Not for &SmolBitmap
Source§impl Not for SmolBitmap
impl Not for SmolBitmap
Source§fn not(self) -> Self::Output
fn not(self) -> Self::Output
Returns the bitwise complement of the bitmap up to its capacity.
§Examples
use smol_bitmap::SmolBitmap;
let mut a = SmolBitmap::new();
a.insert(0);
a.insert(2);
let b = !a.clone();
assert!(!b.get(0));
assert!(b.get(1));
assert!(!b.get(2));Source§type Output = SmolBitmap
type Output = SmolBitmap
! operator.Source§impl Octal for SmolBitmap
impl Octal for SmolBitmap
Source§impl Ord for SmolBitmap
impl Ord for SmolBitmap
Source§impl PartialEq for SmolBitmap
impl PartialEq for SmolBitmap
Source§impl PartialOrd for SmolBitmap
impl PartialOrd for SmolBitmap
Source§impl Serialize for SmolBitmap
impl Serialize for SmolBitmap
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize the SmolBitmap using little-endian byte order.
§Errors
Returns an error if serialization fails.
Source§impl TryFrom<&SmolBitmap> for u128
impl TryFrom<&SmolBitmap> for u128
Source§impl TryFrom<&SmolBitmap> for u16
impl TryFrom<&SmolBitmap> for u16
Source§impl TryFrom<&SmolBitmap> for u32
impl TryFrom<&SmolBitmap> for u32
Source§impl TryFrom<&SmolBitmap> for u64
impl TryFrom<&SmolBitmap> for u64
Source§impl TryFrom<&SmolBitmap> for u8
impl TryFrom<&SmolBitmap> for u8
Source§fn try_from(bitmap: &SmolBitmap) -> Result<Self, Self::Error>
fn try_from(bitmap: &SmolBitmap) -> Result<Self, Self::Error>
Attempts to convert a bitmap to an 8-bit unsigned integer.
§Errors
Returns an error if the bitmap has bits set beyond position 7.
§Examples
use core::convert::TryFrom;
use smol_bitmap::SmolBitmap;
let mut bitmap = SmolBitmap::new();
bitmap.insert(1);
bitmap.insert(3);
let value = u8::try_from(&bitmap).unwrap();
assert_eq!(value, 0b00001010);Source§type Error = TryFromBitmapError
type Error = TryFromBitmapError
Source§impl UpperHex for SmolBitmap
impl UpperHex for SmolBitmap
impl Eq for SmolBitmap
Auto Trait Implementations§
impl Freeze for SmolBitmap
impl RefUnwindSafe for SmolBitmap
impl Send for SmolBitmap
impl Sync for SmolBitmap
impl Unpin for SmolBitmap
impl UnwindSafe for SmolBitmap
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
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§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.