BPHash
General object hashing library for C++
Namespaces | Classes | Typedefs | Enumerations | Functions
bphash Namespace Reference

Namespaces

 detail
 

Classes

class  Hasher
 Class that is used to hash objects. More...
 
struct  is_hashable
 Trait class that determines if a type is hashable or not. More...
 
struct  is_hashable< T * >
 
struct  is_hashable< T >
 
struct  is_hashable< T, Targs... >
 
struct  PointerWrapper
 Wrapper for pointers and arrays. More...
 
struct  StdHash
 A class that can be used in place of std::hash in containers. More...
 

Typedefs

typedef std::vector< uint8_t > HashValue
 Stores the value of a hash. More...
 

Enumerations

enum  HashType {
  HashType::Hash32, HashType::Hash64, HashType::Hash128, HashType::Hash32_x32,
  HashType::Hash64_x32, HashType::Hash128_x32, HashType::Hash32_x64, HashType::Hash64_x64,
  HashType::Hash128_x64
}
 Type of hash to use. More...
 

Functions

std::string hash_to_string (const HashValue &hash)
 Return a string representation of a hash. More...
 
HashValue truncate_hash (const HashValue &hash, size_t nbytes)
 Truncate the hash to a given number of bytes. More...
 
template<typename T >
convert_hash (const HashValue &hash)
 Convert the hash to a given integral type. More...
 
template<typename T >
PointerWrapper< T > hash_pointer (const T *ptr, size_t len=1)
 Wrap a raw pointer so that it can be hashed. More...
 
template<typename... Targs>
HashValue make_hash (HashType type, const Targs &...objs)
 Convenience function for hashing objects in a single function call. More...
 
template<typename InputIterator >
HashValue make_hash_range (HashType type, InputIterator first, InputIterator last)
 Convenience function hashing selected elements of a container. More...
 
template<typename T , size_t N>
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::array< T, N > &a, Hasher &h)
 Hashing of std::array. More...
 
template<typename T >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::complex< T > &a, Hasher &h)
 Hashing of std::complex. More...
 
template<typename T , typename Alloc >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::forward_list< T, Alloc > &a, Hasher &h)
 Hashing of std::forward_list. More...
 
template<typename T , typename Alloc >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::list< T, Alloc > &a, Hasher &h)
 Hashing of std::list. More...
 
template<typename Key , typename T , typename Compare , typename Alloc >
std::enable_if< is_hashable< Key, T >::value, void >::type hash_object (const std::map< Key, T, Compare, Alloc > &m, Hasher &h)
 Hashing of std::map. More...
 
template<typename T , typename Deleter >
PointerWrapper< T > hash_pointer (const std::unique_ptr< T, Deleter > &ptr, size_t len=1)
 Create a wrapper around a unique_ptr. More...
 
template<typename T , typename Deleter >
PointerWrapper< T > hash_pointer (const std::unique_ptr< T[], Deleter > &ptr, size_t len)
 Create a wrapper around a unique_ptr. More...
 
template<typename T >
PointerWrapper< T > hash_pointer (const std::shared_ptr< T > &ptr, size_t len=1)
 Create a wrapper around a shared_ptr. More...
 
template<typename T , typename Deleter >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::unique_ptr< T, Deleter > &p, Hasher &h)
 Hashing of std::unique_ptr. More...
 
template<typename T >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::shared_ptr< T > &p, Hasher &h)
 Hashing of std::shared_ptr. More...
 
template<typename Key , typename Compare , typename Alloc >
std::enable_if< is_hashable< Key >::value, void >::type hash_object (const std::set< Key, Compare, Alloc > &s, Hasher &h)
 Hashing of std::set. More...
 
template<typename charT , typename Traits , typename Alloc >
std::enable_if< is_hashable< charT >::value, void >::type hash_object (const std::basic_string< charT, Traits, Alloc > &s, Hasher &h)
 Hashing of std::string. More...
 
template<typename... Types>
std::enable_if< is_hashable< Types... >::value, void >::type hash_object (const std::tuple< Types... > &tup, Hasher &h)
 
template<typename Key , typename T , typename HashT , typename Pred , typename Alloc >
std::enable_if< is_hashable< Key, T >::value, void >::type hash_object (const std::unordered_map< Key, T, HashT, Pred, Alloc > &m, Hasher &h)
 Hashing of std::unordered_map. More...
 
template<typename Key , typename HashT , typename Pred , typename Alloc >
std::enable_if< is_hashable< Key >::value, void >::type hash_object (const std::unordered_set< Key, HashT, Pred, Alloc > &s, Hasher &h)
 Hashing of std::unordered_set. More...
 
