Fixed: CityHash is supported only in 64bit mode
[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 <cds/details/defs.h>
7
8 #include "hashing/sha256.h"
9 #include "hashing/md5.h"
10 #if CDS_BUILD_BITS == 64
11 #   include "hashing/city.h"
12 #endif
13
14 namespace hashing {
15
16     template <class Hasher>
17     class hasher {
18         typedef Hasher hasher_type;
19         hasher_type m_hasher;
20     public:
21         struct hash_type {
22             uint8_t h[hasher_type::HashBytes];
23         };
24
25         hash_type operator()( void const * pBuf, size_t len )
26         {
27             m_hasher.reset();
28             m_hasher.add(pBuf, len);
29             hash_type result;
30             m_hasher.getHash( result.h );
31             return result;
32         }
33
34         hash_type operator()( std::string const& s )
35         {
36             return operator()( reinterpret_cast<void const *>(s.c_str()), s.length());
37         }
38
39         template <typename T>
40         hash_type operator()( T const& s )
41         {
42             return operator()( reinterpret_cast<void const *>(&s), sizeof(s));
43         }
44     };
45
46     typedef hasher<SHA256> sha256;
47     typedef hasher<MD5> md5;
48
49 #if CDS_BUILD_BITS == 64
50     class city32 {
51     public:
52         typedef uint32_t hash_type;
53
54         hash_type operator()( void const * pBuf, size_t len )
55         {
56             return CityHash32( reinterpret_cast<char const *>( pBuf ), len );
57         }
58
59         hash_type operator()( std::string const& s )
60         {
61             return CityHash32( s.c_str(), s.length() );
62         }
63
64         template <typename T>
65         hash_type operator()( T const& s )
66         {
67             return CityHash32( reinterpret_cast<char const *>( &s ), sizeof(s));
68         }
69
70         struct less
71         {
72             bool operator()( hash_type lhs, hash_type rhs ) const
73             {
74                 return lhs < rhs;
75             }
76         };
77     };
78
79     class city64 {
80     public:
81         typedef uint64_t hash_type;
82
83         hash_type operator()( void const * pBuf, size_t len )
84         {
85             return CityHash64( reinterpret_cast<char const *>( pBuf ), len );
86         }
87
88         hash_type operator()( std::string const& s )
89         {
90             return CityHash64( s.c_str(), s.length() );
91         }
92
93         template <typename T>
94         hash_type operator()( T const& s )
95         {
96             return CityHash64( reinterpret_cast<char const *>( &s ), sizeof(s));
97         }
98
99         struct less
100         {
101             bool operator()( hash_type lhs, hash_type rhs ) const
102             {
103                 return lhs < rhs;
104             }
105         };
106     };
107
108     class city128 {
109     public:
110         typedef uint128 hash_type;
111
112         hash_type operator()( void const * pBuf, size_t len )
113         {
114             return CityHash128( reinterpret_cast<char const *>( pBuf ), len );
115         }
116
117         hash_type operator()( std::string const& s )
118         {
119             return CityHash128( s.c_str(), s.length() );
120         }
121
122         template <typename T>
123         hash_type operator()( T const& s )
124         {
125             return CityHash128( reinterpret_cast<char const *>( &s ), sizeof(s));
126         }
127
128         struct less
129         {
130             bool operator()( hash_type const& lhs, hash_type const& rhs ) const
131             {
132                 if ( lhs.first != rhs.first )
133                     return lhs.second < rhs.second;
134                 return lhs.first < rhs.first;
135             }
136         };
137     };
138 #endif // #if CDS_BUILD_BITS == 64
139
140
141 } // namespace hashing
142
143 #endif // #ifndef CDSUNIT_HASH_FUNC_H