1 /*
2     Copyright (c) 2005-2021 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_common_range_based_for_support_H
18 #define __TBB_test_common_range_based_for_support_H
19 
20 #include "config.h"
21 
22 #include <utility>
23 
24 namespace range_based_for_support_tests {
25 
26 template <typename ValueType, typename Container, typename BinaryAccumulator, typename InitValueType>
range_based_for_accumulate(const Container & c,BinaryAccumulator accumulator,InitValueType init)27 inline InitValueType range_based_for_accumulate( const Container& c, BinaryAccumulator accumulator, InitValueType init ) {
28     InitValueType range_based_for_accumulated = init;
29 
30     for (ValueType x : c) {
31         range_based_for_accumulated = accumulator(range_based_for_accumulated, x);
32     }
33     return range_based_for_accumulated;
34 }
35 
36 template <typename Container, typename BinaryAccumulator, typename InitValueType>
range_based_for_accumulate(const Container & c,BinaryAccumulator accumulator,InitValueType init)37 inline InitValueType range_based_for_accumulate( const Container& c, BinaryAccumulator accumulator, InitValueType init ) {
38     return range_based_for_accumulate<typename Container::value_type>(c, accumulator, init);
39 }
40 
41 template <typename IntegralType>
gauss_summ_of_int_sequence(IntegralType sequence_length)42 IntegralType gauss_summ_of_int_sequence( IntegralType sequence_length ) {
43     return (sequence_length + 1) * sequence_length / 2;
44 }
45 
46 struct UnifiedSummer {
47     template <typename T>
operatorUnifiedSummer48     T operator()( const T& lhs, const T& rhs ) {
49         return lhs + rhs;
50     }
51 
52     template <typename T, typename U>
operatorUnifiedSummer53     U operator()( const U& lhs, const std::pair<T, U>& rhs ) {
54         return lhs + rhs.second;
55     }
56 }; // struct UnifiedSummer
57 
58 struct pair_second_summer{
59     template <typename first_type, typename second_type>
operatorpair_second_summer60     second_type operator() (second_type const& lhs, std::pair<first_type, second_type> const& rhs) const
61     {
62         return lhs + rhs.second;
63     }
64 };
65 
66 } // namespace range_based_for_support_tests
67 
68 #endif // __TBB_test_common_range_based_for_support_H
69