CppELib 1.7.0
Loading...
Searching...
No Matches
FixedVector.h
Go to the documentation of this file.
1#ifndef CONTAINER_FIXED_VECTOR_H_INCLUDED
2#define CONTAINER_FIXED_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
26template <typename T, std::size_t MaxSize>
28public:
29 typedef T value_type;
30 typedef std::size_t size_type;
31 typedef std::ptrdiff_t difference_type;
33 typedef const value_type* const_iterator;
37 typedef const value_type* const_pointer;
38#ifndef CPPELIB_NO_STD_ITERATOR
39 typedef std::reverse_iterator<iterator> reverse_iterator;
40 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
41#endif
42
43private:
44 union InternalBuf {
45 double dummyForAlignment;
46 char buf[sizeof(T) * MaxSize];
47 };
48 InternalBuf m_realBuf;
49 T (&m_virtualBuf)[MaxSize];
50 size_type m_end;
51
52 class BadAlloc : public Container::BadAlloc {
53 public:
55 const char* what() const CPPELIB_CONTAINER_NOEXCEPT
56 {
57 return "FixedVector::BadAlloc";
58 }
59 };
60
61public:
63 : m_realBuf(), m_virtualBuf(*reinterpret_cast<T(*)[MaxSize]>(&m_realBuf)), m_end(0U)
64 {}
65
66 explicit FixedVector(size_type n, const T& data = T())
67 : m_realBuf(), m_virtualBuf(*reinterpret_cast<T(*)[MaxSize]>(&m_realBuf)), m_end(0U)
68 {
69 assign(n, data);
70 }
71
72 template <typename InputIterator>
74 : m_realBuf(), m_virtualBuf(*reinterpret_cast<T(*)[MaxSize]>(&m_realBuf)), m_end(0U)
75 {
77 }
78
80 : m_realBuf(), m_virtualBuf(*reinterpret_cast<T(*)[MaxSize]>(&m_realBuf)), m_end(0U)
81 {
82 assign(x.begin(), x.end());
83 }
84
86 {
88 }
89
91 {
92 if (this != &x) {
93 assign(x.begin(), x.end());
94 }
95 return *this;
96 }
97
99 {
100 return m_end;
101 }
102
104 {
105 return MaxSize;
106 }
107
109 {
110 return max_size() - size();
111 }
112
113 bool empty() const
114 {
115 return size() == 0;
116 }
117
118 bool full() const
119 {
120 return size() == max_size();
121 }
122
123 void clear()
124 {
125 destroy_range(begin(), end());
126 m_end = 0U;
127 }
128
130 {
131 return *(begin() + idx);
132 }
133
135 {
136 return *(begin() + idx);
137 }
138
140 {
141 if (idx >= size()) {
142 CPPELIB_CONTAINER_THROW(OutOfRange("FixedVector::at"));
143 }
144 return *(begin() + idx);
145 }
146
148 {
149 if (idx >= size()) {
150 CPPELIB_CONTAINER_THROW(OutOfRange("FixedVector::at"));
151 }
152 return *(begin() + idx);
153 }
154
156 {
157 return begin();
158 }
159
161 {
162 return begin();
163 }
164
166 {
167 return &m_virtualBuf[0];
168 }
169
171 {
172 return &m_virtualBuf[0];
173 }
174
176 {
177 return begin() + m_end;
178 }
179
181 {
182 return begin() + m_end;
183 }
184
185#ifndef CPPELIB_NO_STD_ITERATOR
187 {
188 return reverse_iterator(end());
189 }
190
192 {
193 return const_reverse_iterator(end());
194 }
195
197 {
198 return reverse_iterator(begin());
199 }
200
202 {
204 }
205#endif
206
208 {
210 return *begin();
211 }
212
214 {
216 return *begin();
217 }
218
220 {
222 return *(end() - 1);
223 }
224
226 {
228 return *(end() - 1);
229 }
230
231 void resize(size_type n, const T& data = T())
232 {
233 if (size() >= n) {
234 destroy_range(begin() + n, end());
235 m_end = n;
236 return;
237 }
238
239 if (max_size() < n) {
240 CPPELIB_CONTAINER_THROW(BadAlloc());
241 }
242 const size_type rest = n - size();
243 for (size_type i = 0U; i < rest; ++i) {
245 }
246 }
247
248 void push_back(const T& data)
249 {
250 if (full()) {
251 CPPELIB_CONTAINER_THROW(BadAlloc());
252 }
253 construct(&*end(), data);
254 ++m_end;
255 }
256
257 void pop_back()
258 {
260 destroy(&*(end() - 1));
261 --m_end;
262 }
263
264 void assign(size_type n, const T& data)
265 {
266 if (max_size() < n) {
267 CPPELIB_CONTAINER_THROW(BadAlloc());
268 }
269 clear();
270 resize(n, data);
271 }
272
273 template <typename InputIterator>
275 {
276 size_type n = 0U;
277 for (InputIterator i = first; i != last; ++i) {
278 ++n;
279 }
280 if (max_size() < n) {
281 CPPELIB_CONTAINER_THROW(BadAlloc());
282 }
283 clear();
284 for (; first != last; ++first) {
286 }
287 }
288
290 {
291 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
292 insert_n(pos, 1U, data);
293 return pos;
294 }
295
297 {
298 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
299 insert_n(pos, n, data);
300 }
301
302 template <typename InputIterator>
304 {
305 DEBUG_ASSERT((begin() <= pos) && (pos <= end()));
307 insert_dispatch(pos, first, last, Integral());
308 }
309
311 {
312 DEBUG_ASSERT((begin() <= pos) && (pos < end()));
313 return erase(pos, pos + 1);
314 }
315
317 {
319 DEBUG_ASSERT((begin() <= first) && (first < end()));
320 DEBUG_ASSERT((begin() <= last) && (last <= end()));
321 const difference_type n = last - first;
322 for (iterator i = last; i != end(); ++i) {
323 *(i - n) = *i;
324 }
325 destroy_range(end() - n, end());
326 m_end -= n;
327 return first;
328 }
329
330private:
331 template <typename U, std::size_t N>
332 friend bool operator==(const FixedVector<U, N>& x, const FixedVector<U, N>& y);
333
334 template <typename Integer>
335 void insert_dispatch(iterator pos, Integer n, Integer data, TrueType)
336 {
337 insert_n(pos, static_cast<size_type>(n), static_cast<T>(data));
338 }
339
340 template <typename InputIterator>
342 {
343 insert_range(pos, first, last);
344 }
345
346 void insert_n(iterator pos, size_type n, const T& data)
347 {
348 if (available_size() < n) {
349 CPPELIB_CONTAINER_THROW(BadAlloc());
350 }
351
353 iterator old_end = end(); // cppcheck-suppress constVariablePointer
354 if (num_elems_pos_to_end > n) {
355 for (size_type i = 0U; i < n; ++i) {
356 construct(&*end());
357 ++m_end;
358 }
359 for (iterator it = old_end - 1; it != pos - 1; --it) {
360 *(it + n) = *it;
361 }
362 for (iterator it = pos; it != pos + n; ++it) {
363 *it = data;
364 }
365 } else {
366 for (size_type i = 0U; i < n - num_elems_pos_to_end; ++i) {
367 construct(&*end(), data);
368 ++m_end;
369 }
370 for (iterator it = pos; it != pos + num_elems_pos_to_end; ++it) {
371 construct(&*end(), *it);
372 ++m_end;
373 }
374 for (iterator it = pos; it != old_end; ++it) {
375 *it = data;
376 }
377 }
378 }
379
380 template <typename InputIterator>
381 void insert_range(iterator pos, InputIterator first, InputIterator last)
382 {
383 size_type n = 0U;
384 for (InputIterator i = first; i != last; ++i) {
385 ++n;
386 }
387 if (available_size() < n) {
388 CPPELIB_CONTAINER_THROW(BadAlloc());
389 }
390
392 iterator old_end = end(); // cppcheck-suppress constVariablePointer
393 if (num_elems_pos_to_end > n) {
394 for (size_type i = 0U; i < n; ++i) {
395 construct(&*end());
396 ++m_end;
397 }
398 for (iterator it = old_end - 1; it != pos - 1; --it) {
399 *(it + n) = *it;
400 }
401 for (; first != last; ++pos, ++first) {
402 *pos = *first;
403 }
404 } else {
406 for (size_type i = 0U; i < num_elems_pos_to_end; ++i) {
407 ++mid;
408 }
409 for (size_type i = 0U; i < n - num_elems_pos_to_end; ++i) {
410 construct(&*end(), *mid);
411 ++mid;
412 ++m_end;
413 }
414 for (iterator it = pos; it != pos + num_elems_pos_to_end; ++it) {
415 construct(&*end(), *it);
416 ++m_end;
417 }
418 for (iterator it = pos; it != old_end; ++it, ++first) {
419 *it = *first;
420 }
421 }
422 }
423
424};
425
426template <typename T, std::size_t MaxSize>
428{
429 if (x.size() != y.size()) {
430 return false;
431 }
432 for (std::size_t i = 0U; i < x.size(); ++i) {
433 if (!(x[i] == y[i])) {
434 return false;
435 }
436 }
437 return true;
438}
439
440template <typename T, std::size_t MaxSize>
442{
443 return !(x == y);
444}
445
446}
447
448#endif // CONTAINER_FIXED_VECTOR_H_INCLUDED
#define DEBUG_ASSERT(x)
The same as CHECK_ASSERT() macro.
Definition Assertion.h:39
Definition ContainerException.h:34
STL-like vector container with fixed capacity.
Definition FixedVector.h:27
FixedVector & operator=(const FixedVector &x)
Definition FixedVector.h:90
value_type & reference
Definition FixedVector.h:34
const value_type & const_reference
Definition FixedVector.h:35
const value_type * const_iterator
Definition FixedVector.h:33
bool empty() const
Definition FixedVector.h:113
const value_type * const_pointer
Definition FixedVector.h:37
bool full() const
Definition FixedVector.h:118
void insert(iterator pos, size_type n, const T &data)
Definition FixedVector.h:296
T value_type
Definition FixedVector.h:29
reverse_iterator rbegin()
Definition FixedVector.h:186
const_reference operator[](size_type idx) const
Definition FixedVector.h:134
void insert(iterator pos, InputIterator first, InputIterator last)
Definition FixedVector.h:303
~FixedVector()
Definition FixedVector.h:85
std::size_t size_type
Definition FixedVector.h:30
pointer data()
Definition FixedVector.h:155
std::reverse_iterator< iterator > reverse_iterator
Definition FixedVector.h:39
void resize(size_type n, const T &data=T())
Definition FixedVector.h:231
void pop_back()
Definition FixedVector.h:257
const_reverse_iterator rbegin() const
Definition FixedVector.h:191
const_reference at(size_type idx) const
Definition FixedVector.h:147
size_type max_size() const
Definition FixedVector.h:103
size_type available_size() const
Definition FixedVector.h:108
iterator end()
Definition FixedVector.h:175
const_iterator end() const
Definition FixedVector.h:180
reference back()
Definition FixedVector.h:219
void push_back(const T &data)
Definition FixedVector.h:248
const_reverse_iterator rend() const
Definition FixedVector.h:201
const_pointer data() const
Definition FixedVector.h:160
FixedVector()
Definition FixedVector.h:62
iterator insert(iterator pos, const T &data)
Definition FixedVector.h:289
reference at(size_type idx)
Definition FixedVector.h:139
void clear()
Definition FixedVector.h:123
FixedVector(const FixedVector &x)
Definition FixedVector.h:79
value_type * iterator
Definition FixedVector.h:32
FixedVector(size_type n, const T &data=T())
Definition FixedVector.h:66
void assign(size_type n, const T &data)
Definition FixedVector.h:264
reverse_iterator rend()
Definition FixedVector.h:196
iterator erase(iterator first, iterator last)
Definition FixedVector.h:316
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition FixedVector.h:40
size_type size() const
Definition FixedVector.h:98
const_reference back() const
Definition FixedVector.h:225
reference operator[](size_type idx)
Definition FixedVector.h:129
void assign(InputIterator first, InputIterator last)
Definition FixedVector.h:274
std::ptrdiff_t difference_type
Definition FixedVector.h:31
friend bool operator==(const FixedVector< U, N > &x, const FixedVector< U, N > &y)
const_reference front() const
Definition FixedVector.h:213
iterator erase(iterator pos)
Definition FixedVector.h:310
const_iterator begin() const
Definition FixedVector.h:170
iterator begin()
Definition FixedVector.h:165
reference front()
Definition FixedVector.h:207
value_type * pointer
Definition FixedVector.h:36
FixedVector(InputIterator first, InputIterator last)
Definition FixedVector.h:73
Definition ContainerException.h:23
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