From 4959733702f3e0ea59816e32eec8994e35316e29 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 17 Dec 2024 09:59:06 +0800 Subject: [PATCH] tmp --- example/text_io_ex.cpp | 10 +-- lib/io/dsv_io.cpp | 108 +++++++++++++++++++----- lib/io/dsv_io.h | 181 ++++++++++++++++++++++++----------------- 3 files changed, 199 insertions(+), 100 deletions(-) diff --git a/example/text_io_ex.cpp b/example/text_io_ex.cpp index cc54073..43ac764 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,8 +49,8 @@ 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); @@ -69,11 +69,11 @@ int main(int argc, char const *argv[]) try tc.save_csv("out"); double c = 4.25242153654; - tc.cell(0, 0, c, 12); - std::clog << std::setprecision(12) << tc.cell(0, 0) << "\n"; + tc.cell(1, 0, c, 12); + std::clog << std::setprecision(12) << tc.cell(1, 0) << "\n"; tc.info(); -*/ + return 0; } catch(std::exception &e) diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 0cd67e3..a0e4e89 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -140,7 +140,7 @@ void gctl::dsv_io::load_text(std::string filename, std::string file_exten, table } // 补齐可能的空格 - cell_content empty_cell; + table_cell empty_cell; empty_cell.str_ = ""; for (size_t i = 0; i < row_num_; i++) { @@ -294,13 +294,44 @@ void gctl::dsv_io::info() return; } -gctl::geodsv_io::geodsv_io() : dsv_io::dsv_io(){} - -gctl::geodsv_io::~geodsv_io() +int gctl::dsv_io::name_index(std::string name, bool iter_row) { - dsv_io::~dsv_io(); + if (iter_row) + { + if (thead_ != RowHead && thead_ != BothHead) + { + throw std::runtime_error("[gctl::dsv_io] The table is initialized with no row names."); + } + + for (size_t i = 0; i < row_num_; i++) + { + if (table_[i][0].str_ == name) return i; + } + + throw gctl::runtime_error("[gctl::dsv_io] No row found by the input name: " + name); + return 0; + } + else + { + if (thead_ != ColumnHead && thead_ != BothHead) + { + throw std::runtime_error("[gctl::dsv_io] The table is initialized with no column names."); + } + + for (size_t i = 0; i < col_num_; i++) + { + if (table_[0][i].str_ == name) return i; + } + + throw gctl::runtime_error("[gctl::dsv_io] No column found by the input name: " + name); + return 0; + } } +gctl::geodsv_io::geodsv_io(){} + +gctl::geodsv_io::~geodsv_io(){} + gctl::geodsv_io::geodsv_io(std::string filename, std::string file_exten, table_headtype_e t) { file_ = ""; @@ -320,7 +351,7 @@ void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array= col_num_ || yid >= col_num_ || xid == yid) { - throw std::runtime_error("[gctl::geodsv_io::fill_column_point2dc] Invalid column index."); + throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } std::stringstream ss; @@ -340,64 +371,97 @@ void gctl::geodsv_io::fill_column_point2dc(int xid, int yid, const array &data, int p) +{ + fill_column_point2dc(name_index(xname, false), name_index(yname, false), data, p); + return; +} + void gctl::geodsv_io::fill_column_point3dc(int xid, int yid, int zid, const array &data, int p) { if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) { - throw std::runtime_error("[gctl::geodsv_io::fill_column_point3dc] Invalid column index."); + throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } + int st = 0; + if (thead_ == ColumnHead || thead_ == BothHead) st = 1; + std::stringstream ss; std::string s; - for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) + for (size_t i = st; i < std::min(row_num_, (int) data.size()); i++) { ss.clear(); - ss << data[i].x; + ss << data[i - st].x; ss >> s; table_[i][xid].str_ = s; ss.clear(); - ss << data[i].y; + ss << data[i - st].y; ss >> s; table_[i][yid].str_ = s; ss.clear(); - ss << data[i].z; + ss << data[i - st].z; ss >> s; table_[i][zid].str_ = s; } return; } +void gctl::geodsv_io::fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array &data, int p) +{ + fill_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data, p); + return; +} + void gctl::geodsv_io::get_column_point2dc(int xid, int yid, array &data) { if (xid >= col_num_ || yid >= col_num_ || xid == yid) { - throw std::runtime_error("[gctl::geodsv_io::fill_column_point2dc] Invalid column index."); + throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - data.resize(row_num_); - for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) + int st = 0; + if (thead_ == ColumnHead || thead_ == BothHead) st = 1; + + data.resize(row_num_ - st); + for (size_t i = st; i < row_num_; i++) { - data[i].x = table_[i][xid].value(); - data[i].y = table_[i][yid].value(); + data[i - st].x = table_[i][xid].value(); + data[i - st].y = table_[i][yid].value(); } return; } +void gctl::geodsv_io::get_column_point2dc(std::string xname, std::string yname, array &data) +{ + get_column_point2dc(name_index(xname, false), name_index(yname, false), data); + return; +} + void gctl::geodsv_io::get_column_point3dc(int xid, int yid, int zid, array &data) { if (xid >= col_num_ || yid >= col_num_ || zid >= col_num_ || xid == yid || yid == zid || xid == zid) { - throw std::runtime_error("[gctl::geodsv_io::fill_column_point3dc] Invalid column index."); + throw std::runtime_error("[gctl::geodsv_io] Invalid column index."); } - data.resize(row_num_); - for (size_t i = 0; i < std::min(row_num_, (int) data.size()); i++) + int st = 0; + if (thead_ == ColumnHead || thead_ == BothHead) st = 1; + + data.resize(row_num_ - st); + for (size_t i = st; i < row_num_; i++) { - data[i].x = table_[i][xid].value(); - data[i].y = table_[i][yid].value(); - data[i].z = table_[i][zid].value(); + data[i - st].x = table_[i][xid].value(); + data[i - st].y = table_[i][yid].value(); + data[i - st].z = table_[i][zid].value(); } return; +} + +void gctl::geodsv_io::get_column_point3dc(std::string xname, std::string yname, std::string zname, array &data) +{ + get_column_point3dc(name_index(xname, false), name_index(yname, false), name_index(zname, false), data); + return; } \ No newline at end of file diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 9585595..8e0fb72 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -25,8 +25,8 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#ifndef _GCTL_TEXT_IO2_H -#define _GCTL_TEXT_IO2_H +#ifndef _GCTL_DSV_IO_H +#define _GCTL_DSV_IO_H #include "../core.h" #include "../utility.h" @@ -34,7 +34,7 @@ namespace gctl { - struct cell_content + struct table_cell { std::string str_; @@ -78,7 +78,7 @@ namespace gctl }; /** - * @brief 文本读写类 + * @brief DSV文本读写类 * * 可以处理的文本数据应该符合下述要求: * 1. 以'#'开始的行均为注释行,标识符可由用户指定; @@ -100,7 +100,7 @@ namespace gctl // 头信息行 注释行 标记行 std::vector heads_, annotates_, tags_; // 内容表格 - std::vector > table_; + std::vector > table_; public: /** @@ -264,6 +264,15 @@ namespace gctl */ void info(); + /** + * @brief 返回名称为name的行或列的索引 + * + * @param name 名称 + * @param iter_row 搜索行名称(默认为搜索列名称) + * @return 索引 + */ + int name_index(std::string name, bool iter_row = false); + /** * @brief 填充列 * @@ -362,7 +371,7 @@ namespace gctl { if (idx >= col_num_) { - throw std::runtime_error("[gctl::dsv_io::fill_column] Invalid column index."); + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } int st = 0; @@ -378,21 +387,7 @@ namespace gctl template void dsv_io::fill_column(std::string name, const array &data, int p) { - if (thead_ != ColumnHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io::fill_column] The table is initialized with no column names."); - } - - for (size_t i = 0; i < col_num_; i++) - { - if (table_[0][i].str_ == name) - { - fill_column(i, data, p); - return; - } - } - - throw std::runtime_error("[gctl::dsv_io::fill_column] No column found by the input name."); + fill_column(name_index(name, false), data, p); return; } @@ -401,7 +396,7 @@ namespace gctl { if (idx >= row_num_) { - throw std::runtime_error("[gctl::dsv_io::fill_row] Invalid row index."); + throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } int st = 0; @@ -417,22 +412,7 @@ namespace gctl template void dsv_io::fill_row(std::string name, const array &data, int p) { - if (thead_ != RowHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io::fill_row] The table is initialized with no row names."); - } - - for (size_t i = 0; i < row_num_; i++) - { - if (table_[i][0].str_ == name) - { - fill_row(i, data, p); - return; - } - } - - throw std::runtime_error("[gctl::dsv_io::fill_row] No row found by the input name."); - return; + fill_row(name_index(name, true), data, p); } template @@ -440,7 +420,7 @@ namespace gctl { if (idx >= col_num_) { - throw std::runtime_error("[gctl::dsv_io::get_column] Invalid column index."); + throw std::runtime_error("[gctl::dsv_io] Invalid column index."); } int st = 0; @@ -457,22 +437,7 @@ namespace gctl template void dsv_io::get_column(std::string name, array &data) { - if (thead_ != ColumnHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io::get_column] The table is initialized with no column names."); - } - - for (size_t i = 0; i < col_num_; i++) - { - if (table_[0][i].str_ == name) - { - get_column(i, data); - return; - } - } - - throw std::runtime_error("[gctl::dsv_io::get_column] No column found by the input name."); - return; + get_column(name_index(name, false), data); } template @@ -480,7 +445,7 @@ namespace gctl { if (idx >= row_num_) { - throw std::runtime_error("[gctl::dsv_io::get_row] Invalid row index."); + throw std::runtime_error("[gctl::dsv_io] Invalid row index."); } int st = 0; @@ -497,28 +462,26 @@ namespace gctl template void dsv_io::get_row(std::string name, array &data) { - if (thead_ != RowHead && thead_ != BothHead) - { - throw std::runtime_error("[gctl::dsv_io::get_row] The table is initialized with no row names."); - } - - for (size_t i = 0; i < row_num_; i++) - { - if (table_[i][0].str_ == name) - { - get_row(i, data); - return; - } - } - - throw std::runtime_error("[gctl::dsv_io::get_row] No row found by the input name."); - return; + get_row(name_index(name, true), data); } + /** + * @brief 地理数据类型DSV文件读写类 + * + */ class geodsv_io : public dsv_io { public: + /** + * @brief Construct a new geodsv_io object + * + */ geodsv_io(); + + /** + * @brief Destroy the geodsv_io object + * + */ ~geodsv_io(); /** @@ -529,14 +492,86 @@ namespace gctl */ geodsv_io(std::string filename, std::string file_exten = ".txt", table_headtype_e t = NoHead); + /** + * @brief 填充二维坐标列 + * + * @param xid x坐标列索引 + * @param yid y坐标列索引 + * @param data 返回的二维坐标数据 + * @param p 填入的浮点数据有效位数(精度) + */ void fill_column_point2dc(int xid, int yid, const array &data, int p = 6); + /** + * @brief 填充二维坐标列 + * + * @param xname x坐标列名称 + * @param yname y坐标列名称 + * @param data 返回的二维坐标数据 + * @param p 填入的浮点数据有效位数(精度) + */ + void fill_column_point2dc(std::string xname, std::string yname, const array &data, int p = 6); + + /** + * @brief 填充三维坐标列 + * + * @param xid x坐标列索引 + * @param yid y坐标列索引 + * @param zid z坐标列索引 + * @param data 返回的三维坐标数据 + * @param p 填入的浮点数据有效位数(精度) + */ void fill_column_point3dc(int xid, int yid, int zid, const array &data, int p = 6); + /** + * @brief 填充三维坐标列 + * + * @param xname x坐标列名称 + * @param yname y坐标列名称 + * @param zname z坐标列名称 + * @param data 返回的三维坐标数据 + * @param p 填入的浮点数据有效位数(精度) + */ + void fill_column_point3dc(std::string xname, std::string yname, std::string zname, const array &data, int p = 6); + + /** + * @brief 读取二维坐标列 + * + * @param xid x坐标列索引 + * @param yid y坐标列索引 + * @param data 返回的二维坐标数据 + */ void get_column_point2dc(int xid, int yid, array &data); + /** + * @brief 读取二维坐标列 + * + * @param xname x坐标列名称 + * @param yname y坐标列名称 + * @param data 返回的二维坐标数据 + */ + void get_column_point2dc(std::string xname, std::string yname, array &data); + + /** + * @brief 读取三维坐标列 + * + * @param xid x坐标列索引 + * @param yid y坐标列索引 + * @param zid z坐标列索引 + * @param data 返回的三维坐标数据 + */ void get_column_point3dc(int xid, int yid, int zid, array &data); + + /** + * @brief 读取三维坐标列 + * + * @param xname x坐标列名称 + * @param yname y坐标列名称 + * @param zname z坐标列名称 + * @param data 返回的三维坐标数据 + */ + void get_column_point3dc(std::string xname, std::string yname, std::string zname, array &data); }; } -#endif //_GCTL_TEXT_IO2_H \ No newline at end of file +#endif //_GCTL_DSV_IO_H \ No newline at end of file