5#ifndef DUNE_PERSISTENTCONTAINERMAP_HH 
    6#define DUNE_PERSISTENTCONTAINERMAP_HH 
   13#include <dune/common/hybridutilities.hh> 
   24  template< 
class G, 
class IdSet, 
class Map >
 
   30    template< 
class reference, 
class iterator >
 
   31    class IteratorWrapper;
 
   36    typedef typename Map::mapped_type Value;
 
   37    typedef typename Map::size_type Size;
 
   39    typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
 
   40    typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
 
   51    template< 
class Entity >
 
   52    const Value &operator[] ( 
const Entity &entity )
 const 
   55      typename Map::const_iterator pos = data_.find( idSet().
id( entity ) );
 
   56      assert( pos != data_.end() );
 
   60    template< 
class Entity >
 
   61    Value &operator[] ( 
const Entity &entity )
 
   64      typename Map::iterator pos = data_.find( idSet().
id( entity ) );
 
   65      assert( pos != data_.end() );
 
   69    template< 
class Entity >
 
   70    const Value &operator() ( 
const Entity &entity, 
int subEntity )
 const 
   72      typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
 
   73      assert( pos != data_.end() );
 
   77    template< 
class Entity >
 
   78    Value &operator() ( 
const Entity &entity, 
int subEntity )
 
   80      typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
 
   81      assert( pos != data_.end() );
 
   85    Size size ()
 const { 
return data_.size(); }
 
   87    void resize ( 
const Value &value = Value() )
 
   90        [ & ]( 
auto i ){ 
if( 
int(i) == this->codimension() ) this->
template resize< i >( value ); } );
 
   93    void shrinkToFit () {}
 
   95    void fill ( 
const Value &value ) { std::fill( begin(), end(), value ); }
 
   97    void swap ( 
This &other )
 
   99      std::swap( grid_, other.grid_ );
 
  100      std::swap( codim_, other.codim_ );
 
  101      std::swap( idSet_, other.idSet_ );
 
  102      std::swap( data_, other.data_ );
 
  105    ConstIterator begin () 
const;
 
  108    ConstIterator end () 
const;
 
  111    int codimension ()
 const { 
return codim_; }
 
  114    const Grid &grid ()
 const { 
return *grid_; }
 
  116    template< 
int codim >
 
  117    void resize ( 
const Value &value );
 
  119    template< 
int codim >
 
  120    void migrateLevel ( 
int level, 
const Value &value, Map &data,
 
  121                        std::integral_constant< bool, true > );
 
  123    template< 
int codim >
 
  124    void migrateLevel ( 
int level, 
const Value &value, Map &data,
 
  125                        std::integral_constant< bool, false > );
 
  127    static void migrateEntry ( 
const typename IdSet::IdType &
id, 
const Value &value,
 
  128                               Map &oldData, Map &newData );
 
  130    const IdSet &idSet ()
 const { 
return *idSet_; }
 
  143  template< 
class G, 
class IdSet, 
class Map >
 
  144  template< 
class value, 
class iterator >
 
  148    typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
 
  151    IteratorWrapper ( 
const iterator &it ) : it_( it ) {}
 
  153    operator ConstWrapper ()
 const { 
return ConstWrapper( it_ ); }
 
  155    value &operator* () { 
return it_->second; }
 
  156    value *operator-> () { 
return &(it_->second); }
 
  158    bool operator== ( 
const IteratorWrapper &other )
 const { 
return (it_ == other.it_); }
 
  159    bool operator!= ( 
const IteratorWrapper &other )
 const { 
return (it_ != other.it_); }
 
  161    IteratorWrapper &operator++ () { ++it_; 
return *
this; }
 
  173  template< 
class G, 
class IdSet, 
class Map >
 
  174  inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
 
  175  PersistentContainerMap< G, IdSet, Map >::begin ()
 const 
  177    return ConstIterator( data_.begin() );
 
  180  template< 
class G, 
class IdSet, 
class Map >
 
  181  inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
 
  182  PersistentContainerMap< G, IdSet, Map >::begin ()
 
  184    return Iterator( data_.begin() );
 
  188  template< 
class G, 
class IdSet, 
class Map >
 
  189  inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
 
  190  PersistentContainerMap< G, IdSet, Map >::end ()
 const 
  192    return ConstIterator( data_.end() );
 
  195  template< 
class G, 
class IdSet, 
class Map >
 
  196  inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
 
  197  PersistentContainerMap< G, IdSet, Map >::end ()
 
  199    return Iterator( data_.end() );
 
  203  template< 
class G, 
class IdSet, 
class Map >
 
  204  template< 
int codim >
 
  205  inline void PersistentContainerMap< G, IdSet, Map >::resize ( 
const Value &value )
 
  207    std::integral_constant< bool, Capabilities::hasEntityIterator< Grid, codim >::v > hasEntityIterator;
 
  208    assert( codim == codimension() );
 
  212    std::swap( data, data_ );
 
  215    const int maxLevel = grid().maxLevel();
 
  216    for ( 
int level = 0; level <= maxLevel; ++level )
 
  217      migrateLevel< codim >( level, value, data, hasEntityIterator );
 
  221  template< 
class G, 
class IdSet, 
class Map >
 
  222  template< 
int codim >
 
  223  inline void PersistentContainerMap< G, IdSet, Map >
 
  224  ::migrateLevel ( 
int level, 
const Value &value, Map &data,
 
  225                   std::integral_constant< bool, true > )
 
  228    typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
 
  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_ );
 
  237  template< 
class G, 
class IdSet, 
class Map >
 
  238  template< 
int codim >
 
  239  inline void PersistentContainerMap< G, IdSet, Map >
 
  240  ::migrateLevel ( 
int level, 
const Value &value, Map &data,
 
  241                   std::integral_constant< bool, false > )
 
  244    typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
 
  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 )
 
  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_ );
 
  258  template< 
class G, 
class IdSet, 
class Map >
 
  259  inline void PersistentContainerMap< G, IdSet, Map >
 
  260  ::migrateEntry ( 
const typename IdSet::IdType &
id, 
const Value &value,
 
  261                   Map &oldData, Map &newData )
 
  264    const std::pair< typename Map::iterator, bool > inserted
 
  265      = newData.insert( std::make_pair( 
id, value ) );
 
  268    if( inserted.second )
 
  270      const typename Map::iterator pos = oldData.find( 
id );
 
  271      if( pos != oldData.end() )
 
  273        inserted.first->second = pos->second;
 
  274        oldData.erase( pos );
 
Wrapper class for entities.
Definition: entity.hh:66
 
static constexpr int codimension
Know your own codimension.
Definition: entity.hh:106
 
GridFamily::Traits::LevelGridView LevelGridView
type of view for level grid
Definition: grid.hh:402
 
Id Set Interface.
Definition: indexidset.hh:447
 
IdTypeImp IdType
Type used to represent an id.
Definition: indexidset.hh:453
 
map-based implementation of the PersistentContainer
Definition: persistentcontainermap.hh:26
 
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:257
 
constexpr 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:238
 
constexpr 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:260
 
Dune namespace.
Definition: alignedallocator.hh:13
 
Traits for type conversions and type information.