1 // tuple_comparison.hpp -----------------------------------------------------
3 // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com)
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
12 // (The idea and first impl. of comparison operators was from Doug Gregor)
14 // -----------------------------------------------------------------
16 #ifndef BOOST_TUPLE_COMPARISON_HPP
17 #define BOOST_TUPLE_COMPARISON_HPP
19 #include "boost/tuple/tuple.hpp"
21 // -------------------------------------------------------------
22 // equality and comparison operators
24 // == and != compare tuples elementwise
25 // <, >, <= and >= use lexicographical ordering
27 // Any operator between tuples of different length fails at compile time
28 // No dependencies between operators are assumed
29 // (i.e. !(a<b) does not imply a>=b, a!=b does not imply a==b etc.
30 // so any weirdnesses of elementary operators are respected).
32 // -------------------------------------------------------------
38 inline bool operator==(const null_type
&, const null_type
&) { return true; }
39 inline bool operator>=(const null_type
&, const null_type
&) { return true; }
40 inline bool operator<=(const null_type
&, const null_type
&) { return true; }
41 inline bool operator!=(const null_type
&, const null_type
&) { return false; }
42 inline bool operator<(const null_type
&, const null_type
&) { return false; }
43 inline bool operator>(const null_type
&, const null_type
&) { return false; }
47 // comparison operators check statically the length of its operands and
48 // delegate the comparing task to the following functions. Hence
49 // the static check is only made once (should help the compiler).
50 // These functions assume tuples to be of the same length.
53 template<class T1
, class T2
>
54 inline bool eq(const T1
& lhs
, const T2
& rhs
) {
55 return lhs
.get_head() == rhs
.get_head() &&
56 eq(lhs
.get_tail(), rhs
.get_tail());
59 inline bool eq
<null_type
,null_type
>(const null_type
&, const null_type
&) { return true; }
61 template<class T1
, class T2
>
62 inline bool neq(const T1
& lhs
, const T2
& rhs
) {
63 return lhs
.get_head() != rhs
.get_head() ||
64 neq(lhs
.get_tail(), rhs
.get_tail());
67 inline bool neq
<null_type
,null_type
>(const null_type
&, const null_type
&) { return false; }
69 template<class T1
, class T2
>
70 inline bool lt(const T1
& lhs
, const T2
& rhs
) {
71 return lhs
.get_head() < rhs
.get_head() ||
72 ( !(rhs
.get_head() < lhs
.get_head()) &&
73 lt(lhs
.get_tail(), rhs
.get_tail()));
76 inline bool lt
<null_type
,null_type
>(const null_type
&, const null_type
&) { return false; }
78 template<class T1
, class T2
>
79 inline bool gt(const T1
& lhs
, const T2
& rhs
) {
80 return lhs
.get_head() > rhs
.get_head() ||
81 ( !(rhs
.get_head() > lhs
.get_head()) &&
82 gt(lhs
.get_tail(), rhs
.get_tail()));
85 inline bool gt
<null_type
,null_type
>(const null_type
&, const null_type
&) { return false; }
87 template<class T1
, class T2
>
88 inline bool lte(const T1
& lhs
, const T2
& rhs
) {
89 return lhs
.get_head() <= rhs
.get_head() &&
90 ( !(rhs
.get_head() <= lhs
.get_head()) ||
91 lte(lhs
.get_tail(), rhs
.get_tail()));
94 inline bool lte
<null_type
,null_type
>(const null_type
&, const null_type
&) { return true; }
96 template<class T1
, class T2
>
97 inline bool gte(const T1
& lhs
, const T2
& rhs
) {
98 return lhs
.get_head() >= rhs
.get_head() &&
99 ( !(rhs
.get_head() >= lhs
.get_head()) ||
100 gte(lhs
.get_tail(), rhs
.get_tail()));
103 inline bool gte
<null_type
,null_type
>(const null_type
&, const null_type
&) { return true; }
105 } // end of namespace detail
110 template<class T1
, class T2
, class S1
, class S2
>
111 inline bool operator==(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
113 // check that tuple lengths are equal
114 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
116 return detail::eq(lhs
, rhs
);
121 template<class T1
, class T2
, class S1
, class S2
>
122 inline bool operator!=(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
125 // check that tuple lengths are equal
126 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
128 return detail::neq(lhs
, rhs
);
132 template<class T1
, class T2
, class S1
, class S2
>
133 inline bool operator<(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
135 // check that tuple lengths are equal
136 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
138 return detail::lt(lhs
, rhs
);
142 template<class T1
, class T2
, class S1
, class S2
>
143 inline bool operator>(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
145 // check that tuple lengths are equal
146 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
148 return detail::gt(lhs
, rhs
);
152 template<class T1
, class T2
, class S1
, class S2
>
153 inline bool operator<=(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
155 // check that tuple lengths are equal
156 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
158 return detail::lte(lhs
, rhs
);
162 template<class T1
, class T2
, class S1
, class S2
>
163 inline bool operator>=(const cons
<T1
, T2
>& lhs
, const cons
<S1
, S2
>& rhs
)
165 // check that tuple lengths are equal
166 BOOST_STATIC_ASSERT(length
<T2
>::value
== length
<S2
>::value
);
168 return detail::gte(lhs
, rhs
);
171 } // end of namespace tuples
172 } // end of namespace boost
175 #endif // BOOST_TUPLE_COMPARISON_HPP