From 2bbbd47929793b6c38c178e96e3014f6c33f9b85 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Fri, 9 Oct 2020 10:59:52 +0200 Subject: [PATCH] Make result finding procedure in ResultStore a free-standing function Moved the logic of finding a result in ResultStore to separate function and parameterized it with QMap<...>. This is a pre-step to make find procedure uniform regardless of the storage we are looking in (either visible or pending as of now) Change-Id: I41641d70751925f223e992f52fbc7814085c452d Reviewed-by: Sona Kurazyan --- src/corelib/thread/qresultstore.cpp | 66 +++++++++++++++++------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/corelib/thread/qresultstore.cpp b/src/corelib/thread/qresultstore.cpp index 0b82b938e1..b6af27dfa9 100644 --- a/src/corelib/thread/qresultstore.cpp +++ b/src/corelib/thread/qresultstore.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -43,6 +43,42 @@ QT_BEGIN_NAMESPACE namespace QtPrivate { +/*! + \internal + + Finds result in \a store by \a index + */ +static ResultIteratorBase findResult(const QMap &store, int index) +{ + if (store.isEmpty()) + return ResultIteratorBase(store.end()); + QMap::const_iterator it = store.lowerBound(index); + + // lowerBound returns either an iterator to the result or an iterator + // to the nearest greater index. If the latter happens it might be + // that the result is stored in a vector at the previous index. + if (it == store.end()) { + --it; + if (it.value().isVector() == false) { + return ResultIteratorBase(store.end()); + } + } else { + if (it.key() > index) { + if (it == store.begin()) + return ResultIteratorBase(store.end()); + --it; + } + } + + const int vectorIndex = index - it.key(); + + if (vectorIndex >= it.value().count()) + return ResultIteratorBase(store.end()); + else if (it.value().isVector() == false && vectorIndex != 0) + return ResultIteratorBase(store.end()); + return ResultIteratorBase(it, vectorIndex); +} + /*! \class QtPrivate::ResultItem \internal @@ -214,33 +250,7 @@ bool ResultStoreBase::hasNextResult() const ResultIteratorBase ResultStoreBase::resultAt(int index) const { - if (m_results.isEmpty()) - return ResultIteratorBase(m_results.end()); - QMap::const_iterator it = m_results.lowerBound(index); - - // lowerBound returns either an iterator to the result or an iterator - // to the nearest greater index. If the latter happens it might be - // that the result is stored in a vector at the previous index. - if (it == m_results.end()) { - --it; - if (it.value().isVector() == false) { - return ResultIteratorBase(m_results.end()); - } - } else { - if (it.key() > index) { - if (it == m_results.begin()) - return ResultIteratorBase(m_results.end()); - --it; - } - } - - const int vectorIndex = index - it.key(); - - if (vectorIndex >= it.value().count()) - return ResultIteratorBase(m_results.end()); - else if (it.value().isVector() == false && vectorIndex != 0) - return ResultIteratorBase(m_results.end()); - return ResultIteratorBase(it, vectorIndex); + return findResult(m_results, index); } bool ResultStoreBase::contains(int index) const