From a44bb9f5bf5f5834d65bd1401101cfcd85eee7b6 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 29 Dec 2019 19:58:53 +0800 Subject: [PATCH] osd/osd_types: add pg_t::compare() it's equivalent to the `<=>` operator in C++20. it helps to simplify the definition of other equality operators. Signed-off-by: Kefu Chai --- src/osd/osd_types.h | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/osd/osd_types.h b/src/osd/osd_types.h index 9fa3fd8758d36..67961c41230e9 100644 --- a/src/osd/osd_types.h +++ b/src/osd/osd_types.h @@ -464,6 +464,19 @@ struct pg_t { hobject_t get_hobj_start() const; hobject_t get_hobj_end(unsigned pg_num) const; + // strong ordering is supported + inline int compare(const pg_t& p) const noexcept { + if (auto delta = pool() - p.pool(); delta != 0) { + return delta; + } else if (ps() < p.ps()) { + return -1; + } else if (ps() > p.ps()) { + return 1; + } else { + return 0; + } + } + void encode(ceph::buffer::list& bl) const { using ceph::encode; __u8 v = 1; @@ -492,28 +505,22 @@ struct pg_t { WRITE_CLASS_ENCODER(pg_t) inline bool operator<(const pg_t& l, const pg_t& r) { - return l.pool() < r.pool() || - (l.pool() == r.pool() && (l.ps() < r.ps())); + return l.compare(r) < 0; } inline bool operator<=(const pg_t& l, const pg_t& r) { - return l.pool() < r.pool() || - (l.pool() == r.pool() && (l.ps() <= r.ps())); + return l.compare(r) <= 0; } inline bool operator==(const pg_t& l, const pg_t& r) { - return l.pool() == r.pool() && - l.ps() == r.ps(); + return l.compare(r) == 0; } inline bool operator!=(const pg_t& l, const pg_t& r) { - return l.pool() != r.pool() || - l.ps() != r.ps(); + return l.compare(r) != 0; } inline bool operator>(const pg_t& l, const pg_t& r) { - return l.pool() > r.pool() || - (l.pool() == r.pool() && (l.ps() > r.ps())); + return l.compare(r) > 0; } inline bool operator>=(const pg_t& l, const pg_t& r) { - return l.pool() > r.pool() || - (l.pool() == r.pool() && (l.ps() >= r.ps())); + return l.compare(r) >= 0; } std::ostream& operator<<(std::ostream& out, const pg_t &pg); -- 2.39.5