Skip to content
Snippets Groups Projects
Commit dd4a8677 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

tune unique to handle pointers properly

parent 204f73e5
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -140,6 +140,74 @@ const T & min ( const T & a, const T & b, const Args & ... args) { ...@@ -140,6 +140,74 @@ const T & min ( const T & a, const T & b, const Args & ... args) {
return min ( b > a ? a : b, args ... ); return min ( b > a ? a : b, args ... );
} }
   
template < typename _ForwardIterator, typename _BinaryPredicate >
_ForwardIterator __adjacent_find ( _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __binary_pred ) {
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
while (++__next != __last) {
if ( __binary_pred( * __first, * __next ) )
return __first;
__first = __next;
}
return __last;
}
template < typename _ForwardIterator, typename _BinaryPredicate >
_ForwardIterator __unique ( _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __binary_pred ) {
// Skip the beginning, if already unique.
__first = ext::__adjacent_find ( __first, __last, __binary_pred );
if ( __first == __last )
return __last;
// Do the real copy work.
_ForwardIterator __dest = __first;
++ __first;
while ( ++__first != __last )
if ( ! __binary_pred ( * __dest, * __first ) )
std::swap ( * ++__dest, * __first );
return ++ __dest;
}
/**
* @brief Shuffles values in a sequece so that consecutive duplicate values are pushed to the front and others to the back.
* @ingroup mutating_algorithms
* @param __first A forward iterator.
* @param __last A forward iterator.
* @return An iterator designating the end of the resulting sequence.
*
* All but the first element from each group of consecutive
* values that compare equal are pushed to the end of the sequence.
* unique() is stable, so the relative order of elements that are
* not pushed to the end is unchanged.
* Elements between the end of the resulting sequence and @p __last
* are still present, but their order is unspecified.
*/
template < typename _ForwardIterator >
inline _ForwardIterator unique ( _ForwardIterator __first, _ForwardIterator __last ) {
return ext::__unique(__first, __last, [] ( const auto & a, const auto & b ) { return a == b; } );
}
/**
* @brief Shuffles values in a sequece so that consecutive duplicate values are pushed to the front and others to the back.
* @ingroup mutating_algorithms
* @param __first A forward iterator.
* @param __last A forward iterator.
* @param __binary_pred A binary predicate.
* @return An iterator designating the end of the resulting sequence.
*
* All but the first element from each group of consecutive
* values for which @p __binary_pred returns true are pushed to the end of the sequence.
* unique() is stable, so the relative order of elements that are
* not pushed to the end is unchanged.
* Elements between the end of the resulting sequence and @p __last
* are still present, but their order is unspecified.
*/
template < typename _ForwardIterator, typename _BinaryPredicate >
inline _ForwardIterator unique ( _ForwardIterator __first, _ForwardIterator __last, _BinaryPredicate __binary_pred ) {
return ext::__unique(__first, __last, __binary_pred );
}
} /* namespace ext */ } /* namespace ext */
   
#endif /* __ALGORITHM_HPP_ */ #endif /* __ALGORITHM_HPP_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment