8#include <range/v3/view/enumerate.hpp>
9#include <range/v3/view/filter.hpp>
15namespace rng = ranges;
20 inline auto get_size_in_bytes()
22 return static_cast<std::streamsize
>(
sizeof(T));
26 inline auto get_size_in_bytes(
const std::vector<T>& data)
28 return static_cast<std::streamsize
>(
sizeof(T) * data.size());
35 Mille::Mille(std::string_view outFileName,
bool asBinary,
bool writeZero)
41 (asBinary ? (std::ios::binary | std::ios::out | std::ios::trunc) : std::ios::out | std::ios::trunc));
44 throw std::runtime_error(fmt::format(
"Mille::Mille: Could not open {} as output file.", outFileName));
50 if (data_point.
sigma <= 0.)
57 if (
buffer_.get_current_size() == 0)
64 for (
const auto [index, value] :
65 rng::views::enumerate(data_point.
locals) |
66 rng::views::filter([
this](
const auto& index_deriv)
67 { return index_deriv.second != 0 or is_zero_written_; }))
69 buffer_.add_entry(
static_cast<int>(index + 1), value);
74 for (
const auto& [label, deriv] : data_point.
globals)
80 buffer_.add_entry(label, deriv);
84 fmt::print(stderr,
"Mille::mille: Invalid label {} <= 0 or > ", label);
92 if (special_data.empty())
98 throw std::logic_error(
"Mille::special: Special values already stored for this record.");
100 if (
buffer_.get_current_size() == 0)
106 buffer_.add_entry(0, -
static_cast<float>(special_data.size()));
107 for (
const auto& [index, value] : special_data)
109 buffer_.add_entry(index, value);
126 const auto data_size =
static_cast<int>(
buffer_.get_current_size());
127 output_file_.write(
reinterpret_cast<const char*
>(&data_size), get_size_in_bytes<
decltype(data_size)>());
129 get_size_in_bytes(
buffer_.get_values()));
131 get_size_in_bytes(
buffer_.get_indices()));
146 throw std::runtime_error(
147 fmt::format(
"Mille::checkBufferSize: Buffer too short ({}), \n need space for nLocal ({}) \nGlobal "
148 "({}) 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