feat(kernel/std): add three way compare for std::basic_string and std::vector

This commit is contained in:
EnderIce2 2025-05-10 16:32:54 +00:00
parent dbb5a483e0
commit 9626ec4662
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
2 changed files with 37 additions and 8 deletions

View File

@ -26,6 +26,7 @@
#include <convert.h>
#include <iterator>
#include <cstddef>
#include <compare>
#include <memory>
#include <ranges>
@ -2264,12 +2265,24 @@ namespace std
return lhs.compare(rhs) == 0;
}
template <class CharT, class Traits, class Alloc>
constexpr auto operator<=>(const std::basic_string<CharT, Traits, Alloc> &lhs, const std::basic_string<CharT, Traits, Alloc> &rhs) noexcept -> decltype(static_cast<weak_ordering>(0 <=> 0))
{
return static_cast<weak_ordering>(lhs.compare(rhs) <=> 0);
}
template <class CharT, class Traits, class Alloc>
constexpr bool operator==(const std::basic_string<CharT, Traits, Alloc> &lhs, const CharT *rhs)
{
return lhs.compare(rhs) == 0;
}
template <class CharT, class Traits, class Alloc>
constexpr auto operator<=>(const std::basic_string<CharT, Traits, Alloc> &lhs, const CharT *rhs) -> decltype(static_cast<weak_ordering>(0 <=> 0))
{
return static_cast<weak_ordering>(lhs.compare(rhs) <=> 0);
}
template <class CharT, class Traits, class Alloc>
std::basic_string<CharT, Traits, Alloc> constexpr operator+(const std::basic_string<CharT, Traits, Alloc> &lhs, const std::basic_string<CharT, Traits, Alloc> &rhs)
{

View File

@ -21,6 +21,7 @@
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include <compare>
#include <memory>
namespace std
@ -984,17 +985,32 @@ namespace std
constexpr bool operator==(const std::vector<T, Alloc> &lhs, const std::vector<T, Alloc> &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 <class T, class Alloc>
constexpr auto operator<=>(const std::vector<T, Alloc> &lhs, const std::vector<T, Alloc> &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 <class T, class Alloc>