Skip to content

Instantly share code, notes, and snippets.

@danlark1
Created April 20, 2022 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlark1/f4a3d2f14281dc10aecc5ff5b27978a4 to your computer and use it in GitHub Desktop.
Save danlark1/f4a3d2f14281dc10aecc5ff5b27978a4 to your computer and use it in GitHub Desktop.
// Ensures that *__x, *__y and *__z are ordered according to the comparator __c,
// under the assumption that *__y and *__z are already ordered.
template <class _Compare, class _RandomAccessIterator>
inline void __partially_sorted_swap(_RandomAccessIterator __x, _RandomAccessIterator __y,
_RandomAccessIterator __z, _Compare __c) {
using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
bool __r = __c(*__z, *__x);
value_type __tmp = __r ? *__z : *__x;
*__z = __r ? *__x : *__z;
__r = __c(__tmp, *__y);
*__x = __r ? *__x : *__y;
}
template <class _Compare, class _RandomAccessIterator>
inline void __sort3(_RandomAccessIterator __x1, _RandomAccessIterator __x2,
_RandomAccessIterator __x3, _Compare __c) {
_VSTD::__cond_swap<_Compare>(__x2, __x3, __c);
_VSTD::__partially_sorted_swap<_Compare>(__x1, __x2, __x3, __c);
}
@xandris
Copy link

xandris commented Apr 21, 2022

I think something's missing in __partially_sorted_swap. __tmp is read from once to compare it with __y, but its value is never stored anywhere. So is it missing *__y = __r ? __y : __tmp; at the end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment