From 7c7ffa3b106bd8a9d2bbc59d50ba5743e469f5d4 Mon Sep 17 00:00:00 2001 From: Michael Marshall Date: Mon, 25 Feb 2019 15:38:47 +0000 Subject: [PATCH] Added text read/write --- Grid/serialisation/TextIO.h | 34 +++++++++++++++++++++++++++++++++- tests/IO/Test_serialisation.cc | 2 ++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Grid/serialisation/TextIO.h b/Grid/serialisation/TextIO.h index ada1aaf6..b9b13d07 100644 --- a/Grid/serialisation/TextIO.h +++ b/Grid/serialisation/TextIO.h @@ -51,6 +51,8 @@ namespace Grid void writeDefault(const std::string &s, const U &x); template void writeDefault(const std::string &s, const std::vector &x); + template + void writeMultiDim(const std::string &s, const std::vector & Dimensions, const U * pDataRowMajor, size_t NumElements); private: void indent(void); private: @@ -69,6 +71,8 @@ namespace Grid void readDefault(const std::string &s, U &output); template void readDefault(const std::string &s, std::vector &output); + template + void readMultiDim(const std::string &s, std::vector &buf, std::vector &dim); private: void checkIndent(void); private: @@ -95,7 +99,18 @@ namespace Grid write(s, x[i]); } } - + + template + void TextWriter::writeMultiDim(const std::string &s, const std::vector & Dimensions, const U * pDataRowMajor, size_t NumElements) + { + uint64_t Rank = Dimensions.size(); + write(s, Rank); + for( uint64_t d : Dimensions ) + write(s, d); + while( NumElements-- ) + write(s, *pDataRowMajor++); + } + // Reader template implementation //////////////////////////////////////////// template void TextReader::readDefault(const std::string &s, U &output) @@ -121,6 +136,23 @@ namespace Grid read("", output[i]); } } + + template + void TextReader::readMultiDim(const std::string &s, std::vector &buf, std::vector &dim) + { + const char sz[] = ""; + uint64_t Rank; + read(sz, Rank); + dim.resize( Rank ); + size_t NumElements = 1; + for( auto &d : dim ) { + read(sz, d); + NumElements *= d; + } + buf.resize( NumElements ); + for( auto i = 0; i < NumElements; i++ ) + read(s, buf[i]); + } } #endif diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index 305de088..77f69b7c 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -299,6 +299,8 @@ int main(int argc,char **argv) EigenHdf5IOTest(".xml"); std::cout << "\n==== detailed binary tensor tests (Grid::EigenIO)" << std::endl; EigenHdf5IOTest(".bin"); + std::cout << "\n==== detailed text tensor tests (Grid::EigenIO)" << std::endl; + EigenHdf5IOTest(".dat"); std::cout << "\n==== vector flattening/reconstruction" << std::endl; typedef std::vector>> vec3d;