From 9626ec4662bbf5797164c218f83284329b3afb18 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sat, 10 May 2025 16:32:54 +0000 Subject: [PATCH] feat(kernel/std): :sparkles: add three way compare for std::basic_string and std::vector --- Kernel/include_std/string | 13 +++++++++++++ Kernel/include_std/vector | 32 ++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Kernel/include_std/string b/Kernel/include_std/string index bf824807..f946b7f2 100644 --- a/Kernel/include_std/string +++ b/Kernel/include_std/string @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -2264,12 +2265,24 @@ namespace std return lhs.compare(rhs) == 0; } + template + constexpr auto operator<=>(const std::basic_string &lhs, const std::basic_string &rhs) noexcept -> decltype(static_cast(0 <=> 0)) + { + return static_cast(lhs.compare(rhs) <=> 0); + } + template constexpr bool operator==(const std::basic_string &lhs, const CharT *rhs) { return lhs.compare(rhs) == 0; } + template + constexpr auto operator<=>(const std::basic_string &lhs, const CharT *rhs) -> decltype(static_cast(0 <=> 0)) + { + return static_cast(lhs.compare(rhs) <=> 0); + } + template std::basic_string constexpr operator+(const std::basic_string &lhs, const std::basic_string &rhs) { diff --git a/Kernel/include_std/vector b/Kernel/include_std/vector index ad309312..4de81ffa 100644 --- a/Kernel/include_std/vector +++ b/Kernel/include_std/vector @@ -21,6 +21,7 @@ #include #include #include +#include #include namespace std @@ -984,17 +985,32 @@ namespace std constexpr bool operator==(const std::vector &lhs, const std::vector &rhs) { if (lhs.size() != rhs.size()) - { return false; - } - for (size_t i = 0; i < lhs.size(); i++) + + return std::equal(lhs.begin(), lhs.end(), rhs.begin()); + } + + template + constexpr auto operator<=>(const std::vector &lhs, const std::vector &rhs) + { + // return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), __synth_three_way); + + auto it1 = lhs.begin(); + auto it2 = rhs.begin(); + + while (it1 != lhs.end() && it2 != rhs.end()) { - if (lhs[i] != rhs[i]) - { - return false; - } + if (*it1 < *it2) + return std::strong_ordering::less; + if (*it1 > *it2) + return std::strong_ordering::greater; + ++it1; + ++it2; } - return true; + + if (it1 == lhs.end() && it2 == rhs.end()) + return std::strong_ordering::equal; + return (it1 == lhs.end()) ? std::strong_ordering::less : std::strong_ordering::greater; } template