diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2020-10-09 10:59:52 +0200 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2020-10-09 16:26:05 +0200 |
commit | 2bbbd47929793b6c38c178e96e3014f6c33f9b85 (patch) | |
tree | 69a5c7b9ad1a2be5da016026ecbd610e668e22b0 /src/corelib/thread | |
parent | c22aabe9bc10b8123682a9ed27769a41e862b5e4 (diff) |
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 <sona.kurazyan@qt.io>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qresultstore.cpp | 66 |
1 files 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. @@ -44,6 +44,42 @@ QT_BEGIN_NAMESPACE namespace QtPrivate { /*! + \internal + + Finds result in \a store by \a index + */ +static ResultIteratorBase findResult(const QMap<int, ResultItem> &store, int index) +{ + if (store.isEmpty()) + return ResultIteratorBase(store.end()); + QMap<int, ResultItem>::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<int, ResultItem>::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 |