Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
Loading...
Searching...
No Matches
level_container.hpp
Go to the documentation of this file.
1// Copyright 2026 Slick Quant
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
9#include <vector>
10#include <algorithm>
11#include <functional>
12
14
21public:
23 using iterator = typename std::vector<PriceLevelL2>::iterator;
24 using const_iterator = typename std::vector<PriceLevelL2>::const_iterator;
25 using Comparator = std::function<bool(const PriceLevelL2&, const PriceLevelL2&)>;
26
28 LevelContainer() : side_(Side::Buy), compare_(BidComparator{}) {}
29
33 explicit LevelContainer(Side side, std::size_t initial_capacity = 32)
34 : side_(side),
35 compare_(side == Side::Buy ?
36 Comparator{BidComparator{}} : // Bids: descending
37 Comparator{AskComparator{}}) // Asks: ascending
38 {
39 levels_.reserve(initial_capacity);
40 }
41
43 [[nodiscard]] Side side() const noexcept {
44 return side_;
45 }
46
48 [[nodiscard]] std::size_t size() const noexcept {
49 return levels_.size();
50 }
51
53 [[nodiscard]] bool empty() const noexcept {
54 return levels_.empty();
55 }
56
58 [[nodiscard]] const PriceLevelL2& operator[](std::size_t index) const noexcept {
59 return levels_[index];
60 }
61
63 [[nodiscard]] const PriceLevelL2& at(std::size_t index) const {
64 return levels_.at(index);
65 }
66
68 [[nodiscard]] const PriceLevelL2* best() const noexcept {
69 return levels_.empty() ? nullptr : &levels_.front();
70 }
71
74 [[nodiscard]] iterator find(Price price) noexcept {
75 PriceLevelL2 key{price, 0, 0};
76 auto it = std::lower_bound(levels_.begin(), levels_.end(), key, compare_);
77 if (it != levels_.end() && it->price == price) {
78 return it;
79 }
80 return levels_.end();
81 }
82
84 [[nodiscard]] const_iterator find(Price price) const noexcept {
85 PriceLevelL2 key{price, 0, 0};
86 auto it = std::lower_bound(levels_.begin(), levels_.end(), key, compare_);
87 if (it != levels_.end() && it->price == price) {
88 return it;
89 }
90 return levels_.end();
91 }
92
94 [[nodiscard]] const PriceLevelL2* getLevel(Price price) const noexcept {
95 auto it = find(price);
96 return (it != levels_.end()) ? &(*it) : nullptr;
97 }
98
102 std::pair<iterator, bool> insertOrUpdate(Price price, Quantity quantity, Timestamp timestamp) {
103 // Binary search for insertion position
104 PriceLevelL2 key{price, 0, 0};
105 auto it = std::lower_bound(levels_.begin(), levels_.end(), key, compare_);
106
107 // Check if price already exists
108 if (it != levels_.end() && it->price == price) {
109 // Update existing level
110 it->quantity = quantity;
111 it->timestamp = timestamp;
112 return {it, false};
113 }
114
115 // Insert new level
116 it = levels_.insert(it, PriceLevelL2{price, quantity, timestamp});
117 return {it, true};
118 }
119
122 bool erase(Price price) noexcept {
123 auto it = find(price);
124 if (it != levels_.end()) {
125 levels_.erase(it);
126 return true;
127 }
128 return false;
129 }
130
132 iterator erase(iterator it) noexcept {
133 return levels_.erase(it);
134 }
135
137 void clear() noexcept {
138 levels_.clear();
139 }
140
142 void reserve(std::size_t capacity) {
143 levels_.reserve(capacity);
144 }
145
147 [[nodiscard]] std::size_t capacity() const noexcept {
148 return levels_.capacity();
149 }
150
153 [[nodiscard]] std::vector<PriceLevelL2> getLevels(std::size_t depth = 0) const {
154 if (depth == 0 || depth >= levels_.size()) {
155 return levels_;
156 }
157 return std::vector<PriceLevelL2>(levels_.begin(), levels_.begin() + depth);
158 }
159
161 [[nodiscard]] iterator begin() noexcept { return levels_.begin(); }
162 [[nodiscard]] iterator end() noexcept { return levels_.end(); }
163 [[nodiscard]] const_iterator begin() const noexcept { return levels_.begin(); }
164 [[nodiscard]] const_iterator end() const noexcept { return levels_.end(); }
165 [[nodiscard]] const_iterator cbegin() const noexcept { return levels_.cbegin(); }
166 [[nodiscard]] const_iterator cend() const noexcept { return levels_.cend(); }
167
168private:
169 Side side_; // Side (Buy or Sell)
170 Comparator compare_; // Comparison function for sorting
171 std::vector<PriceLevelL2> levels_; // Sorted vector of price levels
172};
173
Container for storing sorted price levels Uses a sorted vector for cache-friendly storage (optimal fo...
std::function< bool(const PriceLevelL2 &, const PriceLevelL2 &)> Comparator
const_iterator end() const noexcept
const_iterator begin() const noexcept
typename std::vector< PriceLevelL2 >::iterator iterator
LevelContainer(Side side, std::size_t initial_capacity=32)
Constructor with side and initial capacity.
iterator erase(iterator it) noexcept
Remove level by iterator.
iterator find(Price price) noexcept
Find level by price (binary search) Returns iterator to level with matching price,...
bool erase(Price price) noexcept
Remove level by price Returns true if level was found and removed.
Side side() const noexcept
Get Side.
std::size_t capacity() const noexcept
Get capacity.
const PriceLevelL2 * best() const noexcept
Get best level (first element)
const PriceLevelL2 & operator[](std::size_t index) const noexcept
Get level at index (no bounds checking)
bool empty() const noexcept
Check if empty.
LevelContainer()
Default constructor (for array initialization on MSVC)
typename std::vector< PriceLevelL2 >::const_iterator const_iterator
std::vector< PriceLevelL2 > getLevels(std::size_t depth=0) const
Get all levels up to depth.
const PriceLevelL2 & at(std::size_t index) const
Get level at index with bounds checking.
void clear() noexcept
Clear all levels.
const PriceLevelL2 * getLevel(Price price) const noexcept
Get level by price (returns pointer or nullptr)
const_iterator find(Price price) const noexcept
Find level by price (binary search, const version)
std::pair< iterator, bool > insertOrUpdate(Price price, Quantity quantity, Timestamp timestamp)
Insert or update a level If price exists, updates quantity; otherwise inserts new level Returns itera...
std::size_t size() const noexcept
Get number of levels.
const_iterator cend() const noexcept
void reserve(std::size_t capacity)
Reserve capacity.
const_iterator cbegin() const noexcept
#define SLICK_DETAIL_NAMESPACE_BEGIN
Definition config.hpp:129
#define SLICK_DETAIL_NAMESPACE_END
Definition config.hpp:130
int64_t Quantity
Quantity type - volume or size.
Definition types.hpp:21
int64_t Price
Price type - fixed-point representation Example: For 4 decimal places, price = actual_price * 10000 T...
Definition types.hpp:18
Side
Order side - using enum for easy array indexing.
Definition types.hpp:35
uint64_t Timestamp
Timestamp in nanoseconds since epoch.
Definition types.hpp:30
Comparator for sorting ask levels (ascending price)
Comparator for sorting bid levels (descending price)
Price level for Level 2 orderbook (aggregated) Stores total quantity at a specific price point.