Reputation: 23619
I need a data structure in which I want to store information about which instances I already processed during an action. Because of limitations I can't store it in the instance itself (e.g. because I can execute the action in parallel.
What's specific is that the instances for which I want to store information have a unique number, so instead of a pointer to the instance, I could use that unique number to store information.
My first solution was to use an std::set<Instance *>
. Every time i process an instance, I add it to the set so that I know that I already processed that instance.
My second soluction was to use an std::vector<bool>
(actually std::vector<byte>
because bool vectors have a specific specialization which makes it slower than a non-bool vector). The unique number of the instance can be used as index into the vector, and in the vector simply contains true or false to indicate if we already processed the instance or not (luckily my unique numbers start to count from 1).
I could also use a C-style array (on which I can use memset), but since the number of instances (or the number of unique numbers) is not known beforehand, I need to write my own code to extend the array, memset the rest of the array, ... (which is not very hard, but which is something I want to avoid).
Is there any other kind of data structure that is very fast to initialize, and has O(1) lookup time?
Upvotes: 2
Views: 243
Reputation: 299890
Well, with such a simple identification method... I would use a hash table.
Can you not use boost::unordered_map
or std::unordered_map
?
Of course, you might prefer more sophisticated implementations if you want guaranteed O(1) insertion instead of amortized O(1) insertion, but it should get you started.
Upvotes: 5
Reputation:
You may try boost::unordered_set
or the new C++11 std::unordered_set
. They are hashed based containers rather than trees like std::set.
Upvotes: 8