From 16f80386e8c8360e778051ba247b71596c018964 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 15 Nov 2019 13:55:05 +0900 Subject: [PATCH] added qhash functions for timerange and rational So these keys can be used in QHash containers, they need functions for how to create those hashes. --- app/common/rational.cpp | 5 +++++ app/common/rational.h | 2 ++ app/common/timerange.cpp | 14 +++++++------- app/common/timerange.h | 5 +++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/common/rational.cpp b/app/common/rational.cpp index 11e4c3acc..059b3057b 100644 --- a/app/common/rational.cpp +++ b/app/common/rational.cpp @@ -421,3 +421,8 @@ QDebug operator<<(QDebug debug, const rational &r) debug.nospace() << r.numerator() << "/" << r.denominator(); return debug.space(); } + +uint qHash(const rational &r, uint seed) +{ + return qHash(r.toDouble(), seed); +} diff --git a/app/common/rational.h b/app/common/rational.h index 7bd5408b8..519b8fcd1 100644 --- a/app/common/rational.h +++ b/app/common/rational.h @@ -126,4 +126,6 @@ QDebug operator<<(QDebug debug, const rational& r); Q_DECLARE_METATYPE(rational) +uint qHash(const rational& r, uint seed); + #endif // RATIONAL_H diff --git a/app/common/timerange.cpp b/app/common/timerange.cpp index dabcecfac..26888c05d 100644 --- a/app/common/timerange.cpp +++ b/app/common/timerange.cpp @@ -41,14 +41,9 @@ void TimeRange::set_range(const rational &in, const rational &out) normalize(); } -bool TimeRange::operator<(const TimeRange &r) const +bool TimeRange::operator==(const TimeRange &r) const { - return (in() + out()) < (r.in() + r.out()); -} - -bool TimeRange::operator>(const TimeRange &r) const -{ - return (in() + out()) > (r.in() + r.out()); + return in() == r.in() && out() == r.out(); } void TimeRange::normalize() @@ -63,3 +58,8 @@ void TimeRange::normalize() // Calculate length length_ = out_ - in_; } + +uint qHash(const TimeRange &r, uint seed) +{ + return qHash(r.in(), seed) ^ qHash(r.out(), seed); +} diff --git a/app/common/timerange.h b/app/common/timerange.h index fe7f18460..26658c25c 100644 --- a/app/common/timerange.h +++ b/app/common/timerange.h @@ -16,8 +16,7 @@ public: void set_out(const rational& out); void set_range(const rational& in, const rational& out); - bool operator<(const TimeRange &r) const; - bool operator>(const TimeRange &r) const; + bool operator==(const TimeRange& r) const; private: void normalize(); @@ -27,4 +26,6 @@ private: rational length_; }; +uint qHash(const TimeRange& r, uint seed); + #endif // TIMERANGE_H