dune-fem  2.8-git
code.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_DOFMAPPER_CODE_HH
2 #define DUNE_FEM_DOFMAPPER_CODE_HH
3 
4 #include <algorithm>
5 #include <cassert>
6 #include <iostream>
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
14  // DofMapperCode
15  // -------------
16 
18  {
19  protected:
20  typedef const unsigned int *ConstIterator;
21  typedef unsigned int *Iterator;
22 
23  DofMapperCode ( unsigned int numBlocks, unsigned int numDofs )
24  {
25  code_ = new unsigned int[ size( numBlocks, numDofs ) ];
26  code_[ 0 ] = numBlocks;
27  code_[ 1 ] = numDofs;
28  }
29 
30  public:
32  {
33  code_ = new unsigned int[ size( 0, 0 ) ];
34  code_[ 1 ] = code_[ 0 ] = 0;
35  }
36 
37  DofMapperCode ( const DofMapperCode &other )
38  {
39  code_ = new unsigned int[ other.size() ];
40  std::copy( ConstIterator( other.code_ ), other.end(), code_ );
41  }
42 
44  {
45  delete[] code_;
46  }
47 
48  const DofMapperCode &operator= ( const DofMapperCode &other )
49  {
50  if( size() != other.size() )
51  {
52  delete[] code_;
53  code_ = new unsigned int[ other.size() ];
54  }
55  std::copy( ConstIterator( other.code_ ), other.end(), code_ );
56  return *this;
57  }
58 
78  template< class Functor >
79  void operator() ( Functor f ) const
80  {
81  for( ConstIterator it = begin(); it != end(); )
82  {
83  const unsigned int gtIndex = *(it++);
84  const unsigned int subEntity = *(it++);
85  unsigned int nDofs = *(it++);
86  f( gtIndex, subEntity, ConstIterator( it ), ConstIterator( it + nDofs ) );
87  it += nDofs;
88  }
89  }
90 
91  unsigned int numBlocks () const { return code_[ 0 ]; }
92  unsigned int numDofs () const { return code_[ 1 ]; }
93 
94  friend std::ostream &operator<< ( std::ostream &out, const DofMapperCode &code )
95  {
96  out << "{ " << code.numBlocks() << ", " << code.numDofs();
97  for( DofMapperCode::ConstIterator it = code.begin(); it != code.end(); ++it )
98  out << ", " << *it;
99  return out << " }";
100  }
101 
102  protected:
103  ConstIterator begin () const { return code_ + 2; }
104  Iterator begin () { return code_ + 2; }
105  ConstIterator end () const { return code_ + size(); }
106  Iterator end () { return code_ + size(); }
107 
108  std::size_t size () const { return size( numBlocks(), numDofs() ); }
109 
116  static std::size_t size ( unsigned int numBlocks, unsigned int numDofs )
117  {
118  return 2 + 3*numBlocks + numDofs;
119  }
120 
121  // Format of the code_ array:
122  //
123  // code_[0]: number of subentities with DoFs
124  // code_[1]: total number of DoFs
125  //
126  // For all k = 0 ... (numBlocks-1)
127  // (NB: k corresponds to a subentity with DoFs)
128  // It follows a variable size block (offset_0 := 2):
129  //
130  // code_[offset_k + 0]: global geometry type index of the subentity
131  // (this also encodes the codim)
132  // code_[offset_k + 1]: local number i_k of subentity (0 <= i_k < refElem.size( codim ))
133  // code_[offset_k + 2]: #DoFs n_k attached to this subentity
134  //
135  // code_[offset_k + 3 + j]:
136  // for 0 <= j < n_k, the local index of the given DoF, where "local" now
137  // means the number of the corresponding local basis function of the bulk
138  // element, i.e., not the numbering inside the entity.
139  //
140  // offset_(k+1) := (offset_k + 3 + n_k) is then just the start of the
141  // next block.
142  unsigned int *code_;
143  };
144 
145 
146 
147  // DofMapperCodeWriter
148  // -------------------
149 
151  : public DofMapperCode
152  {
153  public:
154  DofMapperCodeWriter ( unsigned int numBlocks, unsigned int numDofs )
156  {}
157 
158  const unsigned int &operator[] ( unsigned int i ) const
159  {
160  assert( (std::ptrdiff_t)i < end() - begin() );
161  return begin()[ i ];
162  }
163 
164  unsigned int &operator[] ( unsigned int i )
165  {
166  assert( (std::ptrdiff_t)i < end() - begin() );
167  return begin()[ i ];
168  }
169  };
170 
171  } // namespace Fem
172 
173 } // namespace Dune
174 
175 #endif // #ifndef DUNE_FEM_DOFMAPPER_CODE_HH
Definition: bindguard.hh:11
Definition: code.hh:18
const unsigned int * ConstIterator
Definition: code.hh:20
static std::size_t size(unsigned int numBlocks, unsigned int numDofs)
Definition: code.hh:116
unsigned int numBlocks() const
Definition: code.hh:91
~DofMapperCode()
Definition: code.hh:43
unsigned int * Iterator
Definition: code.hh:21
Iterator end()
Definition: code.hh:106
ConstIterator end() const
Definition: code.hh:105
std::size_t size() const
Definition: code.hh:108
unsigned int * code_
Definition: code.hh:142
Iterator begin()
Definition: code.hh:104
DofMapperCode(const DofMapperCode &other)
Definition: code.hh:37
DofMapperCode(unsigned int numBlocks, unsigned int numDofs)
Definition: code.hh:23
const DofMapperCode & operator=(const DofMapperCode &other)
Definition: code.hh:48
void operator()(Functor f) const
execute DoF mapper code
Definition: code.hh:79
DofMapperCode()
Definition: code.hh:31
friend std::ostream & operator<<(std::ostream &out, const DofMapperCode &code)
Definition: code.hh:94
ConstIterator begin() const
Definition: code.hh:103
unsigned int numDofs() const
Definition: code.hh:92
Definition: code.hh:152
const unsigned int & operator[](unsigned int i) const
Definition: code.hh:158
DofMapperCodeWriter(unsigned int numBlocks, unsigned int numDofs)
Definition: code.hh:154