1 | /* |
2 | Copyright (c) 2005-2019 Intel Corporation |
3 | |
4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | you may not use this file except in compliance with the License. |
6 | You may obtain a copy of the License at |
7 | |
8 | http://www.apache.org/licenses/LICENSE-2.0 |
9 | |
10 | Unless required by applicable law or agreed to in writing, software |
11 | distributed under the License is distributed on an "AS IS" BASIS, |
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | See the License for the specific language governing permissions and |
14 | limitations under the License. |
15 | */ |
16 | |
17 | #ifndef __TBB_test_range_based_for_H |
18 | #define __TBB_test_range_based_for_H |
19 | |
20 | #include <utility> //for std::pair |
21 | namespace range_based_for_support_tests{ |
22 | |
23 | template<typename value_type, typename container, typename binary_op_type, typename init_value_type> |
24 | inline init_value_type range_based_for_accumulate(container const& c, binary_op_type accumulator, init_value_type init ) |
25 | { |
26 | init_value_type range_for_accumulated = init; |
27 | #if __TBB_RANGE_BASED_FOR_PRESENT |
28 | for (value_type x : c) { |
29 | range_for_accumulated = accumulator(range_for_accumulated, x); |
30 | } |
31 | #else |
32 | for (typename container::const_iterator x =c.begin(); x != c.end(); ++x) { |
33 | range_for_accumulated = accumulator(range_for_accumulated, *x); |
34 | } |
35 | #endif |
36 | return range_for_accumulated; |
37 | } |
38 | |
39 | template<typename container, typename binary_op_type, typename init_value_type> |
40 | inline init_value_type range_based_for_accumulate(container const& c, binary_op_type accumulator, init_value_type init ) |
41 | { |
42 | typedef typename container::value_type value_type; |
43 | return range_based_for_accumulate<value_type>(c,accumulator,init); |
44 | } |
45 | |
46 | template <typename integral_type > |
47 | integral_type gauss_summ_of_int_sequence(integral_type sequence_length){ |
48 | return (sequence_length +1)* sequence_length /2; |
49 | } |
50 | |
51 | struct unified_summer |
52 | { |
53 | template <typename type> |
54 | type operator()(type const& lhs, type const& rhs) |
55 | { |
56 | return lhs + rhs; |
57 | } |
58 | |
59 | template<typename first_type, typename second_type> |
60 | second_type operator()(second_type const& lhs, std::pair<first_type, second_type> const& rhs) |
61 | { |
62 | return lhs + rhs.second; |
63 | } |
64 | }; |
65 | |
66 | struct pair_second_summer{ |
67 | template<typename first_type, typename second_type> |
68 | second_type operator() (second_type const& lhs, std::pair<first_type, second_type> const& rhs) const |
69 | { |
70 | return lhs + rhs.second; |
71 | } |
72 | }; |
73 | } |
74 | |
75 | #endif /* __TBB_test_range_based_for_H */ |
76 | |