Dune Core Modules (2.8.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{
41 template<class T, int n>
43 {
44 public:
45
49 typedef T value_type;
51 typedef T* pointer;
53 typedef T& reference;
55 typedef const T& const_reference;
57 typedef size_t size_type;
59 typedef std::ptrdiff_t difference_type;
64
70 ReservedVector() = default;
71
72 ReservedVector(std::initializer_list<T> const &l)
73 {
74 assert(l.size() <= n);// Actually, this is not needed any more!
75 sz = l.size();
76 std::copy_n(l.begin(), sz, data);
77 }
78
81 bool operator == (const ReservedVector & other) const
82 {
83 bool eq = (sz == other.sz);
84 for (size_type i=0; i<sz && eq; ++i)
85 eq = eq && (data[i] == other.data[i]);
86 return eq;
87 }
88
92 void clear()
93 {
94 sz = 0;
95 }
96
98 void resize(size_t s)
99 {
100 CHECKSIZE(s<=n);
101 sz = s;
102 }
103
105 void push_back(const T& t)
106 {
107 CHECKSIZE(sz<n);
108 data[sz++] = t;
109 }
110
112 void pop_back()
113 {
114 if (! empty()) sz--;
115 }
116
119 return iterator(*this, 0);
120 }
121
124 return const_iterator(*this, 0);
125 }
126
129 return iterator(*this, sz);
130 }
131
134 return const_iterator(*this, sz);
135 }
136
139 {
140 CHECKSIZE(sz>i);
141 return data[i];
142 }
143
146 {
147 CHECKSIZE(sz>i);
148 return data[i];
149 }
150
153 {
154 CHECKSIZE(sz>0);
155 return data[0];
156 }
157
160 {
161 CHECKSIZE(sz>0);
162 return data[0];
163 }
164
167 {
168 CHECKSIZE(sz>0);
169 return data[sz-1];
170 }
171
174 {
175 CHECKSIZE(sz>0);
176 return data[sz-1];
177 }
178
185 {
186 return sz;
187 }
188
190 bool empty() const
191 {
192 return sz==0;
193 }
194
196 static constexpr size_type capacity()
197 {
198 return n;
199 }
200
202 static constexpr size_type max_size()
203 {
204 return n;
205 }
206
210 friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
211 {
212 for (size_t i=0; i<v.size(); i++)
213 s << v[i] << " ";
214 return s;
215 }
216
217 inline friend std::size_t hash_value(const ReservedVector& v) noexcept
218 {
219 return hash_range(v.data,v.data+v.sz);
220 }
221
222 private:
223 T data[n] = {};
224 size_type sz = 0;
225 };
226
227}
228
230
231#undef CHECKSIZE
232
233#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:43
ReservedVector()=default
Constructor.
static constexpr size_type max_size()
Returns the maximum length of the vector.
Definition: reservedvector.hh:202
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:128
static constexpr size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition: reservedvector.hh:196
size_type size() const
Returns number of elements in the vector.
Definition: reservedvector.hh:184
T * pointer
Pointer to T.
Definition: reservedvector.hh:51
const_reference front() const
Returns const reference to first element of vector.
Definition: reservedvector.hh:159
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:123
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
friend std::ostream & operator<<(std::ostream &s, const ReservedVector &v)
Send ReservedVector to an output stream.
Definition: reservedvector.hh:210
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
const_reference back() const
Returns const reference to last element of vector.
Definition: reservedvector.hh:173
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
void pop_back()
Erases the last element of the vector, O(1) time.
Definition: reservedvector.hh:112
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
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: reservedvector.hh:133
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:142
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)