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

Inclusive scan #716

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions include/range/v3/numeric/inclusive_scan.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/// \file
// Range v3 library
//
// Copyright Rostislav Khlebnikov 2017
// Copyright Eric Niebler 2013-2014
//
// Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3
//
#ifndef RANGES_V3_NUMERIC_inclusive_scan_HPP
#define RANGES_V3_NUMERIC_inclusive_scan_HPP

#include <meta/meta.hpp>
#include <range/v3/begin_end.hpp>
#include <range/v3/range_traits.hpp>
#include <range/v3/range_concepts.hpp>
#include <range/v3/algorithm/tagspec.hpp>
#include <range/v3/utility/functional.hpp>
#include <range/v3/utility/iterator_traits.hpp>
#include <range/v3/utility/iterator_concepts.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/utility/unreachable.hpp>
#include <range/v3/numeric/partial_sum.hpp>

namespace ranges
{
inline namespace v3
{
namespace detail {
template<typename AccT, typename BOp, typename I1, typename I2>
using IndirectInvocableAndAssignable = meta::strict_and<
IndirectRegularInvocable<BOp, I1, I2>,
Assignable<detail::decay_t<AccT>&, indirect_result_of_t<BOp&(I1, I2)>>>;
}

template<typename I, typename O, typename BOp = plus, typename P = ident, typename T = void,
typename IProj = projected<projected<I, detail::as_value_type_t<I>>, P>,
typename AccT = meta::if_c<std::is_void<T>::value, value_type_t<IProj>, T>>
using InclusiveScannable = meta::strict_and<
InputIterator<I>,
OutputIterator<O, AccT const&>,
MoveConstructible<AccT>,
detail::IndirectInvocableAndAssignable<AccT, BOp, AccT*, AccT*>,
detail::IndirectInvocableAndAssignable<AccT, BOp, AccT*, IProj>,
detail::IndirectInvocableAndAssignable<AccT, BOp, IProj, IProj>>;


struct inclusive_scan_fn
{
// No-init version
template<typename I, typename S, typename O, typename S2,
typename BOp = plus, typename P = ident,
CONCEPT_REQUIRES_(Sentinel<S, I>() && Sentinel<S2, O>() &&
InclusiveScannable<I, O, BOp, P>())>
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S end, O result, S2 end_result, BOp bop = BOp{}, P proj = P{}) const
{
return detail::partial_sum_no_check(std::move(begin), std::move(end),
std::move(result), std::move(end_result),
std::move(bop), std::move(proj));
}

template<typename I, typename S, typename O, typename BOp = plus,
typename P = ident,
CONCEPT_REQUIRES_(Sentinel<S, I>() && InclusiveScannable<I, O, BOp, P>())>
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S end, O result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(std::move(begin), std::move(end), std::move(result),
unreachable{}, std::move(bop), std::move(proj));
}

template<typename Rng, typename ORef, typename BOp = plus,
typename P = ident, typename I = iterator_t<Rng>,
typename O = uncvref_t<ORef>,
CONCEPT_REQUIRES_(Range<Rng>() && InclusiveScannable<I, O, BOp, P>())>
tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)>
operator()(Rng && rng, ORef && result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), static_cast<ORef&&>(result),
std::move(bop), std::move(proj));
}

template<typename Rng, typename ORng, typename BOp = plus,
typename P = ident, typename I = iterator_t<Rng>,
typename O = iterator_t<ORng>,
CONCEPT_REQUIRES_(Range<Rng>() && Range<ORng>() &&
InclusiveScannable<I, O, BOp, P>())>
tagged_pair<tag::in(safe_iterator_t<Rng>),
tag::out(safe_iterator_t<ORng>)>
operator()(Rng && rng, ORng && result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), begin(result), end(result),
std::move(bop), std::move(proj));
}

// init version
template<typename I, typename S, typename O, typename S2,
typename BOp, typename T, typename P = ident,
CONCEPT_REQUIRES_(Sentinel<S, I>() && Sentinel<S2, O>() &&
InclusiveScannable<I, O, BOp, P, T>())>
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S end, O result, S2 end_result, BOp bop, T init, P proj = P{}) const
{
coerce<value_type_t<I>> v;

if(begin != end && result != end_result)
{
for(; begin != end && result != end_result;
++begin, ++result)
{
auto &&cur = v(*begin);
init = invoke(bop, init, invoke(proj, cur));
*result = init;
}
}
return {begin, result};
}

template<typename I, typename S, typename O, typename BOp,
typename T, typename P = ident,
CONCEPT_REQUIRES_(Sentinel<S, I>() && InclusiveScannable<I, O, BOp, P, T>())>
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S end, O result, BOp bop, T init, P proj = P{}) const
{
return (*this)(std::move(begin), std::move(end), std::move(result),
unreachable{}, std::move(bop), std::move(init), std::move(proj));
}

template<typename Rng, typename ORef, typename BOp, typename T,
typename P = ident, typename I = iterator_t<Rng>,
typename O = uncvref_t<ORef>,
CONCEPT_REQUIRES_(Range<Rng>() && InclusiveScannable<I, O, BOp, P, T>())>
tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)>
operator()(Rng && rng, ORef && result, BOp bop, T init, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), static_cast<ORef&&>(result),
std::move(bop), std::move(init), std::move(proj));
}

