Creating shared hash tables - C ++

The .NET Framework has a dictionary <TKey, TValue> class, which is implemented as a hash table and provides data in constant time (O (1)). I am looking for a similar implementation in C ++. I know about std :: map, but this data search requires logarithmic time. Is there a good hash table implementation in C ++ that will receive data in constant time?

If I write my own, how will I calculate the hash code for the key? Like .NET, I was thinking of the GetHashCode () method for types.

template<typename TKey,typename TVal>
class Dictionary
{
public:
   void Add(TKey key, TVal val){
       int hashCode = key.GetHashCode();
       /* .... */
   }
}

If I liked it above, and this type of key does not have the GetHashCode () method, the compiler will throw an error. But this method will not work if the key is a primitive type of type int. I may need to write a wrapper for int, providing GetHashCode.

, ++- ?

?

+3
6

, ++ 1 std::tr1::unordered_map, ++.

std::hash_map ++, .

+8
+7

, STL/TR1 Boost google-sparsehash ( - ).

, GetHashCode ,

template < typename TKey, typename TValue, typename THash = someDefaultHash<TKey> >
class Dictionary
{
public:
   void Add(TKey key, TVal val){
       int hashCode = THash()(key);
       /* .... */
   }
}

SGI hash_map .

+4

, std:: hash_map?

, STL, . MS SGI ( ) STLport.

+2

++ , - .

+2

, VS 2008 Feature Pack 1, TR1, TR1:: unordered_map.

OTOH, if you really are not creating a huge collection, chances are std :: map will be more competitive than you expect. In my tests, this is pretty common for the two to almost bind, and std :: map can and does go faster faster.

+2
source

Source: https://habr.com/ru/post/1719603/


All Articles