use crate::{LevelId, OrderId, Price, PriceLevel, RestingLimitOrder, Timestamp};
use std::{
cmp::Reverse,
collections::{BTreeMap, BinaryHeap},
};
use rustc_hash::FxHashMap;
use slab::Slab;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct LimitBook<const LEVELS_INITIAL_CAPACITY: usize = 2048> {
pub(crate) orders: FxHashMap<OrderId, RestingLimitOrder>,
pub(crate) levels: Slab<PriceLevel>,
pub(crate) bids: BTreeMap<Price, LevelId>,
pub(crate) asks: BTreeMap<Price, LevelId>,
pub(crate) expiration_queue: BinaryHeap<Reverse<(Timestamp, OrderId)>>,
}
impl<const LEVELS_INITIAL_CAPACITY: usize> LimitBook<LEVELS_INITIAL_CAPACITY> {
pub fn new() -> Self {
Self::default()
}
}
impl LimitBook {
pub fn orders(&self) -> &FxHashMap<OrderId, RestingLimitOrder> {
&self.orders
}
pub fn levels(&self) -> &Slab<PriceLevel> {
&self.levels
}
pub fn bids(&self) -> &BTreeMap<Price, LevelId> {
&self.bids
}
pub fn asks(&self) -> &BTreeMap<Price, LevelId> {
&self.asks
}
pub fn expiration_queue(&self) -> &BinaryHeap<Reverse<(Timestamp, OrderId)>> {
&self.expiration_queue
}
pub fn get_bid_level(&self, price: Price) -> Option<&PriceLevel> {
self.bids
.get(&price)
.map(|level_id| &self.levels[*level_id])
}
pub fn get_ask_level(&self, price: Price) -> Option<&PriceLevel> {
self.asks
.get(&price)
.map(|level_id| &self.levels[*level_id])
}
}