Remove CDS_CXX11_VARIADIC_TEMPLATE_SUPPORT macro and emulating code
[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 <cds/opt/options.h>
8 #include <cds/details/hash_functor_selector.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 cds::details::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> or <tt> boost::hash<Q> </tt>
40             depending of compiler you use.
41
42             Note that default hash function like <tt> std::hash<Q> </tt> or <tt> boost::hash<Q> </tt>
43             is generally not suitable for complex type \p Q and its derivatives.
44             You should manually provide particular hash functor for such types.
45         */
46         template <typename Hash>
47         struct hash_selector
48         {
49             typedef Hash    type    ;   ///< resulting implementation of hash functor
50         };
51
52         template <>
53         struct hash_selector<opt::none>
54         {
55             struct type {
56                 template <typename Q>
57                 size_t operator()( Q const& key ) const
58                 {
59                     return hash<Q>()( key );
60                 }
61             };
62         };
63         //@endcond
64     }   // namespace v
65
66     //@cond
67     namespace details {
68         template <class> struct hash_list;
69         template <typename... Functors>
70         struct hash_list< std::tuple<Functors...> >
71         {
72             static size_t const size = sizeof...(Functors);
73             typedef size_t values[size];
74             typedef std::tuple<Functors...> hash_tuple_type;
75
76             hash_tuple_type hash_tuple;
77
78             hash_list()
79             {}
80
81             hash_list( hash_tuple_type const& t)
82                 : hash_tuple( t )
83             {}
84 #       ifdef CDS_MOVE_SEMANTICS_SUPPORT
85             hash_list( hash_tuple_type&& t)
86                 : hash_tuple( std::forward<hash_tuple_type>(t) )
87             {}
88 #       endif
89
90             template <size_t I, typename T>
91             typename std::enable_if< (I == sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
92             {}
93
94             template <size_t I, typename T>
95             typename std::enable_if< (I < sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
96             {
97                 dest[I] = std::get<I>( hash_tuple )( v );
98                 apply<I+1>( dest, v );
99             }
100
101             template <typename T>
102             void operator()( size_t * dest, T const& v ) const
103             {
104                 apply<0>( dest, v );
105             }
106         };
107     } // namespace details
108     //@endcond
109
110     //@cond
111     // At least, two functors must be provided. Single functor is not supported
112 //#if CDS_COMPILER != CDS_COMPILER_INTEL
113     // Intel C++ compiler does not support
114     template <typename Functor> struct hash< std::tuple<Functor> >;
115 //#endif
116     //@endcond
117
118     /// Multi-functor hash option setter - specialization for std::tuple
119     template <typename... Functors>
120     struct hash< std::tuple<Functors...> >
121     {
122 //#   if CDS_COMPILER == CDS_COMPILER_INTEL
123         //static_assert( sizeof...(Functors) > 1, "At least, two functors must be provided. Single functor is not supported" );
124 //#   endif
125         //@cond
126         template <typename Base> struct pack: public Base
127         {
128             typedef details::hash_list< std::tuple<Functors...> >  hash;
129         };
130         //@endcond
131     };
132
133
134     //@cond
135     namespace details {
136
137         template <class HashList, typename WrappedType, typename Wrapper>
138         struct hash_list_wrapper {
139             typedef HashList                            hash_list;
140             typedef WrappedType                         wrapped_type;
141             typedef Wrapper                             wrapper_type;
142
143             typedef typename hash_list::hash_tuple_type hash_tuple_type;
144             static size_t const size = hash_list::size;
145
146             hash_list   m_wrappedList;
147
148             hash_list_wrapper()
149             {}
150             hash_list_wrapper( hash_tuple_type const& t)
151                 : m_wrappedList( t )
152             {}
153 #       ifdef CDS_MOVE_SEMANTICS_SUPPORT
154             hash_list_wrapper( hash_tuple_type&& t)
155                 : m_wrappedList( std::forward<hash_tuple_type>(t) )
156             {}
157 #       endif
158
159             void operator()( size_t * dest, wrapped_type const& what ) const
160             {
161                 m_wrappedList( dest, wrapper_type()( what ));
162             }
163
164             template <typename Q>
165             void operator()( size_t * dest, Q const& what) const
166             {
167                 m_wrappedList( dest, what );
168             }
169         };
170
171     } // namespace details
172     //@endcond
173
174 }} // namespace cds::opt
175
176 #endif // #ifndef __CDS_OPT_HASH_H