Skip to content

Commit

Permalink
Merge pull request #8 from dewert99/bitset-insert
Browse files Browse the repository at this point in the history
Fixed return value of `BitSet::insert` to match `HashSet`
  • Loading branch information
dewert99 authored May 30, 2024
2 parents 4c1cd1b + 9706a37 commit 3457c7c
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/raw/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<T: Default> DefaultVec<T> {

type Elt = u32;

/// Set of indexes
#[derive(Default, Clone)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
pub struct BitSet(DefaultVec<Elt>);
Expand All @@ -56,18 +57,36 @@ fn split(x: usize) -> (usize, Elt) {
(x / Elt::BITS as usize, 1 << offset)
}

/// Set of indexes
impl BitSet {
/// Adds an element to the set
/// If the set did not have this value present, true is returned.
/// If the set did have this value present, false is returned.
///
/// ```rust
/// use plat_egg::raw::bitset::BitSet;
/// let mut s = BitSet::default();
/// assert!(s.insert(0));
/// assert!(!s.insert(0));
/// ```
pub fn insert(&mut self, x: usize) -> bool {
let (chunk_idx, mask) = split(x);
let chunk = self.0.get_mut(chunk_idx);
let res = (*chunk & mask) != 0;
let res = (*chunk & mask) == 0;
*chunk |= mask;
res
}

/// Removes an element form the set
/// Returns whether the value was present in the set.
///
/// ```rust
/// use plat_egg::raw::bitset::BitSet;
/// let mut s = BitSet::default();
/// assert!(!s.remove(0));
/// s.insert(0);
/// assert!(s.remove(0));
/// assert!(!s.remove(0))
/// ```
pub fn remove(&mut self, x: usize) -> bool {
let (chunk_idx, mask) = split(x);
let chunk = self.0.get_mut(chunk_idx);
Expand Down

0 comments on commit 3457c7c

Please sign in to comment.