template<typename T1 , typename T2 >
std::enable_if< is_hashable< T1, T2 >::value, void >::type hash_object (const std::pair< T1, T2 > &p, Hasher &h)
 Hashing of std::pair. More...
 
template<typename T , typename Alloc >
std::enable_if< is_hashable< T >::value, void >::type hash_object (const std::vector< T, Alloc > &v, Hasher &h)
 Hashing of std::vector. More...
 
template<typename Alloc >
void hash_object (const std::vector< bool, Alloc > &v, Hasher &h)
 Hashing of std::vector<bool> More...
 

Typedef Documentation

typedef std::vector<uint8_t> bphash::HashValue

Stores the value of a hash.

While you can use this like a normal std::vector, consider using the other utilities in this file (such as truncate_hash and hash_to_string)

Definition at line 24 of file Hash.hpp.

Enumeration Type Documentation

enum bphash::HashType
strong

Type of hash to use.

Enumerator
Hash32 

Default 32-bit hash.

Hash64 

Default 64-bit hash.

Hash128 

Default 128-bit hash.

Hash32_x32 

Default 32-bit hash for x32.

Hash64_x32 

Default 64-bit hash for x32.

Hash128_x32 

Default 128-bit hash for x32.

Hash32_x64 

Default 32-bit hash for x86-64.

Hash64_x64 

Default 64-bit hash for x86-64.

Hash128_x64 

Default 128-bit hash for x86-64.

Definition at line 25 of file Hasher.hpp.

Function Documentation

template<typename T >
T bphash::convert_hash ( const HashValue hash)

Convert the hash to a given integral type.

If the type is larger than the size of the stored hash, it is padded with zeros. Otherwise, the hash it is truncated.

Template Parameters
Thetype to convert to. Must be an integral type.
Parameters
[in]hashThe hash to convert
Returns
The hash converted to an integral type

Definition at line 50 of file Hash.hpp.

Here is the call graph for this function:

template<typename T >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::complex< T > &  a,
Hasher h 
)

Hashing of std::complex.

Definition at line 20 of file complex.hpp.

template<typename T , typename Alloc >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::forward_list< T, Alloc > &  a,
Hasher h 
)

Hashing of std::forward_list.

Definition at line 20 of file forward_list.hpp.

Here is the call graph for this function:

template<typename Key , typename Compare , typename Alloc >
std::enable_if<is_hashable<Key>::value, void>::type bphash::hash_object ( const std::set< Key, Compare, Alloc > &  s,
Hasher h 
)

Hashing of std::set.

Definition at line 20 of file set.hpp.

Here is the call graph for this function:

template<typename charT , typename Traits , typename Alloc >
std::enable_if<is_hashable<charT>::value, void>::type bphash::hash_object ( const std::basic_string< charT, Traits, Alloc > &  s,
Hasher h 
)

Hashing of std::string.

Definition at line 20 of file string.hpp.

Here is the call graph for this function:

template<typename T , typename Alloc >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::list< T, Alloc > &  a,
Hasher h 
)

Hashing of std::list.

Definition at line 20 of file list.hpp.

Here is the call graph for this function:

template<typename Key , typename HashT , typename Pred , typename Alloc >
std::enable_if<is_hashable<Key>::value, void>::type bphash::hash_object ( const std::unordered_set< Key, HashT, Pred, Alloc > &  s,
Hasher h 
)

Hashing of std::unordered_set.

Definition at line 20 of file unordered_set.hpp.

Here is the call graph for this function:

template<typename T1 , typename T2 >
std::enable_if<is_hashable<T1, T2>::value, void>::type bphash::hash_object ( const std::pair< T1, T2 > &  p,
Hasher h 
)

Hashing of std::pair.

Definition at line 20 of file utility.hpp.

template<typename T , size_t N>
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::array< T, N > &  a,
Hasher h 
)

Hashing of std::array.

Definition at line 20 of file array.hpp.

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename Key , typename T , typename Compare , typename Alloc >
std::enable_if<is_hashable<Key, T>::value, void>::type bphash::hash_object ( const std::map< Key, T, Compare, Alloc > &  m,
Hasher h 
)

Hashing of std::map.

Definition at line 21 of file map.hpp.

Here is the call graph for this function:

template<typename T , typename Alloc >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::vector< T, Alloc > &  v,
Hasher h 
)

Hashing of std::vector.

Definition at line 21 of file vector.hpp.

Here is the call graph for this function:

template<typename Key , typename T , typename HashT , typename Pred , typename Alloc >
std::enable_if<is_hashable<Key, T>::value, void>::type bphash::hash_object ( const std::unordered_map< Key, T, HashT, Pred, Alloc > &  m,
Hasher h 
)

Hashing of std::unordered_map.

Definition at line 21 of file unordered_map.hpp.

