-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Use <=> operator for base_uint, Issue, and Book: #4411
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
#include <ripple/beast/utility/Zero.h> | ||
#include <boost/endian/conversion.hpp> | ||
#include <boost/functional/hash.hpp> | ||
#include <algorithm> | ||
#include <array> | ||
#include <cstring> | ||
#include <functional> | ||
|
@@ -549,103 +550,66 @@ using uint160 = base_uint<160>; | |
using uint256 = base_uint<256>; | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline int | ||
compare(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
[[nodiscard]] inline constexpr std::strong_ordering | ||
operator<=>(base_uint<Bits, Tag> const& lhs, base_uint<Bits, Tag> const& rhs) | ||
{ | ||
auto ret = std::mismatch(a.cbegin(), a.cend(), b.cbegin()); | ||
|
||
if (ret.first == a.cend()) | ||
return 0; | ||
// This comparison might seem wrong on a casual inspection because it | ||
// compares data internally stored as std::uint32_t byte-by-byte. But | ||
// note that the underlying data is stored in big endian, even if the | ||
// plaform is little endian. This makes the comparison correct. | ||
// | ||
// FIXME: use std::lexicographical_compare_three_way once support is | ||
// added to MacOS. | ||
Comment on lines
+556
to
+562
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice comment! |
||
|
||
// a > b | ||
if (*ret.first > *ret.second) | ||
return 1; | ||
auto const ret = std::mismatch(lhs.cbegin(), lhs.cend(), rhs.cbegin()); | ||
|
||
// a < b | ||
return -1; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator<(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return compare(a, b) < 0; | ||
} | ||
// a == b | ||
if (ret.first == lhs.cend()) | ||
return std::strong_ordering::equivalent; | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator<=(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return compare(a, b) <= 0; | ||
return (*ret.first > *ret.second) ? std::strong_ordering::greater | ||
: std::strong_ordering::less; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator>(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
template <std::size_t Bits, typename Tag> | ||
[[nodiscard]] inline constexpr bool | ||
operator==(base_uint<Bits, Tag> const& lhs, base_uint<Bits, Tag> const& rhs) | ||
{ | ||
return compare(a, b) > 0; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator>=(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return compare(a, b) >= 0; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator==(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return compare(a, b) == 0; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator!=(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return compare(a, b) != 0; | ||
return (lhs <=> rhs) == 0; | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
inline constexpr bool | ||
operator==(base_uint<Bits, Tag> const& a, std::uint64_t b) | ||
{ | ||
return a == base_uint<Bits, Tag>(b); | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline bool | ||
operator!=(base_uint<Bits, Tag> const& a, std::uint64_t b) | ||
{ | ||
return !(a == b); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
template <std::size_t Bits, class Tag> | ||
inline const base_uint<Bits, Tag> | ||
inline constexpr base_uint<Bits, Tag> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like all the |
||
operator^(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return base_uint<Bits, Tag>(a) ^= b; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline const base_uint<Bits, Tag> | ||
inline constexpr base_uint<Bits, Tag> | ||
operator&(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return base_uint<Bits, Tag>(a) &= b; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline const base_uint<Bits, Tag> | ||
inline constexpr base_uint<Bits, Tag> | ||
operator|(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return base_uint<Bits, Tag>(a) |= b; | ||
} | ||
|
||
template <std::size_t Bits, class Tag> | ||
inline const base_uint<Bits, Tag> | ||
inline constexpr base_uint<Bits, Tag> | ||
operator+(base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b) | ||
{ | ||
return base_uint<Bits, Tag>(a) += b; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,58 +47,4 @@ reversed(Book const& book) | |
return Book(book.out, book.in); | ||
} | ||
|
||
/** Ordered comparison. */ | ||
int | ||
compare(Book const& lhs, Book const& rhs) | ||
{ | ||
int const diff(compare(lhs.in, rhs.in)); | ||
if (diff != 0) | ||
return diff; | ||
return compare(lhs.out, rhs.out); | ||
} | ||
|
||
/** Equality comparison. */ | ||
/** @{ */ | ||
bool | ||
operator==(Book const& lhs, Book const& rhs) | ||
{ | ||
return (lhs.in == rhs.in) && (lhs.out == rhs.out); | ||
} | ||
|
||
bool | ||
operator!=(Book const& lhs, Book const& rhs) | ||
{ | ||
return (lhs.in != rhs.in) || (lhs.out != rhs.out); | ||
} | ||
/** @} */ | ||
|
||
/** Strict weak ordering. */ | ||
/** @{ */ | ||
bool | ||
operator<(Book const& lhs, Book const& rhs) | ||
{ | ||
int const diff(compare(lhs.in, rhs.in)); | ||
if (diff != 0) | ||
return diff < 0; | ||
return lhs.out < rhs.out; | ||
} | ||
|
||
bool | ||
operator>(Book const& lhs, Book const& rhs) | ||
{ | ||
return rhs < lhs; | ||
} | ||
|
||
bool | ||
operator>=(Book const& lhs, Book const& rhs) | ||
{ | ||
return !(lhs < rhs); | ||
} | ||
|
||
bool | ||
operator<=(Book const& lhs, Book const& rhs) | ||
{ | ||
return !(rhs < lhs); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at all this removed code. Nice work! |
||
} // namespace ripple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the
[[nodiscard]]
andconstexpr
. Nice!