Ashley
Ashley

Reputation: 41

Multiple keys Hash Table (unordered_map)

I need to use multiple keys(int type) to store and retrieve a single value from a hash table. I would use multiple key to index a single item. I need fast insertion and look up for the hash table. By the way, I am not allowed to use the Boost library in the implementation.

How could I do that?

Upvotes: 3

Views: 15951

Answers (4)

aeh
aeh

Reputation: 775

If its always going to be a combination for retrieval.

Then its better to form a single compound key using multiple keys.

You can do this either

  1. Storing the key as a concatenated string of ints like

     (int1,int2,int3) => data
    
  2. Using a higher data type like uint64_t where in u can add individual values to form a key

    // Refer comment below for the approach
    

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54148

If the key to your container is comprised of the combination of multiple ints, you could use boost::tuple as your key, to encapsulate the ints without more work on your part. This holds provided your count of key int subcomponents is fixed.

Upvotes: 2

Yakov Galka
Yakov Galka

Reputation: 72479

If you mean that two ints form a single key then unordered_map<std::pair<int,int>, value_type>. If you want to index the same set of data by multiple keys then look at Boost.MultiIndex.

Upvotes: 4

jkerian
jkerian

Reputation: 17016

Easiest way is probably to keep a map of pointers/indexes to the elements in a list.

A few more details are needed here though, do you need to support deletion? how are the elements setup? Can you use boost::shared pointers? (rather helpful if you need to support deletion)

I'm assuming that the value object in this case is large, or there is some other reason you can't simply duplicate values in a regular map.

Upvotes: 1

Related Questions