containers: added methods contains(), remove(), replace(), replaceOrAdd(),

corresponding to contains(), removeIf(), replaceIf(), and replaceOrAdd(), but overloaded for const T& as well as for predicates.
refs #106
This commit is contained in:
Mathew Sutcliffe
2014-01-25 19:26:57 +00:00
parent 33d555e151
commit 6f19d0a479
2 changed files with 35 additions and 0 deletions

View File

@@ -94,6 +94,16 @@ namespace BlackMisc
return std::any_of(derived().begin(), derived().end(), p);
}
/*!
* \brief Return true if there is an element equal to given object
* \param object is this object in container?
* \return
*/
bool contains(const T &object) const
{
return std::find(derived().begin(), derived().end(), object) != derived().end();
}
/*!
* \brief Return a copy containing only those elements matching a particular key/value pair.
* \param key1 A pointer to a member function of T.

View File

@@ -267,6 +267,22 @@ namespace BlackMisc
removeIf(BlackMisc::Predicates::MemberEqual<T>(key1, value1));
}
/*!
* \brief Remove the given object, if it is contained.
*/
void remove(const T &object)
{
std::remove(begin(), end(), object);
}
/*!
* \brief Replace elements matching the given element with a replacement.
*/
void replace(const T &original, const T &replacement)
{
std::replace(begin(), end(), original, replacement);
}
/*!
* \brief Replace elements for which a given predicate returns true.
*/
@@ -297,6 +313,15 @@ namespace BlackMisc
else { push_back(replacement); }
}
/*!
* \brief Replace elements matching the given element. If there is no match, push the new element on the end.
*/
void replaceOrAdd(const T &original, const T &replacement)
{
if (this->contains(original)) { replace(original, 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.