1 | // tuple_comparison.hpp ----------------------------------------------------- |
2 | // |
3 | // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) |
4 | // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com) |
5 | // |
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) |
9 | // |
10 | // For more information, see http://www.boost.org |
11 | // |
12 | // (The idea and first impl. of comparison operators was from Doug Gregor) |
13 | |
14 | // ----------------------------------------------------------------- |
15 | |
16 | #ifndef BOOST_TUPLE_COMPARISON_HPP |
17 | #define BOOST_TUPLE_COMPARISON_HPP |
18 | |
19 | #include "boost/tuple/tuple.hpp" |
20 | |
21 | // ------------------------------------------------------------- |
22 | // equality and comparison operators |
23 | // |
24 | // == and != compare tuples elementwise |
25 | // <, >, <= and >= use lexicographical ordering |
26 | // |
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). |
31 | // |
32 | // ------------------------------------------------------------- |
33 | |
34 | |
35 | namespace boost { |
36 | namespace tuples { |
37 | |
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; } |
44 | |
45 | |
46 | namespace detail { |
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. |
51 | |
52 | |
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()); |
57 | } |
58 | template<> |
59 | inline bool eq<null_type,null_type>(const null_type&, const null_type&) { return true; } |
60 | |
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()); |
65 | } |
66 | template<> |
67 | inline bool neq<null_type,null_type>(const null_type&, const null_type&) { return false; } |
68 | |
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())); |
74 | } |
75 | template<> |
76 | inline bool lt<null_type,null_type>(const null_type&, const null_type&) { return false; } |
77 | |
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())); |
83 | } |
84 | template<> |
85 | inline bool gt<null_type,null_type>(const null_type&, const null_type&) { return false; } |
86 | |
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())); |
92 | } |
93 | template<> |
94 | inline bool lte<null_type,null_type>(const null_type&, const null_type&) { return true; } |
95 | |
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())); |
101 | } |
102 | template<> |
103 | inline bool gte<null_type,null_type>(const null_type&, const null_type&) { return true; } |
104 | |
105 | } // end of namespace detail |
106 | |
107 | |
108 | // equal ---- |
109 | |
110 | template<class T1, class T2, class S1, class S2> |
111 | inline bool operator==(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
112 | { |
113 | // check that tuple lengths are equal |
114 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
115 | |
116 | return detail::eq(lhs, rhs); |
117 | } |
118 | |
119 | // not equal ----- |
120 | |
121 | template<class T1, class T2, class S1, class S2> |
122 | inline bool operator!=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
123 | { |
124 | |
125 | // check that tuple lengths are equal |
126 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
127 | |
128 | return detail::neq(lhs, rhs); |
129 | } |
130 | |
131 | // < |
132 | template<class T1, class T2, class S1, class S2> |
133 | inline bool operator<(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
134 | { |
135 | // check that tuple lengths are equal |
136 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
137 | |
138 | return detail::lt(lhs, rhs); |
139 | } |
140 | |
141 | // > |
142 | template<class T1, class T2, class S1, class S2> |
143 | inline bool operator>(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
144 | { |
145 | // check that tuple lengths are equal |
146 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
147 | |
148 | return detail::gt(lhs, rhs); |
149 | } |
150 | |
151 | // <= |
152 | template<class T1, class T2, class S1, class S2> |
153 | inline bool operator<=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
154 | { |
155 | // check that tuple lengths are equal |
156 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
157 | |
158 | return detail::lte(lhs, rhs); |
159 | } |
160 | |
161 | // >= |
162 | template<class T1, class T2, class S1, class S2> |
163 | inline bool operator>=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs) |
164 | { |
165 | // check that tuple lengths are equal |
166 | BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value); |
167 | |
168 | return detail::gte(lhs, rhs); |
169 | } |
170 | |
171 | } // end of namespace tuples |
172 | } // end of namespace boost |
173 | |
174 | |
175 | #endif // BOOST_TUPLE_COMPARISON_HPP |
176 | |