diff --git a/README.md b/README.md index 4c220bc..79192e2 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,24 @@ [![Crates.io](https://img.shields.io/crates/v/bitmap-allocator)](https://crates.io/crates/bitmap-allocator) [![Docs.rs](https://docs.rs/bitmap-allocator/badge.svg)](https://docs.rs/bitmap-allocator) [![CI](https://github.com/rcore-os/bitmap-allocator/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/rcore-os/bitmap-allocator/actions/workflows/main.yml) -[![Coverage Status](https://coveralls.io/repos/github/rcore-os/bitmap-allocator/badge.svg?branch=master)](https://coveralls.io/github/rcore-os/bitmap-allocator?branch=master) Bit allocator based on segment tree algorithm. + +## Example + +```rust +use bitmap_allocator::{BitAlloc, BitAlloc1M}; + +let mut ba = BitAlloc1M::default(); +ba.insert(0..16); +for i in 0..16 { + assert!(ba.test(i)); +} +ba.remove(2..8); +assert_eq!(ba.alloc(), Some(0)); +assert_eq!(ba.alloc(), Some(1)); +assert_eq!(ba.alloc(), Some(8)); +ba.dealloc(0); +ba.dealloc(1); +ba.dealloc(8); +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 133bd80..7b84907 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,27 @@ pub trait BitAlloc: Default { } /// A bitmap of 256 bits +/// +/// ## Example +/// +/// ```rust +/// use bitmap_allocator::{BitAlloc, BitAlloc256}; +/// +/// let mut ba = BitAlloc256::default(); +/// ba.insert(0..16); +/// for i in 0..16 { +/// assert!(ba.test(i)); +/// } +/// ba.remove(2..8); +/// assert_eq!(ba.alloc(), Some(0)); +/// assert_eq!(ba.alloc(), Some(1)); +/// assert_eq!(ba.alloc(), Some(8)); +/// ba.dealloc(0); +/// ba.dealloc(1); +/// ba.dealloc(8); +/// +/// assert!(!ba.is_empty()); +/// ``` pub type BitAlloc256 = BitAllocCascade16; /// A bitmap of 4K bits pub type BitAlloc4K = BitAllocCascade16; @@ -196,6 +217,28 @@ impl BitAllocCascade16 { /// A bitmap consisting of only 16 bits. /// BitAlloc16 acts as the leaf (except the leaf bits of course) nodes in the segment trees. +/// +/// ## Example +/// +/// ```rust +/// use bitmap_allocator::{BitAlloc, BitAlloc16}; +/// +/// let mut ba = BitAlloc16::default(); +/// assert_eq!(BitAlloc16::CAP, 16); +/// ba.insert(0..16); +/// for i in 0..16 { +/// assert!(ba.test(i)); +/// } +/// ba.remove(2..8); +/// assert_eq!(ba.alloc(), Some(0)); +/// assert_eq!(ba.alloc(), Some(1)); +/// assert_eq!(ba.alloc(), Some(8)); +/// ba.dealloc(0); +/// ba.dealloc(1); +/// ba.dealloc(8); +/// +/// assert!(!ba.is_empty()); +/// ``` #[derive(Default)] pub struct BitAlloc16(u16);