remove.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Andrey Diduh 2019
  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_VIEW_REMOVE_HPP
  14. #define RANGES_V3_VIEW_REMOVE_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <concepts/concepts.hpp>
  19. #include <range/v3/range_fwd.hpp>
  20. #include <range/v3/functional/bind_back.hpp>
  21. #include <range/v3/functional/comparisons.hpp>
  22. #include <range/v3/view/remove_if.hpp>
  23. #include <range/v3/view/view.hpp>
  24. #include <range/v3/detail/prologue.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-views
  28. /// @{
  29. namespace views
  30. {
  31. struct remove_base_fn
  32. {
  33. private:
  34. template<typename Value>
  35. struct pred_
  36. {
  37. Value value_;
  38. template(typename T)(
  39. requires equality_comparable_with<T, Value const &>)
  40. bool operator()(T && other) const
  41. {
  42. return static_cast<T &&>(other) == value_;
  43. }
  44. };
  45. public:
  46. template(typename Rng, typename Value)(
  47. requires move_constructible<Value> AND viewable_range<Rng> AND
  48. input_range<Rng> AND
  49. indirectly_comparable<iterator_t<Rng>, Value const *, equal_to>)
  50. constexpr auto operator()(Rng && rng, Value value) const
  51. {
  52. return remove_if(static_cast<Rng &&>(rng),
  53. pred_<Value>{std::move(value)});
  54. }
  55. template(typename Rng, typename Value, typename Proj)(
  56. requires move_constructible<Value> AND viewable_range<Rng> AND
  57. input_range<Rng> AND
  58. indirectly_comparable<iterator_t<Rng>, Value const *, equal_to, Proj>)
  59. constexpr auto operator()(Rng && rng, Value value, Proj proj) const
  60. {
  61. return remove_if(static_cast<Rng &&>(rng),
  62. pred_<Value>{std::move(value)},
  63. std::move(proj));
  64. }
  65. };
  66. struct remove_bind_fn
  67. {
  68. template<typename Value>
  69. constexpr auto operator()(Value value) const // TODO: underconstrained
  70. {
  71. return make_view_closure(bind_back(remove_base_fn{}, std::move(value)));
  72. }
  73. template(typename Value, typename Proj)(
  74. requires (!range<Value>)) // TODO: underconstrained
  75. constexpr auto operator()(Value && value, Proj proj) const
  76. {
  77. return make_view_closure(bind_back(
  78. remove_base_fn{}, static_cast<Value &&>(value), std::move(proj)));
  79. }
  80. };
  81. struct RANGES_EMPTY_BASES remove_fn
  82. : remove_base_fn, remove_bind_fn
  83. {
  84. using remove_base_fn::operator();
  85. using remove_bind_fn::operator();
  86. };
  87. /// \relates remove_fn
  88. /// \ingroup group-views
  89. RANGES_INLINE_VARIABLE(remove_fn, remove)
  90. } // namespace views
  91. /// @}
  92. } // namespace ranges
  93. #include <range/v3/detail/epilogue.hpp>
  94. #endif // RANGES_V3_VIEW_REMOVE_HPP