fixed an error in less() predicate
[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         struct less
66         {
67             bool operator()( hash_type lhs, hash_type rhs ) const
68             {
69                 return lhs < rhs;
70             }
71         };
72     };
73
74     class city64 {
75     public:
76         typedef uint64_t hash_type;
77
78         hash_type operator()( void const * pBuf, size_t len )
79         {
80             return CityHash64( reinterpret_cast<char const *>( pBuf ), len );
81         }
82
83         hash_type operator()( std::string const& s )
84         {
85             return CityHash64( s.c_str(), s.length() );
86         }
87
88         template <typename T>
89         hash_type operator()( T const& s )
90         {
91             return CityHash64( reinterpret_cast<char const *>( &s ), sizeof(s));
92         }
93
94         struct less
95         {
96             bool operator()( hash_type lhs, hash_type rhs ) const
97             {
98                 return lhs < rhs;
99             }
100         };
101     };
102
103     class city128 {
104     public:
105         typedef uint128 hash_type;
106
107         hash_type operator()( void const * pBuf, size_t len )
108         {
109             return CityHash128( reinterpret_cast<char const *>( pBuf ), len );
110         }
111
112         hash_type operator()( std::string const& s )
113         {
114             return CityHash128( s.c_str(), s.length() );
115         }
116
117         template <typename T>
118         hash_type operator()( T const& s )
119         {
120             return CityHash128( reinterpret_cast<char const *>( &s ), sizeof(s));
121         }
122
123         struct less
124         {
125             bool operator()( hash_type const& lhs, hash_type const& rhs ) const
126             {
127                 if ( lhs.first != rhs.first )
128                     return lhs.second < rhs.second;
129                 return lhs.first < rhs.first;
130             }
131         };
132     };
133
134 } // namespace hashing
135
136 #endif // #ifndef CDSUNIT_HASH_FUNC_H