9#include <range/v3/view/enumerate.hpp>
10#include <range/v3/view/filter.hpp>
16namespace rng = ranges;
21 inline auto get_size_in_bytes()
23 return static_cast<std::streamsize
>(
sizeof(T));
27 inline auto get_size_in_bytes(
const std::vector<T>& data)
29 return static_cast<std::streamsize
>(
sizeof(T) * data.size());
36 Mille::Mille(std::string_view outFileName,
bool asBinary,
bool writeZero)
42 (asBinary ? (std::ios::binary | std::ios::out | std::ios::trunc) : std::ios::out | std::ios::trunc));
45 throw std::runtime_error(fmt::format(
"Mille::Mille: Could not open {} as output file.", outFileName));
51 if (data_point.
sigma <= 0.)
58 if (
buffer_.get_current_size() == 0)
65 for (
const auto [index, value] :
66 rng::views::enumerate(data_point.
locals) |
67 rng::views::filter([
this](
const auto& index_deriv)
68 { return index_deriv.second != 0 or is_zero_written_; }))
70 buffer_.add_entry(
static_cast<int>(index + 1), value);
75 for (
const auto& [label, deriv] : data_point.
globals)
81 buffer_.add_entry(label, deriv);
85 fmt::print(stderr,
"Mille::mille: Invalid label {} <= 0 or > ", label);
93 if (special_data.empty())
99 throw std::logic_error(
"Mille::special: Special values already stored for this record.");
101 if (
buffer_.get_current_size() == 0)
107 buffer_.add_entry(0, -
static_cast<float>(special_data.size()));
108 for (
const auto& [index, value] : special_data)
110 buffer_.add_entry(index, value);
127 const auto data_size =
static_cast<int>(
buffer_.get_current_size());
128 output_file_.write(
reinterpret_cast<const char*
>(&data_size), get_size_in_bytes<
decltype(data_size)>());
130 get_size_in_bytes(
buffer_.get_values()));
132 get_size_in_bytes(
buffer_.get_indices()));
147 throw std::runtime_error(
148 fmt::format(
"Mille::checkBufferSize: Buffer too short ({}), \n need space for nLocal ({}) \nGlobal "
149 "({}) local/global derivatives, {} already stored!",
void write_to_non_binary()
void mille(const MilleDataPoint &data_point)
void check_buffer_size(std::size_t nLocal, std::size_t nGlobal)
Mille(std::string_view outFileName, bool asBinary=true, bool writeZero=false)
std::ofstream output_file_
static constexpr unsigned int max_label_size_
std::size_t max_buffer_size_
MilleBuffer< int, float > buffer_
void special(const std::vector< std::pair< int, float > > &special_data)
std::vector< std::pair< int, float > > globals
std::vector< float > locals