This commit is contained in:
张壹 2025-01-20 10:49:53 +08:00
parent b54c1b3191
commit c3b2701bba
4 changed files with 75 additions and 29 deletions

View File

@ -29,4 +29,4 @@ add_example(multinary_ex OFF)
add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex ON)
add_example(array_ex OFF)

View File

@ -44,6 +44,13 @@ int main(int argc, char const *argv[]) try
tc.filt_column("America", "Continent_s", {"Name_s", "Population_n", "GNP_n"}, tout);
//tc.match_column("America", "Continent_s", {}, tout);
//tout.add_column("GNP_n2", "Population_n");
//array<int> GNP_n2(tout.row_number(), 1000.0);
//tout.fill_column(GNP_n2, "GNP_n2");
int lr_id = tout.add_row();
tout.fill_row(array<std::string>{"Asia", "China", "14000000", "1949"}, lr_id);
tout.set_delimeter('|');
tout.save_text("out");

View File

@ -293,6 +293,29 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten)
outfile << "# " << annotates_[i] << std::endl;
}
// 探测是否有行头
bool col_st = 1;
for (int i = 0; i <= row_num_; i++)
{
if (table_[i][0].out_ok_ && table_[i][0].str_ != "")
{
col_st = 0;
break;
}
}
for (int i = 0; i <= row_num_; i++)
{
// 单独处理第一列 即行头
outfile << table_[i][col_st].str_;
for (int j = col_st + 1; j <= col_num_; j++)
{
if (table_[i][j].out_ok_) outfile << deli_sym_ << table_[i][j].str_;
}
outfile << std::endl;
}
/*
// 单独处理第一行 即列头
bool line_st = false;
if (table_[0][0].out_ok_ && table_[0][0].str_ != "")
@ -336,7 +359,7 @@ void gctl::dsv_io::save_text(std::string filename, std::string file_exten)
}
outfile << std::endl;
}
*/
outfile.close();
return;
}
@ -508,14 +531,16 @@ void gctl::dsv_io::row_output(int idx, switch_type_e s)
void gctl::dsv_io::row_output(std::string name, switch_type_e s)
{
row_output(name_index(name), s);
row_output(name_index(name, true), s);
return;
}
void gctl::dsv_io::add_column(int idx, std::string name)
int gctl::dsv_io::add_column(std::string name, int idx)
{
if (idx <= 0) throw std::runtime_error("[gctl::dsv_io] Invalid column index.");
table_cell empty_cell;
if (idx <= 0)
if (idx > col_num_)
{
for (size_t i = 0; i < table_.size(); i++)
{
@ -524,6 +549,7 @@ void gctl::dsv_io::add_column(int idx, std::string name)
table_[0].back().str_ = name;
col_num_++;
return col_num_;
}
else
{
@ -534,25 +560,28 @@ void gctl::dsv_io::add_column(int idx, std::string name)
table_[0][idx].str_ = name;
col_num_++;
return idx;
}
return;
return -1;
}
void gctl::dsv_io::add_column(std::string id_name, std::string name)
int gctl::dsv_io::add_column(std::string name, std::string id_name)
{
add_column(name_index(id_name), name);
return;
return add_column(name, name_index(id_name));
}
void gctl::dsv_io::add_row(int idx, std::string name)
int gctl::dsv_io::add_row(std::string name, int idx)
{
if (idx <= 0) throw std::runtime_error("[gctl::dsv_io] Invalid row index.");
std::vector<table_cell> empty_line;
if (idx <= 0)
if (idx > row_num_)
{
table_.push_back(empty_line);
table_.back().resize(col_num_ + 1);
table_.back().front().str_ = name;
row_num_++;
return row_num_;
}
else
{
@ -560,14 +589,14 @@ void gctl::dsv_io::add_row(int idx, std::string name)
table_[idx].resize(col_num_ + 1);
table_[idx].front().str_ = name;
row_num_++;
return idx;
}
return;
return -1;
}
void gctl::dsv_io::add_row(std::string id_name, std::string name)
int gctl::dsv_io::add_row(std::string name, std::string id_name)
{
add_row(name_index(id_name), name);
return;
return add_row(name, name_index(id_name, true));
}
void gctl::dsv_io::filt_column(std::string cnd_str, std::string cnd_col,

View File

@ -404,40 +404,50 @@ namespace gctl
void row_output(std::string name, switch_type_e s = Disable);
/**
* @brief idx的列之后插入一个空白列
* @brief idx的位置插入一个空白列idx大于列数则在表尾添加一列
*
* @param idx 0
* @param idx
* @param name
*
* @return 1 -1
*/
void add_column(int idx, std::string name = "");
int add_column(std::string name = "", int idx = 9999);
/**
* @brief id_name的列之后插入一个空白列
* @brief id_name的列的位置插入一个空白列
*
* @param id_name
* @param name
*/
void add_column(std::string id_name, std::string name = "");
/**
* @brief idx的列插入一个空白行
*
* @param idx 0
* @param name
* @return 1 -1
*/
void add_row(int idx, std::string name = "");
int add_column(std::string name, std::string id_name);
/**
* @brief id_name的列插入一个空白行
* @brief idx的位置插入一个空白行idx大于等于行数则在表尾添加一行
*
* @param idx
* @param name
*
* @return 1 -1
*/
int add_row(std::string name = "", int idx = 9999);
/**
* @brief id_name的行的位置插入一个空白行
*
* @param id_name
* @param name
*
* @return 1 -1
*/
void add_row(std::string id_name, std::string name = "");
int add_row(std::string name, std::string id_name);
/**
* @brief
*
* @note
*
* @param cnd_str
* @param cnd_col
* @param out_col