split_when.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef RANGES_V3_ACTION_SPLIT_WHEN_HPP
  14. #define RANGES_V3_ACTION_SPLIT_WHEN_HPP
  15. #include <vector>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/action/action.hpp>
  19. #include <range/v3/action/concepts.hpp>
  20. #include <range/v3/functional/bind_back.hpp>
  21. #include <range/v3/functional/invoke.hpp>
  22. #include <range/v3/iterator/concepts.hpp>
  23. #include <range/v3/iterator/traits.hpp>
  24. #include <range/v3/range/conversion.hpp>
  25. #include <range/v3/utility/static_const.hpp>
  26. #include <range/v3/view/split_when.hpp>
  27. #include <range/v3/detail/prologue.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-actions
  31. /// @{
  32. namespace actions
  33. {
  34. struct split_when_fn
  35. {
  36. template<typename Rng>
  37. using split_value_t =
  38. meta::if_c<(bool)ranges::container<Rng>, //
  39. uncvref_t<Rng>, std::vector<range_value_t<Rng>>>;
  40. template<typename Fun>
  41. constexpr auto operator()(Fun fun) const
  42. {
  43. return make_action_closure(
  44. bind_back(split_when_fn{}, static_cast<Fun &&>(fun)));
  45. }
  46. // BUGBUG something is not right with the actions. It should be possible
  47. // to move a container into a split and have elements moved into the result.
  48. template(typename Rng, typename Fun)(
  49. requires forward_range<Rng> AND
  50. invocable<Fun &, iterator_t<Rng>, sentinel_t<Rng>> AND
  51. invocable<Fun &, iterator_t<Rng>, iterator_t<Rng>> AND
  52. copy_constructible<Fun> AND
  53. convertible_to<invoke_result_t<Fun &, iterator_t<Rng>,
  54. sentinel_t<Rng>>,
  55. std::pair<bool, iterator_t<Rng>>>)
  56. std::vector<split_value_t<Rng>> operator()(Rng && rng, Fun fun) const
  57. {
  58. return views::split_when(rng, std::move(fun)) |
  59. to<std::vector<split_value_t<Rng>>>();
  60. }
  61. template(typename Rng, typename Fun)(
  62. requires forward_range<Rng> AND
  63. predicate<Fun const &, range_reference_t<Rng>> AND
  64. copy_constructible<Fun>)
  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. /// \relates actions::split_when_fn
  72. RANGES_INLINE_VARIABLE(split_when_fn, split_when)
  73. } // namespace actions
  74. /// @}
  75. } // namespace ranges
  76. #include <range/v3/detail/epilogue.hpp>
  77. #endif