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

Add views::successor #1517

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
116 changes: 116 additions & 0 deletions include/range/v3/view/successor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/// \file
// Range v3 library
//
// Copyright Semir Vrana 2020-present
//
// 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_VIEW_SUCCESSOR_HPP
#define RANGES_V3_VIEW_SUCCESSOR_HPP

#include <type_traits>
#include <utility>

#include <meta/meta.hpp>

#include <range/v3/range_fwd.hpp>

#include <range/v3/functional/invoke.hpp>
#include <range/v3/iterator/unreachable_sentinel.hpp>
#include <range/v3/range/access.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/range/traits.hpp>
#include <range/v3/utility/optional.hpp>
#include <range/v3/utility/semiregular_box.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/view/facade.hpp>

#include <range/v3/detail/prologue.hpp>

namespace ranges
{
/// \addtogroup group-views
/// @{

/// Iteratively calls a function on an initial value
/// successor_view(f, x) -> [x, f(x), f(f(x)), ...]
template<typename G, typename T>
struct successor_view : view_facade<successor_view<G, T>, infinite>
{
private:
friend range_access;
using result_t = T;
semiregular_box_t<G> gen_;
semiregular_box_t<T> val_;
struct cursor
{
private:
successor_view * view_;

public:
cursor() = default;
explicit cursor(successor_view * view)
: view_(view)
{}
const result_t & read() const
{
return view_->val_;
}
void next()
{
view_->val_ = ranges::invoke(view_->gen_, std::move(view_->val_));
}
};
cursor begin_cursor()
{
return cursor{this};
}
unreachable_sentinel_t end_cursor() const
{
return {};
}

public:
successor_view() = default;
explicit successor_view(G g, T x)
: gen_(std::move(g))
, val_(std::move(x))
{}
const result_t & cached()
{
return *val_;
}
};

namespace views
{
struct successor_fn
{
template<typename G, typename T>
auto operator()(G g, T x) const -> CPP_ret(successor_view<G, T>)( //
requires invocable<G &, T &&> && copy_constructible<G> &&
copy_constructible<T> &&
assignable_from<T &, invoke_result_t<G &, T &&>>)
{
return successor_view<G, T>{std::move(g), std::move(x)};
sv1990 marked this conversation as resolved.
Show resolved Hide resolved
}
};

/// \relates successor_fn
/// \ingroup group-views
RANGES_INLINE_VARIABLE(successor_fn, successor)
} // namespace views
/// \@}
} // namespace ranges

#include <range/v3/detail/epilogue.hpp>
#include <range/v3/detail/satisfy_boost_range.hpp>
RANGES_SATISFY_BOOST_RANGE(::ranges::successor_view)

#endif
1 change: 1 addition & 0 deletions test/view/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rv3_add_test(test.view.span view.span span.cpp)
rv3_add_test(test.view.split view.split split.cpp)
rv3_add_test(test.view.stride view.stride stride.cpp)
rv3_add_test(test.view.subrange view.subrange subrange.cpp)
rv3_add_test(test.view.successor view.successor successor.cpp)
rv3_add_test(test.view.tail view.tail tail.cpp)
rv3_add_test(test.view.take view.take take.cpp)
rv3_add_test(test.view.take_exactly view.take_exactly take_exactly.cpp)
Expand Down
62 changes: 62 additions & 0 deletions test/view/successor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Range v3 library
//
// Copyright Semir Vrana 2020-present
//
// 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

#include <range/v3/core.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/successor.hpp>
#include <range/v3/view/take_exactly.hpp>

#include "../simple_test.hpp"
#include "../test_utils.hpp"

namespace views = ranges::views;

int main()
{
{
auto powers_of_10 = views::successor([](int x) { return x * 10; }, 1);
CPP_assert(ranges::input_range<decltype(powers_of_10)> &&
ranges::view_<decltype(powers_of_10)>);
check_equal(powers_of_10 | views::take_exactly(5), {1, 10, 100, 1000, 10000});
}

// Test that we only call the function once for each dereferenceable position
{
int i = 0;
auto rng = views::successor(
[&i](int n) {
++i;
return n + 1;
},
0);
auto rng2 = std::move(rng);
auto it = rng2.begin();
CHECK(i == 0);
CHECK(*it == 0);
CHECK(i == 0);
++it;
CHECK(i == 1);
CHECK(*it == 1);
CHECK(i == 1);
}

// Test that skipping past positions works correctly
{
auto times_10 = [](int x) { return x * 10; };
auto rng = ranges::views::successor(times_10, 1) //
| ranges::views::drop_exactly(3) //
| ranges::views::take_exactly(2);

check_equal(rng, {1000, 10000});
}

return test_result();
}