universalmapper.hh

Go to the documentation of this file.
00001 // $Id$
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 {
00036   template <typename G, typename IDS>
00037   class UniversalMapper :
00038         public Mapper<G,UniversalMapper<G,IDS> >
00039   {
00040         typedef typename IDS::IdType IdType;
00041   public:
00042 
00045     using Mapper< G, UniversalMapper >::map;
00046     using Mapper< G, UniversalMapper >::contains;
00047 
00054         UniversalMapper (const G& grid, const IDS& idset)
00055           : g(grid), ids(idset), index()
00056         {
00057           n=0; // zero data elements
00058         }
00059 
00067         template<class EntityType>
00068         int map (const EntityType& e) const
00069         {
00070           IdType id = ids.id(e);                             // get id
00071           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00072           if (it!=index.end()) return it->second;            // return index if found
00073           index[id] = n++;                                   // put next index in map
00074           return n-1;                                        // and return it
00075         }
00076 
00077   
00087     int map (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc) const
00088         {
00089       IdType id = ids.subId(e,i,cc);           // get id
00090           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00091           if (it!=index.end()) return it->second;            // return index if found
00092           index[id] = n++;                                   // put next index in map
00093           return n-1;                                        // and return it
00094         }
00095 
00104         int size () const
00105         {
00106           return n;
00107         }
00108  
00117         template<class EntityType>
00118         bool contains (const EntityType& e, int& result) const
00119         {
00120           IdType id = ids.id(e);                             // get id
00121           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00122           if (it!=index.end())
00123                 {
00124                   result = it->second;
00125                   return true;
00126                 }
00127           else
00128                 return false;
00129         }
00130 
00139     bool contains (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc, int& result) const
00140         {
00141       IdType id = ids.subId(e,i,cc);           // get id
00142           typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
00143           if (it!=index.end())
00144                 {
00145                   result = it->second;
00146                   return true;
00147                 }
00148           else
00149                 return false;
00150         }
00151  
00154         void update ()
00155         { // nothing to do here
00156         }
00157 
00158         // clear the mapper
00159         void clear ()
00160         {
00161           index.clear();
00162           n = 0;
00163         }
00164   
00165   private:
00166         mutable int n; // number of data elements required
00167         const G& g;
00168         const IDS& ids;
00169         mutable std::map<IdType,int> index;
00170   };
00171 
00172 
00173 
00174 
00182   template <typename G>
00183   class GlobalUniversalMapper : public UniversalMapper<G,typename G::Traits::GlobalIdSet> 
00184   {
00185   public:
00186         /* @brief The constructor
00187            @param grid A reference to a grid.
00188          */
00189         GlobalUniversalMapper (const G& grid) 
00190           : UniversalMapper<G,typename G::Traits::GlobalIdSet>(grid,grid.globalIdSet())
00191         {}
00192   };
00193 
00201   template <typename G>
00202   class LocalUniversalMapper : public UniversalMapper<G,typename G::Traits::LocalIdSet> 
00203   {
00204   public:
00205         /* @brief The constructor
00206            @param grid A reference to a grid.
00207          */
00208         LocalUniversalMapper (const G& grid) 
00209           : UniversalMapper<G,typename G::Traits::LocalIdSet>(grid,grid.localIdSet())
00210         {}
00211   };
00212 
00213 
00215 }
00216 #endif

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].