add filter to dsv_io

This commit is contained in:
张壹 2025-01-02 19:29:24 +08:00
parent a3b16ecdf5
commit a8a80f0dc4
3 changed files with 105 additions and 0 deletions

View File

@ -82,6 +82,10 @@ int main(int argc, char const *argv[]) try
tc.add_row(3);
tc.fill_row(array<double>{5.5, 4.4, 3.3, 2.2, 1.1}, 3);
geodsv_io out_table;
tc.filter("dist > 1000", {"dist"}, {"x (m)", "y (m)", "elev (m)"}, out_table);
out_table.save_text("out2");
_1s_vector s = tc.get_tags();
s.push_back("Elev = 1000");
tc.set_tags(s);

View File

@ -362,6 +362,28 @@ void gctl::dsv_io::init_table(int row, int col, table_headtype_e t)
return;
}
void gctl::dsv_io::init_table(const std::vector<std::vector<std::string> > &str_table, table_headtype_e t)
{
row_num_ = str_table.size();
col_num_ = str_table[0].size();
// 初始的列头和行头均为空白
table_.resize(row_num_ + 1);
for (size_t i = 0; i < row_num_ + 1; i++)
{
table_[i].resize(col_num_ + 1);
}
for (size_t i = 1; i <= row_num_; i++)
{
for (size_t j = 1; j <= col_num_; j++)
{
table_[i][j].str_ = str_table[i - 1][j - 1];
}
}
return;
}
void gctl::dsv_io::info(table_headtype_e t)
{
std::clog << "File: " << file_ << "\n------------\n";
@ -637,6 +659,65 @@ void gctl::dsv_io::cal_column(std::string expr_str, const std::vector<std::strin
return;
}
void gctl::dsv_io::filter(std::string cnd_str, const std::vector<std::string> &cnd_col,
const std::vector<std::string> &out_col, dsv_io& out_table)
{
array<int> idx(cnd_col.size());
for (size_t i = 0; i < cnd_col.size(); i++)
{
idx[i] = name_index(cnd_col[i]);
}
array<int> odx(out_col.size());
for (size_t i = 0; i < out_col.size(); i++)
{
odx[i] = name_index(out_col[i]);
}
exprtk::symbol_table<double> symbol_table;
array<double> var(cnd_col.size());
for (size_t i = 0; i < var.size(); i++)
{
symbol_table.add_variable(cnd_col[i], var[i]);
}
exprtk::expression<double> expression;
expression.register_symbol_table(symbol_table);
exprtk::parser<double> parser;
if (!parser.compile(cnd_str, expression))
{
throw std::runtime_error("[gctl::dsv_io] Fail to compile the math expression.");
}
std::vector<std::string> str_line;
std::vector<std::vector<std::string> > str_table;
for (size_t i = 1; i <= row_num_; i++)
{
for (size_t j = 0; j < var.size(); j++)
{
var[j] = table_[i][idx[j]].value<double>();
}
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++)
{
str_line.push_back(table_[i][odx[j]].str_);
}
str_table.push_back(str_line);
}
}
out_table.init_table(str_table);
out_table.set_column_names(out_col);
return;
}
#endif // GCTL_EXPRTK
gctl::geodsv_io::geodsv_io(){}

View File

@ -352,9 +352,18 @@ namespace gctl
*
* @param row
* @param col
* @param t
*/
void init_table(int row, int col, table_headtype_e t = ColumnHead);
/**
* @brief
*
* @param str_table
* @param t
*/
void init_table(const std::vector<std::vector<std::string> > &str_table, table_headtype_e t = ColumnHead);
/**
* @brief
*
@ -463,6 +472,17 @@ namespace gctl
*/
void cal_column(std::string expr_str, const std::vector<std::string> &col_list, int p = 6);
/**
* @brief
*
* @param cnd_str
* @param cnd_col
* @param out_col
* @param out_table
*/
void filter(std::string cnd_str, const std::vector<std::string> &cnd_col,
const std::vector<std::string> &out_col, dsv_io &out_table);
#endif // GCTL_EXPRTK
/**