My question is really simple - having unordered_map<int, int>
I need to track maximum key
with minimum overhead. Not at any moment, but at certain "checkpoints". More details below.
In my trading application I would like to use unordered_map
to store OrderBook items. It should map int
to int
where key is price and value is volume. (actually price is decimal but i'm using int64 internally)
I'm going to use two unordered_map
- one for bid
orders and one for offer
orders.
I need to know Bid
- maximum key of one map and Offer
minimum key of another map. Normally I have "several" updates in one pack - and I need to recalculate Bid and Offer only when all updates are processed. For example assume I have such orderbook
100 1
102 1
104 1
Bid (max of key) is 104.
Now I have two updates:
remove key 104
add 103 1
end of transaction
Executing should be as follows:
1:
100 1
102 1
Bid undefined (transaction in progress)
2:
100 1
102 1
103 1
Bid = 103
How can I do that with minimum latency ? Latency is primary requirement, if ignore latency I can do a lot of things:
- trivial: just scan all keys and find best bid/ask if previous one was deleted
- i even can use sorted map
So it seems i need some simple algorithm to track max / min of some set without recalculating it every time?