DUNE PDELab (2.8)

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
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
49 typedef typename base_type::value_type value_type;
50 typedef typename base_type::pointer pointer;
53 typedef typename base_type::size_type size_type;
55 typedef typename base_type::const_iterator iterator;
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
87 const_reference back() const
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
104 void resize(size_type s)
105 {
106 assert(s <= _mi.size());
107 _size = s;
108 }
109
110 View back_popped() const
111 {
112 assert(_size > 0);
113 return View(_mi,_size-1);
114 }
115
116 size_type size() const
117 {
118 return _size;
119 }
120
121 bool empty() const
122 {
123 return _size == 0;
124 }
125
126 friend std::ostream& operator<< (std::ostream& s, const View& mi)
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
144 MultiIndex()
145 {}
146
147 MultiIndex(const View& view)
148 : base_type(static_cast<const base_type&>(view._mi))
149 {
150 this->resize(view.size());
151 }
152
153 void set(typename ReservedVector<T,n>::value_type index)
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
169 friend std::ostream& operator<< (std::ostream& s, const MultiIndex& mi)
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
186 View view(std::size_t size) const
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>
221 inline std::size_t hash_value(const MultiIndex<T,n>& mi)
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
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:151
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
static const std::size_t max_depth
The maximum possible depth of the MultiIndex.
Definition: multiindex.hh:36
bool operator==(const MultiIndex &r) const
Tests whether two MultiIndices are equal.
Definition: multiindex.hh:195
void pop_front()
Erases the first element of the multi-index, O(N) time.
Definition: multiindex.hh:160
size_t size_type
An unsigned integral type.
Definition: reservedvector.hh:57
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition: multiindex.hh:203
A Vector class with statically reserved memory.
Definition: reservedvector.hh:43
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:128
size_type size() const
Returns number of elements in the vector.
Definition: reservedvector.hh:184
T * pointer
Pointer to T.
Definition: reservedvector.hh:51
void push_back(const T &t)
Appends an element to the end of a vector, up to the maximum size n, O(1) time.
Definition: reservedvector.hh:105
bool empty() const
Returns true if vector has no elements.
Definition: reservedvector.hh:190
T value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:49
reference front()
Returns reference to first element of vector.
Definition: reservedvector.hh:152
void resize(size_t s)
Specifies a new size for the vector.
Definition: reservedvector.hh:98
void clear()
Erases all elements.
Definition: reservedvector.hh:92
reference back()
Returns reference to last element of vector.
Definition: reservedvector.hh:166
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:118
T & reference
Reference to T.
Definition: reservedvector.hh:53
const T & const_reference
Const reference to T.
Definition: reservedvector.hh:55
Dune::GenericIterator< const ReservedVector, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition: reservedvector.hh:63
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: reservedvector.hh:138
std::ptrdiff_t difference_type
A signed integral type.
Definition: reservedvector.hh:59
Dune::GenericIterator< ReservedVector, value_type > iterator
Iterator used to iterate through a vector.
Definition: reservedvector.hh:61
size_t size_type
An unsigned integral type.
Definition: reservedvector.hh:57
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:635
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:98
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition: hash.hh:115
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition: hash.hh:107
Dune namespace.
Definition: alignedallocator.hh:11
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:320
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  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)