I have a scenario where I'm pushing change-lists to another system. Each list contains zero or more inserted, updated or deleted notifications.
Inserting is easy; the notification contains the target index and a pointer to the item. Updating is easy; I pass a pointer to the item.
Deleting seems straight-forward; I need to pass the index of the item to delete, but how do I know the index? Indexes start at zero and must be contiguous, but I make them up at insertion time. So I need to keep track of the index I make up for each item.
I can do this with, for example, a map: std::map<item*, int>
, but then when I remove an item, I have to go re-number everything past it, which is O(N).
These lists of items are going to be large to the point where O(N) iteration is not acceptable. I'm sure this problem has been solved, I just don't know what the solution would be called. Searching for anything related to "linked list" creates a ton of noise.
One possible solution is a skip-list, where each node in the sublists knows how many nodes in the main list it skips, and since searching a skip list is O(log N) we can keep track as we go and find the index in O(log N) and also delete items in O(log N).
However implementing a skip-list seems like overkill here... is there a simpler solution?
EDIT:
Thanks all for your suggestions, but I think I've convinced myself the skip list is the right way to solve this problem here.