Chris@16: // tuple_comparison.hpp ----------------------------------------------------- Chris@16: // Chris@16: // Copyright (C) 2001 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) Chris@16: // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // For more information, see http://www.boost.org Chris@16: // Chris@16: // (The idea and first impl. of comparison operators was from Doug Gregor) Chris@16: Chris@16: // ----------------------------------------------------------------- Chris@16: Chris@16: #ifndef BOOST_TUPLE_COMPARISON_HPP Chris@16: #define BOOST_TUPLE_COMPARISON_HPP Chris@16: Chris@16: #include "boost/tuple/tuple.hpp" Chris@16: Chris@16: // ------------------------------------------------------------- Chris@16: // equality and comparison operators Chris@16: // Chris@16: // == and != compare tuples elementwise Chris@16: // <, >, <= and >= use lexicographical ordering Chris@16: // Chris@16: // Any operator between tuples of different length fails at compile time Chris@16: // No dependencies between operators are assumed Chris@16: // (i.e. !(a=b, a!=b does not imply a==b etc. Chris@16: // so any weirdnesses of elementary operators are respected). Chris@16: // Chris@16: // ------------------------------------------------------------- Chris@16: Chris@16: Chris@16: namespace boost { Chris@16: namespace tuples { Chris@16: Chris@16: inline bool operator==(const null_type&, const null_type&) { return true; } Chris@16: inline bool operator>=(const null_type&, const null_type&) { return true; } Chris@16: inline bool operator<=(const null_type&, const null_type&) { return true; } Chris@16: inline bool operator!=(const null_type&, const null_type&) { return false; } Chris@16: inline bool operator<(const null_type&, const null_type&) { return false; } Chris@16: inline bool operator>(const null_type&, const null_type&) { return false; } Chris@16: Chris@16: Chris@16: namespace detail { Chris@16: // comparison operators check statically the length of its operands and Chris@16: // delegate the comparing task to the following functions. Hence Chris@16: // the static check is only made once (should help the compiler). Chris@16: // These functions assume tuples to be of the same length. Chris@16: Chris@16: Chris@16: template Chris@16: inline bool eq(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() == rhs.get_head() && Chris@16: eq(lhs.get_tail(), rhs.get_tail()); Chris@16: } Chris@16: template<> Chris@16: inline bool eq(const null_type&, const null_type&) { return true; } Chris@16: Chris@16: template Chris@16: inline bool neq(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() != rhs.get_head() || Chris@16: neq(lhs.get_tail(), rhs.get_tail()); Chris@16: } Chris@16: template<> Chris@16: inline bool neq(const null_type&, const null_type&) { return false; } Chris@16: Chris@16: template Chris@16: inline bool lt(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() < rhs.get_head() || Chris@16: ( !(rhs.get_head() < lhs.get_head()) && Chris@16: lt(lhs.get_tail(), rhs.get_tail())); Chris@16: } Chris@16: template<> Chris@16: inline bool lt(const null_type&, const null_type&) { return false; } Chris@16: Chris@16: template Chris@16: inline bool gt(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() > rhs.get_head() || Chris@16: ( !(rhs.get_head() > lhs.get_head()) && Chris@16: gt(lhs.get_tail(), rhs.get_tail())); Chris@16: } Chris@16: template<> Chris@16: inline bool gt(const null_type&, const null_type&) { return false; } Chris@16: Chris@16: template Chris@16: inline bool lte(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() <= rhs.get_head() && Chris@16: ( !(rhs.get_head() <= lhs.get_head()) || Chris@16: lte(lhs.get_tail(), rhs.get_tail())); Chris@16: } Chris@16: template<> Chris@16: inline bool lte(const null_type&, const null_type&) { return true; } Chris@16: Chris@16: template Chris@16: inline bool gte(const T1& lhs, const T2& rhs) { Chris@16: return lhs.get_head() >= rhs.get_head() && Chris@16: ( !(rhs.get_head() >= lhs.get_head()) || Chris@16: gte(lhs.get_tail(), rhs.get_tail())); Chris@16: } Chris@16: template<> Chris@16: inline bool gte(const null_type&, const null_type&) { return true; } Chris@16: Chris@16: } // end of namespace detail Chris@16: Chris@16: Chris@16: // equal ---- Chris@16: Chris@16: template Chris@16: inline bool operator==(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::eq(lhs, rhs); Chris@16: } Chris@16: Chris@16: // not equal ----- Chris@16: Chris@16: template Chris@16: inline bool operator!=(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::neq(lhs, rhs); Chris@16: } Chris@16: Chris@16: // < Chris@16: template Chris@16: inline bool operator<(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::lt(lhs, rhs); Chris@16: } Chris@16: Chris@16: // > Chris@16: template Chris@16: inline bool operator>(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::gt(lhs, rhs); Chris@16: } Chris@16: Chris@16: // <= Chris@16: template Chris@16: inline bool operator<=(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::lte(lhs, rhs); Chris@16: } Chris@16: Chris@16: // >= Chris@16: template Chris@16: inline bool operator>=(const cons& lhs, const cons& rhs) Chris@16: { Chris@16: // check that tuple lengths are equal Chris@16: BOOST_STATIC_ASSERT(length::value == length::value); Chris@16: Chris@16: return detail::gte(lhs, rhs); Chris@16: } Chris@16: Chris@16: } // end of namespace tuples Chris@16: } // end of namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_TUPLE_COMPARISON_HPP