Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Özgür Ozan Sen
ogs
Commits
308985bc
Commit
308985bc
authored
9 years ago
by
Christoph Lehmann
Browse files
Options
Downloads
Patches
Plain Diff
[BL] handle maps with unique keys
parent
7ec202d4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BaseLib/uniqueInsert.h
+52
-0
52 additions, 0 deletions
BaseLib/uniqueInsert.h
with
52 additions
and
0 deletions
BaseLib/uniqueInsert.h
+
52
−
0
View file @
308985bc
...
...
@@ -17,6 +17,8 @@
#include
<algorithm>
#include
"StringTools.h"
namespace
BaseLib
{
template
<
typename
Container
>
...
...
@@ -26,6 +28,56 @@ void uniquePushBack(Container& container, typename Container::value_type const&
container
.
push_back
(
element
);
}
//! Inserts the given \c key with the given \c value into the \c map if an entry with the
//! given \c key does not yet exist; otherwise an \c error_message is printed and the
//! program is aborted.
template
<
typename
Map
,
typename
Key
,
typename
Value
>
void
insertIfKeyUniqueElseError
(
Map
&
map
,
Key
const
&
key
,
Value
&&
value
,
std
::
string
const
&
error_message
)
{
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;
//! otherwise an \c error_message is printed and the program is aborted.
//! Cf. also the const overload below.
//! \remark Use as: \code{.cpp} get_or_error<Value>(some_map, some_key, "error message") \endcode
template
<
typename
Map
,
typename
Key
>
typename
Map
::
mapped_type
&
getOrError
(
Map
&
map
,
Key
const
&
key
,
std
::
string
const
&
error_message
)
{
auto
it
=
map
.
find
(
key
);
if
(
it
==
map
.
end
())
{
ERR
(
"%s Key `%s' does not exist."
,
error_message
.
c_str
(),
tostring
(
key
).
c_str
());
std
::
abort
();
}
return
it
->
second
;
}
//! \overload
template
<
typename
Map
,
typename
Key
>
typename
Map
::
mapped_type
const
&
getOrError
(
Map
const
&
map
,
Key
const
&
key
,
std
::
string
const
&
error_message
)
{
auto
it
=
map
.
find
(
key
);
if
(
it
==
map
.
end
())
{
ERR
(
"%s Key `%s' does not exist."
,
error_message
.
c_str
(),
tostring
(
key
).
c_str
());
std
::
abort
();
}
return
it
->
second
;
}
}
// end namespace BaseLib
#endif
/* UNIQUELISTINSERT_H_ */
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment