unique.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2017-2017.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_MOVE_ALGO_UNIQUE_HPP
  12. #define BOOST_MOVE_ALGO_UNIQUE_HPP
  13. #include <boost/move/detail/config_begin.hpp>
  14. #include <boost/move/utility_core.hpp>
  15. namespace boost {
  16. namespace movelib {
  17. //! <b>Requires</b>: The comparison function shall be an equivalence relation. The type of *first shall satisfy
  18. //! the MoveAssignable requirements
  19. //!
  20. //! <b>Effects</b>: For a nonempty range, eliminates all but the first element from every consecutive group
  21. //! of equivalent elements referred to by the iterator i in the range [first + 1, last) for which the
  22. //! following conditions hold: pred(*(i - 1), *i) != false.
  23. //!
  24. //! <b>Returns</b>: The end of the resulting range.
  25. //!
  26. //! <b>Complexity</b>: For nonempty ranges, exactly (last - first) - 1 applications of the corresponding predicate.
  27. template<class ForwardIterator, class BinaryPredicate>
  28. ForwardIterator unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
  29. {
  30. if (first != last) {
  31. ForwardIterator next(first);
  32. ++next;
  33. for (; next != last; ++next, ++first) {
  34. if (pred(*first, *next)) { //Find first equal element
  35. while (++next != last)
  36. if (!pred(*first, *next))
  37. *++first = ::boost::move(*next);
  38. break;
  39. }
  40. }
  41. ++first;
  42. }
  43. return first;
  44. }
  45. } //namespace movelib {
  46. } //namespace boost {
  47. #include <boost/move/detail/config_end.hpp>
  48. #endif //#define BOOST_MOVE_ALGO_UNIQUE_HPP