I want a structure that can :-
- add new element fast (an element is always a pointer, not data)
- remove existing element fast using only information where the pointer points to.
(B* b1,B* b2, test b1==b2 directly)
The removing tends to occur in random order (all over places).
User callsremove(T*)
. - iterate fast
The data structure that suits the criteria seems to be HashSet
.
However, Hashset
suffers these disadvantages :-
- Adding an element require hash function which is slow.
- Adding an element : need to access some kind of basket management, even more slow.
- Removing an element suffers the same symptom as adding.
- Iterating suffers from basket access
O(size of basket)
, and have to travel pass empty basket.
I tested it (n = 500 to 1000), it is very slower than array.
Question
What data structure can support this requirement?
My poor solution
Only solution I found is to store index directly into the element.
It is fast and meet all the criteria, but seems to be a hack.
With this hack, there is an additional strange limitation.
-> A certain element can only be contained in one DataStructure.
If I want more, the hacking become more complicate.
class B{
int indexSlot1_;
public: static int& indexSlot1(B* b){ return b->indexSlot1_; }
//... provide additional slots if required ....
}
Datastructure<B*,&B::indexSlot1> database1;
Datastructure<B*,&B::indexSlot2> database2;