dimanche 29 mars 2015

Delete from set in constant time with pointers

Lets assume I have a structure of the form.



struct Order {
int id;
double price;
// more data
}


The ID is a unique identifier. Now I to keep track of these Orders in an OrderBook. I need to provide an insert (creates a new Order object) and an erase method (delete Order with certain id). The idea is of course to make is efficient as possible.



struct OrderBook {
unordered_map<int, Order> orders; // ID -> Orders
// ...
}


Additionally, I have to have fast access to the Order with the highest price in the current OrderBook.


I was thinking about containing a pair of the type pair<double, id> (for Price and ID) sorted lexicographically in a data structure (maybe set?). Insertion would be efficient (O(log n)) but since the data structure is ordered by price and not by ID finding and deleting an element from this data structure would be in (O(n)). So can I store a pointer in the Order object that points to the Price/ID pair? But that still doesn't delete it within the set right? Any ideas?


Aucun commentaire:

Enregistrer un commentaire