You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

split.hpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_HPP
  14. #define RANGES_V3_ACTION_SPLIT_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/iterator/concepts.hpp>
  21. #include <range/v3/iterator/traits.hpp>
  22. #include <range/v3/range/conversion.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. #include <range/v3/view/split.hpp>
  25. #include <range/v3/view/transform.hpp>
  26. namespace ranges
  27. {
  28. /// \addtogroup group-actions
  29. /// @{
  30. namespace actions
  31. {
  32. struct split_fn
  33. {
  34. private:
  35. template<typename Rng>
  36. using split_value_t = meta::if_c<(bool)ranges::container<Rng>, uncvref_t<Rng>,
  37. std::vector<range_value_t<Rng>>>;
  38. public:
  39. // BUGBUG something is not right with the actions. It should be possible
  40. // to move a container into a split and have elements moved into the result.
  41. template<typename Rng>
  42. auto operator()(Rng && rng, range_value_t<Rng> val) const
  43. -> CPP_ret(std::vector<split_value_t<Rng>>)( //
  44. requires input_range<Rng> && indirectly_comparable<
  45. iterator_t<Rng>, range_value_t<Rng> const *, ranges::equal_to>)
  46. {
  47. return views::split(rng, std::move(val)) |
  48. views::transform(to<split_value_t<Rng>>()) | to_vector;
  49. }
  50. template<typename Rng, typename Pattern>
  51. auto operator()(Rng && rng, Pattern && pattern) const
  52. -> CPP_ret(std::vector<split_value_t<Rng>>)( //
  53. requires input_range<Rng> && viewable_range<Pattern> &&
  54. forward_range<Pattern> && indirectly_comparable<
  55. iterator_t<Rng>, iterator_t<Pattern>, ranges::equal_to> &&
  56. (forward_range<Rng> || detail::tiny_range<Pattern>))
  57. {
  58. return views::split(rng, static_cast<Pattern &&>(pattern)) |
  59. views::transform(to<split_value_t<Rng>>()) | to_vector;
  60. }
  61. };
  62. /// \ingroup group-actions
  63. /// \relates split_fn
  64. /// \sa action
  65. RANGES_INLINE_VARIABLE(action<split_fn>, split)
  66. } // namespace actions
  67. /// @}
  68. } // namespace ranges
  69. #endif