JAD - 4 months ago 29

C++ Question

I'm using the unordered_map from gnu++0x to store a huge amount of data. I want to pre-allocate space for the large number of elements, since I can bound the total space used.

What I would like to be able to do is call:

`std::unordered_map m;`

m.resize(pow(2,x));

where x is known.

unordered_map doesn't support this. I would rather use unordered_map if possible, since it will eventually be part of the standard.

Some other constraints:

Need reliable O(1) access and mutation of the map. The desired hash and comparison functions are already non-standard and somewhat expensive. O(log n) mutation (as with std::map) is too expensive.

-> The expensive hash and comparison also make amortization-based growth way too expensive. Each extra insert requires O(n) operations from those functions, which results in an extra quadratic term in the algorithm's run time, since the exponential storage requirements need O(n) growths.

Answer

```
m.rehash(pow(2,x));
```

if `pow(2, x)`

is the number of buckets you want preallocated. You can also:

```
m.reserve(pow(2,x));
```

but now `pow(2, x)`

is the number of elements you are planning on inserting. Both functions do nothing but preallocate buckets. They don't insert any elements. And they are both meant to be used exactly for your use case.

Note: You aren't guaranteed to get exactly `pow(2, x)`

buckets. Some implementations will use only a number of buckets which is a power of 2. Other implementations will use only a prime number of buckets. Still others will use only a subset of primes for the number of buckets. But in any case, the implementation should accept your *hint* at the number of buckets you desire, and then internally round up to its next acceptable number of buckets.