Use spaceship operator in some basic data structures.

This commit is contained in:
John Preston 2021-11-10 12:02:58 +04:00
parent 963722cce1
commit edd93598a9
3 changed files with 32 additions and 269 deletions

View File

@ -35,6 +35,8 @@ struct MsgId {
constexpr MsgId operator--(int) noexcept { constexpr MsgId operator--(int) noexcept {
return bare--; return bare--;
} }
constexpr auto operator<=>(const MsgId &other) const = default;
constexpr bool operator==(const MsgId &other) const = default;
int64 bare = 0; int64 bare = 0;
}; };
@ -49,30 +51,6 @@ Q_DECLARE_METATYPE(MsgId);
return MsgId(a.bare - b.bare); return MsgId(a.bare - b.bare);
} }
[[nodiscard]] inline constexpr bool operator==(MsgId a, MsgId b) noexcept {
return (a.bare == b.bare);
}
[[nodiscard]] inline constexpr bool operator!=(MsgId a, MsgId b) noexcept {
return (a.bare != b.bare);
}
[[nodiscard]] inline constexpr bool operator<(MsgId a, MsgId b) noexcept {
return (a.bare < b.bare);
}
[[nodiscard]] inline constexpr bool operator>(MsgId a, MsgId b) noexcept {
return (a.bare > b.bare);
}
[[nodiscard]] inline constexpr bool operator<=(MsgId a, MsgId b) noexcept {
return (a.bare <= b.bare);
}
[[nodiscard]] inline constexpr bool operator>=(MsgId a, MsgId b) noexcept {
return (a.bare >= b.bare);
}
constexpr auto StartClientMsgId = MsgId(0x01 - (1LL << 58)); constexpr auto StartClientMsgId = MsgId(0x01 - (1LL << 58));
constexpr auto EndClientMsgId = MsgId(-(1LL << 57)); constexpr auto EndClientMsgId = MsgId(-(1LL << 57));
constexpr auto ServerMaxMsgId = MsgId(1LL << 56); constexpr auto ServerMaxMsgId = MsgId(1LL << 56);
@ -132,52 +110,13 @@ struct FullMsgId {
constexpr bool operator!() const noexcept { constexpr bool operator!() const noexcept {
return msg == 0; return msg == 0;
} }
constexpr auto operator<=>(const FullMsgId &other) const = default;
constexpr bool operator==(const FullMsgId &other) const = default;
ChannelId channel = NoChannel; ChannelId channel = NoChannel;
MsgId msg = 0; MsgId msg = 0;
}; };
[[nodiscard]] inline constexpr bool operator<(
const FullMsgId &a,
const FullMsgId &b) noexcept {
if (a.channel < b.channel) {
return true;
} else if (a.channel > b.channel) {
return false;
}
return a.msg < b.msg;
}
[[nodiscard]] inline constexpr bool operator>(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return b < a;
}
[[nodiscard]] inline constexpr bool operator<=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(b < a);
}
[[nodiscard]] inline constexpr bool operator>=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(a < b);
}
[[nodiscard]] inline constexpr bool operator==(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return (a.channel == b.channel) && (a.msg == b.msg);
}
[[nodiscard]] inline constexpr bool operator!=(
const FullMsgId &a,
const FullMsgId &b) noexcept {
return !(a == b);
}
Q_DECLARE_METATYPE(FullMsgId); Q_DECLARE_METATYPE(FullMsgId);
namespace std { namespace std {

View File

@ -34,103 +34,15 @@ struct ChatIdType {
[[nodiscard]] constexpr bool operator!() const noexcept { [[nodiscard]] constexpr bool operator!() const noexcept {
return !bare; return !bare;
} }
[[nodiscard]] constexpr auto operator<=>(const ChatIdType &other) const = default;
[[nodiscard]] constexpr bool operator==(const ChatIdType &other) const = default;
constexpr auto operator<=>(PeerIdZero) const = delete;
[[nodiscard]] constexpr bool operator==(PeerIdZero) const noexcept {
return (bare == 0);
}
}; };
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator==(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare == b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator!=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare != b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator<(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare < b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator>(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare > b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator<=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare <= b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator>=(
ChatIdType<Shift> a,
ChatIdType<Shift> b) noexcept {
return (a.bare >= b.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator==(
ChatIdType<Shift> a,
PeerIdZero) noexcept {
return (a.bare == 0);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator==(
PeerIdZero,
ChatIdType<Shift> a) noexcept {
return (0 == a.bare);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator!=(
ChatIdType<Shift> a,
PeerIdZero) noexcept {
return (a.bare != 0);
}
template <uchar Shift>
[[nodiscard]] inline constexpr bool operator!=(
PeerIdZero,
ChatIdType<Shift> a) noexcept {
return (0 != a.bare);
}
template <uchar Shift>
bool operator<(ChatIdType<Shift>, PeerIdZero) = delete;
template <uchar Shift>
bool operator<(PeerIdZero, ChatIdType<Shift>) = delete;
template <uchar Shift>
bool operator>(ChatIdType<Shift>, PeerIdZero) = delete;
template <uchar Shift>
bool operator>(PeerIdZero, ChatIdType<Shift>) = delete;
template <uchar Shift>
bool operator<=(ChatIdType<Shift>, PeerIdZero) = delete;
template <uchar Shift>
bool operator<=(PeerIdZero, ChatIdType<Shift>) = delete;
template <uchar Shift>
bool operator>=(ChatIdType<Shift>, PeerIdZero) = delete;
template <uchar Shift>
bool operator>=(PeerIdZero, ChatIdType<Shift>) = delete;
using UserId = ChatIdType<0>; using UserId = ChatIdType<0>;
using ChatId = ChatIdType<1>; using ChatId = ChatIdType<1>;
using ChannelId = ChatIdType<2>; using ChannelId = ChatIdType<2>;
@ -177,65 +89,15 @@ struct PeerId {
return !value; return !value;
} }
[[nodiscard]] constexpr auto operator<=>(const PeerId &other) const = default;
[[nodiscard]] constexpr bool operator==(const PeerId &other) const = default;
constexpr auto operator<=>(PeerIdZero) const = delete;
[[nodiscard]] constexpr bool operator==(PeerIdZero) const noexcept {
return (value == 0);
}
}; };
[[nodiscard]] inline constexpr bool operator==(PeerId a, PeerId b) noexcept {
return (a.value == b.value);
}
[[nodiscard]] inline constexpr bool operator!=(PeerId a, PeerId b) noexcept {
return (a.value != b.value);
}
[[nodiscard]] inline constexpr bool operator<(PeerId a, PeerId b) noexcept {
return (a.value < b.value);
}
[[nodiscard]] inline constexpr bool operator>(PeerId a, PeerId b) noexcept {
return (a.value > b.value);
}
[[nodiscard]] inline constexpr bool operator<=(PeerId a, PeerId b) noexcept {
return (a.value <= b.value);
}
[[nodiscard]] inline constexpr bool operator>=(PeerId a, PeerId b) noexcept {
return (a.value >= b.value);
}
[[nodiscard]] inline constexpr bool operator==(
PeerId a,
PeerIdZero) noexcept {
return (a.value == 0);
}
[[nodiscard]] inline constexpr bool operator==(
PeerIdZero,
PeerId a) noexcept {
return (0 == a.value);
}
[[nodiscard]] inline constexpr bool operator!=(
PeerId a,
PeerIdZero) noexcept {
return (a.value != 0);
}
[[nodiscard]] inline constexpr bool operator!=(
PeerIdZero,
PeerId a) noexcept {
return (0 != a.value);
}
bool operator<(PeerId, PeerIdZero) = delete;
bool operator<(PeerIdZero, PeerId) = delete;
bool operator>(PeerId, PeerIdZero) = delete;
bool operator>(PeerIdZero, PeerId) = delete;
bool operator<=(PeerId, PeerIdZero) = delete;
bool operator<=(PeerIdZero, PeerId) = delete;
bool operator>=(PeerId, PeerIdZero) = delete;
bool operator>=(PeerIdZero, PeerId) = delete;
[[nodiscard]] inline constexpr bool peerIsUser(PeerId id) noexcept { [[nodiscard]] inline constexpr bool peerIsUser(PeerId id) noexcept {
return id.is<UserId>(); return id.is<UserId>();
} }

View File

@ -40,29 +40,8 @@ public:
Data::Folder *folder() const; Data::Folder *folder() const;
PeerData *peer() const; PeerData *peer() const;
inline bool operator<(const Key &other) const { constexpr auto operator<=>(const Key &other) const = default;
return _value < other._value; constexpr bool operator==(const Key &other) const = default;
}
inline bool operator>(const Key &other) const {
return (other < *this);
}
inline bool operator<=(const Key &other) const {
return !(other < *this);
}
inline bool operator>=(const Key &other) const {
return !(*this < other);
}
inline bool operator==(const Key &other) const {
return _value == other._value;
}
inline bool operator!=(const Key &other) const {
return !(*this == other);
}
// Not working :(
//friend inline auto value_ordering_helper(const Key &key) {
// return key.value;
//}
private: private:
Entry *_value = nullptr; Entry *_value = nullptr;
@ -74,41 +53,24 @@ struct RowDescriptor {
RowDescriptor(Key key, FullMsgId fullId) : key(key), fullId(fullId) { RowDescriptor(Key key, FullMsgId fullId) : key(key), fullId(fullId) {
} }
constexpr auto operator<=>(const RowDescriptor &other) const noexcept {
if (const auto result = (key <=> other.key); result != 0) {
return result;
} else if (!fullId.msg && !other.fullId.msg) {
return (fullId.msg <=> other.fullId.msg);
} else {
return (fullId <=> other.fullId);
}
}
constexpr bool operator==(const RowDescriptor &other) const noexcept {
return ((*this) <=> other) == 0;
}
Key key; Key key;
FullMsgId fullId; FullMsgId fullId;
}; };
inline bool operator==(const RowDescriptor &a, const RowDescriptor &b) {
return (a.key == b.key)
&& ((a.fullId == b.fullId) || (!a.fullId.msg && !b.fullId.msg));
}
inline bool operator!=(const RowDescriptor &a, const RowDescriptor &b) {
return !(a == b);
}
inline bool operator<(const RowDescriptor &a, const RowDescriptor &b) {
if (a.key < b.key) {
return true;
} else if (a.key > b.key) {
return false;
}
return a.fullId < b.fullId;
}
inline bool operator>(const RowDescriptor &a, const RowDescriptor &b) {
return (b < a);
}
inline bool operator<=(const RowDescriptor &a, const RowDescriptor &b) {
return !(b < a);
}
inline bool operator>=(const RowDescriptor &a, const RowDescriptor &b) {
return !(a < b);
}
struct EntryState { struct EntryState {
enum class Section { enum class Section {
History, History,