Skip to content

Commit

Permalink
Three way comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Jan 2, 2024
1 parent 6312478 commit f4c595c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/momo/MapUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ namespace internal
return !pvIsEqual(pair1, ref2);
}

//? <, >, <=, >=
#ifdef MOMO_HAS_THREE_WAY_COMPARISON
template<typename Pair2>
friend auto operator<=>(const MapReferenceStd& ref1, const Pair2& pair2)
requires requires { std::tie(ref1.first, ref1.second) <=> std::tie(pair2.first, pair2.second); }
{
return std::tie(ref1.first, ref1.second) <=> std::tie(pair2.first, pair2.second);
}
#endif

protected:
explicit MapReferenceStd(MapReference mapRef) noexcept
Expand Down
5 changes: 5 additions & 0 deletions include/momo/UserSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@
#endif
#endif

#if defined(__cpp_impl_three_way_comparison) && defined(__cpp_lib_three_way_comparison) \
&& defined (__cpp_concepts)
#define MOMO_HAS_THREE_WAY_COMPARISON
#endif

#ifdef __has_cpp_attribute
#if __has_cpp_attribute(nodiscard)
#define MOMO_NODISCARD [[nodiscard]]
Expand Down
4 changes: 4 additions & 0 deletions include/momo/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#include <tuple>
#include <initializer_list>

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
#include <compare>
#endif

#define MOMO_FRIEND_SWAP(Object) \
friend void swap(Object& object1, Object& object2) \
noexcept(noexcept(object1.Swap(object2))) \
Expand Down
9 changes: 9 additions & 0 deletions include/momo/stdish/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,14 @@ namespace internal
return !(*this == right);
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const map_base& right) const
requires requires (const_reference ref) { ref <=> ref; }
{
return std::lexicographical_compare_three_way(begin(), end(),
right.begin(), right.end());
}
#else
bool operator<(const map_base& right) const
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
Expand All @@ -679,6 +687,7 @@ namespace internal
{
return right <= *this;
}
#endif

protected: //?
void ptAssign(std::initializer_list<value_type> values)
Expand Down
11 changes: 11 additions & 0 deletions include/momo/stdish/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,16 @@ class set
return !(*this == right);
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const set& right) const
requires requires (const_reference ref) { std::tie(ref) <=> std::tie(ref); }
{
auto comp = [] (const value_type& value1, const value_type& value2)
{ return std::tie(value1) <=> std::tie(value2); };
return std::lexicographical_compare_three_way(begin(), end(),
right.begin(), right.end(), comp);
}
#else
bool operator<(const set& right) const
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
Expand All @@ -573,6 +583,7 @@ class set
{
return right <= *this;
}
#endif

private:
static TreeSet pvCreateSet(set&& right, const allocator_type& alloc)
Expand Down
11 changes: 11 additions & 0 deletions include/momo/stdish/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@ class vector
return !(*this == right);
}

#ifdef MOMO_HAS_THREE_WAY_COMPARISON
auto operator<=>(const vector& right) const
requires requires (const_reference ref) { std::tie(ref) <=> std::tie(ref); }
{
auto comp = [] (const value_type& value1, const value_type& value2)
{ return std::tie(value1) <=> std::tie(value2); };
return std::lexicographical_compare_three_way(begin(), end(),
right.begin(), right.end(), comp);
}
#else
bool operator<(const vector& right) const
{
return std::lexicographical_compare(begin(), end(), right.begin(), right.end());
Expand All @@ -482,6 +492,7 @@ class vector
{
return right <= *this;
}
#endif

private:
static Array pvCreateArray(vector&& right, const allocator_type& alloc)
Expand Down

0 comments on commit f4c595c

Please sign in to comment.