From: wolf Date: Thu, 9 Aug 2001 12:46:50 +0000 (+0000) Subject: Provide BlockVector with a new constructor that initializes the vector from an iterat... X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3af691def92b7e935ca12c6c13bd9a593a147523;p=dealii-svn.git Provide BlockVector with a new constructor that initializes the vector from an iterator range. git-svn-id: https://svn.dealii.org/trunk@4872 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/2001/c-3-1.html b/deal.II/doc/news/2001/c-3-1.html index ee4dc6318a..e7726ec5cd 100644 --- a/deal.II/doc/news/2001/c-3-1.html +++ b/deal.II/doc/news/2001/c-3-1.html @@ -333,7 +333,8 @@ documentation, etc.
  1. - Changed: The Vector class can now be + Changed: The Vector and + BlockVector classes can now be initialized using a new constructor that takes two iterators that denote a range of elements which are to be copied.
    diff --git a/deal.II/lac/include/lac/block_vector.h b/deal.II/lac/include/lac/block_vector.h index 5ef51077eb..cf3742122d 100644 --- a/deal.II/lac/include/lac/block_vector.h +++ b/deal.II/lac/include/lac/block_vector.h @@ -125,6 +125,29 @@ class BlockVector */ BlockVector (const std::vector &n); + /** + * Constructor. Set the number of + * blocks to + * @p{n.size()}. Initialize the + * vector with the elements + * pointed to by the range of + * iterators given as second and + * third argument. Apart from the + * first argument, this + * constructor is in complete + * analogy to the respective + * constructor of the + * @p{std::vector} class, but the + * first argument is needed in + * order to know how to subdivide + * the block vector into + * different blocks. + */ + template + BlockVector (const std::vector &n, + const InputIterator first, + const InputIterator end); + /** * Destructor. Clears memory */ @@ -486,6 +509,11 @@ class BlockVector //@} + /** + * Exception + */ + DeclException0 (ExcIteratorRangeDoesNotMatchVectorSize); + protected: /** * Pointer to the array of components. @@ -514,6 +542,30 @@ class BlockVector /*----------------------- Inline functions ----------------------------------*/ +template +template +BlockVector::BlockVector (const std::vector &n, + const InputIterator first, + const InputIterator end) +{ + // first set sizes of blocks, but + // don't initialize them as we will + // copy elements soon + reinit (n, true); + InputIterator start = first; + for (unsigned int b=0; b(n[b])); + std::copy (start, end, block(b).begin()); + start = end; + }; + Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize()); +}; + + + + template inline unsigned int BlockVector::size () const diff --git a/deal.II/lac/include/lac/block_vector.templates.h b/deal.II/lac/include/lac/block_vector.templates.h index a5aafd99ab..68af1979db 100644 --- a/deal.II/lac/include/lac/block_vector.templates.h +++ b/deal.II/lac/include/lac/block_vector.templates.h @@ -73,7 +73,7 @@ BlockVector::BlockVector (const BlockVector& v) template void BlockVector::reinit (const unsigned int n_bl, const unsigned int bl_sz, - const bool fast) + const bool fast) { std::vector n(n_bl, bl_sz); reinit(n, fast); diff --git a/tests/lac/block_vector.cc b/tests/lac/block_vector.cc index 618f55ebc1..f884d024cc 100644 --- a/tests/lac/block_vector.cc +++ b/tests/lac/block_vector.cc @@ -18,6 +18,7 @@ #include #include #include +#include void test () { @@ -43,6 +44,27 @@ void test () << std::endl; deallog.pop(); + + // initialization by an iterator + // range + deallog.push ("Initialization from iterators"); + double array[] = { 0, 1, 2, 3, 4, 5 }; + BlockVector v1(ivector, &array[0], &array[6]); + for (unsigned int i=0; i l(&array[0], &array[6]); + BlockVector v2(ivector, l.begin(), l.end()); + for (unsigned int i=0; ilocal"); diff --git a/tests/lac/block_vector.checked b/tests/lac/block_vector.checked index 811f688b14..a0081f9b9d 100644 --- a/tests/lac/block_vector.checked +++ b/tests/lac/block_vector.checked @@ -1,4 +1,6 @@ +DEAL:BlockIndices:Initialization from iterators::0.000 1.000 2.000 3.000 4.000 5.000 +DEAL:BlockIndices:Initialization from iterators::0.000 1.000 2.000 3.000 4.000 5.000 DEAL:BlockIndices:global->local::0 0 0 0 0 0 0 DEAL:BlockIndices:global->local::1 0 1 0 1 0 1 DEAL:BlockIndices:global->local::2 0 2 0 2 0 2