]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/parallel_for_each.h
6b8d862c07f419b7f434bb46eb13810fa3aadc00
[casparcg] / tbb30_20100406oss / include / tbb / parallel_for_each.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_parallel_for_each_H
30 #define __TBB_parallel_for_each_H
31
32 #include "parallel_do.h"
33
34 namespace tbb {
35
36 //! @cond INTERNAL
37 namespace internal {
38     // The class calls user function in operator()
39     template <typename Function, typename Iterator>
40     class parallel_for_each_body : internal::no_assign {
41         const Function &my_func;
42     public:
43         parallel_for_each_body(const Function &_func) : my_func(_func) {}
44         parallel_for_each_body(const parallel_for_each_body<Function, Iterator> &_caller) : my_func(_caller.my_func) {}
45
46         void operator() ( typename std::iterator_traits<Iterator>::value_type& value ) const {
47             my_func(value);
48         }
49     };
50 } // namespace internal
51 //! @endcond
52
53 /** \name parallel_for_each
54     **/
55 //@{
56 //! Calls function f for all items from [first, last) interval using user-supplied context
57 /** @ingroup algorithms */
58 template<typename InputIterator, typename Function>
59 void parallel_for_each(InputIterator first, InputIterator last, const Function& f, task_group_context &context) {
60     internal::parallel_for_each_body<Function, InputIterator> body(f);
61
62     tbb::parallel_do (first, last, body, context);
63 }
64
65 //! Uses default context
66 template<typename InputIterator, typename Function>
67 void parallel_for_each(InputIterator first, InputIterator last, const Function& f) {
68     internal::parallel_for_each_body<Function, InputIterator> body(f);
69
70     tbb::parallel_do (first, last, body);
71 }
72
73 //@}
74
75 } // namespace
76
77 #endif /* __TBB_parallel_for_each_H */