Use library provided erase_if()

`Algorithm::removeIf()` is still valuable as `QHash::removeIf()` predicate require an
iterator or a `std::pair`, which both require more code to unpack the variable and therefore
cumbersome to use.

PR #19353.
This commit is contained in:
Chocobo1 2023-07-24 20:29:02 +08:00 committed by GitHub
parent 9898901236
commit e31c3376bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 8 additions and 42 deletions

View file

@ -54,16 +54,6 @@ namespace Algorithm
it = (p(it.key(), it.value()) ? dict.erase(it) : ++it); it = (p(it.key(), it.value()) ? dict.erase(it) : ++it);
} }
// To be used with set types, such as QSet, std::set
template <typename T, typename UnaryPredicate
, typename std::enable_if_t<!HasMappedType<T>::value, int> = 0>
void removeIf(T &set, UnaryPredicate &&p)
{
auto it = set.begin();
while (it != set.end())
it = (p(*it) ? set.erase(it) : ++it);
}
template <typename List> template <typename List>
List sorted(List list) List sorted(List list)
{ {

View file

@ -1382,7 +1382,7 @@ void SessionImpl::processNextResumeData(ResumeSessionContext *context)
} }
} }
Algorithm::removeIf(resumeData.tags, [this, &torrentID](const QString &tag) erase_if(resumeData.tags, [this, &torrentID](const QString &tag)
{ {
if (hasTag(tag)) if (hasTag(tag))
return false; return false;

View file

@ -148,7 +148,7 @@ void Server::removeConnection(Connection *connection)
void Server::dropTimedOutConnection() void Server::dropTimedOutConnection()
{ {
Algorithm::removeIf(m_connections, [](Connection *connection) erase_if(m_connections, [](Connection *connection)
{ {
if (!connection->hasExpired(KEEP_ALIVE_DURATION)) if (!connection->hasExpired(KEEP_ALIVE_DURATION))
return false; return false;

View file

@ -58,7 +58,7 @@ public:
ThisType &intersect(const ThisType &other) ThisType &intersect(const ThisType &other)
{ {
Algorithm::removeIf(*this, [&other](const value_type &value) -> bool std::erase_if(*this, [&other](const value_type &value) -> bool
{ {
return !other.contains(value); return !other.contains(value);
}); });

View file

@ -957,8 +957,7 @@ void SyncController::onTorrentTrackersChanged(BitTorrent::Torrent *torrent)
const TorrentID torrentID = torrent->id(); const TorrentID torrentID = torrent->id();
Algorithm::removeIf(m_knownTrackers Algorithm::removeIf(m_knownTrackers
, [this, torrentID, currentTrackers] , [this, torrentID, currentTrackers](const QString &knownTracker, QSet<TorrentID> &torrentIDs)
(const QString &knownTracker, QSet<TorrentID> &torrentIDs)
{ {
if (auto idIter = torrentIDs.find(torrentID) if (auto idIter = torrentIDs.find(torrentID)
; (idIter != torrentIDs.end()) && !currentTrackers.contains(knownTracker)) ; (idIter != torrentIDs.end()) && !currentTrackers.contains(knownTracker))

View file

@ -91,34 +91,11 @@ private slots:
} }
} }
void testNonMappedTypeRemoveIf() const void testSorted() const
{ {
{ const QStringList list = {u"c"_s, u"b"_s, u"a"_s};
QSet<char> data = const QStringList sortedList = {u"a"_s, u"b"_s, u"c"_s};
{ QCOMPARE(Algorithm::sorted(list), sortedList);
'a',
'b',
'c',
'b',
'd'
};
Algorithm::removeIf(data, [](const char value)
{
return (value == 'b');
});
QCOMPARE(data.size(), 3);
QVERIFY(data.contains('a'));
QVERIFY(data.contains('c'));
QVERIFY(data.contains('d'));
}
{
std::set<char> data;
Algorithm::removeIf(data, [](const char value)
{
return (value == 'b');
});
QVERIFY(data.empty());
}
} }
}; };