diff options
author | Johannes Singler <singler@ira.uka.de> | 2007-10-08 21:14:45 +0000 |
---|---|---|
committer | Benjamin Kosnik <bkoz@gcc.gnu.org> | 2007-10-08 21:14:45 +0000 |
commit | 0e6c9eaa5c0c9bf00972834f3a47e5146bd47dee (patch) | |
tree | da17effa76d21f3f47f27ae04cc5bc0c5c968251 /libstdc++-v3/include/parallel | |
parent | 9de88093b6e322a53b20ea23560ca3cd753120ec (diff) | |
download | gcc-0e6c9eaa5c0c9bf00972834f3a47e5146bd47dee.tar.gz |
base.h: Added plus and multiplies functor for differently typed objects.
2007-10-08 Johannes Singler <singler@ira.uka.de>
* include/parallel/base.h: Added plus and multiplies functor
for differently typed objects.
* include/parallel/numeric: Use it.
* include/parallel/for_each_selectors.h: Allowed different types.
* include/parallel/partial_sum.h: Fixed return value.
* testsuite/26_numerics/accumulate/1.cc: Tests for accumulate.
* testsuite/26_numerics/inner_product/1.cc: Tests for inner_product.
From-SVN: r129140
Diffstat (limited to 'libstdc++-v3/include/parallel')
-rw-r--r-- | libstdc++-v3/include/parallel/base.h | 63 | ||||
-rw-r--r-- | libstdc++-v3/include/parallel/for_each_selectors.h | 4 | ||||
-rw-r--r-- | libstdc++-v3/include/parallel/numeric | 33 | ||||
-rw-r--r-- | libstdc++-v3/include/parallel/partial_sum.h | 2 |
4 files changed, 81 insertions, 21 deletions
diff --git a/libstdc++-v3/include/parallel/base.h b/libstdc++-v3/include/parallel/base.h index 12bba0455fc..0a86d8351fe 100644 --- a/libstdc++-v3/include/parallel/base.h +++ b/libstdc++-v3/include/parallel/base.h @@ -112,14 +112,6 @@ namespace __gnu_parallel }; - /** @brief Similar to std::equal_to, but allows two different types. */ - template<typename T1, typename T2> - struct equal_to : std::binary_function<T1, T2, bool> - { - bool operator()(const T1& t1, const T2& t2) const - { return t1 == t2; } - }; - /** @brief Similar to std::binder1st, but giving the argument types explicitly. */ template<typename _Predicate, typename argument_type> class unary_negate @@ -190,6 +182,14 @@ namespace __gnu_parallel { return op(__x, value); } }; + /** @brief Similar to std::equal_to, but allows two different types. */ + template<typename T1, typename T2> + struct equal_to : std::binary_function<T1, T2, bool> + { + bool operator()(const T1& t1, const T2& t2) const + { return t1 == t2; } + }; + /** @brief Similar to std::less, but allows two different types. */ template<typename T1, typename T2> struct less : std::binary_function<T1, T2, bool> @@ -212,6 +212,53 @@ namespace __gnu_parallel { return __x < __y; } }; + + /** @brief Similar to std::plus, but allows two different types. */ + template<typename _Tp1, typename _Tp2> + struct plus : public std::binary_function<_Tp1, _Tp2, _Tp1> + { + typedef typeof(*static_cast<_Tp1*>(NULL) + *static_cast<_Tp2*>(NULL)) result; + + result + operator()(const _Tp1& __x, const _Tp2& __y) const + { return __x + __y; } + }; + + // Partial specialization for one type. Same as std::plus. + template<typename _Tp> + struct plus<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp> + { + typedef typeof(*static_cast<_Tp*>(NULL) + *static_cast<_Tp*>(NULL)) result; + + result + operator()(const _Tp& __x, const _Tp& __y) const + { return __x + __y; } + }; + + + /** @brief Similar to std::multiplies, but allows two different types. */ + template<typename _Tp1, typename _Tp2> + struct multiplies : public std::binary_function<_Tp1, _Tp2, _Tp1> + { + typedef typeof(*static_cast<_Tp1*>(NULL) * *static_cast<_Tp2*>(NULL)) result; + + result + operator()(const _Tp1& __x, const _Tp2& __y) const + { return __x * __y; } + }; + + // Partial specialization for one type. Same as std::multiplies. + template<typename _Tp> + struct multiplies<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp> + { + typedef typeof(*static_cast<_Tp*>(NULL) * *static_cast<_Tp*>(NULL)) result; + + result + operator()(const _Tp& __x, const _Tp& __y) const + { return __x * __y; } + }; + + template<typename T, typename _DifferenceTp> class pseudo_sequence; diff --git a/libstdc++-v3/include/parallel/for_each_selectors.h b/libstdc++-v3/include/parallel/for_each_selectors.h index f1d0abf255b..392cc6ac7ea 100644 --- a/libstdc++-v3/include/parallel/for_each_selectors.h +++ b/libstdc++-v3/include/parallel/for_each_selectors.h @@ -335,8 +335,8 @@ namespace __gnu_parallel explicit accumulate_binop_reduct(BinOp& b) : binop(b) {} - template<typename T> - inline T operator()(T x, T y) { return binop(x, y); } + template<typename Result, typename Addend> + Result operator()(const Result& x, const Addend& y) { return binop(x, y); } }; } diff --git a/libstdc++-v3/include/parallel/numeric b/libstdc++-v3/include/parallel/numeric index 52fa600ddba..21b8eea3fdd 100644 --- a/libstdc++-v3/include/parallel/numeric +++ b/libstdc++-v3/include/parallel/numeric @@ -114,7 +114,7 @@ namespace __parallel typedef typename iterator_traits::value_type value_type; typedef typename iterator_traits::iterator_category iterator_category; - return accumulate_switch(begin, end, init, std::plus<value_type>(), + return accumulate_switch(begin, end, init, __gnu_parallel::plus<T, value_type>(), iterator_category(), parallelism_tag); } @@ -126,7 +126,7 @@ namespace __parallel typedef typename iterator_traits::value_type value_type; typedef typename iterator_traits::iterator_category iterator_category; - return accumulate_switch(begin, end, init, std::plus<value_type>(), + return accumulate_switch(begin, end, init, __gnu_parallel::plus<T, value_type>(), iterator_category()); } @@ -241,10 +241,17 @@ namespace __parallel InputIterator2 first2, T init, __gnu_parallel::parallelism parallelism_tag) { - typedef iterator_traits<InputIterator1> traits_type; - typedef typename traits_type::value_type value_type; - return inner_product(first1, last1, first2, init, std::plus<value_type>(), - std::multiplies<value_type>(), parallelism_tag); + typedef iterator_traits<InputIterator1> traits_type1; + typedef typename traits_type1::value_type value_type1; + typedef iterator_traits<InputIterator2> traits_type2; + typedef typename traits_type2::value_type value_type2; + + typedef typename __gnu_parallel::multiplies<value_type1, value_type2>::result + multiplies_result_type; + return inner_product(first1, last1, first2, init, + __gnu_parallel::plus<T, multiplies_result_type>(), + __gnu_parallel::multiplies<value_type1, value_type2>(), + parallelism_tag); } template<typename InputIterator1, typename InputIterator2, typename T> @@ -252,10 +259,16 @@ namespace __parallel inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init) { - typedef iterator_traits<InputIterator1> traits_type; - typedef typename traits_type::value_type value_type; - return inner_product(first1, last1, first2, init, std::plus<value_type>(), - std::multiplies<value_type>()); + typedef iterator_traits<InputIterator1> traits_type1; + typedef typename traits_type1::value_type value_type1; + typedef iterator_traits<InputIterator2> traits_type2; + typedef typename traits_type2::value_type value_type2; + + typedef typename __gnu_parallel::multiplies<value_type1, value_type2>::result + multiplies_result_type; + return inner_product(first1, last1, first2, init, + __gnu_parallel::plus<T, multiplies_result_type>(), + __gnu_parallel::multiplies<value_type1, value_type2>()); } // Sequential fallback. diff --git a/libstdc++-v3/include/parallel/partial_sum.h b/libstdc++-v3/include/parallel/partial_sum.h index 31ded50733b..fbba6860184 100644 --- a/libstdc++-v3/include/parallel/partial_sum.h +++ b/libstdc++-v3/include/parallel/partial_sum.h @@ -190,7 +190,7 @@ namespace __gnu_parallel default: // Partial_sum algorithm not implemented. _GLIBCXX_PARALLEL_ASSERT(0); - return end; + return result + n; } } } |