2c457f79996b50486820f7bfdab5671878a39a37
[libcds.git] / cds / opt / hash.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OPT_HASH_H
4 #define __CDS_OPT_HASH_H
5
6 #include <tuple>
7 #include <functional>
8 #include <cds/opt/options.h>
9
10 namespace cds { namespace opt {
11
12     /// [type-option] Option setter for a hash function
13     /**
14         This option setter specifies hash functor used in unordered containers.
15
16         The default value  of template argument \p Functor is \p cds::opt::v::hash
17         that is synonym for <tt>std::hash</tt> implementation of standard library.
18         If standard C++ library of the compiler you use does not provide TR1 implementation
19         the \p cds library automatically selects <tt>boost::hash</tt>.
20     */
21     template <typename Functor>
22     struct hash {
23         //@cond
24         template <typename Base> struct pack: public Base
25         {
26             typedef Functor hash;
27         };
28         //@endcond
29     };
30
31     namespace v {
32         //@cond
33         using std::hash;
34
35         /// Metafunction selecting default hash implementation
36         /**
37             The metafunction selects appropriate hash functor implementation.
38             If \p Hash is not equal to opt::none, then result of metafunction is \p Hash.
39             Otherwise, the result is <tt> std::hash<Q> </tt>.
40
41             Note that default hash function like <tt> std::hash<Q> </tt>
42             is generally not suitable for complex type \p Q and its derivatives.
43             You should manually provide particular hash functor for such types.
44         */
45         template <typename Hash>
46         struct hash_selector
47         {
48             typedef Hash    type;   ///< resulting implementation of hash functor
49         };
50
51         template <>
52         struct hash_selector<opt::none>
53         {
54             struct type {
55                 template <typename Q>
56                 size_t operator()( Q const& key ) const
57                 {
58                     return std::hash<Q>()( key );
59                 }
60             };
61         };
62         //@endcond
63     }   // namespace v
64
65     //@cond
66     namespace details {
67         template <class> struct hash_list;
68         template <typename... Functors>
69         struct hash_list< std::tuple<Functors...> >
70         {
71             static size_t const size = sizeof...(Functors);
72             typedef size_t values[size];
73             typedef std::tuple<Functors...> hash_tuple_type;
74
75             hash_tuple_type hash_tuple;
76
77             hash_list()
78             {}
79
80             hash_list( hash_tuple_type const& t)
81                 : hash_tuple( t )
82             {}
83             hash_list( hash_tuple_type&& t)
84                 : hash_tuple( std::forward<hash_tuple_type>(t) )
85             {}
86
87             template <size_t I, typename T>
88             typename std::enable_if< (I == sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
89             {}
90
91             template <size_t I, typename T>
92             typename std::enable_if< (I < sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
93             {
94                 dest[I] = std::get<I>( hash_tuple )( v );
95                 apply<I+1>( dest, v );
96             }
97
98             template <typename T>
99             void operator()( size_t * dest, T const& v ) const
100             {
101                 apply<0>( dest, v );
102             }
103         };
104     } // namespace details
105     //@endcond
106
107     /// Declare tuple for hash functors \p Functors
108     template <typename... Functors>
109     using hash_tuple = details::hash_list< std::tuple< Functors... >>;
110
111     //@cond
112     // At least, two functors must be provided. Single functor is not supported
113     template <typename Functor> struct hash< std::tuple<Functor> >;
114     //@endcond
115
116     /// Multi-functor hash option setter - specialization for std::tuple
117     template <typename... Functors>
118     struct hash< std::tuple<Functors...> >
119     {
120         //@cond
121         template <typename Base> struct pack: public Base
122         {
123             typedef details::hash_list< std::tuple<Functors...> >  hash;
124         };
125         //@endcond
126     };
127
128
129     //@cond
130     namespace details {
131
132         template <class HashList, typename WrappedType, typename Wrapper>
133         struct hash_list_wrapper {
134             typedef HashList                            hash_list;
135             typedef WrappedType                         wrapped_type;
136             typedef Wrapper                             wrapper_type;
137
138             typedef typename hash_list::hash_tuple_type hash_tuple_type;
139             static size_t const size = hash_list::size;
140
141             hash_list   m_wrappedList;
142
143             hash_list_wrapper()
144             {}
145             hash_list_wrapper( hash_tuple_type const& t)
146                 : m_wrappedList( t )
147             {}
148             hash_list_wrapper( hash_tuple_type&& t)
149                 : m_wrappedList( std::forward<hash_tuple_type>(t) )
150             {}
151
152             void operator()( size_t * dest, wrapped_type const& what ) const
153             {
154                 m_wrappedList( dest, wrapper_type()( what ));
155             }
156
157             template <typename Q>
158             void operator()( size_t * dest, Q const& what) const
159             {
160                 m_wrappedList( dest, what );
161             }
162         };
163
164     } // namespace details
165     //@endcond
166
167 }} // namespace cds::opt
168
169 #endif // #ifndef __CDS_OPT_HASH_H