template<typename Rng, typename ORng, typename BOp, typename T,
typename P = ident, typename I = iterator_t<Rng>,
typename O = iterator_t<ORng>,
CONCEPT_REQUIRES_(Range<Rng>() && Range<ORng>() &&
InclusiveScannable<I, O, BOp, P, T>())>
tagged_pair<tag::in(safe_iterator_t<Rng>),
tag::out(safe_iterator_t<ORng>)>
operator()(Rng && rng, ORng && result, BOp bop, T init, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), begin(result), end(result),
std::move(bop), std::move(init), std::move(proj));
}

};

RANGES_INLINE_VARIABLE(inclusive_scan_fn, inclusive_scan)
}
}

#endif
61 changes: 35 additions & 26 deletions include/range/v3/numeric/partial_sum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ namespace ranges
};
template<typename I>
using as_value_type_t = composed<as_lvalue_fn, coerce<value_type_t<I>>>;

template<typename I, typename S1, typename O, typename S2,
typename BOp, typename P>
tagged_pair<tag::in(I), tag::out(O)>
partial_sum_no_check(I begin, S1 end, O result, S2 end_result, BOp bop, P proj)
{
using X = projected<projected<I, detail::as_value_type_t<I>>, P>;
coerce<value_type_t<I>> val_i;
coerce<value_type_t<X>> val_x;
if(begin != end && result != end_result)
{
auto &&cur1 = val_i(*begin);
value_type_t<X> t(invoke(proj, cur1));
*result = t;
for(++begin, ++result; begin != end && result != end_result;
++begin, ++result)
{
auto &&cur2 = val_i(*begin);
t = val_x(invoke(bop, t, invoke(proj, cur2)));
*result = t;
}
}
return {begin, result};
}
}
/// \endcond

Expand All @@ -62,30 +86,15 @@ namespace ranges

struct partial_sum_fn
{
template<typename I, typename S1, typename O, typename S2,
template<typename I, typename S, typename O, typename S2,
typename BOp = plus, typename P = ident,
CONCEPT_REQUIRES_(Sentinel<S1, I>() && Sentinel<S2, O>() &&
CONCEPT_REQUIRES_(Sentinel<S, I>() && Sentinel<S2, O>() &&
PartialSummable<I, O, BOp, P>())>
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S1 end, O result, S2 end_result, BOp bop = BOp{}, P proj = P{}) const
operator()(I begin, S end, O result, S2 end_result, BOp bop = BOp{}, P proj = P{}) const
{
using X = projected<projected<I, detail::as_value_type_t<I>>, P>;
coerce<value_type_t<I>> val_i;
coerce<value_type_t<X>> val_x;
if(begin != end && result != end_result)
{
auto &&cur1 = val_i(*begin);
value_type_t<X> t(invoke(proj, cur1));
*result = t;
for(++begin, ++result; begin != end && result != end_result;
++begin, ++result)
{
auto &&cur2 = val_i(*begin);
t = val_x(invoke(bop, t, invoke(proj, cur2)));
*result = t;
}
}
return {begin, result};
return detail::partial_sum_no_check(std::move(begin), std::move(end), std::move(result),
std::move(end_result), std::move(bop), std::move(proj));
}

template<typename I, typename S, typename O, typename BOp = plus,
Expand All @@ -94,8 +103,8 @@ namespace ranges
tagged_pair<tag::in(I), tag::out(O)>
operator()(I begin, S end, O result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(std::move(begin), std::move(end), std::move(result),
unreachable{}, std::move(bop), std::move(proj));
return detail::partial_sum_no_check(std::move(begin), std::move(end), std::move(result),
unreachable{}, std::move(bop), std::move(proj));
}

template<typename Rng, typename ORef, typename BOp = plus,
Expand All @@ -105,8 +114,8 @@ namespace ranges
tagged_pair<tag::in(safe_iterator_t<Rng>), tag::out(O)>
operator()(Rng && rng, ORef && result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), static_cast<ORef&&>(result),
std::move(bop), std::move(proj));
return detail::partial_sum_no_check(begin(rng), end(rng), static_cast<ORef&&>(result),
unreachable{}, std::move(bop), std::move(proj));
}

template<typename Rng, typename ORng, typename BOp = plus,
Expand All @@ -118,8 +127,8 @@ namespace ranges
tag::out(safe_iterator_t<ORng>)>
operator()(Rng && rng, ORng && result, BOp bop = BOp{}, P proj = P{}) const
{
return (*this)(begin(rng), end(rng), begin(result), end(result),
std::move(bop), std::move(proj));
return detail::partial_sum_no_check(begin(rng), end(rng), begin(result), end(result),
std::move(bop), std::move(proj));
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/numeric/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ add_test(test.num.iota num.iota)

add_executable(num.partial_sum partial_sum.cpp)
add_test(test.num.partial_sum num.partial_sum)

add_executable(num.inclusive_scan inclusive_scan.cpp)
add_test(test.num.inclusive_scan num.inclusive_scan)
Loading