From dec957318289b531dc9ea834191a2705e8bfa0b8 Mon Sep 17 00:00:00 2001 From: Teppy Date: Mon, 29 Jul 2024 21:02:12 -0400 Subject: [PATCH] changes --- src/main.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 37507a2..246c320 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,6 +110,7 @@ struct Order { commission_remain: FiNum, // Based on commission1 owner: usize, rt_loc: usize, // Location in the Royalty Tree + pq_loc: usize, // Location in th Priority Queue id: usize, } @@ -137,7 +138,7 @@ impl RoyaltyTree { } fn acc_total(&self) -> FiNum { let mut rval=FiNum::zero(); - for r in &self.tree { rval+= r.acc; } + for r in &self.tree { rval+= r.acc+r.lazy; } rval } fn weight_here_below(&self, index:usize) -> FiNum { @@ -343,7 +344,7 @@ impl Market { fn name_to_number(&self, name:&str) -> Option<&usize> { self.asset_name2num.get(name) } - fn dump(&self) { + fn dump(&mut self) { println!("Dumping Market:"); for t in &self.traders { println!(" Trader {}: {}",t.id,t.name); @@ -357,11 +358,13 @@ impl Market { sorted.sort_by(|a,b| { let a=a.borrow(); let b=b.borrow(); (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)) }); for off in sorted.iter() { let off=off.borrow(); - println!(" {} @ {} ({}), Acc: {}", + println!(" {} @ {} ({}) OrderID: {} PQLoc: {} RTLoc: {} Royalties: {}", off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty, off.buy_qty/off.sell_qty, self.traders[off.owner as usize].name, - self.royalties.get(&ap.1).unwrap().tree[off.rt_loc].acc); + off.id,off.pq_loc,off.rt_loc, + self.royalties.get_mut(&ap.1).unwrap().get_royalty(off.rt_loc)); + //,self.royalties.get(&ap.1).unwrap().tree[off.rt_loc].acc); } pq.dump(); } @@ -449,13 +452,16 @@ impl Market { if sell_qty_remain>0.into() { self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(royalty0_qty); let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()); - rt.add_weight(rt.next_entry,buy_qty); // Weight should really be the quantity you would be able to immediately transact rather than how much you wish for. + let rt_loc=rt.next_entry; + rt.add_weight(rt_loc,buy_qty); // Weight should really be the quantity you would be able to immediately transact rather than how much you wish for. let neworder=Rc::new(RefCell::new( - Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt.next_entry, royalty_remain:royalty1_qty, commission_remain:commission1_qty, id:self.next_order_id } )); + Order { owner:owner, sell_qty:sell_qty_remain, sell_remain:sell_qty_remain, buy_qty:buy_qty, rt_loc: rt_loc, pq_loc:0, royalty_remain:royalty1_qty, commission_remain:commission1_qty, id:self.next_order_id } )); new_order_id=self.next_order_id; self.next_order_id+=1; rt.next_entry+=1; - bids.insert(neworder); + let pq_loc=bids.insert(neworder); + bids.v[pq_loc].borrow_mut().pq_loc=pq_loc; + bids.v[pq_loc].borrow_mut().rt_loc=rt_loc; self.traders[owner].sub_balance(sell_type,sell_qty_remain); self.traders[0].add_balance(sell_type,commission0_qty); log.push(format!("Moved {} {} ({}) to market",sell_qty_remain,self.number_to_name(sell_type),self.traders[owner].name)); @@ -501,14 +507,16 @@ impl PQueue { fn peek(&self) -> &T { &self.v[0] } - fn bubble_up(&mut self, pos: usize) { + fn bubble_up(&mut self, pos: usize) -> usize { if pos>0 { let parent=(pos-1)/2; if self.v[parent] PQueue { } } } - fn insert(&mut self, item: T) { + fn insert(&mut self, item: T) -> usize { self.v.push(item); - self.bubble_up(self.v.len()-1); + self.bubble_up(self.v.len()-1) } fn pop(&mut self) -> Option { if self.v.len()==0 { None }