TRIQS/h5
2.0.0
C++ interface to HDF5
Toggle main menu visibility
Loading...
Searching...
No Matches
vector.cpp
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: Philipp Dumitrescu, Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
21
22
#include "
./vector.hpp
"
23
24
#include <hdf5.h>
25
#include <hdf5_hl.h>
26
27
#include <algorithm>
28
#include <cstring>
29
#include <algorithm>
30
31
namespace
h5 {
32
33
char_buf
to_char_buf
(std::vector<std::string>
const
&v) {
34
// get size of longest string
35
size_t
s = 1;
36
for
(
auto
&x : v) s = std::max(s, x.size() + 1);
37
38
// copy each string to a buffer and pad with zeros
39
std::vector<char> buf;
40
buf.resize(std::max(v.size() * s, 1ul), 0x00);
41
size_t
i = 0;
42
for
(
auto
&x : v) {
43
strcpy(&buf[i * s], x.c_str());
44
++i;
45
}
46
47
// return char_buf
48
auto
len =
v_t
{v.size(), s};
49
return
{.buffer = buf, .lengths = len};
50
}
51
52
char_buf
to_char_buf
(std::vector<std::vector<std::string>>
const
&v) {
53
// get size of longest vector and string
54
size_t
s = 1, lv = 0;
55
for
(
auto
&v1 : v) {
56
lv = std::max(lv, v1.size());
57
for
(
auto
&x : v1) s = std::max(s, x.size() + 1);
58
}
59
60
// copy each string to a buffer and pad with zeros
61
std::vector<char> buf;
62
buf.resize(std::max(v.size() * lv * s, 1ul), 0x00);
63
for
(
int
i = 0, k = 0; i < v.size(); i++)
64
for
(
int
j = 0; j < lv; j++, k++) {
65
if
(j < v[i].size()) strcpy(&buf[k * s], v[i][j].c_str());
66
}
67
68
// return char_buf
69
auto
len =
v_t
{v.size(), lv, s};
70
return
{.buffer = buf, .lengths = len};
71
}
72
73
void
from_char_buf
(
char_buf
const
&cb, std::vector<std::string> &v) {
74
// prepare vector
75
v.clear();
76
v.resize(cb.
lengths
[0]);
77
78
// loop over all strings
79
auto
len_string = cb.
lengths
[1];
80
long
i = 0;
81
for
(
auto
&x : v) {
82
// use full range from char_buf and remove null characters
83
const
char
*bptr = &cb.
buffer
[i * len_string];
84
x = std::string(bptr, bptr + len_string);
85
x.erase(std::remove(begin(x), end(x),
'\0'
), end(x));
86
++i;
87
}
88
}
89
90
void
from_char_buf
(
char_buf
const
&cb, std::vector<std::vector<std::string>> &v) {
91
// prepare vector
92
v.clear();
93
v.resize(cb.
lengths
[0]);
94
95
// loop over all vectors and all strings
96
auto
inner_vec_size = cb.
lengths
[1];
97
auto
len_string = cb.
lengths
[2];
98
long
i = 0;
99
for
(
auto
&v_inner : v) {
100
for
(
int
j = 0; j < inner_vec_size; ++j, ++i) {
101
// use full range from char_buf and remove null characters
102
const
char
*bptr = &cb.
buffer
[i * len_string];
103
auto
s = std::string(bptr, bptr + len_string);
104
s.erase(std::remove(begin(s), end(s),
'\0'
), end(s));
105
v_inner.push_back(s);
106
}
107
}
108
}
109
110
void
h5_write_attribute
(
object
obj, std::string
const
&name, std::vector<std::string>
const
&v) {
h5_write_attribute
(obj, name,
to_char_buf
(v)); }
111
112
void
h5_write_attribute
(
object
obj, std::string
const
&name, std::vector<std::vector<std::string>>
const
&v) {
113
h5_write_attribute
(obj, name,
to_char_buf
(v));
114
}
115
116
void
h5_read_attribute
(
object
obj, std::string
const
&name, std::vector<std::string> &v) {
117
char_buf
cb;
118
h5_read_attribute
(obj, name, cb);
119
from_char_buf
(cb, v);
120
}
121
122
void
h5_read_attribute
(
object
obj, std::string
const
&name, std::vector<std::vector<std::string>> &v) {
123
char_buf
cb;
124
h5_read_attribute
(obj, name, cb);
125
from_char_buf
(cb, v);
126
}
127
128
}
// namespace h5
h5::h5_read_attribute
T h5_read_attribute(object obj, std::string const &name)
Generic implementation for reading an HDF5 attribute.
Definition
generic.hpp:125
h5::h5_write_attribute
void h5_write_attribute(object, std::string const &, std::string const &)
Write a std::string to an HDF5 attribute.
Definition
string.cpp:99
h5::from_char_buf
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
h5::to_char_buf
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
h5::v_t
std::vector< hsize_t > v_t
Vector of h5::hsize_t used throughout the h5 library.
Definition
utils.hpp:59
h5::char_buf
Stores an arbitrary number of strings in a 1-dimensional std::vector<char>.
Definition
string.hpp:161
h5::char_buf::lengths
v_t lengths
Stores the number of strings in each dimension and the max. allowed length of the strings + 1.
Definition
string.hpp:166
h5::char_buf::buffer
std::vector< char > buffer
Stores strings in a 1-dimensional vector.
Definition
string.hpp:163
vector.hpp
Provides functions to read/write std::vector objects from/to HDF5.
h5
stl
vector.cpp
Generated by
1.17.0