Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a fast map having multiple keys?

I'm looking for a C++ associative map container type which I can perform multiple key lookups on. The map needs to have constant time lookups, but I don't care if it's ordered or unordered. It just needs to be fast.

For example, I want to store a bunch of std::vector objects in a map with an int and a void* as the lookup keys. Both the int and the void* must match for my vector to be retrieved.

Does such a container exist already? Or am I going to have to roll my own? If so, how could I implement it? I've been trying to store a boost::unordered_map inside another boost::unordered_map, but I have not had any success with this method, yet. Maybe I will continue Pershing this method if there is no simpler way.

like image 362
Morgan Avatar asked Apr 22 '10 16:04

Morgan


People also ask

Can a map have multiple keys?

Class MultiKeyMap<K,V> A Map implementation that uses multiple keys to map the value. This class is the most efficient way to uses multiple keys to map to a value.

How do you make a HashMap with two keys?

You can't have an hash map with multiple keys, but you can have an object that takes multiple parameters as the key. Create an object called Index that takes an x and y value. Then have your HashMap<Index, Value> to get your result. :) You need to override hashCode and equals .

What is the best data structure for mapping multiple keys to the same value?

HashMap can be one of the best data structures for mapping multiple keys to the same value.


2 Answers

Constant look up requires a hash map. You can use a the boost::unordered_map (or tr1). The key would be the combined hash of the int and the void pointer.

like image 154
pmr Avatar answered Oct 19 '22 21:10

pmr


If you don't want to use boost, you can try map< int, map<void*, vector> >. The lookups are however O(log(map size)).

like image 28
Vlad Avatar answered Oct 19 '22 22:10

Vlad