diff --git a/src/blackmisc/collection.h b/src/blackmisc/collection.h index b0e0a4de5..9dc8b4538 100644 --- a/src/blackmisc/collection.h +++ b/src/blackmisc/collection.h @@ -338,6 +338,31 @@ namespace BlackMisc */ void remove(const CCollection &other) { *this = CCollection(*this).difference(other); } + /*! + * \brief Remove elements for which a given predicate returns true. + * \pre The collection must be initialized. + * \return The number of elements removed. + */ + template + int removeIf(Predicate p) + { + int count = 0; + for (auto it = begin(); it != end();) + { + if (p(*it)) { it = erase(it); count++; } + else { ++it; } + } + return count; + } + + //! \copydoc BlackMisc::CContainerBase::removeIf + template + int removeIf(K0 k0, V0 v0, KeysValues... keysValues) + { + // using-declaration doesn't play nicely with injected template names + return CCollection::CContainerBase::removeIf(k0, v0, keysValues...); + } + /*! * \brief Test for equality. */ diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index 9aa55e97c..2603d68b2 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -87,23 +87,6 @@ namespace BlackMisc return other; } - /*! - * \brief Remove elements for which a given predicate returns true. - * \pre The sequence must be initialized. - * \return The number of elements removed. - */ - template - int removeIf(Predicate p) - { - int count = 0; - for (auto it = derived().begin(); it != derived().end();) - { - if (p(*it)) { it = derived().erase(it); count++; } - else { ++it; } - } - return count; - } - /*! * \brief Remove elements matching some particular key/value pair(s). * \param k0 A pointer to a member function of T. @@ -114,7 +97,7 @@ namespace BlackMisc template int removeIf(K0 k0, V0 v0, KeysValues... keysValues) { - return removeIf(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...)); + return derived().removeIf(BlackMisc::Predicates::MemberEqual(k0, v0, keysValues...)); } public: diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index f4b10f966..3d0685474 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -387,6 +387,28 @@ namespace BlackMisc return count; } + /*! + * \brief Remove elements for which a given predicate returns true. + * \pre The sequence must be initialized. + * \return The number of elements removed. + */ + template + int removeIf(Predicate p) + { + auto newEnd = std::remove_if(begin(), end(), p); + int count = std::distance(newEnd, end()); + erase(newEnd, end()); + return count; + } + + //! \copydoc BlackMisc::CContainerBase::removeIf + template + int removeIf(K0 k0, V0 v0, KeysValues... keysValues) + { + // using-declaration doesn't play nicely with injected template names + return CSequence::CContainerBase::removeIf(k0, v0, keysValues...); + } + /*! * \brief Remove all elements if they are in other * \pre The sequence must be initialized. @@ -394,13 +416,9 @@ namespace BlackMisc */ int removeIfIn(const CSequence &other) { - auto newEnd = std::remove_if(begin(), end(), [&other](const T &v) { return other.contains(v); }); - int count = std::distance(newEnd, end()); - erase(newEnd, end()); - return count; + return removeIf([&other](const T &v) { return other.contains(v); }); } - /*! * \brief Replace elements matching the given element with a replacement. * \return The number of elements replaced.