This commit is contained in:
2024-07-30 15:27:57 -04:00

View File

@@ -110,6 +110,7 @@ struct Order {
commission_remain: FiNum, // Based on commission1 commission_remain: FiNum, // Based on commission1
owner: usize, owner: usize,
rt_loc: usize, // Location in the Royalty Tree rt_loc: usize, // Location in the Royalty Tree
pq_loc: usize, // Location in th Priority Queue
id: usize, id: usize,
} }
@@ -137,7 +138,7 @@ impl RoyaltyTree {
} }
fn acc_total(&self) -> FiNum { fn acc_total(&self) -> FiNum {
let mut rval=FiNum::zero(); let mut rval=FiNum::zero();
for r in &self.tree { rval+= r.acc; } for r in &self.tree { rval+= r.acc+r.lazy; }
rval rval
} }
fn weight_here_below(&self, index:usize) -> FiNum { fn weight_here_below(&self, index:usize) -> FiNum {
@@ -343,7 +344,7 @@ impl Market {
fn name_to_number(&self, name:&str) -> Option<&usize> { fn name_to_number(&self, name:&str) -> Option<&usize> {
self.asset_name2num.get(name) self.asset_name2num.get(name)
} }
fn dump(&self) { fn dump(&mut self) {
println!("Dumping Market:"); println!("Dumping Market:");
for t in &self.traders { for t in &self.traders {
println!(" Trader {}: {}",t.id,t.name); 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)) }); 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() { for off in sorted.iter() {
let off=off.borrow(); let off=off.borrow();
println!(" {} @ {} ({}), Acc: {}", println!(" {} @ {} ({}) OrderID: {} PQLoc: {} RTLoc: {} Royalties: {}",
off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty, off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty,
off.buy_qty/off.sell_qty, off.buy_qty/off.sell_qty,
self.traders[off.owner as usize].name, 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(); pq.dump();
} }
@@ -449,13 +452,16 @@ impl Market {
if sell_qty_remain>0.into() { if sell_qty_remain>0.into() {
self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(royalty0_qty); self.royalties.entry(sell_type).or_insert(RoyaltyTree::new()).add_royalty(royalty0_qty);
let rt=self.royalties.entry(buy_type).or_insert(RoyaltyTree::new()); 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( 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; new_order_id=self.next_order_id;
self.next_order_id+=1; self.next_order_id+=1;
rt.next_entry+=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[owner].sub_balance(sell_type,sell_qty_remain);
self.traders[0].add_balance(sell_type,commission0_qty); 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)); log.push(format!("Moved {} {} ({}) to market",sell_qty_remain,self.number_to_name(sell_type),self.traders[owner].name));
@@ -501,14 +507,16 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
fn peek(&self) -> &T { fn peek(&self) -> &T {
&self.v[0] &self.v[0]
} }
fn bubble_up(&mut self, pos: usize) { fn bubble_up(&mut self, pos: usize) -> usize {
if pos>0 { if pos>0 {
let parent=(pos-1)/2; let parent=(pos-1)/2;
if self.v[parent]<self.v[pos] { if self.v[parent]<self.v[pos] {
self.v.swap(parent,pos); self.v.swap(parent,pos);
self.bubble_up(parent); self.bubble_up(parent)
} }
else { pos }
} }
else { pos }
} }
fn trickle_down(&mut self, pos: usize) { fn trickle_down(&mut self, pos: usize) {
let mut pivot=pos; let mut pivot=pos;
@@ -525,9 +533,9 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
} }
} }
} }
fn insert(&mut self, item: T) { fn insert(&mut self, item: T) -> usize {
self.v.push(item); self.v.push(item);
self.bubble_up(self.v.len()-1); self.bubble_up(self.v.len()-1)
} }
fn pop(&mut self) -> Option<T> { fn pop(&mut self) -> Option<T> {
if self.v.len()==0 { None } if self.v.len()==0 { None }