Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N3960 copy_if and copy_n implemented and tested #1147

Merged
merged 5 commits into from Jun 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
303 changes: 274 additions & 29 deletions hpx/stl/detail/copy.hpp
Expand Up @@ -25,30 +25,37 @@ namespace hpx { namespace parallel
// copy
namespace detail
{
template<typename ExPolicy, typename InIter, typename OutIter,
typename IterTag>
template<typename ExPolicy, typename InIter, typename OutIter>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy_seq(ExPolicy const&, InIter first, InIter last, OutIter dest,
IterTag)
copy(ExPolicy const&, InIter first, InIter last, OutIter dest,
boost::mpl::true_)
{
typedef typename std::iterator_traits<InIter>::iterator_category
category;

std::cout << "Sequential Execution" << std::endl;
try {
return detail::algorithm_result<ExPolicy, OutIter>::get(
std::copy(first,last,dest));
}
catch(std::bad_alloc const& e) {
throw e;
boost::throw_exception(e);
}
catch (...) {
throw hpx::exception_list(boost::current_exception());
boost::throw_exception(
hpx::exception_list(boost::current_exception())
);
}
}

template<typename ExPolicy, typename InIter, typename OutIter,
typename IterTag>
template<typename ExPolicy, typename InIter, typename OutIter>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy(ExPolicy const& policy, InIter first, InIter last, OutIter dest,
IterTag category)
boost::mpl::false_)
{
std::iterator_traits<InIter>::iterator_category category;

std::cout << "Parallel execution" << std::endl;
typedef boost::tuple<InIter, OutIter> iterator_tuple;
typedef detail::zip_iterator<iterator_tuple> zip_iterator;
typedef typename zip_iterator::reference reference;
Expand All @@ -66,52 +73,166 @@ namespace hpx { namespace parallel
boost::mpl::false_()));
}

template<typename InIter, typename OutIter>
OutIter copy(execution_policy const& policy,
InIter first, InIter last, OutIter dest, boost::mpl::false_)
{
switch (detail::which(policy))
{
case detail::execution_policy_enum::sequential:
return detail::copy(
*policy.get<sequential_execution_policy>(),
first, last, dest, boost::mpl::true_());

case detail::execution_policy_enum::parallel:
return detail::copy(
*policy.get<parallel_execution_policy>(),
first, last, dest, boost::mpl::false_());

case detail::execution_policy_enum::vector:
return detail::copy(
*policy.get<vector_execution_policy>(),
first, last, dest, boost::mpl::false_());

case detail::execution_policy_enum::task:
return detail::copy(par,
first, last, dest, boost::mpl::false_());

default:
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"hpx::parallel::detail::copy",
"Not supported execution policy");
break;
}
}

template<typename InIter, typename OutIter>
OutIter copy(execution_policy const& policy,
InIter first, InIter last, OutIter dest, boost::mpl::true_)
{
return detail::copy(sequential_execution_policy(),
first, last, dest, boost::mpl::true_());
}
}

template<typename ExPolicy, typename InIter, typename OutIter>
inline typename boost::enable_if<
is_execution_policy<ExPolicy>,
typename detail::algorithm_result<ExPolicy, OutIter>::type
>::type
copy(ExPolicy && policy, InIter first, InIter last, OutIter dest)
{
typedef typename std::iterator_traits<InIter>::iterator_category
iterator_category;

BOOST_STATIC_ASSERT_MSG(
boost::is_base_of<std::input_iterator_tag,
typename std::iterator_traits<InIter>::iterator_category>::value,
"Required at least input iterator.");

typedef boost::mpl::or_<
is_sequential_execution_policy<ExPolicy>,
boost::is_same<std::input_iterator_tag, iterator_category>
>::type is_seq;

return detail::copy(
policy,
first, last, dest, is_seq());
}

