dune-grid  2.2.1
universalmapper.hh
Go to the documentation of this file.
1 // $Id: universalmapper.hh 7390 2011-02-08 11:26:32Z christi $
2 
3 #ifndef DUNE_UNIVERSALMAPPER_HH
4 #define DUNE_UNIVERSALMAPPER_HH
5 
6 #include <iostream>
7 #include <map>
8 #include "mapper.hh"
9 
16 namespace Dune
17 {
36  template <typename G, typename IDS>
38  public Mapper<G,UniversalMapper<G,IDS> >
39  {
40  typedef typename IDS::IdType IdType;
41  public:
42 
47 
54  UniversalMapper (const G& grid, const IDS& idset)
55  : g(grid), ids(idset), index()
56  {
57  n=0; // zero data elements
58  }
59 
67  template<class EntityType>
68  int map (const EntityType& e) const
69  {
70  IdType id = ids.id(e); // get id
71  typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
72  if (it!=index.end()) return it->second; // return index if found
73  index[id] = n++; // put next index in map
74  return n-1; // and return it
75  }
76 
77 
87  int map (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc) const
88  {
89  IdType id = ids.subId(e,i,cc); // get id
90  typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
91  if (it!=index.end()) return it->second; // return index if found
92  index[id] = n++; // put next index in map
93  return n-1; // and return it
94  }
95 
104  int size () const
105  {
106  return n;
107  }
108 
117  template<class EntityType>
118  bool contains (const EntityType& e, int& result) const
119  {
120  IdType id = ids.id(e); // get id
121  typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
122  if (it!=index.end())
123  {
124  result = it->second;
125  return true;
126  }
127  else
128  return false;
129  }
130 
139  bool contains (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc, int& result) const
140  {
141  IdType id = ids.subId(e,i,cc); // get id
142  typename std::map<IdType,int>::iterator it = index.find(id);// look up in map
143  if (it!=index.end())
144  {
145  result = it->second;
146  return true;
147  }
148  else
149  return false;
150  }
151 
154  void update ()
155  { // nothing to do here
156  }
157 
158  // clear the mapper
159  void clear ()
160  {
161  index.clear();
162  n = 0;
163  }
164 
165  private:
166  mutable int n; // number of data elements required
167  const G& g;
168  const IDS& ids;
169  mutable std::map<IdType,int> index;
170  };
171 
172 
173 
174 
182  template <typename G>
183  class GlobalUniversalMapper : public UniversalMapper<G,typename G::Traits::GlobalIdSet>
184  {
185  public:
186  /* @brief The constructor
187  @param grid A reference to a grid.
188  */
189  GlobalUniversalMapper (const G& grid)
190  : UniversalMapper<G,typename G::Traits::GlobalIdSet>(grid,grid.globalIdSet())
191  {}
192  };
193 
201  template <typename G>
202  class LocalUniversalMapper : public UniversalMapper<G,typename G::Traits::LocalIdSet>
203  {
204  public:
205  /* @brief The constructor
206  @param grid A reference to a grid.
207  */
208  LocalUniversalMapper (const G& grid)
209  : UniversalMapper<G,typename G::Traits::LocalIdSet>(grid,grid.localIdSet())
210  {}
211  };
212 
213 
215 }
216 #endif