2个版本
0.1.1 | 2020年9月19日 |
---|---|
0.1.0 | 2020年7月9日 |
在 数据结构 中排名 952
每月下载量 2,877
用于 9 个 包(直接使用2个)
32KB
796 行
BitSet
简单、与无std兼容、simd优化、BitSet API。
示例
此crate通过BitSet
trait提供其功能。
use bitset_core::BitSet;
此crate提供的bitset容器是无符号整数、无符号整数的切片以及类似simd的类型,以及当std
功能启用时(默认启用)的Vec<_>
和Box<[_]>
。
use bitset_core::BitSet;
let mut bits = [0u32; 4];
assert_eq!(bits.bit_len(), 4 * 32);
bits.bit_init(true); // Set all bits to true
assert!(bits.bit_all()); // All bits are set
bits.bit_reset(13); // Reset the 13th bit
assert!(bits.bit_any()); // At least some bits are set
bits.bit_flip(42); // Flip the 42nd bit twice (no change)
bits.bit_flip(42);
bits.bit_cond(1, false); // Set the bit to runtime value
assert_eq!(bits.bit_test(42), true);
assert_eq!(bits.bit_test(13), false);
assert_eq!(bits.bit_test(1), false);
assert_eq!(bits.bit_count(), 4 * 32 - 2);
通过使用底层原语(如[u32; 4]
)进行simd优化,这些原语与硬件的128位simd寄存器匹配。强烈鼓励编译器对这些原语进行向量化。
use bitset_core::BitSet;
let mut a = [[0x21212121u32; 4]; 16];
let b = [[0x55555555u32; 4]; 16];
a.bit_or(&b);
a.bit_and(&b);
a.bit_xor(&b);
a.bit_not();
assert_eq!(a, [[0xffffffffu32; 4]; 16]);
对于使用std
功能的非固定大小容器,还实现了BitSet
,用于Vec<T>
和Box<[T]>
(其中[T]
:BitSet
)。
未来的工作包括使所有内容都成为const fn,以在编译时启用所有这些功能,但受到const fn中对特质的支持的阻碍。
许可证
在MIT许可证下许可,请参阅license.txt。
贡献
除非你明确声明,否则你提交的任何有意包含在作品中的贡献,均应按上述方式许可,不得附加任何其他条款或条件。