From 007b26be68bcfd6b67fa291c8a6474053e4b9228 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Fri, 22 Oct 2010 15:26:39 +0000 Subject: Fixed bug in random_shuffle to avoid swapping with self llvm-svn: 117098 --- libcxx/include/algorithm | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'libcxx/include/algorithm') diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 74feafd4c45..b895f032bed 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -2686,7 +2686,11 @@ random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) _D __uid; __rs_default __g = __rs_get(); for (--__last, --__d; __first < __last; ++__first, --__d) - swap(*__first, *(__first + __uid(__g, _P(0, __d)))); + { + difference_type __i = __uid(__g, _P(0, __d)); + if (__i != difference_type(0)) + swap(*__first, *(__first + __i)); + } } } @@ -2704,7 +2708,10 @@ random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, if (__d > 1) { for (--__last; __first < __last; ++__first, --__d) - swap(*__first, *(__first + __rand(__d))); + { + difference_type __i = __rand(__d); + swap(*__first, *(__first + __i)); + } } } @@ -2720,7 +2727,11 @@ template { _D __uid; for (--__last, --__d; __first < __last; ++__first, --__d) - swap(*__first, *(__first + __uid(__g, _P(0, __d)))); + { + difference_type __i = __uid(__g, _P(0, __d)); + if (__i != difference_type(0)) + swap(*__first, *(__first + __i)); + } } } -- cgit v1.2.3