Skip to content

Commit

Permalink
Add serde, fn get_mut, fn truncate, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pczarn committed Jul 16, 2024
1 parent f1c913a commit ed529d5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: cargo test --verbosee
- name: Run tests for serde feature
run: cargo test --features serde --verbose

miri:
name: "Miri"
Expand Down
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ documentation = "https://docs.rs/bit-set/"
keywords = ["data-structures", "bitset"]
readme = "README.md"

[dev-dependencies]
rand = "0.8"
[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }

[dependencies.bit-vec]
version = "0.7.0"
default-features = false

[dev-dependencies]
rand = "0.8"
serde_json = "1.0"

[features]
default = ["std"]
std = ["bit-vec/std"]
serde = ["dep:serde", "bit-vec/serde"]
bench = []
10 changes: 10 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Version 0.7.0 (not yet released)
==========================

<a id="v0.7.0"></a>

- `serde::Serialize`, `Deserialize` is derived under the `serde` optional feature
- `impl Display` is implemented
- `impl Debug` has different output
- `fn truncate` is implemented
- `fn get_mut` is implemented
79 changes: 76 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#![cfg_attr(feature = "bench", feature(test))]

extern crate bit_vec;
#[cfg(feature = "serde")]
extern crate serde;

#[cfg(test)]
extern crate rand;
#[cfg(feature = "bench")]
Expand Down Expand Up @@ -119,6 +122,7 @@ fn match_words<'a, 'b, B: BitBlock>(
}
}

#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BitSet<B = u32> {
bit_vec: BitVec<B>,
}
Expand Down Expand Up @@ -341,17 +345,42 @@ impl<B: BitBlock> BitSet<B> {
/// ```
/// use bit_set::BitSet;
///
/// let mut s = BitSet::new();
/// s.insert(0);
/// let mut set = BitSet::new();
/// set.insert(0);
///
/// let bv = s.get_ref();
/// let bv = set.get_ref();
/// assert_eq!(bv[0], true);
/// ```
#[inline]
pub fn get_ref(&self) -> &BitVec<B> {
&self.bit_vec
}

/// Returns a mutable reference to the underlying bit vector.
///
/// # Examples
///
/// ```
/// use bit_set::BitSet;
///
/// let mut set = BitSet::new();
/// set.insert(0);
/// set.insert(3);
///
/// {
/// let bv = set.get_mut();
/// bv.set(1, true);
/// }
///
/// assert!(set.contains(0));
/// assert!(set.contains(1));
/// assert!(set.contains(3));
/// ```
#[inline]
pub fn get_mut(&mut self) -> &mut BitVec<B> {
&mut self.bit_vec
}

#[inline]
fn other_op<F>(&mut self, other: &Self, mut f: F)
where
Expand Down Expand Up @@ -840,6 +869,11 @@ impl<B: BitBlock> BitSet<B> {

true
}

/// Excludes `element` and all greater elements from the `BitSet`.
pub fn truncate(&mut self, element: usize) {
self.bit_vec.truncate(element);
}
}

impl<B: BitBlock> fmt::Debug for BitSet<B> {
Expand Down Expand Up @@ -1548,6 +1582,45 @@ mod tests {
assert!(b.contains(1000));
}

#[test]
fn test_truncate() {
let bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];

let mut s = BitSet::from_bytes(&bytes);
s.truncate(5 * 8);

assert_eq!(s, BitSet::from_bytes(&bytes[..5]));
assert_eq!(s.len(), 5 * 8);
s.truncate(4 * 8);
assert_eq!(s, BitSet::from_bytes(&bytes[..4]));
assert_eq!(s.len(), 4 * 8);
// Truncating to a size > s.len() should be a noop
s.truncate(5 * 8);
assert_eq!(s, BitSet::from_bytes(&bytes[..4]));
assert_eq!(s.len(), 4 * 8);
s.truncate(8);
assert_eq!(s, BitSet::from_bytes(&bytes[..1]));
assert_eq!(s.len(), 8);
s.truncate(0);
assert_eq!(s, BitSet::from_bytes(&[]));
assert_eq!(s.len(), 0);
}

#[cfg(feature = "serde")]
#[test]
fn test_serialization() {
let bset: BitSet = BitSet::new();
let serialized = serde_json::to_string(&bset).unwrap();
let unserialized: BitSet = serde_json::from_str(&serialized).unwrap();
assert_eq!(bset, unserialized);

let elems: Vec<usize> = vec![11, 42, 100, 101];
let bset: BitSet = elems.iter().map(|n| *n).collect();
let serialized = serde_json::to_string(&bset).unwrap();
let unserialized = serde_json::from_str(&serialized).unwrap();
assert_eq!(bset, unserialized);
}

/*
#[test]
fn test_bit_set_append() {
Expand Down

0 comments on commit ed529d5

Please sign in to comment.