diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index 6169cf9..d32cbba 100644 --- a/example/text_io_ex.cpp +++ b/example/text_io_ex.cpp @@ -32,7 +32,7 @@ using namespace gctl; int main(int argc, char const *argv[]) try { - +/* dsv_io tc; tc.set_delimeter('|'); tc.load_text("tmp/world_data", ".txt", BothHead); @@ -49,16 +49,16 @@ int main(int argc, char const *argv[]) try //name.show(std::cout, ','); tc.save_csv("out"); +*/ -/* geodsv_io tc; tc.load_text("tmp/topo", ".txt", ColumnHead); - tc.cell(0, 1, std::string("x (m)")); - tc.cell(0, 2, std::string("y (m)")); - tc.cell(0, 3, std::string("elevation (m)")); + tc.cell(std::string("x (m)"), 0, 1); + tc.cell(std::string("y (m)"), 0, 2); + tc.cell(std::string("elevation (m)"), 0, 3); array topo; - tc.get_column_point3dc(1, 2, 3, topo); + tc.get_column_point3dc(topo, 1, 2, 3); std::clog << std::setprecision(11) << topo.front().z << "\n"; std::clog << topo.back().x << "," << topo.back().y << "," << topo.back().z << "\n"; @@ -66,7 +66,14 @@ int main(int argc, char const *argv[]) try display_vector(tc.get_annotoations(), std::clog, '\n'); display_vector(tc.get_head_records(), std::clog, '\n'); - tc.column_output("C3", Disable); + //tc.column_output("C3", Disable); + + array elev; + tc.get_column(elev, "elevation (m)"); + elev.for_each([](double &d, size_t i){d += 100.0;}); + + tc.add_column(-1, "elev_plus"); + tc.fill_column(elev, "elev_plus"); _1s_vector s = tc.get_tags(); s.push_back("Elev = 1000"); @@ -74,11 +81,11 @@ int main(int argc, char const *argv[]) try tc.save_csv("out"); double c = 4.25242153654; - tc.cell(2, 1, c, 12); + tc.cell(c, 2, 1, 12); std::clog << std::setprecision(12) << tc.cell(2, 1) << "\n"; tc.info(); -*/ + return 0; } catch(std::exception &e) diff --git a/lib/core/array.h b/lib/core/array.h index 5bd23d7..5c0a5ad 100644 --- a/lib/core/array.h +++ b/lib/core/array.h @@ -511,27 +511,27 @@ namespace gctl * @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. - * @param[in] seed Random seed. + * @param[in] seed Random seed. Input 0 to select the seed based on the current time. */ void random_float(ArrValType np1, ArrValType 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] seed Random seed. + * @param[in] np1 Low bound value. + * @param[in] np2 Hig bound value. + * @param[in] seed Random seed. Input 0 to select the seed based on the current time. */ void random_int(ArrValType np1, ArrValType np2, unsigned int seed = 0); /** * @brief Set elements' value as a sequent. * - * @param st_val Start value. - * @param inc Increasement. - * @param st_id Start index. - * @param size Operating size. - * @param space Indexing spacing. + * @param st_val Start value of the sequent. + * @param inc Increasement of the sequent. + * @param st_id Start index. The default starts from the first element + * @param size Operating size. The default operates on all elements + * @param space Indexing spacing. you can skip elements when create the sequent. The default is no skipping. */ void sequence(ArrValType st_val, ArrValType inc, size_t st_id = 0, size_t size = std::numeric_limits::max(), size_t space = 0); diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 44bae89..04e1a81 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -456,6 +456,51 @@ void gctl::dsv_io::row_output(std::string name, switch_type_e s) return; } +void gctl::dsv_io::add_column(std::string name) +{ + table_cell empty_cell; + for (size_t i = 0; i < table_.size(); i++) + { + table_[i].push_back(empty_cell); + } + + table_[0].back().str_ = name; + col_num_++; + return; +} + +void gctl::dsv_io::add_column(int idx, std::string name) +{ + table_cell empty_cell; + if (idx <= 0) + { + for (size_t i = 0; i < table_.size(); i++) + { + table_[i].push_back(empty_cell); + } + + table_[0].back().str_ = name; + col_num_++; + } + else + { + for (size_t i = 0; i < table_.size(); i++) + { + table_[i].insert(table_[i].begin() + idx, empty_cell); + } + + table_[0][idx].str_ = name; + col_num_++; + } + return; +} + +void gctl::dsv_io::add_column(std::string id_name, std::string name) +{ + add_column(name_index(id_name), name); + return; +} + gctl::geodsv_io::geodsv_io(){} gctl::geodsv_io::~geodsv_io(){} @@ -474,7 +519,7 @@ gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_h else load_text(filename, file_exten, t); } -void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array &data, int p) +void gctl::geodsv_io::fill_column_point2dc(const array &data, int xid, int yid, int p) { if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0) { @@ -498,13 +543,13 @@ void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array &data, int p) +void gctl::geodsv_io::fill_column_point2dc(const array &data, std::string xname, std::string yname, int p) { - fill_column_point2dc(name_index(xname, false), name_index(yname, false), data, p); + fill_column_point2dc(data, name_index(xname, false), name_index(yname, false), p); return; } -void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const array &data, int p) +void gctl::geodsv_io::fill_column_point3dc(const array &data, int xid, int yid, int zid, int p) { if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid || xid <= 0 || yid <= 0 || zid <= 0) @@ -534,13 +579,13 @@ void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const arra return; } -void gctl::geodsv_io::fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array &data, int p) +void gctl::geodsv_io::fill_column_point3dc(const array &data, std::string xname, std::string yname, std::string zname, int p) { - fill_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data, p); + fill_column_point3dc(data, name_index(xname, false), name_index(yname, false), name_index(zname, false), p); return; } -void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array &data) +void gctl::geodsv_io::get_column_point2dc(array &data, int xid, int yid) { if (xid > col_num_ || yid > col_num_ || xid == yid || xid <= 0 || yid <= 0) { @@ -556,13 +601,13 @@ void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array &dat return; } -void gctl::geodsv_io::get_column_point2dc(std::string xname, std::string yname, array &data) +void gctl::geodsv_io::get_column_point2dc(array &data, std::string xname, std::string yname) { - get_column_point2dc(name_index(xname, false), name_index(yname, false), data); + get_column_point2dc(data, name_index(xname, false), name_index(yname, false)); return; } -void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array &data) +void gctl::geodsv_io::get_column_point3dc(array &data, int xid, int yid, int zid) { if (xid > col_num_ || yid > col_num_ || zid > col_num_ || xid == yid || yid == zid || xid == zid || xid <= 0 || yid <= 0 || zid <= 0) @@ -580,8 +625,8 @@ void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array &data) +void gctl::geodsv_io::get_column_point3dc(array &data, std::string xname, std::string yname, std::string zname) { - get_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data); + get_column_point3dc(data, name_index(xname, false), name_index(yname, false), name_index(zname, false)); return; } \ No newline at end of file diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 940c26c..0aefa4d 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -344,6 +344,29 @@ namespace gctl * @param s 设置输出类型 */ void row_output(std::string name, switch_type_e s = Disable); + + /** + * @brief 在表格末尾添加一个空白列 + * + * @param name 设置列名称 + */ + void add_column(std::string name = ""); + + /** + * @brief 在索引为idx的列后添加一个空白列 + * + * @param idx 列索引 小于等于0时将在表尾添加一列 + * @param name 设置列名称 + */ + void add_column(int idx, std::string name = ""); + + /** + * @brief 在名称为id_name的列后添加一个空白列 + * + * @param id_name 索引列名称 + * @param name 设置列名称 + */ + void add_column(std::string id_name, std::string name = ""); /** * @brief 填充表格 @@ -370,7 +393,7 @@ namespace gctl * @param data 列数据 * @param p 浮点类数据保存时的有效数字位数 */ - template void fill_column(int idx, const array &data, int p = 6); + template void fill_column(const array &data, int idx, int p = 6); /** * @brief 填充列 @@ -380,7 +403,7 @@ namespace gctl * @param data 列数据 * @param p 浮点类数据保存时的有效数字位数 */ - template void fill_column(std::string name, const array &data, int p = 6); + template void fill_column(const array &data, std::string name, int p = 6); /** * @brief 填充行 @@ -390,7 +413,7 @@ namespace gctl * @param data 行数据 * @param p 浮点类数据保存时的有效数字位数 */ - template void fill_row(int idx, const array &data, int p = 6); + template void fill_row(const array &data, int idx, int p = 6); /** * @brief 填充行 @@ -400,7 +423,7 @@ namespace gctl * @param data 行数据 * @param p 浮点类数据保存时的有效数字位数 */ - template void fill_row(std::string name, const array &data, int p = 6); + template void fill_row(const array &data, std::string name, int p = 6); /** * @brief 获取列数据 @@ -409,7 +432,7 @@ namespace gctl * @param idx 列索引 从1开始 * @param data 列数据 */ - template void get_column(int idx, array &data); + template void get_column(array &data, int idx); /** * @brief 获取列数据 @@ -418,7 +441,7 @@ namespace gctl * @param name 列名称 * @param data 列数据 */ - template void get_column(std::string name, array &data); + template void get_column(array &data, std::string name); /** * @brief 获取行数据 @@ -427,7 +450,7 @@ namespace gctl * @param idx 行索引 从1开始 * @param data 行数据 */ - template void get_row(int idx, array &data); + template void get_row(array &data, int idx); /** * @brief 获取行数据 @@ -436,7 +459,7 @@ namespace gctl * @param name 行名称 * @param data 行数据 */ - template void get_row(std::string name, array &data); + template void get_row(array &data, std::string name); /** * @brief 获取表格单元数据 @@ -457,7 +480,7 @@ namespace gctl * @param d 数据 * @param p 浮点类数据保存时的有效数字位数 */ - template void cell(int r, int c, T d, int p = 6){table_[r][c].value(d, p); return;} + template void cell(T d, int r, int c, int p = 6){table_[r][c].value(d, p); return;} }; template @@ -488,7 +511,7 @@ namespace gctl } template - void dsv_io::fill_column(int idx, const array &data, int p) + void dsv_io::fill_column(const array &data, int idx, int p) { if (idx > col_num_ || idx <= 0) { @@ -503,14 +526,14 @@ namespace gctl } template - void dsv_io::fill_column(std::string name, const array &data, int p) + void dsv_io::fill_column(const array &data, std::string name, int p) { - fill_column(name_index(name, false), data, p); + fill_column(data, name_index(name, false), p); return; } template - void dsv_io::fill_row(int idx, const array &data, int p) + void dsv_io::fill_row(const array &data, int idx, int p) { if (idx > row_num_ || idx <= 0) { @@ -525,14 +548,14 @@ namespace gctl } template - void dsv_io::fill_row(std::string name, const array &data, int p) + void dsv_io::fill_row(const array &data, std::string name, int p) { - fill_row(name_index(name, true), data, p); + fill_row(data, name_index(name, true), p); return; } template - void dsv_io::get_column(int idx, array &data) + void dsv_io::get_column(array &data, int idx) { if (idx > col_num_ || idx <= 0) { @@ -548,14 +571,14 @@ namespace gctl } template - void dsv_io::get_column(std::string name, array &data) + void dsv_io::get_column(array &data, std::string name) { - get_column(name_index(name, false), data); + get_column(data, name_index(name, false)); return; } template - void dsv_io::get_row(int idx, array &data) + void dsv_io::get_row(array &data, int idx) { if (idx > row_num_ || idx <= 0) { @@ -571,9 +594,9 @@ namespace gctl } template - void dsv_io::get_row(std::string name, array &data) + void dsv_io::get_row(array &data, std::string name) { - get_row(name_index(name, true), data); + get_row(data, name_index(name, true)); return; } @@ -612,7 +635,7 @@ namespace gctl * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ - void fill_column_point2dc(int xid, int yid, const array &data, int p = 6); + void fill_column_point2dc(const array &data, int xid, int yid, int p = 6); /** * @brief 填充二维坐标列 @@ -622,7 +645,7 @@ namespace gctl * @param data 返回的二维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ - void fill_column_point2dc(std::string xname, std::string yname, const array &data, int p = 6); + void fill_column_point2dc(const array &data, std::string xname, std::string yname, int p = 6); /** * @brief 填充三维坐标列 @@ -633,7 +656,7 @@ namespace gctl * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ - void fill_column_point3dc(int xid, int yid, int zid, const array &data, int p = 6); + void fill_column_point3dc(const array &data, int xid, int yid, int zid, int p = 6); /** * @brief 填充三维坐标列 @@ -644,7 +667,7 @@ namespace gctl * @param data 返回的三维坐标数据 * @param p 填入的浮点数据有效位数(精度) */ - void fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array &data, int p = 6); + void fill_column_point3dc(const array &data, std::string xname, std::string yname, std::string zname, int p = 6); /** * @brief 读取二维坐标列 @@ -653,7 +676,7 @@ namespace gctl * @param yid y坐标列索引 从1开始 * @param data 返回的二维坐标数据 */ - void get_column_point2dc(int xid, int yid, array &data); + void get_column_point2dc(array &data, int xid, int yid); /** * @brief 读取二维坐标列 @@ -662,7 +685,7 @@ namespace gctl * @param yname y坐标列名称 * @param data 返回的二维坐标数据 */ - void get_column_point2dc(std::string xname, std::string yname, array &data); + void get_column_point2dc(array &data, std::string xname, std::string yname); /** * @brief 读取三维坐标列 @@ -672,7 +695,7 @@ namespace gctl * @param zid z坐标列索引 从1开始 * @param data 返回的三维坐标数据 */ - void get_column_point3dc(int xid, int yid, int zid, array &data); + void get_column_point3dc(array &data, int xid, int yid, int zid); /** * @brief 读取三维坐标列 @@ -682,7 +705,7 @@ namespace gctl * @param zname z坐标列名称 * @param data 返回的三维坐标数据 */ - void get_column_point3dc(std::string xname, std::string yname, std::string zname, array &data); + void get_column_point3dc(array &data, std::string xname, std::string yname, std::string zname); }; } diff --git a/tool/dsviewer/dsviewer.cpp b/tool/dsviewer/dsviewer.cpp index 77df11d..0157c98 100644 --- a/tool/dsviewer/dsviewer.cpp +++ b/tool/dsviewer/dsviewer.cpp @@ -331,7 +331,7 @@ void statistic(const std::vector &cmd_units) _1d_array data; for (size_t i = 1; i < cmd_units.size(); i++) { - tc.get_column(cmd_units[i], data); + tc.get_column(data, cmd_units[i]); std::clog << "column: " << cmd_units[i] << " | " << data.min() << "/" << data.mean() << "/" << data.max() << " | STD: " << data.std() << "\n";