SundanceCellIterator.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCE_CELLITERATOR_H
00032 #define SUNDANCE_CELLITERATOR_H
00033 
00034 
00035 #include "SundanceDefs.hpp"
00036 #include "SundanceSet.hpp"
00037 #include "SundanceMap.hpp"
00038 #include "SundanceCellType.hpp"
00039 #include "SundanceCellReordererImplemBase.hpp"
00040 #include "Teuchos_RefCountPtr.hpp"
00041 #include "SundanceMesh.hpp"
00042 
00043 namespace Sundance
00044 {
00045 using namespace Teuchos;
00046 
00047 /**
00048  * CellIterator is an iterator for walking through cell sets.
00049  * It satisfies the requirements for an input iterator in STL.
00050  *
00051  * This class design violates the usual rules of good OO style:
00052  * it has polymorphic behavior packed into a single class. The
00053  * justification for this decision is to avoid the expense
00054  * of the clone() operations that would be required by the
00055  * copy ctor for iterators were 
00056  * a polymorphic class heirarchy used. 
00057  *
00058  * Two cell set types exist: explicit, where the member cells LIDs
00059  * are enumerated in a physical Set<int> object, and implicit,
00060  * where no physical Set is made, rather, the sequence of cell LIDs
00061  * is obtained through some scheme of walking the mesh. 
00062  *
00063  * The only cell sets that can represented implicitly are
00064  * the set of all cells of a given dimension.
00065  * 
00066  * \see CellSet, CellFilter, CellIteratorPos
00067  */
00068 class CellIterator : public std::iterator<std::input_iterator_tag, int>
00069 {
00070 public:
00071 
00072       
00073   /** 
00074    * CellIteratorPos is used to specify whether a new CellIterator
00075    * is positioned at the beginning or end of a set.
00076    */
00077   enum CellIteratorPos {Begin, End};
00078 
00079   /** Empty ctor */
00080   CellIterator();
00081 
00082   /** Copy ctor */
00083   CellIterator(const CellIterator& other);
00084 
00085   /** Construct an implicit iterator for walking all cells of a given
00086    * dimension on the given mesh. */
00087   CellIterator(const Mesh& mesh, int cellDim, CellIteratorPos pos);
00088 
00089   /** Construct an explicit iterator for walking an explicitly
00090    * enumerated set of cells. */
00091   CellIterator(const Set<int>* cells, CellIteratorPos pos);
00092 
00093   /** */
00094   CellIterator& operator=(const CellIterator& other);
00095       
00096   /** Dereferencing operator */
00097   const int& operator*() const 
00098     {
00099       if (isImplicit_) return currentLID_;
00100       else return *iter_;
00101     }
00102       
00103   /** Postfix increment: advances iterator and returns previous value  */
00104   CellIterator operator++(int) 
00105     {
00106       CellIterator old = *this;
00107       advance();
00108       return old;
00109     }
00110       
00111 
00112   /** Prefix increment: advances iterator, returning new value */
00113   CellIterator& operator++()
00114     {
00115       advance();
00116       return *this;
00117     }
00118 
00119   /** */
00120   bool operator==(const CellIterator& other) const 
00121     {
00122       if (isImplicit_)
00123       {
00124         return currentLID_ == other.currentLID_;
00125       }
00126       else
00127       {
00128         return iter_ == other.iter_;
00129       }
00130     }
00131 
00132   /** */
00133   bool operator!=(const CellIterator& other) const 
00134     {
00135       return !(*this == other);
00136     }
00137 
00138       
00139 private:
00140 
00141   /** Advance the iterator */
00142   void advance()
00143     {
00144       if (isImplicit_) 
00145       {
00146         if (reorderer_ != 0) 
00147         {
00148           currentLID_ = reorderer_->advance(currentLID_);
00149         }
00150         else currentLID_++;
00151       }
00152       else iter_++;
00153     }
00154       
00155   /** Flag indicating whether this iterator is implicit */
00156   bool isImplicit_;
00157       
00158   /** The LID to which this iterator is currently pointing.
00159    * Used only for implicit iterators. */
00160   int currentLID_;
00161 
00162   /** Unmanaged pointer to the reorderer used for walking 
00163    * implicit cell sets. Used only for implicit iterators. */
00164   const CellReordererImplemBase* reorderer_; 
00165 
00166   /** iterator for enumerated cells.
00167    * Used only for explicit iterators. */
00168   Set<int>::const_iterator iter_;
00169 
00170   /** */
00171   static Set<int> dummy() {static Set<int> rtn; return rtn;}
00172 };
00173 }
00174 
00175 
00176 #endif

Site Contact