Chris@16
|
1 // tuple_comparison.hpp -----------------------------------------------------
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
|
Chris@16
|
4 // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com)
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10 // For more information, see http://www.boost.org
|
Chris@16
|
11 //
|
Chris@16
|
12 // (The idea and first impl. of comparison operators was from Doug Gregor)
|
Chris@16
|
13
|
Chris@16
|
14 // -----------------------------------------------------------------
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_TUPLE_COMPARISON_HPP
|
Chris@16
|
17 #define BOOST_TUPLE_COMPARISON_HPP
|
Chris@16
|
18
|
Chris@16
|
19 #include "boost/tuple/tuple.hpp"
|
Chris@16
|
20
|
Chris@16
|
21 // -------------------------------------------------------------
|
Chris@16
|
22 // equality and comparison operators
|
Chris@16
|
23 //
|
Chris@16
|
24 // == and != compare tuples elementwise
|
Chris@16
|
25 // <, >, <= and >= use lexicographical ordering
|
Chris@16
|
26 //
|
Chris@16
|
27 // Any operator between tuples of different length fails at compile time
|
Chris@16
|
28 // No dependencies between operators are assumed
|
Chris@16
|
29 // (i.e. !(a<b) does not imply a>=b, a!=b does not imply a==b etc.
|
Chris@16
|
30 // so any weirdnesses of elementary operators are respected).
|
Chris@16
|
31 //
|
Chris@16
|
32 // -------------------------------------------------------------
|
Chris@16
|
33
|
Chris@16
|
34
|
Chris@16
|
35 namespace boost {
|
Chris@16
|
36 namespace tuples {
|
Chris@16
|
37
|
Chris@16
|
38 inline bool operator==(const null_type&, const null_type&) { return true; }
|
Chris@16
|
39 inline bool operator>=(const null_type&, const null_type&) { return true; }
|
Chris@16
|
40 inline bool operator<=(const null_type&, const null_type&) { return true; }
|
Chris@16
|
41 inline bool operator!=(const null_type&, const null_type&) { return false; }
|
Chris@16
|
42 inline bool operator<(const null_type&, const null_type&) { return false; }
|
Chris@16
|
43 inline bool operator>(const null_type&, const null_type&) { return false; }
|
Chris@16
|
44
|
Chris@16
|
45
|
Chris@16
|
46 namespace detail {
|
Chris@16
|
47 // comparison operators check statically the length of its operands and
|
Chris@16
|
48 // delegate the comparing task to the following functions. Hence
|
Chris@16
|
49 // the static check is only made once (should help the compiler).
|
Chris@16
|
50 // These functions assume tuples to be of the same length.
|
Chris@16
|
51
|
Chris@16
|
52
|
Chris@16
|
53 template<class T1, class T2>
|
Chris@16
|
54 inline bool eq(const T1& lhs, const T2& rhs) {
|
Chris@16
|
55 return lhs.get_head() == rhs.get_head() &&
|
Chris@16
|
56 eq(lhs.get_tail(), rhs.get_tail());
|
Chris@16
|
57 }
|
Chris@16
|
58 template<>
|
Chris@16
|
59 inline bool eq<null_type,null_type>(const null_type&, const null_type&) { return true; }
|
Chris@16
|
60
|
Chris@16
|
61 template<class T1, class T2>
|
Chris@16
|
62 inline bool neq(const T1& lhs, const T2& rhs) {
|
Chris@16
|
63 return lhs.get_head() != rhs.get_head() ||
|
Chris@16
|
64 neq(lhs.get_tail(), rhs.get_tail());
|
Chris@16
|
65 }
|
Chris@16
|
66 template<>
|
Chris@16
|
67 inline bool neq<null_type,null_type>(const null_type&, const null_type&) { return false; }
|
Chris@16
|
68
|
Chris@16
|
69 template<class T1, class T2>
|
Chris@16
|
70 inline bool lt(const T1& lhs, const T2& rhs) {
|
Chris@16
|
71 return lhs.get_head() < rhs.get_head() ||
|
Chris@16
|
72 ( !(rhs.get_head() < lhs.get_head()) &&
|
Chris@16
|
73 lt(lhs.get_tail(), rhs.get_tail()));
|
Chris@16
|
74 }
|
Chris@16
|
75 template<>
|
Chris@16
|
76 inline bool lt<null_type,null_type>(const null_type&, const null_type&) { return false; }
|
Chris@16
|
77
|
Chris@16
|
78 template<class T1, class T2>
|
Chris@16
|
79 inline bool gt(const T1& lhs, const T2& rhs) {
|
Chris@16
|
80 return lhs.get_head() > rhs.get_head() ||
|
Chris@16
|
81 ( !(rhs.get_head() > lhs.get_head()) &&
|
Chris@16
|
82 gt(lhs.get_tail(), rhs.get_tail()));
|
Chris@16
|
83 }
|
Chris@16
|
84 template<>
|
Chris@16
|
85 inline bool gt<null_type,null_type>(const null_type&, const null_type&) { return false; }
|
Chris@16
|
86
|
Chris@16
|
87 template<class T1, class T2>
|
Chris@16
|
88 inline bool lte(const T1& lhs, const T2& rhs) {
|
Chris@16
|
89 return lhs.get_head() <= rhs.get_head() &&
|
Chris@16
|
90 ( !(rhs.get_head() <= lhs.get_head()) ||
|
Chris@16
|
91 lte(lhs.get_tail(), rhs.get_tail()));
|
Chris@16
|
92 }
|
Chris@16
|
93 template<>
|
Chris@16
|
94 inline bool lte<null_type,null_type>(const null_type&, const null_type&) { return true; }
|
Chris@16
|
95
|
Chris@16
|
96 template<class T1, class T2>
|
Chris@16
|
97 inline bool gte(const T1& lhs, const T2& rhs) {
|
Chris@16
|
98 return lhs.get_head() >= rhs.get_head() &&
|
Chris@16
|
99 ( !(rhs.get_head() >= lhs.get_head()) ||
|
Chris@16
|
100 gte(lhs.get_tail(), rhs.get_tail()));
|
Chris@16
|
101 }
|
Chris@16
|
102 template<>
|
Chris@16
|
103 inline bool gte<null_type,null_type>(const null_type&, const null_type&) { return true; }
|
Chris@16
|
104
|
Chris@16
|
105 } // end of namespace detail
|
Chris@16
|
106
|
Chris@16
|
107
|
Chris@16
|
108 // equal ----
|
Chris@16
|
109
|
Chris@16
|
110 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
111 inline bool operator==(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
112 {
|
Chris@16
|
113 // check that tuple lengths are equal
|
Chris@16
|
114 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
115
|
Chris@16
|
116 return detail::eq(lhs, rhs);
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 // not equal -----
|
Chris@16
|
120
|
Chris@16
|
121 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
122 inline bool operator!=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
123 {
|
Chris@16
|
124
|
Chris@16
|
125 // check that tuple lengths are equal
|
Chris@16
|
126 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
127
|
Chris@16
|
128 return detail::neq(lhs, rhs);
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 // <
|
Chris@16
|
132 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
133 inline bool operator<(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
134 {
|
Chris@16
|
135 // check that tuple lengths are equal
|
Chris@16
|
136 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
137
|
Chris@16
|
138 return detail::lt(lhs, rhs);
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 // >
|
Chris@16
|
142 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
143 inline bool operator>(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
144 {
|
Chris@16
|
145 // check that tuple lengths are equal
|
Chris@16
|
146 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
147
|
Chris@16
|
148 return detail::gt(lhs, rhs);
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 // <=
|
Chris@16
|
152 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
153 inline bool operator<=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
154 {
|
Chris@16
|
155 // check that tuple lengths are equal
|
Chris@16
|
156 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
157
|
Chris@16
|
158 return detail::lte(lhs, rhs);
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 // >=
|
Chris@16
|
162 template<class T1, class T2, class S1, class S2>
|
Chris@16
|
163 inline bool operator>=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
|
Chris@16
|
164 {
|
Chris@16
|
165 // check that tuple lengths are equal
|
Chris@16
|
166 BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
|
Chris@16
|
167
|
Chris@16
|
168 return detail::gte(lhs, rhs);
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 } // end of namespace tuples
|
Chris@16
|
172 } // end of namespace boost
|
Chris@16
|
173
|
Chris@16
|
174
|
Chris@16
|
175 #endif // BOOST_TUPLE_COMPARISON_HPP
|