2019-03-08 09:37:02 +11:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base.hpp"
|
|
|
|
|
2020-10-21 15:03:08 +11:00
|
|
|
#include <compare>
|
|
|
|
|
2019-03-08 09:37:02 +11:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace cruft::coord {
|
|
|
|
/// A StrictWeakOrder over coordinate types.
|
|
|
|
///
|
|
|
|
/// The comparison operates across successive elements of the two
|
|
|
|
/// coordinates.
|
|
|
|
template <typename CoordT>
|
2020-10-21 15:03:08 +11:00
|
|
|
std::weak_ordering
|
|
|
|
element_ordering (
|
|
|
|
CoordT const &a,
|
|
|
|
CoordT const &b
|
|
|
|
) noexcept {
|
|
|
|
for (std::size_t i = 0; i < CoordT::elements; ++i)
|
|
|
|
if (auto cmp = a[i] <=> b[i]; cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
|
|
|
|
return std::weak_ordering::equivalent;
|
|
|
|
}
|
2019-05-12 07:51:42 +10:00
|
|
|
|
|
|
|
|
|
|
|
/// A StrictWeakOrder over coordinate types.
|
|
|
|
///
|
|
|
|
/// Deduction is deferred until the function is called, in the same way
|
|
|
|
/// that std::less<> operates.
|
2020-10-21 15:03:08 +11:00
|
|
|
template <typename = void>
|
|
|
|
struct ordering {
|
2019-05-12 07:51:42 +10:00
|
|
|
template <typename CoordT>
|
2020-10-21 15:03:08 +11:00
|
|
|
bool
|
|
|
|
operator() (CoordT const &a, CoordT const &b) const noexcept
|
2019-05-12 07:51:42 +10:00
|
|
|
{
|
2020-10-21 15:03:08 +11:00
|
|
|
return element_ordering<CoordT> (a, b) < 0;
|
2019-05-12 07:51:42 +10:00
|
|
|
}
|
|
|
|
};
|
2019-03-08 09:37:02 +11:00
|
|
|
}
|