tmp update

This commit is contained in:
张壹 2024-09-16 14:28:08 +08:00
parent 8c713c576a
commit eb21f77d2d
5 changed files with 401 additions and 313 deletions

View File

@ -36,7 +36,9 @@ int main(int argc, char const *argv[]) try
array<double> A(10, 0.0, 1.0); array<double> A(10, 0.0, 1.0);
A.log2linear(2); A.log2linear(2);
A.show(); A.show();
A.linear2log(2); A.linear2log(2);
A.for_each([](double &a, size_t i){ a += 1;});
A.show(); A.show();
// copy A to a new array // copy A to a new array
@ -53,24 +55,10 @@ int main(int argc, char const *argv[]) try
// create a new 2D array // create a new 2D array
matrix<int> C(5, 5, 1); matrix<int> C(5, 5, 1);
std::cout << "C = " << std::endl; C.sequence(0, 1, 10);
for (int i = 0; i < C.row_size(); i++)
{
for (int j = 0; j < C.col_size(); j++)
{
C[i][j] += i*10 + j;
std::cout << C.at(i,j) << " ";
}
std::cout << std::endl;
}
// access row elements std::cout << "C = \n";
std::cout << "C[3][:] = " << std::endl; C.show();
for (int i = 0; i < C.col_size(); i++)
{
std::cout << C.get(3)[i] << " ";
}
std::cout << std::endl;
// save array to a binary file // save array to a binary file
save_matrix2binary("tmp/array_ex_out", C, "Int"); save_matrix2binary("tmp/array_ex_out", C, "Int");
@ -78,15 +66,9 @@ int main(int argc, char const *argv[]) try
// import 2D array to a new object // import 2D array to a new object
matrix<int> D; matrix<int> D;
read_binary2matrix("tmp/array_ex_out", D); read_binary2matrix("tmp/array_ex_out", D);
std::cout << "D = " << std::endl;
for (int i = 0; i < D.row_size(); i++) std::cout << "D = \n";
{ D.show();
for (int j = 0; j < D.col_size(); j++)
{
std::cout << D[i][j] << " ";
}
std::cout << std::endl;
}
return 0; return 0;
} }
catch(std::exception &e) catch(std::exception &e)

View File

