00001
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;
00049 }
00050
00056 template<class EntityType>
00057 int map (const EntityType& e) const
00058 {
00059 IdType id = ids.id(e);
00060 typename std::map<IdType,int>::iterator it = index.find(id);
00061 if (it!=index.end()) return it->second;
00062 index[id] = n++;
00063 return n-1;
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);
00077 typename std::map<IdType,int>::iterator it = index.find(id);
00078 if (it!=index.end()) return it->second;
00079 index[id] = n++;
00080 return n-1;
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);
00106 typename std::map<IdType,int>::iterator it = index.find(id);
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>
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);
00127 typename std::map<IdType,int>::iterator it = index.find(id);
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 {
00141 }
00142
00143
00144 void clear ()
00145 {
00146 index.clear();
00147 n = 0;
00148 }
00149
00150 private:
00151 mutable int n;
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
00172
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
00191
00192
00193 LocalUniversalMapper (const G& grid)
00194 : UniversalMapper<G,typename G::Traits::LocalIdSet>(grid,grid.localIdSet())
00195 {}
00196 };
00197
00198
00200 }
00201 #endif