From a77987e600e7a8a4d0c3aa7242abdda0884e6e5d Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 13 Aug 2024 16:30:01 +0200 Subject: [PATCH] QModelIndex: do not use compareThreeWayMulti() and asTuple() There are doubts that the implementation would result in a huge template bloat. Also, compareThreeWayMulti() seems to be inefficient, because it copies the tail over and over again. Rewrite the comparison helper functions to get rid of the compareThreeWayMulti() calls. This also allows us to drop the asTuple() method. Amends ece36a7394594ede3fba6744fb8f5b450a477652. Found in 6.8 API review. Change-Id: I8be4e5f56c350039acde78c2e591e29773f3472c Reviewed-by: Thiago Macieira (cherry picked from commit 564f2cbbddbca3f118198f0fb4ca0a97b95b8a59) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/itemmodels/qabstractitemmodel.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index 418fc6b864f..5444bdc8f1f 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -11,8 +11,6 @@ #include #include -#include - QT_REQUIRE_CONFIG(itemmodel); QT_BEGIN_NAMESPACE @@ -145,11 +143,22 @@ public: constexpr inline bool isValid() const noexcept { return (r >= 0) && (c >= 0) && (m != nullptr); } private: - constexpr auto asTuple() const noexcept { return std::tie(r, c, i, m); } friend constexpr bool comparesEqual(const QModelIndex &lhs, const QModelIndex &rhs) noexcept - { return lhs.asTuple() == rhs.asTuple(); } + { + return lhs.r == rhs.r && lhs.c == rhs.c && lhs.i == rhs.i && lhs.m == rhs.m; + } friend constexpr Qt::strong_ordering compareThreeWay(const QModelIndex &lhs, const QModelIndex &rhs) noexcept - { return QtOrderingPrivate::compareThreeWayMulti(lhs.asTuple(), rhs.asTuple()); } + { + if (auto val = Qt::compareThreeWay(lhs.r, rhs.r); !is_eq(val)) + return val; + if (auto val = Qt::compareThreeWay(lhs.c, rhs.c); !is_eq(val)) + return val; + if (auto val = Qt::compareThreeWay(lhs.i, rhs.i); !is_eq(val)) + return val; + if (auto val = Qt::compareThreeWay(lhs.m, rhs.m); !is_eq(val)) + return val; + return Qt::strong_ordering::equivalent; + } Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QModelIndex) private: inline QModelIndex(int arow, int acolumn, const void *ptr, const QAbstractItemModel *amodel) noexcept