dune-grid 2.9.0
persistentcontainermap.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5#ifndef DUNE_PERSISTENTCONTAINERMAP_HH
6#define DUNE_PERSISTENTCONTAINERMAP_HH
7
8#include <algorithm>
9#include <cassert>
10#include <type_traits>
11#include <utility>
12
13#include <dune/common/hybridutilities.hh>
14#include <dune/common/typetraits.hh>
16
17namespace Dune
18{
19
20 // PersistentContainerMap
21 // ----------------------
22
24 template< class G, class IdSet, class Map >
26 {
28
29 protected:
30 template< class reference, class iterator >
31 class IteratorWrapper;
32
33 public:
34 typedef G Grid;
35
36 typedef typename Map::mapped_type Value;
37 typedef typename Map::size_type Size;
38
41
42 PersistentContainerMap ( const Grid &grid, int codim, const IdSet &idSet, const Value &value )
43 : grid_( &grid ),
44 codim_( codim ),
45 idSet_( &idSet ),
46 data_()
47 {
48 resize( value );
49 }
50
51 template< class Entity >
52 const Value &operator[] ( const Entity &entity ) const
53 {
54 assert( Entity::codimension == codimension() );
55 typename Map::const_iterator pos = data_.find( idSet().id( entity ) );
56 assert( pos != data_.end() );
57 return pos->second;
58 }
59
60 template< class Entity >
61 Value &operator[] ( const Entity &entity )
62 {
63 assert( Entity::codimension == codimension() );
64 typename Map::iterator pos = data_.find( idSet().id( entity ) );
65 assert( pos != data_.end() );
66 return pos->second;
67 }
68
69 template< class Entity >
70 const Value &operator() ( const Entity &entity, int subEntity ) const
71 {
72 typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
73 assert( pos != data_.end() );
74 return pos->second;
75 }
76
77 template< class Entity >
78 Value &operator() ( const Entity &entity, int subEntity )
79 {
80 typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
81 assert( pos != data_.end() );
82 return pos->second;
83 }
84
85 Size size () const { return data_.size(); }
86
87 void resize ( const Value &value = Value() )
88 {
89 Hybrid::forEach( std::make_index_sequence< Grid::dimension+1 >{},
90 [ & ]( auto i ){ if( int(i) == this->codimension() ) this->template resize< i >( value ); } );
91 }
92
93 void shrinkToFit () {}
94
95 void fill ( const Value &value ) { std::fill( begin(), end(), value ); }
96
97 void swap ( This &other )
98 {
99 std::swap( grid_, other.grid_ );
100 std::swap( codim_, other.codim_ );
101 std::swap( idSet_, other.idSet_ );
102 std::swap( data_, other.data_ );
103 }
104
107
110
111 int codimension () const { return codim_; }
112
113 protected:
114 const Grid &grid () const { return *grid_; }
115
116 template< int codim >
117 void resize ( const Value &value );
118
119 template< int codim >
120 void migrateLevel ( int level, const Value &value, Map &data,
121 std::integral_constant< bool, true > );
122
123 template< int codim >
124 void migrateLevel ( int level, const Value &value, Map &data,
125 std::integral_constant< bool, false > );
126
127 static void migrateEntry ( const typename IdSet::IdType &id, const Value &value,
128 Map &oldData, Map &newData );
129
130 const IdSet &idSet () const { return *idSet_; }
131
132 const Grid *grid_;
134 const IdSet *idSet_;
135 Map data_;
136 };
137
138
139
140 // PersistentContainerMap::IteratorWrapper
141 // ---------------------------------------
142
143 template< class G, class IdSet, class Map >
144 template< class value, class iterator >
146 : public iterator
147 {
149
150 public:
151 IteratorWrapper ( const iterator &it ) : it_( it ) {}
152
153 operator ConstWrapper () const { return ConstWrapper( it_ ); }
154
155 value &operator* () { return it_->second; }
156 value *operator-> () { return &(it_->second); }
157
158 bool operator== ( const IteratorWrapper &other ) const { return (it_ == other.it_); }
159 bool operator!= ( const IteratorWrapper &other ) const { return (it_ != other.it_); }
160
161 IteratorWrapper &operator++ () { ++it_; return *this; }
162
163 private:
164 iterator it_;
165 };
166
167
168
169
170 // Implementation of PersistentContainerMap
171 // ----------------------------------------
172
173 template< class G, class IdSet, class Map >
176 {
177 return ConstIterator( data_.begin() );
178 }
179
180 template< class G, class IdSet, class Map >
183 {
184 return Iterator( data_.begin() );
185 }
186
187
188 template< class G, class IdSet, class Map >
191 {
192 return ConstIterator( data_.end() );
193 }
194
195 template< class G, class IdSet, class Map >
198 {
199 return Iterator( data_.end() );
200 }
201
202
203 template< class G, class IdSet, class Map >
204 template< int codim >
205 inline void PersistentContainerMap< G, IdSet, Map >::resize ( const Value &value )
206 {
207 std::integral_constant< bool, Capabilities::hasEntityIterator< Grid, codim >::v > hasEntityIterator;
208 assert( codim == codimension() );
209
210 // create empty map and swap it with current map (no need to copy twice)
211 Map data;
212 std::swap( data, data_ );
213
214 // copy all data from old map into new one (adding new entries, if necessary)
215 const int maxLevel = grid().maxLevel();
216 for ( int level = 0; level <= maxLevel; ++level )
217 migrateLevel< codim >( level, value, data, hasEntityIterator );
218 }
219
220
221 template< class G, class IdSet, class Map >
222 template< int codim >
224 ::migrateLevel ( int level, const Value &value, Map &data,
225 std::integral_constant< bool, true > )
226 {
227 typedef typename Grid::LevelGridView LevelView;
228 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
229
230 const LevelView levelView = grid().levelGridView( level );
231 const LevelIterator end = levelView.template end< codim >();
232 for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
233 migrateEntry( idSet().id( *it ), value, data, data_ );
234 }
235
236
237 template< class G, class IdSet, class Map >
238 template< int codim >
240 ::migrateLevel ( int level, const Value &value, Map &data,
241 std::integral_constant< bool, false > )
242 {
243 typedef typename Grid::LevelGridView LevelView;
244 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
245
246 const LevelView levelView = grid().levelGridView( level );
247 const LevelIterator end = levelView.template end< 0 >();
248 for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
249 {
250 const typename LevelIterator::Entity &entity = *it;
251 const int subEntities = entity.subEntities( codim );
252 for( int i = 0; i < subEntities; ++i )
253 migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
254 }
255 }
256
257
258 template< class G, class IdSet, class Map >
260 ::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
261 Map &oldData, Map &newData )
262 {
263 // insert entry for id
264 const std::pair< typename Map::iterator, bool > inserted
265 = newData.insert( std::make_pair( id, value ) );
266
267 // if entry did not exist previously, copy data
268 if( inserted.second )
269 {
270 const typename Map::iterator pos = oldData.find( id );
271 if( pos != oldData.end() )
272 {
273 inserted.first->second = pos->second;
274 oldData.erase( pos );
275 }
276 }
277 }
278
279} // namespace Dune
280
281#endif // #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
void swap(Dune::PersistentContainer< G, T > &a, Dune::PersistentContainer< G, T > &b)
Definition: utility/persistentcontainer.hh:83
Include standard header files.
Definition: agrid.hh:60
Wrapper class for entities.
Definition: common/entity.hh:66
static constexpr int codimension
Know your own codimension.
Definition: common/entity.hh:106
Id Set Interface.
Definition: indexidset.hh:452
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:458
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition: common/grid.hh:402
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:26
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, false >)
Definition: persistentcontainermap.hh:240
void swap(This &other)
Definition: persistentcontainermap.hh:97
void fill(const Value &value)
Definition: persistentcontainermap.hh:95
G Grid
Definition: persistentcontainermap.hh:34
void resize(const Value &value)
IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator
Definition: persistentcontainermap.hh:39
int codim_
Definition: persistentcontainermap.hh:133
const Grid * grid_
Definition: persistentcontainermap.hh:132
const Value & operator[](const Entity &entity) const
Definition: persistentcontainermap.hh:52
int codimension() const
Definition: persistentcontainermap.hh:111
void resize(const Value &value=Value())
Definition: persistentcontainermap.hh:87
const IdSet & idSet() const
Definition: persistentcontainermap.hh:130
ConstIterator begin() const
Definition: persistentcontainermap.hh:175
static void migrateEntry(const typename IdSet::IdType &id, const Value &value, Map &oldData, Map &newData)
Definition: persistentcontainermap.hh:260
Map::mapped_type Value
Definition: persistentcontainermap.hh:36
Iterator end()
Definition: persistentcontainermap.hh:197
const Grid & grid() const
Definition: persistentcontainermap.hh:114
const Value & operator()(const Entity &entity, int subEntity) const
Definition: persistentcontainermap.hh:70
PersistentContainerMap(const Grid &grid, int codim, const IdSet &idSet, const Value &value)
Definition: persistentcontainermap.hh:42
Iterator begin()
Definition: persistentcontainermap.hh:182
Map::size_type Size
Definition: persistentcontainermap.hh:37
ConstIterator end() const
Definition: persistentcontainermap.hh:190
Map data_
Definition: persistentcontainermap.hh:135
Size size() const
Definition: persistentcontainermap.hh:85
IteratorWrapper< Value, typename Map::iterator > Iterator
Definition: persistentcontainermap.hh:40
const IdSet * idSet_
Definition: persistentcontainermap.hh:134
void migrateLevel(int level, const Value &value, Map &data, std::integral_constant< bool, true >)
Definition: persistentcontainermap.hh:224
void shrinkToFit()
Definition: persistentcontainermap.hh:93
Definition: persistentcontainermap.hh:147
IteratorWrapper(const iterator &it)
Definition: persistentcontainermap.hh:151
A set of traits classes to store static information about grid implementation.