Don't create temporary containers just to iterate over them

Stops temporary containers being created needlessly due to API misuse.
For example, it’s common for developers to assume QHash::values() and
QHash::keys() are free and abuse them, failing to realize their
implementation internally actually iterates the whole container, allocates
memory, and fills a new QList.

Added a removeIf generic algorithm, similar to std ones. We can't use std
algorithms with Qt dictionaries because Qt iterators have different
behavior from the std ones.

Found using clazy.
This commit is contained in:
Luís Pereira 2018-03-06 14:50:10 +00:00 committed by Vladimir Golovnev (Glassez)
parent e22946ef61
commit ac42ccb5e4
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
14 changed files with 121 additions and 61 deletions

View file

@ -91,7 +91,7 @@ void AppController::preferencesAction()
data["incomplete_files_ext"] = session->isAppendExtensionEnabled();
const QVariantHash dirs = pref->getScanDirs();
QVariantMap nativeDirs;
for (QVariantHash::const_iterator i = dirs.begin(), e = dirs.end(); i != e; ++i) {
for (QVariantHash::const_iterator i = dirs.cbegin(), e = dirs.cend(); i != e; ++i) {
if (i.value().type() == QVariant::Int)
nativeDirs.insert(Utils::Fs::toNativePath(i.key()), i.value().toInt());
else
@ -246,7 +246,7 @@ void AppController::setPreferencesAction()
QVariantHash oldScanDirs = pref->getScanDirs();
QVariantHash scanDirs;
ScanFoldersModel *model = ScanFoldersModel::instance();
for (QVariantMap::const_iterator i = nativeDirs.begin(), e = nativeDirs.end(); i != e; ++i) {
for (QVariantMap::const_iterator i = nativeDirs.cbegin(), e = nativeDirs.cend(); i != e; ++i) {
QString folder = Utils::Fs::fromNativePath(i.key());
int downloadType;
QString downloadPath;
@ -275,8 +275,8 @@ void AppController::setPreferencesAction()
}
// Update deleted folders
foreach (QVariant folderVariant, oldScanDirs.keys()) {
QString folder = folderVariant.toString();
for (auto i = oldScanDirs.cbegin(); i != oldScanDirs.cend(); ++i) {
QString folder = i.key();
if (!scanDirs.contains(folder)) {
model->removePath(folder);
qDebug("Removed watched folder %s", qUtf8Printable(folder));