From 5a7613d3f355cf020aad3f4c6514c64c9b6575d0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 21 Oct 2020 14:03:08 +1000 Subject: [PATCH] coord/comparator: convert to use std::weak_ordering This allows more flexibility in some client code (eg, calling this to sort compound objects like region). --- coord/comparator.hpp | 40 +++++++++++++++++----------------------- test/coord.cpp | 15 +++++++-------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/coord/comparator.hpp b/coord/comparator.hpp index 1efd620a..c73cb8e2 100644 --- a/coord/comparator.hpp +++ b/coord/comparator.hpp @@ -10,48 +10,42 @@ #include "base.hpp" +#include + #include /////////////////////////////////////////////////////////////////////////////// namespace cruft::coord { - /// A StrictWeakOrder over coordinate types. - template - struct ordering; - - /// 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; - } + 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 false; - } - }; + return std::weak_ordering::equivalent; + } /// A StrictWeakOrder over coordinate types. /// /// Deduction is deferred until the function is called, in the same way /// that std::less<> operates. - template <> - struct ordering { + template + struct ordering { template - bool operator() (CoordT const &a, CoordT const &b) const noexcept + bool + operator() (CoordT const &a, CoordT const &b) const noexcept { - return ordering {} (a, b); + return element_ordering (a, b) < 0; } }; } diff --git a/test/coord.cpp b/test/coord.cpp index 0ebdd1ad..ba238d84 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -132,19 +132,18 @@ main (void) struct { cruft::point2i a; cruft::point2i b; - bool expected; + std::weak_ordering 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" }, + { { 0, 0 }, { 0, 0 }, std::weak_ordering::equivalent, "equal zeroes" }, + { { 1, 0 }, { 0, 0 }, std::weak_ordering::greater, "a, leading high" }, + { { 0, 1 }, { 0, 0 }, std::weak_ordering::greater, "a, trailing high" }, + { { 0, 0 }, { 1, 0 }, std::weak_ordering::less, "b, leading high" }, + { { 0, 0 }, { 0, 1 }, std::weak_ordering::less, "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); + tap.expect (cruft::coord::element_ordering (t.a, t.b) == t.expected, "ordering; %!", t.message); } return tap.status ();