DUNE PDELab (2.7)

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
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;
52 typedef typename base_type::size_type size_type;
54 typedef typename base_type::const_iterator 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
86 const_reference back() const
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
103 void resize(size_type s)
104 {
105 assert(s <= _mi.size());
106 _size = s;
107 }
108
109 View back_popped() const
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
143 MultiIndex()
144 {}
145
146 MultiIndex(const View& view)
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
221
222#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:159
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:185
bool operator!=(const MultiIndex &r) const
Tests whether two MultiIndices are not equal.
Definition: multiindex.hh:193
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:14
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 (Jul 15, 22:36, 2024)