Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uuid: support for threeway comparison #144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/boost/uuid/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
#define BOOST_UUID_NO_SIMD
#endif

#if !defined(BOOST_UUID_THREEWAY_COMPARE)
#if __cpp_impl_three_way_comparison >= 201907L
#define BOOST_UUID_THREEWAY_COMPARE 1
#else
#define BOOST_UUID_THREEWAY_COMPARE 0
#endif
#endif

#endif // !defined(BOOST_UUID_NO_SIMD)

#endif // BOOST_UUID_DETAIL_CONFIG_HPP_INCLUDED_
18 changes: 18 additions & 0 deletions include/boost/uuid/uuid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
#pragma warning(disable : 4996) // Disable deprecated std::swap_ranges, std::equal
#endif

#if BOOST_UUID_THREEWAY_COMPARE
#include <compare>
#include <cstring>
#endif

#ifdef BOOST_NO_STDC_NAMESPACE
namespace std {
using ::size_t;
Expand Down Expand Up @@ -170,6 +175,19 @@ inline bool operator>=(uuid const& lhs, uuid const& rhs) BOOST_NOEXCEPT
return !(lhs < rhs);
}

#if BOOST_UUID_THREEWAY_COMPARE
inline std::strong_ordering operator<=>(const uuid &lhs, const uuid &rhs)
{
int compare = std::memcmp(lhs.data, rhs.data, sizeof(lhs.data));

if (compare < 0)
return std::strong_ordering::less;
if (compare == 0)
return std::strong_ordering::equal;
return std::strong_ordering::greater;
}
#endif

inline void swap(uuid& lhs, uuid& rhs) BOOST_NOEXCEPT
{
lhs.swap(rhs);
Expand Down
21 changes: 21 additions & 0 deletions test/test_uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ int main(int, char*[])
BOOST_TEST(u3 >= u3);
BOOST_TEST(u2 >= u1);
BOOST_TEST(u3 >= u1);

#if BOOST_UUID_THREEWAY_COMPARE
BOOST_TEST(u1 <=> u2 == std::strong_ordering::less);
BOOST_TEST(u2 <=> u3 == std::strong_ordering::less);
BOOST_TEST(u1 <=> u4 == std::strong_ordering::less);
BOOST_TEST(u1 <=> u5 == std::strong_ordering::less);
BOOST_TEST(u4 <=> u5 == std::strong_ordering::less);
BOOST_TEST(u4 <=> u2 == std::strong_ordering::less);
BOOST_TEST(u5 <=> u2 == std::strong_ordering::less);

BOOST_TEST(u1 <=> u1 == std::strong_ordering::equal);
BOOST_TEST(u2 <=> u2 == std::strong_ordering::equal);
BOOST_TEST(u3 <=> u3 == std::strong_ordering::equal);
BOOST_TEST(u4 <=> u4 == std::strong_ordering::equal);
BOOST_TEST(u5 <=> u5 == std::strong_ordering::equal);

BOOST_TEST(u2 <=> u1 == std::strong_ordering::greater);
BOOST_TEST(u3 <=> u1 == std::strong_ordering::greater);
BOOST_TEST(u2 <=> u1 == std::strong_ordering::greater);
BOOST_TEST(u3 <=> u1 == std::strong_ordering::greater);
#endif
}

{ // ticket 10510
Expand Down
Loading