42 #ifndef QTCONCURRENT_MAPKERNEL_H 43 #define QTCONCURRENT_MAPKERNEL_H 45 #include <QtCore/qglobal.h> 47 #ifndef QT_NO_CONCURRENT 49 #include <QtCore/qtconcurrentiteratekernel.h> 50 #include <QtCore/qtconcurrentreducekernel.h> 61 template <
typename Iterator,
typename MapFunctor>
62 class MapKernel :
public IterateKernel<Iterator, void>
66 typedef void ReturnType;
67 MapKernel(Iterator begin, Iterator
end, MapFunctor _map)
68 : IterateKernel<Iterator, void>(begin, end), map(_map)
71 bool runIteration(Iterator
it,
int,
void *)
77 bool runIterations(Iterator sequenceBeginIterator,
int beginIndex,
int endIndex,
void *)
79 Iterator
it = sequenceBeginIterator;
80 advance(it, beginIndex);
81 for (
int i = beginIndex; i < endIndex; ++i) {
82 runIteration(it, i, 0);
90 template <
typename ReducedResultType,
93 typename ReduceFunctor,
94 typename Reducer = ReduceKernel<ReduceFunctor,
96 typename MapFunctor::result_type> >
97 class MappedReducedKernel :
public IterateKernel<Iterator, ReducedResultType>
99 ReducedResultType reducedResult;
101 ReduceFunctor reduce;
104 typedef ReducedResultType ReturnType;
105 MappedReducedKernel(Iterator begin, Iterator
end, MapFunctor _map, ReduceFunctor _reduce, ReduceOptions reduceOptions)
106 : IterateKernel<Iterator, ReducedResultType>(begin, end), reducedResult(), map(_map), reduce(_reduce), reducer(reduceOptions)
109 MappedReducedKernel(ReducedResultType initialValue,
111 ReduceFunctor _reduce)
112 : reducedResult(initialValue), map(_map), reduce(_reduce)
115 bool runIteration(Iterator
it,
int index, ReducedResultType *)
117 IntermediateResults<typename MapFunctor::result_type> results;
118 results.begin =
index;
119 results.end = index + 1;
121 results.vector.append(
map(*it));
122 reducer.runReduce(reduce, reducedResult, results);
126 bool runIterations(Iterator sequenceBeginIterator,
int begin,
int end, ReducedResultType *)
128 IntermediateResults<typename MapFunctor::result_type> results;
129 results.begin = begin;
131 results.vector.reserve(end - begin);
133 Iterator it = sequenceBeginIterator;
135 for (
int i = begin; i <
end; ++i) {
136 results.vector.append(
map(*(it)));
140 reducer.runReduce(reduce, reducedResult, results);
146 reducer.finish(reduce, reducedResult);
149 bool shouldThrottleThread()
151 return IterateKernel<Iterator, ReducedResultType>::shouldThrottleThread() || reducer.shouldThrottle();
154 bool shouldStartThread()
156 return IterateKernel<Iterator, ReducedResultType>::shouldStartThread() && reducer.shouldStartThread();
159 typedef ReducedResultType ResultType;
160 ReducedResultType *result()
162 return &reducedResult;
166 template <
typename Iterator,
typename MapFunctor>
167 class MappedEachKernel :
public IterateKernel<Iterator, typename MapFunctor::result_type>
170 typedef typename MapFunctor::result_type T;
172 typedef T ReturnType;
173 typedef T ResultType;
175 MappedEachKernel(Iterator begin, Iterator end, MapFunctor _map)
176 : IterateKernel<Iterator, T>(begin, end), map(_map) { }
178 bool runIteration(Iterator it,
int, T *result)
184 bool runIterations(Iterator sequenceBeginIterator,
int begin,
int end, T *results)
187 Iterator it = sequenceBeginIterator;
189 for (
int i = begin; i <
end; ++i) {
190 runIteration(it, i, results + (i - begin));
198 template <
typename Iterator,
typename Functor>
199 inline ThreadEngineStarter<void> startMap(Iterator begin, Iterator end, Functor
functor)
201 return startThreadEngine(
new MapKernel<Iterator, Functor>(begin, end, functor));
204 template <
typename T,
typename Iterator,
typename Functor>
205 inline ThreadEngineStarter<T> startMapped(Iterator begin, Iterator end, Functor functor)
207 return startThreadEngine(
new MappedEachKernel<Iterator, Functor>(begin, end, functor));
214 template <
typename Sequence,
typename Base,
typename Functor>
215 struct SequenceHolder1 :
public Base
217 SequenceHolder1(
const Sequence &_sequence, Functor functor)
218 : Base(_sequence.begin(), _sequence.end(), functor), sequence(_sequence)
228 sequence = Sequence();
232 template <
typename T,
typename Sequence,
typename Functor>
233 inline ThreadEngineStarter<T> startMapped(
const Sequence &sequence, Functor functor)
235 typedef SequenceHolder1<Sequence,
236 MappedEachKernel<typename Sequence::const_iterator , Functor>, Functor>
239 return startThreadEngine(
new SequenceHolderType(sequence, functor));
242 template <
typename IntermediateType,
typename ResultType,
typename Sequence,
typename MapFunctor,
typename ReduceFunctor>
243 inline ThreadEngineStarter<ResultType> startMappedReduced(
const Sequence & sequence,
244 MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
245 ReduceOptions options)
247 typedef typename Sequence::const_iterator Iterator;
248 typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
249 typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
250 typedef SequenceHolder2<Sequence, MappedReduceType, MapFunctor, ReduceFunctor> SequenceHolderType;
251 return startThreadEngine(
new SequenceHolderType(sequence, mapFunctor, reduceFunctor, options));
254 template <
typename IntermediateType,
typename ResultType,
typename Iterator,
typename MapFunctor,
typename ReduceFunctor>
255 inline ThreadEngineStarter<ResultType> startMappedReduced(Iterator begin, Iterator end,
256 MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
257 ReduceOptions options)
259 typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
260 typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
261 return startThreadEngine(
new MappedReduceType(begin, end, mapFunctor, reduceFunctor, options));
271 #endif // QT_NO_CONCURRENT #define QT_END_NAMESPACE
This macro expands to.
#define it(className, varName)
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
static Bool functor(Display *display, XEvent *event, XPointer arg)
static const KeyPair *const end