From a80f36e343973813924e3649b6c479b25128feb0 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 4 Nov 2024 17:46:03 +0000 Subject: [PATCH] Use C++20 default comparisons for section objects --- src/section_data.cpp | 27 +++------------------------ src/sections.cpp | 32 +------------------------------- src/sections.h | 17 ++++------------- 3 files changed, 8 insertions(+), 68 deletions(-) diff --git a/src/section_data.cpp b/src/section_data.cpp index 67e52413e3..d12437f994 100644 --- a/src/section_data.cpp +++ b/src/section_data.cpp @@ -10,6 +10,9 @@ struct section_num { std::string prefix; std::vector num; + + bool operator==(const section_num&) const = default; + auto operator<=>(const section_num&) const = default; }; std::istream& @@ -82,30 +85,6 @@ operator << (std::ostream& os, const section_num& sn) return os; } -bool -operator<(const section_num& x, const section_num& y) -{ - if (x.prefix < y.prefix) - return true; - else if (y.prefix < x.prefix) - return false; - return x.num < y.num; -} - -bool -operator==(const section_num& x, const section_num& y) -{ - if (x.prefix != y.prefix) - return false; - return x.num == y.num; -} - -bool -operator!=(const section_num& x, const section_num& y) -{ - return !(x == y); -} - typedef std::string section_tag; std::string diff --git a/src/sections.cpp b/src/sections.cpp index 546ee39bba..70eca1ac64 100644 --- a/src/sections.cpp +++ b/src/sections.cpp @@ -4,22 +4,9 @@ #include #include #include +#include #include -auto lwg::operator<(section_tag const & x, section_tag const & y) noexcept -> bool { - return (x.prefix < y.prefix) ? true - : (y.prefix < x.prefix) ? false - : x.name < y.name; -} - -auto lwg::operator==(section_tag const & x, section_tag const & y) noexcept -> bool { - return x.prefix == y.prefix && x.name == y.name; -} - -auto lwg::operator!=(section_tag const & x, section_tag const & y) noexcept -> bool { - return !(x == y); -} - auto lwg::operator << (std::ostream& os, section_tag const & tag) -> std::ostream & { os << '['; if (!tag.prefix.empty()) { os << tag.prefix << "::"; } @@ -34,23 +21,6 @@ std::string lwg::as_string(section_tag const & x) : x.prefix + "::" + x.name; } -auto lwg::operator < (section_num const & x, section_num const & y) noexcept -> bool { - // prefixes are unique, so there should be no need for a tiebreak. - return (x.prefix < y.prefix) ? true - : (y.prefix < x.prefix) ? false - : x.num < y.num; -} - -auto lwg::operator == (section_num const & x, section_num const & y) noexcept -> bool { - return (x.prefix != y.prefix) - ? false - : x.num == y.num; -} - -auto lwg::operator != (section_num const & x, section_num const & y) noexcept -> bool { - return !(x == y); -} - auto lwg::operator >> (std::istream& is, section_num& sn) -> std::istream & { sn.prefix.clear(); sn.num.clear(); diff --git a/src/sections.h b/src/sections.h index f9727afa6e..53e8b9de94 100644 --- a/src/sections.h +++ b/src/sections.h @@ -13,33 +13,24 @@ struct section_tag { std::string prefix; // example: fund.ts.v2 std::string name; // example: meta.logical + bool operator==(const section_tag&) const = default; + auto operator<=>(const section_tag&) const = default; }; struct section_num { std::string prefix; // example: fund.ts.v2 std::vector num; // sequence of numbers corresponding to section number // in relevant doc, e.g,, 17.5.2.1.4.2 + bool operator==(const section_num&) const = default; + auto operator<=>(const section_num&) const = default; }; using section_map = std::map; -auto operator < (section_tag const & x, section_tag const & y) noexcept -> bool; -auto operator == (section_tag const & x, section_tag const & y) noexcept -> bool; -auto operator != (section_tag const & x, section_tag const & y) noexcept -> bool; auto operator << (std::ostream & os, section_tag const & tag) -> std::ostream &; // with square brackets std::string as_string(section_tag const & x); // without square brackets -auto operator < (section_num const & x, section_num const & y) noexcept -> bool; - // section 'x' sorts before section 'y' if its 'prefix' field lexicographically - // precedes that of 'y', and its 'nun' field lexicographically precedes that - // of 'y' if the prefix fields are equivalent. - -auto operator == (section_num const & x, section_num const & y) noexcept -> bool; -auto operator != (section_num const & x, section_num const & y) noexcept -> bool; - // Two 'section_num' objects compare equal if their 'prefix' and 'num' both - // compare equal. - auto operator >> (std::istream & is, section_num & sn) -> std::istream &; auto operator << (std::ostream & os, section_num const & sn) -> std::ostream &;