43 template<
typename NamedTuple>
57 void append(
const NamedTuple& record);
61 using Tuple =
typename NamedTuple::Tuple;
68 template<std::size_t...
I>
69 void write_record(
const NamedTuple& record, std::index_sequence<I...>);
75 namespace io_npy_impl {
102 template<
typename... Types>
103 constexpr std::array<
const char*,
sizeof...(Types)>
105 return {kNumpyDtypeCode<typename std::decay<Types>::type>...};
118 bool is_little_endian =
119 (
x.c[0] == 0xD) and (
x.c[1] == 0xC) and (
x.c[2] == 0xB) and (
x.c[3] == 0xA);
122 return is_little_endian ?
'<' :
'>';
125 template<
typename NamedTuple>
130 auto names = nt.names();
134 for (decltype(n) i = 0; i <
n; ++i) {
138 descr += endianness_modifier;
153 template<
typename NamedTuple>
155 const std::string& path)
156 : m_fixed_header_length(0), m_num_tuples(0) {
158 m_file.exceptions(std::ofstream::badbit | std::ofstream::failbit);
160 path, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
168 template<
typename NamedTuple>
170 if (!m_file.is_open()) {
173 write_header(m_num_tuples);
177 template<
typename NamedTuple>
185 template<
typename NamedTuple>
190 header +=
"\x93NUMPY";
192 header +=
static_cast<char>(0x1);
193 header +=
static_cast<char>(0x0);
195 header +=
static_cast<char>(0xAF);
196 header +=
static_cast<char>(0xFE);
198 header +=
"{'descr': ";
200 header +=
", 'fortran_order': False";
201 header +=
", 'shape': (";
205 while (((header.size() + 1) % 16) != 0) {
211 if (m_fixed_header_length == 0) {
212 m_fixed_header_length = header.size();
214 while (header.size() < m_fixed_header_length) {
220 std::size_t header_length = header.size() - 10;
221 header[8] =
static_cast<char>(header_length >> 0);
222 header[9] =
static_cast<char>(header_length >> 8);
224 m_file.write(header.data(), header.size());
227 template<
typename NamedTuple>
228 template<std::size_t...
I>
231 const NamedTuple& record, std::index_sequence<I...>) {
234 using Vacuum =
int[];
235 (
void)Vacuum{(write_bytes(&get<I>(record)), 0)...};
238 template<
typename NamedTuple>
242 m_file.write(reinterpret_cast<const char*>(ptr),
sizeof(
T));