aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::HashFuncSmallKey< Key > Class Template Reference

Generic hash functions for numeric keys smaller than or equal to Size. More...

#include <agrum/base/core/hashFunc.h>

Inheritance diagram for gum::HashFuncSmallKey< Key >:
Collaboration diagram for gum::HashFuncSmallKey< Key >:

Public Member Functions

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

Static Public Member Functions

static Size castToSize (const Key &key)
 Returns the value of a key as a Size.

Protected Attributes

Size hash_size_ {Size(0)}
 The size of the hash table.
unsigned int hash_log2_size_ {0}
 Log of the number of slots of the hash table in base 2.
Size hash_mask_ {Size(0)}
 performing y = x & hash_mask_ guarantees that y is a slot index of the hash table
unsigned int right_shift_ {0}
 performing y = x >> right_shift_ guarantees that y is a slot index of the hash table

Detailed Description

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

Generic hash functions for numeric keys smaller than or equal to Size.

Template Parameters
KeyThe type hashed by this hash function.

Definition at line 261 of file hashFunc.h.

Constructor & Destructor Documentation

◆ HashFuncSmallKey()

template<typename Key>
gum::HashFuncSmallKey< Key >::HashFuncSmallKey ( )

Class constructor.

Member Function Documentation

◆ castToSize()

template<typename Key>
Size gum::HashFuncSmallKey< Key >::castToSize ( const Key & key)
static

Returns the value of a key as a Size.

Parameters
keyThe value to return as a Size.
Returns
Returns the value of a key as a Size.

◆ operator()()

template<typename Key>
virtual Size gum::HashFuncSmallKey< Key >::operator() ( const Key & key) const
finaloverridevirtual

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;
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

and its implementation should be something like:

INLINE Size HashFunc< Key >::operator()(const Key& key) const {
return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
}
unsigned int right_shift_
performing y = x >> right_shift_ guarantees that y is a slot index of the hash table
Definition hashFunc.h:246
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
static constexpr Size gold
Definition hashFunc.h:98

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.

Implements gum::HashFuncBase< Key >.

◆ resize()

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

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
inherited

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}
protectedinherited

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

Definition at line 224 of file hashFunc.h.

224{0};

◆ hash_mask_

template<typename Key>
Size gum::HashFuncBase< Key >::hash_mask_ {Size(0)}
protectedinherited

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 235 of file hashFunc.h.

235{Size(0)};

◆ hash_size_

template<typename Key>
Size gum::HashFuncBase< Key >::hash_size_ {Size(0)}
protectedinherited

The size of the hash table.

Definition at line 221 of file hashFunc.h.

221{Size(0)};

◆ right_shift_

template<typename Key>
unsigned int gum::HashFuncBase< Key >::right_shift_ {0}
protectedinherited

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 246 of file hashFunc.h.

246{0};

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