extern crate flat_tree as flat;
extern crate sparse_bitfield as bitfield;
mod proof;
pub use self::bitfield::{Bitfield, Change};
pub use self::proof::Proof;
pub struct TreeIndex {
bitfield: Bitfield,
}
impl TreeIndex {
pub fn new(bitfield: Bitfield) -> Self {
TreeIndex { bitfield }
}
pub fn get(&mut self, index: usize) -> bool {
self.bitfield.get(index)
}
pub fn set(&mut self, index: usize) -> Change {
if let Change::Unchanged = self.bitfield.set(index, true) {
return Change::Unchanged;
}
let mut index = index;
while self.bitfield.get(flat::sibling(index)) {
index = flat::parent(index);
if let Change::Unchanged = self.bitfield.set(index, true) {
break;
}
}
Change::Changed
}
pub fn proof(&self) -> Proof {
let _nodes: Vec<usize> = Vec::new();
unimplemented!();
}
pub fn digest(&self) {
unimplemented!();
}
pub fn blocks(&self) {
unimplemented!();
}
pub fn roots(&self) {
unimplemented!();
}
pub fn verified_by(&self) {
unimplemented!();
}
}
impl Default for TreeIndex {
fn default() -> Self {
TreeIndex {
bitfield: Bitfield::new(1024),
}
}
}
#[test]
fn can_create_new() {
extern crate flat_tree as flat;
extern crate sparse_bitfield as bitfield;
pub use self::bitfield::Bitfield;
let bitfield = Bitfield::new(1024);
let _tree = TreeIndex::new(bitfield);
}
#[test]
fn can_set() {
extern crate flat_tree as flat;
extern crate sparse_bitfield as bitfield;
pub use self::bitfield::Bitfield;
let bitfield = Bitfield::new(1024);
let mut tree = TreeIndex::new(bitfield);
assert_eq!(tree.set(1), Change::Changed);
assert_eq!(tree.set(1), Change::Unchanged);
assert_eq!(tree.set(0), Change::Changed);
assert_eq!(tree.set(0), Change::Unchanged);
}
#[test]
fn can_get() {
extern crate flat_tree as flat;
extern crate sparse_bitfield as bitfield;
pub use self::bitfield::Bitfield;
let bitfield = Bitfield::new(1024);
let mut tree = TreeIndex::new(bitfield);
tree.set(0);
assert_eq!(tree.get(0), true);
assert_eq!(tree.get(1), false);
}