dune-pdelab  2.7-git
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 
26  template<typename T, std::size_t n>
27  class MultiIndex
28  : public ReservedVector<T,n>
29  {
30 
31  typedef ReservedVector<T,n> base_type;
32 
33  public:
34 
36  static const std::size_t max_depth = n;
37 
38  class View
39  {
40 
41  friend class MultiIndex;
42 
43  public:
44 
46  static const std::size_t max_depth = n;
47 
48  typedef typename base_type::value_type value_type;
49  typedef typename base_type::pointer pointer;
50  typedef typename base_type::const_reference reference;
51  typedef typename base_type::const_reference const_reference;
52  typedef typename base_type::size_type size_type;
53  typedef typename base_type::difference_type difference_type;
54  typedef typename base_type::const_iterator iterator;
55  typedef typename base_type::const_iterator const_iterator;
56 
57  private:
58 
59  View(const MultiIndex& mi, size_type size)
60  : _mi(mi)
61  , _size(size)
62  {}
63 
64  public:
65 
66  void clear()
67  {
68  _size = 0;
69  }
70 
72  {
73  return _mi.front();
74  }
75 
77  {
78  return _mi.front();
79  }
80 
82  {
83  return _mi[_size-1];
84  }
85 
87  {
88  return _mi[_size-1];
89  }
90 
92  {
93  assert(i < _size);
94  return _mi[i];
95  }
96 
98  {
99  assert(i < _size);
100  return _mi[i];
101  }
102 
104  {
105  assert(s <= _mi.size());
106  _size = s;
107  }
108 
110  {
111  assert(_size > 0);
112  return View(_mi,_size-1);
113  }
114 
115  size_type size() const
116  {
117  return _size;
118  }
119 
120  bool empty() const
121  {
122  return _size == 0;
123  }
124 
125  friend std::ostream& operator<< (std::ostream& s, const View& mi)
126  {
127  s << "(";
128  // fill up to maximum depth for consistent formatting
129  for (std::size_t i = mi.size(); i < max_depth; ++i)
130  s << " -";
131  for (typename ReservedVector<T,n>::const_iterator it = mi._mi.begin(); it != mi._mi.begin() + mi.size(); ++it)
132  s << std::setw(3) << *it;
133  s << ")";
134  return s;
135  }
136 
137  private:
138  const MultiIndex& _mi;
139  size_type _size;
140 
141  };
142 
144  {}
145 
147  : base_type(static_cast<const base_type&>(view._mi))
148  {
149  this->resize(view.size());
150  }
151 
152  void set(typename ReservedVector<T,n>::value_type index)
153  {
154  this->clear();
155  this->push_back(index);
156  }
157 
159  friend std::ostream& operator<< (std::ostream& s, const MultiIndex& mi)
160  {
161  s << "(";
162  // fill up to maximum depth for consistent formatting
163  for (std::size_t i = mi.size(); i < max_depth; ++i)
164  s << " -";
165  for (typename ReservedVector<T,n>::const_iterator it = mi.begin(); it != mi.end(); ++it)
166  s << std::setw(3) << *it;
167  s << ")";
168  return s;
169  }
170 
171  View view() const
172  {
173  return View(*this,this->size());
174  }
175 
176  View view(std::size_t size) const
177  {
178  return View(*this,size);
179  }
180 
182 
185  bool operator== (const MultiIndex& r) const
186  {
187  return
188  this->size() == r.size() &&
189  std::equal(this->begin(),this->end(),r.begin());
190  }
191 
193  bool operator!= (const MultiIndex& r) const
194  {
195  return !(*this == r);
196  }
197 
198 #if 0
199  bool operator< (const MultiIndex& r) const
200  {
201  // FIXME: think about natural ordering
202  return _c.size() < _r.size();
203  return std::lexicographical_compare(_c.begin(),_c.end(),r._c.begin(),r._c.end());
204  }
205 #endif
206 
207  };
208 
209 
210  template<typename T, std::size_t n>
211  inline std::size_t hash_value(const MultiIndex<T,n>& mi)
212  {
213  return hash_range(mi.begin(),mi.end());
214  }
215 
216 
217  } // namespace PDELab
218 } // namespace Dune
219 
220 DUNE_DEFINE_HASH(DUNE_HASH_TEMPLATE_ARGS(typename T, std::size_t n),DUNE_HASH_TYPE(Dune::PDELab::MultiIndex<T,n>))
221 
222 #endif // DUNE_PDELAB_COMMON_MULTIINDEX_HH
const std::string s
Definition: function.hh:843
std::size_t index
Definition: interpolate.hh:97
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
std::size_t hash_value(const DOFIndex< T, n1, n2 > &di)
Definition: dofindex.hh:334
A class for representing multi-indices.
Definition: multiindex.hh:29
MultiIndex(const View &view)
Definition: multiindex.hh:146
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:159
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:36
View view(std::size_t size) const
Definition: multiindex.hh:176
bool operator==(const MultiIndex &r) const
Tests whether two MultiIndices are equal.
Definition: multiindex.hh:185
View view() const
Definition: multiindex.hh:171
MultiIndex()
Definition: multiindex.hh:143
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition: multiindex.hh:193
void set(typename ReservedVector< T, n >::value_type index)
Definition: multiindex.hh:152
Definition: multiindex.hh:39
reference back()
Definition: multiindex.hh:81
reference front()
Definition: multiindex.hh:71
base_type::const_reference reference
Definition: multiindex.hh:50
base_type::pointer pointer
Definition: multiindex.hh:49
const_reference back() const
Definition: multiindex.hh:86
friend std::ostream & operator<<(std::ostream &s, const View &mi)
Definition: multiindex.hh:125
base_type::value_type value_type
Definition: multiindex.hh:48
size_type size() const
Definition: multiindex.hh:115
void clear()
Definition: multiindex.hh:66
base_type::const_iterator iterator
Definition: multiindex.hh:54
const_reference operator[](size_type i) const
Definition: multiindex.hh:97
const_reference front() const
Definition: multiindex.hh:76
base_type::size_type size_type
Definition: multiindex.hh:52
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:46
void resize(size_type s)
Definition: multiindex.hh:103
base_type::const_iterator const_iterator
Definition: multiindex.hh:55
base_type::const_reference const_reference
Definition: multiindex.hh:51
bool empty() const
Definition: multiindex.hh:120
reference operator[](size_type i)
Definition: multiindex.hh:91
View back_popped() const
Definition: multiindex.hh:109
base_type::difference_type difference_type
Definition: multiindex.hh:53