From a1e34688aa5ac63406d5ef20e67b93e1f925b5c1 Mon Sep 17 00:00:00 2001 From: wolf Date: Sun, 28 Feb 1999 19:29:15 +0000 Subject: [PATCH] Add raw read/write functionality. git-svn-id: https://svn.dealii.org/trunk@929 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/lac/include/lac/vector.h | 27 +++++++++++++++ deal.II/lac/include/lac/vector.templates.h | 38 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/deal.II/lac/include/lac/vector.h b/deal.II/lac/include/lac/vector.h index 7ae125e0f5..65b5ece2a5 100644 --- a/deal.II/lac/include/lac/vector.h +++ b/deal.II/lac/include/lac/vector.h @@ -381,6 +381,33 @@ class Vector { * Print to given stream, one element per line. */ void print (ostream &) const; + + /** + * Write the vector en bloc to a file. This + * is done in a binary mode, so the output + * is neither readable by humans nor + * (probably) by other computers using + * a different operating system of number + * format. + */ + void block_write (ostream &out) const; + + /** + * Read a vector en block from a file. This + * is done using the inverse operations to + * the above function, so it is reasonably + * fast because the bitstream is not + * interpreted. + * + * The vector is resized if necessary. + * + * A primitive form of error checking is + * performed which will recognize the + * bluntest attempts to interpret some + * data as a vector stored bitwise to a + * file, but not more. + */ + void block_read (istream &in); //@} /** diff --git a/deal.II/lac/include/lac/vector.templates.h b/deal.II/lac/include/lac/vector.templates.h index b05f8cf91f..7a8205afb8 100644 --- a/deal.II/lac/include/lac/vector.templates.h +++ b/deal.II/lac/include/lac/vector.templates.h @@ -554,3 +554,41 @@ void Vector::print (ostream &out) const { +template +void Vector::block_write (ostream &out) const { + AssertThrow (out, ExcIO()); + + out << size() << endl << '['; + out.write (reinterpret_cast(begin()), + reinterpret_cast(end()) + - reinterpret_cast(begin())); + out << ']'; + + AssertThrow (out, ExcIO()); +}; + + + +template +void Vector::block_read (istream &in) { + AssertThrow (in, ExcIO()); + + unsigned int sz; + in >> sz; + // fast initialization, since the + // data elements are overwritten anyway + reinit (sz, true); + + char c; + in >> c; + AssertThrow (c=='[', ExcIO()); + + in.read (reinterpret_cast(begin()), + reinterpret_cast(end()) + - reinterpret_cast(begin())); + + in >> c; + AssertThrow (c==']', ExcIO()); + AssertThrow (in, ExcIO()); +}; + -- 2.39.5