Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dumux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
dumux-repositories
dumux
Commits
58f803cb
Commit
58f803cb
authored
1 year ago
by
Dennis Gläser
Browse files
Options
Downloads
Patches
Plain Diff
[io][chrono] avoid unordered_map
parent
5bc476c7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3668
Feature/std chrono parser
Pipeline
#37386
passed
1 year ago
Stage: check-status
Stage: trigger dumux pipelines
Pipeline: dumux
#37392
Pipeline: dumux-lecture
#37391
Pipeline: dumux
#37390
+3
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dumux/io/chrono.hh
+15
-4
15 additions, 4 deletions
dumux/io/chrono.hh
with
15 additions
and
4 deletions
dumux/io/chrono.hh
+
15
−
4
View file @
58f803cb
...
...
@@ -12,12 +12,15 @@
#ifndef DUMUX_IO_CHRONO_HH
#define DUMUX_IO_CHRONO_HH
#include
<array>
#include
<cctype>
#include
<chrono>
#include
<string>
#include
<
unordered_map
>
#include
<
string_view
>
#include
<algorithm>
#include
<dune/common/exceptions.hh>
namespace
Dumux
::
Chrono
{
//! Try to construct a std::chrono::duration from a string
...
...
@@ -27,7 +30,7 @@ void toDuration(std::chrono::duration<Rep, Period>& duration, const std::string&
using
std
::
chrono
::
duration_cast
;
using
namespace
std
::
chrono_literals
;
using
S
=
std
::
chrono
::
duration
<
Rep
>
;
static
const
std
::
unordered_map
<
std
::
string
,
S
>
unitMap
{
const
expr
std
::
array
<
std
::
pair
<
std
::
string_view
,
S
>
,
9
>
unitMap
{
{
{
""
,
S
(
1
)},
// assume seconds if no unit is given
{
"s"
,
S
(
1
)},
{
"ns"
,
duration_cast
<
S
>
(
std
::
chrono
::
nanoseconds
(
1
))},
...
...
@@ -38,13 +41,21 @@ void toDuration(std::chrono::duration<Rep, Period>& duration, const std::string&
// After requiring cpp20, we can use the aliases in std::chrono
{
"d"
,
duration_cast
<
S
>
(
std
::
chrono
::
hours
(
24
))},
{
"y"
,
duration_cast
<
S
>
(
std
::
chrono
::
seconds
(
31556952
))}
// to match cpp20, see https://en.cppreference.com/w/cpp/chrono/duration
};
}
};
const
auto
unitIt
=
std
::
find_if
(
s
.
rbegin
(),
s
.
rend
(),
[]
(
const
auto
&
c
)
{
return
!
std
::
isalpha
(
c
);
});
const
auto
unitPos
=
s
.
size
()
-
std
::
distance
(
s
.
rbegin
(),
unitIt
);
const
auto
number
=
std
::
stod
(
s
.
substr
(
0
,
unitPos
));
const
auto
unit
=
s
.
substr
(
unitPos
);
duration
=
duration_cast
<
std
::
chrono
::
duration
<
Rep
,
Period
>>
(
number
*
unitMap
.
at
(
unit
));
const
auto
conversion
=
[
&
]
()
{
const
auto
it
=
std
::
find_if
(
unitMap
.
begin
(),
unitMap
.
end
(),
[
u
=
std
::
string_view
{
unit
}]
(
const
auto
&
p
)
{
return
p
.
first
==
u
;
});
if
(
it
==
unitMap
.
end
())
DUNE_THROW
(
Dune
::
InvalidStateException
,
"Unsupported unit "
<<
unit
<<
"."
);
return
it
->
second
;
}
();
duration
=
duration_cast
<
std
::
chrono
::
duration
<
Rep
,
Period
>>
(
number
*
conversion
);
}
//! Try to construct an instance of std::chrono::seconds from a string including a unit suffix
...
...
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