Here is the call graph for this function:

template<typename Alloc >
void bphash::hash_object ( const std::vector< bool, Alloc > &  v,
Hasher h 
)

Hashing of std::vector<bool>

Definition at line 29 of file vector.hpp.

Here is the call graph for this function:

template<typename... Types>
std::enable_if<is_hashable<Types...>::value, void>::type bphash::hash_object ( const std::tuple< Types... > &  tup,
Hasher h 
)

Definition at line 39 of file tuple.hpp.

template<typename T , typename Deleter >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::unique_ptr< T, Deleter > &  p,
Hasher h 
)

Hashing of std::unique_ptr.

It is assumed that the pointer points to a single element. If not, you must wrap the pointer with hash_pointer.

Definition at line 75 of file memory.hpp.

Here is the call graph for this function:

template<typename T >
std::enable_if<is_hashable<T>::value, void>::type bphash::hash_object ( const std::shared_ptr< T > &  p,
Hasher h 
)

Hashing of std::shared_ptr.

It is assumed that the pointer points to a single element. If not, you must wrap the pointer with hash_pointer.

Definition at line 88 of file memory.hpp.

Here is the call graph for this function:

template<typename T , typename Deleter >
PointerWrapper<T> bphash::hash_pointer ( const std::unique_ptr< T, Deleter > &  ptr,
size_t  len = 1 
)

Create a wrapper around a unique_ptr.

Parameters
[in]ptrThe shared pointer to wrap
[in]lenThe number of elements that ptr points to
Returns
The wrapped pointer that is ready for hashing

Definition at line 25 of file memory.hpp.

template<typename T , typename Deleter >
PointerWrapper<T> bphash::hash_pointer ( const std::unique_ptr< T[], Deleter > &  ptr,
size_t  len 
)

Create a wrapper around a unique_ptr.

Note
Since this unique_ptr uses the array syntax, you must specify the length
Parameters
[in]ptrThe shared pointer to wrap
[in]lenThe number of elements that ptr points to
Returns
The wrapped pointer that is ready for hashing

Definition at line 39 of file memory.hpp.

template<typename T >
PointerWrapper<T> bphash::hash_pointer ( const std::shared_ptr< T > &  ptr,
size_t  len = 1 
)

Create a wrapper around a shared_ptr.

Parameters
[in]ptrThe shared pointer to wrap
[in]lenThe number of elements that ptr points to
Returns
The wrapped pointer that is ready for hashing

Definition at line 51 of file memory.hpp.

template<typename T >
PointerWrapper<T> bphash::hash_pointer ( const T *  ptr,
size_t  len = 1 
)

Wrap a raw pointer so that it can be hashed.

Small helper function to help create PointerWrapper objects more cleanly.

Parameters
[in]ptrPointer to be hashed
[in]lenNumber of elements of T that ptr points to
Returns
The pointer information wrapped in PointerWrapper

Definition at line 67 of file Hasher.hpp.

Here is the caller graph for this function:

std::string bphash::hash_to_string ( const HashValue hash)

Return a string representation of a hash.

The string representation is the usual hex representation, with lower case letters. The length of the string will be 2*number_of_bits/8 characters.

Parameters
[in]hashThe hash to convert
Returns
A string representing the hash

Definition at line 17 of file Hash.cpp.

Here is the caller graph for this function:

template<typename... Targs>
HashValue bphash::make_hash ( HashType  type,
const Targs &...  objs 
)

Convenience function for hashing objects in a single function call.

This can be used to easily obtain the hash of several objects at once without needing to manually create a Hasher object, etc.

Parameters
[in]typeThe type of hash to use
[in]objsObjects to hash
Returns
Hash of the given data

Definition at line 254 of file Hasher.hpp.

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename InputIterator >
HashValue bphash::make_hash_range ( HashType  type,
InputIterator  first,
InputIterator  last 
)

Convenience function hashing selected elements of a container.

This can be used to easily obtain the hash of a range of objects in a container (given via two iterators).

Objects in the range [first, last) are hashed (generally the same convention as the STL)

Parameters
[in]typeThe type of hash to use
[in]firstAn iterator of the first object to hash
[in]lastAn iterator of the element following the last element to hash
Returns
Hash of the given data

Definition at line 276 of file Hasher.hpp.

Here is the call graph for this function:

HashValue bphash::truncate_hash ( const HashValue hash,
size_t  nbytes 
)

Truncate the hash to a given number of bytes.

If the desired size is larger than the size of the size of the given hash, the new hash is padded with zeroes.

Parameters
[in]hashThe hash to truncate
[in]nbytesDesired size of the new hash (in bytes)
Returns
A new, truncated hash

Definition at line 32 of file Hash.cpp.

Here is the caller graph for this function: