mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-14 08:45:36 +08:00
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:
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user