3ea5ce923e63336500ce99726d8efaa257fba770
[libcds.git] / tests / hashing / hash_func.h
1 //$$CDS-header$$
2
3 #ifndef CDSUNIT_HASH_FUNC_H
4 #define CDSUNIT_HASH_FUNC_H
5
6 #include "hashing/sha256.h"
7 #include "hashing/md5.h"
8 #include "hashing/city.h"
9
10 namespace hashing {
11
12     template <class Hasher>
13     class hasher {
14         typedef Hasher hasher_type;
15         hasher_type m_hasher;
16     public:
17         struct hash_type {
18             uint8_t h[hasher_type::HashBytes];
19         };
20
21         hash_type operator()( void const * pBuf, size_t len )
22         {
23             m_hasher.reset();
24             m_hasher.add(pBuf, len);
25             hash_type result;
26             m_hasher.getHash( result.h );
27             return result;
28         }
29
30         hash_type operator()( std::string const& s )
31         {
32             return operator()( reinterpret_cast<void const *>(s.c_str()), s.length());
33         }
34
35         template <typename T>
36         hash_type operator()( T const& s )
37         {
38             return operator()( reinterpret_cast<void const *>(&s), sizeof(s));
39         }
40     };
41
42     typedef hasher<SHA256> sha256;
43     typedef hasher<MD5> md5;
44
45     class city32 {
46     public:
47         typedef uint32_t hash_type;
48
49         hash_type operator()( void const * pBuf, size_t len )
50         {
51             return CityHash32( reinterpret_cast<char const *>( pBuf ), len );
52         }
53
54         hash_type operator()( std::string const& s )
55         {
56             return CityHash32( s.c_str(), s.length() );
57         }
58
59         template <typename T>
60         hash_type operator()( T const& s )
61         {
62             return CityHash32( reinterpret_cast<char const *>( &s ), sizeof(s));
63         }
64     };
65
66     class city64 {
67     public:
68         typedef uint64_t hash_type;
69
70         hash_type operator()( void const * pBuf, size_t len )
71         {
72             return CityHash64( reinterpret_cast<char const *>( pBuf ), len );
73         }
74
75         hash_type operator()( std::string const& s )
76         {
77             return CityHash64( s.c_str(), s.length() );
78         }
79
80         template <typename T>
81         hash_type operator()( T const& s )
82         {
83             return CityHash64( reinterpret_cast<char const *>( &s ), sizeof(s));
84         }
85     };
86
87     class city128 {
88     public:
89         typedef uint128 hash_type;
90
91         hash_type operator()( void const * pBuf, size_t len )
92         {
93             return CityHash128( reinterpret_cast<char const *>( pBuf ), len );
94         }
95
96         hash_type operator()( std::string const& s )
97         {
98             return CityHash128( s.c_str(), s.length() );
99         }
100
101         template <typename T>
102         hash_type operator()( T const& s )
103         {
104             return CityHash128( reinterpret_cast<char const *>( &s ), sizeof(s));
105         }
106     };
107
108 } // namespace hashing
109
110 #endif // #ifndef CDSUNIT_HASH_FUNC_H