Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:52:55

0001 // tuple_comparison.hpp -----------------------------------------------------
0002 //
0003 // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
0004 // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com)
0005 //
0006 // Distributed under the Boost Software License, Version 1.0. (See
0007 // accompanying file LICENSE_1_0.txt or copy at
0008 // http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 // For more information, see http://www.boost.org
0011 //
0012 // (The idea and first impl. of comparison operators was from Doug Gregor)
0013 
0014 // -----------------------------------------------------------------
0015 
0016 #ifndef BOOST_TUPLE_COMPARISON_HPP
0017 #define BOOST_TUPLE_COMPARISON_HPP
0018 
0019 #include <boost/tuple/tuple.hpp>
0020 
0021 // -------------------------------------------------------------
0022 // equality and comparison operators
0023 //
0024 // == and != compare tuples elementwise
0025 // <, >, <= and >= use lexicographical ordering
0026 //
0027 // Any operator between tuples of different length fails at compile time
0028 // No dependencies between operators are assumed
0029 // (i.e. !(a<b)  does not imply a>=b, a!=b does not imply a==b etc.
0030 // so any weirdnesses of elementary operators are respected).
0031 //
0032 // -------------------------------------------------------------
0033 
0034 
0035 namespace boost {
0036 namespace tuples {
0037 
0038 inline bool operator==(const null_type&, const null_type&) { return true; }
0039 inline bool operator>=(const null_type&, const null_type&) { return true; }
0040 inline bool operator<=(const null_type&, const null_type&) { return true; }
0041 inline bool operator!=(const null_type&, const null_type&) { return false; }
0042 inline bool operator<(const null_type&, const null_type&) { return false; }
0043 inline bool operator>(const null_type&, const null_type&) { return false; }
0044 
0045 
0046 namespace detail {
0047   // comparison operators check statically the length of its operands and
0048   // delegate the comparing task to the following functions. Hence
0049   // the static check is only made once (should help the compiler).
0050   // These functions assume tuples to be of the same length.
0051 
0052 
0053 template<class T1, class T2>
0054 inline bool eq(const T1& lhs, const T2& rhs) {
0055   return lhs.get_head() == rhs.get_head() &&
0056          eq(lhs.get_tail(), rhs.get_tail());
0057 }
0058 template<>
0059 inline bool eq<null_type,null_type>(const null_type&, const null_type&) { return true; }
0060 
0061 template<class T1, class T2>
0062 inline bool neq(const T1& lhs, const T2& rhs) {
0063   return lhs.get_head() != rhs.get_head()  ||
0064          neq(lhs.get_tail(), rhs.get_tail());
0065 }
0066 template<>
0067 inline bool neq<null_type,null_type>(const null_type&, const null_type&) { return false; }
0068 
0069 template<class T1, class T2>
0070 inline bool lt(const T1& lhs, const T2& rhs) {
0071   return lhs.get_head() < rhs.get_head()  ||
0072           ( !(rhs.get_head() < lhs.get_head()) &&
0073             lt(lhs.get_tail(), rhs.get_tail()));
0074 }
0075 template<>
0076 inline bool lt<null_type,null_type>(const null_type&, const null_type&) { return false; }
0077 
0078 template<class T1, class T2>
0079 inline bool gt(const T1& lhs, const T2& rhs) {
0080   return lhs.get_head() > rhs.get_head()  ||
0081           ( !(rhs.get_head() > lhs.get_head()) &&
0082             gt(lhs.get_tail(), rhs.get_tail()));
0083 }
0084 template<>
0085 inline bool gt<null_type,null_type>(const null_type&, const null_type&) { return false; }
0086 
0087 template<class T1, class T2>
0088 inline bool lte(const T1& lhs, const T2& rhs) {
0089   return lhs.get_head() <= rhs.get_head()  &&
0090           ( !(rhs.get_head() <= lhs.get_head()) ||
0091             lte(lhs.get_tail(), rhs.get_tail()));
0092 }
0093 template<>
0094 inline bool lte<null_type,null_type>(const null_type&, const null_type&) { return true; }
0095 
0096 template<class T1, class T2>
0097 inline bool gte(const T1& lhs, const T2& rhs) {
0098   return lhs.get_head() >= rhs.get_head()  &&
0099           ( !(rhs.get_head() >= lhs.get_head()) ||
0100             gte(lhs.get_tail(), rhs.get_tail()));
0101 }
0102 template<>
0103 inline bool gte<null_type,null_type>(const null_type&, const null_type&) { return true; }
0104 
0105 } // end of namespace detail
0106 
0107 
0108 // equal ----
0109 
0110 template<class T1, class T2, class S1, class S2>
0111 inline bool operator==(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0112 {
0113   // check that tuple lengths are equal
0114   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0115 
0116   return  detail::eq(lhs, rhs);
0117 }
0118 
0119 // not equal -----
0120 
0121 template<class T1, class T2, class S1, class S2>
0122 inline bool operator!=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0123 {
0124 
0125   // check that tuple lengths are equal
0126   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0127 
0128   return detail::neq(lhs, rhs);
0129 }
0130 
0131 // <
0132 template<class T1, class T2, class S1, class S2>
0133 inline bool operator<(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0134 {
0135   // check that tuple lengths are equal
0136   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0137 
0138   return detail::lt(lhs, rhs);
0139 }
0140 
0141 // >
0142 template<class T1, class T2, class S1, class S2>
0143 inline bool operator>(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0144 {
0145   // check that tuple lengths are equal
0146   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0147 
0148   return detail::gt(lhs, rhs);
0149 }
0150 
0151 // <=
0152 template<class T1, class T2, class S1, class S2>
0153 inline bool operator<=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0154 {
0155   // check that tuple lengths are equal
0156   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0157 
0158   return detail::lte(lhs, rhs);
0159 }
0160 
0161 // >=
0162 template<class T1, class T2, class S1, class S2>
0163 inline bool operator>=(const cons<T1, T2>& lhs, const cons<S1, S2>& rhs)
0164 {
0165   // check that tuple lengths are equal
0166   BOOST_STATIC_ASSERT(length<T2>::value == length<S2>::value);
0167 
0168   return detail::gte(lhs, rhs);
0169 }
0170 
0171 } // end of namespace tuples
0172 } // end of namespace boost
0173 
0174 
0175 #endif // BOOST_TUPLE_COMPARISON_HPP