]> https://gitweb.dealii.org/ - dealii.git/commitdiff
More functions.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Sat, 10 Oct 2009 01:45:25 +0000 (01:45 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Sat, 10 Oct 2009 01:45:25 +0000 (01:45 +0000)
git-svn-id: https://svn.dealii.org/trunk@19795 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/index_set.h
deal.II/base/source/index_set.cc [new file with mode: 0644]

index 7b700f175f7e373fd098e7b25eba96789831643c..1bf8729d9b1fc8bb06d7a3466c8c2957213d9dca 100644 (file)
@@ -92,6 +92,25 @@ class IndexSet
                                      */
     bool is_element (const unsigned int index) const;
 
+                                    /**
+                                     * Return whether the index set
+                                     * stored by this object defines
+                                     * a contiguous range. This is
+                                     * true also if no indices are
+                                     * stored at all.
+                                     */
+    bool is_contiguous () const;
+
+                                    /**
+                                     * Compress the internal
+                                     * representation by merging
+                                     * individual elements with
+                                     * contiguous ranges, etc. This
+                                     * function does not have any
+                                     * external effect.
+                                     */
+    void compress () const;
+
   private:
                                     /**
                                      * A type that denotes an index
@@ -113,16 +132,32 @@ class IndexSet
                                      * A set of contiguous ranges of
                                      * indices that make up (part of)
                                      * this index set.
+                                     *
+                                     * The variable is marked
+                                     * "mutable" so that it can be
+                                     * changed by compress(), though
+                                     * this of course doesn't change
+                                     * anything about the external
+                                     * representation of this index
+                                     * set.
                                      */
-    std::set<ContiguousRange, RangeComparison> contiguous_ranges;
+    mutable std::set<ContiguousRange, RangeComparison> contiguous_ranges;
 
                                     /**
                                      * A set of individual indices
                                      * that make up (part of) this
                                      * index set, together with the
                                      * contiguous ranges.
+                                     *
+                                     * The variable is marked
+                                     * "mutable" so that it can be
+                                     * changed by compress(), though
+                                     * this of course doesn't change
+                                     * anything about the external
+                                     * representation of this index
+                                     * set.
                                      */
-    std::set<unsigned int>                     individual_indices;
+    mutable std::set<unsigned int>                     individual_indices;
 
                                     /**
                                      * The overall size of the index
@@ -201,7 +236,15 @@ IndexSet::add_range (const unsigned int begin,
          ExcIndexRange (begin, 0, end));
 
   if (begin != end)
-    contiguous_ranges.insert (ContiguousRange(begin,end));
+    {
+                                      // if it turns out to be a
+                                      // single element then add that
+                                      // separately
+      if (end == begin+1)
+       add_index (begin);
+      else
+       contiguous_ranges.insert (ContiguousRange(begin,end));
+    }
 }
 
 
@@ -243,6 +286,19 @@ IndexSet::is_element (const unsigned int index) const
 
 
 
+inline
+bool
+IndexSet::is_contiguous () const
+{
+  compress ();
+  return ((individual_indices.size() == 0)
+         &&
+         (contiguous_ranges.size() <= 1));
+}
+
+
+
+
 
 DEAL_II_NAMESPACE_CLOSE
 
diff --git a/deal.II/base/source/index_set.cc b/deal.II/base/source/index_set.cc
new file mode 100644 (file)
index 0000000..be02d74
--- /dev/null
@@ -0,0 +1,66 @@
+//---------------------------------------------------------------------------
+//      $Id$
+//    Version: $Name$
+//
+//    Copyright (C) 2005, 2006, 2008, 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.
+//
+//---------------------------------------------------------------------------
+
+#include <base/index_set.h>
+
+DEAL_II_NAMESPACE_OPEN
+
+void
+IndexSet::compress () const
+{
+                                  // first see if any of the
+                                  // individual entries can be merged
+                                  // to form contiguous ranges
+  {
+    std::set<unsigned int>::iterator p = individual_indices.begin();
+    while (p != individual_indices.end())
+      {
+       unsigned int n = 0;
+       std::set<unsigned int>::iterator q = p;
+       ++q;
+       while ((q != individual_indices.end())
+              &&
+              (*q == *p+n+1))
+         {
+           ++q;
+           ++n;
+         }
+
+       if (n>0)
+         {
+           const unsigned int range_end = *q;
+
+                                            // add the new range,
+                                            // delete the elements of
+                                            // the range from the set
+                                            // of individual indices
+           contiguous_ranges.insert (ContiguousRange(*p, *p+n+1));
+           individual_indices.erase (p, q);
+
+                                            // reset p to the element
+                                            // past the last
+           p = individual_indices.lower_bound (range_end);
+         }
+       else
+         ++p;
+      }
+  }
+
+
+                                  // we can roll any of
+                                  // the individual entries into the
+                                  // contiguous ranges
+}
+
+
+DEAL_II_NAMESPACE_CLOSE

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.