-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUnitDimension.cpp
59 lines (56 loc) · 1.38 KB
/
UnitDimension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "openPMD/UnitDimension.hpp"
#include <algorithm>
#include <iterator>
namespace openPMD::unit_representations
{
auto asArray(AsMap const &udim) -> AsArray
{
AsArray res{};
auxiliary::fromMapOfUnitDimension(res.data(), udim);
return res;
}
auto asMap(AsArray const &array, bool skip_zeros) -> AsMap
{
AsMap udim;
for (size_t i = 0; i < array.size(); ++i)
{
if (!skip_zeros || array[i] != 0)
{
udim[static_cast<UnitDimension>(i)] = array[i];
}
}
return udim;
}
auto asArrays(AsMaps const &vec) -> AsArrays
{
AsArrays res;
res.reserve(vec.size());
std::transform(
vec.begin(), vec.end(), std::back_inserter(res), [](auto const &map) {
return asArray(map);
});
return res;
}
auto asMaps(AsArrays const &vec, bool skip_zeros) -> AsMaps
{
AsMaps res;
res.reserve(vec.size());
std::transform(
vec.begin(),
vec.end(),
std::back_inserter(res),
[&](auto const &array) { return asMap(array, skip_zeros); });
return res;
}
namespace auxiliary
{
void fromMapOfUnitDimension(
double *cursor, std::map<UnitDimension, double> const &udim)
{
for (auto [unit, exponent] : udim)
{
cursor[static_cast<uint8_t>(unit)] = exponent;
}
}
} // namespace auxiliary
} // namespace openPMD::unit_representations