Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fixed size unordered_map, how to define?

Is it possible to define a fixed size unordered_map?

Looking at the member functions, there is no resize() similar to std::vector and std::list. Also, google didn't help me.

like image 868
mahmood Avatar asked Jan 19 '13 19:01

mahmood


People also ask

What is default size of unordered_map?

By default, unordered_map containers have a max_load_factor of 1.0.

How much space does unordered_map take?

In libc++ sizeof map is 24 whereas sizeof unordered_map is 40.

Can unordered_map be sorted?

From a logical standpoint, sorting an unordered container makes no sense. It's unordered. And the complexity guarantees that unordered_map is able to achieve require a very specific ordering that you shouldn't be, and aren't, allowed to mess with.

How does unordered_map store values?

As I know, std::unordered_map is used for fast access to elements. This achieves by storing and comparing the key hash instead of the key itself. Also, unordered means that the elements in it are not sorted.


Video Answer


1 Answers

Yes, it is possible but there is no such map in the STL. What you can do is write your own class containing an std::array< std::pair<Key, Value>, N> and provide most of the find(), insert() functionality using std::hash yourself. If you use a std::vector< std::pair<Key, Value> > as data member, you could even have a resize() function to only expand the table explicitly, but not implicitly after an insert().

An important thing to realize is that you also need to provide a way to iterate over the various elements, in order to satisfy all the container requirements. Typically this is done by having auxiliary data that implements a linked list over all the stored elements.

One issue you need to resolve, however, is which replacement policy you use to replace items if your array is full. The std::unorderd_map uses so-called chaining, with -for each entry- a dynamically sized bucket (with at least forward iteration, so at least equivalent to forward_list). Most chess programs have a fixed-size hash table with a replacement policy to always replace an item if a particular table entry is already occupied.

like image 103
TemplateRex Avatar answered Nov 05 '22 11:11

TemplateRex