/usr/include/dune/grid/utility/persistentcontainermap.hh is in libdune-grid-dev 2.5.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_PERSISTENTCONTAINERMAP_HH
#define DUNE_PERSISTENTCONTAINERMAP_HH
#include <algorithm>
#include <cassert>
#include <dune/common/typetraits.hh>
#include <dune/common/forloop.hh>
#include <dune/grid/common/capabilities.hh>
namespace Dune
{
// PersistentContainerMap
// ----------------------
/** \brief map-based implementation of the PersistentContainer */
template< class G, class IdSet, class Map >
class PersistentContainerMap
{
typedef PersistentContainerMap< G, IdSet, Map > This;
protected:
template< class reference, class iterator >
class IteratorWrapper;
template< int codim >
struct Resize;
public:
typedef G Grid;
typedef typename Map::mapped_type Value;
typedef typename Map::size_type Size;
typedef IteratorWrapper< const Value, typename Map::const_iterator > ConstIterator;
typedef IteratorWrapper< Value, typename Map::iterator > Iterator;
PersistentContainerMap ( const Grid &grid, int codim, const IdSet &idSet, const Value &value )
: grid_( &grid ),
codim_( codim ),
idSet_( &idSet ),
data_()
{
resize( value );
}
template< class Entity >
const Value &operator[] ( const Entity &entity ) const
{
assert( Entity::codimension == codimension() );
typename Map::const_iterator pos = data_.find( idSet().id( entity ) );
assert( pos != data_.end() );
return pos->second;
}
template< class Entity >
Value &operator[] ( const Entity &entity )
{
assert( Entity::codimension == codimension() );
typename Map::iterator pos = data_.find( idSet().id( entity ) );
assert( pos != data_.end() );
return pos->second;
}
template< class Entity >
const Value &operator() ( const Entity &entity, int subEntity ) const
{
typename Map::const_iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
assert( pos != data_.end() );
return pos->second;
}
template< class Entity >
Value &operator() ( const Entity &entity, int subEntity )
{
typename Map::iterator pos = data_.find( idSet().subId( entity, subEntity, codimension() ) );
assert( pos != data_.end() );
return pos->second;
}
Size size () const { return data_.size(); }
void resize ( const Value &value = Value() )
{
return ForLoop< Resize, 0, Grid::dimension >::apply( *this, value );
}
void shrinkToFit () {}
void fill ( const Value &value ) { std::fill( begin(), end(), value ); }
void swap ( This &other )
{
std::swap( grid_, other.grid_ );
std::swap( codim_, other.codim_ );
std::swap( idSet_, other.idSet_ );
std::swap( data_, other.data_ );
}
ConstIterator begin () const;
Iterator begin ();
ConstIterator end () const;
Iterator end ();
int codimension () const { return codim_; }
protected:
const Grid &grid () const { return *grid_; }
template< int codim >
void resize ( const Value &value );
template< int codim >
void migrateLevel ( int level, const Value &value, Map &data,
std::integral_constant< bool, true > );
template< int codim >
void migrateLevel ( int level, const Value &value, Map &data,
std::integral_constant< bool, false > );
static void migrateEntry ( const typename IdSet::IdType &id, const Value &value,
Map &oldData, Map &newData );
const IdSet &idSet () const { return *idSet_; }
const Grid *grid_;
int codim_;
const IdSet *idSet_;
Map data_;
};
// PersistentContainerMap::IteratorWrapper
// ---------------------------------------
template< class G, class IdSet, class Map >
template< class value, class iterator >
class PersistentContainerMap< G, IdSet, Map >::IteratorWrapper
: public iterator
{
typedef IteratorWrapper< const value, typename Map::const_iterator > ConstWrapper;
public:
IteratorWrapper ( const iterator &it ) : it_( it ) {}
operator ConstWrapper () const { return ConstWrapper( it_ ); }
value &operator* () { return it_->second; }
value *operator-> () { return &(it_->second); }
bool operator== ( const IteratorWrapper &other ) const { return (it_ == other.it_); }
bool operator!= ( const IteratorWrapper &other ) const { return (it_ != other.it_); }
IteratorWrapper &operator++ () { ++it_; return *this; }
private:
iterator it_;
};
// PersistentContainerMap::Resize
// ------------------------------
template< class G, class IdSet, class Map >
template< int codim >
struct PersistentContainerMap< G, IdSet, Map >::Resize
{
static void apply ( PersistentContainerMap< G, IdSet, Map > &container,
const Value &value )
{
if( codim == container.codimension() )
container.template resize< codim >( value );
}
};
// Implementation of PersistentContainerMap
// ----------------------------------------
template< class G, class IdSet, class Map >
inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
PersistentContainerMap< G, IdSet, Map >::begin () const
{
return ConstIterator( data_.begin() );
}
template< class G, class IdSet, class Map >
inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
PersistentContainerMap< G, IdSet, Map >::begin ()
{
return Iterator( data_.begin() );
}
template< class G, class IdSet, class Map >
inline typename PersistentContainerMap< G, IdSet, Map >::ConstIterator
PersistentContainerMap< G, IdSet, Map >::end () const
{
return ConstIterator( data_.end() );
}
template< class G, class IdSet, class Map >
inline typename PersistentContainerMap< G, IdSet, Map >::Iterator
PersistentContainerMap< G, IdSet, Map >::end ()
{
return Iterator( data_.end() );
}
template< class G, class IdSet, class Map >
template< int codim >
inline void PersistentContainerMap< G, IdSet, Map >::resize ( const Value &value )
{
std::integral_constant< bool, Capabilities::hasEntity< Grid, codim >::v > hasEntity;
assert( codim == codimension() );
// create empty map and swap it with current map (no need to copy twice)
Map data;
std::swap( data, data_ );
// copy all data from old map into new one (adding new entries, if necessary)
const int maxLevel = grid().maxLevel();
for ( int level = 0; level <= maxLevel; ++level )
migrateLevel< codim >( level, value, data, hasEntity );
}
template< class G, class IdSet, class Map >
template< int codim >
inline void PersistentContainerMap< G, IdSet, Map >
::migrateLevel ( int level, const Value &value, Map &data,
std::integral_constant< bool, true > )
{
typedef typename Grid::LevelGridView LevelView;
typedef typename LevelView::template Codim< codim >::Iterator LevelIterator;
const LevelView levelView = grid().levelGridView( level );
const LevelIterator end = levelView.template end< codim >();
for( LevelIterator it = levelView.template begin< codim >(); it != end; ++it )
migrateEntry( idSet().id( *it ), value, data, data_ );
}
template< class G, class IdSet, class Map >
template< int codim >
inline void PersistentContainerMap< G, IdSet, Map >
::migrateLevel ( int level, const Value &value, Map &data,
std::integral_constant< bool, false > )
{
typedef typename Grid::LevelGridView LevelView;
typedef typename LevelView::template Codim< 0 >::Iterator LevelIterator;
const LevelView levelView = grid().levelGridView( level );
const LevelIterator end = levelView.template end< 0 >();
for( LevelIterator it = levelView.template begin< 0 >(); it != end; ++it )
{
const typename LevelIterator::Entity &entity = *it;
const int subEntities = entity.subEntities( codim );
for( int i = 0; i < subEntities; ++i )
migrateEntry( idSet().subId( entity, i, codim ), value, data, data_ );
}
}
template< class G, class IdSet, class Map >
inline void PersistentContainerMap< G, IdSet, Map >
::migrateEntry ( const typename IdSet::IdType &id, const Value &value,
Map &oldData, Map &newData )
{
// insert entry for id
const std::pair< typename Map::iterator, bool > inserted
= newData.insert( std::make_pair( id, value ) );
// if entry did not exist previously, copy data
if( inserted.second )
{
const typename Map::iterator pos = oldData.find( id );
if( pos != oldData.end() )
{
inserted.first->second = pos->second;
oldData.erase( pos );
}
}
}
} // namespace Dune
#endif // #ifndef DUNE_PERSISTENTCONTAINERMAP_HH
|