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.

84 lines
2.8KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. // Copyright Gonzalo Brito Gadeschi 2014
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/range-v3
  13. //
  14. // Implementation based on the code in libc++
  15. // http://http://libcxx.llvm.org/
  16. #ifndef RANGES_V3_ALGORITHM_UNIQUE_HPP
  17. #define RANGES_V3_ALGORITHM_UNIQUE_HPP
  18. #include <range/v3/range_fwd.hpp>
  19. #include <range/v3/algorithm/adjacent_find.hpp>
  20. #include <range/v3/functional/comparisons.hpp>
  21. #include <range/v3/functional/identity.hpp>
  22. #include <range/v3/functional/invoke.hpp>
  23. #include <range/v3/iterator/operations.hpp>
  24. #include <range/v3/range/access.hpp>
  25. #include <range/v3/range/concepts.hpp>
  26. #include <range/v3/range/dangling.hpp>
  27. #include <range/v3/range/traits.hpp>
  28. #include <range/v3/utility/static_const.hpp>
  29. namespace ranges
  30. {
  31. /// \addtogroup group-algorithms
  32. /// @{
  33. RANGES_BEGIN_NIEBLOID(unique)
  34. /// \brief template function \c unique
  35. ///
  36. /// range-based version of the \c unique std algorithm
  37. ///
  38. /// \pre `Rng` is a model of the `forward_range` concept
  39. /// \pre `I` is a model of the `forward_iterator` concept
  40. /// \pre `S` is a model of the `sentinel_for` concept
  41. /// \pre `C` is a model of the `relation` concept
  42. ///
  43. template<typename I, typename S, typename C = equal_to, typename P = identity>
  44. auto RANGES_FUN_NIEBLOID(unique)(I first, S last, C pred = C{}, P proj = P{}) //
  45. ->CPP_ret(I)( //
  46. requires sortable<I, C, P> && sentinel_for<S, I>)
  47. {
  48. first = adjacent_find(std::move(first), last, std::ref(pred), std::ref(proj));
  49. if(first != last)
  50. {
  51. for(I i = next(first); ++i != last;)
  52. if(!invoke(pred, invoke(proj, *first), invoke(proj, *i)))
  53. *++first = iter_move(i);
  54. ++first;
  55. }
  56. return first;
  57. }
  58. /// \overload
  59. template<typename Rng, typename C = equal_to, typename P = identity>
  60. auto RANGES_FUN_NIEBLOID(unique)(Rng && rng, C pred = C{}, P proj = P{}) //
  61. ->CPP_ret(safe_iterator_t<Rng>)( //
  62. requires sortable<iterator_t<Rng>, C, P> && range<Rng>)
  63. {
  64. return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
  65. }
  66. RANGES_END_NIEBLOID(unique)
  67. namespace cpp20
  68. {
  69. using ranges::unique;
  70. }
  71. /// @}
  72. } // namespace ranges
  73. #endif // include guard