| 1 | <div align="center"> |
| 2 | <h1>bit-vec</h1> |
| 3 | <p> |
| 4 | <strong>A compact vector of bits.</strong> |
| 5 | </p> |
| 6 | <p> |
| 7 | |
| 8 | [![crates.io][crates.io shield]][crates.io link] |
| 9 | [![Documentation][docs.rs badge]][docs.rs link] |
| 10 | ![Rust CI][github ci badge] |
| 11 | [![rustc 1.0+]][Rust 1.0] |
| 12 | [![serde_derive: rustc 1.31+]][Rust 1.31] |
| 13 | <br /> |
| 14 | <br /> |
| 15 | [![Dependency Status][deps.rs status]][deps.rs link] |
| 16 | [![Download Status][shields.io download count]][crates.io link] |
| 17 | |
| 18 | </p> |
| 19 | </div> |
| 20 | |
| 21 | [crates.io shield]: https://img.shields.io/crates/v/bit-vec?label=latest |
| 22 | [crates.io link]: https://crates.io/crates/bit-vec |
| 23 | [docs.rs badge]: https://docs.rs/bit-vec/badge.svg?version=0.8.0 |
| 24 | [docs.rs link]: https://docs.rs/bit-vec/0.8.0/bit_vec/ |
| 25 | [github ci badge]: https://github.com/contain-rs/linked-hash-map/workflows/Rust/badge.svg?branch=master |
| 26 | [rustc 1.0+]: https://img.shields.io/badge/rustc-1.0%2B-blue.svg |
| 27 | [serde_derive: rustc 1.31+]: https://img.shields.io/badge/serde_derive-rustc_1.31+-lightgray.svg |
| 28 | [Rust 1.0]: https://blog.rust-lang.org/2015/05/15/Rust-1.0.html |
| 29 | [Rust 1.31]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html |
| 30 | [deps.rs status]: https://deps.rs/crate/bit-vec/0.8.0/status.svg |
| 31 | [deps.rs link]: https://deps.rs/crate/bit-vec/0.8.0 |
| 32 | [shields.io download count]: https://img.shields.io/crates/d/bit-vec.svg |
| 33 | |
| 34 | ## Usage |
| 35 | |
| 36 | Add this to your Cargo.toml: |
| 37 | |
| 38 | ```toml |
| 39 | [dependencies] |
| 40 | bit-vec = "0.8" |
| 41 | ``` |
| 42 | |
| 43 | Since Rust 2018, `extern crate` is no longer mandatory. If your edition is old (Rust 2015), |
| 44 | add this to your crate root: |
| 45 | |
| 46 | ```rust |
| 47 | extern crate bit_vec; |
| 48 | ``` |
| 49 | |
| 50 | If you want [serde](https://github.com/serde-rs/serde) support, include the feature like this: |
| 51 | |
| 52 | ```toml |
| 53 | [dependencies] |
| 54 | bit-vec = { version = "0.8", features = ["serde"] } |
| 55 | ``` |
| 56 | |
| 57 | If you want to use bit-vec in a program that has `#![no_std]`, just drop default features: |
| 58 | |
| 59 | ```toml |
| 60 | [dependencies] |
| 61 | bit-vec = { version = "0.8", default-features = false } |
| 62 | ``` |
| 63 | |
| 64 | If you want to use serde with the alloc crate instead of std, just use the `serde_no_std` feature: |
| 65 | |
| 66 | ```toml |
| 67 | [dependencies] |
| 68 | bit-vec = { version = "0.8", default-features = false, features = ["serde", "serde_no_std"] } |
| 69 | ``` |
| 70 | |
| 71 | If you want [borsh-rs](https://github.com/near/borsh-rs) support, include it like this: |
| 72 | |
| 73 | ```toml |
| 74 | [dependencies] |
| 75 | bit-vec = { version = "0.8", features = ["borsh"] } |
| 76 | ``` |
| 77 | |
| 78 | Other available serialization libraries can be enabled with the |
| 79 | [`miniserde`](https://github.com/dtolnay/miniserde) and |
| 80 | [`nanoserde`](https://github.com/not-fl3/nanoserde) features. |
| 81 | |
| 82 | <!-- cargo-rdme start --> |
| 83 | |
| 84 | ### Description |
| 85 | |
| 86 | Dynamic collections implemented with compact bit vectors. |
| 87 | |
| 88 | ### Examples |
| 89 | |
| 90 | This is a simple example of the [Sieve of Eratosthenes][sieve] |
| 91 | which calculates prime numbers up to a given limit. |
| 92 | |
| 93 | [sieve]: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
| 94 | |
| 95 | ```rust |
| 96 | use bit_vec::BitVec; |
| 97 | |
| 98 | let max_prime = 10000; |
| 99 | |
| 100 | // Store the primes as a BitVec |
| 101 | let primes = { |
| 102 | // Assume all numbers are prime to begin, and then we |
| 103 | // cross off non-primes progressively |
| 104 | let mut bv = BitVec::from_elem(max_prime, true); |
| 105 | |
| 106 | // Neither 0 nor 1 are prime |
| 107 | bv.set(0, false); |
| 108 | bv.set(1, false); |
| 109 | |
| 110 | for i in 2.. 1 + (max_prime as f64).sqrt() as usize { |
| 111 | // if i is a prime |
| 112 | if bv[i] { |
| 113 | // Mark all multiples of i as non-prime (any multiples below i * i |
| 114 | // will have been marked as non-prime previously) |
| 115 | for j in i.. { |
| 116 | if i * j >= max_prime { |
| 117 | break; |
| 118 | } |
| 119 | bv.set(i * j, false) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | bv |
| 124 | }; |
| 125 | |
| 126 | // Simple primality tests below our max bound |
| 127 | let print_primes = 20; |
| 128 | print!("The primes below {} are: ", print_primes); |
| 129 | for x in 0..print_primes { |
| 130 | if primes.get(x).unwrap_or(false) { |
| 131 | print!("{} ", x); |
| 132 | } |
| 133 | } |
| 134 | println!(); |
| 135 | |
| 136 | let num_primes = primes.iter().filter(|x| *x).count(); |
| 137 | println!("There are {} primes below {}", num_primes, max_prime); |
| 138 | assert_eq!(num_primes, 1_229); |
| 139 | ``` |
| 140 | |
| 141 | <!-- cargo-rdme end --> |
| 142 | |
| 143 | ## License |
| 144 | |
| 145 | Dual-licensed for compatibility with the Rust project. |
| 146 | |
| 147 | Licensed under the Apache License Version 2.0: http://www.apache.org/licenses/LICENSE-2.0, |
| 148 | or the MIT license: http://opensource.org/licenses/MIT, at your option. |