3 #ifndef __CDS_OPT_COMPARE_H
4 #define __CDS_OPT_COMPARE_H
8 2011.05.05 khizmax Created
11 #include <cds/opt/options.h>
15 #include <cds/details/std/type_traits.h>
17 namespace cds { namespace opt {
19 /// [type-option] Option setter for key comparing
21 The option sets a type of a functor to compare keys.
22 For comparing two keys \p k1 and \p k2 the functor must return:
27 \p Functor is a functor with following interface:
31 int operator ()(const T& r1, const T& r2)
37 Note that the functor must return \p int, not a \p bool value.
39 There are predefined type for \p Functor:
40 - the functor v::less_comparator that implements comparing functor through \p std::less predicate.
41 - the specialization of v::less_comparator functor intended for the string comparison
43 You may implement your own comparing functor that satisfies \p Functor interface.
45 About relation between \ref opt::less and \ref opt::compare option setters see opt::less description.
47 template <typename Functor>
50 template <typename Base> struct pack: public Base
52 typedef Functor compare;
59 /// Comparator based on \p std::less predicate
61 This functor is predefined type for \p opt::compare option setter.
62 It is based on \p std::less predicate.
65 struct less_comparator {
66 /// Operator that compares two value of type \p T
67 int operator()(T const& v1, T const& v2)
69 if ( std::less<T>()( v1, v2 ) )
71 if ( std::less<T>()( v2, v1 ))
77 /// Comparator specialization for \p std::string
79 This functor uses \p std::string::compare method instead of \p std::less predicate.
81 template <typename T, typename Traits, typename Alloc>
82 struct less_comparator< std::basic_string<T, Traits, Alloc> >
85 typedef std::basic_string<T, Traits, Alloc> string_type;
86 int operator()(string_type const& v1, string_type const& v2)
88 return v1.compare( v2 );
94 /// [type-option] Option setter for \p less predicate
96 The option sets a binary predicate that tests whether a value of a specified type is less than another value of that type.
97 \p Functor interface is similar to \p std::less predicate interface.
98 The standard predicate \p std::less can act as \p Functor:
99 \code typedef cds::opt::less< std::less< int > > opt_less \endcode
101 In addition, the option setter may sets non-standard 2-type predicate (\p std::binary_function):
108 template <typename T, typename Q>
110 bool operator ()( const T& t, const Q& q )
112 bool operator ()( const Q& q, const T& t )
114 bool operator ()( const T& t1, const T& t2 )
115 { return t1.n < t2.n ; }
116 bool operator ()( const Q& q1, const Q& q2 )
120 typedef cds::opt::less< pred_less< foo, int > > opt_less;
123 Generally, the default type for \p Functor is \p std::less but it depends on the container used.
125 \par Relation between \p opt::less and opt::compare option setters
126 Unless otherwise specified, \p compare option setter has high priority. If opt::compare and opt::less options are specified
127 for a container, the opt::compare option is used:
129 // Suppose, hypothetical map_type allows to specify
130 // cds::opt::less and cds::opt::compare options
132 typedef map_type< std::string, int,
133 cds::opt::compare< cds::opt::v::less_comparator< std::string > >,
134 cds::opt::less< std::less< std::string > >
137 // For my_map_type, the cds::opt::compare comparator will be used,
138 // the cds::opt::less option is ignored without any warnings.
141 template <typename Functor>
144 template <typename Base> struct pack: public Base
146 typedef Functor less;
153 template <typename Less>
154 struct make_comparator_from_less
156 typedef Less less_functor;
158 template <typename T, typename Q>
159 int operator ()( T const& t, Q const& q ) const
170 template <typename T, typename Traits, bool Forced = true >
171 struct make_comparator
173 typedef typename Traits::compare compare;
174 typedef typename Traits::less less;
176 typedef typename std::conditional<
177 std::is_same< compare, opt::none >::value,
178 typename std::conditional<
179 std::is_same< less, opt::none >::value,
180 typename std::conditional< Forced, make_comparator_from_less< std::less<T> >, opt::none >::type,
181 make_comparator_from_less< less >
187 template <typename T, CDS_DECL_OPTIONS>
188 struct make_comparator_from_option_list
190 struct default_traits {
191 typedef opt::none compare;
192 typedef opt::none less;
195 typedef typename make_comparator< T,
196 typename opt::make_options<
197 typename opt::find_type_traits< default_traits, CDS_OPTIONS >::type
202 } // namespace details
205 /// [type-option] Option setter for \p equal_to predicate
207 The option sets a binary predicate that tests whether a value of a specified type is equal to another value of that type.
208 \p Functor interface is similar to \p std::equal_to predicate interface.
209 The standard predicate \p std::equal_to can act as \p Functor:
210 \code typedef cds::opt::equal_to< std::equal_to< int > > opt_equal_to \endcode
212 In addition, the option setter may sets non-standard 2-type (or even N-type) predicate (\p std::binary_function):
219 template <typename T, typename Q>
220 struct pred_equal_to {
221 bool operator ()( const T& t, const Q& q )
222 { return t.n == q ; }
223 bool operator ()( const Q& q, const T& t )
224 { return q == t.n ; }
225 bool operator ()( const T& t1, const T& t2 )
226 { return t1.n == t2.n ; }
227 bool operator ()( const Q& q1, const Q& q2 )
228 { return q1 == q2 ; }
231 typedef cds::opt::equal_to< pred_equal_to< foo, int > > opt_equal_to;
234 Generally, the default type for \p Functor is \p std::equal_to but it depends on the container used.
236 template <typename Functor>
239 template <typename Base> struct pack: public Base
241 typedef Functor equal_to;
248 template <typename T, typename Traits, bool Forced = true>
251 typedef typename Traits::equal_to equal_to;
253 typedef typename std::conditional<
254 std::is_same< equal_to, opt::none >::value,
255 typename std::conditional< Forced, std::equal_to<T>, opt::none >::type,
262 }} // namespace cds::opt
264 #endif // #ifndef __CDS_OPT_COMPARE_H