ARCS6 AR6-REV.24062600
読み取り中…
検索中…
一致する文字列を見つけられません
CsvManipulator.hh
[詳解]
1
8//
9// Copyright (C) 2011-2020 Yokokura, Yuki
10// This program is free software;
11// you can redistribute it and/or modify it under the terms of the FreeBSD License.
12// For details, see the License.txt file.
13
14#ifndef CSVMANIPULATOR
15#define CSVMANIPULATOR
16
17#include <cassert>
18#include <array>
19#include <memory>
20#include <string>
21#include <fstream>
22#include "Matrix.hh"
23
24// ARCS組込み用マクロ
25#ifdef ARCS_IN
26 // ARCSに組み込まれる場合
27 #include "ARCSassert.hh"
28 #include "ARCSeventlog.hh"
29#else
30 // ARCSに組み込まれない場合
31 #define arcs_assert(a) (assert(a))
32 #define PassedLog()
33 #define EventLog(a)
34 #define EventLogVar(a)
35#endif
36
37namespace ARCS { // ARCS名前空間
39enum class CsvExpression {
40 NORMAL,
43};
44
47 public:
53 template <CsvExpression E = CsvExpression::EXPONENTIAL, size_t M>
54 static void SaveFile(const std::array<double, M>& Data, const std::string& FileName){
55 std::ofstream fout(FileName.c_str(), std::ios::out | std::ios::trunc); // ファイル出力ストリーム
56 CheckError(fout); // エラーチェック
57 SetExpression<E>(fout); // 浮動小数点数の表現設定
58
59 // CSVデータの書き出し
60 for(size_t j = 0; j < M; ++j){ // 行数分だけ回す
61 fout << Data.at(j); // 数値の書き出し
62 fout << std::endl; // 改行
63 }
64 }
65
71 template <typename T = double, size_t M>
72 static void LoadFile(std::array<T,M>& Data, const std::string& FileName){
73 std::ifstream fin(FileName.c_str()); // ファイル入力ストリーム
74 CheckError(fin, FileName); // エラーチェック
75
76 // CSVデータの読み込み
77 std::string ReadBuff = ""; // 読み込みバッファ
78 size_t j = 0; // 行カウンタ
79 while(getline(fin, ReadBuff)){ // 1行読み込み
80 ++j; // 行カウンタ
81 arcs_assert(j <= M); // 行カウンタ溢れチェック
82
83 // データ型に従った処理
84 if constexpr(std::is_same_v<T,double>){
85 Data.at(j - 1) = std::stod(ReadBuff); // 浮動小数点数値に変換してから配列データに書き込み
86 }
87 if constexpr(std::is_same_v<T,std::string>){
88 Data.at(j - 1) = ReadBuff; // そのまま配列データに書き込み
89 }
90
91 }
92 }
93
102 template <CsvExpression E = CsvExpression::EXPONENTIAL, size_t N, size_t M>
103 static void SaveFile(const std::array<std::array<double, N>, M>& Data, const std::string& FileName, size_t NN, size_t MM){
104 std::ofstream fout(FileName.c_str(), std::ios::out | std::ios::trunc); // ファイル出力ストリーム
105 CheckError(fout); // エラーチェック
106 SetExpression<E>(fout); // 浮動小数点数の表現設定
107
108 // CSVデータの書き出し
109 for(size_t j = 0; j < MM; ++j){ // 行数分だけ回す
110 for(size_t i = 0; i < NN; ++i){ // 列数分だけ回す
111 fout << Data.at(j).at(i); // 数値の書き出し
112 if(i < NN - 1){ // 最後の列以外のときは,
113 fout << ','; // コンマで区切る
114 }
115 }
116 fout << std::endl; // 改行
117 }
118 }
119
126 template <CsvExpression E = CsvExpression::EXPONENTIAL, size_t N, size_t M>
127 static void SaveFile(const std::array<std::array<double, N>, M>& Data, const std::string& FileName){
128 SaveFile<E, N, M>(Data, FileName, N, M);
129 }
130
139 template <CsvExpression E = CsvExpression::EXPONENTIAL, size_t N, size_t M>
140 static void SaveFile(std::unique_ptr< std::array<std::array<double, N>, M> >&& Data, const std::string& FileName, size_t NN, size_t MM){
141 std::ofstream fout(FileName.c_str(), std::ios::out | std::ios::trunc); // ファイル出力ストリーム
142 CheckError(fout); // エラーチェック
143 SetExpression<E>(fout); // 浮動小数点数の表現設定
144
145 // CSVデータの書き出し
146 for(size_t j = 0; j < MM; ++j){ // 行数分だけ回す
147 for(size_t i = 0; i < NN; ++i){ // 列数分だけ回す
148 fout << Data->at(j).at(i); // 数値の書き出し
149 if(i < NN - 1){ // 最後の列以外のときは,
150 fout << ','; // コンマで区切る
151 }
152 }
153 fout << std::endl; // 改行
154 }
155 }
156
162 template <size_t N, size_t M>
163 static void LoadFile(std::array<std::array<double, N>, M>& Data, const std::string& FileName){
164 std::ifstream fin(FileName.c_str()); // ファイル入力ストリーム
165 CheckError(fin, FileName); // エラーチェック
166
167 // CSVデータの読み込み
168 std::string ReadBuff = ""; // 読み込みバッファ
169 std::array<double, N> ValBuff; // 数値配列バッファ
170 size_t j = 0; // 行カウンタ
171 while(getline(fin, ReadBuff)){ // 1行読み込み
172 ++j; // 行カウンタ
173 arcs_assert(j <= M); // 行カウンタ溢れチェック
174 ParseCsvString(ReadBuff, ValBuff); // CSVパース&読み込み
175 for(size_t i = 0; i < N; ++i){
176 Data.at(j-1).at(i) = ValBuff.at(i);
177 }
178 }
179 }
180
187 template <CsvExpression E = CsvExpression::EXPONENTIAL, size_t N, size_t M>
188 static void SaveFile(const Matrix<N,M>& Data, const std::string& FileName){
189 std::ofstream fout(FileName.c_str(), std::ios::out | std::ios::trunc); // ファイル出力ストリーム
190 CheckError(fout); // エラーチェック
191 SetExpression<E>(fout); // 浮動小数点数の表現設定
192
193 // CSVデータの書き出し
194 for(size_t j = 0; j < M; ++j){ // 行数分だけ回す
195 for(size_t i = 0; i < N; ++i){ // 列数分だけ回す
196 fout << Data.GetElement(i+1,j+1); // 数値の書き出し
197 if(i < N - 1){ // 最後の列以外のときは,
198 fout << ','; // コンマで区切る
199 }
200 }
201 fout << std::endl; // 改行
202 }
203 }
204
210 template <size_t N, size_t M>
211 static void LoadFile(Matrix<N,M>& Data, const std::string& FileName){
212 std::ifstream fin(FileName.c_str()); // ファイル入力ストリーム
213 CheckError(fin, FileName); // エラーチェック
214
215 // CSVデータの読み込み
216 std::string ReadBuff = ""; // 読み込みバッファ
217 std::array<double, N> ValBuff; // 数値配列バッファ
218 size_t j = 0; // 行カウンタ
219 while(getline(fin, ReadBuff)){ // 1行読み込み
220 ++j; // 行カウンタ
221 arcs_assert(j <= M); // 行カウンタ溢れチェック
222 ParseCsvString(ReadBuff, ValBuff); // CSVパース&読み込み
223 setrow(Data, ValBuff, j); // 行を書き込む
224 }
225 }
226
227 private:
228 CsvManipulator() = delete;
229 CsvManipulator(CsvManipulator&& r) = delete;
230 ~CsvManipulator() = delete;
231 CsvManipulator(const CsvManipulator&) = delete;
232 const CsvManipulator& operator=(const CsvManipulator&) = delete;
233
236 static void CheckError(const std::ofstream& fileout){
237 arcs_assert(fileout.bad() == false); // 致命的なエラーの場合
238 arcs_assert(fileout.fail() == false); // ファイルを開くのに失敗した場合
239 }
240
244 static void CheckError(const std::ifstream& filein, const std::string& filename){
245 EventLog("Checking CSV File Read Error of " + filename + "...");
246 arcs_assert(filein.is_open() == true); // ファイルを開くのに失敗した場合
247 EventLog("Checking CSV File Read Error of " + filename + "...Done");
248 }
249
253 template <CsvExpression E>
254 static void SetExpression(std::ofstream& fileout){
255 if constexpr(E == CsvExpression::NORMAL){
256 // 通常表記の場合
257 fileout.setf(std::ios::fixed);
258 }
259 if constexpr(E == CsvExpression::EXPONENTIAL){
260 // 指数表記の場合
261 fileout.setf(std::ios::scientific); // 指数表示
262 fileout.width(15); // 総桁数
263 fileout.precision(14); // 小数点桁数
264 }
265 if constexpr(E == CsvExpression::HEXFLOAT){
266 // 16進数浮動小数点表記の場合
267 fileout.setf(std::ios::scientific | std::ios::fixed);
268 }
269 }
270
275 template <size_t N>
276 static void ParseCsvString(const std::string& CsvLine, std::array<double, N>& ValArray){
277 size_t FirstIndex = 0; // 抽出開始位置
278 size_t LastIndex = 0; // 抽出終了位置
279 std::string ParseBuff = ""; // パース用バッファ
280 double ValBuff = 0; // 浮動小数点用バッファ
281 size_t j = 0; // 列番号インデックス
282 while(LastIndex != std::string::npos){ // 「,」が出現しなくなるまでループ
283 arcs_assert(j < N); // 列番号溢れチェック
284 LastIndex = CsvLine.find_first_of(",", FirstIndex); // 次の「,」の位置を探す
285 ParseBuff = CsvLine.substr(FirstIndex, LastIndex - FirstIndex); // 「,」の手前まで文字列抽出
286 ValBuff = std::stod(ParseBuff); // 浮動小数点数値に変換
287 ValArray.at(j) = ValBuff; // 数値配列に書き込み
288 FirstIndex = LastIndex + 1; // 次のループでの「,」の探索開始位置
289 ++j; // 列番号インデックス
290 }
291 }
292
293
294};
295}
296
297#endif
298
ARCS イベントログクラス
#define EventLog(a)
イベントログ用マクロ (任意メッセージ記録版)
Definition ARCSeventlog.hh:27
ARCS用ASSERTクラス
#define arcs_assert(a)
ARCS用assertマクロ a : assert条件
Definition ARCSassert.hh:17
CsvExpression
数値表現の定義
Definition CsvManipulator.hh:39
@ NORMAL
通常表記
@ HEXFLOAT
16進数浮動小数点 (完全に正確な浮動小数点表記)
@ EXPONENTIAL
指数表示 (6.33e+4のような表記)
行列/ベクトル計算クラス(テンプレート版)
CSVファイル操作クラス
Definition CsvManipulator.hh:46
static void SaveFile(std::unique_ptr< std::array< std::array< double, N >, M > > &&Data, const std::string &FileName, size_t NN, size_t MM)
2次元std::arrayをCSVファイルに書き出す関数(スマートポインタ&書き出すサイズを指定する版)
Definition CsvManipulator.hh:140
static void SaveFile(const Matrix< N, M > &Data, const std::string &FileName)
行列をCSVファイルに書き出す関数
Definition CsvManipulator.hh:188
static void LoadFile(Matrix< N, M > &Data, const std::string &FileName)
CSVファイルから行列に読み込む関数
Definition CsvManipulator.hh:211
static void SaveFile(const std::array< std::array< double, N >, M > &Data, const std::string &FileName)
2次元std::arrayをCSVファイルに書き出す関数
Definition CsvManipulator.hh:127
static void SaveFile(const std::array< double, M > &Data, const std::string &FileName)
1次元std::arrayをCSVファイルに書き出す関数
Definition CsvManipulator.hh:54
static void LoadFile(std::array< std::array< double, N >, M > &Data, const std::string &FileName)
CSVファイルから2次元std::arrayに読み込む関数
Definition CsvManipulator.hh:163
static void SaveFile(const std::array< std::array< double, N >, M > &Data, const std::string &FileName, size_t NN, size_t MM)
2次元std::arrayをCSVファイルに書き出す関数(書き出すサイズを指定する版)
Definition CsvManipulator.hh:103
static void LoadFile(std::array< T, M > &Data, const std::string &FileName)
CSVファイルから1次元std::arrayに読み込む関数
Definition CsvManipulator.hh:72
行列/ベクトル計算クラス(テンプレート版)
Definition Matrix.hh:44
constexpr TT GetElement(size_t n, size_t m) const
指定した要素番号の値を返す関数
Definition Matrix.hh:489