I’m looking for a data structure that has the following properties.
- Stores a list of
tuple<Double,Integer,Integer>. Order is only ondouble. Two tuples with the same double value are consider the same. - Supports duplicates.
- Needs to be able to traverse in ascending order. If there are duplicates, the one added later should have higher order.
- Find/Insert fast
- Remove fast, note that remove always follows this pattern
Method contains remove:
for(int i=list.size()-1;i>=0;i--){// assume list is in ascending order
if(list[j:i] can be merged){
remove list[j:i-1];
update list[i]'s two integers;
i = j-1;
}
}
I currently use ArrayList and keep it sorted. Finding is fast with binary search. However insertion and deletion will involve lots of copy in memory, e.g. insertion in the front of the list shifts all the elements.
One solution would be to have a sorted map to lists of tuples:
The declaration line is bit ugly, but it will work. I’ve used maps to lists many times before. The nice thing about it is that you can then delete items from the lists and as long as your lists are each short, you have a smaller number of moves. To iterate over the entire structure, you’d need to create your own iterator, or adapt your original code.