source: NonGTP/Boost/boost/tuple/tuple_comparison.hpp @ 857

Revision 857, 5.6 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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// 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
35namespace boost {
36namespace tuples {
37
38inline bool operator==(const null_type&, const null_type&) { return true; }
39inline bool operator>=(const null_type&, const null_type&) { return true; }
40inline bool operator<=(const null_type&, const null_type&) { return true; }
41inline bool operator!=(const null_type&, const null_type&) { return false; }
42inline bool operator<(const null_type&, const null_type&) { return false; }
43inline bool operator>(const null_type&, const null_type&) { return false; }
44
45
46namespace 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
53template<class T1, class T2>
54inline 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}
58template<>
59inline bool eq<null_type,null_type>(const null_type&, const null_type&) { return true; }
60
61template<class T1, class T2>
62inline 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}
66template<>
67inline bool neq<null_type,null_type>(const null_type&, const null_type&) { return false; }
68
69template<class T1, class T2>
70inline 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}
75template<>
76inline bool lt<null_type,null_type>(const null_type&, const null_type&) { return false; }
77
78template<class T1, class T2>
79inline 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}
84template<>
85inline bool gt<null_type,null_type>(const null_type&, const null_type&) { return false; }
86
87template<class T1, class T2>
88inline 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}
93template<>
94inline bool lte<null_type,null_type>(const null_type&, const null_type&) { return true; }
95
96template<class T1, class T2>
97inline 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}
102template<>
103inline 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
110template<class T1, class T2, class S1, class S2>
111inline 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
121template<class T1, class T2, class S1, class S2>
122inline 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// <
132template<class T1, class T2, class S1, class S2>
133inline 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// >
142template<class T1, class T2, class S1, class S2>
143inline 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// <=
152template<class T1, class T2, class S1, class S2>
153inline 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// >=
162template<class T1, class T2, class S1, class S2>
163inline 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
Note: See TracBrowser for help on using the repository browser.