dune-grid  2.3.1
universalmapper.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // $Id$
4 
5 #ifndef DUNE_UNIVERSALMAPPER_HH
6 #define DUNE_UNIVERSALMAPPER_HH
7 
8 #include <iostream>
9 #include <map>
10 #include "mapper.hh"
11 
18 namespace Dune
19 {
38  template <typename G, typename IDS>
40  public Mapper<G,UniversalMapper<G,IDS> >
41  {
42  typedef typename IDS::IdType IdType;
43  public:
44 
49 
56  UniversalMapper (const G& grid, const IDS& idset)
57  : g(grid), ids(idset), index()
58  {
59  n=0; // zero data elements
60  }
61 
69  template<class EntityType>
70  int map (const EntityType& e) const
71  {
72  IdType id = ids.id(e); // get id
73  typename std::map<IdType,int>::iterator it = index.find(id); // look up in map
74  if (it!=index.end()) return it->second; // return index if found
75  index[id] = n++; // put next index in map
76  return n-1; // and return it
77  }
78 
79 
89  int map (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc) const
90  {
91  IdType id = ids.subId(e,i,cc); // get id
92  typename std::map<IdType,int>::iterator it = index.find(id); // look up in map
93  if (it!=index.end()) return it->second; // return index if found
94  index[id] = n++; // put next index in map
95  return n-1; // and return it
96  }
97 
106  int size () const
107  {
108  return n;
109  }
110 
119  template<class EntityType>
120  bool contains (const EntityType& e, int& result) const
121  {
122  IdType id = ids.id(e); // get id
123  typename std::map<IdType,int>::iterator it = index.find(id); // look up in map
124  if (it!=index.end())
125  {
126  result = it->second;
127  return true;
128  }
129  else
130  return false;
131  }
132 
141  bool contains (const typename G::Traits::template Codim<0>::Entity& e, int i, int cc, int& result) const
142  {
143  IdType id = ids.subId(e,i,cc); // get id
144  typename std::map<IdType,int>::iterator it = index.find(id); // look up in map
145  if (it!=index.end())
146  {
147  result = it->second;
148  return true;
149  }
150  else
151  return false;
152  }
153 
156  void update ()
157  { // nothing to do here
158  }
159 
160  // clear the mapper
161  void clear ()
162  {
163  index.clear();
164  n = 0;
165  }
166 
167  private:
168  mutable int n; // number of data elements required
169  const G& g;
170  const IDS& ids;
171  mutable std::map<IdType,int> index;
172  };
173 
174 
175 
176 
184  template <typename G>
185  class GlobalUniversalMapper : public UniversalMapper<G,typename G::Traits::GlobalIdSet>
186  {
187  public:
188  /* @brief The constructor
189  @param grid A reference to a grid.
190  */
191  GlobalUniversalMapper (const G& grid)
192  : UniversalMapper<G,typename G::Traits::GlobalIdSet>(grid,grid.globalIdSet())
193  {}
194  };
195 
203  template <typename G>
204  class LocalUniversalMapper : public UniversalMapper<G,typename G::Traits::LocalIdSet>
205  {
206  public:
207  /* @brief The constructor
208  @param grid A reference to a grid.
209  */
210  LocalUniversalMapper (const G& grid)
211  : UniversalMapper<G,typename G::Traits::LocalIdSet>(grid,grid.localIdSet())
212  {}
213  };
214 
215 
217 }
218 #endif
Universal mapper based on global ids.
Definition: universalmapper.hh:185
void clear()
Definition: universalmapper.hh:161
void update()
Recalculates map after mesh adaptation.
Definition: universalmapper.hh:156
GlobalUniversalMapper(const G &grid)
Definition: universalmapper.hh:191
UniversalMapper(const G &grid, const IDS &idset)
Construct mapper from grid and one of its id sets.
Definition: universalmapper.hh:56
int map(const EntityType &e) const
Map entity to array index.
Definition: universalmapper.hh:70
int size() const
Return total number of entities in the entity set managed by the mapper.
Definition: universalmapper.hh:106
Provides classes with basic mappers which are used to attach data to a grid.
Mapper interface.
Definition: mapper.hh:110
LocalUniversalMapper(const G &grid)
Definition: universalmapper.hh:210
bool contains(const typename G::Traits::template Codim< 0 >::Entity &e, int i, int cc, int &result) const
Returns true if the entity is contained in the index set.
Definition: universalmapper.hh:141
Implements a mapper for an arbitrary subset of entities.
Definition: universalmapper.hh:39
int map(const typename G::Traits::template Codim< 0 >::Entity &e, int i, int cc) const
Map subentity of codim 0 entity to array index.
Definition: universalmapper.hh:89
Universal mapper based on local ids.
Definition: universalmapper.hh:204
bool contains(const EntityType &e, int &result) const
Returns true if the entity is contained in the index set.
Definition: universalmapper.hh:120