Use <=> operator for base_uint, Issue, and Book: (#4411)

- Implement the `operator==` and the `operator<=>` (aka the spaceship
  operator) in `base_uint`, `Issue`, and `Book`. 
- C++20-compliant compilers automatically provide the remaining
  comparison operators (e.g. `operator<`, `operator<=`, ...).
- Remove the function compare() because it is no longer needed.
- Maintain the same semantics as the existing code.
- Add some unit tests to gain further confidence.
- Fix #2525.
This commit is contained in:
drlongle
2023-03-15 04:54:54 +01:00
committed by GitHub
parent f7b3ddd87b
commit 84cde3ce0b
7 changed files with 138 additions and 216 deletions

View File

@@ -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);
}
} // namespace ripple