]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
New function IndexSet::fill_binary_vector.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 12 Feb 2012 16:53:39 +0000 (16:53 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 12 Feb 2012 16:53:39 +0000 (16:53 +0000)
git-svn-id: https://svn.dealii.org/trunk@25042 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/base/index_set.h
tests/base/index_set_22.cc [new file with mode: 0644]
tests/base/index_set_22/cmp/generic [new file with mode: 0644]

index 89f9899599e10221f6a5450154ab22e0471f45e6..8b1dbc2213e23d0351591ec7622efa2141ac8cd3 100644 (file)
@@ -87,6 +87,11 @@ enabled due to a missing include file in file
 <h3>Specific improvements</h3>
 
 <ol>
+<li> New: The function IndexSet::fill_binary_vector creates a numerical
+representation of an IndexSet containing zeros and ones.
+<br>
+(Wolfgang Bangerth, 2012/02/12)
+
 <li> New: The function IndexSet::clear resets an index set to be empty.
 <br>
 (Wolfgang Bangerth, 2012/02/12)
index 28f8310f44b0626e4a0c0e3dcefccfc6bd92efac..c4162e414ac12aead9c81cfba99399c8339588fa 100644 (file)
@@ -265,6 +265,29 @@ class IndexSet
                                      */
     void fill_index_vector(std::vector<unsigned int> & indices) const;
 
+                                    /**
+                                     * Fill the given vector with either
+                                     * zero or one elements, providing
+                                     * a binary representation of this
+                                     * index set. The given vector is
+                                     * assumed to already have the correct
+                                     * size.
+                                     *
+                                     * The given argument is filled with
+                                     * integer values zero and one, using
+                                     * <code>vector.operator[]</code>. Thus,
+                                     * any object that has such an operator
+                                     * can be used as long as it allows
+                                     * conversion of integers zero and one to
+                                     * elements of the vector. Specifically,
+                                     * this is the case for classes Vector,
+                                     * BlockVector, but also
+                                     * std::vector@<bool@>,
+                                     * std::vector@<int@>, and
+                                     * std::vector@<double@>.
+                                     */
+    template <typename Vector>
+    void fill_binary_vector (Vector &vector) const;
 
                                     /**
                                      * Outputs a text representation of this
@@ -955,6 +978,31 @@ IndexSet::operator != (const IndexSet &is) const
   return ranges != is.ranges;
 }
 
+
+
+template <typename Vector>
+void
+IndexSet::fill_binary_vector (Vector &vector) const
+{
+  Assert (vector.size() == size(),
+         ExcDimensionMismatch (vector.size(), size()));
+
+  compress();
+                                  // first fill all elements of the vector
+                                  // with zeroes.
+  std::fill (vector.begin(), vector.end(), 0);
+
+                                  // then write ones into the elements whose
+                                  // indices are contained in the index set
+  for (std::vector<Range>::iterator it = ranges.begin();
+       it != ranges.end();
+       ++it)
+    for (unsigned int i=it->begin; i<it->end; ++i)
+      vector[i] = 1;
+}
+
+
+
 template <class STREAM>
 inline
 void
diff --git a/tests/base/index_set_22.cc b/tests/base/index_set_22.cc
new file mode 100644 (file)
index 0000000..9ae96cb
--- /dev/null
@@ -0,0 +1,60 @@
+//-----------------------------------------------------------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2010, 2012 by the deal.II authors
+//
+//    This file is subject to QPL and may not be  distributed
+//    without copyright and license information. Please refer
+//    to the file deal.II/doc/license.html for the  text  and
+//    further information on this license.
+//
+//-----------------------------------------------------------------------------
+
+// test IndexSet::fill_binary_vector
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+#include <stdlib.h>
+
+#include <deal.II/base/index_set.h>
+
+
+void test ()
+{
+  IndexSet is1 (100);
+
+                                  // randomly add 90 elements to each
+                                  // set, some of which may be
+                                  // repetitions of previous ones
+  for (unsigned int i=0; i<9*is1.size()/10; ++i)
+    is1.add_index (rand() % is1.size());
+
+  std::vector<bool> zeros_and_ones(is1.size());
+  is1.fill_binary_vector (zeros_and_ones);
+
+  deallog << "Original index set: " << std::endl;
+  is1.print(deallog);
+
+  for (unsigned int i=0; i<is1.size(); i++)
+    Assert(is1.is_element(i) == zeros_and_ones[i],
+          ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("index_set_22/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/index_set_22/cmp/generic b/tests/base/index_set_22/cmp/generic
new file mode 100644 (file)
index 0000000..75a609a
--- /dev/null
@@ -0,0 +1,4 @@
+
+DEAL::Original index set: 
+DEAL::{[2,3], 5, 8, 11, [13,15], 19, [21,27], [29,30], [34,37], 40, [42,43], [45,46], [49,51], 54, [56,59], [62,64], [67,70], [72,73], [76,78], [80,84], [86,88], [90,93], [95,96], [98,99]}
+DEAL::OK

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.