From 08e978c8d66c01f58361ab04d6b684337339ed33 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Wed, 8 Jan 2014 00:22:53 +0000 Subject: [PATCH] moved from CContainerBase to CSequence those methods which only support sequential containers refs #81 --- src/blackmisc/containerbase.h | 141 ---------------------------------- src/blackmisc/sequence.h | 130 +++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 141 deletions(-) diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index c7b44e879..89ccbde9e 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -115,94 +115,6 @@ namespace BlackMisc return contains(BlackMisc::Predicates::MemberEqual(key1, value1)); } - /*! - * \brief Modify by applying a value map to each element for which a given predicate returns true. - * \pre Requires a sequential container. - * \param p - * \param newValues - */ - template - void applyIf(Predicate p, const CValueMap &newValues) - { - std::for_each(derived().begin(), derived().end(), [ &, p ](T &value) { if (p(value)) { value.apply(newValues); } }); - } - - /*! - * \brief Modify by applying a value map to each element matching a particular key/value pair. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \param value1 Will be compared to the return value of key1. - * \param newValues - */ - template - void applyIf(K1 key1, V1 value1, const CValueMap &newValues) - { - applyIf(BlackMisc::Predicates::MemberEqual(key1, value1), newValues); - } - - /*! - * \brief Modify by applying a value map to each element matching a given value map. - * \pre Requires a sequential container. - * \param pattern - * \param newValues - */ - void applyIf(const CValueMap &pattern, const CValueMap &newValues) - { - applyIf([ & ](const T &value) { return value == pattern; }, newValues); - } - - /*! - * \brief Replace elements for which a given predicate returns true. - * \pre Requires a sequential container. - * \param p - * \param replacement - */ - template - void replaceIf(Predicate p, const T &replacement) - { - std::replace_if(derived().begin(), derived().end(), p, replacement); - } - - /*! - * \brief Replace elements matching a particular key/value pair. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \param value1 Will be compared to the return value of key1. - * \param replacement - */ - template - void replaceIf(K1 key1, V1 value1, const T &replacement) - { - replaceIf(BlackMisc::Predicates::MemberEqual(key1, value1), replacement); - } - - /*! - * \brief Replace elements for which a given predicate returns true. If there is no match, push the new element on the end. - * \pre Requires a sequential container. - * \param p - * \param replacement - */ - template - void replaceOrAdd(Predicate p, const T &replacement) - { - if (contains(p)) { replaceIf(p, replacement); } - else { derived().push_back(replacement); } - } - - /*! - * \brief Replace elements matching a particular key/value pair. If there is no match, push the new element on the end. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \param value1 Will be compared to the return value of key1. - * \param replacement - */ - template - void replaceOrAdd(K1 key1, V1 value1, const T &replacement) - { - if (contains(key1, value1)) { replaceIf(key1, value1, replacement); } - else { derived().push_back(replacement); } - } - /*! * \brief Remove elements for which a given predicate returns true. * \param p @@ -224,59 +136,6 @@ namespace BlackMisc removeIf(BlackMisc::Predicates::MemberEqual(key1, value1)); } - /*! - * \brief Return a copy sorted by a given comparator predicate. - * \pre Requires a sequential container. - * \param p - * \return - */ - template - C sorted(Predicate p) const - { - C result = derived(); - std::sort(result.begin(), result.end(), p); - return result; - } - - /*! - * \brief Return a copy sorted by a particular key. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \return - */ - template - C sortedBy(K1 key1) const - { - return sorted(BlackMisc::Predicates::MemberLess(key1)); - } - - /*! - * \brief Return a copy sorted by some particular keys. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \param key2 A pointer to a member function of T. - * \return - */ - template - C sortedBy(K1 key1, K2 key2) const - { - return sorted(BlackMisc::Predicates::MemberLess(key1, key2)); - } - - /*! - * \brief Return a copy sorted by some particular keys. - * \pre Requires a sequential container. - * \param key1 A pointer to a member function of T. - * \param key2 A pointer to a member function of T. - * \param key3 A pointer to a member function of T. - * \return - */ - template - C sortedBy(K1 key1, K2 key2, K3 key3) const - { - return sorted(BlackMisc::Predicates::MemberLess(key1, key2, key3)); - } - public: // CValueObject overrides virtual QVariant toQVariant() const { return QVariant::fromValue(derived()); } diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index 368218b40..df693f287 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -258,6 +258,136 @@ namespace BlackMisc */ iterator erase(iterator it1, iterator it2) { Q_ASSERT(pimpl()); return pimpl()->erase(it1, it2); } + /*! + * \brief Modify by applying a value map to each element for which a given predicate returns true. + * \param p + * \param newValues + */ + template + void applyIf(Predicate p, const CValueMap &newValues) + { + std::for_each(begin(), end(), [ &, p ](T &value) { if (p(value)) { value.apply(newValues); } }); + } + + /*! + * \brief Modify by applying a value map to each element 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. + * \param newValues + */ + template + void applyIf(K1 key1, V1 value1, const CValueMap &newValues) + { + applyIf(BlackMisc::Predicates::MemberEqual(key1, value1), newValues); + } + + /*! + * \brief Modify by applying a value map to each element matching a given value map. + * \param pattern + * \param newValues + */ + void applyIf(const CValueMap &pattern, const CValueMap &newValues) + { + applyIf([ & ](const T &value) { return value == pattern; }, newValues); + } + + /*! + * \brief Replace elements for which a given predicate returns true. + * \param p + * \param replacement + */ + template + void replaceIf(Predicate p, const T &replacement) + { + std::replace_if(begin(), end(), p, replacement); + } + + /*! + * \brief Replace 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. + * \param replacement + */ + template + void replaceIf(K1 key1, V1 value1, const T &replacement) + { + replaceIf(BlackMisc::Predicates::MemberEqual(key1, value1), replacement); + } + + /*! + * \brief Replace elements for which a given predicate returns true. If there is no match, push the new element on the end. + * \param p + * \param replacement + */ + template + void replaceOrAdd(Predicate p, const T &replacement) + { + if (this->contains(p)) { replaceIf(p, replacement); } + else { push_back(replacement); } + } + + /*! + * \brief Replace elements matching a particular key/value pair. If there is no match, push the new element on the end. + * \param key1 A pointer to a member function of T. + * \param value1 Will be compared to the return value of key1. + * \param replacement + */ + template + void replaceOrAdd(K1 key1, V1 value1, const T &replacement) + { + if (this->contains(key1, value1)) { replaceIf(key1, value1, replacement); } + else { push_back(replacement); } + } + + /*! + * \brief Return a copy sorted by a given comparator predicate. + * \param p + * \return + */ + template + CSequence sorted(Predicate p) const + { + CSequence result = *this; + std::sort(result.begin(), result.end(), p); + return result; + } + + /*! + * \brief Return a copy sorted by a particular key. + * \param key1 A pointer to a member function of T. + * \return + */ + template + CSequence sortedBy(K1 key1) const + { + return sorted(BlackMisc::Predicates::MemberLess(key1)); + } + + /*! + * \brief Return a copy sorted by some particular keys. + * \param key1 A pointer to a member function of T. + * \param key2 A pointer to a member function of T. + * \return + */ + template + CSequence sortedBy(K1 key1, K2 key2) const + { + return sorted(BlackMisc::Predicates::MemberLess(key1, key2)); + } + + /*! + * \brief Return a copy sorted by some particular keys. + * \param key1 A pointer to a member function of T. + * \param key2 A pointer to a member function of T. + * \param key3 A pointer to a member function of T. + * \return + */ + template + CSequence sortedBy(K1 key1, K2 key2, K3 key3) const + { + return sorted(BlackMisc::Predicates::MemberLess(key1, key2, key3)); + } + /*! * \brief Test for equality. * \param other