mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
feat(kernel/std): ✨ add three way compare for std::basic_string and std::vector
This commit is contained in:
parent
dbb5a483e0
commit
9626ec4662
@ -26,6 +26,7 @@
|
|||||||
#include <convert.h>
|
#include <convert.h>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <compare>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
|
||||||
@ -2264,12 +2265,24 @@ namespace std
|
|||||||
return lhs.compare(rhs) == 0;
|
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>
|
template <class CharT, class Traits, class Alloc>
|
||||||
constexpr bool operator==(const std::basic_string<CharT, Traits, Alloc> &lhs, const CharT *rhs)
|
constexpr bool operator==(const std::basic_string<CharT, Traits, Alloc> &lhs, const CharT *rhs)
|
||||||
{
|
{
|
||||||
return lhs.compare(rhs) == 0;
|
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>
|
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)
|
std::basic_string<CharT, Traits, Alloc> constexpr operator+(const std::basic_string<CharT, Traits, Alloc> &lhs, const std::basic_string<CharT, Traits, Alloc> &rhs)
|
||||||
{
|
{
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <compare>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
@ -984,17 +985,32 @@ namespace std
|
|||||||
constexpr bool operator==(const std::vector<T, Alloc> &lhs, const std::vector<T, Alloc> &rhs)
|
constexpr bool operator==(const std::vector<T, Alloc> &lhs, const std::vector<T, Alloc> &rhs)
|
||||||
{
|
{
|
||||||
if (lhs.size() != rhs.size())
|
if (lhs.size() != rhs.size())
|
||||||
{
|
|
||||||
return false;
|
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])
|
if (*it1 < *it2)
|
||||||
{
|
return std::strong_ordering::less;
|
||||||
return false;
|
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>
|
template <class T, class Alloc>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user