dune-fem  2.8-git
localdofstorage.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_HPDG_SPACE_DISCONTINUOUSGALERKIN_LOCALDOFSTORAGE_HH
2 #define DUNE_FEM_HPDG_SPACE_DISCONTINUOUSGALERKIN_LOCALDOFSTORAGE_HH
3 
4 #include <cassert>
5 #include <cstddef>
6 
7 #include <algorithm>
8 #include <ostream>
9 #include <vector>
10 
11 namespace Dune
12 {
13 
14  namespace Fem
15  {
16 
17  namespace hpDG
18  {
19 
20  // LocalDofStorage
21  // ---------------
22 
23  template< class GlobalKey >
25  {
27 
28  using container = std::vector< GlobalKey >;
29 
30  public:
32  using value_type = GlobalKey;
33 
35  using iterator = typename container::iterator;
37  using const_iterator = typename container::const_iterator;
38 
44  LocalDofStorage () : size_( 0 ), new_size_( 0 ) {}
45 
53  LocalDofStorage ( const ThisType & ) = default;
54 
56  LocalDofStorage ( ThisType && ) = default;
57 
59  ThisType &operator= ( const ThisType & ) = default;
60 
62  ThisType &operator= ( ThisType && ) = default;
63 
69  iterator begin () { return dofs_.begin(); }
70 
72  const_iterator begin () const { return dofs_.cbegin(); }
73 
75  iterator end () { return begin() + size(); }
76 
78  const_iterator end () const { return begin() + size(); }
79 
85  std::size_t size () const { return size_; }
86 
94  template< class Function >
95  Function reserve ( std::size_t new_size, Function function )
96  {
97  // remember new size
98  new_size_ = new_size;
99 
100  // enlarge dof vector if needed
101  const std::size_t old_size = dofs_.size();
102  if( old_size < new_size_ )
103  {
104  dofs_.resize( new_size );
105  for( std::size_t i = old_size; i < new_size; ++i )
106  dofs_[ i ] = function();
107  }
108 
109  return std::move( function );
110  }
111 
113  template< class Function >
114  Function resize ( Function function )
115  {
116  assert( new_size_ <= dofs_.size() );
117  size_ = new_size_;
118 
119  const std::size_t holes = dofs_.size() - size_;
120  std::for_each( dofs_.rbegin(), dofs_.rbegin() + holes, function );
121 
122  dofs_.resize( size_ );
123  // dofs_.shrink_to_fit();
124 
125  return std::move( function );
126  }
127 
135  GlobalKey &operator[] ( std::size_t n ) { return dofs_[ n ]; }
136 
138  const GlobalKey &operator[] ( std::size_t n ) const { return dofs_[ n ]; }
139 
142 #ifndef DOXYGEN
143 
144  friend std::ostream &operator<< ( std::ostream &ostream, const LocalDofStorage &storage )
145  {
146  const auto &dofs = storage.dofs_;
147  const std::size_t size = dofs.size();
148  for( std::size_t i = 0; i < size; ++i )
149  ostream << dofs[ i ] << " ";
150  ostream << "[" << storage.size_ << "; " << storage.new_size_ << "]";
151  return ostream;
152  }
153 
154 #endif // #ifndef DOXYGEN
155 
156  private:
157  std::size_t size_, new_size_;
158  std::vector< GlobalKey > dofs_;
159  };
160 
161  } // namespace hpDG
162 
163  } // namespace Fem
164 
165 } // namespace Dune
166 
167 #endif // #ifndef DUNE_FEM_HPDG_SPACE_DISCONTINUOUSGALERKIN_LOCALDOFSTORAGE_HH
Definition: bindguard.hh:11
OutStreamInterface< StreamTraits > & operator<<(OutStreamInterface< StreamTraits > &out, const DiscreteFunctionInterface< Impl > &df)
write a discrete function into an output stream
Definition: discretefunction_inline.hh:397
IteratorRange< typename DF::DofIteratorType > dofs(DF &df)
Iterates over all DOFs.
Definition: rangegenerators.hh:76
Abstract class representing a function.
Definition: common/function.hh:50
Definition: localdofstorage.hh:25
iterator begin()
return iterator to beginning
Definition: localdofstorage.hh:69
std::size_t size() const
return number of dofs
Definition: localdofstorage.hh:85
Function resize(Function function)
remove marked dofs from dof vector
Definition: localdofstorage.hh:114
GlobalKey value_type
global key type
Definition: localdofstorage.hh:32
const_iterator begin() const
return iterator to beginning
Definition: localdofstorage.hh:72
GlobalKey & operator[](std::size_t n)
access element
Definition: localdofstorage.hh:135
iterator end()
return iterator to end
Definition: localdofstorage.hh:75
LocalDofStorage(ThisType &&)=default
move constructor
typename container::iterator iterator
iterator type
Definition: localdofstorage.hh:35
LocalDofStorage()
default constructor
Definition: localdofstorage.hh:44
typename container::const_iterator const_iterator
iterator type
Definition: localdofstorage.hh:37
ThisType & operator=(const ThisType &)=default
assignment operator
const_iterator end() const
return iterator to end
Definition: localdofstorage.hh:78
Function reserve(std::size_t new_size, Function function)
enlarge dof vector
Definition: localdofstorage.hh:95
LocalDofStorage(const ThisType &)=default
copy constructor