siplasplas
A library for C++ reflection and introspection

Hashing utilities based on std::hash. More...

Classes

struct  cpp::Hash< T >
 A functor that implements a hash function for values of type T. More...
 
class  cpp::RawHash< T >
 A functor that implements a bytewise hash function for values of type T. More...
 

Typedefs

template<typename Key , typename Value >
using cpp::HashTable = std::unordered_map< Key, Value, Hash< Key >>
 std::unordered_map alias using cpp::Hash as hash. More...
 
template<typename Key >
using cpp::HashSet = std::unordered_set< Key, Hash< Key >>
 std::set alias using cpp::Hash as hash. More...
 

Functions

template<typename T >
constexpr std::size_t cpp::hash (const T &value)
 Implements a hash function for values of type T. More...
 
template<typename T , typename U , typename... Args>
constexpr std::size_t cpp::hash (const T &first, const U &second, const Args &...tail)
 Returns the comination of hashes of a set of values. More...
 
template<typename T >
std::size_t cpp::raw_hash (const T &value)
 Returns a bytewise hash of a given value. More...
 
template<typename T >
constexpr std::size_t cpp::hash_combine (std::size_t seed, const T &value)
 Implements a hash combination function of a given hash value and a value of type T. More...
 

Detailed Description

Hashing utilities based on std::hash.

This implements the following hashing tools:

Typedef Documentation

template<typename Key >
using cpp::HashSet = typedef std::unordered_set<Key, Hash<Key>>

std::set alias using cpp::Hash as hash.

Template Parameters
KeyKey type.
template<typename Key , typename Value >
using cpp::HashTable = typedef std::unordered_map<Key, Value, Hash<Key>>

std::unordered_map alias using cpp::Hash as hash.

Template Parameters
KeyKey type.
ValueValue type.

Function Documentation

template<typename T >
constexpr std::size_t cpp::hash ( const T &  value)

Implements a hash function for values of type T.

Parameters
valueValue to compute the hash code.
Returns
For enumeration types returns the hash of the underlying integral value. For function types returns a bytewise hash of the function object (See cpp::raw_hash()). For tuple and pair types returns a hash combination of all tuple elements (See cpp::hash_combine() and n-ary cpp::hash() overload). Otherwise returns the value given by std::hash<T>.
template<typename T , typename U , typename... Args>
constexpr std::size_t cpp::hash ( const T &  first,
const U &  second,
const Args &...  tail 
)

Returns the comination of hashes of a set of values.

This function is a generalization of unary cpp::hash() that accepts two or more input values. The resulting hash code is computed as the hash combination (See cpp::hash_combine()) of the first value and the hash combination of the rest:

hash first:second:tail -> hash_combine (hash first) (hash second:tail)
Parameters
valuesA variadic pack of at least two elements. These are the values to get the combined hash code.
Returns
Pairwise hash combination of the values. The hash of each value is computed by cpp::hash().
template<typename T >
constexpr std::size_t cpp::hash_combine ( std::size_t  seed,
const T &  value 
)

Implements a hash combination function of a given hash value and a value of type T.

Literally copied from this stack overflow thread which in turn got it from boost::hash_combine().

Parameters
seedSource hash value to combine.
valueValue of type T wich has will be combined.
Returns
the combination between the seed value and the hash of the input value. See boost::hash_combine() link above for the specific combination function.
template<typename T >
std::size_t cpp::raw_hash ( const T &  value)

Returns a bytewise hash of a given value.

This function ignores the std::hash specialization of the value type and implements a bytewise hash value instead. Bytewise hash is computed as a hash combination of each byte of the value storage, in the range [addressof(value), addressof(value) + sizeof(T)). The value is copyed to an intermediary aligned storage to perform the byte traversal.

Returns
A value equivalent to cpp::hash(<bytes of value>...).