coord/comparator: add a trivial ordering comparator

This commit is contained in:
Danny Robson 2019-03-08 09:37:02 +11:00
parent 3c07f96d34
commit 0d27694a51
3 changed files with 61 additions and 2 deletions

View File

@ -258,9 +258,10 @@ list (
cmdopt.hpp
colour.cpp
colour.hpp
coord.hpp
coord/fwd.hpp
coord/base.hpp
coord.hpp
coord/comparator.hpp
coord/init.hpp
coord/iostream.hpp
coord/ops.hpp

38
coord/comparator.hpp Normal file
View File

@ -0,0 +1,38 @@
/*
* 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"
#include <cstddef>
///////////////////////////////////////////////////////////////////////////////
namespace cruft::coord {
/// A StrictWeakOrder over coordinate types.
///
/// The comparison operates across successive elements of the two
/// coordinates.
template <typename CoordT>
struct ordering {
bool operator() (
CoordT const &a,
CoordT const &b
) const noexcept {
for (std::size_t i = 0; i < CoordT::elements; ++i) {
if (a[i] < b[i])
return true;
else if (a[i] > b[i])
return false;
}
return false;
}
};
}

View File

@ -3,6 +3,7 @@
#include "vector.hpp"
#include "coord/iostream.hpp"
#include "coord/comparator.hpp"
#include <type_traits>
@ -118,7 +119,26 @@ main (void)
cruft::make_vector (3, 4, 0),
"rshift, coord fill"
);
}
{
struct {
cruft::point2i a;
cruft::point2i b;
bool expected;
char const *message;
} const TESTS[] {
{ { 0, 0 }, { 0, 0 }, false, "equal zeroes" },
{ { 1, 0 }, { 0, 0 }, false, "a, leading high" },
{ { 0, 1 }, { 0, 0 }, false, "a, trailing high" },
{ { 0, 0 }, { 1, 0 }, true, "b, leading high" },
{ { 0, 0 }, { 0, 1 }, true, "b, trailing high" },
};
cruft::coord::ordering<cruft::point2i> const comp;
for (auto const &t: TESTS)
tap.expect (comp (t.a, t.b) == t.expected, "ordering; %!", t.message);
}
return tap.status ();
}