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