diff --git a/src/blackmisc/collection.h b/src/blackmisc/collection.h index 59253850a..d5411c687 100644 --- a/src/blackmisc/collection.h +++ b/src/blackmisc/collection.h @@ -186,6 +186,12 @@ namespace BlackMisc */ const_iterator find(const T &value) const { Q_ASSERT(pimpl()); return pimpl()->find(value); } + /*! + * \brief Efficient remove using the find and erase of the implementation container. Typically O(log n). + * \pre The sequence must be initialized. + */ + void remove(const T &object) { auto it = find(object); if (it != end()) { erase(pos); } } + /*! * \brief Test for equality. * \todo Improve inefficient implementation. diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index 6873b33ba..8f14dc6db 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -113,6 +113,30 @@ namespace BlackMisc return contains(BlackMisc::Predicates::MemberEqual(key1, value1)); } + /*! + * \brief Remove elements for which a given predicate returns true. + * \pre The sequence must be initialized. + */ + template + void removeIf(Predicate p) + { + for (auto it = derived().begin(); it != derived().end(); ++it) + { + if (p(*it)) { it = derived().erase(it); } + } + } + + /*! + * \brief Remove elements matching a particular key/value pair. + * \param key1 A pointer to a member function of T. + * \param value1 Will be compared to the return value of key1. + */ + template + void removeIf(K1 key1, V1 value1) + { + removeIf(BlackMisc::Predicates::MemberEqual(key1, value1)); + } + public: // CValueObject overrides /*! * \copydoc CValueObject::toQVariant() diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index 1b2a09e45..c7c21acd6 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -252,27 +252,6 @@ namespace BlackMisc applyIf([ & ](const T &value) { return value == pattern; }, newValues); } - /*! - * \brief Remove elements for which a given predicate returns true. - * \pre The sequence must be initialized. - */ - template - void removeIf(Predicate p) - { - erase(std::remove_if(begin(), end(), p), end()); - } - - /*! - * \brief Remove elements matching a particular key/value pair. - * \param key1 A pointer to a member function of T. - * \param value1 Will be compared to the return value of key1. - */ - template - void removeIf(K1 key1, V1 value1) - { - removeIf(BlackMisc::Predicates::MemberEqual(key1, value1)); - } - /*! * \brief Remove the given object, if it is contained. * \pre The sequence must be initialized.