1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#ifndef PURIFY_DATA_H
#define PURIFY_DATA_H
#include <fstream>
#include <string>
#include <vector>
#include <sys/stat.h>
#include "purify/types.h"
#include "purify/directories.h"
namespace purify {
//! read real values from data file
template <class T>
typename std::enable_if<std::is_scalar<T>::value, std::vector<T>>::type read_data(
const std::string& filename) {
std::ifstream data_file(filename);
if (!data_file) throw std::runtime_error("Test data is missing: " + filename);
std::string s;
// read data
std::vector<T> data;
for (std::string s; std::getline(data_file, s, ',');) {
data.push_back(std::stod(s));
}
return data;
}
//! read complex values from data file
template <class T>
typename std::enable_if<std::is_same<t_complex, T>::value, std::vector<T>>::type read_data(
const std::string& filename) {
std::ifstream data_file(filename);
if (!data_file) throw std::runtime_error("Test data is missing: " + filename);
std::string real = "";
std::string imag = "";
// read data
std::vector<T> data;
for (std::string real; std::getline(data_file, real, ',');) {
real.erase(0, 1);
if (!std::getline(data_file, imag, ')')) break;
data.push_back(T(std::stod(real), std::stod(imag)));
real = "";
imag = "";
}
return data;
}
std::vector<t_complex> read_data(const std::vector<t_real>& input) {
std::vector<t_complex> output;
for (auto a = input.begin(); a != input.end(); a++) output.push_back(t_complex(*a, 0.));
return output;
}
} // namespace purify
#endif
|