4 std::ofstream out(path_);
5 if (!out)
throw std::runtime_error(
"Cannot open CSV file: " + path_);
9 std::vector<std::string> header;
11 for (
const auto& v : ds.
vars) header.push_back(v.name);
14 if (outCols.empty()) {
15 std::unordered_set<std::string> uniq;
16 for (
const auto& p : ds.
points)
17 for (
const auto& kv : p.y) uniq.insert(kv.first);
19 outCols.assign(uniq.begin(), uniq.end());
20 std::sort(outCols.begin(), outCols.end());
23 if (!spec.
keys.empty()) {
24 std::unordered_set<std::string> wanted(spec.
keys.begin(), spec.
keys.end());
25 std::vector<std::string> filtered;
26 filtered.reserve(outCols.size());
27 for (
auto& k : outCols)
if (wanted.count(k)) filtered.push_back(k);
28 outCols = std::move(filtered);
31 header.insert(header.end(), outCols.begin(), outCols.end());
34 write_row(out, header, sep);
37 for (
const auto& p : ds.
points) {
38 std::vector<std::string> row;
39 row.reserve(header.size());
41 for (
size_t i = 0; i < ds.
vars.size(); ++i) {
42 double val = (i < p.x.size()) ? p.x[i] : 0.0;
43 row.push_back(double_to_string(val));
46 for (
const auto& col : outCols) {
47 auto it = p.y.find(col);
48 if (it == p.y.end()) row.push_back(
"");
52 write_row(out, row, sep);