Dune Core Modules (2.4.2)

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#ifdef CHECK_RESERVEDVECTOR
17#define CHECKSIZE(X) assert(X)
18#else
19#define CHECKSIZE(X) {}
20#endif
21
22namespace Dune
23{
38 template<class T, int n>
40 {
41 public:
42
46 typedef T value_type;
48 typedef T* pointer;
50 typedef T& reference;
52 typedef const T& const_reference;
54 typedef size_t size_type;
56 typedef std::ptrdiff_t difference_type;
61
67 ReservedVector() : sz(0) {}
68
69 ReservedVector(std::initializer_list<T> const &l)
70 {
71 assert(l.size() <= n);// Actually, this is not needed any more!
72 sz = l.size();
73 std::copy_n(l.begin(), sz, data);
74 }
75
81 void clear()
82 {
83 sz = 0;
84 }
85
87 void resize(size_t s)
88 {
89 CHECKSIZE(s<=n);
90 sz = s;
91 }
92
94 void push_back(const T& t)
95 {
96 CHECKSIZE(sz<n);
97 data[sz++] = t;
98 }
99
101 void pop_back()
102 {
103 if (! empty()) sz--;
104 }
105
108 return iterator(*this, 0);
109 }
110
113 return const_iterator(*this, 0);
114 }
115
118 return iterator(*this, sz);
119 }
120
123 return const_iterator(*this, sz);
124 }
125
128 {
129 CHECKSIZE(sz>i);
130 return data[i];
131 }
132
135 {
136 CHECKSIZE(sz>i);
137 return data[i];
138 }
139
142 {
143 CHECKSIZE(sz>0);
144 return data[0];
145 }
146
149 {
150 CHECKSIZE(sz>0);
151 return data[0];
152 }
153
156 {
157 CHECKSIZE(sz>0);
158 return data[sz-1];
159 }
160
163 {
164 CHECKSIZE(sz>0);
165 return data[sz-1];
166 }
167
174 {
175 return sz;
176 }
177
179 bool empty() const
180 {
181 return sz==0;
182 }
183
186 {
187 return n;
188 }
189
192 {
193 return n;
194 }
195
199 friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
200 {
201 for (size_t i=0; i<v.size(); i++)
202 s << v[i] << " ";
203 return s;
204 }
205
206 private:
207 T data[n];
208 size_type sz;
209 };
210
211}
212
213#undef CHECKSIZE
214
215#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:40
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:117
ReservedVector()
Constructor.
Definition: reservedvector.hh:67
size_type size() const
Returns number of elements in the vector.
Definition: reservedvector.hh:173
T * pointer
Pointer to T.
Definition: reservedvector.hh:48
const_reference front() const
Returns const reference to first element of vector.
Definition: reservedvector.hh:148
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:112
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:94
bool empty() const
Returns true if vector has no elements.
Definition: reservedvector.hh:179
friend std::ostream & operator<<(std::ostream &s, const ReservedVector &v)
Send ReservedVector to an output stream.
Definition: reservedvector.hh:199
T value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:46
reference front()
Returns reference to first element of vector.
Definition: reservedvector.hh:141
static DUNE_CONSTEXPR size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition: reservedvector.hh:185
const_reference back() const
Returns const reference to last element of vector.
Definition: reservedvector.hh:162
void resize(size_t s)
Specifies a new size for the vector.
Definition: reservedvector.hh:87
void clear()
Erases all elements.
Definition: reservedvector.hh:81
reference back()
Returns reference to last element of vector.
Definition: reservedvector.hh:155
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:107
T & reference
Reference to T.
Definition: reservedvector.hh:50
const T & const_reference
Const reference to T.
Definition: reservedvector.hh:52
Dune::GenericIterator< const ReservedVector, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition: reservedvector.hh:60
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: reservedvector.hh:127
std::ptrdiff_t difference_type
A signed integral type.
Definition: reservedvector.hh:56
void pop_back()
Erases the last element of the vector, O(1) time.
Definition: reservedvector.hh:101
Dune::GenericIterator< ReservedVector, value_type > iterator
Iterator used to iterate through a vector.
Definition: reservedvector.hh:58
size_t size_type
An unsigned integral type.
Definition: reservedvector.hh:54
static DUNE_CONSTEXPR size_type max_size()
Returns the maximum length of the vector.
Definition: reservedvector.hh:191
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: reservedvector.hh:122
#define DUNE_CONSTEXPR
Set method or expression constexpr if supported by the compiler.
Definition: constexpr.hh:21
Implements a generic iterator class for writing stl conformant iterators.
Dune namespace.
Definition: alignment.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)