unique.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_VIEW_UNIQUE_HPP
  14. #define RANGES_V3_VIEW_UNIQUE_HPP
  15. #include <utility>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/functional/bind_back.hpp>
  19. #include <range/v3/functional/not_fn.hpp>
  20. #include <range/v3/utility/static_const.hpp>
  21. #include <range/v3/view/adjacent_filter.hpp>
  22. #include <range/v3/view/all.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 unique_base_fn
  32. {
  33. template(typename Rng, typename C = equal_to)(
  34. requires viewable_range<Rng> AND forward_range<Rng> AND
  35. indirect_relation<C, iterator_t<Rng>>)
  36. constexpr adjacent_filter_view<all_t<Rng>, logical_negate<C>> //
  37. operator()(Rng && rng, C pred = {}) const
  38. {
  39. return {all(static_cast<Rng &&>(rng)), not_fn(pred)};
  40. }
  41. };
  42. struct unique_fn : unique_base_fn
  43. {
  44. using unique_base_fn::operator();
  45. template(typename C)(
  46. requires (!range<C>))
  47. constexpr auto operator()(C && pred) const
  48. {
  49. return make_view_closure(
  50. bind_back(unique_base_fn{}, static_cast<C &&>(pred)));
  51. }
  52. };
  53. /// \relates unique_fn
  54. /// \ingroup group-views
  55. RANGES_INLINE_VARIABLE(view_closure<unique_fn>, unique)
  56. } // namespace views
  57. /// @}
  58. } // namespace ranges
  59. #include <range/v3/detail/epilogue.hpp>
  60. #endif