/////////////////////////////////////////////////////////////////////////////
// copy_n
namespace detail
{
template<typename ExPolicy, typename InIter, typename OutIter,
typename IterTag>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy_n_seq(ExPolicy const&, InIter first, std::size_t count, OutIter dest,
IterTag)
{
try{
return detail::algorithm_result<ExPolicy, OutIter>::get(
std::copy_n(first,count,dest));
}
catch(std::bad_alloc const& e) {
boost::throw_exception(e);
}
catch(...) {
boost::throw_exception(
hpx::exception_list(boost::current_exception())
);
}
}

template<typename ExPolicy, typename InIter, typename OutIter,
typename IterTag>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy_n(ExPolicy const& policy, InIter first, std::size_t count, OutIter dest,
IterTag category)
{
typedef boost::tuple<InIter,OutIter> iterator_tuple;
typedef detail::zip_iterator<iterator_tuple> zip_iterator;
typedef typename zip_iterator::reference reference;
typedef
typename detail::algorithm_result<ExPolicy, OutIter>::type
result_type;

return get_iter<1, result_type>(
for_each_n(policy,
detail::make_zip_iterator(boost::make_tuple(first,dest)),
count,
[](reference it) {
*boost::get<1>(it) = *boost::get<0>(it);
},
category));
}

template<typename ExPolicy, typename InIter, typename OutIter>
typename boost::enable_if<
is_parallel_execution_policy<ExPolicy>,
typename detail::algorithm_result<ExPolicy, OutIter>::type
>::type
copy(ExPolicy const& policy, InIter first, InIter last, OutIter dest,
std::input_iterator_tag category)
copy_n(ExPolicy const& policy, InIter first, std::size_t count,
OutIter dest, std::input_iterator_tag category)
{
return detail::copy_seq(policy, first, last, dest, category);
return detail::copy_n_seq(policy, first, count, dest, category);
}

template<typename InIter, typename OutIter, typename IterTag>
OutIter copy(sequential_execution_policy const& policy,
InIter first, InIter last, OutIter dest, IterTag category)
OutIter copy_n(sequential_execution_policy const& policy,
InIter first, std::size_t count, OutIter dest, IterTag category)
{
return detail::copy_seq(policy, first, last, dest, category);
return detail::copy_n_seq(policy, first, count, dest, category);
}

template<typename InIter, typename OutIter, typename IterTag>
OutIter copy(execution_policy const& policy,
InIter first, InIter last, OutIter dest, IterTag category)
OutIter copy_n(execution_policy const& policy,
InIter first, std::size_t count, OutIter dest, IterTag category)
{
switch (detail::which(policy))
{
case detail::execution_policy_enum::sequential:
return detail::copy(
return detail::copy_n(
*policy.get<sequential_execution_policy>(),
first, last, dest, category);
first, count, dest, category);

case detail::execution_policy_enum::parallel:
return detail::copy(
return detail::copy_n(
*policy.get<parallel_execution_policy>(),
first, last, dest, category);
first, count, dest, category);

case detail::execution_policy_enum::vector:
return detail::copy(
*policy.get<vector_execution_policy>(),
first, last, dest, category);
return detail::copy_n(
*policy.get<parallel_execution_policy>(),
first, count, dest, category);

case detail::execution_policy_enum::task:
return detail::copy(par,
first, last, dest, category);
return detail::copy_n(par,
first, count, dest, category);

default:
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"hpx::parallel::detail::transform",
"hpx::parallel::detail::copy_n",
"Not supported execution policy");
break;
}
Expand All @@ -123,15 +244,139 @@ namespace hpx { namespace parallel
is_execution_policy<ExPolicy>,
typename detail::algorithm_result<ExPolicy, OutIter>::type
>::type
copy(ExPolicy&& policy, InIter first, InIter last, OutIter dest)
copy_n(ExPolicy && policy, InIter first, std::size_t count, OutIter dest)
{
BOOST_STATIC_ASSERT_MSG(
boost::is_base_of<std::input_iterator_tag,
typename std::iterator_traits<InIter>::iterator_category>::value,
"Required at least input iterator.");

typedef boost::mpl::or_<
boost::is_same<sequential_execution_policy, ExPolicy>,
boost::is_same<std::input_iterator_tag, iterator_category>
>::type is_seq;

std::iterator_traits<InIter>::iterator_category category;
return detail::copy_n(policy, first, count, dest, category);
}

/////////////////////////////////////////////////////////////////////////////
// copy_if
namespace detail
{
template<typename ExPolicy, typename InIter, typename OutIter,
typename F, typename IterTag>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy_if_seq(ExPolicy const&, InIter first, InIter last, OutIter dest,
F && f, IterTag)
{
try{
return detail::algorithm_result<ExPolicy, OutIter>::get(
std::copy_if(first,last,dest,std::forward<F>(f)));
}
catch(std::bad_alloc const& e) {
boost::throw_exception(e);
}
catch(...) {
boost::throw_exception(
hpx::exception_list(boost::current_exception())
);
}
}

template <typename ExPolicy, typename InIter, typename OutIter,
typename F, typename IterTag>
typename detail::algorithm_result<ExPolicy, OutIter>::type
copy_if(ExPolicy const& policy, InIter first, InIter last, OutIter dest,
F && f, IterTag category)
{
typedef boost::tuple<InIter, OutIter> iterator_tuple;
typedef detail::zip_iterator<iterator_tuple> zip_iterator;
typedef typename zip_iterator::reference reference;
typedef
typename detail::algorithm_result<ExPolicy, OutIter>::type
result_type;

return get_iter<1,result_type>(
for_each_n(policy,
detail::make_zip_iterator(boost::make_tuple(first, dest)),
std::distance(first,last),
[f](reference it) {
if(f(*boost::get<0>(it)))
*boost::get<1>(it)=*boost::get<0>(it);
},
category));
}

template <typename ExPolicy, typename InIter, typename OutIter, typename F>
typename boost::enable_if<
is_parallel_execution_policy<ExPolicy>,
typename detail::algorithm_result<ExPolicy, OutIter>::type
>::type
copy_if (ExPolicy const& policy, InIter first, InIter last,
OutIter dest, F && f, std::input_iterator_tag category)
{
return copy_if_seq(policy, first, last, dest,
std::forward<F>(f), category);
}

template<typename InIter, typename OutIter, typename F, typename IterTag>
OutIter copy_if(sequential_execution_policy const& policy,
InIter first, InIter last, OutIter dest, F && f, IterTag category)
{
return detail::copy_if_seq(policy, first, last, dest,
std::forward<F>(f), category);
}

template <typename InIter, typename OutIter, typename F, typename IterTag>
OutIter copy_if(execution_policy const& policy,
InIter first, InIter last, OutIter dest, F && f, IterTag category)
{
switch(detail::which(policy))
{
case detail::execution_policy_enum::sequential:
return detail::copy_if(
*policy.get<sequential_execution_policy>(),
first, last, dest, std::forward<F>(f), category);

case detail::execution_policy_enum::parallel:
return detail::copy_if(
*policy.get<parallel_execution_policy>(),
first, last, dest, std::forward<F>(f), category);

case detail::execution_policy_enum::vector:
return detail::copy_if(
*policy.get<vector_execution_policy>(),
first, last, dest, std::forward<F>(f), category);

case detail::execution_policy_enum::task:
return detail::copy_if(par,
first, last, dest, std::forward<F>(f), category);

default:
HPX_THROW_EXCEPTION(hpx::bad_parameter,
"hpx::parallel::detail::copy_if",
"Not supported execution policy");
break;
}
}
}

template <typename ExPolicy, typename InIter, typename OutIter, typename F>
inline typename boost::enable_if<
is_execution_policy<ExPolicy>,
typename detail::algorithm_result<ExPolicy, OutIter>::type
>::type
copy_if(ExPolicy&& policy, InIter first, InIter last, OutIter dest, F && f)
{
BOOST_STATIC_ASSERT_MSG(
boost::is_base_of<std::input_iterator_tag,
typename std::iterator_traits<InIter>::iterator_category>::value,
"Required at least input iterator.");

std::iterator_traits<InIter>::iterator_category category;
return detail::copy(policy, first, last, dest, category);
return detail::copy_if(policy, first, last, dest,
std::forward<F>(f), category);
}
}}

Expand Down