From 0db64ca53b097202c49c803d8ba7e2aca517c5bb Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Mon, 24 Jun 2024 15:18:27 +0200 Subject: [PATCH] QJniArray: add arithmetic operators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Required for making the iterator random access. Based on review comments. Task-number: QTBUG-126150 Change-Id: I80ee8ed584747759acb17ee956551caba4d5bdaa Reviewed-by: Soheil Armin Reviewed-by: Tor Arne Vestbø (cherry picked from commit c4e406e3792405cfc0b8cc97a29f136c0fd44a2e) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qjniarray.h | 35 +++++++++++++++++-- .../kernel/qjniarray/tst_qjniarray.cpp | 17 +++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qjniarray.h b/src/corelib/kernel/qjniarray.h index 998465273e1..7f958dc28d4 100644 --- a/src/corelib/kernel/qjniarray.h +++ b/src/corelib/kernel/qjniarray.h @@ -48,23 +48,54 @@ struct QJniArrayIterator ++that.m_index; return that; } - friend QJniArrayIterator operator++(QJniArrayIterator &that, int) noexcept + friend QJniArrayIterator operator++(QJniArrayIterator &that, difference_type) noexcept { auto copy = that; ++that; return copy; } + friend QJniArrayIterator operator+(const QJniArrayIterator &that, difference_type n) noexcept + { + return {that.m_index + n, that.m_array}; + } + friend QJniArrayIterator operator+(difference_type n, const QJniArrayIterator &that) noexcept + { + return that + n; + } + friend QJniArrayIterator &operator+=(QJniArrayIterator &that, difference_type n) noexcept + { + that.m_index += n; + return that; + } friend QJniArrayIterator &operator--(QJniArrayIterator &that) noexcept { --that.m_index; return that; } - friend QJniArrayIterator operator--(QJniArrayIterator &that, int) noexcept + friend QJniArrayIterator operator--(QJniArrayIterator &that, difference_type) noexcept { auto copy = that; --that; return copy; } + friend QJniArrayIterator operator-(const QJniArrayIterator &that, difference_type n) noexcept + { + return {that.m_index - n, that.m_array}; + } + friend QJniArrayIterator operator-(difference_type n, const QJniArrayIterator &that) noexcept + { + return {n - that.m_index, that.m_array}; + } + friend QJniArrayIterator &operator-=(QJniArrayIterator &that, difference_type n) noexcept + { + that.m_index -= n; + return that; + } + friend difference_type operator-(const QJniArrayIterator &lhs, const QJniArrayIterator &rhs) + { + Q_ASSERT(lhs.m_array == rhs.m_array); + return lhs.m_index - rhs.m_index; + } void swap(QJniArrayIterator &other) noexcept { std::swap(m_index, other.m_index); diff --git a/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp index 4b1a7ba6e68..37b632b6349 100644 --- a/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp +++ b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp @@ -234,6 +234,23 @@ void tst_QJniArray::operators() it++; QCOMPARE(*it, 'e'); QCOMPARE(++it, array.end()); + + it -= array.size(); + QCOMPARE(it, array.begin()); + it += 2; + QCOMPARE(*it, 'c'); + + const auto it2 = it + 2; + QCOMPARE(*it2, 'e'); + QCOMPARE(it2 - it, 2); + + it = it2 - 2; + QCOMPARE(*it, 'c'); + + it = 1 + it; + QCOMPARE(*it, 'd'); + it = array.size() - it; + QCOMPARE(*it, 'c'); } { auto it = array.rbegin();