Hashing utilities based on std::hash
.
More...
|
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...
|
|
|
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...
|
|
Hashing utilities based on std::hash
.
This implements the following hashing tools:
- Free hash function: A cpp::hash() free function template for simple hash code retrieval. Because
std::hash<int>()(42)
is horrible.
- Hash combination: cpp::hash() is really a variadic template function that implements pairwise hash combination.
- Improved hash functor: cpp::Hash implements a hash functor for maps that wraps cpp::hash() functionallity. The extended functionallity of cpp::Hash over
std::hash
includes hashing of enum types, pairs, and tuples out of the box.
- Hashing of function pointers: Because we love undefined behavior!
template<typename Key >
using cpp::HashSet = typedef std::unordered_set<Key, Hash<Key>> |
std::set
alias using cpp::Hash as hash.
- Template Parameters
-
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
-
Key | Key type. |
Value | Value type. |
template<typename T >
constexpr std::size_t cpp::hash |
( |
const T & |
value | ) |
|
Implements a hash function for values of type T.
- Parameters
-
value | Value 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:
- Parameters
-
values | A 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
-
seed | Source hash value to combine. |
value | Value 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>...)
.