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