Dune Core Modules (2.7.1)

persistentcontainermap.hh
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/hybridutilities.hh>
10#include <dune/common/std/utility.hh>
13
14namespace Dune
15{
16
17 // PersistentContainerMap
18 // ----------------------
19
21 template< class G, class IdSet, class Map >
23 {
25
26 protected:
27 template< class reference, class iterator >
28 class IteratorWrapper;
29
30 public:
31 typedef G Grid;
32
33 typedef typename Map::mapped_type Value;
34 typedef typename Map::size_type Size;
35
36 typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
37 typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
38
39 PersistentContainerMap ( const Grid &grid, int codim, const IdSet &idSet, const Value &value )
40 : grid_( &grid ),
41 codim_( codim ),
42 idSet_( &idSet ),
43 data_()
44 {
45 resize( value );
46 }
47
48 template< class Entity >
49 const Value &operator[] ( const Entity &entity ) const
50 {
51 assert( Entity::codimension == codimension() );
52 typename Map::const_iterator pos = data_.find( idSet().id( entity ) );
53 assert( pos != data_.end() );
54 return pos->second;
55 }
56
57 template< class Entity >
58 Value &operator[] ( const Entity &entity )
59 {
60 assert( Entity::codimension == codimension() );
61 typename Map::iterator pos = data_.find( idSet().id( entity ) );
62 assert( pos != data_.end() );
63 return pos->second;
64 }
65
66 template< class Entity >
67 const Value &operator() ( const Entity &entity, int subEntity ) const
68 {
69 typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
70 assert( pos != data_.end() );
71 return pos->second;
72 }
73
74 template< class Entity >
75 Value &operator() ( const Entity &entity, int subEntity )
76 {
77 typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
78 assert( pos != data_.end() );
79 return pos->second;
80 }
81
82 Size size () const { return data_.size(); }
83
84 void resize ( const Value &value = Value() )
85 {
86 Hybrid::forEach( Std::make_index_sequence< Grid::dimension+1 >{},
87 [ & ]( auto i ){ if( i == this->codimension() ) this->template resize< i >( value ); } );
88 }
89
90 void shrinkToFit () {}
91
92 void fill ( const Value &value ) { std::fill( begin(), end(), value ); }
93
94 void swap ( This &other )
95 {
96 std::swap( grid_, other.grid_ );
97 std::swap( codim_, other.codim_ );
98 std::swap( idSet_, other.idSet_ );
99 std::swap( data_, other.data_ );
100 }
101
102 ConstIterator begin () const;
103 Iterator begin ();
104
105 ConstIterator end () const;
106 Iterator end ();
107
108 int codimension () const { return codim_; }
109
110 protected:
111 const Grid &grid () const { return *grid_; }
112
113 template< int codim >
114 void resize ( const Value &value );
115
116 template< int codim >
117 void migrateLevel ( int level, const Value &value, Map &data,
118 std::integral_constant< bool, true > );
119
120 template< int codim >
121 void migrateLevel ( int level, const Value &value, Map &data,
122 std::integral_constant< bool, false > );
123
124 static void migrateEntry ( const typename IdSet::IdType &id, const Value &value,
125 Map &oldData, Map &newData );
126
127 const IdSet &idSet () const { return *idSet_; }
128
129 const Grid *grid_;
130 int codim_;
131 const IdSet *idSet_;
132 Map data_;
133 };
134
135
136
137 // PersistentContainerMap::IteratorWrapper
138 // ---------------------------------------
139
140 template< class G, class IdSet, class Map >
141 template< class value, class iterator >
143 : public iterator
144 {
145 typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
146
147 public:
148 IteratorWrapper ( const iterator &it ) : it_( it ) {}
149
150 operator ConstWrapper () const { return ConstWrapper( it_ ); }
151
152 value &operator* () { return it_->second; }
153 value *operator-> () { return &(it_->second); }
154
155 bool operator== ( const IteratorWrapper &other ) const { return (it_ == other.it_); }
156 bool operator!= ( const IteratorWrapper &other ) const { return (it_ != other.it_); }
157
158 IteratorWrapper &operator++ () { ++it_; return *this; }
159
160 private:
161 iterator it_;
162 };
163
164
165
166
167 // Implementation of PersistentContainerMap
168 // ----------------------------------------
169
170 template< class G, class IdSet, class Map >
171 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
172 PersistentContainerMap< G, IdSet, Map >::begin () const
173 {
174 return ConstIterator( data_.begin() );
175 }
176
177 template< class G, class IdSet, class Map >
178 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
179 PersistentContainerMap< G, IdSet, Map >::begin ()
180 {
181 return Iterator( data_.begin() );
182 }
183
184
185 template< class G, class IdSet, class Map >
186 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
187 PersistentContainerMap< G, IdSet, Map >::end () const
188 {
189 return ConstIterator( data_.end() );
190 }
191
192 template< class G, class IdSet, class Map >
193 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
194 PersistentContainerMap< G, IdSet, Map >::end ()
195 {
196 return Iterator( data_.end() );
197 }
198
199
200 template< class G, class IdSet, class Map >
201 template< int codim >
202 inline void PersistentContainerMap< G, IdSet, Map >::resize ( const Value &value )
203 {
204 std::integral_constant< bool, Capabilities::hasEntity< Grid, codim >::v > hasEntity;
205 assert( codim == codimension() );
206
207 // create empty map and swap it with current map (no need to copy twice)
208 Map data;
209 std::swap( data, data_ );
210
211 // copy all data from old map into new one (adding new entries, if necessary)
212 const int maxLevel = grid().maxLevel();
213 for ( int level = 0; level <= maxLevel; ++level )
214 migrateLevel< codim >( level, value, data, hasEntity );
215 }
216
217
218 template< class G, class IdSet, class Map >
219 template< int codim >
220 inline void PersistentContainerMap< G, IdSet, Map >
221 ::migrateLevel ( int level, const Value &value, Map &data,
222 std::integral_constant< bool, true > )
223 {
224 typedef typename Grid::LevelGridView LevelView;
225 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
226
227 const LevelView levelView = grid().levelGridView( level );
228 const LevelIterator end = levelView.template end< codim >();
229 for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
230 migrateEntry( idSet().id( *it ), value, data, data_ );
231 }
232
233
234 template< class G, class IdSet, class Map >
235 template< int codim >
236 inline void PersistentContainerMap< G, IdSet, Map >
237 ::migrateLevel ( int level, const Value &value, Map &data,
238 std::integral_constant< bool, false > )
239 {
240 typedef typename Grid::LevelGridView LevelView;
241 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
242
243 const LevelView levelView = grid().levelGridView( level );
244 const LevelIterator end = levelView.template end< 0 >();
245 for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
246 {
247 const typename LevelIterator::Entity &entity = *it;
248 const int subEntities = entity.subEntities( codim );
249 for( int i = 0; i < subEntities; ++i )
250 migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
251 }
252 }
253
254
255 template< class G, class IdSet, class Map >
256 inline void PersistentContainerMap< G, IdSet, Map >
257 ::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
258 Map &oldData, Map &newData )
259 {
260 // insert entry for id
261 const std::pair< typename Map::iterator, bool > inserted
262 = newData.insert( std::make_pair( id, value ) );
263
264 // if entry did not exist previously, copy data
265 if( inserted.second )
266 {
267 const typename Map::iterator pos = oldData.find( id );
268 if( pos != oldData.end() )
269 {
270 inserted.first->second = pos->second;
271 oldData.erase( pos );
272 }
273 }
274 }
275
276} // namespace Dune
277
278#endif // #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
Wrapper class for entities.
Definition: entity.hh:64
@ codimension
Know your own codimension.
Definition: entity.hh:105
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition: grid.hh:406
Id Set Interface.
Definition: indexidset.hh:441
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:444
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:23
A set of traits classes to store static information about grid implementation.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:267
EnableIfInterOperable< T1, T2, bool >::type operator==(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for equality.
Definition: iteratorfacades.hh:235
EnableIfInterOperable< T1, T2, bool >::type operator!=(const ForwardIteratorFacade< T1, V1, R1, D > &lhs, const ForwardIteratorFacade< T2, V2, R2, D > &rhs)
Checks for inequality.
Definition: iteratorfacades.hh:257
Dune namespace.
Definition: alignedallocator.hh:14
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)