dune-common  2.3.1-rc1
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 RESERVEDVECTOR_HH
4 #define RESERVEDVECTOR_HH
5 
10 #include <iostream>
12 
13 #ifdef CHECK_RESERVEDVECTOR
14 #define CHECKSIZE(X) assert(X)
15 #else
16 #define CHECKSIZE(X) {}
17 #endif
18 
19 namespace Dune
20 {
35  template<class T, int n>
37  {
38  public:
39 
42  typedef T value_type;
45  typedef T* pointer;
47  typedef T& reference;
49  typedef const T& const_reference;
51  typedef size_t size_type;
53  typedef std::ptrdiff_t difference_type;
58 
63  ReservedVector() : sz(0) {}
65 
70  void clear()
72  {
73  sz = 0;
74  }
75 
77  void resize(size_t s)
78  {
79  CHECKSIZE(s<=n);
80  sz = s;
81  }
82 
84  void push_back(const T& t)
85  {
86  CHECKSIZE(sz<n);
87  data[sz++] = t;
88  }
89 
91  void pop_back()
92  {
93  if (! empty()) sz--;
94  }
95 
98  return iterator(*this, 0);
99  }
100 
103  return const_iterator(*this, 0);
104  }
105 
108  return iterator(*this, sz);
109  }
110 
112  const_iterator end() const {
113  return const_iterator(*this, sz);
114  }
115 
118  {
119  CHECKSIZE(sz>i);
120  return data[i];
121  }
122 
125  {
126  CHECKSIZE(sz>i);
127  return data[i];
128  }
129 
132  {
133  CHECKSIZE(sz>0);
134  return data[0];
135  }
136 
139  {
140  CHECKSIZE(sz>0);
141  return data[0];
142  }
143 
146  {
147  CHECKSIZE(sz>0);
148  return data[sz-1];
149  }
150 
153  {
154  CHECKSIZE(sz>0);
155  return data[sz-1];
156  }
157 
162  size_type size () const
164  {
165  return sz;
166  }
167 
169  bool empty() const
170  {
171  return sz==0;
172  }
173 
176  {
177  return n;
178  }
179 
182  {
183  return n;
184  }
185 
188  friend std::ostream& operator<< (std::ostream& s, const ReservedVector& v)
190  {
191  for (size_t i=0; i<v.size(); i++)
192  s << v[i] << " ";
193  return s;
194  }
195 
196  private:
197  T data[n];
198  size_type sz;
199  };
200 
201 }
202 
203 #undef CHECKSIZE
204 
205 #endif // RESERVEDVECTOR_HH
void pop_back()
Erases the last element of the vector, O(1) time.
Definition: reservedvector.hh:91
const T & const_reference
Const reference to T.
Definition: reservedvector.hh:49
reference front()
Returns reference to first element of vector.
Definition: reservedvector.hh:131
reference back()
Returns reference to last element of vector.
Definition: reservedvector.hh:145
size_type size() const
Returns number of elements in the vector.
Definition: reservedvector.hh:163
Implements a generic iterator class for writing stl conformant iterators.
A Vector class with statically reserved memory.
Definition: reservedvector.hh:36
T & reference
Reference to T.
Definition: reservedvector.hh:47
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: reservedvector.hh:117
static size_type max_size()
Returns the maximum length of the vector.
Definition: reservedvector.hh:181
const_reference back() const
Returns const reference to last element of vector.
Definition: reservedvector.hh:152
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:84
static size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition: reservedvector.hh:175
bool empty() const
Returns true if vector has no elements.
Definition: reservedvector.hh:169
T t
Definition: alignment.hh:38
void resize(size_t s)
Specifies a new size for the vector.
Definition: reservedvector.hh:77
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: reservedvector.hh:112
friend std::ostream & operator<<(std::ostream &s, const ReservedVector &v)
Send ReservedVector to an output stream.
Definition: reservedvector.hh:189
#define CHECKSIZE(X)
Definition: reservedvector.hh:16
size_t size_type
An unsigned integral type.
Definition: reservedvector.hh:51
const_reference front() const
Returns const reference to first element of vector.
Definition: reservedvector.hh:138
Generic class for stl-conforming iterators for container classes with operator[]. ...
Definition: genericiterator.hh:150
ReservedVector()
Constructor.
Definition: reservedvector.hh:64
std::ptrdiff_t difference_type
A signed integral type.
Definition: reservedvector.hh:53
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:102
T value_type
The type of object, T, stored in the vector.
Definition: reservedvector.hh:43
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reservedvector.hh:107
Dune::GenericIterator< const ReservedVector, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition: reservedvector.hh:57
void clear()
Erases all elements.
Definition: reservedvector.hh:71
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: reservedvector.hh:97
Dune::GenericIterator< ReservedVector, value_type > iterator
Iterator used to iterate through a vector.
Definition: reservedvector.hh:55
T * pointer
Pointer to T.
Definition: reservedvector.hh:45