-
Notifications
You must be signed in to change notification settings - Fork 444
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
Adding view::remove_when. #1219
Open
brevzin
wants to merge
5
commits into
ericniebler:master
Choose a base branch
from
brevzin:remove-when
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/// \file | ||
// Range v3 library | ||
// | ||
// Copyright Barry Revzin 2019-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_DETAIL_DELIMITER_SPECIFIER_HPP | ||
#define RANGES_V3_DETAIL_DELIMITER_SPECIFIER_HPP | ||
|
||
#include <range/v3/algorithm/find_if_not.hpp> | ||
#include <range/v3/range/concepts.hpp> | ||
#include <range/v3/utility/semiregular.hpp> | ||
|
||
namespace ranges | ||
{ | ||
namespace detail | ||
{ | ||
template<typename Pred> | ||
struct delimiter_specifier | ||
{ | ||
semiregular_t<Pred> pred_; | ||
|
||
template<typename I, typename S> | ||
auto operator()(I cur, S end) const -> CPP_ret(std::pair<bool, I>)( // | ||
requires Sentinel<S, I>) | ||
{ | ||
auto where = ranges::find_if_not(cur, end, std::ref(pred_)); | ||
return {cur != where, where}; | ||
} | ||
}; | ||
} // namespace detail | ||
} // namespace ranges | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/// \file | ||
// Range v3 library | ||
// | ||
// Copyright Barry Revzin 2019-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_REMOVE_WHEN_HPP | ||
#define RANGES_V3_VIEW_REMOVE_WHEN_HPP | ||
|
||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include <meta/meta.hpp> | ||
|
||
#include <range/v3/range_fwd.hpp> | ||
|
||
#include <range/v3/functional/compose.hpp> | ||
#include <range/v3/functional/invoke.hpp> | ||
#include <range/v3/range/access.hpp> | ||
#include <range/v3/range/concepts.hpp> | ||
#include <range/v3/range/traits.hpp> | ||
#include <range/v3/utility/box.hpp> | ||
#include <range/v3/utility/optional.hpp> | ||
#include <range/v3/utility/semiregular.hpp> | ||
#include <range/v3/utility/static_const.hpp> | ||
#include <range/v3/view/adaptor.hpp> | ||
#include <range/v3/view/view.hpp> | ||
#include <range/v3/detail/delimiter_specifier.hpp> | ||
|
||
RANGES_DISABLE_WARNINGS | ||
|
||
namespace ranges | ||
{ | ||
/// \addtogroup group-views | ||
/// @{ | ||
template<typename Rng, typename Fun> | ||
struct RANGES_EMPTY_BASES remove_when_view | ||
: view_adaptor<remove_when_view<Rng, Fun>, Rng, | ||
is_finite<Rng>::value ? finite : range_cardinality<Rng>::value> | ||
, private box<semiregular_t<Fun>> | ||
{ | ||
remove_when_view() = default; | ||
constexpr remove_when_view(Rng rng, Fun fun) | ||
: remove_when_view::view_adaptor{detail::move(rng)} | ||
, remove_when_view::box(detail::move(fun)) | ||
{} | ||
|
||
private: | ||
friend range_access; | ||
bool zero_; | ||
|
||
struct adaptor : adaptor_base | ||
{ | ||
adaptor() = default; | ||
constexpr adaptor(remove_when_view & rng) noexcept | ||
: rng_(&rng) | ||
{} | ||
static constexpr iterator_t<Rng> begin(remove_when_view & rng) | ||
{ | ||
return *rng.begin_; | ||
} | ||
constexpr void next(iterator_t<Rng> & it) const | ||
{ | ||
RANGES_ASSERT(it != ranges::end(rng_->base())); | ||
rng_->satisfy_forward(++it); | ||
} | ||
void advance() = delete; | ||
void distance_to() = delete; | ||
|
||
private: | ||
remove_when_view * rng_; | ||
}; | ||
constexpr adaptor begin_adaptor() | ||
{ | ||
cache_begin(); | ||
return {*this}; | ||
} | ||
constexpr adaptor end_adaptor() | ||
{ | ||
return {*this}; | ||
} | ||
|
||
constexpr void satisfy_forward(iterator_t<Rng> & it) | ||
{ | ||
if(zero_) | ||
{ | ||
// if the last match consumed zero elements, we already bumped the | ||
// position so can stop here. | ||
zero_ = false; | ||
return; | ||
} | ||
|
||
auto const last = ranges::end(this->base()); | ||
auto & fun = this->remove_when_view::box::get(); | ||
if(it != last) | ||
{ | ||
auto p = invoke(fun, it, last); | ||
if(p.first) | ||
{ | ||
zero_ = (it == p.second); | ||
it = p.second; | ||
} | ||
} | ||
} | ||
|
||
constexpr void cache_begin() | ||
{ | ||
if(begin_) | ||
return; | ||
auto it = ranges::begin(this->base()); | ||
zero_ = false; | ||
satisfy_forward(it); | ||
begin_.emplace(std::move(it)); | ||
} | ||
|
||
detail::non_propagating_cache<iterator_t<Rng>> begin_; | ||
}; | ||
|
||
#if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17 | ||
CPP_template(typename Rng, typename Fun)(requires CopyConstructible<Fun>) | ||
remove_when_view(Rng &&, Fun) | ||
->remove_when_view<view::all_t<Rng>, Fun>; | ||
#endif | ||
|
||
namespace view | ||
{ | ||
struct remove_when_fn | ||
{ | ||
private: | ||
friend view_access; | ||
template<typename Fun> | ||
static auto bind(remove_when_fn remove_when, Fun && fun) | ||
{ | ||
return make_pipeable(std::bind( | ||
remove_when, std::placeholders::_1, static_cast<Fun &&>(fun))); | ||
} | ||
|
||
public: | ||
template<typename Rng, typename Fun> | ||
constexpr auto operator()(Rng && rng, Fun fun) const | ||
-> CPP_ret(remove_when_view<all_t<Rng>, detail::delimiter_specifier<Fun>>)( // | ||
requires ViewableRange<Rng> && InputRange<Rng> && Predicate< | ||
Fun const &, range_reference_t<Rng>> && CopyConstructible<Fun>) | ||
{ | ||
return {all(static_cast<Rng &&>(rng)), | ||
detail::delimiter_specifier<Fun>{std::move(fun)}}; | ||
} | ||
template<typename Rng, typename Fun> | ||
auto operator()(Rng && rng, Fun fun) const -> CPP_ret( | ||
remove_when_view<all_t<Rng>, Fun>)( // | ||
requires ViewableRange<Rng> && ForwardRange<Rng> && | ||
Invocable<Fun &, iterator_t<Rng>, sentinel_t<Rng>> && | ||
Invocable<Fun &, iterator_t<Rng>, iterator_t<Rng>> && | ||
CopyConstructible<Fun> && ConvertibleTo< | ||
invoke_result_t<Fun &, iterator_t<Rng>, sentinel_t<Rng>>, | ||
std::pair<bool, iterator_t<Rng>>>) | ||
{ | ||
return {all(static_cast<Rng &&>(rng)), std::move(fun)}; | ||
} | ||
}; | ||
|
||
/// \relates remove_when_fn | ||
/// \ingroup group-views | ||
RANGES_INLINE_VARIABLE(view<remove_when_fn>, remove_when) | ||
} // namespace view | ||
/// @} | ||
} // namespace ranges | ||
|
||
RANGES_RE_ENABLE_WARNINGS | ||
|
||
#include <range/v3/detail/satisfy_boost_range.hpp> | ||
RANGES_SATISFY_BOOST_RANGE(::ranges::remove_when_view) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Range v3 library | ||
// | ||
// Copyright Barry Revzin 2019-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 <list> | ||
#include <string> | ||
|
||
#include <range/v3/utility/copy.hpp> | ||
#include <range/v3/view/remove_when.hpp> | ||
|
||
#include "../simple_test.hpp" | ||
#include "../test_utils.hpp" | ||
|
||
int main() | ||
{ | ||
using namespace ranges; | ||
|
||
// a simple predicate behaves just like a worse remove_if | ||
std::string str = "The quick brown fox"; | ||
auto rng0 = view::remove_when(str, [](char c) { return c == ' '; }); | ||
::check_equal(rng0, std::string("Thequickbrownfox")); | ||
|
||
// a silly predicate that actually doesn't remove anything | ||
std::list<int> lst = {1, 2, 3, 4, 5}; | ||
auto rng1 = | ||
view::remove_when(lst, [](auto i, auto) { return std::make_pair(true, i); }); | ||
::check_equal(rng1, {1, 2, 3, 4, 5}); | ||
|
||
// remove 3s and what's after 3 | ||
auto rng2 = view::remove_when( | ||
lst, [](auto i, auto) { return std::make_pair(*i == 3, next(i, 2)); }); | ||
::check_equal(rng2, {1, 2, 5}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase on master and replace
std::bind
withbind_back
, and make this functionconstexpr
. (You should also#include <range/v3/functional/bind_back.hpp>
.)