universalmapper.hh

Go to the documentation of this file.
00001 // $Id: universalmapper.hh 3146 2006-10-10 10:14:37Z oliver $
00002 
00003 #ifndef DUNE_UNIVERSALMAPPER_HH
00004 #define DUNE_UNIVERSALMAPPER_HH
00005 
00006 #include <iostream>
00007 #include <map>
00008 #include "mapper.hh"
00009 
00016 namespace Dune
00017 {
00034   template <typename G, typename IDS>
00035   class UniversalMapper : Mapper<G,UniversalMapper<G,IDS> > {
00036         typedef typename IDS::IdType IdType;
00037   public:
00038 
00045         UniversalMapper (const G& grid, const IDS& idset)
00046           : g(grid), ids(idset), index()
00047         {
00048           n=0; // zero data elements
00049         }
00050 
00056         template<class EntityType>
00057         int map (const EntityType& e) const
00058         {
00059           IdType id = ids.id(e);                             // get id
00060           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00061           if (it!=index.end()) return it->second;            // return index if found
00062           index[id] = n++;                                   // put next index in map
00063           return n-1;                                        // and return it
00064         }
00065 
00066   
00073         template<int cc>
00074         int map (const typename G::Traits::template Codim<0>::Entity& e, int i) const
00075         {
00076           IdType id = ids.template subId<cc>(e,i);           // get id
00077           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00078           if (it!=index.end()) return it->second;            // return index if found
00079           index[id] = n++;                                   // put next index in map
00080           return n-1;                                        // and return it
00081         }
00082 
00091         int size () const
00092         {
00093           return n;
00094         }
00095  
00102         template<class EntityType>
00103         bool contains (const EntityType& e, int& result) const
00104         {
00105           IdType id = ids.id(e);                             // get id
00106           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00107           if (it!=index.end())
00108                 {
00109                   result = it->second;
00110                   return true;
00111                 }
00112           else
00113                 return false;
00114         }
00115 
00123         template<int cc> // this is now the subentity's codim
00124         bool contains (const typename G::Traits::template Codim<0>::Entity& e, int i, int& result) const
00125         {
00126           IdType id = ids.template subId<cc>(e,i);           // get id
00127           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00128           if (it!=index.end())
00129                 {
00130                   result = it->second;
00131                   return true;
00132                 }
00133           else
00134                 return false;
00135         }
00136  
00139         void update ()
00140         { // nothing to do here
00141         }
00142 
00143         // clear the mapper
00144         void clear ()
00145         {
00146           index.clear();
00147           n = 0;
00148         }
00149   
00150   private:
00151         mutable int n; // number of data elements required
00152         const G& g;
00153         const IDS& ids;
00154         mutable std::map<IdType,int> index;
00155   };
00156 
00157 
00158 
00159 
00167   template <typename G>
00168   class GlobalUniversalMapper : public UniversalMapper<G,typename G::Traits::GlobalIdSet> 
00169   {
00170   public:
00171         /* @brief The constructor
00172            @param grid A reference to a grid.
00173          */
00174         GlobalUniversalMapper (const G& grid) 
00175           : UniversalMapper<G,typename G::Traits::GlobalIdSet>(grid,grid.globalIdSet())
00176         {}
00177   };
00178 
00186   template <typename G>
00187   class LocalUniversalMapper : public UniversalMapper<G,typename G::Traits::LocalIdSet> 
00188   {
00189   public:
00190         /* @brief The constructor
00191            @param grid A reference to a grid.
00192          */
00193         LocalUniversalMapper (const G& grid) 
00194           : UniversalMapper<G,typename G::Traits::LocalIdSet>(grid,grid.localIdSet())
00195         {}
00196   };
00197 
00198 
00200 }
00201 #endif

Generated on Sun Nov 15 22:28:44 2009 for dune-grid by  doxygen 1.5.6