DUNE PDELab (unstable)

multiindex.hh
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/typetree/treepath.hh>
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
55 typedef typename base_type::value_type value_type;
56 typedef typename base_type::pointer pointer;
59 typedef typename base_type::size_type size_type;
61 typedef typename base_type::const_iterator iterator;
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
93 const_reference back() const
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
110 void resize(size_type s)
111 {
112 assert(s <= _mi.size());
113 _size = s;
114 }
115
116 View back_popped() const
117 {
118 assert(_size > 0);
119 return View(_mi,_size-1);
120 }
121
122 size_type size() const
123 {
124 return _size;
125 }
126
127 bool empty() const
128 {
129 return _size == 0;
130 }
131
132 friend std::ostream& operator<< (std::ostream& s, const View& mi)
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
150 MultiIndex()
151 {}
152
153 MultiIndex(const View& view)
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
185 void set(typename ReservedVector<T,n>::value_type index)
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
287 friend std::ostream& operator<< (std::ostream& s, const MultiIndex& mi)
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
304 View view(std::size_t size) const
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>
339 inline std::size_t hash_value(const MultiIndex<T,n>& mi)
340 {
341 return hash_range(mi.begin(),mi.end());
342 }
343
344 template<class... U>
345 MultiIndex(const TypeTree::HybridTreePath<U...>& tp) -> MultiIndex<std::common_type_t<U...>,sizeof...(U)>;
346
347 } // namespace PDELab
348} // namespace Dune
349
351
352#endif // DUNE_PDELAB_COMMON_MULTIINDEX_HH
A hybrid multi-index class that supports both compile time and run time indices.
Definition: hybridmultiindex.hh:81
static constexpr std::size_t size()
Get the size (length) of this multi-index.
Definition: hybridmultiindex.hh:122
static constexpr index_sequence enumerate()
Returns an index_sequence for enumerating the components of this HybridMultiIndex.
Definition: hybridmultiindex.hh:116
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
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
storage_type::size_type size_type
An unsigned integral type.
Definition: reservedvector.hh:67
storage_type::const_reference const_reference
Const reference to T.
Definition: reservedvector.hh:65
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:38
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
storage_type::value_type value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:57
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
storage_type::reference reference
Reference to T.
Definition: reservedvector.hh:63
friend MultiIndex pop_back(MultiIndex mi)
Returns a copy of a multi-index without the back element.
Definition: multiindex.hh:235
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
A Vector class with statically reserved memory.
Definition: reservedvector.hh:49
storage_type::pointer pointer
Pointer to T.
Definition: reservedvector.hh:59
static constexpr size_type max_size() noexcept
Returns the maximum length of the vector.
Definition: reservedvector.hh:379
constexpr iterator end() noexcept
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:247
constexpr size_type size() const noexcept
Returns number of elements in the vector.
Definition: reservedvector.hh:361
constexpr bool empty() const noexcept
Returns true if vector has no elements.
Definition: reservedvector.hh:367
constexpr iterator begin() noexcept
Returns a iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:211
storage_type::iterator iterator
Iterator used to iterate through a vector.
Definition: reservedvector.hh:71
storage_type::difference_type difference_type
A signed integral type.
Definition: reservedvector.hh:69
constexpr void clear() noexcept
Erases all elements.
Definition: reservedvector.hh:155
storage_type::const_iterator const_iterator
Const iterator used to iterate through a vector.
Definition: reservedvector.hh:73
storage_type::size_type size_type
An unsigned integral type.
Definition: reservedvector.hh:67
storage_type::const_reference const_reference
Const reference to T.
Definition: reservedvector.hh:65
constexpr reference back() noexcept
Returns reference to last element of vector.
Definition: reservedvector.hh:331
storage_type::value_type value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:57
constexpr reference operator[](size_type i) noexcept
Returns reference to the i'th element.
Definition: reservedvector.hh:303
constexpr reference front() noexcept
Returns reference to first element of vector.
Definition: reservedvector.hh:317
constexpr void resize(size_type s) noexcept
Specifies a new size for the vector.
Definition: reservedvector.hh:161
storage_type::reference reference
Reference to T.
Definition: reservedvector.hh:63
constexpr void forEach(Range &&range, F &&f)
Range based for loop.
Definition: hybridutilities.hh:257
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:507
constexpr EnableIfInterOperable< T1, T2, bool >::type operator<(const RandomAccessIteratorFacade< T1, V1, R1, D > &lhs, const RandomAccessIteratorFacade< T2, V2, R2, D > &rhs)
Comparison operator.
Definition: iteratorfacades.hh:638
Support for calculating hash values of objects.
#define DUNE_DEFINE_HASH(template_args, type)
Defines the required struct specialization to make type hashable via Dune::hash.
Definition: hash.hh:100
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition: hash.hh:117
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition: hash.hh:109
Dune namespace.
Definition: alignedallocator.hh:13
std::size_t hash_range(It first, It last)
Hashes all elements in the range [first,last) and returns the combined hash.
Definition: hash.hh:322
constexpr std::integer_sequence< T, II... > tail(std::integer_sequence< T, I0, II... >)
For a sequence [head,tail...) return the tail sequence.
Definition: integersequence.hh:58
constexpr std::integral_constant< T, I0 > head(std::integer_sequence< T, I0, II... >)
For a sequence [head,tail...) return the single head element.
Definition: integersequence.hh:53
An stl-compliant random-access container which stores everything on the stack.
Helper classes to provide indices for geometrytypes for use in a vector.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Sep 4, 22:38, 2025)