File size: 2,387 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_GENERAL_HASh_
#define DLIB_GENERAL_HASh_
#include <string>
#include "hash.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------- provide a general hashing function object ----------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T
>
class general_hash
{
public:
inline unsigned long operator() (
const T& item
) const;
};
/*!
Note that the default behavior of general hash is to attempt to cast
an object of type T to an unsigned long and use that as the hash.
REQUIREMENTS ON general_hash
- must have a default constructor
- must be a function object which overloads operator() as follows:
unsigned long operator()(const T& item)
- must take item, compute a hash number and return it
- must not throw
- must define the hash in such a way that all equivalent objects have
the same hash. where equivalent means the following:
definition of equivalent:
a is equivalent to b if
a < b == false and
b < a == false
!*/
// ---------------
template <
typename T
>
unsigned long general_hash<T>::
operator() (
const T& item
) const
{
// hash any types that have a conversion to uint64
return hash(static_cast<uint64>(item));
}
// ---------------
// std::string hash
template <>
inline unsigned long general_hash<std::string>::
operator() (
const std::string& item
) const
{
return hash(item);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_GENERAL_HASh_
|