summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qhash.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-10-16 20:52:48 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-12-02 10:35:14 +0100
commite12e2b43b7e6b42d17556eb8ed392c31589a0b4a (patch)
tree1575582d6d78bd66914c2499daba0df8c997ed48 /src/corelib/tools/qhash.h
parent9341f2e3d0a877b50ba851ff22b0d6ce192faf84 (diff)
Associative containers: add erase_if
Use a trick similar to the one we use for their ranged constructors: support predicates that either take a container's iterator, or that take a std::pair (for STL compatibility). [ChangeLog][QtCore][QMap] Added removeIf() and erase_if(). [ChangeLog][QtCore][QMultiMap] Added removeIf() and erase_if(). [ChangeLog][QtCore][QHash] Added removeIf() and erase_if(). [ChangeLog][QtCore][QMultiHash] Added removeIf() and erase_if(). Change-Id: Ie40aadf6217d7a4126a626c390d530812ebcf020 Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qhash.h')
-rw-r--r--src/corelib/tools/qhash.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 8134f4402c..65ae9b75fd 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -872,6 +872,11 @@ public:
d->erase(it);
return true;
}
+ template <typename Predicate>
+ qsizetype removeIf(Predicate pred)
+ {
+ return QtPrivate::associative_erase_if(*this, pred);
+ }
T take(const Key &key)
{
if (isEmpty()) // prevents detaching shared null
@@ -1354,6 +1359,11 @@ public:
d->erase(it);
return n;
}
+ template <typename Predicate>
+ qsizetype removeIf(Predicate pred)
+ {
+ return QtPrivate::associative_erase_if(*this, pred);
+ }
T take(const Key &key)
{
if (isEmpty()) // prevents detaching shared null
@@ -1946,6 +1956,18 @@ inline size_t qHash(const QMultiHash<Key, T> &key, size_t seed = 0)
return hash;
}
+template <typename Key, typename T, typename Predicate>
+qsizetype erase_if(QHash<Key, T> &hash, Predicate pred)
+{
+ return QtPrivate::associative_erase_if(hash, pred);
+}
+
+template <typename Key, typename T, typename Predicate>
+qsizetype erase_if(QMultiHash<Key, T> &hash, Predicate pred)
+{
+ return QtPrivate::associative_erase_if(hash, pred);
+}
+
QT_END_NAMESPACE
#endif // QHASH_H