9f1b9515374eb3e94a60d147ee0819d554e7e285
[libcds.git] / test / include / cds_test / 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 #if CDS_BUILD_BITS == 64
9 #   include <cds_test/city.h>
10 #endif
11
12 namespace cds_test {
13
14 #if CDS_BUILD_BITS == 64
15     class city32 {
16     public:
17         typedef uint32_t hash_type;
18         typedef hash_type result_type;
19
20         hash_type operator()( void const * pBuf, size_t len ) const
21         {
22             return CityHash32( reinterpret_cast<char const *>( pBuf ), len );
23         }
24
25         hash_type operator()( std::string const& s ) const
26         {
27             return CityHash32( s.c_str(), s.length() );
28         }
29
30         template <typename T>
31         hash_type operator()( T const& s ) const
32         {
33             return CityHash32( reinterpret_cast<char const *>( &s ), sizeof(s));
34         }
35
36         struct less
37         {
38             bool operator()( hash_type lhs, hash_type rhs ) const
39             {
40                 return lhs < rhs;
41             }
42         };
43     };
44
45     class city64 {
46     public:
47         typedef uint64_t hash_type;
48         typedef hash_type result_type;
49
50         hash_type operator()( void const * pBuf, size_t len ) const
51         {
52             return CityHash64( reinterpret_cast<char const *>( pBuf ), len );
53         }
54
55         hash_type operator()( std::string const& s ) const
56         {
57             return CityHash64( s.c_str(), s.length() );
58         }
59
60         template <typename T>
61         hash_type operator()( T const& s ) const
62         {
63             return CityHash64( reinterpret_cast<char const *>( &s ), sizeof(s));
64         }
65
66         struct less
67         {
68             bool operator()( hash_type lhs, hash_type rhs ) const
69             {
70                 return lhs < rhs;
71             }
72         };
73     };
74
75     class city128 {
76     public:
77         typedef uint128 hash_type;
78         typedef hash_type result_type;
79
80         hash_type operator()( void const * pBuf, size_t len ) const
81         {
82             return CityHash128( reinterpret_cast<char const *>( pBuf ), len );
83         }
84
85         hash_type operator()( std::string const& s ) const
86         {
87             return CityHash128( s.c_str(), s.length() );
88         }
89
90         template <typename T>
91         hash_type operator()( T const& s ) const
92         {
93             return CityHash128( reinterpret_cast<char const *>( &s ), sizeof(s));
94         }
95
96         struct less
97         {
98             bool operator()( hash_type const& lhs, hash_type const& rhs ) const
99             {
100                 if ( lhs.first != rhs.first )
101                     return lhs.second < rhs.second;
102                 return lhs.first < rhs.first;
103             }
104         };
105     };
106 #endif // #if CDS_BUILD_BITS == 64
107
108
109 } // namespace cds_test
110
111 #endif // #ifndef CDSUNIT_HASH_FUNC_H