3 #ifndef __CDS_OPT_HASH_H
4 #define __CDS_OPT_HASH_H
8 #include <cds/opt/options.h>
10 namespace cds { namespace opt {
12 /// [type-option] Option setter for a hash function
14 This option setter specifies hash functor used in unordered containers.
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>.
21 template <typename Functor>
24 template <typename Base> struct pack: public Base
35 /// Metafunction selecting default hash implementation
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>.
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.
45 template <typename Hash>
48 typedef Hash type; ///< resulting implementation of hash functor
52 struct hash_selector<opt::none>
56 size_t operator()( Q const& key ) const
58 return std::hash<Q>()( key );
67 template <class> struct hash_list;
68 template <typename... Functors>
69 struct hash_list< std::tuple<Functors...> >
71 static size_t const size = sizeof...(Functors);
72 typedef size_t values[size];
73 typedef std::tuple<Functors...> hash_tuple_type;
75 hash_tuple_type hash_tuple;
80 hash_list( hash_tuple_type const& t)
83 hash_list( hash_tuple_type&& t)
84 : hash_tuple( std::forward<hash_tuple_type>(t) )
87 template <size_t I, typename T>
88 typename std::enable_if< (I == sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
91 template <size_t I, typename T>
92 typename std::enable_if< (I < sizeof...(Functors)) >::type apply( size_t * dest, T const& v ) const
94 dest[I] = std::get<I>( hash_tuple )( v );
95 apply<I+1>( dest, v );
99 void operator()( size_t * dest, T const& v ) const
104 } // namespace details
108 // At least, two functors must be provided. Single functor is not supported
109 template <typename Functor> struct hash< std::tuple<Functor> >;
112 /// Multi-functor hash option setter - specialization for std::tuple
113 template <typename... Functors>
114 struct hash< std::tuple<Functors...> >
117 template <typename Base> struct pack: public Base
119 typedef details::hash_list< std::tuple<Functors...> > hash;
128 template <class HashList, typename WrappedType, typename Wrapper>
129 struct hash_list_wrapper {
130 typedef HashList hash_list;
131 typedef WrappedType wrapped_type;
132 typedef Wrapper wrapper_type;
134 typedef typename hash_list::hash_tuple_type hash_tuple_type;
135 static size_t const size = hash_list::size;
137 hash_list m_wrappedList;
141 hash_list_wrapper( hash_tuple_type const& t)
144 hash_list_wrapper( hash_tuple_type&& t)
145 : m_wrappedList( std::forward<hash_tuple_type>(t) )
148 void operator()( size_t * dest, wrapped_type const& what ) const
150 m_wrappedList( dest, wrapper_type()( what ));
153 template <typename Q>
154 void operator()( size_t * dest, Q const& what) const
156 m_wrappedList( dest, what );
160 } // namespace details
163 }} // namespace cds::opt
165 #endif // #ifndef __CDS_OPT_HASH_H