@ -488,7 +488,7 @@ namespace gctl
* @brief element operate function pointer * @brief element operate function pointer
* *
*/ */
typedef void (*foreach_a_ptr)(ArrValType *ele_ptr, size_t id); typedef void (*foreach_a_ptr)(ArrValType &ele_ptr, size_t id);
/** /**
* @brief Operate on each and every element * @brief Operate on each and every element
@ -1235,7 +1235,7 @@ namespace gctl
{ {
for (size_t i = 0; i < length_; i++) for (size_t i = 0; i < length_; i++)
{ {
func(&val_[i], i); func(val_[i], i);
} }
return; return;
} }

View File

@ -229,7 +229,17 @@ namespace gctl
* @param[in] mode Random types. 'RdNormal' for Gaussian distributed numbers and * @param[in] mode Random types. 'RdNormal' for Gaussian distributed numbers and
* 'RdUniform' for even distributed numbers. * 'RdUniform' for even distributed numbers.
*/ */
void random(MatValType np1, MatValType np2, random_type_e mode = RdNormal, unsigned int seed = 0); void random_float(MatValType np1, MatValType np2, random_type_e mode = RdNormal, unsigned int seed = 0);
/**
* @brief Initialize the array with selected random types.
*
* @param[in] np1 Mean (Gauss) or low bound value (Even)
* @param[in] np2 Standard deviation (Gauss) or hig bound value (Even).
* @param[in] mode Random types. 'RdNormal' for Gaussian distributed numbers and
* 'RdUniform' for even distributed numbers.
*/
void random_int(MatValType np1, MatValType np2, unsigned int seed = 0);
/** /**
* @brief * @brief
@ -267,7 +277,7 @@ namespace gctl
* @param row_inc Row increasement. * @param row_inc Row increasement.
* @param col_inc Column increasement. * @param col_inc Column increasement.
*/ */
void sequent(MatValType st_val, MatValType row_inc, MatValType col_inc); void sequence(MatValType st_val, MatValType row_inc, MatValType col_inc);
/** /**
* @brief * @brief
@ -389,7 +399,7 @@ namespace gctl
* @brief element operate function pointer * @brief element operate function pointer
* *
*/ */
typedef void (*foreach_m_ptr)(MatValType *ele_ptr, size_t r_id, size_t c_id); typedef void (*foreach_m_ptr)(MatValType &ele_ptr, size_t r_id, size_t c_id);
/** /**
* @brief Operate on each and every element * @brief Operate on each and every element
@ -508,7 +518,7 @@ namespace gctl
{ {
if (row_len == 0 || col_len == 0) if (row_len == 0 || col_len == 0)
{ {
throw std::invalid_argument("Invalid matrix size. gctl::matrix<T>::resize(...)"); throw std::invalid_argument("[gctl::matrix<T>::resize] Invalid matrix size.");
} }
// nothing needs to be done // nothing needs to be done
@ -572,7 +582,7 @@ namespace gctl
{ {
if (init_mat == nullptr) if (init_mat == nullptr)
{ {
throw std::domain_error("Invalid pointer. gctl::matrix<T>::resize(...)"); throw std::domain_error("[gctl::matrix<T>::resize] Invalid pointer.");
} }
resize(row_len, col_len); resize(row_len, col_len);
@ -592,7 +602,7 @@ namespace gctl
{ {
if (init_mat == nullptr) if (init_mat == nullptr)
{ {
throw std::domain_error("Invalid pointer. gctl::matrix<T>::resize(...)"); throw std::domain_error("[gctl::matrix<T>::resize] Invalid pointer.");
} }
resize(row_len, col_len); resize(row_len, col_len);
@ -631,19 +641,16 @@ namespace gctl
{ {
if (!b.empty()) if (!b.empty())
{ {
size_t col_s = b[0].size(); size_t max_col = 0;
for (size_t i = 1; i < b.size(); i++) for (size_t i = 0; i < b.size(); i++)
{ {
if (col_s != b[i].size()) max_col = std::max(max_col, b[i].size());
{
throw std::runtime_error("The input 2-D vector is not neatly arranged. From matrix<T>::resize(...)");
}
} }
resize(b.size(), col_s); resize(b.size(), max_col, 0);
for (size_t i = 0; i < row_length; i++) for (size_t i = 0; i < row_length; i++)
{ {
for (size_t j = 0; j < col_length; j++) for (size_t j = 0; j < b[i].size(); j++)
{ {
val[i][j] = b[i][j]; val[i][j] = b[i][j];
} }
@ -660,7 +667,7 @@ namespace gctl
{ {
if (b.size() != row_len*col_len) if (b.size() != row_len*col_len)
{ {
throw std::runtime_error("Invalid initializing parameters. From matrix<T>::resize(...)"); throw std::runtime_error("[gctl::matrix<T>::resize] Invalid matrix parameters.");
} }
resize(row_len, col_len); resize(row_len, col_len);
@ -711,12 +718,10 @@ namespace gctl
} }
template <typename MatValType> template <typename MatValType>
void matrix<MatValType>::random(MatValType np1, MatValType np2, random_type_e mode, unsigned int seed) void matrix<MatValType>::random_float(MatValType np1, MatValType np2, random_type_e mode, unsigned int seed)
{ {
if (empty()) static_assert(std::is_floating_point<MatValType>::value,
{ "gctl::matrix<T>::random_float(...) could only be used with a float point type.");
throw gctl::runtime_error("The matrix is not initialized. From gctl::matrix<T>::random(...)");
}
if (seed == 0) seed = std::chrono::system_clock::now().time_since_epoch().count(); if (seed == 0) seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed); std::default_random_engine generator(seed);
@ -747,6 +752,27 @@ namespace gctl
return; return;
} }
template <typename MatValType>
void matrix<MatValType>::random_int(MatValType np1, MatValType np2, unsigned int seed)
{
static_assert(std::is_integral<MatValType>::value,
"gctl::matrix<T>::random_int(...) could only be used with an integral type.");
if (seed == 0) seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
//添加均匀分布的随机值
std::uniform_int_distribution<MatValType> dist(np1, np2);
for (size_t i = 0; i < row_length; i++)
{
for (size_t j = 0; j < col_length; j++)
{
val[i][j] = dist(generator);
}
}
return;
}
template <typename MatValType> template <typename MatValType>
void matrix<MatValType>::assign_all(MatValType in_val) void matrix<MatValType>::assign_all(MatValType in_val)
{ {
@ -860,7 +886,7 @@ namespace gctl
} }
template <typename MatValType> template <typename MatValType>
void matrix<MatValType>::sequent(MatValType st_val, MatValType row_inc, MatValType col_inc) void matrix<MatValType>::sequence(MatValType st_val, MatValType row_inc, MatValType col_inc)
{ {
for (size_t i = 0; i < row_length; i++) for (size_t i = 0; i < row_length; i++)
{ {
@ -877,7 +903,7 @@ namespace gctl
{ {
#ifdef GCTL_BOUND_CHECK #ifdef GCTL_BOUND_CHECK
if (row_index >= row_length || col_index >= col_length) if (row_index >= row_length || col_index >= col_length)
throw std::out_of_range("Invalid index. gctl::matrix<T>::get(...)"); throw std::out_of_range("[gctl::matrix<T>::get] Invalid index.");
#endif // GCTL_BOUND_CHECK #endif // GCTL_BOUND_CHECK
return val[row_index]; return val[row_index];
@ -888,7 +914,7 @@ namespace gctl
{ {
#ifdef GCTL_BOUND_CHECK #ifdef GCTL_BOUND_CHECK
if (row_index >= row_length || col_index >= col_length) if (row_index >= row_length || col_index >= col_length)
throw std::out_of_range("Invalid index. gctl::matrix<T>::at(...)"); throw std::out_of_range("[gctl::matrix<T>::at] Invalid index.");
#endif // GCTL_BOUND_CHECK #endif // GCTL_BOUND_CHECK
return val[row_index][col_index]; return val[row_index][col_index];
@ -899,7 +925,7 @@ namespace gctl
{ {
#ifdef GCTL_BOUND_CHECK #ifdef GCTL_BOUND_CHECK
if (row_index >= row_length || col_index >= col_length) if (row_index >= row_length || col_index >= col_length)
throw std::out_of_range("Invalid index. gctl::matrix<T>::at(...)"); throw std::out_of_range("[gctl::matrix<T>::at] Invalid index.");
#endif // GCTL_BOUND_CHECK #endif // GCTL_BOUND_CHECK
return val[row_index][col_index]; return val[row_index][col_index];
@ -992,7 +1018,7 @@ namespace gctl
{ {
if (index >= row_length) if (index >= row_length)
{ {
throw std::out_of_range("Invalid index. gctl::matrix<T>::extract(...)"); throw std::out_of_range("[gctl::matrix<T>::extract] Invalid index.");
} }
b.resize(col_length); b.resize(col_length);
@ -1005,7 +1031,7 @@ namespace gctl
if (index >= col_length) if (index >= col_length)
{ {
throw std::out_of_range("Invalid index. Thrown by gctl::matrix<T>::extract(...)"); throw std::out_of_range("[gctl::matrix<T>::extract] Invalid index.");
} }
b.resize(row_length); b.resize(row_length);
@ -1050,7 +1076,7 @@ namespace gctl
{ {
for (size_t j = 0; j < col_length; j++) for (size_t j = 0; j < col_length; j++)
{ {
func(&val[i][j], i, j); func(val[i][j], i, j);
} }
} }
return; return;
@ -1061,9 +1087,10 @@ namespace gctl
{ {
for (size_t i = 0; i < row_length; i++) for (size_t i = 0; i < row_length; i++)
{ {
for (size_t j = 0; j < col_length; j++) os << val[i][0];
for (size_t j = 1; j < col_length; j++)
{ {
os << val[i][j] << sep; os << sep << val[i][j];
} }
os << std::endl; os << std::endl;
} }

View File

@ -34,9 +34,10 @@ gctl::text_descriptor::text_descriptor()
set(); set();
} }
gctl::text_descriptor::text_descriptor(std::string filename, std::string file_exten, char att_sym, char tag_sym, char deli, int h_num) gctl::text_descriptor::text_descriptor(std::string filename, std::string file_exten,
std::string col_order, char deli, char att_sym, char tag_sym, int h_num, int io_psn)
{ {
set(filename, file_exten, att_sym, tag_sym, deli, h_num); set(filename, file_exten, col_order, deli, att_sym, tag_sym, h_num, io_psn);
} }
gctl::text_descriptor::~text_descriptor() gctl::text_descriptor::~text_descriptor()
@ -44,26 +45,70 @@ gctl::text_descriptor::~text_descriptor()
clear(); clear();
} }
void gctl::text_descriptor::set(std::string filename, std::string file_exten, char att_sym, char tag_sym, char deli, int h_num) void gctl::text_descriptor::set(std::string filename, std::string file_exten,
std::string col_order, char deli, char att_sym, char tag_sym, int h_num, int io_psn)
{ {
file_name_ = filename; file_name_ = filename;
file_ext_ = file_exten; file_ext_ = file_exten;
col_str_ = col_order;
att_sym_ = att_sym; att_sym_ = att_sym;
tag_sym_ = tag_sym; tag_sym_ = tag_sym;
delimiter_ = deli; delimiter_ = deli;
head_num_ = h_num; head_num_ = h_num;
float_psn_ = io_psn;
return; return;
} }
void gctl::text_descriptor::clear() void gctl::text_descriptor::clear()
{ {
set();
head_strs_.clear(); head_strs_.clear();
annotates_.clear(); annotates_.clear();
tags_.clear(); tags_.clear();
return; return;
} }
void gctl::text_descriptor::set_float_precision(int psn)
{
float_psn_ = psn;
return;
}
void gctl::text_descriptor::set_head_number(int n)
{
head_num_ = n;
return;
}
void gctl::text_descriptor::set_annotation_symbol(char sym)
{
att_sym_ = sym;
return;
}
void gctl::text_descriptor::set_tag_symbol(char sym)
{
tag_sym_ = sym;
return;
}
void gctl::text_descriptor::set_column_delimeter(char sym)
{
delimiter_ = sym;
return;
}
void gctl::text_descriptor::set_file_extension(std::string ext_s)
{
file_ext_ = ext_s;
return;
}
void gctl::text_descriptor::set_column_order(std::string col_s)
{
col_str_ = col_s;
return;
}
void gctl::read_text_lines(text_descriptor &file_desc, std::vector<std::string> &out_vec, int start_line, int end_line) void gctl::read_text_lines(text_descriptor &file_desc, std::vector<std::string> &out_vec, int start_line, int end_line)
{ {
if (start_line < 0 || start_line > end_line) if (start_line < 0 || start_line > end_line)
@ -276,7 +321,7 @@ void gctl::get_xyz_points(std::string filename, array<point3dc> &out_ps, text_de
{ {
// read file // read file
_2d_vector points_vec; _2d_vector points_vec;
read_text2vector2d(filename, points_vec, desc, file_ext); read_text2vector2d(desc, points_vec);
get_xyz_points(points_vec, out_ps, order); get_xyz_points(points_vec, out_ps, order);
destroy_vector(points_vec); destroy_vector(points_vec);
return; return;
@ -287,7 +332,7 @@ void gctl::get_xyz_points(std::string filename, array<point3ds> &out_ps, text_de
{ {
// read file // read file
_2d_vector points_vec; _2d_vector points_vec;
read_text2vector2d(filename, points_vec, desc, file_ext); read_text2vector2d(desc, points_vec);
get_xyz_points(points_vec, out_ps, order); get_xyz_points(points_vec, out_ps, order);
destroy_vector(points_vec); destroy_vector(points_vec);
return; return;
@ -339,7 +384,7 @@ void gctl::get_data_column(std::string filename, std::initializer_list<array<dou
{ {
// read file // read file
_2d_vector data_vec; _2d_vector data_vec;
read_text2vector2d(filename, data_vec, desc, file_ext); read_text2vector2d(desc, data_vec);
get_data_column(data_vec, dat_val, dat_col); get_data_column(data_vec, dat_val, dat_col);
destroy_vector(data_vec); destroy_vector(data_vec);
return; return;

View File

@ -39,28 +39,30 @@ namespace gctl
{ {
struct text_descriptor struct text_descriptor
{ {
int head_num_; int head_num_, float_psn_;
// 注释行起始符 标记行起始符 分割符 // 注释行起始符 标记行起始符 分割符
char att_sym_, tag_sym_, delimiter_; char att_sym_, tag_sym_, delimiter_;
// 文件扩展名 // 文件扩展名
std::string file_name_, file_ext_; std::string file_name_, file_ext_, col_str_;
// 注释行 标记行 // 注释行 标记行
std::vector<std::string> head_strs_, annotates_, tags_; std::vector<std::string> head_strs_, annotates_, tags_;
text_descriptor(); text_descriptor();
~text_descriptor();
/** /**
* @brief Construct a new text descriptor object * @brief Construct a new text descriptor object
* *
* @param filename * @param filename
* @param file_exten * @param file_exten
* @param col_order
* @param att_sym * @param att_sym
* @param tag_sym * @param tag_sym
* @param deli * @param deli
*/ */
text_descriptor(std::string filename, std::string file_exten = ".txt", char att_sym = '#', char tag_sym = '>', char deli = ' ', int h_num = 0); text_descriptor(std::string filename, std::string file_exten = ".txt",
std::string col_order = "0,1,2", char deli = ' ', char att_sym = '#',
~text_descriptor(); char tag_sym = '>', int h_num = 0, int io_psn = 6);
/** /**
* @brief * @brief
@ -71,9 +73,64 @@ namespace gctl
* @param tag_sym * @param tag_sym
* @param deli * @param deli
*/ */
void set(std::string filename = "Untitled", std::string file_exten = ".txt", char att_sym = '#', char tag_sym = '>', char deli = ' ', int h_num = 0); void set(std::string filename = "Untitled", std::string file_exten = ".txt",
std::string col_order = "0,1,2", char deli = ' ', char att_sym = '#',
char tag_sym = '>', int h_num = 0, int io_psn = 6);
/**
* @brief
*
*/
void clear(); void clear();
/**
* @brief
*
* @param psn
*/
void set_float_precision(int psn);
/**
* @brief
*
* @param n
*/
void set_head_number(int n);
/**
* @brief
*
* @param sym
*/
void set_annotation_symbol(char sym);
/**
* @brief
*
* @param sym
*/
void set_tag_symbol(char sym);
/**
* @brief
*
* @param sym
*/
void set_column_delimeter(char sym);
/**
* @brief
*
* @param ext_s
*/
void set_file_extension(std::string ext_s);
/**
* @brief
*
* @param sym
*/
void set_column_order(std::string col_s);
}; };
/** /**
@ -81,42 +138,46 @@ namespace gctl
* *
* @note >> * @note >>
* *
* @param[in] filename * @param desc
* @param[in] file_ext txt dat xyz csv
* @param out_vec * @param out_vec
* @param[in] annotate
* @param[in] head_record
* @param head_ptr
* *
* @tparam T * @tparam T
*/ */
template <typename T> template <typename T>
void read_text2vector(std::string filename, std::vector<T> &out_vec, char annotate = '#', unsigned int head_record = 0, void read_text2vector(text_descriptor &desc, std::vector<T> &out_vec)
std::vector<std::string> *head_ptr = nullptr, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::string tmp_line;
std::stringstream tmp_ss;
std::ifstream infile; std::ifstream infile;
open_matched_infile(infile, filename, file_ext); open_infile(infile, desc.file_name_, desc.file_ext_);
// clear cleanup old table // clear cleanup old table
if (!out_vec.empty()) destroy_vector(out_vec); out_vec.clear();
if (head_ptr != nullptr && !head_ptr->empty()) destroy_vector(*head_ptr); desc.clear();
if (head_record > 0) std::string tmp_line;
{ std::stringstream tmp_ss;
for (int i = 0; i < head_record; i++) for (int i = 0; i < desc.head_num_; i++)
{ {
getline(infile, tmp_line); getline(infile, tmp_line);
if (head_ptr != nullptr) head_ptr->push_back(tmp_line); desc.head_strs_.push_back(tmp_line);
}
} }
T tmp_d; T tmp_d;
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] == desc.att_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else else
{ {
tmp_ss.clear(); tmp_ss.clear();
@ -124,7 +185,7 @@ namespace gctl
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the element: "+tmp_line+". From read_text2vector(...)"); throw gctl::runtime_error("[gctl::read_text2vector] Fail to parse the line: " + tmp_line);
} }
out_vec.push_back(tmp_d); out_vec.push_back(tmp_d);
@ -135,77 +196,80 @@ namespace gctl
return; return;
} }
/**
* @brief
*
* @note >>
*
* @param desc
* @param out_vec
*
* @tparam T
*/
template <typename T> template <typename T>
void read_text2array(std::string filename, array<T> &out_arr, char annotate = '#', unsigned int head_record = 0, void read_text2array(text_descriptor &desc, array<T> &out_arr)
array<std::string> *head_ptr = nullptr, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::string tmp_line;
std::stringstream tmp_ss;
std::ifstream infile; std::ifstream infile;
open_matched_infile(infile, filename, file_ext); open_infile(infile, desc.file_name_, desc.file_ext_);
// clear cleanup old table // clear cleanup old table
if (!out_arr.empty()) out_arr.clear(); out_arr.clear();
if (head_ptr != nullptr && !head_ptr->empty()) head_ptr->clear(); desc.clear();
if (head_ptr != nullptr && head_record > 0) head_ptr->resize(head_record);
int line_count = 0, annotate_count = 0; std::string tmp_line;
std::stringstream tmp_ss;
for (int i = 0; i < desc.head_num_; i++)
{
getline(infile, tmp_line);
desc.head_strs_.push_back(tmp_line);
}
size_t l_count = 0;
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
line_count++; if (tmp_line[0] == desc.att_sym_)
if (tmp_line[0] == annotate) annotate_count++; {
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else l_count++;
} }
// reset the file pointer // reset the file pointer
infile.clear(); infile.seekg(0); infile.clear(); infile.seekg(0);
if (line_count == 0) for (int i = 0; i < desc.head_num_; i++)
{ {
throw length_error("File: " + filename + " is empty. From read_text2array(...)"); getline(infile, tmp_line);
}
int arr_size = line_count - annotate_count - head_record;
if (arr_size <= 0)
{
throw length_error("No valid entrance in the file: " + filename + " From read_text2array(...)");
}
out_arr.resize(arr_size);
int count = 0;
if (head_record > 0)
{
while (count < head_record)
{
std::getline(infile, tmp_line);
if (tmp_line[0] == annotate) continue;
else
{
if (head_ptr != nullptr) head_ptr->at(count) = tmp_line;
count++;
}
}
} }
out_arr.resize(l_count);
l_count = 0;
T tmp_d; T tmp_d;
count = 0;
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] != desc.att_sym_ &&
else tmp_line[0] != desc.tag_sym_)
{ {
tmp_ss.clear(); tmp_ss.clear();
tmp_ss.str(tmp_line); tmp_ss.str(tmp_line);
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the element: "+tmp_line+". From read_text2array(...)"); throw gctl::runtime_error("[gctl::read_text2vector] Fail to parse the line: " + tmp_line);
} }
out_arr[count] = tmp_d; out_arr[l_count] = tmp_d;
count++; l_count++;
} }
} }
@ -218,49 +282,37 @@ namespace gctl
* *
* @note << * @note <<
* *
* @param[in] filename * @param[in] desc
* @param[in] file_ext
* @param[in] in_vec * @param[in] in_vec
* @param[in] annotate
* @param[in] head_info
* @param[in] precision <<setprecision(precision)
* *
* @tparam T * @tparam T
*/ */
template <typename T> template <typename T>
void save_vector2text(std::string filename, const std::vector<T> &in_vec, char annotate = '#', void save_vector2text(const text_descriptor &desc, const std::vector<T> &in_vec)
std::vector<std::string> *head_ptr = nullptr, int precision = -1, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::ofstream outfile; std::ofstream outfile;
open_matched_outfile(outfile, filename, file_ext); open_outfile(outfile, desc.file_name_, desc.file_ext_);
if (in_vec.empty()) for (int i = 0; i < desc.head_strs_.size(); i++)
{ {
GCTL_ShowWhatError("The input vector is empty. From save_vector2text(...)", GCTL_WARNING_ERROR, 0, 0, 0); outfile << desc.head_strs_[i] << std::endl;
} }
if (head_ptr != nullptr) for (int i = 0; i < desc.tags_.size(); i++)
{ {
for (int i = 0; i < head_ptr->size(); ++i) outfile << "> " << desc.tags_[i] << std::endl;
{
outfile << annotate << " " << head_ptr->at(i) << std::endl;
}
} }
if (precision > 0) for (int i = 0; i < desc.annotates_.size(); i++)
{ {
for (int i = 0; i < in_vec.size(); i++) outfile << "# " << desc.annotates_[i] << std::endl;
{
outfile << std::setprecision(precision) << in_vec[i] << std::endl;
}
outfile.close();
return;
} }
for (int i = 0; i < in_vec.size(); i++) for (int i = 0; i < in_vec.size(); i++)
{ {
outfile << in_vec[i] << std::endl; outfile << std::setprecision(desc.float_psn_) << in_vec[i] << std::endl;
} }
outfile.close(); outfile.close();
return; return;
} }
@ -268,57 +320,48 @@ namespace gctl
/** /**
* @brief * @brief
* *
* @note << * @note <<
* *
* @param[in] filename * @param[in] desc
* @param[in] file_ext
* @param[in] in_arr * @param[in] in_arr
* @param[in] annotate
* @param[in] head_info
* @param[in] precision <<setprecision(precision)
* *
* @tparam T * @tparam T
*/ */
template <typename T> template <typename T>
void save_array2text(std::string filename, const array<T> &in_arr, const text_descriptor &desc, void save_array2text(const text_descriptor &desc, const array<T> &in_arr)
int precision = -1, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::ofstream outfile; std::ofstream outfile;
open_matched_outfile(outfile, filename, file_ext); open_outfile(outfile, desc.file_name_, desc.file_ext_);
if (in_arr.empty()) for (int i = 0; i < desc.head_strs_.size(); i++)
{ {
GCTL_ShowWhatError("The input array is empty. From save_vector2text(...)", GCTL_WARNING_ERROR, 0, 0, 0); outfile << desc.head_strs_[i] << std::endl;
} }
for (int i = 0; i < desc.annotates_.size(); ++i) for (int i = 0; i < desc.tags_.size(); i++)
{ {
outfile << desc.annotates_[i] << std::endl; outfile << "> " << desc.tags_[i] << std::endl;
} }
if (precision > 0) for (int i = 0; i < desc.annotates_.size(); i++)
{ {
for (int i = 0; i < in_arr.size(); i++) outfile << "# " << desc.annotates_[i] << std::endl;
{
outfile << std::fixed << std::setprecision(precision) << in_arr[i] << std::endl;
}
outfile.close();
return;
} }
for (int i = 0; i < in_arr.size(); i++) for (int i = 0; i < in_arr.size(); i++)
{ {
outfile << in_arr[i] << std::endl; outfile << std::setprecision(desc.float_psn_) << in_arr[i] << std::endl;
} }
outfile.close(); outfile.close();
return; return;
} }
template <typename T> template <typename T>
void read_text2vector2d(std::string filename, std::vector<std::vector<T>> &out_vec2d, text_descriptor &desc, std::string file_ext = ".txt,.dat,.xyz,.csv,.tab") void read_text2vector2d(text_descriptor &desc, std::vector<std::vector<T>> &out_vec2d)
{ {
std::ifstream infile; std::ifstream infile;
open_matched_infile(infile, filename, file_ext); open_infile(infile, desc.file_name_, desc.file_ext_);
// clear old table // clear old table
if (!out_vec2d.empty()) destroy_vector(out_vec2d); if (!out_vec2d.empty()) destroy_vector(out_vec2d);
@ -339,10 +382,16 @@ namespace gctl
{ {
if (tmp_line[0] == desc.att_sym_) if (tmp_line[0] == desc.att_sym_)
{ {
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line); desc.annotates_.push_back(tmp_line);
} }
else if (tmp_line[0] == desc.tag_sym_) else if (tmp_line[0] == desc.tag_sym_)
{ {
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line); desc.tags_.push_back(tmp_line);
} }
else else
@ -358,66 +407,35 @@ namespace gctl
} }
template <typename T> template <typename T>
void save_vector2d2text(std::string filename, const std::vector<std::vector<T>> &out_vec2d, void save_vector2d2text(const text_descriptor &desc, const std::vector<std::vector<T>> &out_vec2d,
char delimiter = ' ', char annotate = '#', std::vector<std::string> *head_ptr = nullptr, matrix_order_e order_type = RowMajor)
matrix_order_e order_type = RowMajor, int precision = -1, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::ofstream outfile; std::ofstream outfile;
open_matched_outfile(outfile, filename, file_ext); open_outfile(outfile, desc.file_name_, desc.file_ext_);
if (out_vec2d.empty()) for (int i = 0; i < desc.head_strs_.size(); i++)
{ {
GCTL_ShowWhatError("The input vector is empty. From save_vector2d2text(...)", GCTL_WARNING_ERROR, 0, 0, 0); outfile << desc.head_strs_[i] << std::endl;
} }
if (head_ptr != nullptr) for (int i = 0; i < desc.tags_.size(); i++)
{ {
for (int i = 0; i < head_ptr->size(); i++) outfile << "> " << desc.tags_[i] << std::endl;
{
outfile << annotate << " " << head_ptr->at(i) << std::endl;
} }
for (int i = 0; i < desc.annotates_.size(); i++)
{
outfile << "# " << desc.annotates_[i] << std::endl;
} }
if (order_type == RowMajor) if (order_type == RowMajor)
{
if (precision > 0)
{ {
for (int i = 0; i < out_vec2d.size(); i++) for (int i = 0; i < out_vec2d.size(); i++)
{ {
outfile << std::setprecision(precision) << out_vec2d[i][0]; outfile << std::setprecision(desc.float_psn_) << out_vec2d[i][0];
for (int j = 1; j < out_vec2d.at(i).size(); j++) for (int j = 1; j < out_vec2d.at(i).size(); j++)
{ {
outfile << std::setprecision(precision) << delimiter << out_vec2d[i][j]; outfile << std::setprecision(desc.float_psn_) << desc.delimiter_ << out_vec2d[i][j];
}
outfile << std::endl;
}
outfile.close();
return;
}
for (int i = 0; i < out_vec2d.size(); i++)
{
outfile << out_vec2d[i][0];
for (int j = 1; j < out_vec2d.at(i).size(); j++)
{
outfile << delimiter << out_vec2d[i][j];
}
outfile << std::endl;
}
outfile.close();
return;
}
if (precision > 0)
{
for (int i = 0; i < out_vec2d[0].size(); i++)
{
outfile << std::setprecision(precision) << out_vec2d[0][i];
for (int j = 1; j < out_vec2d.size(); j++)
{
outfile << std::setprecision(precision) << delimiter << out_vec2d[j][i];
} }
outfile << std::endl; outfile << std::endl;
} }
@ -428,10 +446,10 @@ namespace gctl
for (int i = 0; i < out_vec2d[0].size(); i++) for (int i = 0; i < out_vec2d[0].size(); i++)
{ {
outfile << out_vec2d[0][i]; outfile << std::setprecision(desc.float_psn_) << out_vec2d[0][i];
for (int j = 1; j < out_vec2d.size(); j++) for (int j = 1; j < out_vec2d.size(); j++)
{ {
outfile << delimiter << out_vec2d[j][i]; outfile << std::setprecision(desc.float_psn_) << desc.delimiter_ << out_vec2d[j][i];
} }
outfile << std::endl; outfile << std::endl;
} }
@ -441,66 +459,35 @@ namespace gctl
} }
template <typename T> template <typename T>
void save_matrix2text(std::string filename, const matrix<T> &out_arr2d, void save_matrix2text(const text_descriptor &desc, const matrix<T> &out_arr2d,
char delimiter = ' ', char annotate = '#', std::vector<std::string> *head_ptr = nullptr, matrix_order_e order_type = RowMajor)
matrix_order_e order_type = RowMajor, int precision = -1, std::string file_ext = ".txt,.dat,.xyz,.csv")
{ {
std::ofstream outfile; std::ofstream outfile;
open_matched_outfile(outfile, filename, file_ext); open_outfile(outfile, desc.file_name_, desc.file_ext_);
if (out_arr2d.empty()) for (int i = 0; i < desc.head_strs_.size(); i++)
{ {
GCTL_ShowWhatError("The input array is empty. From save_matrix2text(...)", GCTL_WARNING_ERROR, 0, 0, 0); outfile << desc.head_strs_[i] << std::endl;
} }
if (head_ptr != nullptr) for (int i = 0; i < desc.tags_.size(); i++)
{ {
for (int i = 0; i < head_ptr->size(); i++) outfile << "> " << desc.tags_[i] << std::endl;
{
outfile << annotate << " " << head_ptr->at(i) << std::endl;
} }
for (int i = 0; i < desc.annotates_.size(); i++)
{
outfile << "# " << desc.annotates_[i] << std::endl;
} }
if (order_type == RowMajor) if (order_type == RowMajor)
{
if (precision > 0)
{ {
for (int i = 0; i < out_arr2d.row_size(); i++) for (int i = 0; i < out_arr2d.row_size(); i++)
{ {
outfile << std::setprecision(precision) << out_arr2d[i][0]; outfile << std::setprecision(desc.float_psn_) << out_arr2d[i][0];
for (int j = 1; j < out_arr2d.col_size(); j++) for (int j = 1; j < out_arr2d.col_size(); j++)
{ {
outfile << std::setprecision(precision) << delimiter << out_arr2d.at(i, j); outfile << std::setprecision(desc.float_psn_) << desc.delimiter_ << out_arr2d[i][j];
}
outfile << std::endl;
}
outfile.close();
return;
}
for (int i = 0; i < out_arr2d.row_size(); i++)
{
outfile << out_arr2d[i][0];
for (int j = 1; j < out_arr2d.col_size(); j++)
{
outfile << delimiter << out_arr2d.at(i, j);
}
outfile << std::endl;
}
outfile.close();
return;
}
if (precision > 0)
{
for (int i = 0; i < out_arr2d.col_size(); i++)
{
outfile << std::setprecision(precision) << out_arr2d[0][i];
for (int j = 1; j < out_arr2d.row_size(); j++)
{
outfile << std::setprecision(precision) << delimiter << out_arr2d.at(j, i);
} }
outfile << std::endl; outfile << std::endl;
} }
@ -511,10 +498,10 @@ namespace gctl
for (int i = 0; i < out_arr2d.col_size(); i++) for (int i = 0; i < out_arr2d.col_size(); i++)
{ {
outfile << out_arr2d[0][i]; outfile << std::setprecision(desc.float_psn_) << out_arr2d[0][i];
for (int j = 1; j < out_arr2d.row_size(); j++) for (int j = 1; j < out_arr2d.row_size(); j++)
{ {
outfile << delimiter << out_arr2d.at(j, i); outfile << std::setprecision(desc.float_psn_) << desc.delimiter_ << out_arr2d[j][i];
} }
outfile << std::endl; outfile << std::endl;
} }
@ -524,20 +511,19 @@ namespace gctl
} }
template <typename T> template <typename T>
void read_text2vectors(std::string filename, std::string order_str, void read_text2vectors(text_descriptor &desc, std::vector<T> &out_vec)
char delimiter, char annotate, int head_record, std::vector<T> &out_vec)
{ {
std::string tmp_line, tmp_str, tmp_str2; std::string tmp_line, tmp_str, tmp_str2;
std::stringstream tmp_ss; std::stringstream tmp_ss;
std::ifstream infile; std::ifstream infile;
open_infile(infile, filename); open_infile(infile, desc.file_name_, desc.file_ext_);
// clear cleanup old table // clear cleanup old table
if (!out_vec.empty()) destroy_vector(out_vec); if (!out_vec.empty()) destroy_vector(out_vec);
tmp_ss.clear(); tmp_ss.clear();
tmp_ss.str(order_str); // get the first index separated by comma tmp_ss.str(desc.col_str_); // get the first index separated by comma
std::getline(tmp_ss, tmp_str, ','); std::getline(tmp_ss, tmp_str, ',');
// get input column index // get input column index
int col_id; int col_id;
@ -547,27 +533,38 @@ namespace gctl
if (col_id < 0) if (col_id < 0)
{ {
throw runtime_error("Invalid column index. From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Invalid column index.");
} }
if (head_record > 0) for (int i = 0; i < desc.head_num_; i++)
{
for (int i = 0; i < head_record; i++)
{ {
getline(infile, tmp_line); getline(infile, tmp_line);
} desc.head_strs_.push_back(tmp_line);
} }
T tmp_d; T tmp_d;
if (delimiter != ' ') if (desc.delimiter_ != ' ')
{ {
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] == desc.att_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else else
{ {
// replace all delimiters to spaces // replace all delimiters to spaces
tmp_str2 = delimiter; tmp_str2 = desc.delimiter_;
replace_all(tmp_str, tmp_line, tmp_str2, " "); replace_all(tmp_str, tmp_line, tmp_str2, " ");
tmp_ss.clear(); tmp_ss.clear();
tmp_ss.str(tmp_str); tmp_ss.str(tmp_str);
@ -581,7 +578,7 @@ namespace gctl
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the line: "+tmp_line+". From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Fail to parse the line: " + tmp_line);
} }
out_vec.push_back(tmp_d); out_vec.push_back(tmp_d);
@ -592,7 +589,20 @@ namespace gctl
{ {
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] == desc.att_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else else
{ {
tmp_ss.clear(); tmp_ss.clear();
@ -607,7 +617,7 @@ namespace gctl
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the line: "+tmp_line+". From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Fail to parse the line: " + tmp_line);
} }
out_vec.push_back(tmp_d); out_vec.push_back(tmp_d);
@ -620,20 +630,19 @@ namespace gctl
} }
template <typename T, typename... Args> template <typename T, typename... Args>
void read_text2vectors(std::string filename, std::string order_str, char delimiter, void read_text2vectors(text_descriptor &desc, std::vector<T> &out_vec, std::vector<Args>&... rest)
char annotate, int head_record, std::vector<T> &out_vec, std::vector<Args>&... rest)
{ {
std::string tmp_line, tmp_str, tmp_str2; std::string tmp_line, tmp_str, tmp_str2;
std::stringstream tmp_ss; std::stringstream tmp_ss;
std::ifstream infile; std::ifstream infile;
open_infile(infile, filename); open_infile(infile, desc.file_name_, desc.file_ext_);
// clear cleanup old table // clear cleanup old table
if (!out_vec.empty()) destroy_vector(out_vec); if (!out_vec.empty()) destroy_vector(out_vec);
tmp_ss.clear(); tmp_ss.clear();
tmp_ss.str(order_str); // get the first index separated by comma tmp_ss.str(desc.col_str_); // get the first index separated by comma
std::getline(tmp_ss, tmp_str, ','); std::getline(tmp_ss, tmp_str, ',');
// get input column index // get input column index
int col_id; int col_id;
@ -643,30 +652,42 @@ namespace gctl
if (col_id < 0) if (col_id < 0)
{ {
throw runtime_error("Invalid column index. From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Invalid column index.");
} }
int position = order_str.find_first_of(','); int position = desc.col_str_.find_first_of(',');
std::string tmp_order = order_str.substr(position+1, order_str.length()); std::string tmp_order = desc.col_str_.substr(position+1, desc.col_str_.length());
desc.set_column_order(tmp_order);
if (head_record > 0) for (int i = 0; i < desc.head_num_; i++)
{
for (int i = 0; i < head_record; i++)
{ {
getline(infile, tmp_line); getline(infile, tmp_line);
} desc.head_strs_.push_back(tmp_line);
} }
T tmp_d; T tmp_d;
if (delimiter != ' ') if (desc.delimiter_ != ' ')
{ {
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] == desc.att_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else else
{ {
// replace all delimiters to spaces // replace all delimiters to spaces
tmp_str2 = delimiter; tmp_str2 = desc.delimiter_;
replace_all(tmp_str, tmp_line, tmp_str2, " "); replace_all(tmp_str, tmp_line, tmp_str2, " ");
tmp_ss.clear(); tmp_ss.clear();
tmp_ss.str(tmp_str); tmp_ss.str(tmp_str);
@ -680,7 +701,7 @@ namespace gctl
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the line: "+tmp_line+". From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Fail to parse the line: " + tmp_line);
} }
out_vec.push_back(tmp_d); out_vec.push_back(tmp_d);
@ -691,7 +712,20 @@ namespace gctl
{ {
while (std::getline(infile, tmp_line)) while (std::getline(infile, tmp_line))
{ {
if (tmp_line[0] == annotate) continue; if (tmp_line[0] == desc.att_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.annotates_.push_back(tmp_line);
}
else if (tmp_line[0] == desc.tag_sym_)
{
tmp_line = tmp_line.substr(1);
tmp_line.erase(0, tmp_line.find_first_not_of(" \t"));
tmp_line.erase(tmp_line.find_last_not_of(" \t") + 1);
desc.tags_.push_back(tmp_line);
}
else else
{ {
tmp_ss.clear(); tmp_ss.clear();
@ -706,7 +740,7 @@ namespace gctl
tmp_ss >> tmp_d; tmp_ss >> tmp_d;
if (tmp_ss.fail()) if (tmp_ss.fail())
{ {
throw runtime_error("Fail to parse the line: "+tmp_line+". From read_text2vectors(...)"); throw runtime_error("[gctl::read_text2vectors] Fail to parse the line: " + tmp_line);
} }
out_vec.push_back(tmp_d); out_vec.push_back(tmp_d);
@ -715,7 +749,7 @@ namespace gctl
} }
infile.close(); infile.close();
read_text2vectors(filename, tmp_order, delimiter, annotate, head_record, rest...); read_text2vectors(desc, rest...);
return; return;
} }
@ -823,7 +857,7 @@ namespace gctl
for (int i = 0; i < content_ptr->size(); i++) for (int i = 0; i < content_ptr->size(); i++)
{ {
tmp_str = content_ptr->at(i).substr(0, content_ptr->at(i).length()-1); tmp_str = content_ptr->at(i).substr(0, content_ptr->at(i).length()-1);
outfile << content_ptr->at(i) << std::endl; outfile << tmp_str << std::endl;
} }
destroy_vector(*content_ptr); destroy_vector(*content_ptr);