Skip to content
Snippets Groups Projects
Commit a14af946 authored by Christoph Lehmann's avatar Christoph Lehmann
Browse files

[BL] added further unique insertion function

parent f781f5c9
No related branches found
No related tags found
No related merge requests found
...@@ -44,6 +44,31 @@ void insertIfKeyUniqueElseError( ...@@ -44,6 +44,31 @@ void insertIfKeyUniqueElseError(
} }
} }
//! Inserts the given \c key with the given \c value into the \c map if neither an entry
//! with the given \c key nor an entry with the given \c value already exists;
//! otherwise an \c error_message is printed and the program is aborted.
template<typename Map, typename Key, typename Value>
void insertIfKeyValueUniqueElseError(
Map& map, Key const& key, Value&& value,
std::string const& error_message)
{
auto value_compare = [&value](typename Map::value_type const& elem) {
return value == elem.second;
};
if (std::find_if(map.cbegin(), map.cend(), value_compare) != map.cend())
{
ERR("%s Value `%s' already exists.", error_message.c_str(), tostring(value).c_str());
std::abort();
}
auto const inserted = map.emplace(key, std::forward<Value>(value));
if (!inserted.second) { // insertion failed, i.e., key already exists
ERR("%s Key `%s' already exists.", error_message.c_str(), tostring(key).c_str());
std::abort();
}
}
//! Returns the value of \c key from the given \c map if such an entry exists; //! Returns the value of \c key from the given \c map if such an entry exists;
//! otherwise an \c error_message is printed and the program is aborted. //! otherwise an \c error_message is printed and the program is aborted.
//! Cf. also the const overload below. //! Cf. also the const overload below.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment