dune-pdelab  2.0.0
multiindex.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_PDELAB_COMMON_MULTIINDEX_HH
4 #define DUNE_PDELAB_COMMON_MULTIINDEX_HH
5 
6 #include <dune/common/reservedvector.hh>
7 #include <dune/geometry/typeindex.hh>
8 
9 #include <dune/common/hash.hh>
10 
11 #include <algorithm>
12 #include <iomanip>
13 
14 namespace Dune {
15 
16  namespace PDELab {
17 
18 
20 
149  template<typename T, std::size_t n>
151  : public ReservedVector<T,n>
152  {
153 
154  typedef ReservedVector<T,n> base_type;
155 
156  public:
157 
159  static const std::size_t max_depth = n;
160 
161  class View
162  {
163 
164  friend class MultiIndex;
165 
166  public:
167 
169  static const std::size_t max_depth = n;
170 
171  typedef typename base_type::value_type value_type;
172  typedef typename base_type::pointer pointer;
173  typedef typename base_type::const_reference reference;
174  typedef typename base_type::const_reference const_reference;
175  typedef typename base_type::size_type size_type;
176  typedef typename base_type::difference_type difference_type;
177  typedef typename base_type::const_iterator iterator;
178  typedef typename base_type::const_iterator const_iterator;
179 
180  private:
181 
182  View(const MultiIndex& mi, size_type size)
183  : _mi(mi)
184  , _size(size)
185  {}
186 
187  public:
188 
189  void clear()
190  {
191  _size = 0;
192  }
193 
195  {
196  return _mi.front();
197  }
198 
200  {
201  return _mi.front();
202  }
203 
205  {
206  return _mi[_size-1];
207  }
208 
210  {
211  return _mi[_size-1];
212  }
213 
215  {
216  assert(i < _size);
217  return _mi[i];
218  }
219 
221  {
222  assert(i < _size);
223  return _mi[i];
224  }
225 
227  {
228  assert(s <= _mi.size());
229  _size = s;
230  }
231 
233  {
234  assert(_size > 0);
235  return View(_mi,_size-1);
236  }
237 
238  size_type size() const
239  {
240  return _size;
241  }
242 
243  bool empty() const
244  {
245  return _size == 0;
246  }
247 
248  friend std::ostream& operator<< (std::ostream& s, const View& mi)
249  {
250  s << "(";
251  // fill up to maximum depth for consistent formatting
252  for (std::size_t i = mi.size(); i < max_depth; ++i)
253  s << " -";
254  for (typename ReservedVector<T,n>::const_iterator it = mi._mi.begin(); it != mi._mi.begin() + mi.size(); ++it)
255  s << std::setw(3) << *it;
256  s << ")";
257  return s;
258  }
259 
260  private:
261  const MultiIndex& _mi;
263 
264  };
265 
267  {}
268 
269  MultiIndex(const View& view)
270  : base_type(static_cast<const base_type&>(view._mi))
271  {
272  this->resize(view.size());
273  }
274 
275  void set(typename ReservedVector<T,n>::value_type index)
276  {
277  this->clear();
278  this->push_back(index);
279  }
280 
282  friend std::ostream& operator<< (std::ostream& s, const MultiIndex& mi)
283  {
284  s << "(";
285  // fill up to maximum depth for consistent formatting
286  for (std::size_t i = mi.size(); i < max_depth; ++i)
287  s << " -";
288  for (typename ReservedVector<T,n>::const_iterator it = mi.begin(); it != mi.end(); ++it)
289  s << std::setw(3) << *it;
290  s << ")";
291  return s;
292  }
293 
294  View view() const
295  {
296  return View(*this,this->size());
297  }
298 
299  View view(std::size_t size) const
300  {
301  return View(*this,size);
302  }
303 
305 
308  bool operator== (const MultiIndex& r) const
309  {
310  return
311  this->size() == r.size() &&
312  std::equal(this->begin(),this->end(),r.begin());
313  }
314 
316  bool operator!= (const MultiIndex& r) const
317  {
318  return !(*this == r);
319  }
320 
321 #if 0
322  bool operator< (const MultiIndex& r) const
323  {
324  // FIXME: think about natural ordering
325  return _c.size() < _r.size();
326  return std::lexicographical_compare(_c.begin(),_c.end(),r._c.begin(),r._c.end());
327  }
328 #endif
329 
330  };
331 
332 
333  template<typename T, std::size_t n>
334  inline std::size_t hash_value(const MultiIndex<T,n>& mi)
335  {
336  return hash_range(mi.begin(),mi.end());
337  }
338 
339 
340  } // namespace PDELab
341 } // namespace Dune
342 
343 DUNE_DEFINE_HASH(DUNE_HASH_TEMPLATE_ARGS(typename T, std::size_t n),DUNE_HASH_TYPE(Dune::PDELab::MultiIndex<T,n>))
344 
345 #endif // DUNE_PDELAB_COMMON_MULTIINDEX_HH
void clear()
Definition: multiindex.hh:189
base_type::value_type value_type
Definition: multiindex.hh:171
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:159
void set(typename ReservedVector< T, n >::value_type index)
Definition: multiindex.hh:275
base_type::const_reference reference
Definition: multiindex.hh:173
reference operator[](size_type i)
Definition: multiindex.hh:214
base_type::pointer pointer
Definition: multiindex.hh:172
base_type::difference_type difference_type
Definition: multiindex.hh:176
base_type::size_type size_type
Definition: multiindex.hh:175
View view(std::size_t size) const
Definition: multiindex.hh:299
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:169
bool empty() const
Definition: multiindex.hh:243
const_reference back() const
Definition: multiindex.hh:209
base_type::const_reference const_reference
Definition: multiindex.hh:174
size_type size() const
Definition: multiindex.hh:238
const_reference operator[](size_type i) const
Definition: multiindex.hh:220
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition: multiindex.hh:316
View back_popped() const
Definition: multiindex.hh:232
std::size_t _size
Definition: datahandleprovider.hh:46
reference back()
Definition: multiindex.hh:204
void resize(size_type s)
Definition: multiindex.hh:226
View view() const
Definition: multiindex.hh:294
reference front()
Definition: multiindex.hh:194
Definition: multiindex.hh:161
A multi-index representing a degree of freedom in a GridFunctionSpace.
Definition: multiindex.hh:150
MultiIndex(const View &view)
Definition: multiindex.hh:269
friend std::ostream & operator<<(std::ostream &s, const View &mi)
Definition: multiindex.hh:248
const_reference front() const
Definition: multiindex.hh:199
bool operator==(const MultiIndex &r) const
Tests whether two MultiIndices are equal.
Definition: multiindex.hh:308
MultiIndex()
Definition: multiindex.hh:266
std::size_t hash_value(const DOFIndex< T, n1, n2 > &di)
Definition: dofindex.hh:196
base_type::const_iterator iterator
Definition: multiindex.hh:177
const std::string s
Definition: function.hh:1103
friend std::ostream & operator<<(std::ostream &s, const MultiIndex &mi)
Writes a pretty representation of the MultiIndex to the given std::ostream.
Definition: multiindex.hh:282
base_type::const_iterator const_iterator
Definition: multiindex.hh:178