Embedded Template Library 1.0
Loading...
Searching...
No Matches
pvoidvector.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2016 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_PVOIDVECTOR_INCLUDED
32#define ETL_PVOIDVECTOR_INCLUDED
33
34#define ETL_IN_PVOIDVECTOR
35
36#include "../platform.h"
37#include "../algorithm.h"
38#include "vector_base.h"
39#include "../type_traits.h"
40#include "../error_handler.h"
41#include "../functional.h"
42#include "../iterator.h"
43
44#include <stddef.h>
45
46#include "minmax_push.h"
47
48namespace etl
49{
50 //***************************************************************************
53 //***************************************************************************
54 class pvoidvector : public vector_base
55 {
56 public:
57
58 typedef void* value_type;
59 typedef value_type& reference;
60 typedef const value_type& const_reference;
61 typedef value_type* pointer;
62 typedef const value_type* const_pointer;
63 typedef value_type* iterator;
64 typedef const value_type* const_iterator;
65 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
66 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
67 typedef size_t size_type;
68 typedef etl::iterator_traits<iterator>::difference_type difference_type;
69
70 public:
71
72 //*********************************************************************
75 //*********************************************************************
76 iterator begin()
77 {
78 return p_buffer;
79 }
80
81 //*********************************************************************
84 //*********************************************************************
85 const_iterator begin() const
86 {
87 return const_iterator(p_buffer);
88 }
89
90 //*********************************************************************
93 //*********************************************************************
94 iterator end()
95 {
96 return p_end;
97 }
98
99 //*********************************************************************
102 //*********************************************************************
103 const_iterator end() const
104 {
105 return const_iterator(p_end);
106 }
107
108 //*********************************************************************
111 //*********************************************************************
112 const_iterator cbegin() const
113 {
114 return const_iterator(p_buffer);
115 }
116
117 //*********************************************************************
120 //*********************************************************************
121 const_iterator cend() const
122 {
123 return const_iterator(p_end);
124 }
125
126 //*********************************************************************
129 //*********************************************************************
130 reverse_iterator rbegin()
131 {
132 return reverse_iterator(end());
133 }
134
135 //*********************************************************************
138 //*********************************************************************
139 const_reverse_iterator rbegin() const
140 {
141 return const_reverse_iterator(end());
142 }
143
144 //*********************************************************************
147 //*********************************************************************
148 reverse_iterator rend()
149 {
150 return reverse_iterator(begin());
151 }
152
153 //*********************************************************************
156 //*********************************************************************
157 const_reverse_iterator rend() const
158 {
159 return const_reverse_iterator(begin());
160 }
161
162 //*********************************************************************
165 //*********************************************************************
166 const_reverse_iterator crbegin() const
167 {
168 return const_reverse_iterator(cend());
169 }
170
171 //*********************************************************************
174 //*********************************************************************
175 const_reverse_iterator crend() const
176 {
177 return const_reverse_iterator(cbegin());
178 }
179
180 //*********************************************************************
185 //*********************************************************************
186 void resize(size_t new_size)
187 {
188 ETL_ASSERT_OR_RETURN(new_size <= CAPACITY, ETL_ERROR(vector_full));
189
190 p_end = p_buffer + new_size;
191 }
192
193 //*********************************************************************
199 //*********************************************************************
200 void resize(size_t new_size, value_type value)
201 {
202 ETL_ASSERT_OR_RETURN(new_size <= CAPACITY, ETL_ERROR(vector_full));
203
204 pointer p_new_end = p_buffer + new_size;
205
206 // Size up if necessary.
207 if (p_end < p_new_end)
208 {
209 etl::fill(p_end, p_new_end, value);
210 }
211
212 p_end = p_new_end;
213 }
214
215 //*********************************************************************
218 //*********************************************************************
220 {
221 ETL_ASSERT_OR_RETURN(new_size <= CAPACITY, ETL_ERROR(vector_full));
222
223 p_end = p_buffer + new_size;
224 }
225
226 //*********************************************************************
230 //*********************************************************************
232 {
233 return p_buffer[i];
234 }
235
236 //*********************************************************************
240 //*********************************************************************
242 {
243 return p_buffer[i];
244 }
245
246 //*********************************************************************
251 //*********************************************************************
252 reference at(size_t i)
253 {
254 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
255 return p_buffer[i];
256 }
257
258 //*********************************************************************
263 //*********************************************************************
264 const_reference at(size_t i) const
265 {
266 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
267 return p_buffer[i];
268 }
269
270 //*********************************************************************
273 //*********************************************************************
275 {
276 return p_buffer[0];
277 }
278
279 //*********************************************************************
282 //*********************************************************************
284 {
285 return p_buffer[0];
286 }
287
288 //*********************************************************************
291 //*********************************************************************
293 {
294 return *(p_end - 1);
295 }
296
297 //*********************************************************************
300 //*********************************************************************
302 {
303 return *(p_end - 1);
304 }
305
306 //*********************************************************************
309 //*********************************************************************
311 {
312 return p_buffer;
313 }
314
315 //*********************************************************************
318 //*********************************************************************
320 {
321 return p_buffer;
322 }
323
324 //*********************************************************************
330 //*********************************************************************
331 template <typename TIterator>
334 {
335#if ETL_IS_DEBUG_BUILD
336 difference_type d = etl::distance(first, last);
337 ETL_ASSERT_OR_RETURN(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
338#endif
339
340 initialise();
341
342 while (first != last)
343 {
344 *p_end++ = (void*)(*first);
345 ++first;
346 }
347 }
348
349 //*********************************************************************
355 //*********************************************************************
356 template <typename TIterator>
359 {
360#if ETL_IS_DEBUG_BUILD
361 difference_type d = etl::distance(first, last);
362 ETL_ASSERT_OR_RETURN(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
363#endif
364
365 initialise();
366
367 void** p_first = (void**)(first);
368 void** p_last = (void**)(last);
369
370 p_end = etl::copy(p_first, p_last, p_buffer);
371 }
372
373 //*********************************************************************
378 //*********************************************************************
379 void assign(size_t n, value_type value)
380 {
381 ETL_ASSERT_OR_RETURN(n <= CAPACITY, ETL_ERROR(vector_full));
382
383 initialise();
384
385 p_end = etl::fill_n(p_buffer, n, value);
386 }
387
388 //*************************************************************************
390 //*************************************************************************
391 void clear()
392 {
393 initialise();
394 }
395
396 //*********************************************************************
400 //*********************************************************************
401 void push_back(value_type value)
402 {
403#if defined(ETL_CHECK_PUSH_POP)
404 ETL_ASSERT_OR_RETURN(size() != CAPACITY, ETL_ERROR(vector_full));
405#endif
406 *p_end++ = value;
407 }
408
409 //*********************************************************************
413 //*********************************************************************
414 void emplace_back(value_type value)
415 {
416#if defined(ETL_CHECK_PUSH_POP)
417 ETL_ASSERT_OR_RETURN(size() != CAPACITY, ETL_ERROR(vector_full));
418#endif
419 * p_end++ = value;
420 }
421
422 //*************************************************************************
425 //*************************************************************************
426 void pop_back()
427 {
428#if defined(ETL_CHECK_PUSH_POP)
429 ETL_ASSERT_OR_RETURN(size() > 0, ETL_ERROR(vector_empty));
430#endif
431 --p_end;
432 }
433
434 //*********************************************************************
439 //*********************************************************************
440#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
442#endif
443 iterator insert(const_iterator position, value_type value)
444 {
445 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
446
447 iterator position_ = to_iterator(position);
448
449 if (size() != CAPACITY)
450 {
451 if (position_ != end())
452 {
453 ++p_end;
454 etl::copy_backward(position_, end() - 1, end());
455 *position_ = value;
456 }
457 else
458 {
459 *p_end++ = value;
460 }
461 }
462
463 return position_;
464 }
465#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
466 #include "diagnostic_pop.h"
467#endif
468
469 //*************************************************************************
472 //*************************************************************************
473#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
475#endif
476 iterator emplace(const_iterator position)
477 {
478 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
479
480 iterator position_ = to_iterator(position);
481
482 if (position_ != end())
483 {
484 ++p_end;
485 etl::copy_backward(position_, end() - 1, end());
486 *position_ = ETL_NULLPTR;
487 }
488 else
489 {
490 *p_end++ = ETL_NULLPTR;
491 }
492
493 return position_;
494 }
495#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
496 #include "diagnostic_pop.h"
497#endif
498
499 //*************************************************************************
502 //*************************************************************************
503#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
505#endif
506 iterator emplace(const_iterator position, value_type value)
507 {
508 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
509
510 iterator position_ = to_iterator(position);
511
512 if (position_ != end())
513 {
514 ++p_end;
515 etl::copy_backward(position_, end() - 1, end());
516 *position_ = value;
517 }
518 else
519 {
520 *p_end++ = value;
521 }
522
523 return position_;
524 }
525#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
526 #include "diagnostic_pop.h"
527#endif
528
529 //*********************************************************************
535 //*********************************************************************
536#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
538#endif
539 void insert(const_iterator position, size_t n, value_type value)
540 {
541 ETL_ASSERT_OR_RETURN((size() + n) <= CAPACITY, ETL_ERROR(vector_full));
542
543 iterator position_ = to_iterator(position);
544
545 etl::copy_backward(position_, p_end, p_end + n);
546 etl::fill_n(position_, n, value);
547
548 p_end += n;
549 }
550#if defined(ETL_COMPILER_GCC) && defined(ETL_IN_UNIT_TEST)
551 #include "diagnostic_pop.h"
552#endif
553
554 //*********************************************************************
561 //*********************************************************************
562 template <typename TIterator>
563 void insert(const_iterator position, TIterator first, TIterator last)
564 {
565 size_t count = etl::distance(first, last);
566
567 iterator position_ = to_iterator(position);
568
569 ETL_ASSERT_OR_RETURN((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
570
571 etl::copy_backward(position_, p_end, p_end + count);
572 etl::copy(first, last, position_);
573 p_end += count;
574 }
575
576 //*********************************************************************
580 //*********************************************************************
581 iterator erase(iterator i_element)
582 {
583 etl::copy(i_element + 1, end(), i_element);
584 --p_end;
585
586 return i_element;
587 }
588
589 //*********************************************************************
593 //*********************************************************************
594 iterator erase(const_iterator i_element)
595 {
596 iterator i_element_ = to_iterator(i_element);
597
598 etl::copy(i_element_ + 1, end(), i_element_);
599 --p_end;
600
601 return i_element_;
602 }
603
604 //*********************************************************************
611 //*********************************************************************
612 iterator erase(const_iterator first, const_iterator last)
613 {
614 iterator first_ = to_iterator(first);
615 iterator last_ = to_iterator(last);
616
617 etl::copy(last_, end(), first_);
618 size_t n_delete = static_cast<size_t>(etl::distance(first, last));
619
620 // Just adjust the count.
621 p_end -= n_delete;
622
623 return first_;
624 }
625
626 //*************************************************************************
628 //*************************************************************************
630 {
631 if (&rhs != this)
632 {
633 this->initialise();
634 this->resize(rhs.size());
635 etl::copy_n(rhs.data(), rhs.size(), this->data());
636 }
637
638 return *this;
639 }
640
641#if ETL_USING_CPP11
642 //*************************************************************************
644 //*************************************************************************
646 {
647 if (&rhs != this)
648 {
649 this->initialise();
650 this->resize(rhs.size());
651 etl::copy_n(rhs.data(), rhs.size(), this->data());
652 rhs.initialise();
653 }
654
655 return *this;
656 }
657#endif
658
659 //*************************************************************************
662 //*************************************************************************
664 {
665 return size_t(p_end - p_buffer);
666 }
667
668 //*************************************************************************
671 //*************************************************************************
672 bool empty() const
673 {
674 return (p_end == p_buffer);
675 }
676
677 //*************************************************************************
680 //*************************************************************************
681 bool full() const
682 {
683 return size() == CAPACITY;
684 }
685
686 //*************************************************************************
689 //*************************************************************************
690 size_t available() const
691 {
692 return max_size() - size();
693 }
694
695 protected:
696
697 //*********************************************************************
699 //*********************************************************************
700 pvoidvector(void** p_buffer_, size_t MAX_SIZE)
701 : vector_base(MAX_SIZE)
702 , p_buffer(p_buffer_)
703 , p_end(p_buffer_)
704 {
705 }
706
707 //*********************************************************************
709 //*********************************************************************
711 {
712 p_end = p_buffer;
713 }
714
715 //*************************************************************************
717 //*************************************************************************
719 {
720 uintptr_t length = static_cast<uintptr_t>(p_end - p_buffer);
721
722 p_buffer = p_buffer_;
723 p_end = p_buffer_ + length;
724 }
725
726 void** p_buffer;
727 void** p_end;
728
729 private:
730
731 //*************************************************************************
733 //*************************************************************************
734 iterator to_iterator(const_iterator itr) const
735 {
736 return const_cast<iterator>(itr);
737 }
738
739 // Disable copy construction.
740 pvoidvector(const pvoidvector&);
741 };
742
743 //***************************************************************************
749 //***************************************************************************
751 {
752 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
753 }
754
755 //***************************************************************************
761 //***************************************************************************
763 {
764 return !(lhs == rhs);
765 }
766
767 //***************************************************************************
773 //***************************************************************************
775 {
776 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
777 }
778
779 //***************************************************************************
785 //***************************************************************************
787 {
788 return (rhs < lhs);
789 }
790
791 //***************************************************************************
797 //***************************************************************************
799 {
800 return !(lhs > rhs);
801 }
802
803 //***************************************************************************
809 //***************************************************************************
811 {
812 return !(lhs < rhs);
813 }
814}
815
816#include "minmax_pop.h"
817
818#undef ETL_IN_PVOIDVECTOR
819
820#endif
#define ETL_ASSERT(b, e)
Definition error_handler.h:316
bool full() const
Definition pvoidvector.h:681
iterator erase(const_iterator first, const_iterator last)
Definition pvoidvector.h:612
etl::enable_if<!etl::is_pointer< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition pvoidvector.h:333
const_reference at(size_t i) const
Definition pvoidvector.h:264
iterator begin()
Definition pvoidvector.h:76
void emplace_back(value_type value)
Definition pvoidvector.h:414
iterator erase(iterator i_element)
Definition pvoidvector.h:581
pointer data()
Definition pvoidvector.h:310
size_type max_size() const
Definition vector_base.h:140
const_reverse_iterator rend() const
Definition pvoidvector.h:157
reference operator[](size_t i)
Definition pvoidvector.h:231
void initialise()
Initialise the vector.
Definition pvoidvector.h:710
bool empty() const
Definition pvoidvector.h:672
const_iterator end() const
Definition pvoidvector.h:103
const size_type CAPACITY
The maximum number of elements in the vector.
Definition vector_base.h:170
iterator emplace(const_iterator position)
Definition pvoidvector.h:476
etl::pvoidvector & operator=(const etl::pvoidvector &rhs)
Assignment operator.
Definition pvoidvector.h:629
void insert(const_iterator position, size_t n, value_type value)
Definition pvoidvector.h:539
reverse_iterator rend()
Definition pvoidvector.h:148
iterator insert(const_iterator position, value_type value)
Definition pvoidvector.h:443
void clear()
Clears the vector.
Definition pvoidvector.h:391
void assign(size_t n, value_type value)
Definition pvoidvector.h:379
iterator emplace(const_iterator position, value_type value)
Definition pvoidvector.h:506
void resize(size_t new_size)
Definition pvoidvector.h:186
void pop_back()
Definition pvoidvector.h:426
const_reverse_iterator crend() const
Definition pvoidvector.h:175
const_reference front() const
Definition pvoidvector.h:283
void repair_buffer(void **p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition pvoidvector.h:718
reference back()
Definition pvoidvector.h:292
iterator end()
Definition pvoidvector.h:94
void uninitialized_resize(size_t new_size)
Definition pvoidvector.h:219
const_pointer data() const
Definition pvoidvector.h:319
reference front()
Definition pvoidvector.h:274
pvoidvector(void **p_buffer_, size_t MAX_SIZE)
Constructor.
Definition pvoidvector.h:700
void push_back(value_type value)
Definition pvoidvector.h:401
const_reference back() const
Definition pvoidvector.h:301
size_t available() const
Definition pvoidvector.h:690
const_iterator cend() const
Definition pvoidvector.h:121
void insert(const_iterator position, TIterator first, TIterator last)
Definition pvoidvector.h:563
const_iterator begin() const
Definition pvoidvector.h:85
const_iterator cbegin() const
Definition pvoidvector.h:112
size_type size() const
Definition pvoidvector.h:663
iterator erase(const_iterator i_element)
Definition pvoidvector.h:594
const_reverse_iterator crbegin() const
Definition pvoidvector.h:166
reverse_iterator rbegin()
Definition pvoidvector.h:130
etl::enable_if< etl::is_pointer< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition pvoidvector.h:358
reference at(size_t i)
Definition pvoidvector.h:252
void resize(size_t new_size, value_type value)
Definition pvoidvector.h:200
const_reverse_iterator rbegin() const
Definition pvoidvector.h:139
Definition pvoidvector.h:55
Definition vector_base.h:122
Definition vector_base.h:80
Definition vector_base.h:66
Definition vector_base.h:94
bitset_ext
Definition absolute.h:38
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:693
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:705
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:654
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:642
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:666
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:681
iterator
Definition iterator.h:399
pair holds two objects of arbitrary type
Definition utility.h:164