dune-grid  2.3.1
persistentcontainermap.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 #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
4 #define DUNE_PERSISTENTCONTAINERMAP_HH
5 
6 #include <algorithm>
7 #include <cassert>
8 
9 #include <dune/common/typetraits.hh>
10 #include <dune/common/forloop.hh>
12 
13 namespace Dune
14 {
15 
16  // PersistentContainerMap
17  // ----------------------
18 
20  template< class G, class IdSet, class Map >
22  {
24 
25  protected:
26  template< class reference, class iterator >
28 
29  template< int codim >
30  struct Resize;
31 
32  public:
33  typedef G Grid;
34 
35  typedef typename Map::mapped_type Value;
36  typedef typename Map::size_type Size;
37 
38  typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
39  typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
40 
41  PersistentContainerMap ( const Grid &grid, int codim, const IdSet &idSet, const Value &value )
42  : grid_( &grid ),
43  codim_( codim ),
44  idSet_( &idSet ),
45  data_()
46  {
47  resize( value );
48  }
49 
50  template< class Entity >
51  const Value &operator[] ( const Entity &entity ) const
52  {
53  assert( Entity::codimension == codimension() );
54  typename Map::const_iterator pos = data_.find( idSet().id( entity ) );
55  assert( pos != data_.end() );
56  return pos->second;
57  }
58 
59  template< class Entity >
60  Value &operator[] ( const Entity &entity )
61  {
62  assert( Entity::codimension == codimension() );
63  typename Map::iterator pos = data_.find( idSet().id( entity ) );
64  assert( pos != data_.end() );
65  return pos->second;
66  }
67 
68  template< class Entity >
69  const Value &operator() ( const Entity &entity, int subEntity ) const
70  {
71  typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
72  assert( pos != data_.end() );
73  return pos->second;
74  }
75 
76  template< class Entity >
77  Value &operator() ( const Entity &entity, int subEntity )
78  {
79  typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
80  assert( pos != data_.end() );
81  return pos->second;
82  }
83 
84  Size size () const { return data_.size(); }
85 
86  void resize ( const Value &value = Value() )
87  {
88  return ForLoop< Resize, 0, Grid::dimension >::apply( *this, value );
89  }
90 
91  void shrinkToFit () {}
92 
93  void fill ( const Value &value ) { std::fill( begin(), end(), value ); }
94 
95  void swap ( This &other )
96  {
97  std::swap( grid_, other.grid_ );
98  std::swap( codim_, other.codim_ );
99  std::swap( idSet_, other.idSet_ );
100  std::swap( data_, other.data_ );
101  }
102 
103  ConstIterator begin () const;
104  Iterator begin ();
105 
106  ConstIterator end () const;
107  Iterator end ();
108 
109  int codimension () const { return codim_; }
110 
111 
112  // deprecated stuff, will be removed after Dune 2.3
113 
114  typedef Grid GridType DUNE_DEPRECATED_MSG("Use Grid instead.");
115  typedef Value Data DUNE_DEPRECATED_MSG("Use Value instead.");
116 
117  void reserve () DUNE_DEPRECATED_MSG("Use resize() instead.")
118  { return resize(); }
119 
120  void clear () DUNE_DEPRECATED_MSG("Use resize() instead.")
121  {
122  resize( Value() );
123  shrinkToFit();
124  fill( Value() );
125  }
126 
127  void update () DUNE_DEPRECATED_MSG("Use resize() instead.")
128  {
129  resize( Value() );
130  shrinkToFit();
131  }
132 
133  protected:
134  const Grid &grid () const { return *grid_; }
135 
136  template< int codim >
137  void resize ( const Value &value );
138 
139  template< int codim >
140  void migrateLevel ( int level, const Value &value, Map &data,
141  integral_constant< bool, true > );
142 
143  template< int codim >
144  void migrateLevel ( int level, const Value &value, Map &data,
145  integral_constant< bool, false > );
146 
147  static void migrateEntry ( const typename IdSet::IdType &id, const Value &value,
148  Map &oldData, Map &newData );
149 
150  protected:
151  const IdSet &idSet () const { return *idSet_; }
152 
153  const Grid *grid_;
154  int codim_;
155  const IdSet *idSet_;
156  Map data_;
157  };
158 
159 
160 
161  // PersistentContainerMap::IteratorWrapper
162  // ---------------------------------------
163 
164  template< class G, class IdSet, class Map >
165  template< class value, class iterator >
166  class PersistentContainerMap< G, IdSet, Map >::IteratorWrapper
167  : public iterator
168  {
169  typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
170 
171  public:
172  IteratorWrapper ( const iterator &it ) : it_( it ) {}
173 
174  operator ConstWrapper () const { return ConstWrapper( it_ ); }
175 
176  value &operator* () { return it_->second; }
177  value *operator-> () { return &(it_->second); }
178 
179  bool operator== ( const IteratorWrapper &other ) const { return (it_ == other.it_); }
180  bool operator!= ( const IteratorWrapper &other ) const { return (it_ != other.it_); }
181 
182  IteratorWrapper &operator++ () { ++it_; return *this; }
183 
184  private:
185  iterator it_;
186  };
187 
188 
189 
190  // PersistentContainerMap::Resize
191  // ------------------------------
192 
193  template< class G, class IdSet, class Map >
194  template< int codim >
195  struct PersistentContainerMap< G, IdSet, Map >::Resize
196  {
198  const Value &value )
199  {
200  if( codim == container.codimension() )
201  container.template resize< codim >( value );
202  }
203  };
204 
205 
206 
207  // Implementation of PersistentContainerMap
208  // ----------------------------------------
209 
210  template< class G, class IdSet, class Map >
213  {
214  return ConstIterator( data_.begin() );
215  }
216 
217  template< class G, class IdSet, class Map >
220  {
221  return Iterator( data_.begin() );
222  }
223 
224 
225  template< class G, class IdSet, class Map >
228  {
229  return ConstIterator( data_.end() );
230  }
231 
232  template< class G, class IdSet, class Map >
235  {
236  return Iterator( data_.end() );
237  }
238 
239 
240  template< class G, class IdSet, class Map >
241  template< int codim >
242  inline void PersistentContainerMap< G, IdSet, Map >::resize ( const Value &value )
243  {
244  integral_constant< bool, Capabilities::hasEntity< Grid, codim >::v > hasEntity;
245  assert( codim == codimension() );
246 
247  // create empty map and swap it with current map (no need to copy twice)
248  Map data;
249  std::swap( data, data_ );
250 
251  // copy all data from old map into new one (adding new entries, if necessary)
252  const int maxLevel = grid().maxLevel();
253  for ( int level = 0; level <= maxLevel; ++level )
254  migrateLevel< codim >( level, value, data, hasEntity );
255  }
256 
257 
258  template< class G, class IdSet, class Map >
259  template< int codim >
261  ::migrateLevel ( int level, const Value &value, Map &data,
262  integral_constant< bool, true > )
263  {
264  typedef typename Grid::LevelGridView LevelView;
265  typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
266 
267  const LevelView levelView = grid().levelGridView( level );
268  const LevelIterator end = levelView.template end< codim >();
269  for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
270  migrateEntry( idSet().id( *it ), value, data, data_ );
271  }
272 
273 
274  template< class G, class IdSet, class Map >
275  template< int codim >
277  ::migrateLevel ( int level, const Value &value, Map &data,
278  integral_constant< bool, false > )
279  {
280  typedef typename Grid::LevelGridView LevelView;
281  typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
282 
283  const LevelView levelView = grid().levelGridView( level );
284  const LevelIterator end = levelView.template end< 0 >();
285  for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
286  {
287  const typename LevelIterator::Entity &entity = *it;
288  for( int i = 0; i < entity.template count< codim >(); ++i )
289  migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
290  }
291  }
292 
293 
294  template< class G, class IdSet, class Map >
296  ::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
297  Map &oldData, Map &newData )
298  {
299  // insert entry for id
300  const std::pair< typename Map::iterator, bool > inserted
301  = newData.insert( std::make_pair( id, value ) );
302 
303  // if entry did not exist previously, copy data
304  if( inserted.second )
305  {
306  const typename Map::iterator pos = oldData.find( id );
307  if( pos != oldData.end() )
308  {
309  inserted.first->second = pos->second;
310  oldData.erase( pos );
311  }
312  }
313  }
314 
315 } // namespace Dune
316 
317 #endif // #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
Know your own codimension.
Definition: common/entity.hh:99
const Value & operator[](const Entity &entity) const
Definition: persistentcontainermap.hh:51
static void apply(PersistentContainerMap< G, IdSet, Map > &container, const Value &value)
Definition: persistentcontainermap.hh:197
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:21
const IdSet * idSet_
Definition: persistentcontainermap.hh:155
Wrapper class for entities.
Definition: common/entity.hh:56
IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator
Definition: persistentcontainermap.hh:38
void reserve()
Definition: persistentcontainermap.hh:117
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:406
void swap(This &other)
Definition: persistentcontainermap.hh:95
Map data_
Definition: persistentcontainermap.hh:156
const Grid & grid() const
Definition: persistentcontainermap.hh:134
PersistentContainerMap(const Grid &grid, int codim, const IdSet &idSet, const Value &value)
Definition: persistentcontainermap.hh:41
int codimension() const
Definition: persistentcontainermap.hh:109
void migrateLevel(int level, const Value &value, Map &data, integral_constant< bool, true >)
Definition: persistentcontainermap.hh:261
void resize(const Value &value=Value())
Definition: persistentcontainermap.hh:86
const Value & operator()(const Entity &entity, int subEntity) const
Definition: persistentcontainermap.hh:69
const Grid * grid_
Definition: persistentcontainermap.hh:153
ConstIterator end() const
Definition: persistentcontainermap.hh:227
Partition< All_Partition >::LevelGridView LevelGridView
View types for All_Partition.
Definition: common/grid.hh:426
IteratorWrapper< Value, typename Map::iterator > Iterator
Definition: persistentcontainermap.hh:39
A set of traits classes to store static information about grid implementation.
Size size() const
Definition: persistentcontainermap.hh:84
ConstIterator begin() const
Definition: persistentcontainermap.hh:212
void shrinkToFit()
Definition: persistentcontainermap.hh:91
const IdSet & idSet() const
Definition: persistentcontainermap.hh:151
G Grid
Definition: persistentcontainermap.hh:30
static void migrateEntry(const typename IdSet::IdType &id, const Value &value, Map &oldData, Map &newData)
Definition: persistentcontainermap.hh:296
IteratorWrapper(const iterator &it)
Definition: persistentcontainermap.hh:172
int codim_
Definition: persistentcontainermap.hh:154
void update()
Definition: persistentcontainermap.hh:127
void fill(const Value &value)
Definition: persistentcontainermap.hh:93
IteratorImp::Entity Entity
The Entity that this EntityPointer can point to.
Definition: common/entitypointer.hh:123
Map::size_type Size
Definition: persistentcontainermap.hh:36
Id Set Interface.
Definition: common/grid.hh:360
Map::mapped_type Value
Definition: persistentcontainermap.hh:35
Enables iteration over all entities of a given codimension and level of a grid. See also the document...
Definition: common/leveliterator.hh:29
void clear()
Definition: persistentcontainermap.hh:120
Definition: persistentcontainermap.hh:27