1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
13 #ifndef vector_comparison_h
14 #define vector_comparison_h
16 #include <cml/core/cml_assert.h>
17 #include <cml/et/size_checking.h>
18 #include <cml/et/scalar_ops.h>
20 /* This is used below to create a more meaningful compile-time error when
21 * vector_comparison is not provided with vector or VectorExpr arguments:
23 struct vector_comparison_expects_vector_args_error
;
25 #define CML_VEC_VEC_ORDER(_order_, _op_, _OpT_) \
26 template<typename E1, class AT1, typename E2, class AT2> \
29 const vector<E1,AT1>& left, \
30 const vector<E2,AT2>& right) \
32 return detail::vector_##_order_ (left, right, _OpT_ <E1,E2>()); \
35 #define CML_VEC_VECXPR_ORDER(_order_, _op_, _OpT_) \
36 template<typename E, class AT, class XprT> \
39 const vector<E,AT>& left, \
40 VECXPR_ARG_TYPE right) \
42 return detail::vector_##_order_ (left, right, \
43 _OpT_ <E, typename XprT::value_type>()); \
46 #define CML_VECXPR_VEC_ORDER(_order_, _op_, _OpT_) \
47 template<class XprT, typename E, class AT> \
50 VECXPR_ARG_TYPE left, \
51 const vector<E,AT>& right) \
53 return detail::vector_##_order_ (left, right, \
54 _OpT_ <typename XprT::value_type, E>()); \
57 #define CML_VECXPR_VECXPR_ORDER(_order_, _op_, _OpT_) \
58 template<class XprT1, class XprT2> \
61 VECXPR_ARG_TYPE_N(1) left, \
62 VECXPR_ARG_TYPE_N(2) right) \
64 return detail::vector_##_order_ (left, right, \
66 typename XprT1::value_type, \
67 typename XprT2::value_type>()); \
74 /** Vector strict weak ordering relationship.
76 * OpT must implement a strict weak order on the vector element type.
77 * operator< and operator> on integer and floating-point types are
80 template<typename LeftT
, typename RightT
, typename OpT
>
82 vector_weak_order(const LeftT
& left
, const RightT
& right
, OpT
)
85 typedef et::ExprTraits
<LeftT
> left_traits
;
86 typedef et::ExprTraits
<RightT
> right_traits
;
88 /* vector_comparison() requires vector expressions: */
90 (et::VectorExpressions
<LeftT
,RightT
>::is_true
),
91 vector_comparison_expects_vector_args_error
);
92 /* Note: parens are required here so that the preprocessor ignores the
96 typedef typename
et::VectorPromote
<
97 typename
left_traits::result_type
,
98 typename
right_traits::result_type
100 typedef typename
result_type::size_tag size_tag
;
102 /* Verify expression size: */
103 ssize_t N
= (ssize_t
) et::CheckedSize(left
,right
,size_tag());
104 for(ssize_t i
= 0; i
< N
; ++ i
) {
106 left_traits().get(left
,i
),
107 right_traits().get(right
,i
)
110 /* If weak order (a < b) is satisfied, return true: */
112 } else if(OpT().apply(
113 right_traits().get(right
,i
),
114 left_traits().get(left
,i
)
117 /* If !(b < a), then return false: */
121 /* Have !(a < b) && !(b < a) <=> (a >= b && b >= a) <=> (a == b).
122 * so need to test next element:
127 /* XXX Can this be unrolled in any reasonable way? */
129 /* If we get here, then left == right: */
133 /** Vector total order relationship.
135 * OpT must implement a total order on the vector element type. operator<=
136 * and operator>= on integer and floating-point types are examples.
138 template<typename LeftT
, typename RightT
, typename OpT
>
140 vector_total_order(const LeftT
& left
, const RightT
& right
, OpT
)
143 typedef et::ExprTraits
<LeftT
> left_traits
;
144 typedef et::ExprTraits
<RightT
> right_traits
;
146 /* vector_comparison() requires vector expressions: */
147 CML_STATIC_REQUIRE_M(
148 (et::VectorExpressions
<LeftT
,RightT
>::is_true
),
149 vector_comparison_expects_vector_args_error
);
150 /* Note: parens are required here so that the preprocessor ignores the
154 typedef typename
et::VectorPromote
<
155 typename
left_traits::result_type
,
156 typename
right_traits::result_type
158 typedef typename
result_type::size_tag size_tag
;
160 /* Verify expression size: */
161 ssize_t N
= (ssize_t
) et::CheckedSize(left
,right
,size_tag());
162 for(ssize_t i
= 0; i
< N
; ++ i
) {
164 /* Test total order: */
166 left_traits().get(left
,i
),
167 right_traits().get(right
,i
)
170 /* Automatically true if weak order (a <= b) && !(b <= a) <=>
171 * (a <= b) && (b > a) <=> (a < b) is satisfied:
174 right_traits().get(right
,i
),
175 left_traits().get(left
,i
)
179 /* Otherwise, have equality (a <= b) && (b <= a), so continue
187 /* Total order isn't satisfied (a > b), so return false: */
191 /* XXX Can this be unrolled in any reasonable way? */
193 /* Total (==) or weak (<) order was satisfied, so return true: */
199 /* XXX There is a better way to handle these with operator traits... */
201 CML_VEC_VEC_ORDER( total_order
, operator==, et::OpEqual
)
202 CML_VECXPR_VEC_ORDER( total_order
, operator==, et::OpEqual
)
203 CML_VEC_VECXPR_ORDER( total_order
, operator==, et::OpEqual
)
204 CML_VECXPR_VECXPR_ORDER( total_order
, operator==, et::OpEqual
)
206 CML_VEC_VEC_ORDER( weak_order
, operator!=, et::OpNotEqual
)
207 CML_VECXPR_VEC_ORDER( weak_order
, operator!=, et::OpNotEqual
)
208 CML_VEC_VECXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
209 CML_VECXPR_VECXPR_ORDER( weak_order
, operator!=, et::OpNotEqual
)
211 CML_VEC_VEC_ORDER( weak_order
, operator<, et::OpLess
)
212 CML_VECXPR_VEC_ORDER( weak_order
, operator<, et::OpLess
)
213 CML_VEC_VECXPR_ORDER( weak_order
, operator<, et::OpLess
)
214 CML_VECXPR_VECXPR_ORDER( weak_order
, operator<, et::OpLess
)
216 CML_VEC_VEC_ORDER( weak_order
, operator>, et::OpGreater
)
217 CML_VECXPR_VEC_ORDER( weak_order
, operator>, et::OpGreater
)
218 CML_VEC_VECXPR_ORDER( weak_order
, operator>, et::OpGreater
)
219 CML_VECXPR_VECXPR_ORDER( weak_order
, operator>, et::OpGreater
)
221 CML_VEC_VEC_ORDER( total_order
, operator<=, et::OpLessEqual
)
222 CML_VECXPR_VEC_ORDER( total_order
, operator<=, et::OpLessEqual
)
223 CML_VEC_VECXPR_ORDER( total_order
, operator<=, et::OpLessEqual
)
224 CML_VECXPR_VECXPR_ORDER( total_order
, operator<=, et::OpLessEqual
)
226 CML_VEC_VEC_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
227 CML_VECXPR_VEC_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
228 CML_VEC_VECXPR_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
229 CML_VECXPR_VECXPR_ORDER( total_order
, operator>=, et::OpGreaterEqual
)
235 // -------------------------------------------------------------------------