### 2 releases

0.1.1 | Sep 19, 2020 |
---|---|

0.1.0 | Jul 9, 2020 |

#**742** in Data structures

**2,021** downloads per month

Used in **9** crates
(2 directly)

**MIT**license

32KB

796 lines

# BitSet

Straightforward, no-std compatible, simd optimized, BitSet API.

## Examples

This crate provides its functionality through the

trait.`BitSet`

`use` `bitset_core``::`BitSet`;`

The containers for the bitset provided by this crate are unsigned integers, slices of unsigned integers and simd-like types, and

, `Vec``<``_``>`

if the `Box``<``[``_``]``>`

feature is enabled (enabled by default).`std`

`use` `bitset_core``::`BitSet`;`
`let` `mut` bits `=` `[``0``u32``;` `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``)``;`

Simd optimization is provided by using underlying primitives such as

which match the hardware's 128-bit simd registers. The compiler is heavily encouraged to vectorize these primitives.`[``u32``;` `4``]`

`use` `bitset_core``::`BitSet`;`
`let` `mut` a `=` `[``[``0x21212121``u32``;` `4``]``;` `16``]``;`
`let` b `=` `[``[``0x55555555``u32``;` `4``]``;` `16``]``;`
a`.``bit_or``(``&`b`)``;`
a`.``bit_and``(``&`b`)``;`
a`.``bit_xor``(``&`b`)``;`
a`.``bit_not``(``)``;`
`assert_eq!``(`a`,` `[``[``0xffffffff``u32``;` `4``]``;` `16``]``)``;`

For non fixed-size containers using the

feature `std`

is also implemented for `BitSet`

and `Vec``<`T`>`

(where `Box``<``[`T`]``>`

: `[`T`]`

).`BitSet`

Future work includes making everything const fn to enable all of this at compiletime, blocked on support for traits in const fn.

## License

Licensed under MIT License, see license.txt.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.