aGrUM  0.16.0
gum::HashFuncBase< Key > Class Template Referenceabstract

All hash functions should inherit from this class. More...

#include <agrum/core/hashFunc.h>

+ Inheritance diagram for gum::HashFuncBase< Key >:

Public Member Functions

void resize (const Size new_size)
 Update the hash function to take into account a resize of the hash table. More...
 
Size size () const
 Returns the hash table size as known by the hash function. More...
 
virtual Size operator() (const Key &key) const =0
 Computes the hashed value of a key. More...
 

Protected Attributes

Size _hash_size {Size(0)}
 The size of the hash table. More...
 
unsigned int _hash_log2_size {0}
 Log of the number of slots of the hash table in base 2. More...
 
Size _hash_mask {Size(0)}
 performing y = x & _hash_mask guarantees that y is a slot index of the hash table More...
 
unsigned int _right_shift {0}
 performing y = x >> _right_shift guarantees that y is a slot index of the hash table More...
 

Detailed Description

template<typename Key>
class gum::HashFuncBase< Key >

All hash functions should inherit from this class.

Whenever you create a new hash function you must inherit from this class. Otherwise, your hash function will not compile because gum::HashTable will refer directly to this class.

The way gum::HashTable work, you do not need to define constructors, destructors and assignment operators: the defaults created by C++ will work correctly. However, if your hash function contains new attributes, you must override the resize function to properly set these attributes. You may even have to redefine the default constructor and/or the assignment operator, but this should not occur very often.

In fact, usually, when you create a new hash function, you will only need to write something like:

template <> class HashFunc<MyObject> : public HashFuncBase<MyObject> {
public:
Size operator() (const MyObject& key) const {
// here write the code using
// HashFuncBase<MyObject>::_hash_size and/or
// HashFuncBase<MyObject>::_hash_log2_size and/or
// HashFuncBase<MyObject>::_hash_mask
}
};

For instance, here is how HashFunc<string> is implemented:

template <> class HashFunc<std::string>: public HashFuncBase<std::string> {
public:
Size operator() (const std::string& key) const {
Size h = 0;
for (size_t i = 0, j = key.size(); i < j; ++i) {
h = 19 * h + key[i];
}
return ((h * GUM_HASHTABLE_INT_GOLD) & _hash_mask);
}
};

The gum::HashFunc::_hash_size attribute corresponds to the number of slots in the gum::HashTable. Your function should always return a number in [0,_hash_size). As the number of slots in a gum::HashTable is always a power of 2, it may be convenient to know the number of bits used by the hashed keys, this is precisely the information contained in gum::HashFunc::_hash_log2_size. Finally, gum::HashFunc::_hash_mask is a mask that can be used to ensure that the hashed key actually belongs to [0,_hash_size). This is used in particular in the hash function for hashing strings.

Template Parameters
KeyThe type hashed by this hash function.

Definition at line 149 of file hashFunc.h.

Member Function Documentation

◆ operator()()

template<typename Key>
virtual Size gum::HashFuncBase< Key >::operator() ( const Key &  key) const
pure virtual

Computes the hashed value of a key.

The classes inheriting from HashFuncBase should always declare Operator() as follows:

Size operator()(const Key& key) const override final;

and its implementation should be something like:

INLINE Size HashFunc< Key >::operator()(const Key& key) const {
return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
}

By doing this, compilers optimize the code so that it is significantly speeded-up because no virtual table will be used and everything is most certainly inlined. Of course, you need to define a static method castToSize which should take as argument a const Key& and return a Size

Parameters
keyThe key to compute the hashed value.
Returns
Returns the hashed value of a key.

Implemented in gum::HashFunc< Instantiation >, gum::HashFunc< Set< T, Alloc > >, gum::HashFunc< RefPtr< Type > >, gum::HashFunc< Debug >, gum::HashFunc< std::vector< Idx > >, gum::HashFunc< std::string >, gum::HashFunc< learning::EdgeDeletion >, gum::HashFunc< learning::EdgeAddition >, gum::HashFunc< learning::ArcReversal >, gum::HashFunc< learning::ArcDeletion >, gum::HashFunc< learning::ArcAddition >, gum::HashFunc< std::pair< Key1, Key2 > >, gum::HashFunc< learning::GraphChange >, gum::HashFunc< learning::IdSet< ALLOC > >, gum::HashFuncLargeCastKey< Key >, gum::HashFuncMediumCastKey< Key >, gum::HashFuncSmallCastKey< Key >, gum::HashFuncSmallKey< Key >, gum::HashFuncSmallKey< long >, gum::HashFuncSmallKey< unsigned long >, gum::HashFuncSmallKey< int >, gum::HashFuncSmallKey< unsigned int >, gum::HashFuncSmallKey< bool >, gum::HashFunc< credal::lp::LpCol >, and gum::HashFunc< std::tuple< unsigned int, unsigned int, unsigned int > >.

◆ resize()

template<typename Key>
void gum::HashFuncBase< Key >::resize ( const Size  new_size)

Update the hash function to take into account a resize of the hash table.

When the user wishes to resize the gum::HashTable so that the array is of size s, the gum::HashTable resizes itself to the smallest power of 2 greater than or equal to s. This new size is computed by function gum::HashFuncBase::resize(gum::Size). Hence, s should be the size of the array of lists, not the number of elements stored into the gum::HashTable.

Parameters
new_sizeThe hashtable's size wished by the user. Actually, a hashtable of size n is an array of n lists.
Exceptions
SizeErrorRaised if s is too small.

◆ size()

template<typename Key>
Size gum::HashFuncBase< Key >::size ( ) const

Returns the hash table size as known by the hash function.

Returns
Returns the size of the hash table, i.e., the number of slots (lists) it contains.

Member Data Documentation

◆ _hash_log2_size

template<typename Key>
unsigned int gum::HashFuncBase< Key >::_hash_log2_size {0}
protected

Log of the number of slots of the hash table in base 2.

Definition at line 204 of file hashFunc.h.

◆ _hash_mask

template<typename Key>
Size gum::HashFuncBase< Key >::_hash_mask {Size(0)}
protected

performing y = x & _hash_mask guarantees that y is a slot index of the hash table

To transform a Size x into a slot index of the hash table, you can either use x & _hash_mask or x >> _right_shift depending on whether you want to exploit the least significant bits of x (&) or the most significant one (>>).

Definition at line 215 of file hashFunc.h.

◆ _hash_size

template<typename Key>
Size gum::HashFuncBase< Key >::_hash_size {Size(0)}
protected

The size of the hash table.

Definition at line 201 of file hashFunc.h.

◆ _right_shift

template<typename Key>
unsigned int gum::HashFuncBase< Key >::_right_shift {0}
protected

performing y = x >> _right_shift guarantees that y is a slot index of the hash table

To transform a Size x into a slot index of the hash table, you can either use x & _hash_mask or x >> _right_shift depending on whether you want to exploit the least significant bits of x (&) or the most significant one (>>).

Definition at line 226 of file hashFunc.h.


The documentation for this class was generated from the following file: