This commit is contained in:
张壹 2025-01-03 14:40:15 +08:00
parent 2c41a0c898
commit 946cffbb15
3 changed files with 282 additions and 40 deletions

View File

@ -32,25 +32,22 @@ using namespace gctl;
int main(int argc, char const *argv[]) try
{
/*
dsv_io tc;
dsv_io tc, tout;
tc.set_delimeter('|');
tc.load_text("tmp/world_data", ".txt", BothHead);
tc.info(BothHead);
//tc.info();
//_1s_vector name = tc.get_row_names();
//display_vector(name);
//tc.set_column_type(Int, "IndepYear_n");
//tc.filt_column("IndepYear_n < 0", {"IndepYear_n"}, {"Name_s", "Population_n", "GNP_n"}, tout);
//_1s_array name;
//tc.get_column("Name_s", name);
//name.show(std::cout, '|');
tc.filt_column("America", "Continent_s", {"Name_s", "Population_n", "GNP_n"}, tout);
//tc.match_column("America", "Continent_s", {}, tout);
//tc.get_row("AUS", name);
//name.show(std::cout, ',');
tc.save_csv("out");
*/
tout.set_delimeter('|');
tout.save_text("out");
/*
geodsv_io tc;
tc.load_text("tmp/topo", ".txt", ColumnHead);
tc.set_column_names({"x (m)", "y (m)", "elev (m)"});
@ -96,6 +93,7 @@ int main(int argc, char const *argv[]) try
std::clog << std::setprecision(12) << tc.cell<double>(2, 1) << "\n";
tc.info();
*/
return 0;
}
catch(std::exception &e)

View File

