From 91b23567b08212eaa8ef268b3d35a427cc236875 Mon Sep 17 00:00:00 2001 From: bangerth Date: Thu, 19 Nov 2009 04:20:31 +0000 Subject: [PATCH] IndexSet::operator==/!= and operator &. git-svn-id: https://svn.dealii.org/trunk@20136 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/index_set.h | 139 ++++++++++++++++++++++++++ tests/base/index_set_13.cc | 72 +++++++++++++ tests/base/index_set_13/cmp/generic | 22 ++++ 3 files changed, 233 insertions(+) create mode 100644 tests/base/index_set_13.cc create mode 100644 tests/base/index_set_13/cmp/generic diff --git a/deal.II/base/include/base/index_set.h b/deal.II/base/include/base/index_set.h index e774eac9ac..cb2571825f 100644 --- a/deal.II/base/include/base/index_set.h +++ b/deal.II/base/include/base/index_set.h @@ -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::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 index 0000000000..4a83e5ae67 --- /dev/null +++ b/tests/base/index_set_13.cc @@ -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 +#include +#include +#include + +#include + + +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