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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! Hierarchical active-price bitmaps (ARCHITECTURE.md section 6.2).
//!
//! Level 0 holds one bit per tick index. Each higher level holds one
//! bit per word of the level below, set exactly when that child word is
//! non-zero. Updates rewrite the parent bit to `(child word != 0)` and
//! propagate only while a parent word actually changed, so the summary
//! invariant (INV-BITMAP-1) is a local fact, and best price discovery
//! descends the hierarchy in O(depth) word reads.
/// One side's active-level bitmap over the dense tick index space.
pub struct Bitmap {
/// `levels[0]` is the leaf level; the last level has exactly one
/// word. All levels are allocated once at construction.
levels: Vec<Vec<u64>>,
/// Number of tick indices covered (the book grid length).
grid_len: u32,
}
impl Bitmap {
/// Build the hierarchy for `grid_len` tick indices. `grid_len` is a
/// validated multiple of 64.
pub fn new(grid_len: u32) -> Self {
let mut levels = Vec::new();
let mut words = (grid_len / 64) as usize;
loop {
levels.push(vec![0u64; words]);
if words == 1 {
break;
}
words = words.div_ceil(64);
}
Self { levels, grid_len }
}
/// Number of hierarchy levels, root included.
pub const fn depth(&self) -> usize {
self.levels.len()
}
/// Read one word, for the invariant checker and tests.
pub fn word(&self, level: usize, word_idx: usize) -> u64 {
self.levels[level][word_idx]
}
/// Number of words at one level, for the invariant checker.
pub fn words_at(&self, level: usize) -> usize {
self.levels[level].len()
}
/// True when no tick index is active.
pub fn is_empty(&self) -> bool {
self.levels[self.levels.len() - 1][0] == 0
}
/// True when the tick index is active at the leaf level.
pub fn contains(&self, idx: u32) -> bool {
let word = self.levels[0][(idx / 64) as usize];
(word >> (idx % 64)) & 1 == 1
}
/// Mark a tick index active.
pub fn set(&mut self, idx: u32) {
self.update(idx, true);
}
/// Mark a tick index inactive.
pub fn clear(&mut self, idx: u32) {
self.update(idx, false);
}
/// Shared update path: rewrite the leaf bit, then rewrite each
/// parent bit to `(child word != 0)`, propagating only while the
/// parent word actually changed (the section 6.2 sync rule). The
/// parent bit is never blindly set or cleared: a word that still
/// has other bits keeps its parent bit set.
fn update(&mut self, idx: u32, active: bool) {
let mut word_idx = (idx / 64) as usize;
let bit = idx % 64;
let before = self.levels[0][word_idx];
let after = if active {
before | (1u64 << bit)
} else {
before & !(1u64 << bit)
};
if after == before {
return; // leaf unchanged: every ancestor already agrees
}
self.levels[0][word_idx] = after;
let mut child_nonzero = after != 0;
for level in 1..self.levels.len() {
let parent_bit = u32::try_from(word_idx % 64).unwrap_or(0);
word_idx /= 64;
let before = self.levels[level][word_idx];
let after = if child_nonzero {
before | (1u64 << parent_bit)
} else {
before & !(1u64 << parent_bit)
};
if after == before {
return; // ancestors already reflect this word
}
self.levels[level][word_idx] = after;
child_nonzero = after != 0;
}
}
/// The highest active tick index, or `None` when empty.
pub fn highest(&self) -> Option<u32> {
self.descend(true)
}
/// The lowest active tick index, or `None` when empty.
pub fn lowest(&self) -> Option<u32> {
self.descend(false)
}
/// Descend the hierarchy from the root, choosing the highest set
/// bit (for best bid) or the lowest set bit (for best ask) at each
/// level.
fn descend(&self, want_highest: bool) -> Option<u32> {
let mut word_idx = 0usize;
for level in (0..self.levels.len()).rev() {
let word = self.levels[level][word_idx];
if word == 0 {
return None;
}
let bit = if want_highest {
word.ilog2()
} else {
word.trailing_zeros()
};
if level == 0 {
let idx = u32::try_from(word_idx * 64 + bit as usize).unwrap_or(u32::MAX);
return if idx < self.grid_len { Some(idx) } else { None };
}
word_idx = word_idx * 64 + bit as usize;
}
None
}
/// The highest active tick index strictly below `exclusive`, by
/// leaf-word scan. Used by L2 depth iteration.
pub fn highest_below(&self, exclusive: u32) -> Option<u32> {
if exclusive == 0 {
return None;
}
let leaf = &self.levels[0];
let mut word_idx = (exclusive / 64) as usize;
let offset = exclusive % 64;
// Bits at or above the offset are out of range; with offset 0
// the whole starting word is out of range and the scan simply
// steps down from the word below it.
let mut candidate = if offset == 0 {
0
} else {
leaf[word_idx] & ((1u64 << offset) - 1)
};
loop {
if candidate != 0 {
let idx = word_idx * 64 + candidate.ilog2() as usize;
return Some(u32::try_from(idx).unwrap_or(u32::MAX));
}
if word_idx == 0 {
return None;
}
word_idx -= 1;
candidate = leaf[word_idx];
}
}
/// The lowest active tick index strictly above `exclusive`, by
/// leaf-word scan. Used by L2 depth iteration.
pub fn lowest_above(&self, exclusive: u32) -> Option<u32> {
let leaf = &self.levels[0];
let mut word_idx = (exclusive / 64) as usize;
let offset = exclusive % 64;
// Bits at or below the offset are out of range; with offset 63
// the whole starting word is out of range and the scan simply
// steps up from the word above it.
let mut candidate = if offset == 63 {
0
} else {
leaf[word_idx] & (!0u64 << (offset + 1))
};
loop {
if candidate != 0 {
let idx = word_idx * 64 + candidate.trailing_zeros() as usize;
return Some(u32::try_from(idx).unwrap_or(u32::MAX));
}
word_idx += 1;
if word_idx >= leaf.len() {
return None;
}
candidate = leaf[word_idx];
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Independent summary check: every parent bit equals (child != 0).
fn assert_summary(bitmap: &Bitmap) {
for level in 1..bitmap.depth() {
for w in 0..bitmap.words_at(level) {
let word = bitmap.word(level, w);
for bit in 0..64usize {
let child_idx = w * 64 + bit;
if child_idx >= bitmap.words_at(level - 1) {
assert_eq!((word >> bit) & 1, 0, "padding bit set");
} else {
let expect = u64::from(bitmap.word(level - 1, child_idx) != 0);
assert_eq!(
(word >> bit) & 1,
expect,
"summary mismatch at level {level} word {w} bit {bit}"
);
}
}
}
}
}
#[test]
fn bitmap_empty_has_no_best() {
let bitmap = Bitmap::new(4096);
assert!(bitmap.is_empty());
assert_eq!(bitmap.highest(), None);
assert_eq!(bitmap.lowest(), None);
assert_summary(&bitmap);
}
#[test]
fn bitmap_best_tracks_sets_and_clears() {
let mut bitmap = Bitmap::new(4096);
bitmap.set(10);
bitmap.set(4000);
bitmap.set(64);
assert_eq!(bitmap.highest(), Some(4000));
assert_eq!(bitmap.lowest(), Some(10));
assert_summary(&bitmap);
bitmap.clear(4000);
assert_eq!(bitmap.highest(), Some(64));
assert_summary(&bitmap);
bitmap.clear(10);
bitmap.clear(64);
assert!(bitmap.is_empty());
assert_summary(&bitmap);
}
#[test]
fn bitmap_clearing_one_bit_keeps_sibling_bits() {
// Regression: the parent bit must be rewritten to
// (child word != 0), never blindly cleared. Two indices in one
// leaf word; clearing one must keep the other discoverable.
let mut bitmap = Bitmap::new(4096);
bitmap.set(99);
bitmap.set(100);
bitmap.clear(99);
assert_eq!(bitmap.highest(), Some(100));
assert_eq!(bitmap.lowest(), Some(100));
assert!(bitmap.contains(100));
assert!(!bitmap.contains(99));
assert_summary(&bitmap);
}
#[test]
fn bitmap_summary_sync_on_last_order_removal() {
// The engine-level name for this edge is
// bitmap_summary_sync_on_last_order_removal: clearing the final
// active index must propagate zeros all the way to the root.
let mut bitmap = Bitmap::new(131_072);
bitmap.set(65_537);
assert!(!bitmap.is_empty());
bitmap.clear(65_537);
for level in 0..bitmap.depth() {
for w in 0..bitmap.words_at(level) {
assert_eq!(
bitmap.word(level, w),
0,
"level {level} word {w} not zeroed"
);
}
}
}
#[test]
fn bitmap_neighbours_scan_leaf_words() {
let mut bitmap = Bitmap::new(512);
for idx in [3u32, 70, 129, 500] {
bitmap.set(idx);
}
assert_eq!(bitmap.highest_below(500), Some(129));
assert_eq!(bitmap.highest_below(129), Some(70));
assert_eq!(bitmap.highest_below(3), None);
assert_eq!(bitmap.lowest_above(3), Some(70));
assert_eq!(bitmap.lowest_above(129), Some(500));
assert_eq!(bitmap.lowest_above(500), None);
assert_eq!(bitmap.lowest_above(0), Some(3));
}
}