dune-grid 2.8.0
Loading...
Searching...
No Matches
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#include <type_traits>
9#include <utility>
10
11#include <dune/common/hybridutilities.hh>
12#include <dune/common/typetraits.hh>
14
15namespace Dune
16{
17
18 // PersistentContainerMap
19 // ----------------------
20
22 template< class G, class IdSet, class Map >
24 {
26
27 protected:
28 template< class reference, class iterator >
29 class IteratorWrapper;
30
31 public:
32 typedef G Grid;
33
34 typedef typename Map::mapped_type Value;
35 typedef typename Map::size_type Size;
36
39
40 PersistentContainerMap ( const Grid &grid, int codim, const IdSet &idSet, const Value &value )
41 : grid_( &grid ),
42 codim_( codim ),
43 idSet_( &idSet ),
44 data_()
45 {
46 resize( value );
47 }
48
49 template< class Entity >
50 const Value &operator[] ( const Entity &entity ) const
51 {
52 assert( Entity::codimension == codimension() );
53 typename Map::const_iterator pos = data_.find( idSet().id( entity ) );
54 assert( pos != data_.end() );
55 return pos->second;
56 }
57
58 template< class Entity >
59 Value &operator[] ( const Entity &entity )
60 {
61 assert( Entity::codimension == codimension() );
62 typename Map::iterator pos = data_.find( idSet().id( entity ) );
63 assert( pos != data_.end() );
64 return pos->second;
65 }
66
67 template< class Entity >
68 const Value &operator() ( const Entity &entity, int subEntity ) const
69 {
70 typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
71 assert( pos != data_.end() );
72 return pos->second;
73 }
74
75 template< class Entity >
76 Value &operator() ( const Entity &entity, int subEntity )
77 {
78 typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
79 assert( pos != data_.end() );
80 return pos->second;
81 }
82
83 Size size () const { return data_.size(); }
84
85 void resize ( const Value &value = Value() )
86 {
87 Hybrid::forEach( std::make_index_sequence< Grid::dimension+1 >{},
88 [ & ]( auto i ){ if( i == this->codimension() ) this->template resize< i >( 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
168 // Implementation of PersistentContainerMap
169 // ----------------------------------------
170
171 template< class G, class IdSet, class Map >
174 {
175 return ConstIterator( data_.begin() );
176 }
177
178 template< class G, class IdSet, class Map >
181 {
182 return Iterator( data_.begin() );
183 }
184
185
186 template< class G, class IdSet, class Map >
189 {
190 return ConstIterator( data_.end() );
191 }
192
193 template< class G, class IdSet, class Map >
196 {
197 return Iterator( data_.end() );
198 }
199
200
201 template< class G, class IdSet, class Map >
202 template< int codim >
203 inline void PersistentContainerMap< G, IdSet, Map >::resize ( const Value &value )
204 {
205 std::integral_constant< bool, Capabilities::hasEntityIterator< Grid, codim >::v > hasEntityIterator;
206 assert( codim == codimension() );
207
208 // create empty map and swap it with current map (no need to copy twice)
209 Map data;
210 std::swap( data, data_ );
211
212 // copy all data from old map into new one (adding new entries, if necessary)
213 const int maxLevel = grid().maxLevel();
214 for ( int level = 0; level <= maxLevel; ++level )
215 migrateLevel< codim >( level, value, data, hasEntityIterator );
216 }
217
218
219 template< class G, class IdSet, class Map >
220 template< int codim >
221 inline void PersistentContainerMap< G, IdSet, Map >
222 ::migrateLevel ( int level, const Value &value, Map &data,
223 std::integral_constant< bool, true > )
224 {
225 typedef typename Grid::LevelGridView LevelView;
226 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
227
228 const LevelView levelView = grid().levelGridView( level );
229 const LevelIterator end = levelView.template end< codim >();
230 for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
231 migrateEntry( idSet().id( *it ), value, data, data_ );
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, false > )
240 {
241 typedef typename Grid::LevelGridView LevelView;
242 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
243
244 const LevelView levelView = grid().levelGridView( level );
245 const LevelIterator end = levelView.template end< 0 >();
246 for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
247 {
248 const typename LevelIterator::Entity &entity = *it;
249 const int subEntities = entity.subEntities( codim );
250 for( int i = 0; i < subEntities; ++i )
251 migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
252 }
253 }
254
255
256 template< class G, class IdSet, class Map >
258 ::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
259 Map &oldData, Map &newData )
260 {
261 // insert entry for id
262 const std::pair< typename Map::iterator, bool > inserted
263 = newData.insert( std::make_pair( id, value ) );
264
265 // if entry did not exist previously, copy data
266 if( inserted.second )
267 {
268 const typename Map::iterator pos = oldData.find( id );
269 if( pos != oldData.end() )
270 {
271 inserted.first->second = pos->second;
272 oldData.erase( pos );
273 }
274 }
275 }
276
277} // namespace Dune
278
279#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:58
Wrapper class for entities.
Definition: common/entity.hh:64
@ codimension
Know your own codimension.
Definition: common/entity.hh:105
Id Set Interface.
Definition: indexidset.hh:450
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:456
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition: common/grid.hh:405
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:24
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, false >)
Definition: persistentcontainermap.hh:238
void swap(This &other)
Definition: persistentcontainermap.hh:95
void fill(const Value &value)
Definition: persistentcontainermap.hh:93
G Grid
Definition: persistentcontainermap.hh:32
void resize(const Value &value)
IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator
Definition: persistentcontainermap.hh:37
int codim_
Definition: persistentcontainermap.hh:131
const Grid * grid_
Definition: persistentcontainermap.hh:130
const Value & operator[](const Entity &entity) const
Definition: persistentcontainermap.hh:50
int codimension() const
Definition: persistentcontainermap.hh:109
void resize(const Value &value=Value())
Definition: persistentcontainermap.hh:85
const IdSet & idSet() const
Definition: persistentcontainermap.hh:128
ConstIterator begin() const
Definition: persistentcontainermap.hh:173
static void migrateEntry(const typename IdSet::IdType &id, const Value &value, Map &oldData, Map &newData)
Definition: persistentcontainermap.hh:258
Map::mapped_type Value
Definition: persistentcontainermap.hh:34
Iterator end()
Definition: persistentcontainermap.hh:195
const Grid & grid() const
Definition: persistentcontainermap.hh:112
const Value & operator()(const Entity &entity, int subEntity) const
Definition: persistentcontainermap.hh:68
PersistentContainerMap(const Grid &grid, int codim, const IdSet &idSet, const Value &value)
Definition: persistentcontainermap.hh:40
Iterator begin()
Definition: persistentcontainermap.hh:180
Map::size_type Size
Definition: persistentcontainermap.hh:35
ConstIterator end() const
Definition: persistentcontainermap.hh:188
Map data_
Definition: persistentcontainermap.hh:133
Size size() const
Definition: persistentcontainermap.hh:83
IteratorWrapper< Value, typename Map::iterator > Iterator
Definition: persistentcontainermap.hh:38
const IdSet * idSet_
Definition: persistentcontainermap.hh:132
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, true >)
Definition: persistentcontainermap.hh:222
void shrinkToFit()
Definition: persistentcontainermap.hh:91
Definition: persistentcontainermap.hh:145
IteratorWrapper(const iterator &it)
Definition: persistentcontainermap.hh:149
A set of traits classes to store static information about grid implementation.