diff --git a/src/main.rs b/src/main.rs index c891449..2113a4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,66 +59,75 @@ struct RoyaltyTree { tree: Vec, } +struct Royalty { + weight: FiNum, // Here and below + lazy: FiNum, // To be distributed to here and to below based on weights + acc: FiNum, // Accumulated here + } + +impl Royalty { + fn new() -> Self { + Royalty { weight:FiNum::zero(), lazy:FiNum::zero(), acc:FiNum::zero() } + } + } + impl RoyaltyTree { fn new() -> Self { RoyaltyTree { tree:Vec::new() } } - fn get_weight(&self, index:usize) -> FiNum { - println!("Get_weight {} self.tree.len() {} ",index,self.tree.len()); - if index=self.tree.len() { index=wt_left(index).unwrap() } - self.tree[index].weight + fn weight_here_below(&self, index:usize) -> FiNum { + if index FiNum { + if index&1==0 { + FiNum::zero() + } else { + self.weight_here_below(wt_left (index).unwrap())+self.weight_here_below(wt_right(index).unwrap()) } } - } - -struct Royalty { - weight: FiNum, // Here and below - acc: FiNum, // Here and Below - } - -impl Royalty { - fn new(weight: FiNum) -> Self { - Royalty { weight:weight, acc:FiNum::new(0u64) } + fn weight_here(&self, index:usize) -> FiNum { + if index&1==0 { + self.weight_here_below(index) + } else { + self.weight_here_below(index)-self.weight_below(index) + } + } + fn expand_to(&mut self, index: usize) -> &mut Self { + for _ in self.tree.len()..=index+1 { self.tree.push(Royalty::new()) } + self } - } - -impl RoyaltyTree { - fn insert(&mut self, weight: FiNum, royalty:FiNum) -> usize { - let last=self.tree.len(); - let mut nweight=weight; - if let Some(left )=wt_left (last) { if left &mut Self { + if index FiNum { + fn capture(&mut self, index: usize) -> &mut Self { // Clear out lazy by drilling from root to index let ff=wt_forefather(index); let mut index=index; - let mut rval=FiNum::zero(); - loop { - let cap=self.tree[index].acc*weight/self.tree[index].weight; - self.tree[index].weight-=weight; - self.tree[index].acc-=cap; - rval+=cap; - if index!=ff { index=wt_parent(index); } else { break; } + while index!=ff { + self.capture0(index); + index=wt_parent(index); } - rval - } + self + } fn dump(&self) { for index in 0..self.tree.len() { println!("Index {} Weight {}",index,self.tree[index].weight);