]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
IndexSet::operator==/!= and operator &.
authorbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 19 Nov 2009 04:20:31 +0000 (04:20 +0000)
committerbangerth <bangerth@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 19 Nov 2009 04:20:31 +0000 (04:20 +0000)
git-svn-id: https://svn.dealii.org/trunk@20136 0785d39b-7218-0410-832d-ea1e28bc413d

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

index e774eac9acc88d07c56dd976928b9c9dc12acde0..cb2571825f36a380c525f5253af3b58a406dfe0c 100644 (file)
@@ -160,6 +160,41 @@ class IndexSet
                                      */
     void compress () const;
 
+                                    /**
+                                     * Comparison for equality of
+                                     * index sets. This operation is
+                                     * only allowed if the size of
+                                     * the two sets is the same
+                                     * (though of course they do not
+                                     * have to have the same number
+                                     * of indices).
+                                     */
+    bool operator == (const IndexSet &is) const;
+
+                                    /**
+                                     * Comparison for inequality of
+                                     * index sets. This operation is
+                                     * only allowed if the size of
+                                     * the two sets is the same
+                                     * (though of course they do not
+                                     * have to have the same number
+                                     * of indices).
+                                     */
+    bool operator != (const IndexSet &is) const;
+
+                                    /**
+                                     * Return the intersection of the
+                                     * current index set and the
+                                     * argument given, i.e. a set of
+                                     * indices that are elements of
+                                     * both index sets. The two index
+                                     * sets must have the same size
+                                     * (though of course they do not
+                                     * have to have the same number
+                                     * of indices).
+                                     */
+    IndexSet operator & (const IndexSet &is) const;
+
 #ifdef DEAL_II_USE_TRILINOS
                                     /**
                                      * Given an MPI communicator,
@@ -258,6 +293,15 @@ class IndexSet
                     &&
                     (range_1.end < range_2.end)));
          }
+
+       friend
+       inline bool operator== (const Range &range_1,
+                               const Range &range_2)
+         {
+           return ((range_1.begin == range_2.begin)
+                   ||
+                   (range_1.begin == range_2.begin));
+         }
     };
 
                                     /**
@@ -548,6 +592,101 @@ IndexSet::index_within_set (const unsigned int n) const
 
 
 
+inline
+bool
+IndexSet::operator == (const IndexSet &is) const
+{
+  Assert (size() == is.size(),
+         ExcDimensionMismatch (size(), is.size()));
+
+  compress ();
+  is.compress ();
+
+  return ranges == is.ranges;
+}
+
+
+
+inline
+bool
+IndexSet::operator != (const IndexSet &is) const
+{
+  Assert (size() == is.size(),
+         ExcDimensionMismatch (size(), is.size()));
+
+  compress ();
+  is.compress ();
+
+  return ranges != is.ranges;
+}
+
+
+
+inline
+IndexSet
+IndexSet::operator & (const IndexSet &is) const
+{
+  Assert (size() == is.size(),
+         ExcDimensionMismatch (size(), is.size()));
+
+  compress ();
+  is.compress ();
+
+  std::vector<Range>::const_iterator r1 = ranges.begin(),
+                                    r2 = is.ranges.begin();
+  IndexSet result (size());
+
+  while ((r1 != ranges.end())
+        &&
+        (r2 != is.ranges.end()))
+    {
+                                      // if r1 and r2 do not overlap
+                                      // at all, then move the
+                                      // pointer that sits to the
+                                      // left of the other up by one
+      if (r1->end <= r2->begin)
+       ++r1;
+      else if (r2->end <= r1->begin)
+       ++r2;
+      else
+       {
+                                          // the ranges must overlap
+                                          // somehow
+         Assert (((r1->begin <= r2->begin) &&
+                  (r1->end > r2->begin))
+                 ||
+                 ((r2->begin <= r1->begin) &&
+                  (r2->end > r1->begin)),
+                 ExcInternalError());
+
+                                          // add the overlapping
+                                          // range to the result
+         result.add_range (std::max (r1->begin,
+                                     r2->begin),
+                           std::min (r1->end,
+                                     r2->end));
+
+                                          // now move that iterator
+                                          // that ends earlier one
+                                          // up. note that it has to
+                                          // be this one because a
+                                          // subsequent range may
+                                          // still have a chance of
+                                          // overlapping with the
+                                          // range that ends later
+         if (r1->end <= r2->end)
+           ++r1;
+         else
+           ++r2;
+       }
+    }
+
+  result.compress ();
+  return result;
+}
+
+
+
 
 
 DEAL_II_NAMESPACE_CLOSE
diff --git a/tests/base/index_set_13.cc b/tests/base/index_set_13.cc
new file mode 100644 (file)
index 0000000..4a83e5a
--- /dev/null
@@ -0,0 +1,72 @@
+//-----------------------------------------------------------------------------
+//    $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2009 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::operator &
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <base/index_set.h>
+
+
+void test ()
+{
+  IndexSet is1 (20);
+  IndexSet is2 (20);
+
+  is1.add_index (2);
+  is1.add_index (3);
+  is1.add_index (4);
+  is1.add_index (6);
+  is1.add_index (7);
+
+  is2.add_range (4,9);
+
+  IndexSet is3 = is1 & is2;
+
+  for (unsigned int i=0; i<is3.size(); ++i)
+    {
+      deallog << i << ' ' << (is3.is_element(i) ? "true" : "false")
+             << std::endl;
+
+      Assert ((is1.is_element(i) && is2.is_element(i))
+             ==
+             is3.is_element(i),
+             ExcInternalError());
+    }
+
+                                  // some sanity tests
+  Assert ((is1 & is2) == (is2 & is1), ExcInternalError());
+  Assert ((is1 & is3) == (is2 & is3), ExcInternalError());
+  Assert ((is1 & is3) == is3, ExcInternalError());
+  Assert ((is3 & is1) == is3, ExcInternalError());
+  Assert ((is3 & is3) == is3, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("index_set_13/output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/index_set_13/cmp/generic b/tests/base/index_set_13/cmp/generic
new file mode 100644 (file)
index 0000000..ae1bf53
--- /dev/null
@@ -0,0 +1,22 @@
+
+DEAL::0 false
+DEAL::1 false
+DEAL::2 false
+DEAL::3 false
+DEAL::4 true
+DEAL::5 false
+DEAL::6 true
+DEAL::7 true
+DEAL::8 false
+DEAL::9 false
+DEAL::10 false
+DEAL::11 false
+DEAL::12 false
+DEAL::13 false
+DEAL::14 false
+DEAL::15 false
+DEAL::16 false
+DEAL::17 false
+DEAL::18 false
+DEAL::19 false
+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.