Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "bmap" -version = "0.2.0" +version = "0.2.1" edition = "2021" license = "0BSD" # https://crates.io/category_slugs categories = [ "data-structures" ] keywords = [ "bitmap", "bitvec" ] Index: src/lib.rs ================================================================== --- src/lib.rs +++ src/lib.rs @@ -1,7 +1,9 @@ //! Container of bits adressable by an index. +use std::fmt; + pub mod err; pub use err::Error; /// Number of bits per slot in the bits vector. @@ -17,10 +19,31 @@ /// Number of remaining bits. This is functionally equivalent to "total /// number of zeroes in the container". remain: usize } + +impl fmt::Debug for CountedBitmap { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let blocks: Vec = self + .iter_ones_block() + .map(|(first, last)| { + if first != last { + format!("{}-{}", first, last) + } else { + format!("{}", first) + } + }) + .collect(); + let set_blocks = blocks.join(","); + write!( + f, + "BMap {{ length: {}, remain: {}, [{}] }}", + self.length, self.remain, set_blocks + ) + } +} impl CountedBitmap { /// Create a new bit vector which can hold `bitcount` number of bits. /// @@ -550,8 +573,45 @@ let mut bmap = CountedBitmap::new(1); assert!(!bmap.is_finished()); bmap.set(0).unwrap(); assert!(bmap.is_finished()); } + + #[test] + fn dbg_output0() { + let bmap = CountedBitmap::new(3); + let s = format!("{:?}", bmap); + assert_eq!(s, "BMap { length: 3, remain: 3, [] }"); + } + + #[test] + fn dbg_output1() { + let mut bmap = CountedBitmap::new(3); + bmap.set(1).unwrap(); + let s = format!("{:?}", bmap); + assert_eq!(s, "BMap { length: 3, remain: 2, [1] }"); + } + + #[test] + fn dbg_output2() { + let mut bmap = CountedBitmap::new(5); + bmap.set(1).unwrap(); + bmap.set(2).unwrap(); + let s = format!("{:?}", bmap); + assert_eq!(s, "BMap { length: 5, remain: 3, [1-2] }"); + } + + #[test] + fn dbg_output3() { + let mut bmap = CountedBitmap::new(7); + bmap.set(1).unwrap(); + bmap.set(2).unwrap(); + + bmap.set(4).unwrap(); + bmap.set(5).unwrap(); + + let s = format!("{:?}", bmap); + assert_eq!(s, "BMap { length: 7, remain: 3, [1-2,4-5] }"); + } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -2,23 +2,43 @@ ⚠️ indicates a breaking change. ## [Unreleased] -[Details](/vdiff?from=bmap-0.1.0&to=trunk) +[Details](/vdiff?from=bmap-0.2.1&to=trunk) + +### Added + +### Changed + +### Removed + +--- + +## [0.2.1] - 2024-03-21 + +[Details](/vdiff?from=bmap-0.2.0&to=bmap-0.2.1) + +### Changed + +- Implement `Debug` for `CountedBitmap`. + +--- + +## [0.2.0] - 2024-03-21 + +[Details](/vdiff?from=bmap-0.1.0&to=bmap-0.2.0) ### Added - `CountedBitmap::is_empty()` returns `true` is no bits have been set in the - bit map. + bit map. ### Changed - ⚠️ `CountedBitmap::new()` made infallible. -### Removed - --- ## [0.1.0] Initial release.