Fixed bug in random_shuffle to avoid swapping with self
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@117098 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
bce9c3132d
commit
4e59948249
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user