17 stable releases (3 major)

3.0.3 Aug 2, 2023
3.0.2 Jul 25, 2022
3.0.1 Oct 22, 2021
2.0.6 Jan 25, 2021
0.1.0 Feb 1, 2018

#124 in Data structures

Download history 3450/week @ 2024-06-13 4629/week @ 2024-06-20 4572/week @ 2024-06-27 4054/week @ 2024-07-04 3252/week @ 2024-07-11 3748/week @ 2024-07-18 3471/week @ 2024-07-25 3200/week @ 2024-08-01 3280/week @ 2024-08-08 3086/week @ 2024-08-15 3734/week @ 2024-08-22 3951/week @ 2024-08-29 2896/week @ 2024-09-05 3167/week @ 2024-09-12 2964/week @ 2024-09-19 3249/week @ 2024-09-26

12,841 downloads per month
Used in 26 crates (13 directly)

Apache-2.0/MIT

67KB
1K SLoC

Build Status Latest version Documentation

Vector of Bits (Vob)

A Vob is a "vector of bits": a sequence of bits which exposes a Vec-like interface. Whereas Vec<bool> requires 1 byte of storage per bit, Vob requires only 1 bit of storage per bit. Vob is broadly similar to BitVec, but has an API more closely aligned to Vec<bool> and allows non-32-bit backing storage. On 64-bit systems this can lead to a substantial performance increase, particularly when functions such as iter_set_bits are used.

Usage

use vob::vob;

let mut v = vob![false, true, false];
assert_eq!(v[2], false);
v.set(2, true);
assert_eq!(v[2], true);
assert_eq!(v.iter_set_bits(..).collect::<Vec<_>>(), vec![1, 2]);

Migrating from Vec<bool>

As far as possible, Vob is intended to have a superset of Vec<bool>'s interface, which should make porting most code fairly simple. However, Vec<bool> contains several functions which are not yet implemented in Vob: these are missing simply due to a lack of a current use-case rather than because of any fundamental incompatibilities.

There is one missing feature which, currently, is impossible to implement: assignment to an index. In other words one cannot currently express v[0] = true for a Vob v. Until IndexGet / IndexMove and equivalents are implemented in rustc, this restriction appears to be inevitable. Note that referencing by index works (though via a hack identical to that used in BitVec): one can write println!("{}", v[0]) for a Vob v, for example.

Migrating from BitVec

Vob is directly inspired by the BitVec crate, but aims to provide an interface more closely aligned to Vec<bool>. Several functions in BitVec have different names in Vob, but porting is in general fairly simple. The main semantic difference is that Vobs clear() function empties the Vob of contents (i.e. sets its length to 0), whereas BitVec's function of the same name unsets all bits (keeping the length unchanged). The same effect as BitVec's clear can be achieved by using Vob's set_all(false) function.

Dependencies

~94–330KB