2个版本

使用旧的Rust 2015

0.4.4 2017年4月4日
0.4.3 2016年7月17日

#22 in #bitmask

Download history 91/week @ 2024-03-13 148/week @ 2024-03-20 149/week @ 2024-03-27 136/week @ 2024-04-03 154/week @ 2024-04-10 152/week @ 2024-04-17 126/week @ 2024-04-24 189/week @ 2024-05-01 113/week @ 2024-05-08 196/week @ 2024-05-15 80/week @ 2024-05-22 69/week @ 2024-05-29 126/week @ 2024-06-05 196/week @ 2024-06-12 112/week @ 2024-06-19 105/week @ 2024-06-26

553 每月下载量
用于 10 个crate(6个直接使用)

MIT/Apache

62KB
1K SLoC

编译时大小位数组。

文档可在 https://ambaxter.github.io/bit_array/bit_array 查找


lib.rs:

使用位数组实现的集合。

示例

这是一个简单的埃拉托斯特尼筛法的示例,它计算给定限制范围内的质数。

extern crate typenum;
use bit_array::BitArray;
use typenum::{Unsigned, U10000};


// Store the primes as a BitArray
let primes = {
    // Assume all numbers are prime to begin, and then we
    // cross off non-primes progressively
    let mut bv = BitArray::<u32, U10000>::from_elem(true);

    // Neither 0 nor 1 are prime
    bv.set(0, false);
    bv.set(1, false);

    for i in 2.. 1 + (U10000::to_usize() as f64).sqrt() as usize {
        // if i is a prime
        if bv[i] {
            // Mark all multiples of i as non-prime (any multiples below i * i
            // will have been marked as non-prime previously)
            for j in i.. {
                if i * j >= U10000::to_usize() {
                    break;
                }
                bv.set(i * j, false)
            }
        }
    }
    bv
};

// Simple primality tests below our max bound
let print_primes = 20;
print!("The primes below {} are: ", print_primes);
for x in 0..print_primes {
    if primes.get(x).unwrap_or(false) {
        print!("{} ", x);
    }
}
println!("");

let num_primes = primes.iter().filter(|x| *x).count();
println!("There are {} primes below {}", num_primes, U10000::to_usize());
assert_eq!(num_primes, 1_229);

依赖项

~365KB