refactor: Add uint256::operator<=>()

There is already a non-standard and internally used Compare() function,
and an standard operator<().

Also, there is already transaction_identifier::operator<=>().

It seems more consistent to remove the internal Compare() and have a
single standard C++20 <=> operator.
This commit is contained in:
MarcoFalke
2026-08-05 13:55:49 +02:00
parent fa6df14c23
commit faec059dfe
2 changed files with 3 additions and 21 deletions

View File

@@ -7,7 +7,6 @@
#include <attributes.h>
#include <uint256.h>
#include <util/types.h>
#include <compare>
#include <cstddef>
@@ -28,22 +27,12 @@ class transaction_identifier
// Note: Use FromUint256 externally instead.
transaction_identifier(const uint256& wrapped) : m_wrapped{wrapped} {}
constexpr int Compare(const transaction_identifier<has_witness>& other) const { return m_wrapped.Compare(other.m_wrapped); }
template <typename Other>
constexpr int Compare(const Other& other) const
{
static_assert(ALWAYS_FALSE<Other>, "Forbidden comparison type");
return 0;
}
public:
transaction_identifier() : m_wrapped{} {}
consteval explicit transaction_identifier(std::string_view hex_str) : m_wrapped{uint256{hex_str}} {}
template <typename Other>
bool operator==(const Other& other) const { return Compare(other) == 0; }
template <typename Other>
std::strong_ordering operator<=>(const Other& other) const { return Compare(other) <=> 0; }
constexpr bool operator==(const transaction_identifier&) const = default;
constexpr auto operator<=>(const transaction_identifier&) const = default;
const uint256& ToUint256() const LIFETIMEBOUND { return m_wrapped; }
static transaction_identifier FromUint256(const uint256& id) { return {id}; }

View File

@@ -65,14 +65,7 @@ public:
* @note Does NOT match the ordering on the corresponding \ref
* base_uint::CompareTo, which starts comparing from the end.
*/
constexpr int Compare(const base_blob& other) const {
auto cmp = m_data <=> other.m_data;
if (cmp < 0) return -1;
if (cmp > 0) return 1;
return 0;
}
friend constexpr bool operator<(const base_blob& a, const base_blob& b) { return a.Compare(b) < 0; }
constexpr std::strong_ordering operator<=>(const base_blob& other) const = default;
/** @name Hex representation
*