matchcore/orderbook/book/
limit_book.rs1use super::PriceLevel;
2use crate::{LevelId, OrderId, Price, RestingLimitOrder, Timestamp};
3
4use slab::Slab;
5
6use std::{
7 cmp::Reverse,
8 collections::{BTreeMap, BinaryHeap, HashMap},
9};
10
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13#[derive(Debug, Clone, Default)]
14pub struct LimitBook<const LEVELS_INITIAL_CAPACITY: usize = 2048> {
15 pub(crate) orders: HashMap<OrderId, RestingLimitOrder>,
17
18 pub(crate) levels: Slab<PriceLevel>,
20
21 pub(crate) bids: BTreeMap<Price, LevelId>,
23
24 pub(crate) asks: BTreeMap<Price, LevelId>,
26
27 pub(crate) expiration_queue: BinaryHeap<Reverse<(Timestamp, OrderId)>>,
30}
31
32impl<const LEVELS_INITIAL_CAPACITY: usize> LimitBook<LEVELS_INITIAL_CAPACITY> {
33 pub fn new() -> Self {
35 Self::default()
36 }
37}
38
39impl LimitBook {
40 pub fn orders(&self) -> &HashMap<OrderId, RestingLimitOrder> {
42 &self.orders
43 }
44
45 pub fn levels(&self) -> &Slab<PriceLevel> {
47 &self.levels
48 }
49
50 pub fn bids(&self) -> &BTreeMap<Price, LevelId> {
52 &self.bids
53 }
54
55 pub fn asks(&self) -> &BTreeMap<Price, LevelId> {
57 &self.asks
58 }
59
60 pub fn expiration_queue(&self) -> &BinaryHeap<Reverse<(Timestamp, OrderId)>> {
62 &self.expiration_queue
63 }
64
65 pub fn get_bid_level(&self, price: Price) -> Option<&PriceLevel> {
66 self.bids
67 .get(&price)
68 .map(|level_id| &self.levels[*level_id])
69 }
70
71 pub fn get_ask_level(&self, price: Price) -> Option<&PriceLevel> {
72 self.asks
73 .get(&price)
74 .map(|level_id| &self.levels[*level_id])
75 }
76}