Loading...
Searching...
No Matches
hash.hh File Reference

Support for calculating hash values of objects. More...

`#include <functional>`
`#include <dune/common/typetraits.hh>`

Go to the source code of this file.

## Classes

struct | Dune::hash< T >
| Functor for hashing objects of type T. More...

## Namespaces

namespace | Dune
| Dune namespace

## Macros

#define | DUNE_DEFINE_HASH(template_args, type)
| Defines the required struct specialization to make type hashable via Dune::hash.
#define | DUNE_HASH_TEMPLATE_ARGS(...)
| Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
#define | DUNE_HASH_TYPE(...)
| Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.

## Functions

template<typename T>
void | Dune::hash_combine (std::size_t &seed, const T &arg)
| Calculates the hash value of arg and combines it in-place with seed.
template<typename It>
std::size_t | Dune::hash_range (It first, It last)
| Hashes all elements in the range [first,last) and returns the combined hash.
template<typename It>
void | Dune::hash_range (std::size_t &seed, It first, It last)
| Hashes all elements in the range [first,last) and combines the hashes in-place with seed.

## Detailed Description

Support for calculating hash values of objects.

This file provides the functor Dune::hash to calculate hash values and some infrastructure to simplify extending Dune::hash for user-defined types, independent of the actual underlying implementation.

## Macro Definition Documentation

## DUNE_DEFINE_HASH

#define DUNE_DEFINE_HASH | ( |  | template_args,
|  |  | type )

Defines the required struct specialization to make type hashable via Dune::hash.

In order to calculate the hash, operator() of the generated specialization will return the result of an unqualified call to the global function hash_value(const type&). As the call is not qualified, the function will be found using argument-dependent lookup, allowing implementors to conveniently place it inside the class body.

Consider the following type:

namespace ns {
template<typename A, int i>
class Foo
{
...
};
}

In order to add support for Dune::hash, you need to extend the definition like this:

namespace ns {
template<typename A, int i>
class Foo
{
...
// The keyword "friend" turns this into a global function that is a friend of Foo.
inline friend std::size_t hash_value(const Foo& arg)
{
return ...;
}
};
}
// Define hash struct specialization
DUNE_DEFINE_HASH(DUNE_HASH_TEMPLATE_ARGS(typename A, int i),DUNE_HASH_TYPE(Foo<A,i>))
DUNE_DEFINE_HASH
#define DUNE_DEFINE_HASH(template_args, type)
Defines the required struct specialization to make type hashable via Dune::hash.
Definition hash.hh:100
DUNE_HASH_TYPE
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition hash.hh:117
DUNE_HASH_TEMPLATE_ARGS
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition hash.hh:109

Warning
  As the specialization has to be placed in the original namespace of the hash struct (e.g. std), this macro must be called from the global namespace!

Parameters

  template_args | The template arguments required by the hash struct specialization, wrapped in a call to DUNE_HASH_TEMPLATE_ARGS. If this is a complete specialization, call DUNE_HASH_TEMPLATE_ARGS without arguments.
  type | The exact type of the specialization, wrapped in a call to DUNE_HASH_TYPE.

## DUNE_HASH_TEMPLATE_ARGS

#define DUNE_HASH_TEMPLATE_ARGS | ( |  | ... | ) |

Wrapper macro for the template arguments in DUNE_DEFINE_HASH.

This macro should always be used as a wrapper for the template arguments when calling DUNE_DEFINE_HASH. It works around some preprocessor limitations when the template arguments contain commas or the list is completely empty.

## DUNE_HASH_TYPE

#define DUNE_HASH_TYPE | ( |  | ... | ) |

Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.

This macro should always be used as a wrapper for the type of the specialization when calling DUNE_DEFINE_HASH. It works around some preprocessor limitations when the type contains commas.