-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathorderlist.h
More file actions
65 lines (64 loc) · 1.96 KB
/
orderlist.h
File metadata and controls
65 lines (64 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <stdexcept>
#include "order.h"
// TODO add forward_iterator support so that friend class in not needed
class OrderList {
friend class OrderBook;
private:
Node* head=nullptr;
Node* tail=nullptr;
F _price;
public:
OrderList(F price) : _price(price){}
const F& price() const { return _price; }
struct Iterator
{
friend class OrderList;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = Order*;
using reference = Order*&; // or also value_type&
value_type operator*() const { return current->order; }
// Prefix increment
Iterator& operator++() { current = current->next; return *this; }
bool operator== (const Iterator& other) const { return current == other.current; };
operator void *() const { return current; }
private:
Iterator(Node *node) : current(node){}
Node *current;
};
void pushback(Order * const order) {
auto node = &order->node;
node->order = order;
if(head==nullptr) {
head=node;
tail=node;
} else {
node->prev = tail;
tail->next = node;
tail = node;
}
}
void remove(Order * const order){
if(order->node.order==nullptr) throw std::runtime_error("node is null on removal");
auto node = &order->node;
order->node.order = nullptr;
if(head==node) {
head=node->next;
}
if(tail==node) {
tail=node->prev;
}
if(node->prev) {
node->prev->next = node->next;
}
if(node->next) {
node->next->prev = node->prev;
}
}
Order* front() const {
return head==nullptr ? nullptr : head->order;
}
Iterator begin() const { return Iterator(head); }
Iterator end() const { return Iterator(nullptr); }
};