TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches
vector.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
16
22#ifndef LIBH5_STL_VECTOR_HPP
23#define LIBH5_STL_VECTOR_HPP
24
25#include "./string.hpp"
26#include "../complex.hpp"
27#include "../format.hpp"
28#include "../group.hpp"
29#include "../scalar.hpp"
30#include "../utils.hpp"
31
32#include <string>
33#include <vector>
34#include <type_traits>
35
36namespace h5 {
37
38 namespace array_interface {
39
48 template <typename T>
49 array_view array_view_from_vector(std::vector<T> const &v) {
50 array_view res{hdf5_type<T>(), (void *)v.data(), 1, is_complex_v<std::decay_t<T>>};
51 res.slab.count[0] = v.size();
52 res.parent_shape[0] = v.size();
53 return res;
54 }
55
56 } // namespace array_interface
57
63 // Specialization of h5::hdf5_format_impl for std::vector<std::string>.
64 H5_SPECIALIZE_FORMAT2(std::vector<std::string>, vector<string>);
65
67 template <typename T>
68 struct hdf5_format_impl<std::vector<T>> {
69 static std::string invoke() { return "List"; }
70 };
71
78 char_buf to_char_buf(std::vector<std::string> const &v);
79
86 char_buf to_char_buf(std::vector<std::vector<std::string>> const &v);
87
94 void from_char_buf(char_buf const &cb, std::vector<std::string> &v);
95
102 void from_char_buf(char_buf const &cb, std::vector<std::vector<std::string>> &v);
103
118 template <typename T>
119 void h5_write(group g, std::string const &name, std::vector<T> const &v) {
120 if constexpr (std::is_arithmetic_v<T> or is_complex_v<T>) {
121 // vector of arithmetic/complex types
123 } else if constexpr (std::is_same_v<T, std::string> or std::is_same_v<T, std::vector<std::string>>) {
124 // vector (of vectors) of strings
125 h5_write(g, name, to_char_buf(v));
126 } else {
127 // vector of generic types
128 auto gr = g.create_group(name);
129 write_hdf5_format(gr, v);
130 for (int i = 0; i < v.size(); ++i) h5_write(gr, std::to_string(i), v[i]);
131 }
132 }
133
148 template <typename T>
149 void h5_read(group g, std::string name, std::vector<T> &v) {
150 // throw exception if no link with the given name exists
151 if (not g.has_key(name)) throw make_runtime_error("Error in h5_read: Dataset/Subgroup with name ", name, " does not exist");
152
153 if (g.has_subgroup(name)) {
154 // vector of generic type
155 auto g2 = g.open_group(name);
156 v.resize(g2.get_all_dataset_names().size() + g2.get_all_subgroup_names().size());
157 for (int i = 0; i < v.size(); ++i) { h5_read(g2, std::to_string(i), v[i]); }
158 } else {
159 if constexpr (std::is_arithmetic_v<T> or is_complex_v<T>) {
160 // vector of arithmetic/complex types
161 auto ds_info = array_interface::get_dataset_info(g, name);
162 if (ds_info.rank() != 1 + is_complex_v<T>)
163 throw make_runtime_error("Error in h5_read: Reading a vector from an array of rank ", ds_info.rank(), " is not allowed");
164 v.resize(ds_info.lengths[0]);
166 } else if constexpr (std::is_same_v<T, std::string> or std::is_same_v<T, std::vector<std::string>>) {
167 // vector of strings or vector of vector of strings
168 char_buf cb;
169 h5_read(g, name, cb);
170 from_char_buf(cb, v);
171 } else {
172 // unsupported type
173 throw make_runtime_error("Error in h5_read: HDF5 datatype not supported for reading into a std::vector");
174 }
175 }
176 }
177
185 void h5_write_attribute(object obj, std::string const &name, std::vector<std::vector<std::string>> const &v);
186
194 void h5_read_attribute(object obj, std::string const &name, std::vector<std::vector<std::string>> &v);
195
203 void h5_write_attribute(object obj, std::string const &name, std::vector<std::string> const &v);
204
212 void h5_read_attribute(object obj, std::string const &name, std::vector<std::string> &v);
213
216} // namespace h5
217
218#endif // LIBH5_STL_VECTOR_HPP
A handle to an HDF5 group.
Definition group.hpp:44
bool has_subgroup(std::string const &key) const
Check if a subgroup with the given key exists in the group and is accessible.
Definition group.cpp:55
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
group open_group(std::string const &key) const
Open a subgroup with the given key in the group.
Definition group.cpp:96
bool has_key(std::string const &key) const
Check if a link with the given key exists in the group.
Definition group.cpp:53
Provides a compound type and type traits for complex numbers.
Provides utilities for reading and writing hdf5_format tags.
Provides a handle to an HDF5 group and various methods to simplify the creation/opening of subgroups,...
void write_hdf5_format(object obj, T const &)
Write an hdf5_format tag for type T to an HDF5 attribute with the name 'Format' using template argume...
Definition format.hpp:113
constexpr bool is_complex_v
Boolean type trait set to true for std::complex types.
Definition complex.hpp:64
datatype hdf5_type()
Map a given C++ type to an HDF5 datatype.
Definition object.hpp:156
void read(group g, std::string const &name, array_view v, hyperslab sl)
Read a given hyperslab from an HDF5 dataset into an array view.
dataset_info get_dataset_info(dataset ds)
Retrieve the shape and the h5::datatype from a dataset.
void write(group g, std::string const &name, array_view const &v, bool compress)
Write an array view to an HDF5 dataset.
array_view array_view_from_vector(std::vector< T > const &v)
Create an h5::array_interface::array_view for a std::vector.
Definition vector.hpp:49
T h5_read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:51
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition generic.hpp:120
void h5_write(group g, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 dataset.
Definition scalar.hpp:70
void h5_write_attribute(object obj, std::string const &name, T const &x) H5_REQUIRES(std
Write a scalar to an HDF5 attribute.
Definition scalar.hpp:125
void from_char_buf(char_buf const &cb, std::vector< std::string > &v)
Create a vector of strings from an h5::char_buf.
Definition vector.cpp:73
char_buf to_char_buf(std::vector< std::string > const &v)
Create an h5::char_buf from a vector of strings.
Definition vector.cpp:33
std::runtime_error make_runtime_error(Ts const &...ts)
Create a std::runtime_error with an error message constructed from the given arguments.
Definition utils.hpp:69
Provides a generic interface to read/write scalars from/to HDF5.
Provides functions to read/write std::string, char* and h5::char_buf objects from/to HDF5.
Struct representing a view on an n-dimensional array/dataspace.
hyperslab slab
h5::array_interface::hyperslab specifying the selection of the view.
v_t count
Number of elements or blocks to select along each dimension.
Stores an arbitrary number of strings in a 1-dimensional std::vector<char>.
Definition string.hpp:160
Default type trait to get the hdf5_format tag of type T by calling its static member function T::hdf5...
Definition string.hpp:34
Provides some utility functions for h5.