use crate::trees::bp::{BpTree, DEFAULT_BLOCK_SIZE};
use crate::trees::TreeBuilder;
use crate::BitVec;
pub struct BpBuilder<const BLOCK_SIZE: usize = DEFAULT_BLOCK_SIZE> {
excess: i64,
bit_vec: BitVec,
}
impl<const BLOCK_SIZE: usize> BpBuilder<BLOCK_SIZE> {
#[must_use]
pub fn new() -> Self {
Self {
excess: 0,
bit_vec: BitVec::new(),
}
}
#[must_use]
pub fn with_capacity(capacity: u64) -> Self {
Self {
excess: 0,
bit_vec: BitVec::with_capacity((capacity * 2) as usize),
}
}
}
impl<const BLOCK_SIZE: usize> Default for BpBuilder<BLOCK_SIZE> {
fn default() -> Self {
Self::new()
}
}
impl<const BLOCK_SIZE: usize> TreeBuilder for BpBuilder<BLOCK_SIZE> {
type Tree = BpTree<BLOCK_SIZE>;
fn enter_node(&mut self) {
self.excess += 1;
self.bit_vec.append_bit(1);
}
fn leave_node(&mut self) {
self.excess -= 1;
self.bit_vec.append_bit(0);
}
fn build(self) -> Result<Self::Tree, i64> {
if self.excess != 0 {
Err(self.excess)
} else {
Ok(BpTree::from_bit_vector(self.bit_vec))
}
}
}