CppELib 1.7.0
Loading...
Searching...
No Matches
PreallocatedVector.h
Go to the documentation of this file.
1#ifndef CONTAINER_PREALLOCATED_VECTOR_H_INCLUDED
2#define CONTAINER_PREALLOCATED_VECTOR_H_INCLUDED
3
4#include <cstddef>
5#ifndef CPPELIB_NO_STD_ITERATOR
6#include <iterator>
7#endif
9#include "private/TypeTraits.h"
10#include "private/Construct.h"
11#include "Assertion/Assertion.h"
12
13namespace Container {
14
25template <typename T>
27public:
28 typedef T value_type;
29 typedef std::size_t size_type;
30 typedef std::ptrdiff_t difference_type;
32 typedef const value_type* const_iterator;
36 typedef const value_type* const_pointer;
37#ifndef CPPELIB_NO_STD_ITERATOR
38 typedef std::reverse_iterator<iterator> reverse_iterator;
39 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
40#endif
41
42private:
43 T* m_buf;
44 size_type m_buf_size;
45 size_type m_end;
46
47 class BadAlloc : public Container::BadAlloc {
48 public:
49 BadAlloc() : Container::BadAlloc() {}
50 const char* what() const CPPELIB_CONTAINER_NOEXCEPT
51 {
52 return "PreallocatedVector::BadAlloc";
53 }
54 };
55
57
58public:
64 : m_buf(0), m_buf_size(0U), m_end(0U)
65 {
66 }
67
78
87
97 {
98 if (m_buf != 0) {
99 return;
100 }
101 m_buf = static_cast<T*>(preallocated_buffer);
102 m_buf_size = buffer_size;
103 }
104
105 PreallocatedVector& operator=(const PreallocatedVector& x) // cppcheck-suppress operatorEqVarError
106 {
107 if (this != &x) {
108 assign(x.begin(), x.end());
109 }
110 return *this;
111 }
112
114 {
115 return m_end;
116 }
117
119 {
120 return m_buf_size / sizeof(T);
121 }
122
124 {
125 return max_size() - size();
126 }
127
128 bool empty() const
129 {
130 return size() == 0;
131 }
132
133 bool full() const
134 {
135 return size() == max_size();
136 }
137
138 void clear()
139 {
140 destroy_range(begin(), end());
141 m_end = 0U;
142 }
143
145 {
146 return *(begin() + idx);
147 }
148
150 {
151 return *(begin() + idx);
152 }
153
155 {
156 if (idx >= size()) {
157 CPPELIB_CONTAINER_THROW(OutOfRange("PreallocatedVector::at"));
158 }
159 return *(begin() + idx);
160 }
161
163 {
164 if (idx >= size()) {
165 CPPELIB_CONTAINER_THROW(OutOfRange("PreallocatedVector::at"));
166 }
167 return *(begin() + idx);
168 }
169
171 {
172 return begin();
173 }
174
176 {
177 return begin();
178 }
179
181 {
182 return &m_buf[0];
183 }
184
186 {
187 return &m_buf[0];
188 }
189
191 {
192 return begin() + m_end;
193 }
194
196 {
197 return begin() + m_end;
198 }
199
200#ifndef CPPELIB_NO_STD_ITERATOR
202 {
203 return reverse_iterator(end());
204 }
205
207 {
208 return const_reverse_iterator(end());
209 }
210
212 {
213 return reverse_iterator(begin());
214 }
215
217 {
219 }
220#endif
221
223 {
225 return *begin();
226 }
227
229 {
231 return *begin();
232 }
233
235 {
237 return *(end() - 1);
238 }
239
241 {
243 return *(end() - 1);
244 }
245
246 void resize(size_type n, const T& data = T())
247 {
248 if (size() >= n) {
249 destroy_range(begin() + n, end());
250 m_end = n;
251 return;
252 }
253
254 if (max_size() < n) {
255 CPPELIB_CONTAINER_THROW(BadAlloc());
256 }
257 const size_type rest = n - size();
258 for (size_type i = 0U; i < rest; ++i) {
260 }
261 }
262
263 void push_back(const T& data)
264 {
265 if (full()) {
266 CPPELIB_CONTAINER_THROW(BadAlloc());
267 }
268 construct(&*end(), data);
269 ++m_end;
270 }
271
272 void pop_back()
273 {
275 destroy(&*(end() - 1));
276 --m_end;
277 }
278
279 void assign(size_type n, const T& data)
280 {
281 if (max_size() < n) {
282 CPPELIB_CONTAINER_THROW(BadAlloc());
283 }
284 clear();
285 resize(n, data);
286 }
287
288 template <typename InputIterator>
290 {
291 size_type n = 0U;
292 for (InputIterator i = first; i != last; ++i) {
293 ++n;
294 }
295 if (max_size() < n) {
296 CPPELIB_CONTAINER_THROW(BadAlloc());
297 }
298 clear();
299 for (; first != last; ++first) {
301 }
302 }
303
305 {
306 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
307 insert_n(pos, 1U, data);
308 return pos;
309 }
310
312 {
313 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
314 insert_n(pos, n, data);
315 }
316
317 template <typename InputIterator>
319 {
320 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
322 insert_dispatch(pos, first, last, Integral());
323 }
324
326 {
327 DEBUG_ASSERT((begin() <= pos) && (pos < end()));
328 return erase(pos, pos + 1);
329 }
330
332 {
334 DEBUG_ASSERT((begin() <= first) && (first < end()));
335 DEBUG_ASSERT((begin() <= last) && (last <= end()));
336 const difference_type n = last - first;
337 for (iterator i = last; i != end(); ++i) {
338 *(i - n) = *i;
339 }
340 destroy_range(end() - n, end());
341 m_end -= n;
342 return first;
343 }
344
345private:
346 template <typename U>
348
349 template <typename Integer>
350 void insert_dispatch(iterator pos, Integer n, Integer data, TrueType)
351 {
352 insert_n(pos, static_cast<size_type>(n), static_cast<T>(data));
353 }
354
355 template <typename InputIterator>
357 {
358 insert_range(pos, first, last);
359 }
360
361 void insert_n(iterator pos, size_type n, const T& data)
362 {
363 if (available_size() < n) {
364 CPPELIB_CONTAINER_THROW(BadAlloc());
365 }
366
368 iterator old_end = end(); // cppcheck-suppress constVariablePointer
369 if (num_elems_pos_to_end > n) {
370 for (size_type i = 0U; i < n; ++i) {
371 construct(&*end());
372 ++m_end;
373 }
374 for (iterator it = old_end - 1; it != pos - 1; --it) {
375 *(it + n) = *it;
376 }
377 for (iterator it = pos; it != pos + n; ++it) {
378 *it = data;
379 }
380 } else {
381 for (size_type i = 0U; i < n - num_elems_pos_to_end; ++i) {
382 construct(&*end(), data);
383 ++m_end;
384 }
385 for (iterator it = pos; it != pos + num_elems_pos_to_end; ++it) {
386 construct(&*end(), *it);
387 ++m_end;
388 }
389 for (iterator it = pos; it != old_end; ++it) {
390 *it = data;
391 }
392 }
393 }
394
395 template <typename InputIterator>
396 void insert_range(iterator pos, InputIterator first, InputIterator last)
397 {
398 size_type n = 0U;
399 for (InputIterator i = first; i != last; ++i) {
400 ++n;
401 }
402 if (available_size() < n) {
403 CPPELIB_CONTAINER_THROW(BadAlloc());
404 }
405
407 iterator old_end = end(); // cppcheck-suppress constVariablePointer
408 if (num_elems_pos_to_end > n) {
409 for (size_type i = 0U; i < n; ++i) {
410 construct(&*end());
411 ++m_end;
412 }
413 for (iterator it = old_end - 1; it != pos - 1; --it) {
414 *(it + n) = *it;
415 }
416 for (; first != last; ++pos, ++first) {
417 *pos = *first;
418 }
419 } else {
421 for (size_type i = 0U; i < num_elems_pos_to_end; ++i) {
422 ++mid;
423 }
424 for (size_type i = 0U; i < n - num_elems_pos_to_end; ++i) {
425 construct(&*end(), *mid);
426 ++mid;
427 ++m_end;
428 }
429 for (iterator it = pos; it != pos + num_elems_pos_to_end; ++it) {
430 construct(&*end(), *it);
431 ++m_end;
432 }
433 for (iterator it = pos; it != old_end; ++it, ++first) {
434 *it = *first;
435 }
436 }
437 }
438
439};
440
441template <typename T>
443{
444 if (x.max_size() != y.max_size()) {
445 return false;
446 }
447 if (x.size() != y.size()) {
448 return false;
449 }
450 for (std::size_t i = 0U; i < x.size(); ++i) {
451 if (!(x[i] == y[i])) {
452 return false;
453 }
454 }
455 return true;
456}
457
458template <typename T>
460{
461 return !(x == y);
462}
463
464}
465
466#endif // CONTAINER_PREALLOCATED_VECTOR_H_INCLUDED
#define DEBUG_ASSERT(x)
The same as CHECK_ASSERT() macro.
Definition Assertion.h:39
Definition ContainerException.h:34
Definition ContainerException.h:23
STL-like vector container using pre-allocated buffer.
Definition PreallocatedVector.h:26
void assign(size_type n, const T &data)
Definition PreallocatedVector.h:279
const_reference at(size_type idx) const
Definition PreallocatedVector.h:162
std::size_t size_type
Definition PreallocatedVector.h:29
size_type available_size() const
Definition PreallocatedVector.h:123
const_reference operator[](size_type idx) const
Definition PreallocatedVector.h:149
~PreallocatedVector()
Destructor.
Definition PreallocatedVector.h:83
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition PreallocatedVector.h:39
T value_type
Definition PreallocatedVector.h:28
const_iterator begin() const
Definition PreallocatedVector.h:185
bool empty() const
Definition PreallocatedVector.h:128
void init(void *preallocated_buffer, size_type buffer_size)
Initialize.
Definition PreallocatedVector.h:96
const value_type * const_pointer
Definition PreallocatedVector.h:36
pointer data()
Definition PreallocatedVector.h:170
void assign(InputIterator first, InputIterator last)
Definition PreallocatedVector.h:289
size_type max_size() const
Definition PreallocatedVector.h:118
size_type size() const
Definition PreallocatedVector.h:113
PreallocatedVector & operator=(const PreallocatedVector &x)
Definition PreallocatedVector.h:105
void clear()
Definition PreallocatedVector.h:138
reference front()
Definition PreallocatedVector.h:222
reference at(size_type idx)
Definition PreallocatedVector.h:154
friend bool operator==(const PreallocatedVector< U > &x, const PreallocatedVector< U > &y)
reverse_iterator rbegin()
Definition PreallocatedVector.h:201
iterator erase(iterator first, iterator last)
Definition PreallocatedVector.h:331
const value_type & const_reference
Definition PreallocatedVector.h:34
const_reference back() const
Definition PreallocatedVector.h:240
iterator end()
Definition PreallocatedVector.h:190
const value_type * const_iterator
Definition PreallocatedVector.h:32
reverse_iterator rend()
Definition PreallocatedVector.h:211
void insert(iterator pos, InputIterator first, InputIterator last)
Definition PreallocatedVector.h:318
value_type * pointer
Definition PreallocatedVector.h:35
const_iterator end() const
Definition PreallocatedVector.h:195
const_pointer data() const
Definition PreallocatedVector.h:175
value_type & reference
Definition PreallocatedVector.h:33
void pop_back()
Definition PreallocatedVector.h:272
iterator erase(iterator pos)
Definition PreallocatedVector.h:325
void resize(size_type n, const T &data=T())
Definition PreallocatedVector.h:246
std::ptrdiff_t difference_type
Definition PreallocatedVector.h:30
value_type * iterator
Definition PreallocatedVector.h:31
bool full() const
Definition PreallocatedVector.h:133
void insert(iterator pos, size_type n, const T &data)
Definition PreallocatedVector.h:311
iterator insert(iterator pos, const T &data)
Definition PreallocatedVector.h:304
PreallocatedVector()
Default constructor.
Definition PreallocatedVector.h:63
reference operator[](size_type idx)
Definition PreallocatedVector.h:144
void push_back(const T &data)
Definition PreallocatedVector.h:263
const_reference front() const
Definition PreallocatedVector.h:228
iterator begin()
Definition PreallocatedVector.h:180
std::reverse_iterator< iterator > reverse_iterator
Definition PreallocatedVector.h:38
reference back()
Definition PreallocatedVector.h:234
const_reverse_iterator rend() const
Definition PreallocatedVector.h:216
const_reverse_iterator rbegin() const
Definition PreallocatedVector.h:206
PreallocatedVector(void *preallocated_buffer, size_type buffer_size)
Constructor.
Definition PreallocatedVector.h:74
Definition Array.h:10
bool operator==(const Array< T, Size > &x, const Array< T, Size > &y)
Definition Array.h:160
bool operator!=(const Array< T, Size > &x, const Array< T, Size > &y)
Definition Array.h:171