]>
Commit | Line | Data |
---|---|---|
c2ba9709 JS |
1 | // -*- C++ -*- |
2 | ||
d652f226 | 3 | // Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
c2ba9709 JS |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
c2ba9709 JS |
9 | // version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
c2ba9709 JS |
24 | |
25 | /** | |
26 | * @file parallel/numeric | |
27 | * | |
a3e6b31a | 28 | * @brief Parallel STL function calls corresponding to stl_numeric.h. |
c2ba9709 JS |
29 | * The functions defined here mainly do case switches and |
30 | * call the actual parallelized versions in other files. | |
31 | * Inlining policy: Functions that basically only contain one function call, | |
32 | * are declared inline. | |
33 | * This file is a GNU parallel extension to the Standard C++ Library. | |
34 | */ | |
35 | ||
36 | // Written by Johannes Singler and Felix Putze. | |
37 | ||
38 | #ifndef _GLIBCXX_PARALLEL_NUMERIC_H | |
39 | #define _GLIBCXX_PARALLEL_NUMERIC_H 1 | |
40 | ||
41 | #include <numeric> | |
53567bbd | 42 | #include <bits/stl_function.h> |
c2ba9709 JS |
43 | #include <parallel/numericfwd.h> |
44 | #include <parallel/iterator.h> | |
45 | #include <parallel/for_each.h> | |
46 | #include <parallel/for_each_selectors.h> | |
47 | #include <parallel/partial_sum.h> | |
48 | ||
12ffa228 | 49 | namespace std _GLIBCXX_VISIBILITY(default) |
c2ba9709 JS |
50 | { |
51 | namespace __parallel | |
52 | { | |
53 | // Sequential fallback. | |
1acba85b JS |
54 | template<typename _IIter, typename _Tp> |
55 | inline _Tp | |
56 | accumulate(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 | 57 | __gnu_parallel::sequential_tag) |
12ffa228 | 58 | { return _GLIBCXX_STD_A::accumulate(__begin, __end, __init); } |
c2ba9709 | 59 | |
1acba85b JS |
60 | template<typename _IIter, typename _Tp, typename _BinaryOperation> |
61 | inline _Tp | |
62 | accumulate(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 | 63 | _BinaryOperation __binary_op, __gnu_parallel::sequential_tag) |
12ffa228 | 64 | { return _GLIBCXX_STD_A::accumulate(__begin, __end, __init, __binary_op); } |
c2ba9709 JS |
65 | |
66 | // Sequential fallback for input iterator case. | |
1acba85b JS |
67 | template<typename _IIter, typename _Tp, typename _IteratorTag> |
68 | inline _Tp | |
69 | __accumulate_switch(_IIter __begin, _IIter __end, | |
15ac3c72 JS |
70 | _Tp __init, _IteratorTag) |
71 | { return accumulate(__begin, __end, __init, | |
77d16198 | 72 | __gnu_parallel::sequential_tag()); } |
1acba85b JS |
73 | |
74 | template<typename _IIter, typename _Tp, typename _BinaryOperation, | |
15ac3c72 | 75 | typename _IteratorTag> |
1acba85b JS |
76 | inline _Tp |
77 | __accumulate_switch(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 | 78 | _BinaryOperation __binary_op, _IteratorTag) |
1acba85b | 79 | { return accumulate(__begin, __end, __init, __binary_op, |
15ac3c72 | 80 | __gnu_parallel::sequential_tag()); } |
c2ba9709 JS |
81 | |
82 | // Parallel algorithm for random access iterators. | |
d385563f | 83 | template<typename __RAIter, typename _Tp, typename _BinaryOperation> |
1acba85b JS |
84 | _Tp |
85 | __accumulate_switch(__RAIter __begin, __RAIter __end, | |
15ac3c72 JS |
86 | _Tp __init, _BinaryOperation __binary_op, |
87 | random_access_iterator_tag, | |
88 | __gnu_parallel::_Parallelism __parallelism_tag | |
89 | = __gnu_parallel::parallel_unbalanced) | |
5817ff8e PC |
90 | { |
91 | if (_GLIBCXX_PARALLEL_CONDITION( | |
15ac3c72 JS |
92 | static_cast<__gnu_parallel::_SequenceIndex>(__end - __begin) |
93 | >= __gnu_parallel::_Settings::get().accumulate_minimal_n | |
94 | && __gnu_parallel::__is_parallel(__parallelism_tag))) | |
95 | { | |
96 | _Tp __res = __init; | |
97 | __gnu_parallel::__accumulate_selector<__RAIter> | |
98 | __my_selector; | |
99 | __gnu_parallel:: | |
78605f0a | 100 | __for_each_template_random_access_ed(__begin, __end, |
d385563f PC |
101 | __gnu_parallel::_Nothing(), |
102 | __my_selector, | |
103 | __gnu_parallel:: | |
104 | __accumulate_binop_reduct | |
105 | <_BinaryOperation>(__binary_op), | |
106 | __res, __res, -1); | |
15ac3c72 JS |
107 | return __res; |
108 | } | |
5817ff8e | 109 | else |
15ac3c72 JS |
110 | return accumulate(__begin, __end, __init, __binary_op, |
111 | __gnu_parallel::sequential_tag()); | |
5817ff8e | 112 | } |
c2ba9709 JS |
113 | |
114 | // Public interface. | |
1acba85b JS |
115 | template<typename _IIter, typename _Tp> |
116 | inline _Tp | |
117 | accumulate(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 | 118 | __gnu_parallel::_Parallelism __parallelism_tag) |
5817ff8e | 119 | { |
1acba85b JS |
120 | typedef std::iterator_traits<_IIter> _IteratorTraits; |
121 | typedef typename _IteratorTraits::value_type _ValueType; | |
122 | typedef typename _IteratorTraits::iterator_category _IteratorCategory; | |
5817ff8e | 123 | |
1acba85b | 124 | return __accumulate_switch(__begin, __end, __init, |
d385563f PC |
125 | __gnu_parallel::_Plus<_Tp, _ValueType>(), |
126 | _IteratorCategory(), __parallelism_tag); | |
5817ff8e | 127 | } |
c2ba9709 | 128 | |
1acba85b JS |
129 | template<typename _IIter, typename _Tp> |
130 | inline _Tp | |
131 | accumulate(_IIter __begin, _IIter __end, _Tp __init) | |
5817ff8e | 132 | { |
1acba85b JS |
133 | typedef std::iterator_traits<_IIter> _IteratorTraits; |
134 | typedef typename _IteratorTraits::value_type _ValueType; | |
135 | typedef typename _IteratorTraits::iterator_category _IteratorCategory; | |
5817ff8e | 136 | |
1acba85b | 137 | return __accumulate_switch(__begin, __end, __init, |
d385563f PC |
138 | __gnu_parallel::_Plus<_Tp, _ValueType>(), |
139 | _IteratorCategory()); | |
5817ff8e | 140 | } |
6f95a65a | 141 | |
1acba85b JS |
142 | template<typename _IIter, typename _Tp, typename _BinaryOperation> |
143 | inline _Tp | |
144 | accumulate(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 JS |
145 | _BinaryOperation __binary_op, |
146 | __gnu_parallel::_Parallelism __parallelism_tag) | |
5817ff8e | 147 | { |
1acba85b JS |
148 | typedef iterator_traits<_IIter> _IteratorTraits; |
149 | typedef typename _IteratorTraits::iterator_category _IteratorCategory; | |
150 | return __accumulate_switch(__begin, __end, __init, __binary_op, | |
d385563f | 151 | _IteratorCategory(), __parallelism_tag); |
5817ff8e | 152 | } |
c2ba9709 | 153 | |
1acba85b JS |
154 | template<typename _IIter, typename _Tp, typename _BinaryOperation> |
155 | inline _Tp | |
156 | accumulate(_IIter __begin, _IIter __end, _Tp __init, | |
15ac3c72 | 157 | _BinaryOperation __binary_op) |
5817ff8e | 158 | { |
1acba85b JS |
159 | typedef iterator_traits<_IIter> _IteratorTraits; |
160 | typedef typename _IteratorTraits::iterator_category _IteratorCategory; | |
161 | return __accumulate_switch(__begin, __end, __init, __binary_op, | |
d385563f | 162 | _IteratorCategory()); |
5817ff8e | 163 | } |
6f95a65a BK |
164 | |
165 | ||
c2ba9709 | 166 | // Sequential fallback. |
1acba85b JS |
167 | template<typename _IIter1, typename _IIter2, typename _Tp> |
168 | inline _Tp | |
169 | inner_product(_IIter1 __first1, _IIter1 __last1, | |
15ac3c72 JS |
170 | _IIter2 __first2, _Tp __init, |
171 | __gnu_parallel::sequential_tag) | |
12ffa228 | 172 | { return _GLIBCXX_STD_A::inner_product( |
15ac3c72 | 173 | __first1, __last1, __first2, __init); } |
5817ff8e | 174 | |
1acba85b | 175 | template<typename _IIter1, typename _IIter2, typename _Tp, |
4459d22e | 176 | typename _BinaryFunction1, typename _BinaryFunction2> |
1acba85b | 177 | inline _Tp |
4459d22e JS |
178 | inner_product(_IIter1 __first1, _IIter1 __last1, |
179 | _IIter2 __first2, _Tp __init, _BinaryFunction1 __binary_op1, | |
180 | _BinaryFunction2 __binary_op2, | |
181 | __gnu_parallel::sequential_tag) | |
12ffa228 | 182 | { return _GLIBCXX_STD_A::inner_product(__first1, __last1, __first2, __init, |
15ac3c72 | 183 | __binary_op1, __binary_op2); } |
c2ba9709 JS |
184 | |
185 | // Parallel algorithm for random access iterators. | |
1acba85b | 186 | template<typename _RAIter1, typename _RAIter2, |
4459d22e | 187 | typename _Tp, typename _BinaryFunction1, typename _BinaryFunction2> |
1acba85b JS |
188 | _Tp |
189 | __inner_product_switch(_RAIter1 __first1, | |
d385563f PC |
190 | _RAIter1 __last1, |
191 | _RAIter2 __first2, _Tp __init, | |
192 | _BinaryFunction1 __binary_op1, | |
193 | _BinaryFunction2 __binary_op2, | |
194 | random_access_iterator_tag, | |
195 | random_access_iterator_tag, | |
196 | __gnu_parallel::_Parallelism __parallelism_tag | |
197 | = __gnu_parallel::parallel_unbalanced) | |
5817ff8e | 198 | { |
1acba85b | 199 | if (_GLIBCXX_PARALLEL_CONDITION((__last1 - __first1) |
15ac3c72 JS |
200 | >= __gnu_parallel::_Settings::get(). |
201 | accumulate_minimal_n | |
202 | && __gnu_parallel:: | |
203 | __is_parallel(__parallelism_tag))) | |
204 | { | |
205 | _Tp __res = __init; | |
206 | __gnu_parallel:: | |
207 | __inner_product_selector<_RAIter1, | |
208 | _RAIter2, _Tp> __my_selector(__first1, __first2); | |
209 | __gnu_parallel:: | |
78605f0a JS |
210 | __for_each_template_random_access_ed( |
211 | __first1, __last1, __binary_op2, __my_selector, __binary_op1, | |
212 | __res, __res, -1); | |
15ac3c72 JS |
213 | return __res; |
214 | } | |
5817ff8e | 215 | else |
15ac3c72 JS |
216 | return inner_product(__first1, __last1, __first2, __init, |
217 | __gnu_parallel::sequential_tag()); | |
5817ff8e | 218 | } |
c2ba9709 JS |
219 | |
220 | // No parallelism for input iterators. | |
1acba85b | 221 | template<typename _IIter1, typename _IIter2, typename _Tp, |
4459d22e | 222 | typename _BinaryFunction1, typename _BinaryFunction2, |
15ac3c72 | 223 | typename _IteratorTag1, typename _IteratorTag2> |
1acba85b JS |
224 | inline _Tp |
225 | __inner_product_switch(_IIter1 __first1, _IIter1 __last1, | |
d385563f PC |
226 | _IIter2 __first2, _Tp __init, |
227 | _BinaryFunction1 __binary_op1, | |
228 | _BinaryFunction2 __binary_op2, | |
229 | _IteratorTag1, _IteratorTag2) | |
230 | { return inner_product(__first1, __last1, __first2, __init, __binary_op1, | |
231 | __binary_op2, __gnu_parallel::sequential_tag()); } | |
5817ff8e | 232 | |
1acba85b | 233 | template<typename _IIter1, typename _IIter2, typename _Tp, |
4459d22e | 234 | typename _BinaryFunction1, typename _BinaryFunction2> |
1acba85b JS |
235 | inline _Tp |
236 | inner_product(_IIter1 __first1, _IIter1 __last1, | |
4459d22e JS |
237 | _IIter2 __first2, _Tp __init, _BinaryFunction1 __binary_op1, |
238 | _BinaryFunction2 __binary_op2, | |
15ac3c72 | 239 | __gnu_parallel::_Parallelism __parallelism_tag) |
5817ff8e | 240 | { |
1acba85b JS |
241 | typedef iterator_traits<_IIter1> _TraitsType1; |
242 | typedef typename _TraitsType1::iterator_category _IteratorCategory1; | |
5817ff8e | 243 | |
1acba85b JS |
244 | typedef iterator_traits<_IIter2> _TraitsType2; |
245 | typedef typename _TraitsType2::iterator_category _IteratorCategory2; | |
5817ff8e | 246 | |
d385563f PC |
247 | return __inner_product_switch(__first1, __last1, __first2, __init, |
248 | __binary_op1, __binary_op2, | |
249 | _IteratorCategory1(), _IteratorCategory2(), | |
250 | __parallelism_tag); | |
5817ff8e PC |
251 | } |
252 | ||
1acba85b | 253 | template<typename _IIter1, typename _IIter2, typename _Tp, |
4459d22e | 254 | typename _BinaryFunction1, typename _BinaryFunction2> |
1acba85b JS |
255 | inline _Tp |
256 | inner_product(_IIter1 __first1, _IIter1 __last1, | |
4459d22e JS |
257 | _IIter2 __first2, _Tp __init, _BinaryFunction1 __binary_op1, |
258 | _BinaryFunction2 __binary_op2) | |
5817ff8e | 259 | { |
1acba85b JS |
260 | typedef iterator_traits<_IIter1> _TraitsType1; |
261 | typedef typename _TraitsType1::iterator_category _IteratorCategory1; | |
5817ff8e | 262 | |
1acba85b JS |
263 | typedef iterator_traits<_IIter2> _TraitsType2; |
264 | typedef typename _TraitsType2::iterator_category _IteratorCategory2; | |
5817ff8e | 265 | |
d385563f PC |
266 | return __inner_product_switch(__first1, __last1, __first2, __init, |
267 | __binary_op1, __binary_op2, | |
268 | _IteratorCategory1(), | |
269 | _IteratorCategory2()); | |
5817ff8e | 270 | } |
c2ba9709 | 271 | |
1acba85b JS |
272 | template<typename _IIter1, typename _IIter2, typename _Tp> |
273 | inline _Tp | |
274 | inner_product(_IIter1 __first1, _IIter1 __last1, | |
15ac3c72 JS |
275 | _IIter2 __first2, _Tp __init, |
276 | __gnu_parallel::_Parallelism __parallelism_tag) | |
5817ff8e | 277 | { |
4459d22e JS |
278 | typedef iterator_traits<_IIter1> _TraitsType1; |
279 | typedef typename _TraitsType1::value_type _ValueType1; | |
280 | typedef iterator_traits<_IIter2> _TraitsType2; | |
281 | typedef typename _TraitsType2::value_type _ValueType2; | |
5817ff8e PC |
282 | |
283 | typedef typename | |
2305a1e8 | 284 | __gnu_parallel::_Multiplies<_ValueType1, _ValueType2>::result_type |
1acba85b | 285 | _MultipliesResultType; |
52fe3d5b | 286 | return __gnu_parallel::inner_product(__first1, __last1, __first2, __init, |
1acba85b | 287 | __gnu_parallel::_Plus<_Tp, _MultipliesResultType>(), |
5817ff8e | 288 | __gnu_parallel:: |
4459d22e | 289 | _Multiplies<_ValueType1, _ValueType2>(), |
1acba85b | 290 | __parallelism_tag); |
5817ff8e | 291 | } |
c2ba9709 | 292 | |
1acba85b JS |
293 | template<typename _IIter1, typename _IIter2, typename _Tp> |
294 | inline _Tp | |
295 | inner_product(_IIter1 __first1, _IIter1 __last1, | |
15ac3c72 | 296 | _IIter2 __first2, _Tp __init) |
5817ff8e | 297 | { |
4459d22e JS |
298 | typedef iterator_traits<_IIter1> _TraitsType1; |
299 | typedef typename _TraitsType1::value_type _ValueType1; | |
300 | typedef iterator_traits<_IIter2> _TraitsType2; | |
301 | typedef typename _TraitsType2::value_type _ValueType2; | |
5817ff8e PC |
302 | |
303 | typedef typename | |
2305a1e8 | 304 | __gnu_parallel::_Multiplies<_ValueType1, _ValueType2>::result_type |
1acba85b | 305 | _MultipliesResultType; |
52fe3d5b | 306 | return __gnu_parallel::inner_product(__first1, __last1, __first2, __init, |
1acba85b | 307 | __gnu_parallel::_Plus<_Tp, _MultipliesResultType>(), |
5817ff8e | 308 | __gnu_parallel:: |
4459d22e | 309 | _Multiplies<_ValueType1, _ValueType2>()); |
5817ff8e | 310 | } |
6f95a65a | 311 | |
c2ba9709 | 312 | // Sequential fallback. |
1acba85b JS |
313 | template<typename _IIter, typename _OutputIterator> |
314 | inline _OutputIterator | |
315 | partial_sum(_IIter __begin, _IIter __end, _OutputIterator __result, | |
15ac3c72 | 316 | __gnu_parallel::sequential_tag) |
12ffa228 | 317 | { return _GLIBCXX_STD_A::partial_sum(__begin, __end, __result); } |
c2ba9709 JS |
318 | |
319 | // Sequential fallback. | |
1acba85b | 320 | template<typename _IIter, typename _OutputIterator, |
d385563f | 321 | typename _BinaryOperation> |
1acba85b JS |
322 | inline _OutputIterator |
323 | partial_sum(_IIter __begin, _IIter __end, _OutputIterator __result, | |
15ac3c72 | 324 | _BinaryOperation __bin_op, __gnu_parallel::sequential_tag) |
12ffa228 | 325 | { return _GLIBCXX_STD_A::partial_sum(__begin, __end, __result, __bin_op); } |
c2ba9709 JS |
326 | |
327 | // Sequential fallback for input iterator case. | |
1acba85b | 328 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 JS |
329 | typename _BinaryOperation, typename _IteratorTag1, |
330 | typename _IteratorTag2> | |
1acba85b JS |
331 | inline _OutputIterator |
332 | __partial_sum_switch(_IIter __begin, _IIter __end, | |
d385563f PC |
333 | _OutputIterator __result, _BinaryOperation __bin_op, |
334 | _IteratorTag1, _IteratorTag2) | |
12ffa228 | 335 | { return _GLIBCXX_STD_A::partial_sum(__begin, __end, __result, __bin_op); } |
c2ba9709 JS |
336 | |
337 | // Parallel algorithm for random access iterators. | |
1acba85b | 338 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 | 339 | typename _BinaryOperation> |
1acba85b JS |
340 | _OutputIterator |
341 | __partial_sum_switch(_IIter __begin, _IIter __end, | |
d385563f PC |
342 | _OutputIterator __result, _BinaryOperation __bin_op, |
343 | random_access_iterator_tag, | |
344 | random_access_iterator_tag) | |
5817ff8e | 345 | { |
a4797b34 | 346 | if (_GLIBCXX_PARALLEL_CONDITION( |
15ac3c72 JS |
347 | static_cast<__gnu_parallel::_SequenceIndex>(__end - __begin) |
348 | >= __gnu_parallel::_Settings::get().partial_sum_minimal_n)) | |
349 | return __gnu_parallel::__parallel_partial_sum(__begin, __end, | |
d385563f | 350 | __result, __bin_op); |
5817ff8e | 351 | else |
15ac3c72 JS |
352 | return partial_sum(__begin, __end, __result, __bin_op, |
353 | __gnu_parallel::sequential_tag()); | |
5817ff8e | 354 | } |
c2ba9709 JS |
355 | |
356 | // Public interface. | |
1acba85b JS |
357 | template<typename _IIter, typename _OutputIterator> |
358 | inline _OutputIterator | |
359 | partial_sum(_IIter __begin, _IIter __end, _OutputIterator __result) | |
5817ff8e | 360 | { |
1acba85b | 361 | typedef typename iterator_traits<_IIter>::value_type _ValueType; |
52fe3d5b | 362 | return __gnu_parallel::partial_sum(__begin, __end, |
0e50f335 | 363 | __result, std::plus<_ValueType>()); |
5817ff8e | 364 | } |
c2ba9709 JS |
365 | |
366 | // Public interface | |
1acba85b | 367 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 | 368 | typename _BinaryOperation> |
1acba85b JS |
369 | inline _OutputIterator |
370 | partial_sum(_IIter __begin, _IIter __end, _OutputIterator __result, | |
15ac3c72 | 371 | _BinaryOperation __binary_op) |
5817ff8e | 372 | { |
4459d22e JS |
373 | typedef iterator_traits<_IIter> _ITraitsType; |
374 | typedef typename _ITraitsType::iterator_category _IIteratorCategory; | |
5817ff8e | 375 | |
1acba85b JS |
376 | typedef iterator_traits<_OutputIterator> _OTraitsType; |
377 | typedef typename _OTraitsType::iterator_category _OIterCategory; | |
5817ff8e | 378 | |
1acba85b | 379 | return __partial_sum_switch(__begin, __end, __result, __binary_op, |
d385563f | 380 | _IIteratorCategory(), _OIterCategory()); |
5817ff8e | 381 | } |
c2ba9709 JS |
382 | |
383 | // Sequential fallback. | |
1acba85b JS |
384 | template<typename _IIter, typename _OutputIterator> |
385 | inline _OutputIterator | |
15ac3c72 JS |
386 | adjacent_difference(_IIter __begin, _IIter __end, _OutputIterator __result, |
387 | __gnu_parallel::sequential_tag) | |
12ffa228 | 388 | { return _GLIBCXX_STD_A::adjacent_difference(__begin, __end, __result); } |
c2ba9709 JS |
389 | |
390 | // Sequential fallback. | |
1acba85b | 391 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 | 392 | typename _BinaryOperation> |
1acba85b JS |
393 | inline _OutputIterator |
394 | adjacent_difference(_IIter __begin, _IIter __end, | |
15ac3c72 JS |
395 | _OutputIterator __result, _BinaryOperation __bin_op, |
396 | __gnu_parallel::sequential_tag) | |
12ffa228 | 397 | { return _GLIBCXX_STD_A::adjacent_difference(__begin, __end, |
d385563f | 398 | __result, __bin_op); } |
c2ba9709 JS |
399 | |
400 | // Sequential fallback for input iterator case. | |
1acba85b | 401 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 JS |
402 | typename _BinaryOperation, typename _IteratorTag1, |
403 | typename _IteratorTag2> | |
1acba85b | 404 | inline _OutputIterator |
d385563f PC |
405 | __adjacent_difference_switch(_IIter __begin, _IIter __end, |
406 | _OutputIterator __result, | |
407 | _BinaryOperation __bin_op, _IteratorTag1, | |
408 | _IteratorTag2) | |
15ac3c72 JS |
409 | { return adjacent_difference(__begin, __end, __result, __bin_op, |
410 | __gnu_parallel::sequential_tag()); } | |
c2ba9709 JS |
411 | |
412 | // Parallel algorithm for random access iterators. | |
1acba85b | 413 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 | 414 | typename _BinaryOperation> |
1acba85b | 415 | _OutputIterator |
d385563f PC |
416 | __adjacent_difference_switch(_IIter __begin, _IIter __end, |
417 | _OutputIterator __result, | |
418 | _BinaryOperation __bin_op, | |
419 | random_access_iterator_tag, | |
420 | random_access_iterator_tag, | |
421 | __gnu_parallel::_Parallelism | |
422 | __parallelism_tag | |
423 | = __gnu_parallel::parallel_balanced) | |
5817ff8e PC |
424 | { |
425 | if (_GLIBCXX_PARALLEL_CONDITION( | |
15ac3c72 JS |
426 | static_cast<__gnu_parallel::_SequenceIndex>(__end - __begin) |
427 | >= __gnu_parallel::_Settings::get().adjacent_difference_minimal_n | |
428 | && __gnu_parallel::__is_parallel(__parallelism_tag))) | |
429 | { | |
430 | bool __dummy = true; | |
431 | typedef __gnu_parallel::_IteratorPair<_IIter, _OutputIterator, | |
432 | random_access_iterator_tag> _ItTrip; | |
433 | *__result = *__begin; | |
78605f0a JS |
434 | _ItTrip __begin_pair(__begin + 1, __result + 1), |
435 | __end_pair(__end, __result + (__end - __begin)); | |
15ac3c72 JS |
436 | __gnu_parallel::__adjacent_difference_selector<_ItTrip> |
437 | __functionality; | |
438 | __gnu_parallel:: | |
78605f0a JS |
439 | __for_each_template_random_access_ed( |
440 | __begin_pair, __end_pair, __bin_op, __functionality, | |
441 | __gnu_parallel::_DummyReduct(), __dummy, __dummy, -1); | |
6aa7cd49 | 442 | return __functionality._M_finish_iterator; |
15ac3c72 | 443 | } |
5817ff8e | 444 | else |
15ac3c72 JS |
445 | return adjacent_difference(__begin, __end, __result, __bin_op, |
446 | __gnu_parallel::sequential_tag()); | |
5817ff8e | 447 | } |
c2ba9709 JS |
448 | |
449 | // Public interface. | |
1acba85b JS |
450 | template<typename _IIter, typename _OutputIterator> |
451 | inline _OutputIterator | |
452 | adjacent_difference(_IIter __begin, _IIter __end, | |
15ac3c72 JS |
453 | _OutputIterator __result, |
454 | __gnu_parallel::_Parallelism __parallelism_tag) | |
5817ff8e | 455 | { |
1acba85b JS |
456 | typedef iterator_traits<_IIter> _TraitsType; |
457 | typedef typename _TraitsType::value_type _ValueType; | |
d385563f PC |
458 | return adjacent_difference(__begin, __end, __result, |
459 | std::minus<_ValueType>(), | |
460 | __parallelism_tag); | |
5817ff8e | 461 | } |
6f95a65a | 462 | |
1acba85b JS |
463 | template<typename _IIter, typename _OutputIterator> |
464 | inline _OutputIterator | |
465 | adjacent_difference(_IIter __begin, _IIter __end, | |
15ac3c72 | 466 | _OutputIterator __result) |
5817ff8e | 467 | { |
1acba85b JS |
468 | typedef iterator_traits<_IIter> _TraitsType; |
469 | typedef typename _TraitsType::value_type _ValueType; | |
15ac3c72 | 470 | return adjacent_difference(__begin, __end, __result, |
d385563f | 471 | std::minus<_ValueType>()); |
5817ff8e PC |
472 | } |
473 | ||
1acba85b | 474 | template<typename _IIter, typename _OutputIterator, |
15ac3c72 | 475 | typename _BinaryOperation> |
1acba85b JS |
476 | inline _OutputIterator |
477 | adjacent_difference(_IIter __begin, _IIter __end, | |
15ac3c72 JS |
478 | _OutputIterator __result, _BinaryOperation __binary_op, |
479 | __gnu_parallel::_Parallelism __parallelism_tag) | |
5817ff8e | 480 | { |
4459d22e JS |
481 | typedef iterator_traits<_IIter> _ITraitsType; |
482 | typedef typename _ITraitsType::iterator_category _IIteratorCategory; | |
5817ff8e | 483 | |
1acba85b JS |
484 | typedef iterator_traits<_OutputIterator> _OTraitsType; |
485 | typedef typename _OTraitsType::iterator_category _OIterCategory; | |
5817ff8e | 486 | |
d385563f PC |
487 | return __adjacent_difference_switch(__begin, __end, __result, |
488 | __binary_op, | |
489 | _IIteratorCategory(), | |
490 | _OIterCategory(), | |
491 | __parallelism_tag); | |
5817ff8e PC |
492 | } |
493 | ||
1acba85b | 494 | template<typename _IIter, typename _OutputIterator, |
d385563f | 495 | typename _BinaryOperation> |
1acba85b JS |
496 | inline _OutputIterator |
497 | adjacent_difference(_IIter __begin, _IIter __end, | |
d385563f | 498 | _OutputIterator __result, _BinaryOperation __binary_op) |
5817ff8e | 499 | { |
4459d22e JS |
500 | typedef iterator_traits<_IIter> _ITraitsType; |
501 | typedef typename _ITraitsType::iterator_category _IIteratorCategory; | |
5817ff8e | 502 | |
1acba85b JS |
503 | typedef iterator_traits<_OutputIterator> _OTraitsType; |
504 | typedef typename _OTraitsType::iterator_category _OIterCategory; | |
5817ff8e | 505 | |
d385563f PC |
506 | return __adjacent_difference_switch(__begin, __end, __result, |
507 | __binary_op, | |
508 | _IIteratorCategory(), | |
509 | _OIterCategory()); | |
5817ff8e | 510 | } |
c2ba9709 JS |
511 | } // end namespace |
512 | } // end namespace | |
513 | ||
514 | #endif /* _GLIBCXX_NUMERIC_H */ |