From: guido Date: Wed, 16 Apr 2003 11:40:12 +0000 (+0000) Subject: block_read and block_write functions X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb80496ffff2c3a652759d2b96070d3d0cc48312;p=dealii-svn.git block_read and block_write functions git-svn-id: https://svn.dealii.org/trunk@7397 0785d39b-7218-0410-832d-ea1e28bc413d --- 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::n () const template inline -const typename SparseMatrixEZ::Entry* +typename SparseMatrixEZ::Entry* SparseMatrixEZ::locate (const unsigned int row, - const unsigned int col) const + const unsigned int col) { Assert (row::locate (const unsigned int row, const unsigned int end = r.start + r.length; for (unsigned int i=r.start;icolumn == col) return entry; if (entry->column == Entry::invalid) @@ -1223,6 +1262,17 @@ SparseMatrixEZ::locate (const unsigned int row, +template +inline +const typename SparseMatrixEZ::Entry* +SparseMatrixEZ::locate (const unsigned int row, + const unsigned int col) const +{ + SparseMatrixEZ* t = const_cast*> (this); + return t->locate(row,col); +} + + template inline typename SparseMatrixEZ::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::print_statistics(STREAM& out, bool full) } +template +void +SparseMatrixEZ::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::const_iterator r = row_info.begin(); + const typename std::vector::const_iterator re = row_info.end(); + + while (r != re) + { + out.write(reinterpret_cast(&*r), + sizeof(RowInfo)); + ++r; + } + + out << "]["; + + typename std::vector::const_iterator d = data.begin(); + const typename std::vector::const_iterator de = data.end(); + + while (d != de) + { + out.write(reinterpret_cast(&*d), + sizeof(Entry)); + ++d; + } + + out << ']'; + + AssertThrow (out, ExcIO()); +} + + +#define CHECKFOR(in,a,c) {in >> c; AssertThrow(c == a, ExcIO());} + +template +void +SparseMatrixEZ::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[i]), + sizeof(RowInfo)); + + CHECKFOR(in,']',c); + CHECKFOR(in,'[',c); + + for (unsigned int i=0;i(&data[i]), + sizeof(Entry)); + + CHECKFOR(in,']',c); +} + #endif // __deal2__sparse_matrix_ez_templates_h