| 1 |
/// \file |
| 2 |
// Range v3 library |
| 3 |
// |
| 4 |
// Copyright Eric Niebler 2013-present |
| 5 |
// |
| 6 |
// Use, modification and distribution is subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying |
| 8 |
// file LICENSE_1_0.txt or copy at |
| 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 10 |
// |
| 11 |
// Project home: https://github.com/ericniebler/range-v3 |
| 12 |
// |
| 13 |
|
| 14 |
#ifndef RANGES_V3_ACTION_SPLIT_WHEN_HPP |
| 15 |
#define RANGES_V3_ACTION_SPLIT_WHEN_HPP |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include <meta/meta.hpp> |
| 20 |
|
| 21 |
#include <range/v3/range_fwd.hpp> |
| 22 |
|
| 23 |
#include <range/v3/action/action.hpp> |
| 24 |
#include <range/v3/action/concepts.hpp> |
| 25 |
#include <range/v3/functional/bind_back.hpp> |
| 26 |
#include <range/v3/functional/invoke.hpp> |
| 27 |
#include <range/v3/iterator/concepts.hpp> |
| 28 |
#include <range/v3/iterator/traits.hpp> |
| 29 |
#include <range/v3/range/conversion.hpp> |
| 30 |
#include <range/v3/utility/static_const.hpp> |
| 31 |
#include <range/v3/view/split_when.hpp> |
| 32 |
|
| 33 |
#include <range/v3/detail/prologue.hpp> |
| 34 |
|
| 35 |
namespace ranges |
| 36 |
{ |
| 37 |
/// \addtogroup group-actions |
| 38 |
/// @{ |
| 39 |
namespace actions |
| 40 |
{ |
| 41 |
struct split_when_fn |
| 42 |
{ |
| 43 |
template<typename Rng> |
| 44 |
using split_value_t = |
| 45 |
meta::if_c<(bool)ranges::container<Rng>, // |
| 46 |
uncvref_t<Rng>, std::vector<range_value_t<Rng>>>; |
| 47 |
|
| 48 |
template<typename Fun> |
| 49 |
constexpr auto operator()(Fun fun) const |
| 50 |
{ |
| 51 |
return make_action_closure( |
| 52 |
bind_back(split_when_fn{}, static_cast<Fun &&>(fun))); |
| 53 |
} |
| 54 |
|
| 55 |
// BUGBUG something is not right with the actions. It should be possible |
| 56 |
// to move a container into a split and have elements moved into the result. |
| 57 |
template(typename Rng, typename Fun)( |
| 58 |
requires forward_range<Rng> AND |
| 59 |
invocable<Fun &, iterator_t<Rng>, sentinel_t<Rng>> AND |
| 60 |
invocable<Fun &, iterator_t<Rng>, iterator_t<Rng>> AND |
| 61 |
copy_constructible<Fun> AND |
| 62 |
convertible_to<invoke_result_t<Fun &, iterator_t<Rng>, |
| 63 |
sentinel_t<Rng>>, |
| 64 |
std::pair<bool, iterator_t<Rng>>>) |
| 65 |
std::vector<split_value_t<Rng>> operator()(Rng && rng, Fun fun) const |
| 66 |
{ |
| 67 |
return views::split_when(rng, std::move(fun)) | |
| 68 |
to<std::vector<split_value_t<Rng>>>(); |
| 69 |
} |
| 70 |
|
| 71 |
template(typename Rng, typename Fun)( |
| 72 |
requires forward_range<Rng> AND |
| 73 |
predicate<Fun const &, range_reference_t<Rng>> AND |
| 74 |
copy_constructible<Fun>) |
| 75 |
std::vector<split_value_t<Rng>> operator()(Rng && rng, Fun fun) const |
| 76 |
{ |
| 77 |
return views::split_when(rng, std::move(fun)) | |
| 78 |
to<std::vector<split_value_t<Rng>>>(); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
/// \relates actions::split_when_fn |
| 83 |
RANGES_INLINE_VARIABLE(split_when_fn, split_when) |
| 84 |
} // namespace actions |
| 85 |
/// @} |
| 86 |
} // namespace ranges |
| 87 |
|
| 88 |
#include <range/v3/detail/epilogue.hpp> |
| 89 |
|
| 90 |
#endif |