| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtConcurrent 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 | #ifndef QTCONCURRENT_MAP_H |
| 41 | #define QTCONCURRENT_MAP_H |
| 42 | |
| 43 | #include <QtConcurrent/qtconcurrent_global.h> |
| 44 | |
| 45 | #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) |
| 46 | |
| 47 | #include <QtConcurrent/qtconcurrentmapkernel.h> |
| 48 | #include <QtConcurrent/qtconcurrentreducekernel.h> |
| 49 | #include <QtConcurrent/qtconcurrentfunctionwrappers.h> |
| 50 | #include <QtCore/qstringlist.h> |
| 51 | |
| 52 | QT_BEGIN_NAMESPACE |
| 53 | |
| 54 | |
| 55 | |
| 56 | namespace QtConcurrent { |
| 57 | |
| 58 | // map() on sequences |
| 59 | template <typename Sequence, typename MapFunctor> |
| 60 | QFuture<void> map(Sequence &sequence, MapFunctor map) |
| 61 | { |
| 62 | return startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map)); |
| 63 | } |
| 64 | |
| 65 | // map() on iterators |
| 66 | template <typename Iterator, typename MapFunctor> |
| 67 | QFuture<void> map(Iterator begin, Iterator end, MapFunctor map) |
| 68 | { |
| 69 | return startMap(begin, end, QtPrivate::createFunctionWrapper(map)); |
| 70 | } |
| 71 | |
| 72 | // mappedReduced() for sequences. |
| 73 | template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> |
| 74 | QFuture<ResultType> mappedReduced(const Sequence &sequence, |
| 75 | MapFunctor map, |
| 76 | ReduceFunctor reduce, |
| 77 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 78 | { |
| 79 | return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType> |
| 80 | (sequence, |
| 81 | QtPrivate::createFunctionWrapper(map), |
| 82 | QtPrivate::createFunctionWrapper(reduce), |
| 83 | options); |
| 84 | } |
| 85 | |
| 86 | template <typename Sequence, typename MapFunctor, typename ReduceFunctor> |
| 87 | QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(const Sequence &sequence, |
| 88 | MapFunctor map, |
| 89 | ReduceFunctor reduce, |
| 90 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 91 | { |
| 92 | return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> |
| 93 | (sequence, |
| 94 | QtPrivate::createFunctionWrapper(map), |
| 95 | QtPrivate::createFunctionWrapper(reduce), |
| 96 | options); |
| 97 | } |
| 98 | |
| 99 | // mappedReduced() for iterators |
| 100 | template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor> |
| 101 | QFuture<ResultType> mappedReduced(Iterator begin, |
| 102 | Iterator end, |
| 103 | MapFunctor map, |
| 104 | ReduceFunctor reduce, |
| 105 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 106 | { |
| 107 | return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType> |
| 108 | (begin, end, |
| 109 | QtPrivate::createFunctionWrapper(map), |
| 110 | QtPrivate::createFunctionWrapper(reduce), |
| 111 | options); |
| 112 | } |
| 113 | |
| 114 | template <typename Iterator, typename MapFunctor, typename ReduceFunctor> |
| 115 | QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> mappedReduced(Iterator begin, |
| 116 | Iterator end, |
| 117 | MapFunctor map, |
| 118 | ReduceFunctor reduce, |
| 119 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 120 | { |
| 121 | return startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> |
| 122 | (begin, end, |
| 123 | QtPrivate::createFunctionWrapper(map), |
| 124 | QtPrivate::createFunctionWrapper(reduce), |
| 125 | options); |
| 126 | } |
| 127 | |
| 128 | // mapped() for sequences |
| 129 | template <typename Sequence, typename MapFunctor> |
| 130 | QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(const Sequence &sequence, MapFunctor map) |
| 131 | { |
| 132 | return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(sequence, QtPrivate::createFunctionWrapper(map)); |
| 133 | } |
| 134 | |
| 135 | // mapped() for iterator ranges. |
| 136 | template <typename Iterator, typename MapFunctor> |
| 137 | QFuture<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType> mapped(Iterator begin, Iterator end, MapFunctor map) |
| 138 | { |
| 139 | return startMapped<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType>(begin, end, QtPrivate::createFunctionWrapper(map)); |
| 140 | } |
| 141 | |
| 142 | // blockingMap() for sequences |
| 143 | template <typename Sequence, typename MapFunctor> |
| 144 | void blockingMap(Sequence &sequence, MapFunctor map) |
| 145 | { |
| 146 | QFuture<void> future = startMap(sequence.begin(), sequence.end(), QtPrivate::createFunctionWrapper(map)); |
| 147 | future.waitForFinished(); |
| 148 | } |
| 149 | |
| 150 | // blockingMap() for iterator ranges |
| 151 | template <typename Iterator, typename MapFunctor> |
| 152 | void blockingMap(Iterator begin, Iterator end, MapFunctor map) |
| 153 | { |
| 154 | QFuture<void> future = startMap(begin, end, QtPrivate::createFunctionWrapper(map)); |
| 155 | future.waitForFinished(); |
| 156 | } |
| 157 | |
| 158 | // blockingMappedReduced() for sequences |
| 159 | template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> |
| 160 | ResultType blockingMappedReduced(const Sequence &sequence, |
| 161 | MapFunctor map, |
| 162 | ReduceFunctor reduce, |
| 163 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 164 | { |
| 165 | QFuture<ResultType> future = |
| 166 | QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType> |
| 167 | (sequence, |
| 168 | QtPrivate::createFunctionWrapper(map), |
| 169 | QtPrivate::createFunctionWrapper(reduce), |
| 170 | options); |
| 171 | return future.result(); |
| 172 | } |
| 173 | |
| 174 | template <typename MapFunctor, typename ReduceFunctor, typename Sequence> |
| 175 | typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(const Sequence &sequence, |
| 176 | MapFunctor map, |
| 177 | ReduceFunctor reduce, |
| 178 | ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) |
| 179 | { |
| 180 | QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> future = |
| 181 | QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> |
| 182 | (sequence, |
| 183 | QtPrivate::createFunctionWrapper(map), |
| 184 | QtPrivate::createFunctionWrapper(reduce), |
| 185 | options); |
| 186 | return future.result(); |
| 187 | } |
| 188 | |
| 189 | // blockingMappedReduced() for iterator ranges |
| 190 | template <typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor> |
| 191 | ResultType blockingMappedReduced(Iterator begin, |
| 192 | Iterator end, |
| 193 | MapFunctor map, |
| 194 | ReduceFunctor reduce, |
| 195 | QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce)) |
| 196 | { |
| 197 | QFuture<ResultType> future = |
| 198 | QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, ResultType> |
| 199 | (begin, end, |
| 200 | QtPrivate::createFunctionWrapper(map), |
| 201 | QtPrivate::createFunctionWrapper(reduce), |
| 202 | options); |
| 203 | return future.result(); |
| 204 | } |
| 205 | |
| 206 | template <typename Iterator, typename MapFunctor, typename ReduceFunctor> |
| 207 | typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingMappedReduced(Iterator begin, |
| 208 | Iterator end, |
| 209 | MapFunctor map, |
| 210 | ReduceFunctor reduce, |
| 211 | QtConcurrent::ReduceOptions options = QtConcurrent::ReduceOptions(QtConcurrent::UnorderedReduce | QtConcurrent::SequentialReduce)) |
| 212 | { |
| 213 | QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> future = |
| 214 | QtConcurrent::startMappedReduced<typename QtPrivate::MapResultType<void, MapFunctor>::ResultType, typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> |
| 215 | (begin, end, |
| 216 | QtPrivate::createFunctionWrapper(map), |
| 217 | QtPrivate::createFunctionWrapper(reduce), |
| 218 | options); |
| 219 | return future.result(); |
| 220 | } |
| 221 | |
| 222 | // mapped() for sequences with a different putput sequence type. |
| 223 | template <typename OutputSequence, typename InputSequence, typename MapFunctor> |
| 224 | OutputSequence blockingMapped(const InputSequence &sequence, MapFunctor map) |
| 225 | { |
| 226 | return blockingMappedReduced<OutputSequence> |
| 227 | (sequence, |
| 228 | QtPrivate::createFunctionWrapper(map), |
| 229 | QtPrivate::PushBackWrapper(), |
| 230 | QtConcurrent::OrderedReduce); |
| 231 | } |
| 232 | |
| 233 | template <typename MapFunctor, typename InputSequence> |
| 234 | typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType blockingMapped(const InputSequence &sequence, MapFunctor map) |
| 235 | { |
| 236 | typedef typename QtPrivate::MapResultType<InputSequence, MapFunctor>::ResultType OutputSequence; |
| 237 | return blockingMappedReduced<OutputSequence> |
| 238 | (sequence, |
| 239 | QtPrivate::createFunctionWrapper(map), |
| 240 | QtPrivate::PushBackWrapper(), |
| 241 | QtConcurrent::OrderedReduce); |
| 242 | } |
| 243 | |
| 244 | // mapped() for iterator ranges |
| 245 | template <typename Sequence, typename Iterator, typename MapFunctor> |
| 246 | Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map) |
| 247 | { |
| 248 | return blockingMappedReduced<Sequence> |
| 249 | (begin, end, |
| 250 | QtPrivate::createFunctionWrapper(map), |
| 251 | QtPrivate::PushBackWrapper(), |
| 252 | QtConcurrent::OrderedReduce); |
| 253 | } |
| 254 | |
| 255 | template <typename Iterator, typename MapFunctor> |
| 256 | typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType blockingMapped(Iterator begin, Iterator end, MapFunctor map) |
| 257 | { |
| 258 | typedef typename QtPrivate::MapResultType<Iterator, MapFunctor>::ResultType OutputSequence; |
| 259 | return blockingMappedReduced<OutputSequence> |
| 260 | (begin, end, |
| 261 | QtPrivate::createFunctionWrapper(map), |
| 262 | QtPrivate::PushBackWrapper(), |
| 263 | QtConcurrent::OrderedReduce); |
| 264 | } |
| 265 | |
| 266 | } // namespace QtConcurrent |
| 267 | |
| 268 | |
| 269 | QT_END_NAMESPACE |
| 270 | |
| 271 | #endif // QT_NO_CONCURRENT |
| 272 | |
| 273 | #endif |
| 274 | |