Verified Commit cc6eafa4 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

ResourceBase: use erase-remove for getting list of valid items

In majority of cases all the items will be valid, so creating a
copy of the list by detaching is more efficient than copying it
item by item in a for loop. And for the cases when we need to
remove some items, the erase-remove idiom is the best choice.
parent ed21a3f3
......@@ -54,6 +54,7 @@
#include "akonadiagentbase_debug.h"
#include <iterator>
#include <shared/akranges.h>
#include <KLocalizedString>
......@@ -254,12 +255,7 @@ protected Q_SLOTS:
return;
}
Item::List validItems;
for (const Akonadi::Item &item : items) {
if (!item.remoteId().isEmpty()) {
validItems << item;
}
}
const Item::List validItems = filterValidItems(items);
if (validItems.isEmpty()) {
changeProcessed();
return;
......@@ -275,12 +271,7 @@ protected Q_SLOTS:
return;
}
Item::List validItems;
for (const Akonadi::Item &item : items) {
if (!item.remoteId().isEmpty()) {
validItems << item;
}
}
const Item::List validItems = filterValidItems(items);
if (validItems.isEmpty()) {
changeProcessed();
return;
......@@ -306,12 +297,7 @@ protected Q_SLOTS:
return;
}
Item::List validItems;
for (const Akonadi::Item &item : items) {
if (!item.remoteId().isEmpty()) {
validItems << item;
}
}
const Item::List validItems = filterValidItems(items);
if (validItems.isEmpty()) {
changeProcessed();
return;
......@@ -331,12 +317,7 @@ protected Q_SLOTS:
void itemsRemoved(const Item::List &items) override
{
Item::List validItems;
for (const Akonadi::Item &item : items) {
if (!item.remoteId().isEmpty()) {
validItems << item;
}
}
const Item::List validItems = filterValidItems(items);
if (validItems.isEmpty()) {
changeProcessed();
return;
......@@ -442,6 +423,15 @@ protected Q_SLOTS:
AgentBasePrivate::tagRemoved(tag);
}
private:
static Item::List filterValidItems(Item::List items)
{
items.erase(std::remove_if(items.begin(), items.end(),
[](const auto &item) { return item.remoteId().isEmpty(); }),
items.end());
return items;
}
public:
// synchronize states
Collection currentCollection;
......
Markdown is supported
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