TRIQS/h5 1.3.0
C++ interface to HDF5
Loading...
Searching...
No Matches

Detailed Description

Specialized functions to read/write a std::vector from/to HDF5.

The following code writes a std::vector to an HDF5 file, reads the same vector and outputs it to stdout:

#include <h5/h5.hpp>
#include <iostream>
#include <vector>
int main() {
// create file in read/write mode
h5::file file("vec.h5", 'w');
// write vector
h5::write(file, "myvec", std::vector<double> { 1.1, 2.2, 3.3 });
// read into vector
std::vector<double> vec;
h5::read(file, "myvec", vec);
// output vector
for (auto x : vec) std::cout << x << std::endl;
}
A handle to an HDF5 file.
Definition file.hpp:43
T read(group g, std::string const &key)
Generic implementation for reading from an HDF5 dataset/subgroup.
Definition generic.hpp:73
void write(group g, std::string const &key, T const &x, auto const &...args)
Generic implementation for writing a variable to an HDF5 dataset/subgroup.
Definition generic.hpp:105
Includes all relevant h5 headers.

Output:

1.1
2.2
3.3

Contents of vec.h5:

HDF5 "vec.h5" {
GROUP "/" {
DATASET "myvec" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 3 ) / ( 3 ) }
DATA {
(0): 1.1, 2.2, 3.3
}
}
}
}

Classes

struct  h5::hdf5_format_impl< std::vector< std::string > >
 Specialization of h5::hdf5_format_impl for std::vector<std::string>. More...
 
struct  h5::hdf5_format_impl< std::vector< T > >
 Specialization of h5::hdf5_format_impl for std::vector. More...
 

Functions

void h5::from_char_buf (char_buf const &cb, std::vector< std::string > &v)
 Create a vector of strings from an h5::char_buf.
 
void h5::from_char_buf (char_buf const &cb, std::vector< std::vector< std::string > > &v)
 Create a vector of vectors of strings from an h5::char_buf.
 
template<typename T >
void h5::h5_read (group g, std::string name, std::vector< T > &v)
 Read a std::vector from an HDF5 dataset/subgroup.
 
void h5::h5_read_attribute (object obj, std::string const &name, std::vector< std::string > &v)
 Read a vector of strings from an HDF5 attribute.
 
void h5::h5_read_attribute (object obj, std::string const &name, std::vector< std::vector< std::string > > &v)
 Read a vector of vectors of strings from an HDF5 attribute.
 
template<typename T >
void h5::h5_write (group g, std::string const &name, std::vector< T > const &v)
 Write a std::vector to an HDF5 dataset/subgroup.
 
void h5::h5_write_attribute (object obj, std::string const &name, std::vector< std::string > const &v)
 Write a vectors of strings to an HDF5 attribute.
 
void h5::h5_write_attribute (object obj, std::string const &name, std::vector< std::vector< std::string > > const &v)
 Write a vector of vectors of strings to an HDF5 attribute.
 
char_buf h5::to_char_buf (std::vector< std::string > const &v)
 Create an h5::char_buf from a vector of strings.
 
char_buf h5::to_char_buf (std::vector< std::vector< std::string > > const &v)
 Create an h5::char_buf from a vector of vectors of strings.
 

Function Documentation

◆ from_char_buf() [1/2]

void h5::from_char_buf ( char_buf const & cb,
std::vector< std::string > & v )

#include <h5/stl/vector.cpp>

Create a vector of strings from an h5::char_buf.

Parameters
cbh5::char_buf.
vVector of strings.

Definition at line 73 of file vector.cpp.

◆ from_char_buf() [2/2]

void h5::from_char_buf ( char_buf const & cb,
std::vector< std::vector< std::string > > & v )

#include <h5/stl/vector.cpp>

Create a vector of vectors of strings from an h5::char_buf.

Parameters
cbh5::char_buf.
vVector of vectors of strings.

Definition at line 90 of file vector.cpp.

