Dune Core Modules (2.6.0)

reservedvector.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_COMMON_RESERVEDVECTOR_HH
4#define DUNE_COMMON_RESERVEDVECTOR_HH
5
10#include <algorithm>
11#include <iostream>
12#include <cstddef>
14#include <initializer_list>
15
16#include <dune/common/hash.hh>
17
18#ifdef CHECK_RESERVEDVECTOR
19#define CHECKSIZE(X) assert(X)
20#else
21#define CHECKSIZE(X) {}
22#endif
23
24namespace Dune
25{
40 template<class T, int n>
42 {
43 public:
44
48 typedef T value_type;
50 typedef T* pointer;
52 typedef T& reference;
54 typedef const T& const_reference;
56 typedef size_t size_type;
58 typedef std::ptrdiff_t difference_type;
63
69 ReservedVector() : sz(0) {}
70
71 ReservedVector(std::initializer_list<T> const &l)
72 {
73 assert(l.size() <= n);// Actually, this is not needed any more!
74 sz = l.size();
75 std::copy_n(l.begin(), sz, data);
76 }
77
80 bool operator == (const ReservedVector & other) const
81 {
82 bool eq = (sz == other.sz);
83 for (size_type i=0; i<sz && eq; ++i)
84 eq = eq && (data[i] == other.data[i]);
85 return eq;
86 }
87
91 void clear()
92 {
93 sz = 0;
94 }
95
97 void resize(size_t s)
98 {
99 CHECKSIZE(s<=n);
100 sz = s;
101 }
102
104 void push_back(const T& t)
105 {
106 CHECKSIZE(sz<n);
107 data[sz++] = t;
108 }
109
111 void pop_back()
112 {
113 if (! empty()) sz--;
114 }
115
118 return iterator(*this, 0);
119 }
120
123 return const_iterator(*this, 0);
124 }
125
128 return iterator(*this, sz);
129 }
130
133 return const_iterator(*this, sz);
134 }
135
138 {
139 CHECKSIZE(sz>i);
140 return data[i];
141 }
142
145 {
146 CHECKSIZE(sz>i);
147 return data[i];
148 }
149
152 {
153 CHECKSIZE(sz>0);
154 return data[0];
155 }
156
159 {
160 CHECKSIZE(sz>0);
161 return data[0];
162 }
163
166 {
167 CHECKSIZE(sz>0);
168 return data[sz-1];
169 }
170
173 {
174 CHECKSIZE(sz>0);
175 return data[sz-1];
176 }
177
184 {
185 return sz;
186 }
187
189 bool empty() const
190 {
191 return sz==0;
192 }
193
195 static constexpr size_type capacity()
196 {
197 return n;
198 }
199
201 static constexpr size_type max_size()
202 {
203 return n;
204 }
205
209 friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
210 {
211 for (size_t i=0; i<v.size(); i++)
212 s << v[i] << " ";
213 return s;
214 }
215
216 inline friend std::size_t hash_value(const ReservedVector& v) noexcept
217 {
218 return hash_range(v.data,v.data+v.sz);
219 }
220
221 private:
222 T data[n];
223 size_type sz;
224 };
225
226}
227
229
230#undef CHECKSIZE
231
232#endif // DUNE_COMMON_RESERVEDVECTOR_HH
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:151
A Vector class with statically reserved memory.
Definition: reservedvector.hh:42
static constexpr size_type max_size()
Returns the maximum length of the vector.
Definition: reservedvector.hh:201
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:127
ReservedVector()
Constructor.
Definition: reservedvector.hh:69
static constexpr size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition: reservedvector.hh:195
size_type size() const
Returns number of elements in the vector.
Definition: reservedvector.hh:183
T * pointer
Pointer to T.
Definition: reservedvector.hh:50
const_reference front() const
Returns const reference to first element of vector.
Definition: reservedvector.hh:158
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:122
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:104
bool empty() const
Returns true if vector has no elements.
Definition: reservedvector.hh:189
friend std::ostream & operator<<(std::ostream &s, const ReservedVector &v)
Send ReservedVector to an output stream.
Definition: reservedvector.hh:209
T value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:48
reference front()
Returns reference to first element of vector.
Definition: reservedvector.hh:151
const_reference back() const
Returns const reference to last element of vector.
Definition: reservedvector.hh:172
void resize(size_t s)
Specifies a new size for the vector.
Definition: reservedvector.hh:97
void clear()
Erases all elements.
Definition: reservedvector.hh:91
reference back()
Returns reference to last element of vector.
Definition: reservedvector.hh:165
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:117
T & reference
Reference to T.
Definition: reservedvector.hh:52
const T & const_reference
Const reference to T.
Definition: reservedvector.hh:54
Dune::GenericIterator< const ReservedVector, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition: reservedvector.hh:62
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: reservedvector.hh:137
std::ptrdiff_t difference_type
A signed integral type.
Definition: reservedvector.hh:58
void pop_back()
Erases the last element of the vector, O(1) time.
Definition: reservedvector.hh:111
Dune::GenericIterator< ReservedVector, value_type > iterator
Iterator used to iterate through a vector.
Definition: reservedvector.hh:60
size_t size_type
An unsigned integral type.
Definition: reservedvector.hh:56
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: reservedvector.hh:132
Implements a generic iterator class for writing stl conformant iterators.
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:133
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:10
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)