dune-grid 3.0-git
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
13namespace 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 >
27 class IteratorWrapper;
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
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
105
108
109 int codimension () const { return codim_; }
110
111 protected:
112 const Grid &grid () const { return *grid_; }
113
114 template< int codim >
115 void resize ( const Value &value );
116
117 template< int codim >
118 void migrateLevel ( int level, const Value &value, Map &data,
119 std::integral_constant< bool, true > );
120
121 template< int codim >
122 void migrateLevel ( int level, const Value &value, Map &data,
123 std::integral_constant< bool, false > );
124
125 static void migrateEntry ( const typename IdSet::IdType &id, const Value &value,
126 Map &oldData, Map &newData );
127
128 const IdSet &idSet () const { return *idSet_; }
129
130 const Grid *grid_;
132 const IdSet *idSet_;
133 Map data_;
134 };
135
136
137
138 // PersistentContainerMap::IteratorWrapper
139 // ---------------------------------------
140
141 template< class G, class IdSet, class Map >
142 template< class value, class iterator >
144 : public iterator
145 {
147
148 public:
149 IteratorWrapper ( const iterator &it ) : it_( it ) {}
150
151 operator ConstWrapper () const { return ConstWrapper( it_ ); }
152
153 value &operator* () { return it_->second; }
154 value *operator-> () { return &(it_->second); }
155
156 bool operator== ( const IteratorWrapper &other ) const { return (it_ == other.it_); }
157 bool operator!= ( const IteratorWrapper &other ) const { return (it_ != other.it_); }
158
159 IteratorWrapper &operator++ () { ++it_; return *this; }
160
161 private:
162 iterator it_;
163 };
164
165
166
167 // PersistentContainerMap::Resize
168 // ------------------------------
169
170 template< class G, class IdSet, class Map >
171 template< int codim >
173 {
175 const Value &value )
176 {
177 if( codim == container.codimension() )
178 container.template resize< codim >( value );
179 }
180 };
181
182
183
184 // Implementation of PersistentContainerMap
185 // ----------------------------------------
186
187 template< class G, class IdSet, class Map >
190 {
191 return ConstIterator( data_.begin() );
192 }
193
194 template< class G, class IdSet, class Map >
197 {
198 return Iterator( data_.begin() );
199 }
200
201
202 template< class G, class IdSet, class Map >
205 {
206 return ConstIterator( data_.end() );
207 }
208
209 template< class G, class IdSet, class Map >
212 {
213 return Iterator( data_.end() );
214 }
215
216
217 template< class G, class IdSet, class Map >
218 template< int codim >
220 {
221 std::integral_constant< bool, Capabilities::hasEntity< Grid, codim >::v > hasEntity;
222 assert( codim == codimension() );
223
224 // create empty map and swap it with current map (no need to copy twice)
225 Map data;
226 std::swap( data, data_ );
227
228 // copy all data from old map into new one (adding new entries, if necessary)
229 const int maxLevel = grid().maxLevel();
230 for ( int level = 0; level <= maxLevel; ++level )
231 migrateLevel< codim >( level, value, data, hasEntity );
232 }
233
234
235 template< class G, class IdSet, class Map >
236 template< int codim >
238 ::migrateLevel ( int level, const Value &value, Map &data,
239 std::integral_constant< bool, true > )
240 {
241 typedef typename Grid::LevelGridView LevelView;
242 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
243
244 const LevelView levelView = grid().levelGridView( level );
245 const LevelIterator end = levelView.template end< codim >();
246 for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
247 migrateEntry( idSet().id( *it ), value, data, data_ );
248 }
249
250
251 template< class G, class IdSet, class Map >
252 template< int codim >
254 ::migrateLevel ( int level, const Value &value, Map &data,
255 std::integral_constant< bool, false > )
256 {
257 typedef typename Grid::LevelGridView LevelView;
258 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
259
260 const LevelView levelView = grid().levelGridView( level );
261 const LevelIterator end = levelView.template end< 0 >();
262 for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
263 {
264 const typename LevelIterator::Entity &entity = *it;
265 const int subEntities = entity.subEntities( codim );
266 for( int i = 0; i < subEntities; ++i )
267 migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
268 }
269 }
270
271
272 template< class G, class IdSet, class Map >
274 ::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
275 Map &oldData, Map &newData )
276 {
277 // insert entry for id
278 const std::pair< typename Map::iterator, bool > inserted
279 = newData.insert( std::make_pair( id, value ) );
280
281 // if entry did not exist previously, copy data
282 if( inserted.second )
283 {
284 const typename Map::iterator pos = oldData.find( id );
285 if( pos != oldData.end() )
286 {
287 inserted.first->second = pos->second;
288 oldData.erase( pos );
289 }
290 }
291 }
292
293} // namespace Dune
294
295#endif // #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
void swap(Dune::PersistentContainer< G, T > &a, Dune::PersistentContainer< G, T > &b)
Definition utility/persistentcontainer.hh:81
Include standard header files.
Definition agrid.hh:60
Wrapper class for entities.
Definition common/entity.hh:62
@ codimension
Know your own codimension.
Definition common/entity.hh:104
Id Set Interface.
Definition indexidset.hh:397
IdTypeImp IdType
Type used to represent an id.
Definition indexidset.hh:400
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition common/grid.hh:406
map-based implementation of the PersistentContainer
Definition persistentcontainermap.hh:22
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, false >)
Definition persistentcontainermap.hh:254
void swap(This &other)
Definition persistentcontainermap.hh:95
void fill(const Value &value)
Definition persistentcontainermap.hh:93
G Grid
Definition persistentcontainermap.hh:33
void resize(const Value &value)
Definition persistentcontainermap.hh:219
IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator
Definition persistentcontainermap.hh:38
int codim_
Definition persistentcontainermap.hh:131
const Grid * grid_
Definition persistentcontainermap.hh:130
const Value & operator[](const Entity &entity) const
Definition persistentcontainermap.hh:51
int codimension() const
Definition persistentcontainermap.hh:109
void resize(const Value &value=Value())
Definition persistentcontainermap.hh:86
const IdSet & idSet() const
Definition persistentcontainermap.hh:128
ConstIterator begin() const
Definition persistentcontainermap.hh:189
static void migrateEntry(const typename IdSet::IdType &id, const Value &value, Map &oldData, Map &newData)
Definition persistentcontainermap.hh:274
Map::mapped_type Value
Definition persistentcontainermap.hh:35
Iterator end()
Definition persistentcontainermap.hh:211
const Grid & grid() const
Definition persistentcontainermap.hh:112
const Value & operator()(const Entity &entity, int subEntity) const
Definition persistentcontainermap.hh:69
PersistentContainerMap(const Grid &grid, int codim, const IdSet &idSet, const Value &value)
Definition persistentcontainermap.hh:41
Iterator begin()
Definition persistentcontainermap.hh:196
Map::size_type Size
Definition persistentcontainermap.hh:36
ConstIterator end() const
Definition persistentcontainermap.hh:204
Map data_
Definition persistentcontainermap.hh:133
Size size() const
Definition persistentcontainermap.hh:84
IteratorWrapper< Value, typename Map::iterator > Iterator
Definition persistentcontainermap.hh:39
const IdSet * idSet_
Definition persistentcontainermap.hh:132
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, true >)
Definition persistentcontainermap.hh:238
void shrinkToFit()
Definition persistentcontainermap.hh:91
Definition persistentcontainermap.hh:145
IteratorWrapper(const iterator &it)
Definition persistentcontainermap.hh:149
Definition persistentcontainermap.hh:173
static void apply(PersistentContainerMap< G, IdSet, Map > &container, const Value &value)
Definition persistentcontainermap.hh:174
A set of traits classes to store static information about grid implementation.