Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
Use spaceship op to cover relational ops for comparing two bi_t objects
Browse files Browse the repository at this point in the history
  • Loading branch information
OTheDev committed Feb 18, 2024
1 parent 6b5c9da commit 75b6d7c
Showing 2 changed files with 7 additions and 32 deletions.
11 changes: 3 additions & 8 deletions include/bi.hpp
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ SPDX-License-Identifier: Apache-2.0

#include <algorithm>
#include <cassert>
#include <compare>
#include <iostream>
#include <limits>
#include <span>
@@ -126,15 +127,9 @@ class BI_API bi_t {
bi_t& operator<<=(bi_bitcount_t shift);
bi_t& operator>>=(bi_bitcount_t shift);

// Relational operators
bool operator<(const bi_t&) const noexcept;
bool operator>(const bi_t&) const noexcept;
bool operator<=(const bi_t&) const noexcept;
bool operator>=(const bi_t&) const noexcept;

// Equality operators
// Comparisons
std::strong_ordering operator<=>(const bi_t& other) const noexcept;
bool operator==(const bi_t&) const noexcept;
bool operator!=(const bi_t&) const noexcept;

// Bitwise operators
bi_t operator~() const;
28 changes: 4 additions & 24 deletions src/bi.cpp
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ SPDX-License-Identifier: Apache-2.0
#include "bi.hpp"

#include <array>
#include <compare>
#include <iostream>
#include <version>
#if defined(__cpp_lib_format)
@@ -432,39 +433,18 @@ int bi_t::cmp(const bi_t& x, const bi_t& y) noexcept {
}

/**
* @name Relational operators
* @name Comparisons
*/
///@{

bool bi_t::operator<(const bi_t& other) const noexcept {
return cmp(*this, other) < 0;
std::strong_ordering bi_t::operator<=>(const bi_t& other) const noexcept {
return cmp(*this, other) <=> 0;
}

bool bi_t::operator>(const bi_t& other) const noexcept { return other < *this; }

bool bi_t::operator<=(const bi_t& other) const noexcept {
return !(*this > other);
}

bool bi_t::operator>=(const bi_t& other) const noexcept {
return !(*this < other);
}

///@}

/**
* @name Equality operators
*/
///@{

bool bi_t::operator==(const bi_t& other) const noexcept {
return cmp(*this, other) == 0;
}

bool bi_t::operator!=(const bi_t& other) const noexcept {
return !(*this == other);
}

///@}

/**

0 comments on commit 75b6d7c

Please sign in to comment.