42 #ifndef QTCONCURRENT_FILTERKERNEL_H 43 #define QTCONCURRENT_FILTERKERNEL_H 45 #include <QtCore/qglobal.h> 47 #ifndef QT_NO_CONCURRENT 49 #include <QtCore/qtconcurrentiteratekernel.h> 50 #include <QtCore/qtconcurrentmapkernel.h> 51 #include <QtCore/qtconcurrentreducekernel.h> 65 typedef typename T::value_type value_type;
69 struct qValueType<const T*>
81 template <
typename Sequence,
typename KeepFunctor,
typename ReduceFunctor>
82 class FilterKernel :
public IterateKernel<typename Sequence::const_iterator, void>
84 typedef ReduceKernel<ReduceFunctor, Sequence, typename Sequence::value_type> Reducer;
85 typedef IterateKernel<typename Sequence::const_iterator, void> IterateKernelType;
86 typedef typename ReduceFunctor::result_type T;
88 Sequence reducedResult;
95 FilterKernel(Sequence &_sequence, KeepFunctor _keep, ReduceFunctor _reduce)
96 : IterateKernelType(const_cast<const Sequence &>(_sequence).begin(), const_cast<const Sequence &>(_sequence).
end()), reducedResult(),
103 bool runIteration(
typename Sequence::const_iterator
it,
int index, T *)
105 IntermediateResults<typename Sequence::value_type> results;
106 results.begin =
index;
107 results.end = index + 1;
110 results.vector.append(*it);
112 reducer.runReduce(reduce, reducedResult, results);
116 bool runIterations(
typename Sequence::const_iterator sequenceBeginIterator,
int begin,
int end, T *)
118 IntermediateResults<typename Sequence::value_type> results;
119 results.begin = begin;
121 results.vector.reserve(end - begin);
124 typename Sequence::const_iterator it = sequenceBeginIterator;
126 for (
int i = begin; i <
end; ++i) {
128 results.vector.append(*it);
132 reducer.runReduce(reduce, reducedResult, results);
138 reducer.finish(reduce, reducedResult);
139 sequence = reducedResult;
142 inline bool shouldThrottleThread()
144 return IterateKernelType::shouldThrottleThread() || reducer.shouldThrottle();
147 inline bool shouldStartThread()
149 return IterateKernelType::shouldStartThread() && reducer.shouldStartThread();
152 typedef void ReturnType;
153 typedef void ResultType;
157 template <
typename ReducedResultType,
159 typename KeepFunctor,
160 typename ReduceFunctor,
161 typename Reducer = ReduceKernel<ReduceFunctor,
163 typename qValueType<Iterator>::value_type> >
164 class FilteredReducedKernel :
public IterateKernel<Iterator, ReducedResultType>
166 ReducedResultType reducedResult;
168 ReduceFunctor reduce;
170 typedef IterateKernel<Iterator, ReducedResultType> IterateKernelType;
173 FilteredReducedKernel(Iterator begin,
176 ReduceFunctor _reduce,
177 ReduceOptions reduceOption)
178 : IterateKernelType(begin, end), reducedResult(), keep(_keep), reduce(_reduce), reducer(reduceOption)
182 FilteredReducedKernel(ReducedResultType initialValue,
184 ReduceFunctor reduce,
186 : reducedResult(initialValue), keep(keep), reduce(reduce), reducer(reduceOption)
190 bool runIteration(Iterator it,
int index, ReducedResultType *)
192 IntermediateResults<typename qValueType<Iterator>::value_type> results;
193 results.begin =
index;
194 results.end = index + 1;
197 results.vector.append(*it);
199 reducer.runReduce(reduce, reducedResult, results);
203 bool runIterations(Iterator sequenceBeginIterator,
int begin,
int end, ReducedResultType *)
205 IntermediateResults<typename qValueType<Iterator>::value_type> results;
206 results.begin = begin;
208 results.vector.reserve(end - begin);
210 Iterator it = sequenceBeginIterator;
212 for (
int i = begin; i <
end; ++i) {
214 results.vector.append(*it);
218 reducer.runReduce(reduce, reducedResult, results);
224 reducer.finish(reduce, reducedResult);
227 inline bool shouldThrottleThread()
229 return IterateKernelType::shouldThrottleThread() || reducer.shouldThrottle();
232 inline bool shouldStartThread()
234 return IterateKernelType::shouldStartThread() && reducer.shouldStartThread();
237 typedef ReducedResultType ReturnType;
238 typedef ReducedResultType ResultType;
239 ReducedResultType *result()
241 return &reducedResult;
246 template <
typename Iterator,
typename KeepFunctor>
247 class FilteredEachKernel :
public IterateKernel<Iterator, typename qValueType<Iterator>::value_type>
249 typedef typename qValueType<Iterator>::value_type T;
250 typedef IterateKernel<Iterator, T> IterateKernelType;
255 typedef T ReturnType;
256 typedef T ResultType;
258 FilteredEachKernel(Iterator begin, Iterator end, KeepFunctor _keep)
259 : IterateKernelType(begin, end), keep(_keep)
264 if (this->futureInterface)
265 this->futureInterface->setFilterMode(
true);
266 IterateKernelType::start();
269 bool runIteration(Iterator it,
int index, T *)
272 this->reportResult(&(*it), index);
274 this->reportResult(0, index);
278 bool runIterations(Iterator sequenceBeginIterator,
int begin,
int end, T *)
280 const int count = end - begin;
281 IntermediateResults<typename qValueType<Iterator>::value_type> results;
282 results.begin = begin;
284 results.vector.reserve(count);
286 Iterator it = sequenceBeginIterator;
288 for (
int i = begin; i <
end; ++i) {
290 results.vector.append(*it);
294 this->reportResults(results.vector, begin, count);
299 template <
typename Iterator,
typename KeepFunctor>
301 ThreadEngineStarter<typename qValueType<Iterator>::value_type>
302 startFiltered(Iterator begin, Iterator end, KeepFunctor
functor)
304 return startThreadEngine(
new FilteredEachKernel<Iterator, KeepFunctor>(begin, end, functor));
307 template <
typename Sequence,
typename KeepFunctor>
308 inline ThreadEngineStarter<typename Sequence::value_type>
309 startFiltered(
const Sequence &sequence, KeepFunctor functor)
311 typedef SequenceHolder1<Sequence,
312 FilteredEachKernel<typename Sequence::const_iterator, KeepFunctor>,
315 return startThreadEngine(
new SequenceHolderType(sequence, functor));
318 template <
typename ResultType,
typename Sequence,
typename MapFunctor,
typename ReduceFunctor>
319 inline ThreadEngineStarter<ResultType> startFilteredReduced(
const Sequence & sequence,
320 MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
321 ReduceOptions options)
323 typedef typename Sequence::const_iterator Iterator;
324 typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type > Reducer;
325 typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType;
326 typedef SequenceHolder2<Sequence, FilteredReduceType, MapFunctor, ReduceFunctor> SequenceHolderType;
327 return startThreadEngine(
new SequenceHolderType(sequence, mapFunctor, reduceFunctor, options));
331 template <
typename ResultType,
typename Iterator,
typename MapFunctor,
typename ReduceFunctor>
332 inline ThreadEngineStarter<ResultType> startFilteredReduced(Iterator begin, Iterator end,
333 MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
334 ReduceOptions options)
336 typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type> Reducer;
337 typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType;
338 return startThreadEngine(
new FilteredReduceType(begin, end, mapFunctor, reduceFunctor, options));
349 #endif // QT_NO_CONCURRENT #define QT_END_NAMESPACE
This macro expands to.
#define it(className, varName)
#define QT_BEGIN_NAMESPACE
This macro expands to.
ReduceOption
This enum specifies the order of which results from the map or filter function are passed to the redu...
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