◆ h5_read()

template<typename T >
void h5::h5_read ( group g,
std::string name,
std::vector< T > & v )

#include <h5/stl/vector.hpp>

Read a std::vector from an HDF5 dataset/subgroup.

Depending on the type of T, the following is read:

  • If T is a simple type (arithmetic or complex), a 1d dataset is read.
  • If T is std::string, an h5::char_buf is read, i.e. a 2d dataset of char with dimensions (length of vector, max length of strings).
  • Otherwise, it opens a subgroup and reads each element from the subgroup.
Template Parameters
TValue tupe of std::vector.
Parameters
gh5::group containing the dataset/subgroup.
nameName of the dataset/subgroup from which the std::vector is read.
vstd::vector to read into.

Definition at line 149 of file vector.hpp.

◆ h5_read_attribute() [1/2]

void h5::h5_read_attribute ( object obj,
std::string const & name,
std::vector< std::string > & v )

#include <h5/stl/vector.cpp>

Read a vector of strings from an HDF5 attribute.

Parameters
objParent h5::object to which the attribute is attached.
nameName of the attribute.
vVector of strings to read into.

Definition at line 116 of file vector.cpp.

◆ h5_read_attribute() [2/2]

void h5::h5_read_attribute ( object obj,
std::string const & name,
std::vector< std::vector< std::string > > & v )

#include <h5/stl/vector.cpp>

Read a vector of vectors of strings from an HDF5 attribute.

Parameters
objh5::object to which the attribute is attached.
nameName of the attribute.
vVector of vectors of strings to read into.

Definition at line 122 of file vector.cpp.

◆ h5_write()

template<typename T >
void h5::h5_write ( group g,
std::string const & name,
std::vector< T > const & v )

#include <h5/stl/vector.hpp>

Write a std::vector to an HDF5 dataset/subgroup.

Depending on the type of T, the following is written:

  • If T is a simple type (arithmetic or complex), a 1d dataset is written.
  • If T is std::string, an h5::char_buf is written, i.e. a 2d dataset of char with dimensions (length of vector, max length of strings).
  • Otherwise, it creates a subgroup and writes each element to the subgroup.
Template Parameters
TValue tupe of std::vector.
Parameters
gh5::group in which the dataset/subgroup is created.
nameName of the dataset/subgroup to which the std::vector is written.
vstd::vector to be written.

Definition at line 119 of file vector.hpp.

◆ h5_write_attribute() [1/2]

void h5::h5_write_attribute ( object obj,
std::string const & name,
std::vector< std::string > const & v )

#include <h5/stl/vector.cpp>

Write a vectors of strings to an HDF5 attribute.

Parameters
objh5::object to which the attribute is attached.
nameName of the attribute.
vVector of strings to be written.

Definition at line 110 of file vector.cpp.

◆ h5_write_attribute() [2/2]

void h5::h5_write_attribute ( object obj,
std::string const & name,
std::vector< std::vector< std::string > > const & v )

#include <h5/stl/vector.cpp>

Write a vector of vectors of strings to an HDF5 attribute.

Parameters
objh5::object to which the attribute is attached.
nameName of the attribute.
vVector of vectors of strings to be written.

Definition at line 112 of file vector.cpp.

◆ to_char_buf() [1/2]

char_buf h5::to_char_buf ( std::vector< std::string > const & v)

#include <h5/stl/vector.cpp>

Create an h5::char_buf from a vector of strings.

Parameters
vVector of strings.
Returns
h5::char_buf containing the strings.

Definition at line 33 of file vector.cpp.

◆ to_char_buf() [2/2]

char_buf h5::to_char_buf ( std::vector< std::vector< std::string > > const & v)

#include <h5/stl/vector.cpp>

Create an h5::char_buf from a vector of vectors of strings.

Parameters
vVector of a vector of strings.
Returns
h5::char_buf containing the strings.

Definition at line 52 of file vector.cpp.