3#ifndef DUNE_PERSISTENTCONTAINERMAP_HH
4#define DUNE_PERSISTENTCONTAINERMAP_HH
11#include <dune/common/hybridutilities.hh>
22 template<
class G,
class IdSet,
class Map >
28 template<
class reference,
class iterator >
29 class IteratorWrapper;
34 typedef typename Map::mapped_type Value;
35 typedef typename Map::size_type Size;
37 typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
38 typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
49 template<
class Entity >
50 const Value &operator[] (
const Entity &entity )
const
53 typename Map::const_iterator pos = data_.find( idSet().
id( entity ) );
54 assert( pos != data_.end() );
58 template<
class Entity >
59 Value &operator[] (
const Entity &entity )
62 typename Map::iterator pos = data_.find( idSet().
id( entity ) );
63 assert( pos != data_.end() );
67 template<
class Entity >
68 const Value &operator() (
const Entity &entity,
int subEntity )
const
70 typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
71 assert( pos != data_.end() );
75 template<
class Entity >
76 Value &operator() (
const Entity &entity,
int subEntity )
78 typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
79 assert( pos != data_.end() );
83 Size size ()
const {
return data_.size(); }
85 void resize (
const Value &value = Value() )
88 [ & ](
auto i ){
if( i == this->codimension() ) this->
template resize< i >( value ); } );
91 void shrinkToFit () {}
93 void fill (
const Value &value ) { std::fill( begin(), end(), value ); }
95 void swap (
This &other )
97 std::swap( grid_, other.grid_ );
98 std::swap( codim_, other.codim_ );
99 std::swap( idSet_, other.idSet_ );
100 std::swap( data_, other.data_ );
103 ConstIterator begin ()
const;
106 ConstIterator end ()
const;
109 int codimension ()
const {
return codim_; }
112 const Grid &grid ()
const {
return *grid_; }
114 template<
int codim >
115 void resize (
const Value &value );
117 template<
int codim >
118 void migrateLevel (
int level,
const Value &value, Map &data,
119 std::integral_constant< bool, true > );
121 template<
int codim >
122 void migrateLevel (
int level,
const Value &value, Map &data,
123 std::integral_constant< bool, false > );
125 static void migrateEntry (
const typename IdSet::IdType &
id,
const Value &value,
126 Map &oldData, Map &newData );
128 const IdSet &idSet ()
const {
return *idSet_; }
141 template<
class G,
class IdSet,
class Map >
142 template<
class value,
class iterator >
146 typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
149 IteratorWrapper (
const iterator &it ) : it_( it ) {}
151 operator ConstWrapper ()
const {
return ConstWrapper( it_ ); }
153 value &operator* () {
return it_->second; }
154 value *operator-> () {
return &(it_->second); }
156 bool operator== (
const IteratorWrapper &other )
const {
return (it_ == other.it_); }
157 bool operator!= (
const IteratorWrapper &other )
const {
return (it_ != other.it_); }
159 IteratorWrapper &operator++ () { ++it_;
return *
this; }
171 template<
class G,
class IdSet,
class Map >
172 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
173 PersistentContainerMap< G, IdSet, Map >::begin ()
const
175 return ConstIterator( data_.begin() );
178 template<
class G,
class IdSet,
class Map >
179 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
180 PersistentContainerMap< G, IdSet, Map >::begin ()
182 return Iterator( data_.begin() );
186 template<
class G,
class IdSet,
class Map >
187 inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
188 PersistentContainerMap< G, IdSet, Map >::end ()
const
190 return ConstIterator( data_.end() );
193 template<
class G,
class IdSet,
class Map >
194 inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
195 PersistentContainerMap< G, IdSet, Map >::end ()
197 return Iterator( data_.end() );
201 template<
class G,
class IdSet,
class Map >
202 template<
int codim >
203 inline void PersistentContainerMap< G, IdSet, Map >::resize (
const Value &value )
205 std::integral_constant< bool, Capabilities::hasEntityIterator< Grid, codim >::v > hasEntityIterator;
206 assert( codim == codimension() );
210 std::swap( data, data_ );
213 const int maxLevel = grid().maxLevel();
214 for (
int level = 0; level <= maxLevel; ++level )
215 migrateLevel< codim >( level, value, data, hasEntityIterator );
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 > )
226 typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
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_ );
235 template<
class G,
class IdSet,
class Map >
236 template<
int codim >
237 inline void PersistentContainerMap< G, IdSet, Map >
238 ::migrateLevel (
int level,
const Value &value, Map &data,
239 std::integral_constant< bool, false > )
242 typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
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 )
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_ );
256 template<
class G,
class IdSet,
class Map >
257 inline void PersistentContainerMap< G, IdSet, Map >
258 ::migrateEntry (
const typename IdSet::IdType &
id,
const Value &value,
259 Map &oldData, Map &newData )
262 const std::pair< typename Map::iterator, bool > inserted
263 = newData.insert( std::make_pair(
id, value ) );
266 if( inserted.second )
268 const typename Map::iterator pos = oldData.find(
id );
269 if( pos != oldData.end() )
271 inserted.first->second = pos->second;
272 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:405
Id Set Interface.
Definition: indexidset.hh:450
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:456
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:24
A set of traits classes to store static information about grid implementation.
Traits for type conversions and type information.
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:266
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:11