This commit is contained in:
2024-06-03 19:36:51 -04:00
parent 2c953e4416
commit 9c11e7a1bf
3 changed files with 498 additions and 12 deletions

View File

@@ -52,7 +52,7 @@ struct Order {
sell_remain: FiNum,
buy_qty: FiNum,
owner: usize,
rt_loc: usize, // Location in the Royalty Tree
rt_loc: usize, // Location in the Royalty Tree
}
struct RoyaltyTree {
@@ -61,8 +61,22 @@ struct RoyaltyTree {
impl RoyaltyTree {
fn new() -> Self {
RoyaltyTree { tree:Vec::new() }
}
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() { self.tree[index].weight }
else {
let ff=wt_forefather(index);
let mut index=index;
while index!=ff {
if index<self.tree.len() { return self.tree[index].weight }
else { index=wt_parent(index) }
}
while index>=self.tree.len() { index=wt_left(index).unwrap() }
self.tree[index].weight
}
}
}
struct Royalty {
@@ -78,21 +92,24 @@ impl Royalty {
impl RoyaltyTree {
fn insert(&mut self, weight: FiNum) -> usize {
self.tree.push(Royalty::new(weight));
let last=self.tree.len()-1;
let last=self.tree.len();
let mut nweight=weight;
if let Some(left )=wt_left (last) { if left <self.tree.len() { nweight+=self.tree[left ].weight } }
if let Some(right)=wt_right(last) { if right<self.tree.len() { nweight+=self.tree[right].weight } }
self.tree.push(Royalty::new(nweight));
let forefather=wt_forefather(last);
let mut pivot=last;
while pivot!=forefather {
pivot=wt_parent(pivot);
for i in self.tree.len()-1..=pivot {
self.tree.push(Royalty::new(0.into()));
self.tree[i].weight=if let Some(v)=wt_left (i) { self.tree[v].weight } else { FiNum::zero() }
+if let Some(v)=wt_right(i) { self.tree[v].weight } else { FiNum::zero() };
}
if pivot<self.tree.len() { self.tree[pivot].weight+=weight }
}
self.tree[last].weight+=weight;
last
}
fn dump(&self) {
for index in 0..self.tree.len() {
println!("Index {} Weight {}",index,self.tree[index].weight);
}
}
}
@@ -339,7 +356,7 @@ impl Market {
self.add_trader_balance(luni ,usd,650000000.into());
let mut count=0;
let mut tries=0;
for _i in 1..=10000000 {
for _i in 1..=1000000 {
let seller=if rng.gen_bool(0.5) { teppy } else { luni };
let (buy_type,sell_type,buy_qty,sell_qty):(usize,usize,FiNum,FiNum);
if rng.gen_bool(0.5) {
@@ -367,6 +384,10 @@ fn wt_level(index:usize) -> usize {
(index^(index+1)).trailing_ones() as usize-1
}
fn wt_leaf(index:usize) -> bool {
index&1==0
}
fn wt_left(index:usize) -> Option<usize> {
let level=wt_level(index);
if level>0 { Some(index-(1<<(wt_level(index)-1))) } else { None }
@@ -405,6 +426,16 @@ fn tree_stuff() {
}
}
fn royalty_stuff() {
let mut rt=RoyaltyTree::new();
for _ in 0..20 {
rt.insert(FiNum::new_i32(10));
}
for index in 0..rt.tree.len() {
println!("Index: {} Royalty: {}",index,rt.tree[index].weight);
}
}
fn main() {
let args: Vec<String> = env::args().collect();
@@ -413,6 +444,7 @@ fn main() {
match mode {
"--exercise" => m.exercise(),
"--treestuff" => tree_stuff(),
"--royaltystuff" => royalty_stuff(),
_ => println!("Unknown mode: {}",mode),
}
}