Giter VIP home page Giter VIP logo

bm's Introduction

bm

bm provides simple bitmaps for Rust.

Quick start

Add bm to Cargo.toml:

[dependencies]
bm = { git = "https://github.com/reynoldsbd/bm" }

(bm does not depend on std or any other crate)

The Bitmap trait is implemented for several types and slice-types. For instance, you can treat any byte-slice as a bitmap just by importing Bitmap:

use bm::Bitmap;
let mut bitmap = &[0u8; 10][..];

// Check how many bits are available
assert_eq!(80, bitmap.bit_count());

// Retrieve the value of bit 0
assert_eq!(false, bitmap.get_bit(0));

// Set the value of bit 5
bitmap.set_bit(5, true);
assert_eq!(true, bitmap.get_bit(5));

The AtomicBitmap trait defines a Sync-friendly bitmap API, and this crate provides a lock-free implementation of AtomicBitmap for AtomicUsize and slices thereof:

use bm::AtomicBitmap;
use std::sync::atomic::{ AtomicUsize, Ordering };
let bitmap = AtomicUsize::new(0); // No mut needed!

// Atomically load the value of a bit
assert_eq!(false, bitmap.load_bit(0, Ordering::Acquire));

// Atomically set the value of the bit
if !bitmap.compare_and_swap(5, false, true, Ordering::Release) {
    // Return value was the same as the `current` parameter, meaning the swap was successful
    assert_eq!(true, bitmap.load_bit(5));
} else {
    // Failed to swap; `current` was not correct value
    println!("Oh no!");
}

bm's People

Contributors

reynoldsbd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.