#include "autoopt/importer.hpp" namespace autoopt { std::generator> parse_dat_file( const std::filesystem::path& filename) { // check if file exists if (!std::filesystem::exists(filename)) { throw std::runtime_error("File does not exist: " + filename.string()); } std::fstream file(filename, std::ios::in); std::string line; while (std::getline(file, line)) { if (line.empty() || line == "\r" || line[0] == '#') { continue; } std::istringstream iss(line); double x, y; iss >> x; for (size_t i = 1; i < 5; ++i) { iss >> y; // skip unused columns } co_yield {x, -y}; } co_return; } std::pair, std::vector> parse_row(auto& iter, auto sentinel) { std::vector xs; std::vector ys; xs.push_back((*iter).first); ys.push_back((*iter).second); ++iter; double last_x = xs[0]; bool forward = last_x < (*iter).first; while (iter != sentinel && ((forward && last_x < (*iter).first) || (!forward && last_x > (*iter).first))) { xs.push_back((*iter).first); ys.push_back((*iter).second); last_x = (*iter).first; ++iter; } if (!forward) { std::reverse(xs.begin(), xs.end()); std::reverse(ys.begin(), ys.end()); } return std::make_pair(std::move(xs), std::move(ys)); } raw_data read_data(const std::filesystem::path &filename){ auto lines = parse_dat_file(filename); raw_data result; auto it = lines.begin(); while (it != lines.end()) { auto [xs, ys] = parse_row(it, lines.end()); if (result._xs.rows() == 0) { result._xs = Eigen::VectorXd(xs.size()); std::copy(xs.begin(), xs.end(), result._xs.begin()); } Eigen::VectorXd ys_vec = Eigen::VectorXd(ys.size()); std::copy(ys.begin(), ys.end(), ys_vec.begin()); result._yss.push_back(ys_vec); } return result; } } // namespace autoopt