Commit 921aff71 authored by Dmitry Kolesnikov's avatar Dmitry Kolesnikov
Browse files

Fix shuffling of one track

There was an error in the code responsible for making the current
element the first. If the shuffle button is pressed when there is
only one track in the queue, Elisa breaks.

The for loop changes the values to playListSize - 1, and the last
element is added separately. But at the same time, this element has
already been processed before, which leads to a discrepancy.

Another point is that if there is only one element in the queue, then
std::swap(x, x) occurs here, which can be regarded as undefined
behavior. The same happens if the current track is already the first.

Now these cases are processed.
parent c1e93a8b
Pipeline #230887 passed with stage
in 5 minutes and 11 seconds
......@@ -253,8 +253,13 @@ void MediaPlayListProxyModel::setShufflePlayList(const bool value)
QModelIndexList from;
from.reserve(playListSize);
std::swap(d->mRandomMapping[0], d->mRandomMapping[currentTrackRow()]);
from.append(index(d->mRandomMapping.at(0), 0));
// Adding the current track first if it is not the only one
if (playListSize > 1) {
if (currentTrackRow() != 0) {
std::swap(d->mRandomMapping[0], d->mRandomMapping[currentTrackRow()]);
}
from.append(index(d->mRandomMapping.at(0), 0));
}
// Fisher-Yates algorithm
for (int i = 1; i < playListSize - 1; ++i) {
const int swapIndex = d->mRandomGenerator.bounded(i, playListSize);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment