From 5a54e7fb1ec526fc08398bfaffd0487d96391144 Mon Sep 17 00:00:00 2001 From: hky1999 <976929993@qq.com> Date: Tue, 10 Dec 2024 19:52:17 +0800 Subject: [PATCH 1/2] [fix] modify readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4c220bc..bbe9ba9 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,5 @@ [![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. From 81bae8e44413137f8945e4e081899ead680e6c7e Mon Sep 17 00:00:00 2001 From: hky1999 <976929993@qq.com> Date: Tue, 10 Dec 2024 20:04:10 +0800 Subject: [PATCH 2/2] [feat] add examples --- README.md | 19 +++++++++++++++++++ src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/README.md b/README.md index bbe9ba9..79192e2 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,22 @@ [![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) 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);