3#ifndef DUNE_PERSISTENTCONTAINERMAP_HH
4#define DUNE_PERSISTENTCONTAINERMAP_HH
9#include <dune/common/hybridutilities.hh>
10#include <dune/common/std/utility.hh>
21 template<
class G,
class IdSet,
class Map >
27 template<
class reference,
class iterator >
28 class IteratorWrapper;
33 typedef typename Map::mapped_type Value;
34 typedef typename Map::size_type Size;
36 typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
37 typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
48 template<
class Entity >
49 const Value &operator[] (
const Entity &entity )
const
52 typename Map::const_iterator pos = data_.find( idSet().
id( entity ) );
53 assert( pos != data_.end() );
57 template<
class Entity >
58 Value &operator[] (
const Entity &entity )
61 typename Map::iterator pos = data_.find( idSet().
id( entity ) );
62 assert( pos != data_.end() );
66 template<
class Entity >
67 const Value &operator() (
const Entity &entity,
int subEntity )
const
69 typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
70 assert( pos != data_.end() );
74 template<
class Entity >
75 Value &operator() (
const Entity &entity,
int subEntity )
77 typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
78 assert( pos != data_.end() );
82 Size size ()
const {
return data_.size(); }
84 void resize (
const Value &value = Value() )
87 [ & ](
auto i ){
if( i == this->codimension() ) this->
template resize< i >( value ); } );
90 void shrinkToFit () {}
92 void fill (
const Value &value ) { std::fill( begin(), end(), value ); }
94 void swap (
This &other )
96 std::swap( grid_, other.grid_ );
97 std::swap( codim_, other.codim_ );
98 std::swap( idSet_, other.idSet_ );
99 std::swap( data_, other.data_ );
102 ConstIterator begin ()
const;
105 ConstIterator end ()
const;
108 int codimension ()
const {
return codim_; }
111 const Grid &grid ()
const {
return *grid_; }
113 template<
int codim >
114 void resize (
const Value &value );
116 template<
int codim >
117 void migrateLevel (
int level,
const Value &value, Map &data,
118 std::integral_constant< bool, true > );
120 template<
int codim >
121 void migrateLevel (
int level,
const Value &value, Map &data,
122 std::integral_constant< bool, false > );
124 static void migrateEntry (
const typename IdSet::IdType &
id,
const Value &value,
125 Map &oldData, Map &newData );
127 const IdSet &idSet ()
const {
return *idSet_; }
140 template<
class G,
class IdSet,
class Map >
141 template<
class value,
class iterator >
145 typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
148 IteratorWrapper (
const iterator &it ) : it_( it ) {}
150 operator ConstWrapper ()
const {
return ConstWrapper( it_ ); }
152 value &operator* () {
return it_->second; }
153 value *operator-> () {
return &(it_->second); }
155 bool operator== (
const IteratorWrapper &other )
const {
return (it_ == other.it_); }
156 bool operator!= (
const IteratorWrapper &other )
const {
return (it_ != other.it_); }
158 IteratorWrapper &operator++ () { ++it_;
return *
this; }
170 template<
class G,
class IdSet,
class Map >
171 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
172 PersistentContainerMap< G, IdSet, Map >::begin ()
const
174 return ConstIterator( data_.begin() );
177 template<
class G,
class IdSet,
class Map >
178 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
179 PersistentContainerMap< G, IdSet, Map >::begin ()
181 return Iterator( data_.begin() );
185 template<
class G,
class IdSet,
class Map >
186 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
187 PersistentContainerMap< G, IdSet, Map >::end ()
const
189 return ConstIterator( data_.end() );
192 template<
class G,
class IdSet,
class Map >
193 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
194 PersistentContainerMap< G, IdSet, Map >::end ()
196 return Iterator( data_.end() );
200 template<
class G,
class IdSet,
class Map >
201 template<
int codim >
202 inline void PersistentContainerMap< G, IdSet, Map >::resize (
const Value &value )
204 std::integral_constant< bool, Capabilities::hasEntity< Grid, codim >::v > hasEntity;
205 assert( codim == codimension() );
209 std::swap( data, data_ );
212 const int maxLevel = grid().maxLevel();
213 for (
int level = 0; level <= maxLevel; ++level )
214 migrateLevel< codim >( level, value, data, hasEntity );
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 > )
225 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
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_ );
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 > )
241 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
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 )
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_ );
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 )
261 const std::pair< typename Map::iterator, bool > inserted
262 = newData.insert( std::make_pair(
id, value ) );
265 if( inserted.second )
267 const typename Map::iterator pos = oldData.find(
id );
268 if( pos != oldData.end() )
270 inserted.first->second = pos->second;
271 oldData.erase( pos );
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:308
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:233
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:255
Dune namespace.
Definition: alignedallocator.hh:10
Traits for type conversions and type information.