]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Provide BlockVector with a new constructor that initializes the vector from an iterat...
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 9 Aug 2001 12:46:50 +0000 (12:46 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 9 Aug 2001 12:46:50 +0000 (12:46 +0000)
git-svn-id: https://svn.dealii.org/trunk@4872 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/2001/c-3-1.html
deal.II/lac/include/lac/block_vector.h
deal.II/lac/include/lac/block_vector.templates.h
tests/lac/block_vector.cc
tests/lac/block_vector.checked

index ee4dc6318a594f780505b10b0521d3e86d1add7a..e7726ec5cd4d93bc9c7fd7c4905f8b08ee093e89 100644 (file)
@@ -333,7 +333,8 @@ documentation, etc</a>.
 
 <ol>
   <li> <p>
-       Changed: The <code class="class">Vector</code> class can now be
+       Changed: The <code class="class">Vector</code> and
+       <code class="class">BlockVector</code> classes can now be
        initialized using a new constructor that takes two iterators
        that denote a range of elements which are to be copied.
        <br>
index 5ef51077eb8b19c2635c4ec46f3d61f1075af37c..cf3742122d70899103702c87f6ae474bc933dff2 100644 (file)
@@ -125,6 +125,29 @@ class BlockVector
                                      */
     BlockVector (const std::vector<unsigned int> &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 <typename InputIterator>
+    BlockVector (const std::vector<unsigned int> &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 <typename Number>
+template <typename InputIterator>
+BlockVector<Number>::BlockVector (const std::vector<unsigned int> &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.size(); ++b)
+    {
+      InputIterator end = start;
+      std::advance (end, static_cast<signed int>(n[b]));
+      std::copy (start, end, block(b).begin());
+      start = end;
+    };
+  Assert (start == end, ExcIteratorRangeDoesNotMatchVectorSize());
+};
+
+
+
+
 template <typename Number>
 inline
 unsigned int BlockVector<Number>::size () const
index a5aafd99ab1281ce11bf4a7a3183f2a32e471aa3..68af1979dbd06f55acf9fb4bb1bd6d2cc4b4d547 100644 (file)
@@ -73,7 +73,7 @@ BlockVector<Number>::BlockVector (const BlockVector<Number>& v)
 template <typename Number>
 void BlockVector<Number>::reinit (const unsigned int n_bl,
                                  const unsigned int bl_sz,
-                                 const bool                  fast)
+                                 const bool         fast)
 {
   std::vector<unsigned int> n(n_bl, bl_sz);
   reinit(n, fast);
index 618f55ebc1ce1e76e632476a3e7d4ac38dc47e2b..f884d024cc8d035f3f66d14c13f762fa519a8f26 100644 (file)
@@ -18,6 +18,7 @@
 #include <lac/block_vector.h>
 #include <fstream>
 #include <vector>
+#include <list>
 
 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<double> v1(ivector, &array[0], &array[6]);
+  for (unsigned int i=0; i<v1.size(); ++i)
+    deallog << v1(i) << ' ';
+  deallog << std::endl;
+
+                                  // same test, but do not initialize
+                                  // from double*'s, but from
+                                  // std::list iterators.
+  std::list<double> l(&array[0], &array[6]);
+  BlockVector<double> v2(ivector, l.begin(), l.end());
+  for (unsigned int i=0; i<v2.size(); ++i)
+    deallog << v2(i) << ' ';
+  deallog << std::endl;
+  deallog.pop ();
+
+  
   i3.reinit(ivector);
 
   deallog.push("global->local");
index 811f688b145f6753f6a848d9842d416657571a47..a0081f9b9db6ef2c4be407510c10f2c41df93381 100644 (file)
@@ -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

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.