diff --git a/doc/reference.qbk b/doc/reference.qbk index 98c66d93..6afee1e2 100644 --- a/doc/reference.qbk +++ b/doc/reference.qbk @@ -53,6 +53,7 @@ Header: `` * [funcref boost::compute::find_if find_if()] * [funcref boost::compute::find_if_not find_if_not()] * [funcref boost::compute::for_each for_each()] +* [funcref boost::compute::for_each_n for_each_n()] * [funcref boost::compute::gather gather()] * [funcref boost::compute::generate generate()] * [funcref boost::compute::generate_n generate_n()] diff --git a/include/boost/compute/algorithm.hpp b/include/boost/compute/algorithm.hpp index df0b076e..39f51639 100644 --- a/include/boost/compute/algorithm.hpp +++ b/include/boost/compute/algorithm.hpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include diff --git a/include/boost/compute/algorithm/for_each_n.hpp b/include/boost/compute/algorithm/for_each_n.hpp new file mode 100644 index 00000000..040c223e --- /dev/null +++ b/include/boost/compute/algorithm/for_each_n.hpp @@ -0,0 +1,35 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2013-2014 Kyle Lutz +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// See http://kylelutz.github.com/compute for more information. +//---------------------------------------------------------------------------// + +#ifndef BOOST_COMPUTE_ALGORITHM_FOR_EACH_N_HPP +#define BOOST_COMPUTE_ALGORITHM_FOR_EACH_N_HPP + +#include + +namespace boost { +namespace compute { + +/// Calls \p function on each element in the range [\p first, \p first +/// \c + \p count). +/// +/// \see for_each() +template +inline UnaryFunction for_each_n(InputIterator first, + Size count, + UnaryFunction function, + command_queue &queue = system::default_queue()) +{ + return ::boost::compute::for_each(first, first + count, function, queue); +} + +} // end compute namespace +} // end boost namespace + +#endif // BOOST_COMPUTE_ALGORITHM_FOR_EACH_N_HPP diff --git a/test/test_for_each.cpp b/test/test_for_each.cpp index 10ec9bea..e1439d28 100644 --- a/test/test_for_each.cpp +++ b/test/test_for_each.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "context_setup.hpp" @@ -30,4 +31,14 @@ BOOST_AUTO_TEST_CASE(for_each_nop) bc::for_each(vector.begin(), vector.end(), nop, queue); } +BOOST_AUTO_TEST_CASE(for_each_n_nop) +{ + bc::vector vector(4, context); + bc::iota(vector.begin(), vector.end(), 0); + + BOOST_COMPUTE_FUNCTION(void, nop, (int ignored), {}); + + bc::for_each_n(vector.begin(), vector.size(), nop, queue); +} + BOOST_AUTO_TEST_SUITE_END()