Simplifies the boolean-bitfield implementation to use bit-vec crate

This commit is contained in:
Alex Stokes 2018-10-24 12:21:51 +02:00
parent 2e2a1faff4
commit a9decd3aed
4 changed files with 119 additions and 277 deletions

View File

@ -31,7 +31,7 @@ impl Encodable for AttestationRecord {
s.append(&self.shard_id); s.append(&self.shard_id);
s.append_vec(&self.oblique_parent_hashes); s.append_vec(&self.oblique_parent_hashes);
s.append(&self.shard_block_hash); s.append(&self.shard_block_hash);
s.append_vec(&self.attester_bitfield.to_be_vec()); s.append_vec(&self.attester_bitfield.to_bytes());
s.append(&self.justified_slot); s.append(&self.justified_slot);
s.append(&self.justified_block_hash); s.append(&self.justified_block_hash);
s.append_vec(&self.aggregate_sig.as_bytes()); s.append_vec(&self.aggregate_sig.as_bytes());

View File

@ -5,3 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
[dependencies] [dependencies]
ssz = { path = "../ssz" } ssz = { path = "../ssz" }
bit-vec = "0.5.0"
[dev-dependencies]
rand = "0.5.5"

View File

@ -1,7 +1,3 @@
# Boolean Bitfield # Boolean Bitfield
A work-in-progress implementation of an unbounded boolean bitfield. Implements a set of boolean as a tightly-packed vector of bits.
Based upon a `Vec<u8>`
Documentation TBC...

View File

@ -1,161 +1,74 @@
/* extern crate bit_vec;
* Implemenation of a bitfield as a vec. Only
* supports bytes (Vec<u8>) as the underlying
* storage.
*
* A future implementation should be more efficient,
* this is just to get the job done for now.
*/
extern crate ssz; extern crate ssz;
use std::cmp::max; #[cfg(test)]
extern crate rand;
#[derive(Eq, Clone, Default, Debug)] use bit_vec::BitVec;
pub struct BooleanBitfield {
len: usize, /// A BooleanBitfield represents a set of booleans compactly stored as a vector of bits.
vec: Vec<u8>, #[derive(Debug, Clone, PartialEq)]
pub struct BooleanBitfield(BitVec);
/// Error represents some reason a request against a bitfield was not satisfied
#[derive(Debug)]
pub enum Error {
/// OutOfBounds refers to indexing into a bitfield where no bits exist; returns the illegal index and the current size of the bitfield, respectively
OutOfBounds(usize, usize),
} }
impl BooleanBitfield { impl BooleanBitfield {
/// Create a new bitfield with a length of zero. /// Create a new bitfield with a length of zero.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self { 0: BitVec::new() }
len: 0,
vec: vec![0],
}
} }
/// Create a new bitfield of a certain capacity /// Create a new bitfield using the supplied `bytes` as input
pub fn with_capacity(capacity: usize) -> Self { pub fn from_bytes(bytes: &[u8]) -> Self {
let mut vec = Vec::with_capacity(capacity / 8 + 1); Self {
vec.push(0); 0: BitVec::from_bytes(bytes),
Self { len: 0, vec } }
} }
/// Read the value of a bit. /// Read the value of a bit.
/// ///
/// Will return `true` if the bit has been set to `true` /// If the index is in bounds, then result is Ok(value) where value is `true` if the bit is 1 and `false` if the bit is 0.
/// without then being set to `False`. /// If the index is out of bounds, we return an error to that extent.
pub fn get_bit(&self, i: usize) -> bool { pub fn get(&self, i: usize) -> Result<bool, Error> {
let bit = |i: usize| i % 8; match self.0.get(i) {
let byte = |i: usize| i / 8; Some(value) => Ok(value),
None => Err(Error::OutOfBounds(i, self.0.len())),
if byte(i) >= self.vec.len() {
false
} else {
self.vec[byte(i)] & (1 << (bit(i) as u8)) != 0
} }
} }
/// Set the value of a bit. /// Set the value of a bit.
/// ///
/// If this bit is larger than the length of the underlying byte /// Returns the previous value if successful.
/// array it will be extended. /// If the index is out of bounds, we return an error to that extent.
pub fn set_bit(&mut self, i: usize, to: bool) { pub fn set(&mut self, i: usize, value: bool) -> Result<bool, Error> {
let bit = |i: usize| i % 8; let previous = self.get(i)?;
let byte = |i: usize| i / 8; self.0.set(i, value);
Ok(previous)
self.len = max(self.len, i + 1);
if byte(i) >= self.vec.len() {
self.vec.resize(byte(i) + 1, 0);
}
if to {
self.vec[byte(i)] = self.vec[byte(i)] | (1 << (bit(i) as u8))
} else {
self.vec[byte(i)] = self.vec[byte(i)] & !(1 << (bit(i) as u8))
}
} }
/// Return the "length" of this bitfield. Length is defined as /// Returns the index of the highest set bit. Some(n) if some bit is set, None otherwise.
/// the highest bit that has been set. pub fn highest_set_bit(&self) -> Option<usize> {
/// self.0.iter().rposition(|bit| bit)
/// Note: this is distinct from the length of the underlying }
/// vector.
/// Returns the number of bits in this bitfield.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.len self.0.len()
} }
/// True if no bits have ever been set. A bit that is set and then /// Returns the number of `1` bits in the bitfield
/// unset will still count to the length of the bitfield. pub fn num_set_bits(&self) -> usize {
/// self.0.iter().filter(|&bit| bit).count()
/// Note: this is distinct from the length of the underlying
/// vector.
pub fn is_empty(&self) -> bool {
self.len == 0
} }
/// The number of bytes required to represent the bitfield. /// Returns a vector of bytes representing the bitfield
pub fn num_bytes(&self) -> usize { pub fn to_bytes(&self) -> Vec<u8> {
self.vec.len() self.0.to_bytes()
}
/// Iterate through the underlying vector and count the number of
/// true bits.
pub fn num_true_bits(&self) -> u64 {
let mut count: u64 = 0;
for byte in &self.vec {
for bit in 0..8 {
if byte & (1 << (bit as u8)) != 0 {
count += 1;
}
}
}
count
}
/// Iterate through the underlying vector and find the highest
/// set bit. Useful for instantiating a new instance from
/// some set of bytes.
pub fn compute_length(bytes: &[u8]) -> usize {
for byte in (0..bytes.len()).rev() {
for bit in (0..8).rev() {
if bytes[byte] & (1 << (bit as u8)) != 0 {
return (byte * 8) + bit + 1;
}
}
}
0
}
/// Get the byte at a position, assuming big-endian encoding.
pub fn get_byte(&self, n: usize) -> Option<&u8> {
self.vec.get(n)
}
/// Clone and return the underlying byte array (`Vec<u8>`).
pub fn to_vec(&self) -> Vec<u8> {
self.vec.clone()
}
/// Clone and return the underlying byte array (`Vec<u8>`) in big-endinan format.
pub fn to_be_vec(&self) -> Vec<u8> {
let mut o = self.vec.clone();
o.reverse();
o
}
}
impl<'a> From<&'a [u8]> for BooleanBitfield {
fn from(input: &[u8]) -> Self {
let mut vec = input.to_vec();
vec.reverse();
BooleanBitfield {
vec,
len: BooleanBitfield::compute_length(input),
}
}
}
impl PartialEq for BooleanBitfield {
fn eq(&self, other: &BooleanBitfield) -> bool {
(self.vec == other.vec) & (self.len == other.len)
}
}
impl ssz::Encodable for BooleanBitfield {
fn ssz_append(&self, s: &mut ssz::SszStream) {
s.append_vec(&self.to_vec());
} }
} }
@ -165,12 +78,13 @@ impl ssz::Decodable for BooleanBitfield {
if (ssz::LENGTH_BYTES + len) > bytes.len() { if (ssz::LENGTH_BYTES + len) > bytes.len() {
return Err(ssz::DecodeError::TooShort); return Err(ssz::DecodeError::TooShort);
} }
if len == 0 { if len == 0 {
Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES)) Ok((BooleanBitfield::new(), index + ssz::LENGTH_BYTES))
} else { } else {
let b = BooleanBitfield::from(&bytes[(index + 4)..(index + len + 4)]); let field = BooleanBitfield::from_bytes(&bytes[(index + 4)..(index + len + 4)]);
let index = index + ssz::LENGTH_BYTES + len; let index = index + ssz::LENGTH_BYTES + len;
Ok((b, index)) Ok((field, index))
} }
} }
} }
@ -178,149 +92,77 @@ impl ssz::Decodable for BooleanBitfield {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use ssz::Decodable;
#[test] #[test]
fn test_new_from_slice() { fn test_empty_bitfield() {
let s = [0]; let mut field = BooleanBitfield::new();
let b = BooleanBitfield::from(&s[..]);
assert_eq!(b.len, 0);
let s = [255]; for _ in 0..100 {
let b = BooleanBitfield::from(&s[..]); let index: usize = rand::random();
assert_eq!(b.len, 8); assert!(field.get(index).is_err());
assert!(field.set(index, rand::random()).is_err())
let s = [0, 1];
let b = BooleanBitfield::from(&s[..]);
assert_eq!(b.len, 9);
let s = [31];
let b = BooleanBitfield::from(&s[..]);
assert_eq!(b.len, 5);
}
#[test]
fn test_ssz_encoding() {
let mut b = BooleanBitfield::new();
b.set_bit(8, true);
let mut stream = ssz::SszStream::new();
stream.append(&b);
assert_eq!(stream.drain(), vec![0, 0, 0, 2, 0, 1]);
}
/*
#[test]
fn test_ssz_decoding() {
/*
* Correct input
*/
let input = vec![0, 0, 0, 2, 0, 1];
let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap();
assert_eq!(i, 6);
assert_eq!(b.num_true_bits(), 1);
assert_eq!(b.get_bit(8), true);
/*
* Input too long
*/
let mut input = vec![0, 0, 0, 2, 0, 1];
input.push(42);
let (b, i) = BooleanBitfield::ssz_decode(&input, 0).unwrap();
assert_eq!(i, 6);
assert_eq!(b.num_true_bits(), 1);
assert_eq!(b.get_bit(8), true);
/*
* Input too short
*/
let input = vec![0, 0, 0, 2, 1];
let res = BooleanBitfield::ssz_decode(&input, 0);
assert_eq!(res, Err(ssz::DecodeError::TooShort));
}
*/
#[test]
fn test_new_bitfield_len() {
let b = BooleanBitfield::new();
assert_eq!(b.len(), 0);
assert_eq!(b.to_be_vec(), vec![0]);
let b = BooleanBitfield::with_capacity(100);
assert_eq!(b.len(), 0);
assert_eq!(b.to_be_vec(), vec![0]);
}
#[test]
fn test_bitfield_set() {
let mut b = BooleanBitfield::new();
b.set_bit(0, false);
assert_eq!(b.to_be_vec(), [0]);
b = BooleanBitfield::new();
b.set_bit(7, true);
assert_eq!(b.to_be_vec(), [128]);
b.set_bit(7, false);
assert_eq!(b.to_be_vec(), [0]);
assert_eq!(b.len(), 8);
b = BooleanBitfield::new();
b.set_bit(7, true);
b.set_bit(0, true);
assert_eq!(b.to_be_vec(), [129]);
b.set_bit(7, false);
assert_eq!(b.to_be_vec(), [1]);
assert_eq!(b.len(), 8);
b = BooleanBitfield::new();
b.set_bit(8, true);
assert_eq!(b.to_be_vec(), [1, 0]);
assert_eq!(b.len(), 9);
b.set_bit(8, false);
assert_eq!(b.to_be_vec(), [0, 0]);
assert_eq!(b.len(), 9);
b = BooleanBitfield::new();
b.set_bit(15, true);
assert_eq!(b.to_be_vec(), [128, 0]);
b.set_bit(15, false);
assert_eq!(b.to_be_vec(), [0, 0]);
assert_eq!(b.len(), 16);
b = BooleanBitfield::new();
b.set_bit(8, true);
b.set_bit(15, true);
assert_eq!(b.to_be_vec(), [129, 0]);
b.set_bit(15, false);
assert_eq!(b.to_be_vec(), [1, 0]);
assert_eq!(b.len(), 16);
}
#[test]
fn test_bitfield_get() {
let test_nums = vec![0, 8, 15, 42, 1337];
for i in test_nums {
let mut b = BooleanBitfield::new();
assert_eq!(b.get_bit(i), false);
b.set_bit(i, true);
assert_eq!(b.get_bit(i), true);
b.set_bit(i, true);
} }
} }
const INPUT: &[u8] = &[0b0000_0010, 0b0000_0010];
#[test] #[test]
fn test_bitfield_num_true_bits() { fn test_get_from_bitfield() {
let mut b = BooleanBitfield::new(); let field = BooleanBitfield::from_bytes(INPUT);
assert_eq!(b.num_true_bits(), 0); let unset = field.get(0).unwrap();
b.set_bit(15, true); assert!(!unset);
assert_eq!(b.num_true_bits(), 1); let set = field.get(6).unwrap();
b.set_bit(15, false); assert!(set);
assert_eq!(b.num_true_bits(), 0); let set = field.get(14).unwrap();
b.set_bit(0, true); assert!(set);
b.set_bit(7, true); }
b.set_bit(8, true);
b.set_bit(1337, true); #[test]
assert_eq!(b.num_true_bits(), 4); fn test_set_for_bitfield() {
let mut field = BooleanBitfield::from_bytes(INPUT);
let previous = field.set(10, true).unwrap();
assert!(!previous);
let previous = field.get(10).unwrap();
assert!(previous);
let previous = field.set(6, false).unwrap();
assert!(previous);
let previous = field.get(6).unwrap();
assert!(!previous);
}
#[test]
fn test_highest_set_bit() {
let field = BooleanBitfield::from_bytes(INPUT);
assert_eq!(field.highest_set_bit().unwrap(), 14);
let field = BooleanBitfield::new();
assert_eq!(field.highest_set_bit(), None);
}
#[test]
fn test_len() {
let field = BooleanBitfield::from_bytes(INPUT);
assert_eq!(field.len(), 16);
let field = BooleanBitfield::new();
assert_eq!(field.len(), 0);
}
#[test]
fn test_num_set_bits() {
let field = BooleanBitfield::from_bytes(INPUT);
assert_eq!(field.num_set_bits(), 2);
let field = BooleanBitfield::new();
assert_eq!(field.num_set_bits(), 0);
}
#[test]
fn test_to_bytes() {
let field = BooleanBitfield::from_bytes(INPUT);
assert_eq!(field.to_bytes(), INPUT);
let field = BooleanBitfield::new();
assert_eq!(field.to_bytes(), vec![]);
} }
} }