dune-pdelab 2.9
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
8
9#include <dune/common/hash.hh>
10
11#include <algorithm>
12#include <iomanip>
13
14namespace Dune {
15
16 namespace PDELab {
17
18
20
26 template<typename T, std::size_t n>
28 : public ReservedVector<T,n>
29 {
30
32
33 public:
34
36 static const std::size_t max_depth = n;
37 using typename base_type::size_type;
38
39 class View
40 {
41
42 friend class MultiIndex;
43
44 public:
45
47 static const std::size_t max_depth = n;
48
50 typedef typename base_type::pointer pointer;
57
58 private:
59
60 View(const MultiIndex& mi, size_type size)
61 : _mi(mi)
62 , _size(size)
63 {}
64
65 public:
66
67 void clear()
68 {
69 _size = 0;
70 }
71
73 {
74 return _mi.front();
75 }
76
78 {
79 return _mi.front();
80 }
81
83 {
84 return _mi[_size-1];
85 }
86
88 {
89 return _mi[_size-1];
90 }
91
93 {
94 assert(i < _size);
95 return _mi[i];
96 }
97
99 {
100 assert(i < _size);
101 return _mi[i];
102 }
103
105 {
106 assert(s <= _mi.size());
107 _size = s;
108 }
109
111 {
112 assert(_size > 0);
113 return View(_mi,_size-1);
114 }
115
117 {
118 return _size;
119 }
120
121 bool empty() const
122 {
123 return _size == 0;
124 }
125
127 {
128 s << "(";
129 // fill up to maximum depth for consistent formatting
130 for (std::size_t i = mi.size(); i < max_depth; ++i)
131 s << " -";
132 for (typename ReservedVector<T,n>::const_iterator it = mi._mi.begin(); it != mi._mi.begin() + mi.size(); ++it)
133 s << std::setw(3) << *it;
134 s << ")";
135 return s;
136 }
137
138 private:
139 const MultiIndex& _mi;
140 size_type _size;
141
142 };
143
145 {}
146
148 : base_type(static_cast<const base_type&>(view._mi))
149 {
150 this->resize(view.size());
151 }
152
154 {
155 this->clear();
156 this->push_back(index);
157 }
158
161 {
162 size_type sz = this->size();
163 assert(not this->empty());
164 std::copy(std::begin(*this)+1, std::begin(*this)+sz, std::begin(*this));
165 this->resize(--sz);
166 }
167
170 {
171 s << "(";
172 // fill up to maximum depth for consistent formatting
173 for (std::size_t i = mi.size(); i < max_depth; ++i)
174 s << " -";
175 for (typename ReservedVector<T,n>::const_iterator it = mi.begin(); it != mi.end(); ++it)
176 s << std::setw(3) << *it;
177 s << ")";
178 return s;
179 }
180
181 View view() const
182 {
183 return View(*this,this->size());
184 }
185
187 {
188 return View(*this,size);
189 }
190
192
195 bool operator== (const MultiIndex& r) const
196 {
197 return
198 this->size() == r.size() &&
199 std::equal(this->begin(),this->end(),r.begin());
200 }
201
203 bool operator!= (const MultiIndex& r) const
204 {
205 return !(*this == r);
206 }
207
208#if 0
209 bool operator< (const MultiIndex& r) const
210 {
211 // FIXME: think about natural ordering
212 return _c.size() < _r.size();
213 return std::lexicographical_compare(_c.begin(),_c.end(),r._c.begin(),r._c.end());
214 }
215#endif
216
217 };
218
219
220 template<typename T, std::size_t n>
222 {
223 return hash_range(mi.begin(),mi.end());
224 }
225
226
227 } // namespace PDELab
228} // namespace Dune
229
231
232#endif // DUNE_PDELAB_COMMON_MULTIINDEX_HH
#define DUNE_DEFINE_HASH(template_args, type)
#define DUNE_HASH_TYPE(...)
#define DUNE_HASH_TEMPLATE_ARGS(...)
std::size_t hash_range(It first, It last)
std::size_t index
Definition interpolate.hh:97
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!
storage_type::pointer pointer
constexpr iterator end() noexcept
constexpr bool operator<(const ReservedVector &that) const noexcept
constexpr size_type size() const noexcept
constexpr bool empty() const noexcept
constexpr iterator begin() noexcept
constexpr void push_back(const value_type &t) noexcept(std::is_nothrow_copy_assignable_v< value_type >)
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 reference front() noexcept
constexpr void resize(size_type s) noexcept
A class for representing multi-indices.
Definition multiindex.hh:29
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:169
MultiIndex(const View &view)
Definition multiindex.hh:147
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:186
bool operator==(const MultiIndex &r) const
Tests whether two MultiIndices are equal.
Definition multiindex.hh:195
View view() const
Definition multiindex.hh:181
void pop_front()
Erases the first element of the multi-index, O(N) time.
Definition multiindex.hh:160
MultiIndex()
Definition multiindex.hh:144
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition multiindex.hh:203
void set(typename ReservedVector< T, n >::value_type index)
Definition multiindex.hh:153
Definition multiindex.hh:40
reference back()
Definition multiindex.hh:82
reference front()
Definition multiindex.hh:72
base_type::const_reference reference
Definition multiindex.hh:51
friend std::ostream & operator<<(std::ostream &s, const View &mi)
Definition multiindex.hh:126
base_type::pointer pointer
Definition multiindex.hh:50
const_reference back() const
Definition multiindex.hh:87
base_type::value_type value_type
Definition multiindex.hh:49
size_type size() const
Definition multiindex.hh:116
void clear()
Definition multiindex.hh:67
base_type::const_iterator iterator
Definition multiindex.hh:55
const_reference operator[](size_type i) const
Definition multiindex.hh:98
const_reference front() const
Definition multiindex.hh:77
base_type::size_type size_type
Definition multiindex.hh:53
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition multiindex.hh:47
void resize(size_type s)
Definition multiindex.hh:104
base_type::const_iterator const_iterator
Definition multiindex.hh:56
base_type::const_reference const_reference
Definition multiindex.hh:52
bool empty() const
Definition multiindex.hh:121
reference operator[](size_type i)
Definition multiindex.hh:92
View back_popped() const
Definition multiindex.hh:110
base_type::difference_type difference_type
Definition multiindex.hh:54
T begin(T... args)
T copy(T... args)
T equal(T... args)
T lexicographical_compare(T... args)
T setw(T... args)