MADNESS  version 0.9
Classes | Namespaces | Typedefs | Functions
worldhash.h File Reference

Defines hash functions for use in distributed containers. More...

#include <madness/madness_config.h>
#include <madness/world/typestuff.h>
#include <madness/world/enable_if.h>
#include <stdint.h>
#include <cstddef>
#include <iterator>
Include dependency graph for worldhash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  madness::Hash< T >
 Hash functor. More...
 

Namespaces

 madness
 Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
 
 madness::detail
 

Typedefs

typedef std::size_t madness::hashT
 The hash value type. More...
 

Functions

uint32_t hashword (const uint32_t *k, size_t length, uint32_t initval)
 
uint32_t hashlittle (const void *key, size_t length, uint32_t initval)
 
template<class T >
enable_if_c
< std::is_fundamental< T >
::value &&((sizeof(T)%sizeof(uint32_t))==0),
hashT >::type 
madness::hash_value (const T t)
 Hash a single fundamental object. More...
 
template<class T >
madness::enable_if_c
< std::is_fundamental< T >
::value &&((sizeof(T)%sizeof(uint32_t))!=0),
hashT >::type 
madness::hash_value (const T t)
 Hash a single fundamental object. More...
 
template<typename T >
hashT madness::hash_value (const T *t)
 Hash a pointer address. More...
 
template<typename T >
enable_if_c
<!(std::is_fundamental< T >
::value||std::is_pointer< T >
::value||std::is_array< T >
::value), hashT >::type 
madness::hash_value (const T &t)
 Hash a class object. More...
 
template<typename T >
hashT madness::hash_value (const std::basic_string< T > &t)
 Hash a string. More...
 
void madness::detail::combine_hash (hashT &seed, hashT hash)
 Internal use only. More...
 
template<class T >
void madness::hash_combine (hashT &seed, const T &v)
 Combine hash values. More...
 
template<class It >
void madness::hash_range (hashT &seed, It first, It last)
 Combine the hash values of an iterator range. More...
 
template<class It >
hashT madness::hash_range (It first, It last)
 Combine the hash values of an iterator range. More...
 
template<class T , std::size_t n>
hashT madness::hash_range (const T(&t)[n])
 Combine the hash values of a C-style array. More...
 
template<class T , std::size_t n>
void madness::hash_range (hashT &seed, const T(&t)[n])
 Combine the hash values of a C-style array. More...
 
template<class T >
enable_if< std::is_fundamental
< T > >::type 
madness::hash_range (hashT &seed, const T *t, std::size_t n)
 Combine the hash values of a pointer range. More...
 
template<class T >
disable_if
< std::is_fundamental< T >
>::type 
madness::hash_range (hashT &seed, const T *t, std::size_t n)
 Combine the hash values of a pointer range. More...
 
template<class T >
hashT madness::hash_range (const T *t, std::size_t n)
 Combine the hash values of a pointer range. More...
 

Detailed Description

Defines hash functions for use in distributed containers.

MADNESS uses hashing functions are modeled after Boost.Functional/Hash. It has many similar function calls including hash_value, hash_combine, and hash_range. In addition, it is also compatible with C++ TR1 hashing functors. The madness::Hash functor interface is identical to both boost::hash and std::hash , and any one of these may be used. By default, MADNESS hashing functions can hash all fundamental types including integral types, floating point types, pointer types, std::string, std::wstring, and std::array. Presently hashT is typedef to std::size_t .

Since having a good hash is important, we are using Bob Jenkin's "lookup v3" hash from http://www.burtleburtle.net/bob/c/lookup3.c. The preferred interface for these function is with the madness::Hash<T> functor, but you can also use hash_value or the "lookup v3" interface directly.

Note: Bob Jenkin's "lookup v3" hash returns a uint32_t hash value, which is cast to std::size_t. This is done for compatibility with std::hash.

WARNING: While both std::hash and madness::Hash have the same interface, they will not generate the same hash values from the same key value.

MADNESS hashing consists of one functor and three functions

There are several options for creating and using hash functions for your custom types. The easiest method is to define a hash_value function for your key type in the same namespace. This function will automatically be used by MADNESS hashing containers. The hashing function should have the following form.

namespace MyNamespace {
class Key {
// ...
};
madness::hashT hash_value(const Key& t) {
// ...
}
} // namespace MyNamespace

If your object is in the madness namespace, you may also use the intrusive method and define a hash member function for your key.

namespace madness {
class Key {
public:
// ...
madness::hashT hash() const {
// ...
}
};
}

You can create a specialization of madness::Hash for your type directly.

class Key {
// ...
};
namespace madness {
template <>
struct Hash<Key> {
hashT operator()(const Key& a) const {
// ...
}
};
} // namespace madness

If you use any of the above methods, MADNESS hash_combine and hash_range functions will be able to hash your custom types.

In addition to these methods, you can use std::hash, boost::hash, or create your own custom hashing functor that has the same form as madness::hash. However, if you want to use a hashing functor other than madness::Hash, you will need to provide the appropriate template parameter to the hashing container.

Function Documentation

uint32_t hashlittle ( const void *  key,
size_t  length,
uint32_t  initval 
)

References a(), b(), c, HASH_LITTLE_ENDIAN, k, and mix.

Referenced by madness::hash_range(), and madness::hash_value().

uint32_t hashword ( const uint32_t *  k,
size_t  length,
uint32_t  initval 
)

References a(), b(), c, and mix.

Referenced by madness::hash_range(), and madness::hash_value().