]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
block_read and block_write functions
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 16 Apr 2003 11:40:12 +0000 (11:40 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 16 Apr 2003 11:40:12 +0000 (11:40 +0000)
git-svn-id: https://svn.dealii.org/trunk@7397 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/sparse_matrix_ez.h
deal.II/lac/include/lac/sparse_matrix_ez.templates.h

index f49ec9d6be515b3a41e5a955ae0a39e8543c31a1..82763cf7a45e5500217873b763b82e073b03e444 100644 (file)
@@ -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*
index 47c7750af0bf506180f8453b5886336d58901af1..e8083ab1ea28d083365a30ea3b2bc8f54a156fd1 100644 (file)
@@ -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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.