diff options
author | Howard Hinnant <hhinnant@apple.com> | 2010-10-22 15:26:39 +0000 |
---|---|---|
committer | Howard Hinnant <hhinnant@apple.com> | 2010-10-22 15:26:39 +0000 |
commit | 007b26be68bcfd6b67fa291c8a6474053e4b9228 (patch) | |
tree | 5ea2b174716c2f706761fb6e095a0db08edad55e /libcxx/include | |
parent | 412c362d9ee922585027cedf97002cba5a30a6bc (diff) | |
download | bcm5719-llvm-007b26be68bcfd6b67fa291c8a6474053e4b9228.tar.gz bcm5719-llvm-007b26be68bcfd6b67fa291c8a6474053e4b9228.zip |
Fixed bug in random_shuffle to avoid swapping with self
llvm-svn: 117098
Diffstat (limited to 'libcxx/include')
-rw-r--r-- | libcxx/include/algorithm | 17 |
1 files changed, 14 insertions, 3 deletions
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<class _RandomAccessIterator, class _UniformRandomNumberGenerator> { _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)); + } } } |