13 #include "CSVDatabase.h"
14 #include "Utilities.h"
31 const std::string& file_name,
40 const std::string& file_name,
41 const std::string& type,
56 const std::string& file_name,
57 const int*
const data,
59 const bool distributed)
61 CAROM_VERIFY(!file_name.empty());
62 CAROM_VERIFY(data !=
nullptr);
63 CAROM_VERIFY(nelements > 0);
65 std::ofstream d_fs(file_name.c_str());
66 for (
int i = 0; i < nelements; ++i)
68 d_fs << data[i] << std::endl;
75 const std::string& file_name,
76 const double*
const data,
78 const bool distributed)
80 CAROM_VERIFY(!file_name.empty());
81 CAROM_VERIFY(data !=
nullptr);
82 CAROM_VERIFY(nelements > 0);
84 std::ofstream d_fs(file_name.c_str());
85 d_fs << std::setprecision(16) << std::fixed;
86 for (
int i = 0; i < nelements; ++i)
88 d_fs << data[i] << std::endl;
95 const std::string& file_name,
96 const std::vector<double>& data,
98 const bool distributed)
100 CAROM_VERIFY(!file_name.empty());
101 CAROM_VERIFY(nelements > 0);
102 CAROM_VERIFY(data.size() == nelements);
104 std::ofstream d_fs(file_name.c_str());
105 d_fs << std::setprecision(16) << std::fixed;
106 for (
int i = 0; i < nelements; ++i)
108 d_fs << data[i] << std::endl;
115 const std::string& file_name,
116 const std::vector<std::complex<double>>& data,
119 CAROM_VERIFY(!file_name.empty());
120 CAROM_VERIFY(nelements > 0);
121 CAROM_VERIFY(data.size() == nelements);
123 std::ofstream d_fs(file_name.c_str());
124 d_fs << std::setprecision(16) << std::fixed;
125 for (
int i = 0; i < nelements; ++i)
127 d_fs << std::real(data[i]) <<
"," << std::imag(data[i]) << std::endl;
134 const std::string& file_name,
135 const std::vector<std::string>& data,
138 CAROM_VERIFY(!file_name.empty());
139 CAROM_VERIFY(nelements > 0);
140 CAROM_ASSERT(data !=
nullptr);
142 std::ofstream d_fs(file_name.c_str());
143 for (
int i = 0; i < nelements; ++i)
145 d_fs << data[i] << std::endl;
152 const std::string& file_name,
155 const bool distributed)
157 CAROM_VERIFY(!file_name.empty());
158 #ifndef DEBUG_CHECK_ASSERTIONS
159 CAROM_NULL_USE(nelements);
162 std::ifstream d_fs(file_name.c_str());
165 CAROM_VERIFY(!d_fs.fail());
167 for (
int i = 0; i < nelements; ++i)
170 data[i] = data_entry;
177 const std::string& file_name,
178 std::vector<int> &data,
181 CAROM_VERIFY(!file_name.empty());
182 if (!append) data.clear();
184 std::ifstream d_fs(file_name.c_str());
186 while (d_fs >> data_entry)
188 data.push_back(data_entry);
195 const std::string& file_name,
198 const bool distributed)
200 CAROM_VERIFY(!file_name.empty());
201 #ifndef DEBUG_CHECK_ASSERTIONS
202 CAROM_NULL_USE(nelements);
205 std::ifstream d_fs(file_name.c_str());
206 CAROM_VERIFY(!d_fs.fail());
207 double data_entry = 0.0;
208 for (
int i = 0; i < nelements; ++i)
211 data[i] = data_entry;
218 const std::string& file_name,
221 const std::vector<int>& idx,
222 const bool distributed)
224 CAROM_VERIFY(!file_name.empty());
225 #ifndef DEBUG_CHECK_ASSERTIONS
226 CAROM_NULL_USE(nelements);
235 std::ifstream d_fs(file_name.c_str());
236 CAROM_VERIFY(!d_fs.fail());
238 double data_entry = 0.0;
239 for (
int i = 0; i < nelements; ++i)
244 data[k++] = data_entry;
251 CAROM_VERIFY(k == idx.size());
258 const std::string& file_name,
264 const bool distributed)
266 CAROM_VERIFY(!file_name.empty());
267 #ifndef DEBUG_CHECK_ASSERTIONS
268 CAROM_NULL_USE(nelements);
271 std::ifstream d_fs(file_name.c_str());
272 CAROM_VERIFY(!d_fs.fail());
273 std::string line, data_entry;
275 int curr_block_remaining = block_size;
276 while (count < nelements && d_fs >> line)
278 std::stringstream d_ss(line);
279 while (std::getline(d_ss, data_entry,
','))
287 data[count++] = std::stod(data_entry);
288 curr_block_remaining--;
289 if (curr_block_remaining == 0)
292 curr_block_remaining = block_size;
302 const std::string& file_name,
303 std::vector<double> &data,
306 CAROM_VERIFY(!file_name.empty());
307 if (!append) data.clear();
309 std::ifstream d_fs(file_name.c_str());
311 while (d_fs >> data_entry)
313 data.push_back(data_entry);
320 const std::string& file_name,
321 std::vector<std::string> &data,
324 CAROM_VERIFY(!file_name.empty());
325 if (!append) data.clear();
327 std::ifstream d_fs(file_name.c_str());
328 std::string data_entry;
329 while (std::getline(d_fs, data_entry))
331 data.push_back(data_entry);
338 const std::string& file_name)
341 CAROM_VERIFY(!file_name.empty());
343 std::ifstream d_fs(file_name.c_str());
344 std::string data_entry;
345 while (std::getline(d_fs, data_entry))
void getIntegerVector(const std::string &file_name, std::vector< int > &data, bool append=false)
Reads a vector of integers associated with the supplied filename.
void putDoubleVector(const std::string &file_name, const std::vector< double > &data, int nelements, const bool distributed=false) override
Writes a vector of doubles associated with the supplied filename to the currently open CSV database f...
void putIntegerArray(const std::string &file_name, const int *const data, int nelements, const bool distributed=false) override
Writes an array of integers associated with the supplied filename.
void getIntegerArray(const std::string &file_name, int *data, int nelements, const bool distributed=false) override
Reads an array of integers associated with the supplied filename.
int getLineCount(const std::string &file_name)
Count the number of lines of CSV database file.
CSVDatabase()
Default constructor.
bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL) override
Creates a new CSV database file with the supplied name. NOTE: CSVDatabase does not actually create a ...
bool close()
Closes the currently open CSV database file.
virtual ~CSVDatabase()
Destructor.
void getStringVector(const std::string &file_name, std::vector< std::string > &data, bool append=false)
Reads a vector of strings associated with the supplied filename.
void getDoubleVector(const std::string &file_name, std::vector< double > &data, bool append=false)
Reads a vector of doubles associated with the supplied filename.
void putDoubleArray(const std::string &file_name, const double *const data, int nelements, const bool distributed=false) override
Writes an array of doubles associated with the supplied filename.
void getDoubleArray(const std::string &file_name, double *data, int nelements, const bool distributed=false) override
Reads an array of doubles associated with the supplied filename.
void putStringVector(const std::string &file_name, const std::vector< std::string > &data, int nelements)
Writes a vector of strings associated with the supplied filename.
void putComplexVector(const std::string &file_name, const std::vector< std::complex< double >> &data, int nelements)
Writes a vector of complex doubles associated with the supplied filename.
bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL) override
Opens an existing CSV database file with the supplied name. NOTE: CSVDatabase does not actually open ...
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL)
Creates a new database file with the supplied name.
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL)
Opens an existing database file with the supplied name.