| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2020 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qresultstore.h" |
| 41 | |
| 42 | QT_BEGIN_NAMESPACE |
| 43 | |
| 44 | namespace QtPrivate { |
| 45 | |
| 46 | /*! |
| 47 | \internal |
| 48 | |
| 49 | Finds result in \a store by \a index |
| 50 | */ |
| 51 | static ResultIteratorBase findResult(const QMap<int, ResultItem> &store, int index) |
| 52 | { |
| 53 | if (store.isEmpty()) |
| 54 | return ResultIteratorBase(store.end()); |
| 55 | QMap<int, ResultItem>::const_iterator it = store.lowerBound(index); |
| 56 | |
| 57 | // lowerBound returns either an iterator to the result or an iterator |
| 58 | // to the nearest greater index. If the latter happens it might be |
| 59 | // that the result is stored in a vector at the previous index. |
| 60 | if (it == store.end()) { |
| 61 | --it; |
| 62 | if (it.value().isVector() == false) { |
| 63 | return ResultIteratorBase(store.end()); |
| 64 | } |
| 65 | } else { |
| 66 | if (it.key() > index) { |
| 67 | if (it == store.begin()) |
| 68 | return ResultIteratorBase(store.end()); |
| 69 | --it; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const int vectorIndex = index - it.key(); |
| 74 | |
| 75 | if (vectorIndex >= it.value().count()) |
| 76 | return ResultIteratorBase(store.end()); |
| 77 | else if (it.value().isVector() == false && vectorIndex != 0) |
| 78 | return ResultIteratorBase(store.end()); |
| 79 | return ResultIteratorBase(it, vectorIndex); |
| 80 | } |
| 81 | |
| 82 | /*! |
| 83 | \class QtPrivate::ResultItem |
| 84 | \internal |
| 85 | */ |
| 86 | |
| 87 | /*! |
| 88 | \class QtPrivate::ResultIteratorBase |
| 89 | \internal |
| 90 | */ |
| 91 | |
| 92 | /*! |
| 93 | \class QtPrivate::ResultStoreBase |
| 94 | \internal |
| 95 | */ |
| 96 | |
| 97 | ResultIteratorBase::ResultIteratorBase() |
| 98 | : mapIterator(QMap<int, ResultItem>::const_iterator()), m_vectorIndex(0) { } |
| 99 | ResultIteratorBase::ResultIteratorBase(QMap<int, ResultItem>::const_iterator _mapIterator, int _vectorIndex) |
| 100 | : mapIterator(_mapIterator), m_vectorIndex(_vectorIndex) { } |
| 101 | |
| 102 | int ResultIteratorBase::vectorIndex() const { return m_vectorIndex; } |
| 103 | int ResultIteratorBase::resultIndex() const { return mapIterator.key() + m_vectorIndex; } |
| 104 | |
| 105 | ResultIteratorBase ResultIteratorBase::operator++() |
| 106 | { |
| 107 | if (canIncrementVectorIndex()) { |
| 108 | ++m_vectorIndex; |
| 109 | } else { |
| 110 | ++mapIterator; |
| 111 | m_vectorIndex = 0; |
| 112 | } |
| 113 | return *this; |
| 114 | } |
| 115 | |
| 116 | int ResultIteratorBase::batchSize() const |
| 117 | { |
| 118 | return mapIterator.value().count(); |
| 119 | } |
| 120 | |
| 121 | void ResultIteratorBase::batchedAdvance() |
| 122 | { |
| 123 | ++mapIterator; |
| 124 | m_vectorIndex = 0; |
| 125 | } |
| 126 | |
| 127 | bool ResultIteratorBase::operator==(const ResultIteratorBase &other) const |
| 128 | { |
| 129 | return (mapIterator == other.mapIterator && m_vectorIndex == other.m_vectorIndex); |
| 130 | } |
| 131 | |
| 132 | bool ResultIteratorBase::operator!=(const ResultIteratorBase &other) const |
| 133 | { |
| 134 | return !operator==(other); |
| 135 | } |
| 136 | |
| 137 | bool ResultIteratorBase::isVector() const |
| 138 | { |
| 139 | return mapIterator.value().isVector(); |
| 140 | } |
| 141 | |
| 142 | bool ResultIteratorBase::canIncrementVectorIndex() const |
| 143 | { |
| 144 | return (m_vectorIndex + 1 < mapIterator.value().m_count); |
| 145 | } |
| 146 | |
| 147 | bool ResultIteratorBase::isValid() const |
| 148 | { |
| 149 | return mapIterator.value().isValid(); |
| 150 | } |
| 151 | |
| 152 | ResultStoreBase::ResultStoreBase() |
| 153 | : insertIndex(0), resultCount(0), m_filterMode(false), filteredResults(0) { } |
| 154 | |
| 155 | ResultStoreBase::~ResultStoreBase() |
| 156 | { |
| 157 | // QFutureInterface's dtor must delete the contents of m_results. |
| 158 | Q_ASSERT(m_results.isEmpty()); |
| 159 | } |
| 160 | |
| 161 | void ResultStoreBase::setFilterMode(bool enable) |
| 162 | { |
| 163 | m_filterMode = enable; |
| 164 | } |
| 165 | |
| 166 | bool ResultStoreBase::filterMode() const |
| 167 | { |
| 168 | return m_filterMode; |
| 169 | } |
| 170 | |
| 171 | void ResultStoreBase::syncResultCount() |
| 172 | { |
| 173 | ResultIteratorBase it = resultAt(resultCount); |
| 174 | while (it != end()) { |
| 175 | resultCount += it.batchSize(); |
| 176 | it = resultAt(resultCount); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void ResultStoreBase::insertResultItemIfValid(int index, ResultItem &resultItem) |
| 181 | { |
| 182 | if (resultItem.isValid()) { |
| 183 | m_results[index] = resultItem; |
| 184 | syncResultCount(); |
| 185 | } else { |
| 186 | filteredResults += resultItem.count(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | int ResultStoreBase::insertResultItem(int index, ResultItem &resultItem) |
| 191 | { |
| 192 | int storeIndex; |
| 193 | if (m_filterMode && index != -1 && index > insertIndex) { |
| 194 | pendingResults[index] = resultItem; |
| 195 | storeIndex = index; |
| 196 | } else { |
| 197 | storeIndex = updateInsertIndex(index, resultItem.count()); |
| 198 | insertResultItemIfValid(storeIndex - filteredResults, resultItem); |
| 199 | } |
| 200 | syncPendingResults(); |
| 201 | return storeIndex; |
| 202 | } |
| 203 | |
| 204 | bool ResultStoreBase::containsValidResultItem(int index) const |
| 205 | { |
| 206 | // index might refer to either visible or pending result |
| 207 | const bool inPending = m_filterMode && index != -1 && index > insertIndex; |
| 208 | const auto &store = inPending ? pendingResults : m_results; |
| 209 | auto it = findResult(store, index); |
| 210 | return it != ResultIteratorBase(store.end()) && it.isValid(); |
| 211 | } |
| 212 | |
| 213 | void ResultStoreBase::syncPendingResults() |
| 214 | { |
| 215 | // check if we can insert any of the pending results: |
| 216 | QMap<int, ResultItem>::iterator it = pendingResults.begin(); |
| 217 | while (it != pendingResults.end()) { |
| 218 | int index = it.key(); |
| 219 | if (index != resultCount + filteredResults) |
| 220 | break; |
| 221 | |
| 222 | ResultItem result = it.value(); |
| 223 | insertResultItemIfValid(index - filteredResults, result); |
| 224 | pendingResults.erase(it); |
| 225 | it = pendingResults.begin(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | int ResultStoreBase::addResult(int index, const void *result) |
| 230 | { |
| 231 | ResultItem resultItem(result, 0); // 0 means "not a vector" |
| 232 | return insertResultItem(index, resultItem); |
| 233 | } |
| 234 | |
| 235 | int ResultStoreBase::addResults(int index, const void *results, int vectorSize, int totalCount) |
| 236 | { |
| 237 | if (m_filterMode == false || vectorSize == totalCount) { |
| 238 | ResultItem resultItem(results, vectorSize); |
| 239 | return insertResultItem(index, resultItem); |
| 240 | } else { |
| 241 | if (vectorSize > 0) { |
| 242 | ResultItem filteredIn(results, vectorSize); |
| 243 | insertResultItem(index, filteredIn); |
| 244 | } |
| 245 | ResultItem filteredAway(nullptr, totalCount - vectorSize); |
| 246 | return insertResultItem(index + vectorSize, filteredAway); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | ResultIteratorBase ResultStoreBase::begin() const |
| 251 | { |
| 252 | return ResultIteratorBase(m_results.begin()); |
| 253 | } |
| 254 | |
| 255 | ResultIteratorBase ResultStoreBase::end() const |
| 256 | { |
| 257 | return ResultIteratorBase(m_results.end()); |
| 258 | } |
| 259 | |
| 260 | bool ResultStoreBase::hasNextResult() const |
| 261 | { |
| 262 | return begin() != end(); |
| 263 | } |
| 264 | |
| 265 | ResultIteratorBase ResultStoreBase::resultAt(int index) const |
| 266 | { |
| 267 | return findResult(m_results, index); |
| 268 | } |
| 269 | |
| 270 | bool ResultStoreBase::contains(int index) const |
| 271 | { |
| 272 | return (resultAt(index) != end()); |
| 273 | } |
| 274 | |
| 275 | int ResultStoreBase::count() const |
| 276 | { |
| 277 | return resultCount; |
| 278 | } |
| 279 | |
| 280 | // returns the insert index, calling this function with |
| 281 | // index equal to -1 returns the next available index. |
| 282 | int ResultStoreBase::updateInsertIndex(int index, int _count) |
| 283 | { |
| 284 | if (index == -1) { |
| 285 | index = insertIndex; |
| 286 | insertIndex += _count; |
| 287 | } else { |
| 288 | insertIndex = qMax(index + _count, insertIndex); |
| 289 | } |
| 290 | return index; |
| 291 | } |
| 292 | |
| 293 | } // namespace QtPrivate |
| 294 | |
| 295 | QT_END_NAMESPACE |
| 296 | |