dune-pdelab 2.10-git
Loading...
Searching...
No Matches
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
7
9
11#include <dune/common/hash.hh>
12
13#include <algorithm>
14#include <iomanip>
15
16namespace Dune {
17
18 namespace PDELab {
19
20
22
28 template<typename T, std::size_t n>
30 : public ReservedVector<T,n>
31 {
32
34
35 public:
36
38 static const std::size_t max_depth = n;
39
40 using typename base_type::size_type;
41 using typename base_type::value_type;
42 using typename base_type::reference;
43 using typename base_type::const_reference;
44
45 class View
46 {
47
48 friend class MultiIndex;
49
50 public:
51
53 static const std::size_t max_depth = n;
54
56 typedef typename base_type::pointer pointer;
63
64 private:
65
66 View(const MultiIndex& mi, size_type size)
67 : _mi(mi)
68 , _size(size)
69 {}
70
71 public:
72
73 void clear()
74 {
75 _size = 0;
76 }
77
79 {
80 return _mi.front();
81 }
82
84 {
85 return _mi.front();
86 }
87
89 {
90 return _mi[_size-1];
91 }
92
94 {
95 return _mi[_size-1];
96 }
97
99 {
100 assert(i < _size);
101 return _mi[i];
102 }
103
105 {
106 assert(i < _size);
107 return _mi[i];
108 }
109
111 {
112 assert(s <= _mi.size());
113 _size = s;
114 }
115
117 {
118 assert(_size > 0);
119 return View(_mi,_size-1);
120 }
121
123 {
124 return _size;
125 }
126
127 bool empty() const
128 {
129 return _size == 0;
130 }
131
133 {
134 s << "(";
135 // fill up to maximum depth for consistent formatting
136 for (std::size_t i = mi.size(); i < max_depth; ++i)
137 s << " -";
138 for (typename ReservedVector<T,n>::const_iterator it = mi._mi.begin(); it != mi._mi.begin() + mi.size(); ++it)
139 s << std::setw(3) << *it;
140 s << ")";
141 return s;
142 }
143
144 private:
145 const MultiIndex& _mi;
146 size_type _size;
147
148 };
149
151 {}
152
154 : base_type(static_cast<const base_type&>(view._mi))
155 {
156 this->resize(view.size());
157 }
158
161 {}
162
164 template<std::size_t _n>
165 requires (n != _n)
167 {
168 assert(rv.size() <= n);
169 this->resize(rv.size());
170 for (std::size_t i = 0; i < std::min(n,_n); ++i)
171 (*this)[i] = rv[i];
172 }
173
175 template<class... U>
177 {
178 this->resize(tp.size());
179 Dune::Hybrid::forEach(tp.enumerate(),[&](auto i){
180 (*this)[i] = tp[i];
181 });
182 }
183
184
186 {
187 this->clear();
188 this->push_back(index);
189 }
190
193 return mi.front();
194 }
195
198 return mi.front();
199 }
200
203 return mi.back();
204 }
205
207 friend const_reference back(const MultiIndex& mi) {
208 return mi.back();
209 }
210
213 {
214 mi.push_back(t);
215 return mi;
216 }
217
219 void push_front(const value_type& t)
220 {
221 size_type sz = this->size() + 1;
222 this->resize(sz);
223 std::copy_backward(std::begin(*this), std::begin(*this)+sz-1, std::begin(*this)+sz);
224 (*this)[0] = t;
225 }
226
229 {
230 mi.push_front(t);
231 return mi;
232 }
233
236 {
237 mi.pop_back();
238 return mi;
239 }
240
243 {
244 size_type sz = this->size();
245 assert(not this->empty());
246 if (sz > 1)
247 std::copy(std::begin(*this)+1, std::begin(*this)+sz, std::begin(*this));
248 this->resize(--sz);
249 }
250
253 {
254 mi.pop_front();
255 return mi;
256 }
257
260 mi.back() += t;
261 return mi;
262 }
263
266 mi.front() += t;
267 return mi;
268 }
269
272 assert(head.size() + tail.size() <= MultiIndex::max_size());
273 size_type head_size = head.size();
274 head.resize(head.size() + tail.size());
275 std::copy(tail.cbegin(), tail.cend(), head.begin()+head_size);
276 return head;
277 }
278
281 if constexpr (MultiIndex::max_size() > 1)
282 std::reverse(rv.begin(),rv.end());
283 return rv;
284 }
285
288 {
289 s << "(";
290 // fill up to maximum depth for consistent formatting
291 for (std::size_t i = mi.size(); i < max_depth; ++i)
292 s << " -";
293 for (typename ReservedVector<T,n>::const_iterator it = mi.begin(); it != mi.end(); ++it)
294 s << std::setw(3) << *it;
295 s << ")";
296 return s;
297 }
298
299 View view() const
300 {
301 return View(*this,this->size());
302 }
303
305 {
306 return View(*this,size);
307 }
308
310
313 bool operator== (const MultiIndex& r) const
314 {
315 return
316 this->size() == r.size() &&
317 std::equal(this->begin(),this->end(),r.begin());
318 }
319
321 bool operator!= (const MultiIndex& r) const
322 {
323 return !(*this == r);
324 }
325
326#if 0
327 bool operator< (const MultiIndex& r) const
328 {
329 // FIXME: think about natural ordering
330 return _c.size() < _r.size();
331 return std::lexicographical_compare(_c.begin(),_c.end(),r._c.begin(),r._c.end());
332 }
333#endif
334
335 };
336
337
338 template<typename T, std::size_t n>
340 {
341 return hash_range(mi.begin(),mi.end());
342 }
343
344 template<class... U>
346
347 } // namespace PDELab
348} // namespace Dune
349
351
352#endif // DUNE_PDELAB_COMMON_MULTIINDEX_HH
#define DUNE_DEFINE_HASH(template_args, type)
#define DUNE_HASH_TYPE(...)
#define DUNE_HASH_TEMPLATE_ARGS(...)
constexpr void forEach(Range &&range, F &&f)
constexpr EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
std::size_t index
Definition interpolate.hh:97
std::size_t hash_range(It first, It last)
constexpr std::integer_sequence< T, II... > tail(std::integer_sequence< T, I0, II... >)
constexpr std::integral_constant< T, I0 > head(std::integer_sequence< T, I0, II... >)
const std::string s
Definition function.hh:843
std::size_t hash_value(const DOFIndex< T, n1, n2 > &di)
Definition dofindex.hh:334
For backward compatibility – Do not use this!
static constexpr std::size_t size()
static constexpr index_sequence enumerate()
storage_type::pointer pointer
static constexpr size_type max_size() noexcept
constexpr iterator end() noexcept
constexpr size_type size() const noexcept
constexpr bool empty() const noexcept
constexpr iterator begin() noexcept
storage_type::difference_type difference_type
constexpr void clear() noexcept
storage_type::const_iterator const_iterator
storage_type::size_type size_type
storage_type::const_reference const_reference
storage_type::value_type value_type
constexpr void resize(size_type s) noexcept
storage_type::reference reference
A class for representing multi-indices.
Definition multiindex.hh:31
friend MultiIndex pop_front(MultiIndex mi)
Returns a copy of a multi-index without the front element.
Definition multiindex.hh:252
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:287
friend reference back(MultiIndex &mi)
Returns the back element of the MultiIndex.
Definition multiindex.hh:202
friend MultiIndex reverse(MultiIndex rv)
Reverses the order of elements in the MultiIndex.
Definition multiindex.hh:280
friend MultiIndex accumulate_back(MultiIndex mi, const value_type &t)
Accumulates a value to the back element of the MultiIndex.
Definition multiindex.hh:259
MultiIndex(const View &view)
Definition multiindex.hh:153
friend reference front(MultiIndex &mi)
Returns the front element of the MultiIndex.
Definition multiindex.hh:192
MultiIndex(const ReservedVector< T, n > &rv)
Copy constructor from ReservedVector.
Definition multiindex.hh:160
friend const_reference front(const MultiIndex &mi)
Returns the front element of the MultiIndex.
Definition multiindex.hh:197
friend MultiIndex push_front(MultiIndex mi, const value_type &t)
Returns a copy of a multi-index with prepended element.
Definition multiindex.hh:228
friend const_reference back(const MultiIndex &mi)
Returns the back element of the MultiIndex.
Definition multiindex.hh:207
friend MultiIndex accumulate_front(MultiIndex mi, const value_type &t)
Accumulates a value to the front element of the MultiIndex.
Definition multiindex.hh:265
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition multiindex.hh:38
View view(std::size_t size) const
Definition multiindex.hh:304
bool operator==(const MultiIndex &r) const
Tests whether two MultiIndices are equal.
Definition multiindex.hh:313
MultiIndex(const MultiIndex< T, _n > &rv)
Copy constructor from a multi-index of different size.
Definition multiindex.hh:166
View view() const
Definition multiindex.hh:299
void pop_front()
Erases the last element of the vector, O(1) time.
Definition multiindex.hh:242
friend MultiIndex join(MultiIndex head, const MultiIndex &tail)
Concatenates two MultiIndices into a new MultiIndex, up to the maximum size n.
Definition multiindex.hh:271
friend MultiIndex push_back(MultiIndex mi, const value_type &t)
Returns a copy of a multi-index with appended element.
Definition multiindex.hh:212
void push_front(const value_type &t)
Appends an element to the beginning of a vector, up to the maximum size n, O(n) time.
Definition multiindex.hh:219
friend MultiIndex pop_back(MultiIndex mi)
Returns a copy of a multi-index without the back element.
Definition multiindex.hh:235
MultiIndex()
Definition multiindex.hh:150
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition multiindex.hh:321
MultiIndex(const TypeTree::HybridTreePath< U... > &tp)
Copy constructor from a hybrid multi-index.
Definition multiindex.hh:176
void set(typename ReservedVector< T, n >::value_type index)
Definition multiindex.hh:185
Definition multiindex.hh:46
reference back()
Definition multiindex.hh:88
reference front()
Definition multiindex.hh:78
base_type::const_reference reference
Definition multiindex.hh:57
friend std::ostream & operator<<(std::ostream &s, const View &mi)
Definition multiindex.hh:132
base_type::pointer pointer
Definition multiindex.hh:56
const_reference back() const
Definition multiindex.hh:93
base_type::value_type value_type
Definition multiindex.hh:55
size_type size() const
Definition multiindex.hh:122
void clear()
Definition multiindex.hh:73
base_type::const_iterator iterator
Definition multiindex.hh:61
const_reference operator[](size_type i) const
Definition multiindex.hh:104
const_reference front() const
Definition multiindex.hh:83
base_type::size_type size_type
Definition multiindex.hh:59
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition multiindex.hh:53
void resize(size_type s)
Definition multiindex.hh:110
base_type::const_iterator const_iterator
Definition multiindex.hh:62
base_type::const_reference const_reference
Definition multiindex.hh:58
bool empty() const
Definition multiindex.hh:127
reference operator[](size_type i)
Definition multiindex.hh:98
View back_popped() const
Definition multiindex.hh:116
base_type::difference_type difference_type
Definition multiindex.hh:60
T begin(T... args)
T copy_backward(T... args)
T copy(T... args)
T equal(T... args)
T lexicographical_compare(T... args)
T min(T... args)
T reverse(T... args)
T setw(T... args)