Note that there are some explanatory texts on larger screens.

plurals
  1. POLooking for a data structure that's fast to initialize and fast to lookup (O(1))
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>My first solution was to use an <code>std::set&lt;Instance *&gt;</code>. Every time i process an instance, I add it to the set so that I know that I already processed that instance.</p> <ul> <li>Advantage: this is very fast to initialize</li> <li>Disadvantage: lookups are not O(1), but O(logN)</li> </ul> <p>My second soluction was to use an <code>std::vector&lt;bool&gt;</code> (actually <code>std::vector&lt;byte&gt;</code> 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).</p> <ul> <li>Advantage: lookups are O(1)</li> <li>Disadvantage: initialization if relatively slow, since std::vector needs to initialize every element explicitly (and probably also independently)</li> </ul> <p>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).</p> <p>Is there any other kind of data structure that is very fast to initialize, and has O(1) lookup time?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload