TRIQS/h5
2.0.0
C++ interface to HDF5
Toggle main menu visibility
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1
// Copyright (c) 2019-2024 Simons Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0.txt
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell, chuffa
16
21
22
#ifndef LIBH5_STL_MAP_HPP
23
#define LIBH5_STL_MAP_HPP
24
25
#include "
../format.hpp
"
26
#include "
../group.hpp
"
27
#include "
./string.hpp
"
28
29
#include <map>
30
#include <string>
31
#include <type_traits>
32
#include <utility>
33
34
namespace
h5 {
35
40
46
template
<
typename
Key,
typename
T,
typename
Compare>
47
struct
hdf5_format_impl
<std::map<Key, T, Compare>> {
48
static
std::string invoke() {
return
std::is_same_v<Key, std::string> ?
"Dict"
:
"DictNonStrKey"
; }
49
};
50
61
template
<
typename
Key,
typename
T,
typename
Compare>
62
void
h5_write
(
group
g, std::string
const
&name, std::map<Key, T, Compare>
const
&m) {
63
// create the subgroup and write the hdf5_format tag
64
auto
gr = g.
create_group
(name);
65
write_hdf5_format
(gr, m);
66
67
// write element by element
68
if
constexpr
(std::is_same_v<Key, std::string>) {
69
// if key is a string, use it for the dataset name
70
for
(
auto
const
&[key, val] : m)
h5_write
(gr, key, val);
71
}
else
{
72
// otherwise, create a subgroup for each key-value pair
73
int
idx = 0;
74
for
(
auto
const
&[key, val] : m) {
75
auto
element_gr = gr.create_group(std::to_string(idx));
76
h5_write
(element_gr,
"key"
, key);
77
h5_write
(element_gr,
"val"
, val);
78
++idx;
79
}
80
}
81
}
82
93
template
<
typename
Key,
typename
T,
typename
Compare>
94
void
h5_read
(
group
g, std::string
const
&name, std::map<Key, T, Compare> &m) {
95
// open the subgroup and clear the map
96
auto
gr = g.
open_group
(name);
97
m.clear();
98
99
// loop over all subgroups and datasets in the current group
100
for
(
auto
const
&x : gr.get_all_subgroup_dataset_names()) {
101
T val;
102
if
constexpr
(std::is_same_v<Key, std::string>) {
103
// if key is a string, read from the dataset with the same name
104
h5_read
(gr, x, val);
105
m.emplace(x, std::move(val));
106
}
else
{
107
// otherwise, read from the subgroup
108
auto
element_gr = gr.open_group(x);
109
Key key;
110
h5_read
(element_gr,
"key"
, key);
111
h5_read
(element_gr,
"val"
, val);
112
m.emplace(std::move(key), std::move(val));
113
}
114
}
115
}
116
118
119
}
// namespace h5
120
121
#endif
// LIBH5_STL_MAP_HPP
h5::group
A handle to an HDF5 group.
Definition
group.hpp:44
h5::group::create_group
group create_group(std::string const &key, bool delete_if_exists=true) const
Create a subgroup with the given key in the group.
Definition
group.cpp:109
h5::group::open_group
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
Definition
group.cpp:96
format.hpp
Provides utilities for reading and writing hdf5_format tags.
group.hpp
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
h5::write_hdf5_format
void write_hdf5_format(object obj)
Write an hdf5_format tag for type T to an HDF5 attribute with the name 'Format'.
Definition
format.hpp:115
h5::h5_read
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition
generic.hpp:53
h5::h5_write
void h5_write(group g, std::string const &name, T const &x)
Write a scalar to an HDF5 dataset.
Definition
scalar.hpp:71
string.hpp
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
h5::hdf5_format_impl
Default type trait to get the hdf5_format tag of type T by calling its static member function T::hdf5...
Definition
format.hpp:52
h5
stl
map.hpp
Generated by
1.17.0