views:

55

answers:

4

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?

Thanks.

A: 

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.

jkerian
+1  A: 

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.

ybungalobill
+1  A: 

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.

Steve Townsend
Thanks for your reply. I have updated my question regarding I am not allowed to use Boost library.
Ashley
@Ashley - why not? Without Boost you would not have ready access to unordered_map.
Steve Townsend
I think the unordered_map I have access to currently is the implementation from the GNU C++ library.
Ashley
A: 

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
    
aeh
Approach #2 is good, for a better explanation see http://stackoverflow.com/questions/3761914/storing-and-retrieving-multiple-keys-in-c/3762694#3762694 ; the formula seems incorrect, it might be (assuming `N` being width of int in bits): `( (int1 << 2*N) + (int2 << N) + int3 )` provided `data` has at least `3*N` bits.
ArunSaha