diff --git a/CMakeLists.txt b/CMakeLists.txt index e4e0ccc4..08c7fa99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/coord/comparator.hpp b/coord/comparator.hpp new file mode 100644 index 00000000..22f3b082 --- /dev/null +++ b/coord/comparator.hpp @@ -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 + */ + +#pragma once + +#include "base.hpp" + +#include + + +/////////////////////////////////////////////////////////////////////////////// +namespace cruft::coord { + /// A StrictWeakOrder over coordinate types. + /// + /// The comparison operates across successive elements of the two + /// coordinates. + template + 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; + } + }; +} diff --git a/test/coord.cpp b/test/coord.cpp index e3177abe..d9161b44 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -3,6 +3,7 @@ #include "vector.hpp" #include "coord/iostream.hpp" +#include "coord/comparator.hpp" #include @@ -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 const comp; + for (auto const &t: TESTS) + tap.expect (comp (t.a, t.b) == t.expected, "ordering; %!", t.message); + } return tap.status (); }