Cancel without searching
transcript
An order book holds bids and asks. Orders can be cancelled at any time. A heap cannot remove from the middle.
Bids sit in a heap with the highest on top. Asks sit in a heap with the lowest on top. A set holds the ids of live orders.
A cancel only removes the id from the live set. The heap entry stays where it is. That is all the cancel does.
To read the best price, look at the top. Check whether that order is still live. If not, throw it away and look again.
Each order enters a heap once. Each order is thrown away at most once. So every operation stays fast.
A cancel of an unknown id changes nothing. A second cancel changes nothing either. A side with no live order reports minus one.
In a real market, orders change constantly. The best price is read all the time. So removal has to be cheap.
Mark the cancel in a set. Discard at the top. Full breakdown in the lecture.