This commit is contained in:
2024-05-20 11:00:45 -04:00
parent d65eee0a51
commit 545d94c204

View File

@@ -43,6 +43,12 @@ trait Dumpable {
fn dump(&self); fn dump(&self);
} }
impl Dumpable for i32 {
fn dump(&self) {
println!("Dump Integer: {}",self);
}
}
impl Dumpable for Offer { impl Dumpable for Offer {
fn dump(&self) { fn dump(&self) {
println!("Giving {}/{} to get {}",self.sell_remain,self.sell_qty,self.buy_qty); 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))); sorted.sort_by(|a,b| (a.sell_qty/a.buy_qty).cmp(&(b.sell_qty/b.buy_qty)));
for off in sorted.iter() { for off in sorted.iter() {
println!(" {} @ {} ({})", println!(" {} @ {} ({})",
off.buy_qty*off.sell_remain/off.sell_qty, off.sell_remain, // off.buy_qty*off.sell_remain/off.sell_qty,
off.sell_qty/off.buy_qty, off.buy_qty/off.sell_qty,
self.traders[off.owner as usize].name); 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 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 { impl PartialOrd for Offer {
fn partial_cmp(&self, other:&Self) -> Option<Ordering> { fn partial_cmp(&self, other:&Self) -> Option<Ordering> {
let ord0=self .sell_qty/self .buy_qty; 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 ord0<ord1 { Some(Ordering::Less) } if ord0<ord1 { Some(Ordering::Less) }
else if ord0>ord1 { Some(Ordering::Greater) } else if ord0>ord1 { Some(Ordering::Greater) }
else { Some(Ordering::Equal) } else { Some(Ordering::Equal) }
@@ -203,7 +209,7 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
fn bubble_up(&mut self, pos: usize) { fn bubble_up(&mut self, pos: 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);
} }
@@ -214,9 +220,9 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
let child0=pos*2+1; let child0=pos*2+1;
let child1=pos*2+2; let child1=pos*2+2;
if child0<self.v.len() { if child0<self.v.len() {
if self.v[pos]>self.v[child0] { pivot=child0; } if self.v[pos]<self.v[child0] { pivot=child0; }
if child1<self.v.len() { if child1<self.v.len() {
if self.v[pivot]>self.v[child1] { pivot=child1; } if self.v[pivot]<self.v[child1] { pivot=child1; }
} }
if pivot!=pos { if pivot!=pos {
self.v.swap(pivot,pos); self.v.swap(pivot,pos);
@@ -248,15 +254,8 @@ impl<T: Dumpable + std::cmp::PartialOrd + std::fmt::Debug> PQueue<T> {
fn main() { fn main() {
let mut pq:PQueue<Offer>=PQueue::new(); let mut rng: StdRng=StdRng::seed_from_u64(10u64);
pq.insert(Offer { owner:1, sell_qty:12.into(), sell_remain:12.into(), buy_qty:4.into() } ); let debug_index=1000000;
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 m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12 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 teppy=m.register_trader("Teppy");
let luni =m.register_trader("Luni"); let luni =m.register_trader("Luni");
@@ -264,7 +263,7 @@ fn main() {
let btc =m.register_asset("BTC"); let btc =m.register_asset("BTC");
m.add_trader_balance(teppy,btc,100.into()); m.add_trader_balance(teppy,btc,100.into());
m.add_trader_balance(luni ,usd,1000000.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 seller=if rng.gen_bool(0.5) { teppy } else { luni };
let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum); let (buy_type,sell_type,buy_qty,sell_qty):(i32,i32,FiNum,FiNum);
if rng.gen_bool(0.5) { if rng.gen_bool(0.5) {
@@ -279,12 +278,12 @@ fn main() {
sell_qty=buy_qty*rng.gen_range(60000..=70000).into(); 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); 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!"); println!("Fifty-two!");
m.dump(); m.dump();
} }
m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty); m.make_offer(seller,sell_type,buy_type,sell_qty,buy_qty);
if i==3 { if i==debug_index {
println!("Fifty-two!"); println!("Fifty-two!");
m.dump(); m.dump();
} }