Add len() to BooleanBitfield

This commit is contained in:
Paul Hauner 2018-07-16 16:34:34 +10:00
parent f0d61c340a
commit 573294beee
2 changed files with 14 additions and 0 deletions

View File

@ -7,21 +7,25 @@
* this is just to get the job done for now.
*/
extern crate rlp;
use std::cmp::max;
use self::rlp::{ RlpStream, Encodable };
pub struct BooleanBitfield{
len: usize,
vec: Vec<u8>
}
impl BooleanBitfield {
pub fn new() -> Self {
Self {
len: 0,
vec: vec![]
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
len: 0,
vec: Vec::with_capacity(capacity)
}
}
@ -47,6 +51,7 @@ impl BooleanBitfield {
}
pub fn set_bit(&mut self, bit: &usize, to: &bool) {
self.len = max(self.len, *bit + 1);
self.set_bit_on_byte(*bit % 8, *bit / 8, to);
}
@ -61,6 +66,8 @@ impl BooleanBitfield {
}
}
pub fn len(&self) -> usize { self.len }
// Return the total number of bits set to true.
pub fn num_true_bits(&self) -> u64 {
let mut count: u64 = 0;
@ -102,6 +109,7 @@ mod tests {
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);
@ -109,18 +117,21 @@ mod tests {
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]);
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);
@ -128,6 +139,7 @@ mod tests {
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]

View File

@ -4,6 +4,8 @@ use super::bls::Keypair;
use self::rand::thread_rng;
// Returns a keypair for use in testing purposes.
// It is dangerous because we provide no guarantees
// that the private key is unique or in-fact private.
pub fn get_dangerous_test_keypair() -> Keypair {
let mut rng = thread_rng();
Keypair::generate(&mut rng)