qtconcurrentmapkernel.h
上传用户:detong
上传日期:2022-06-22
资源大小:20675k
文件大小:9k
源码类别:

系统编程

开发平台:

Unix_Linux

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  4. ** Contact: Qt Software Information (qt-info@nokia.com)
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** Commercial Usage
  9. ** Licensees holding valid Qt Commercial licenses may use this file in
  10. ** accordance with the Qt Commercial License Agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Nokia.
  13. **
  14. **
  15. ** GNU General Public License Usage
  16. ** Alternatively, this file may be used under the terms of the GNU
  17. ** General Public License versions 2.0 or 3.0 as published by the Free
  18. ** Software Foundation and appearing in the file LICENSE.GPL included in
  19. ** the packaging of this file.  Please review the following information
  20. ** to ensure GNU General Public Licensing requirements will be met:
  21. ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
  22. ** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
  23. ** exception, Nokia gives you certain additional rights. These rights
  24. ** are described in the Nokia Qt GPL Exception version 1.3, included in
  25. ** the file GPL_EXCEPTION.txt in this package.
  26. **
  27. ** Qt for Windows(R) Licensees
  28. ** As a special exception, Nokia, as the sole copyright holder for Qt
  29. ** Designer, grants users of the Qt/Eclipse Integration plug-in the
  30. ** right for the Qt/Eclipse Integration to link to functionality
  31. ** provided by Qt Designer and its related libraries.
  32. **
  33. ** If you are unsure which license is appropriate for your use, please
  34. ** contact the sales department at qt-sales@nokia.com.
  35. **
  36. ****************************************************************************/
  37. #ifndef QTCONCURRENT_MAPKERNEL_H
  38. #define QTCONCURRENT_MAPKERNEL_H
  39. #include <QtCore/qglobal.h>
  40. #ifndef QT_NO_CONCURRENT
  41. #include <QtCore/qtconcurrentiteratekernel.h>
  42. #include <QtCore/qtconcurrentreducekernel.h>
  43. QT_BEGIN_HEADER
  44. QT_BEGIN_NAMESPACE
  45. QT_MODULE(Core)
  46. #ifndef qdoc
  47. namespace QtConcurrent {
  48. // map kernel, works with both parallel-for and parallel-while
  49. template <typename Iterator, typename MapFunctor>
  50. class MapKernel : public IterateKernel<Iterator, void>
  51. {
  52.     MapFunctor map;
  53. public:
  54.     typedef void ReturnType;
  55.     MapKernel(Iterator begin, Iterator end, MapFunctor _map)
  56.         : IterateKernel<Iterator, void>(begin, end), map(_map)
  57.     { }
  58.     bool runIteration(Iterator it, int, void *)
  59.     {
  60.         map(*it);
  61.         return false;
  62.     }
  63.     bool runIterations(Iterator sequenceBeginIterator, int beginIndex, int endIndex, void *)
  64.     {
  65.         Iterator it = sequenceBeginIterator;
  66.         advance(it, beginIndex);
  67.         for (int i = beginIndex; i < endIndex; ++i) {
  68.             runIteration(it, i, 0);
  69.             advance(it, 1);
  70.         }
  71.        
  72.         return false;
  73.     }
  74. };
  75. template <typename ReducedResultType,
  76.           typename Iterator,
  77.           typename MapFunctor,
  78.           typename ReduceFunctor,
  79.           typename Reducer = ReduceKernel<ReduceFunctor,
  80.                                           ReducedResultType,
  81.                                           typename MapFunctor::result_type> >
  82. class MappedReducedKernel : public IterateKernel<Iterator, ReducedResultType>
  83. {
  84.     ReducedResultType reducedResult;
  85.     MapFunctor map;
  86.     ReduceFunctor reduce;
  87.     Reducer reducer;
  88. public:
  89.     typedef ReducedResultType ReturnType;
  90.     MappedReducedKernel(Iterator begin, Iterator end, MapFunctor _map, ReduceFunctor _reduce, ReduceOptions reduceOptions)
  91.         : IterateKernel<Iterator, ReducedResultType>(begin, end), reducedResult(), map(_map), reduce(_reduce), reducer(reduceOptions)
  92.     { }
  93.     MappedReducedKernel(ReducedResultType initialValue,
  94.                      MapFunctor _map,
  95.                      ReduceFunctor _reduce)
  96.         : reducedResult(initialValue), map(_map), reduce(_reduce)
  97.     { }
  98.     bool runIteration(Iterator it, int index, ReducedResultType *)
  99.     {
  100.         IntermediateResults<typename MapFunctor::result_type> results;
  101.         results.begin = index;
  102.         results.end = index + 1;
  103.         results.vector.append(map(*it));
  104.         reducer.runReduce(reduce, reducedResult, results);
  105.         return false;
  106.     }
  107.     bool runIterations(Iterator sequenceBeginIterator, int begin, int end, ReducedResultType *)
  108.     {
  109.         IntermediateResults<typename MapFunctor::result_type> results;
  110.         results.begin = begin;
  111.         results.end = end;
  112.         results.vector.reserve(end - begin);
  113.         Iterator it = sequenceBeginIterator;
  114.         advance(it, begin);
  115.         for (int i = begin; i < end; ++i) {
  116.             results.vector.append(map(*(it)));
  117.             advance(it, 1);
  118.         }
  119.         reducer.runReduce(reduce, reducedResult, results);
  120.         return false;
  121.     }
  122.     void finish()
  123.     {
  124.         reducer.finish(reduce, reducedResult);
  125.     }
  126.     bool shouldThrottleThread()
  127.     {
  128.         return IterateKernel<Iterator, ReducedResultType>::shouldThrottleThread() || reducer.shouldThrottle();
  129.     }
  130.     bool shouldStartThread()
  131.     {
  132.         return IterateKernel<Iterator, ReducedResultType>::shouldStartThread() && reducer.shouldStartThread();
  133.     }
  134.     typedef ReducedResultType ResultType;
  135.     ReducedResultType *result()
  136.     {
  137.         return &reducedResult;
  138.     }
  139. };
  140. template <typename Iterator, typename MapFunctor>
  141. class MappedEachKernel : public IterateKernel<Iterator, typename MapFunctor::result_type>
  142. {
  143.     MapFunctor map;
  144.     typedef typename MapFunctor::result_type T;
  145. public:
  146.     typedef T ReturnType;
  147.     typedef T ResultType;
  148.     MappedEachKernel(Iterator begin, Iterator end, MapFunctor _map)
  149.         : IterateKernel<Iterator, T>(begin, end), map(_map) { }
  150.     bool runIteration(Iterator it, int,  T *result)
  151.     {
  152.         *result = map(*it);
  153.         return true;
  154.     }
  155.     bool runIterations(Iterator sequenceBeginIterator, int begin, int end, T *results)
  156.     {
  157.         Iterator it = sequenceBeginIterator;
  158.         advance(it, begin);
  159.         for (int i = begin; i < end; ++i) {
  160.             runIteration(it, i, results + (i - begin));
  161.             advance(it, 1);
  162.         }
  163.         return true;
  164.     }
  165. };
  166. template <typename Iterator, typename Functor>
  167. inline ThreadEngineStarter<void> startMap(Iterator begin, Iterator end, Functor functor)
  168. {
  169.     return startThreadEngine(new MapKernel<Iterator, Functor>(begin, end, functor));
  170. }
  171. template <typename T, typename Iterator, typename Functor>
  172. inline ThreadEngineStarter<T> startMapped(Iterator begin, Iterator end, Functor functor)
  173. {
  174.     return startThreadEngine(new MappedEachKernel<Iterator, Functor>(begin, end, functor));
  175. }
  176. /*
  177.     The SequnceHolder class is used to hold a reference to the
  178.     sequence we are working on.
  179. */
  180. template <typename Sequence, typename Base, typename Functor>
  181. struct SequenceHolder1 : public Base
  182. {
  183.     SequenceHolder1(const Sequence &_sequence, Functor functor)
  184.         : Base(_sequence.begin(), _sequence.end(), functor), sequence(_sequence)
  185.     { }
  186.     Sequence sequence;
  187.     void finish()
  188.     {
  189.         Base::finish();
  190.         // Clear the sequence to make sure all temporaries are destroyed
  191.         // before finished is signaled.
  192.         sequence = Sequence();
  193.     }
  194. };
  195. template <typename T, typename Sequence, typename Functor>
  196. inline ThreadEngineStarter<T> startMapped(const Sequence &sequence, Functor functor)
  197. {
  198.     typedef SequenceHolder1<Sequence,
  199.                             MappedEachKernel<typename Sequence::const_iterator , Functor>, Functor>
  200.                             SequenceHolderType;
  201.     return startThreadEngine(new SequenceHolderType(sequence, functor));
  202. }
  203. template <typename IntermediateType, typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor>
  204. inline ThreadEngineStarter<ResultType> startMappedReduced(const Sequence & sequence,
  205.                                                            MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
  206.                                                            ReduceOptions options)
  207. {
  208.     typedef typename Sequence::const_iterator Iterator;
  209.     typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
  210.     typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
  211.     typedef SequenceHolder2<Sequence, MappedReduceType, MapFunctor, ReduceFunctor> SequenceHolderType;
  212.     return startThreadEngine(new SequenceHolderType(sequence, mapFunctor, reduceFunctor, options));
  213. }
  214. template <typename IntermediateType, typename ResultType, typename Iterator, typename MapFunctor, typename ReduceFunctor>
  215. inline ThreadEngineStarter<ResultType> startMappedReduced(Iterator begin, Iterator end,
  216.                                                            MapFunctor mapFunctor, ReduceFunctor reduceFunctor,
  217.                                                            ReduceOptions options)
  218. {
  219.     typedef ReduceKernel<ReduceFunctor, ResultType, IntermediateType> Reducer;
  220.     typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> MappedReduceType;
  221.     return startThreadEngine(new MappedReduceType(begin, end, mapFunctor, reduceFunctor, options));
  222. }
  223. } // namespace QtConcurrent
  224. #endif //qdoc
  225. QT_END_NAMESPACE
  226. QT_END_HEADER
  227. #endif // QT_NO_CONCURRENT
  228. #endif