Flex
Flex exchange system
orders_queue.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 namespace tvm { namespace xchg {
10 
12 
16 class orders_queue {
17 public:
18  uint128 all_amount_;
19  big_queue<OrderInfoXchg> orders_;
20 
22  bool empty() const { return orders_.empty(); }
23 
25  void drop_no_post_orders(process_queue_state& state, bool sell) {
26  for (auto it = orders_.begin(); it != orders_.end();) {
27  auto next_it = std::next(it);
28  auto ord = *it;
29  if (!ord.post_order) {
30  state.on_no_post_order_done({it.idx_, (*it)}, sell);
31  all_amount_ -= ord.amount;
32  orders_.erase(it);
33  }
34  it = next_it;
35  }
36  }
37 };
38 
41 public:
43  explicit orders_queue_cached(orders_queue& q) : q_(q) {}
44 
46  bool empty() const { return q_.orders_.empty(); }
47 
49  OrderInfoXchgWithIdx& front_with_idx() {
50  if (!head_) {
51  head_ = q_.orders_.front_with_idx_opt();
52  head_orig_amount_ = head_->second.amount;
53  }
54  require(!!head_, error_code::iterator_overflow);
55  return *head_;
56  }
57 
59  void pop() {
60  require(!!head_, error_code::iterator_overflow);
61  q_.all_amount_ -= head_orig_amount_;
62  q_.orders_.pop();
63  head_.reset();
64  }
65 
68  if (head_) {
69  [[maybe_unused]] auto [idx, ord] = *head_;
70  q_.all_amount_ -= (head_orig_amount_ - ord.amount);
71  q_.orders_.change_front(ord);
72  }
73  }
74  orders_queue& q_;
75  opt<OrderInfoXchgWithIdx> head_;
76  uint128 head_orig_amount_;
77 };
78 
79 }} // namespace tvm::xchg
80 
Working version of orders_queue with cached head order.
Definition: orders_queue.hpp:40
void pop()
Pop front order.
Definition: orders_queue.hpp:59
bool empty() const
Is queue empty.
Definition: orders_queue.hpp:46
OrderInfoXchgWithIdx & front_with_idx()
Get queue head (front) with caching.
Definition: orders_queue.hpp:49
orders_queue_cached(orders_queue &q)
Constructor.
Definition: orders_queue.hpp:43
~orders_queue_cached()
Destructor to store the remaining head order back to queue.
Definition: orders_queue.hpp:67
Orders queue to keep orders and common state (tokens amount).
Definition: orders_queue.hpp:16
bool empty() const
Is queue empty.
Definition: orders_queue.hpp:22
uint128 all_amount_
Amount of tokens in all orders.
Definition: orders_queue.hpp:18
big_queue< OrderInfoXchg > orders_
Orders queue.
Definition: orders_queue.hpp:19
void drop_no_post_orders(process_queue_state &state, bool sell)
Drop orders without post_order flag.
Definition: orders_queue.hpp:25
Processing orders queue state for PriceXchg.
Definition: process_queue_state.hpp:17
void on_no_post_order_done(OrderInfoXchgWithIdx ord_idx, bool sell)
When we have order without post-order flag and other side queue is empty.
Definition: process_queue_state.hpp:100