1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_COUNT_IF_WITH_REDUCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_COUNT_IF_WITH_REDUCE_HPP
14 #include <boost/compute/algorithm/reduce.hpp>
15 #include <boost/compute/iterator/transform_iterator.hpp>
16 #include <boost/compute/types/fundamental.hpp>
22 template<class Predicate, class Arg>
23 struct invoked_countable_predicate
25 invoked_countable_predicate(Predicate p, Arg a)
26 : predicate(p), arg(a)
34 template<class Predicate, class Arg>
35 inline meta_kernel& operator<<(meta_kernel &kernel,
36 const invoked_countable_predicate<Predicate, Arg> &expr)
38 return kernel << "(" << expr.predicate(expr.arg) << " ? 1 : 0)";
41 // the countable_predicate wraps Predicate and converts its result from
42 // bool to ulong so that it can be used with reduce()
43 template<class Predicate>
44 struct countable_predicate
46 typedef ulong_ result_type;
48 countable_predicate(Predicate predicate)
49 : m_predicate(predicate)
54 invoked_countable_predicate<Predicate, Arg> operator()(const Arg &arg) const
56 return invoked_countable_predicate<Predicate, Arg>(m_predicate, arg);
59 Predicate m_predicate;
62 // counts the number of elements matching predicate using reduce()
63 template<class InputIterator, class Predicate>
64 inline size_t count_if_with_reduce(InputIterator first,
69 countable_predicate<Predicate> reduce_predicate(predicate);
72 ::boost::compute::reduce(
73 ::boost::compute::make_transform_iterator(first, reduce_predicate),
74 ::boost::compute::make_transform_iterator(last, reduce_predicate),
76 ::boost::compute::plus<ulong_>(),
80 return static_cast<size_t>(count);
83 } // end detail namespace
84 } // end compute namespace
85 } // end boost namespace
87 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_COUNT_IF_WITH_REDUCE_HPP