mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-09-12 05:32:22 +02:00
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:
@@ -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}; }
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user