@ -77,7 +77,7 @@ void gctl::dsv_io::clear()
void gctl::dsv_io::get_row_names(std::vector<std::string> &names)
{
names.resize(row_num_);
for (size_t i = 1; i < row_num_; i++)
for (size_t i = 1; i <= row_num_; i++)
{
names[i - 1] = table_[i][0].str_;
}
@ -87,21 +87,21 @@ void gctl::dsv_io::get_row_names(std::vector<std::string> &names)
void gctl::dsv_io::get_column_names(std::vector<std::string> &names)
{
names.resize(col_num_);
for (size_t i = 1; i < col_num_; i++)
for (size_t i = 1; i <= col_num_; i++)
{
names[i - 1] = table_[0][i].str_;
}
return;
}
void gctl::dsv_io::set_row_names(const std::vector<std::string> &names)
void gctl::dsv_io::set_row_names(const std::vector<std::string> &names, std::string corner_name)
{
for (size_t i = 1; i <= std::min(row_num_, (int) names.size()); i++)
{
table_[i][0].str_ = names[i - 1];
}
table_[0][0].str_ = "row_name";
table_[0][0].str_ = corner_name;
return;
}
@ -438,6 +438,11 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row)
if (iter_row)
{
for (size_t i = 1; i <= row_num_; i++)
{
if (table_[i][0].str_ == name) return i;
}
if (regex_search(name, ret, patr))
{
int r = atoi(std::string(ret[1]).c_str());
@ -445,15 +450,15 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row)
else return -1;
}
for (size_t i = 1; i <= row_num_; i++)
{
if (table_[i][0].str_ == name) return i;
}
return -1;
}
else
{
for (size_t i = 1; i <= col_num_; i++)
{
if (table_[0][i].str_ == name) return i;
}
if (regex_search(name, ret, patc))
{
int c = atoi(std::string(ret[1]).c_str());
@ -461,11 +466,6 @@ int gctl::dsv_io::name_index(std::string name, bool iter_row)
else return -1;
}
for (size_t i = 1; i <= col_num_; i++)
{
if (table_[0][i].str_ == name) return i;
}
return -1;
}
}
@ -593,6 +593,165 @@ void gctl::dsv_io::add_row(std::string id_name, std::string name)
return;
}
void gctl::dsv_io::filt_column(std::string cnd_str, std::string cnd_col,
const std::vector<std::string> &out_col, dsv_io &out_table)
{
int idx = name_index(cnd_col);
if (idx < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
array<int> odx;
bool out_row = false;
if (out_col.empty()) out_row = true;
else
{
odx.resize(out_col.size());
for (size_t i = 0; i < out_col.size(); i++)
{
odx[i] = name_index(out_col[i]);
if (odx[i] < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
}
}
std::smatch ret;
std::regex pat(cnd_str);
std::vector<std::string> str_line, row_names;
std::vector<std::vector<std::string> > str_table;
for (size_t i = 1; i <= row_num_; i++)
{
if (regex_search(table_[i][idx].str_, ret, pat))
{
if (out_row)
{
str_line.clear();
for (size_t j = 1; j <= col_num_; j++)
{
str_line.push_back(table_[i][j].str_);
}
str_table.push_back(str_line);
}
else
{
str_line.clear();
str_line.push_back(table_[i][idx].str_);
for (size_t j = 0; j < odx.size(); j++)
{
str_line.push_back(table_[i][odx[j]].str_);
}
str_table.push_back(str_line);
}
row_names.push_back(table_[i][0].str_);
}
}
out_table.init_table(str_table);
std::vector<std::string> io_col;
if (out_row)
{
get_column_names(io_col);
out_table.cell(table_[0][0].str_, 0, 0);
}
else
{
io_col.push_back(cnd_col);
for (size_t j = 0; j < odx.size(); j++)
{
io_col.push_back(out_col[j]);
}
}
out_table.set_column_names(io_col);
out_table.set_row_names(row_names, table_[0][0].str_);
destroy_vector(row_names);
destroy_vector(io_col);
destroy_vector(str_line);
destroy_vector(str_table);
return;
}
void gctl::dsv_io::filt_column(rowbool_func_t func, const std::vector<std::string> &out_col, dsv_io &out_table)
{
array<int> odx;
bool out_row = false;
if (out_col.empty()) out_row = true;
else
{
odx.resize(out_col.size());
for (size_t i = 0; i < out_col.size(); i++)
{
odx[i] = name_index(out_col[i]);
if (odx[i] < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
}
}
std::vector<std::string> str_line, row_names;
std::vector<std::vector<std::string> > str_table;
for (size_t i = 1; i <= row_num_; i++)
{
if (func(table_[i]))
{
if (out_row)
{
str_line.clear();
for (size_t j = 1; j <= col_num_; j++)
{
str_line.push_back(table_[i][j].str_);
}
str_table.push_back(str_line);
}
else
{
str_line.clear();
for (size_t j = 0; j < odx.size(); j++)
{
str_line.push_back(table_[i][odx[j]].str_);
}
str_table.push_back(str_line);
}
row_names.push_back(table_[i][0].str_);
}
}
out_table.init_table(str_table);
std::vector<std::string> io_col;
if (out_row)
{
get_column_names(io_col);
out_table.cell(table_[0][0].str_, 0, 0);
}
else
{
for (size_t j = 0; j < odx.size(); j++)
{
io_col.push_back(out_col[j]);
}
}
out_table.set_column_names(io_col);
out_table.set_row_names(row_names, table_[0][0].str_);
destroy_vector(row_names);
destroy_vector(io_col);
destroy_vector(str_line);
destroy_vector(str_table);
return;
}
#ifdef GCTL_EXPRTK
void gctl::dsv_io::cal_column(std::string expr_str, const std::vector<std::string> &col_list, int p)
@ -602,6 +761,8 @@ void gctl::dsv_io::cal_column(std::string expr_str, const std::vector<std::strin
{
idx[i] = name_index(col_list[i]);
if (idx[i] < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
if (table_[0][idx[i]].type_ != Int && table_[0][idx[i]].type_ != Float)
{
throw std::runtime_error("[gctl::dsv_io] Invalid column type for numerical calculating.");
@ -647,16 +808,25 @@ void gctl::dsv_io::filt_column(std::string cnd_str, const std::vector<std::strin
{
idx[i] = name_index(cnd_col[i]);
if (idx[i] < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
if (table_[0][idx[i]].type_ != Int && table_[0][idx[i]].type_ != Float)
{
throw std::runtime_error("[gctl::dsv_io] Invalid column type for numerical calculating.");
}
}
array<int> odx(out_col.size());
for (size_t i = 0; i < out_col.size(); i++)
array<int> odx;
bool out_row = false;
if (out_col.empty()) out_row = true;
else
{
odx[i] = name_index(out_col[i]);
odx.resize(out_col.size());
for (size_t i = 0; i < out_col.size(); i++)
{
odx[i] = name_index(out_col[i]);
if (odx[i] < 0) throw std::runtime_error("[gctl::dsv_io::] Invalid column index or name.");
}
}
exprtk::symbol_table<double> symbol_table;
@ -664,7 +834,7 @@ void gctl::dsv_io::filt_column(std::string cnd_str, const std::vector<std::strin
for (size_t i = 0; i < var.size(); i++)
{
symbol_table.add_variable(cnd_col[i], var[i]);
symbol_table.add_variable(cnd_col[i], var[i]);
}
exprtk::expression<double> expression;
@ -676,7 +846,7 @@ void gctl::dsv_io::filt_column(std::string cnd_str, const std::vector<std::strin
throw std::runtime_error("[gctl::dsv_io] Fail to compile the math expression.");
}
std::vector<std::string> str_line;
std::vector<std::string> str_line, row_names;
std::vector<std::vector<std::string> > str_table;
for (size_t i = 1; i <= row_num_; i++)
@ -688,18 +858,65 @@ void gctl::dsv_io::filt_column(std::string cnd_str, const std::vector<std::strin
if (expression.value() > 0.5) // return 1 if matched or 0 if dismatched
{
str_line.clear();
for (size_t j = 0; j < odx.size(); j++)
if (out_row)
{
str_line.push_back(table_[i][odx[j]].str_);
str_line.clear();
for (size_t j = 1; j <= col_num_; j++)
{
str_line.push_back(table_[i][j].str_);
}
str_table.push_back(str_line);
}
else
{
str_line.clear();
for (size_t j = 0; j < idx.size(); j++)
{
str_line.push_back(table_[i][idx[j]].str_);
}
for (size_t j = 0; j < odx.size(); j++)
{
str_line.push_back(table_[i][odx[j]].str_);
}
str_table.push_back(str_line);
}
str_table.push_back(str_line);
row_names.push_back(table_[i][0].str_);
}
}
out_table.init_table(str_table);
out_table.set_column_names(out_col);
std::vector<std::string> io_col;
if (out_row)
{
get_column_names(io_col);
out_table.cell(table_[0][0].str_, 0, 0);
}
else
{
for (size_t j = 0; j < idx.size(); j++)
{
io_col.push_back(cnd_col[j]);
}
for (size_t j = 0; j < odx.size(); j++)
{
io_col.push_back(out_col[j]);
}
}
out_table.set_column_names(io_col);
out_table.set_row_names(row_names, table_[0][0].str_);
destroy_vector(row_names);
destroy_vector(io_col);
destroy_vector(str_line);
destroy_vector(str_table);
return;
}

View File

@ -275,7 +275,7 @@ namespace gctl
*
* @param names
*/
void set_row_names(const std::vector<std::string> &names);
void set_row_names(const std::vector<std::string> &names, std::string corner_name = "row-idx");
/**
* @brief
@ -449,6 +449,32 @@ namespace gctl
*/
void add_row(std::string id_name, std::string name = "");
/**
* @brief
*
* @param cnd_str
* @param cnd_col
* @param out_col
* @param out_table
*/
void filt_column(std::string cnd_str, std::string cnd_col,
const std::vector<std::string> &out_col, dsv_io &out_table);
/**
* @brief row operate function pointer
*
*/
typedef bool (*rowbool_func_t)(const std::vector<table_cell> &table_row);
/**
* @brief
*
* @param func
* @param out_col
* @param out_table
*/
void filt_column(rowbool_func_t func, const std::vector<std::string> &out_col, dsv_io &out_table);
#ifdef GCTL_EXPRTK
/**
@ -467,10 +493,11 @@ namespace gctl
* @brief
*
* @note float和Int类型的列数据才能用于计算exprtk库完成
* 使strtk库的相关内容使
*
* @param cnd_str
* @param cnd_col
* @param out_col
* @param out_col
* @param out_table
*/
void filt_column(std::string cnd_str, const std::vector<std::string> &cnd_col,