Replace qSort/qStableSort/qSwap

This commit is contained in:
Jonas Kvinge 2018-10-19 20:18:46 +02:00
parent 0969e7f504
commit 0cda4e27aa
24 changed files with 115 additions and 35 deletions

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <functional>
#include <algorithm>
#include <QObject>
#include <QtGlobal>
@ -61,6 +62,8 @@
#include "playlist/songmimedata.h"
#include "covermanager/albumcoverloader.h"
using std::bind;
using std::sort;
using std::placeholders::_1;
using std::placeholders::_2;
@ -1312,7 +1315,7 @@ void CollectionModel::GetChildSongs(CollectionItem *item, QList<QUrl> *urls, Son
const_cast<CollectionModel*>(this)->LazyPopulate(item);
QList<CollectionItem*> children = item->children;
qSort(children.begin(), children.end(), std::bind(&CollectionModel::CompareItems, this, _1, _2));
std::sort(children.begin(), children.end(), std::bind(&CollectionModel::CompareItems, this, _1, _2));
for (CollectionItem *child : children)
GetChildSongs(child, urls, songs, song_ids);

View File

@ -22,6 +22,7 @@
#include "config.h"
#include <functional>
#include <algorithm>
#include <QObject>
#include <QtGlobal>
@ -55,6 +56,8 @@
#include "contextalbumsmodel.h"
using std::bind;
using std::sort;
using std::placeholders::_1;
using std::placeholders::_2;
@ -479,7 +482,7 @@ void ContextAlbumsModel::GetChildSongs(CollectionItem *item, QList<QUrl> *urls,
const_cast<ContextAlbumsModel*>(this)->LazyPopulate(item);
QList<CollectionItem*> children = item->children;
qSort(children.begin(), children.end(), std::bind(&ContextAlbumsModel::CompareItems, this, _1, _2));
std::sort(children.begin(), children.end(), std::bind(&ContextAlbumsModel::CompareItems, this, _1, _2));
for (CollectionItem *child : children)
GetChildSongs(child, urls, songs, song_ids);

View File

@ -33,6 +33,8 @@
#include "settings/settingsdialog.h"
using std::unique_ptr;
class TaskManager;
class ApplicationImpl;
class TagReaderClient;

View File

@ -152,6 +152,10 @@
# include "core/macsystemtrayicon.h"
#endif
using std::bind;
using std::floor;
using std::stable_sort;
#ifdef Q_OS_MACOS
// Non exported mac-specific function.
void qt_mac_set_dock_menu(QMenu*);
@ -1547,6 +1551,7 @@ void MainWindow::EditTracks() {
}
void MainWindow::EditTagDialogAccepted() {
for (PlaylistItemPtr item : edit_tag_dialog_->playlist_items()) {
item->Reload();
}
@ -1555,14 +1560,16 @@ void MainWindow::EditTagDialogAccepted() {
ui_->playlist->view()->update();
app_->playlist_manager()->current()->Save();
}
void MainWindow::RenumberTracks() {
QModelIndexList indexes =ui_->playlist->view()->selectionModel()->selection().indexes();
int track = 1;
// Get the index list in order
qStableSort(indexes);
std::stable_sort(indexes.begin(), indexes.end());
// if first selected song has a track number set, start from that offset
if (!indexes.isEmpty()) {
@ -1587,6 +1594,7 @@ void MainWindow::RenumberTracks() {
}
track++;
}
}
void MainWindow::SongSaveComplete(TagReaderReply *reply,const QPersistentModelIndex &index) {

View File

@ -57,6 +57,8 @@
#include "playlist/playlistitem.h"
#include "settings/settingsdialog.h"
using std::unique_ptr;
class About;
class AlbumCoverManager;;
class Application;

View File

@ -21,6 +21,8 @@
#include "config.h"
#include <algorithm>
#include <taglib/fileref.h>
#include <taglib/id3v1genres.h>
@ -66,6 +68,8 @@
#include "covermanager/albumcoverloader.h"
#include "tagreadermessages.pb.h"
using std::sort;
const QStringList Song::kColumns = QStringList() << "title"
<< "album"
<< "artist"
@ -495,7 +499,7 @@ int CompareSongsName(const Song &song1, const Song &song2) {
void Song::SortSongsListAlphabetically(SongList *songs) {
Q_ASSERT(songs);
qSort(songs->begin(), songs->end(), CompareSongsName);
std::sort(songs->begin(), songs->end(), CompareSongsName);
}
void Song::Init(const QString &title, const QString &artist, const QString &album, qint64 length_nanosec) {

View File

@ -22,6 +22,7 @@
#include <stdlib.h>
#include <memory>
#include <algorithm>
#ifdef HAVE_GSTREAMER
# include <gst/gst.h>
@ -65,6 +66,9 @@
#endif
using std::placeholders::_1;
using std::bind;
using std::stable_sort;
using std::shared_ptr;
QSet<QString> SongLoader::sRawUriSchemes;
const int SongLoader::kDefaultTimeout = 5000;
@ -323,7 +327,7 @@ void SongLoader::LoadLocalDirectory(const QString &filename) {
LoadLocalPartial(it.next());
}
qStableSort(songs_.begin(), songs_.end(), CompareSongs);
std::stable_sort(songs_.begin(), songs_.end(), CompareSongs);
// Load the first song:
// all songs will be loaded async, but we want the first one in our list to be fully loaded,

View File

@ -43,6 +43,8 @@
#include "coverprovider.h"
#include "coverproviders.h"
using std::stable_sort;
const int AlbumCoverFetcherSearch::kSearchTimeoutMs = 12000;
const int AlbumCoverFetcherSearch::kImageLoadTimeoutMs = 3000;
const int AlbumCoverFetcherSearch::kTargetSize = 500;
@ -148,7 +150,7 @@ void AlbumCoverFetcherSearch::AllProvidersFinished() {
// Now we have to load some images and figure out which one is the best.
// We'll sort the list of results by category, then load the first few images from each category and use some heuristics to score them.
// If no images are good enough we'll keep loading more images until we find one that is or we run out of results.
qStableSort(results_.begin(), results_.end(), CompareProviders);
std::stable_sort(results_.begin(), results_.end(), CompareProviders);
FetchMoreImages();
}

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <memory>
#include <algorithm>
#include <QtGlobal>
#include <QObject>
@ -81,6 +82,8 @@
#include "ui_albumcovermanager.h"
using std::stable_sort;
const char *AlbumCoverManager::kSettingsGroup = "CoverManager";
AlbumCoverManager::AlbumCoverManager(Application *app, CollectionBackend *collection_backend, QWidget *parent, QNetworkAccessManager *network)
@ -286,7 +289,7 @@ void AlbumCoverManager::Reset() {
new QListWidgetItem(artist_icon_, tr("Various artists"), ui_->artists, Various_Artists);
QStringList artists(collection_backend_->GetAllArtistsWithAlbums());
qStableSort(artists.begin(), artists.end(), CompareNocase);
std::stable_sort(artists.begin(), artists.end(), CompareNocase);
for (const QString &artist : artists) {
if (artist.isEmpty()) continue;
@ -326,7 +329,7 @@ void AlbumCoverManager::ArtistChanged(QListWidgetItem *current) {
}
// Sort by album name. The list is already sorted by sqlite but it was done case sensitively.
qStableSort(albums.begin(), albums.end(), CompareAlbumNameNocase);
std::stable_sort(albums.begin(), albums.end(), CompareAlbumNameNocase);
for (const CollectionBackend::Album &info : albums) {
// Don't show songs without an album, obviously

View File

@ -20,6 +20,8 @@
#include "config.h"
#include <algorithm>
#include <QWidget>
#include <QMap>
#include <QString>
@ -34,6 +36,8 @@
#include "coversearchstatisticsdialog.h"
#include "ui_coversearchstatisticsdialog.h"
using std::sort;
CoverSearchStatisticsDialog::CoverSearchStatisticsDialog(QWidget *parent)
: QDialog(parent), ui_(new Ui_CoverSearchStatisticsDialog) {
@ -61,7 +65,7 @@ CoverSearchStatisticsDialog::~CoverSearchStatisticsDialog() { delete ui_; }
void CoverSearchStatisticsDialog::Show(const CoverSearchStatistics &statistics) {
QStringList providers(statistics.total_images_by_provider_.keys());
qSort(providers);
std::sort(providers.begin(), providers.end());
ui_->summary->setText(tr("Got %1 covers out of %2 (%3 failed)")
.arg(statistics.chosen_images_)

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <memory>
#include <algorithm>
#include <QtGlobal>
#include <QtConcurrentRun>
@ -63,6 +64,9 @@
#include "organiseerrordialog.h"
#include "ui_organisedialog.h"
using std::shared_ptr;
using std::stable_sort;
const char *OrganiseDialog::kDefaultFormat = "%artist/%album{ (Disc %disc)}/{%track - }%title.%extension";
const char *OrganiseDialog::kSettingsGroup = "OrganiseDialog";
@ -111,7 +115,7 @@ OrganiseDialog::OrganiseDialog(TaskManager *task_manager, QWidget *parent)
// Get the titles of the tags to put in the insert menu
QStringList tag_titles = tags.keys();
qStableSort(tag_titles);
std::stable_sort(tag_titles.begin(), tag_titles.end());
// Build the insert menu
QMenu *tag_menu = new QMenu(this);

View File

@ -20,6 +20,8 @@
#include "config.h"
#include <algorithm>
#include <QWidget>
#include <QDialog>
#include <QtAlgorithms>
@ -33,6 +35,8 @@
#include "organiseerrordialog.h"
#include "ui_organiseerrordialog.h"
using std::stable_sort;
OrganiseErrorDialog::OrganiseErrorDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_OrganiseErrorDialog) {
ui_->setupUi(this);
@ -61,7 +65,7 @@ void OrganiseErrorDialog::Show(OperationType type, const SongList &songs_with_er
void OrganiseErrorDialog::Show(OperationType type, const QStringList &files_with_errors) {
QStringList sorted_files = files_with_errors;
qStableSort(sorted_files);
std::stable_sort(sorted_files.begin(), sorted_files.end());
switch (type) {
case Type_Copy:

View File

@ -45,6 +45,8 @@
#include <QUrl>
#include <QTimerEvent>
using std::unique_ptr;
class GstEngine;
class GstBufferConsumer;
class GstElementDeleter;

View File

@ -50,6 +50,8 @@
#include "internetservice.h"
#include "internetmodel.h"
using std::advance;
const int InternetSearch::kDelayedSearchTimeoutMs = 200;
const int InternetSearch::kMaxResultsPerEmission = 2000;
const int InternetSearch::kArtHeight = 32;

View File

@ -22,6 +22,7 @@
#include "config.h"
#include <functional>
#include <algorithm>
#include <QtGlobal>
#include <QWidget>

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <stdbool.h>
#include <algorithm>
#include <QObject>
#include <QList>
@ -46,6 +47,8 @@
#include "core/network.h"
#include "core/timeconstants.h"
using std::stable_sort;
const char *AcoustidClient::kClientId = "0qjUoxbowg";
const char *AcoustidClient::kUrl = "http://api.acoustid.org/v2/lookup";
const int AcoustidClient::kDefaultTimeout = 5000; // msec
@ -153,7 +156,7 @@ void AcoustidClient::RequestFinished(QNetworkReply *reply, int request_id) {
}
}
qStableSort(id_source_list);
std::stable_sort(id_source_list.begin(), id_source_list.end());
QList<QString> id_list;
for (const IdSource& is : id_source_list) {

View File

@ -20,6 +20,8 @@
#include "config.h"
#include <algorithm>
#include <QObject>
#include <QList>
#include <QPair>
@ -44,6 +46,9 @@
#include "core/utilities.h"
#include "musicbrainzclient.h"
using std::sort;
using std::stable_sort;
const char *MusicBrainzClient::kTrackUrl = "http://musicbrainz.org/ws/2/recording/";
const char *MusicBrainzClient::kDiscUrl = "http://musicbrainz.org/ws/2/discid/";
const char *MusicBrainzClient::kDateRegex = "^[12]\\d{3}";
@ -220,7 +225,7 @@ void MusicBrainzClient::RequestFinished(QNetworkReply *reply, int id, int reques
// Merge the results we have
ResultList ret;
QList<PendingResults> result_list_list = pending_results_.take(id);
qSort(result_list_list);
std::sort(result_list_list.begin(), result_list_list.end());
for (const PendingResults &result_list : result_list_list) {
ret << result_list.results_;
}
@ -242,6 +247,7 @@ bool MusicBrainzClient::MediumHasDiscid(const QString &discid, QXmlStreamReader
}
qLog(Debug) << "Reached end of xml stream without encountering </disc-list>";
return false;
}
MusicBrainzClient::ResultList MusicBrainzClient::ParseMedium(QXmlStreamReader *reader) {
@ -330,7 +336,7 @@ MusicBrainzClient::ResultList MusicBrainzClient::ParseTrack(QXmlStreamReader *re
ret << result;
}
else {
qStableSort(releases);
std::stable_sort(releases.begin(), releases.end());
for (const Release &release : releases) {
ret << release.CopyAndMergeInto(result);
}
@ -401,7 +407,7 @@ MusicBrainzClient::ResultList MusicBrainzClient::UniqueResults(const ResultList&
ResultList ret;
if (opt == SortResults) {
ret = QSet<Result>::fromList(results).toList();
qSort(ret);
std::sort(ret.begin(), ret.end());
}
else { // KeepOriginalOrder
// Qt doesn't provide a ordered set (QSet "stores values in an unspecified order" according to Qt documentation).

View File

@ -94,6 +94,10 @@ using std::placeholders::_1;
using std::placeholders::_2;
using std::shared_ptr;
using std::unordered_map;
using std::sort;
using std::stable_sort;
using std::greater;
using std::swap;
const char *Playlist::kCddaMimeType = "x-content/audio-cdda";
const char *Playlist::kRowsMimetype = "application/x-strawberry-playlist-rows";
@ -684,7 +688,7 @@ bool Playlist::dropMimeData(const QMimeData *data, Qt::DropAction action, int ro
pid = !own_pid;
}
qStableSort(source_rows); // Make sure we take them in order
std::stable_sort(source_rows.begin(), source_rows.end()); // Make sure we take them in order
if (source_playlist == this) {
// Dragged from this playlist - rearrange the items
@ -1194,17 +1198,17 @@ void Playlist::sort(int column, Qt::SortOrder order) {
if (column == Column_Album) {
// When sorting by album, also take into account discs and tracks.
qStableSort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Track, order, _1, _2));
qStableSort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Disc, order, _1, _2));
qStableSort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Album, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Track, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Disc, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Album, order, _1, _2));
}
else if (column == Column_Filename) {
// When sorting by full paths we also expect a hierarchical order. This returns a breath-first ordering of paths.
qStableSort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Filename, order, _1, _2));
qStableSort(begin, new_items.end(), std::bind(&Playlist::ComparePathDepths, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::CompareItems, Column_Filename, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::ComparePathDepths, order, _1, _2));
}
else {
qStableSort(begin, new_items.end(), std::bind(&Playlist::CompareItems, column, order, _1, _2));
std::stable_sort(begin, new_items.end(), std::bind(&Playlist::CompareItems, column, order, _1, _2));
}
undo_stack_->push(new PlaylistUndoCommands::SortItems(this, column, order, new_items));
@ -1321,7 +1325,7 @@ void Playlist::RemoveItemsWithoutUndo(const QList<int> &indicesIn) {
// Sort the indices descending because removing elements 'backwards' is easier - indices don't 'move' in the process.
QList<int> indices = indicesIn;
qSort(indices.begin(), indices.end(), DescendingIntLessThan);
std::sort(indices.begin(), indices.end(), DescendingIntLessThan);
for (int j = 0; j < indices.count(); j++) {
int beginning = indices[j], end = indices[j];
@ -1367,7 +1371,7 @@ bool Playlist::removeRows(QList<int> &rows) {
}
// Start from the end to be sure that indices won't 'move' during the removal process
qSort(rows.begin(), rows.end(), qGreater<int>());
std::sort(rows.begin(), rows.end(), std::greater<int>());
QList<int> part;
while (!rows.isEmpty()) {

View File

@ -21,6 +21,7 @@
#include "config.h"
#include <math.h>
#include <algorithm>
#include <QApplication>
#include <QObject>
@ -77,6 +78,8 @@
#include "settings/playbacksettingspage.h"
#include "settings/playlistsettingspage.h"
using std::sort;
const int PlaylistView::kStateVersion = 6;
const int PlaylistView::kGlowIntensitySteps = 24;
const int PlaylistView::kAutoscrollGraceTimeout = 30; // seconds
@ -572,7 +575,7 @@ void PlaylistView::RemoveSelected(bool deleting_from_disk) {
int last_row = selection.last().top();
// Sort the selection so we remove the items at the *bottom* first, ensuring we don't have to mess around with changing row numbers
qSort(selection.begin(), selection.end(), CompareSelectionRanges);
std::sort(selection.begin(), selection.end(), CompareSelectionRanges);
for (const QItemSelectionRange &range : selection) {
if (range.top() < last_row) rows_removed += range.height();
@ -613,7 +616,7 @@ QList<int> PlaylistView::GetEditableColumns() {
QModelIndex index = model()->index(0, col);
if (index.flags() & Qt::ItemIsEditable) columns << h->visualIndex(col);
}
qSort(columns);
std::sort(columns.begin(), columns.end());
return columns;
}

View File

@ -20,6 +20,8 @@
#include "config.h"
#include <algorithm>
#include <QObject>
#include <QIODevice>
#include <QDataStream>
@ -39,6 +41,8 @@
#include "playlist.h"
#include "queue.h"
using std::stable_sort;
const char *Queue::kRowsMimetype = "application/x-strawberry-queue-rows";
Queue::Queue(QObject *parent) : QAbstractProxyModel(parent) {}
@ -279,7 +283,9 @@ bool Queue::dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
QList<int> proxy_rows;
QDataStream stream(data->data(kRowsMimetype));
stream >> proxy_rows;
qStableSort(proxy_rows); // Make sure we take them in order
// Make sure we take them in order
std::stable_sort(proxy_rows.begin(), proxy_rows.end());
Move(proxy_rows, row);
}
@ -355,7 +361,7 @@ QVariant Queue::headerData(int section, Qt::Orientation orientation, int role) c
void Queue::Remove(QList<int> &proxy_rows) {
// Order the rows
qStableSort(proxy_rows);
std::stable_sort(proxy_rows.begin(), proxy_rows.end());
// Reflects immediately changes in the playlist
layoutAboutToBeChanged();

View File

@ -19,6 +19,7 @@
*/
#include <stdbool.h>
#include <algorithm>
#include <QWidget>
#include <QDialog>
@ -39,6 +40,8 @@
#include "queuemanager.h"
#include "ui_queuemanager.h"
using std::stable_sort;
QueueManager::QueueManager(QWidget *parent)
: QDialog(parent),
ui_(new Ui_QueueManager),
@ -105,7 +108,7 @@ void QueueManager::CurrentPlaylistChanged(Playlist *playlist) {
void QueueManager::MoveUp() {
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
qStableSort(indexes);
std::stable_sort(indexes.begin(), indexes.end());
if (indexes.isEmpty() || indexes.first().row() == 0) return;
@ -118,7 +121,7 @@ void QueueManager::MoveUp() {
void QueueManager::MoveDown() {
QModelIndexList indexes = ui_->list->selectionModel()->selectedRows();
qStableSort(indexes);
std::stable_sort(indexes.begin(), indexes.end());
if (indexes.isEmpty() || indexes.last().row() == current_playlist_->queue()->rowCount()-1)
return;

View File

@ -41,6 +41,8 @@
#include "wplparser.h"
#include "xspfparser.h"
using std::stable_sort;
const int PlaylistParser::kMagicSize = 512;
PlaylistParser::PlaylistParser(CollectionBackendInterface *collection, QObject *parent)
@ -66,7 +68,7 @@ QStringList PlaylistParser::file_extensions() const {
ret << parser->file_extensions();
}
qStableSort(ret);
std::stable_sort(ret.begin(), ret.end());
return ret;
}
@ -79,7 +81,7 @@ QStringList PlaylistParser::mime_types() const {
if (!parser->mime_type().isEmpty()) ret << parser->mime_type();
}
qStableSort(ret);
std::stable_sort(ret.begin(), ret.end());
return ret;
}

View File

@ -20,6 +20,8 @@
#include "config.h"
#include <algorithm>
#include <QtGlobal>
#include <QWidget>
#include <QDialog>
@ -62,6 +64,8 @@
#undef AddJob
#endif
using std::sort;
const char *TranscodeDialog::kSettingsGroup = "Transcoder";
const int TranscodeDialog::kProgressInterval = 500;
const int TranscodeDialog::kMaxDestinationItems = 10;
@ -89,7 +93,7 @@ TranscodeDialog::TranscodeDialog(QWidget *parent)
// Get presets
QList<TranscoderPreset> presets = Transcoder::GetAllPresets();
qSort(presets.begin(), presets.end(), ComparePresetsByName);
std::sort(presets.begin(), presets.end(), ComparePresetsByName);
for (const TranscoderPreset &preset : presets) {
ui_->format->addItem(QString("%1 (.%2)").arg(preset.name_, preset.extension_), QVariant::fromValue(preset));
}

View File

@ -24,6 +24,7 @@
#include <glib/gtypes.h>
#include <stdlib.h>
#include <memory>
#include <algorithm>
#include <gst/gst.h>
#include <QtGlobal>
@ -47,10 +48,10 @@
#include "transcoder.h"
using std::shared_ptr;
using std::sort;
int Transcoder::JobFinishedEvent::sEventType = -1;
TranscoderPreset::TranscoderPreset(Song::FileType type, const QString &name, const QString &extension, const QString &codec_mimetype, const QString &muxer_mimetype)
: type_(type),
name_(name),
@ -145,7 +146,7 @@ GstElement *Transcoder::CreateElementForMimeType(const QString &element_type, co
if (suitable_elements_.isEmpty()) return nullptr;
// Sort by rank
qSort(suitable_elements_);
std::sort(suitable_elements_.begin(), suitable_elements_.end());
const SuitableElement &best = suitable_elements_.last();
LogLine(QString("Using '%1' (rank %2)").arg(best.name_).arg(best.rank_));