Boost logo

Boost :

From: Dave Abrahams (abrahams_at_[hidden])
Date: 2000-06-08 11:21:56


How often have you written an aggregate class from which you needed
to use all or some data members to form a sort key for a standard
container or algorithm? Happens to me all the time. As such, I
propose the following suite of template functions as an addition to
utility.hpp (please, someone, suggest a better name!)

// CAVEAT: completely untested.
template <class T1, class T2, class T3, class T4, class T5, class T6,
class T7>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2,
    const T3& x3, const T3& y3,
    const T4& x4, const T4& y4,
    const T5& x5, const T5& y5,
    const T6& x6, const T6& y6,
    const T7& x7, const T7& y7)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && (std::less(x2, y2) || !std::less(y2, x2)
            && (std::less(x3, y3) || !std::less(y3, x3)
                && (std::less(x4, y4) || !std::less(y4, x4)
                    && (std::less(x5, y5) || !std::less(y5, x5)
                        && (std::less(x6, y6) || !std::less(y6, x6)
                            && std::less(x7, y7))))));
}

template <class T1, class T2, class T3, class T4, class T5, class T6>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2,
    const T3& x3, const T3& y3,
    const T4& x4, const T4& y4,
    const T5& x5, const T5& y5,
    const T6& x6, const T6& y6)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && (std::less(x2, y2) || !std::less(y2, x2)
            && (std::less(x3, y3) || !std::less(y3, x3)
                && (std::less(x4, y4) || !std::less(y4, x4)
                    && (std::less(x5, y5) || !std::less(y5, x5)
                        && std::less(x6, y6)))));
}

template <class T1, class T2, class T3, class T4, class T5>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2,
    const T3& x3, const T3& y3,
    const T4& x4, const T4& y4,
    const T5& x5, const T5& y5)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && (std::less(x2, y2) || !std::less(y2, x2)
            && (std::less(x3, y3) || !std::less(y3, x3)
                && (std::less(x4, y4) || !std::less(y4, x4)
                    && std::less(x5, y5))));
}

template <class T1, class T2, class T3, class T4>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2,
    const T3& x3, const T3& y3,
    const T4& x4, const T4& y4)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && (std::less(x2, y2) || !std::less(y2, x2)
            && (std::less(x3, y3) || !std::less(y3, x3)
                && std::less(x4, y4)));
}

template <class T1, class T2, class T3>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2,
    const T3& x3, const T3& y3)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && (std::less(x2, y2) || !std::less(y2, x2)
            && std::less(x3, y3));
}

template <class T1, class T2>
bool lexicographic_less(
    const T1& x1, const T1& y1,
    const T2& x2, const T2& y2)
{
    return std::less(x1, y1) || !std::less(y1, x1)
        && std::less(x2, y2);
}

template <class T1>
bool lexicographic_less(
    const T1& x1, const T1& y1)
{
    return std::less(x1, y1);
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk