Skip to content

Commit

Permalink
feat: replace VecDeque with Vec since we are not using pop() anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
rubik committed Sep 26, 2020
1 parent f20cee4 commit 68d7a02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions src/orderbook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{BTreeMap, VecDeque};
use std::collections::BTreeMap;

use crate::models::{FillMetadata, OrderEvent, OrderType, Side};
use crate::arena::OrderArena;
use crate::models::{FillMetadata, OrderEvent, OrderType, Side};

const DEFAULT_MAP_SIZE: usize = 10_000;
const DEFAULT_QUEUE_SIZE: usize = 10;
Expand All @@ -10,8 +10,8 @@ const DEFAULT_QUEUE_SIZE: usize = 10;
pub struct OrderBook {
pub min_ask: Option<u64>,
pub max_bid: Option<u64>,
pub asks: BTreeMap<u64, VecDeque<usize>>,
pub bids: BTreeMap<u64, VecDeque<usize>>,
pub asks: BTreeMap<u64, Vec<usize>>,
pub bids: BTreeMap<u64, Vec<usize>>,
pub arena: OrderArena,
}

Expand Down Expand Up @@ -168,9 +168,9 @@ impl OrderBook {
self.bids
.entry(price)
.or_insert_with(|| {
VecDeque::with_capacity(DEFAULT_QUEUE_SIZE)
Vec::with_capacity(DEFAULT_QUEUE_SIZE)
})
.push_back(index);
.push(index);
}
}
Side::Ask => {
Expand All @@ -196,9 +196,9 @@ impl OrderBook {
self.asks
.entry(price)
.or_insert_with(|| {
VecDeque::with_capacity(DEFAULT_QUEUE_SIZE)
Vec::with_capacity(DEFAULT_QUEUE_SIZE)
})
.push_back(index);
.push(index);
}
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ impl OrderBook {

fn process_queue(
arena: &mut OrderArena,
opposite_orders: &mut VecDeque<usize>,
opposite_orders: &mut Vec<usize>,
remaining_qty: u64,
id: u128,
fills: &mut Vec<FillMetadata>,
Expand Down
12 changes: 6 additions & 6 deletions tests/orderbook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lobster::{FillMetadata, OrderBook, OrderEvent, OrderType, Side};
use std::collections::{BTreeMap, VecDeque};
use std::collections::BTreeMap;

const DEFAULT_QUEUE_SIZE: usize = 10;
const BID_ASK_COMBINATIONS: [(Side, Side); 2] =
Expand All @@ -14,23 +14,23 @@ fn init_ob(events: Vec<OrderType>) -> (OrderBook, Vec<OrderEvent>) {
(ob, results)
}

fn init_book(orders: Vec<(u64, usize)>) -> BTreeMap<u64, VecDeque<usize>> {
fn init_book(orders: Vec<(u64, usize)>) -> BTreeMap<u64, Vec<usize>> {
let mut bk = BTreeMap::new();
for (p, i) in orders {
bk.entry(p)
.or_insert_with(|| VecDeque::with_capacity(DEFAULT_QUEUE_SIZE))
.push_back(i);
.or_insert_with(|| Vec::with_capacity(DEFAULT_QUEUE_SIZE))
.push(i);
}
bk
}

fn init_book_holes(
orders: Vec<(u64, usize)>,
holes: Vec<u64>,
) -> BTreeMap<u64, VecDeque<usize>> {
) -> BTreeMap<u64, Vec<usize>> {
let mut bk = init_book(orders);
for h in holes {
bk.insert(h, VecDeque::new());
bk.insert(h, Vec::new());
}
bk
}
Expand Down

0 comments on commit 68d7a02

Please sign in to comment.