From bb80496ffff2c3a652759d2b96070d3d0cc48312 Mon Sep 17 00:00:00 2001
From: guido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Date: Wed, 16 Apr 2003 11:40:12 +0000
Subject: [PATCH] block_read and block_write functions

git-svn-id: https://svn.dealii.org/trunk@7397 0785d39b-7218-0410-832d-ea1e28bc413d
---
 deal.II/lac/include/lac/sparse_matrix_ez.h    | 58 ++++++++++++-
 .../include/lac/sparse_matrix_ez.templates.h  | 87 +++++++++++++++++++
 2 files changed, 141 insertions(+), 4 deletions(-)

diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.h b/deal.II/lac/include/lac/sparse_matrix_ez.h
index f49ec9d6be..82763cf7a4 100644
--- a/deal.II/lac/include/lac/sparse_matrix_ez.h
+++ b/deal.II/lac/include/lac/sparse_matrix_ez.h
@@ -877,6 +877,35 @@ class SparseMatrixEZ : public Subscriptor
 //  			  const char         *zero_string = " ",
 //  			  const double        denominator = 1.) const;
 
+    				     /**
+				      * Write the data of this object
+				      * in binary mode to a file.
+				      *
+				      * Note that this binary format
+				      * is platform dependent.
+				      */
+    void block_write (std::ostream &out) const;
+
+				     /**
+				      * Read data that has previously
+				      * been written by
+				      * @p{block_write}.
+				      *
+				      * The object is resized on this
+				      * operation, and all previous
+				      * contents are lost.
+				      *
+				      * 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 (std::istream &in);
+
+
 				     /**
 				      * Determine an estimate for the
 				      * memory consumption (in bytes)
@@ -922,13 +951,23 @@ class SparseMatrixEZ : public Subscriptor
 		   << ") cannot be allocated.");
   private:
 				     /**
-				      * Find an entry. Return a
+				      * Find an entry and return a
+				      * const pointer. Return a
 				      * zero-pointer if the entry does
 				      * not exist.
 				      */
     const Entry* locate (const unsigned int row,
 			 const unsigned int col) const;
 
+				     /**
+				      * Find an entry and return a
+				      * writable pointer. Return a
+				      * zero-pointer if the entry does
+				      * not exist.
+				      */
+    Entry* locate (const unsigned int row,
+		   const unsigned int col);
+
 				     /**
 				      * Find an entry or generate it.
 				      */
@@ -1201,9 +1240,9 @@ unsigned int SparseMatrixEZ<number>::n () const
 
 template <typename number>
 inline
-const typename SparseMatrixEZ<number>::Entry*
+typename SparseMatrixEZ<number>::Entry*
 SparseMatrixEZ<number>::locate (const unsigned int row,
-				const unsigned int col) const
+				const unsigned int col)
 {
   Assert (row<m(), ExcIndexRange(row,0,m()));
   Assert (col<n(), ExcIndexRange(col,0,n()));
@@ -1212,7 +1251,7 @@ SparseMatrixEZ<number>::locate (const unsigned int row,
   const unsigned int end = r.start + r.length;
   for (unsigned int i=r.start;i<end;++i)
     {
-      const Entry * const entry = &data[i];
+      Entry * const entry = &data[i];
       if (entry->column == col)
 	return entry;
       if (entry->column == Entry::invalid)
@@ -1223,6 +1262,17 @@ SparseMatrixEZ<number>::locate (const unsigned int row,
 
 
 
+template <typename number>
+inline
+const typename SparseMatrixEZ<number>::Entry*
+SparseMatrixEZ<number>::locate (const unsigned int row,
+				const unsigned int col) const
+{
+  SparseMatrixEZ<number>* t = const_cast<SparseMatrixEZ<number>*> (this);
+  return t->locate(row,col);
+}
+
+
 template <typename number>
 inline
 typename SparseMatrixEZ<number>::Entry*
diff --git a/deal.II/lac/include/lac/sparse_matrix_ez.templates.h b/deal.II/lac/include/lac/sparse_matrix_ez.templates.h
index 47c7750af0..e8083ab1ea 100644
--- a/deal.II/lac/include/lac/sparse_matrix_ez.templates.h
+++ b/deal.II/lac/include/lac/sparse_matrix_ez.templates.h
@@ -409,4 +409,91 @@ SparseMatrixEZ<number>::print_statistics(STREAM& out, bool full)
 }
 
 
+template <typename number>
+void
+SparseMatrixEZ<number>::block_write (std::ostream &out) const 
+{
+  AssertThrow (out, ExcIO());
+  
+                                   // first the simple objects,
+                                   // bracketed in [...]
+  out << '[' << row_info.size() << "]["
+      << n_columns << "]["
+      << data.size() << "]["
+      << increment << "][";
+                                   // then write out real data
+  typename std::vector<RowInfo>::const_iterator r = row_info.begin();
+  const typename std::vector<RowInfo>::const_iterator re = row_info.end();
+  
+  while (r != re)
+    {
+      out.write(reinterpret_cast<const char*>(&*r),
+		sizeof(RowInfo));
+      ++r;
+    }
+
+  out << "][";
+  
+  typename std::vector<Entry>::const_iterator d = data.begin();
+  const typename std::vector<Entry>::const_iterator de = data.end();
+
+  while (d != de)
+    {
+      out.write(reinterpret_cast<const char*>(&*d),
+		sizeof(Entry));
+      ++d;
+    }
+  
+  out << ']';
+  
+  AssertThrow (out, ExcIO());
+}
+
+
+#define CHECKFOR(in,a,c) {in >> c; AssertThrow(c == a, ExcIO());}
+
+template <typename number>
+void
+SparseMatrixEZ<number>::block_read (std::istream &in)
+{
+  AssertThrow (in, ExcIO());
+
+  char c;
+  int n;
+                                   // first read in simple data
+  CHECKFOR(in,'[',c);
+  in >> n;
+  row_info.resize(n);
+  
+  CHECKFOR(in,']',c);
+  CHECKFOR(in,'[',c);
+  in >> n_columns;
+  
+  CHECKFOR(in,']',c);
+  CHECKFOR(in,'[',c);
+  in >> n;
+  data.resize(n);
+  
+  CHECKFOR(in,']',c);
+  CHECKFOR(in,'[',c);
+  in >> increment;
+
+  CHECKFOR(in,']',c);
+  CHECKFOR(in,'[',c);
+
+                                   // then read data
+  for (unsigned int i=0;i<row_info.size();++i)
+    in.read(reinterpret_cast<char*>(&row_info[i]),
+	    sizeof(RowInfo));
+  
+  CHECKFOR(in,']',c);
+  CHECKFOR(in,'[',c);
+	    
+  for (unsigned int i=0;i<data.size();++i)
+    in.read(reinterpret_cast<char*>(&data[i]),
+	    sizeof(Entry));
+  
+  CHECKFOR(in,']',c);
+}
+
 #endif // __deal2__sparse_matrix_ez_templates_h
-- 
2.39.5