diff --git a/src/main.rs b/src/main.rs index 7bc82f3..691b9f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,12 @@ trait Dumpable { fn dump(&self); } +impl Dumpable for i32 { + fn dump(&self) { + println!("Dump Integer: {}",self); + } + } + impl Dumpable for Offer { fn dump(&self) { println!("Giving {}/{} to get {}",self.sell_remain,self.sell_qty,self.buy_qty); @@ -128,11 +134,11 @@ impl Market { sorted.sort_by(|a,b| (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty))); for off in sorted.iter() { println!(" {} @ {} ({})", - off.buy_qty*off.sell_remain/off.sell_qty, - off.sell_qty/off.buy_qty, + 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); } - //value.dump(); + pq.dump(); } } fn make_offer(&mut self, owner:i32, sell_type:i32, buy_type:i32, sell_qty_initial:FiNum, buy_qty_initial:FiNum) -> bool // Dollars, Bitcoin, 64000, 1 @@ -171,7 +177,7 @@ impl Market { impl PartialOrd for Offer { fn partial_cmp(&self, other:&Self) -> Option { let ord0=self .sell_qty/self .buy_qty; - let ord1=other.buy_qty /other.sell_qty; + let ord1=other.sell_qty/other.buy_qty; if ord0ord1 { Some(Ordering::Greater) } else { Some(Ordering::Equal) } @@ -203,7 +209,7 @@ impl PQueue { fn bubble_up(&mut self, pos: usize) { if pos>0 { let parent=(pos-1)/2; - if self.v[parent]>self.v[pos] { + if self.v[parent] PQueue { let child0=pos*2+1; let child1=pos*2+2; if child0self.v[child0] { pivot=child0; } + if self.v[pos]self.v[child1] { pivot=child1; } + if self.v[pivot] PQueue { fn main() { - let mut pq:PQueue=PQueue::new(); - pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:4.into() } ); - pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:2.into() } ); - pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:3.into() } ); - println!("Pop: {}",pq.pop().unwrap().buy_qty); - println!("Pop: {}",pq.pop().unwrap().buy_qty); - println!("Pop: {}",pq.pop().unwrap().buy_qty); - - let mut rng: StdRng=StdRng::seed_from_u64(1u64); + let mut rng: StdRng=StdRng::seed_from_u64(10u64); + let debug_index=1000000; let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12 let teppy=m.register_trader("Teppy"); let luni =m.register_trader("Luni"); @@ -264,7 +263,7 @@ fn main() { let btc =m.register_asset("BTC"); m.add_trader_balance(teppy,btc,100.into()); m.add_trader_balance(luni ,usd,1000000.into()); - for i in 1..=100 { + for i in 1..=1000 { let seller=if rng.gen_bool(0.5) { teppy } else { luni }; let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum); if rng.gen_bool(0.5) { @@ -279,12 +278,12 @@ fn main() { sell_qty=buy_qty*rng.gen_range(60000..=70000).into(); } println!("Index {} Sell {} {} to buy {} {} ({})",i,sell_qty,m.number_to_name(sell_type),buy_qty,m.number_to_name(buy_type),m.traders[seller as usize].name); - if i==3 { + if i==debug_index { println!("Fifty-two!"); m.dump(); } m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty); - if i==3 { + if i==debug_index { println!("Fifty-two!"); m.dump(); }