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 //*********************************************************************
231 reference operator [](size_t i)
232 {
233 return p_buffer[i];
234 }
235
236 //*********************************************************************
240 //*********************************************************************
241 const_reference operator [](size_t i) const
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 //*********************************************************************
274 reference front()
275 {
276 return p_buffer[0];
277 }
278
279 //*********************************************************************
282 //*********************************************************************
283 const_reference front() const
284 {
285 return p_buffer[0];
286 }
287
288 //*********************************************************************
291 //*********************************************************************
292 reference back()
293 {
294 return *(p_end - 1);
295 }
296
297 //*********************************************************************
300 //*********************************************************************
301 const_reference back() const
302 {
303 return *(p_end - 1);
304 }
305
306 //*********************************************************************
309 //*********************************************************************
310 pointer data()
311 {
312 return p_buffer;
313 }
314
315 //*********************************************************************
318 //*********************************************************************
319 const_pointer data() const
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::mem_move(p_first, p_last, p_buffer) + (p_last - p_first);
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::mem_move(position_, end() - 1, position_ + 1);
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::mem_move(position_, end() - 1, position_ + 1);
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::mem_move(position_, end() - 1, position_ + 1);
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::mem_move(position_, p_end, position_ + 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>
564 insert(const_iterator position, TIterator first, TIterator last)
565 {
566 size_t count = etl::distance(first, last);
567
568 iterator position_ = to_iterator(position);
569
570 ETL_ASSERT_OR_RETURN((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
571
572 etl::mem_move(position_, p_end, position_ + count);
573 etl::copy(first, last, position_);
574 p_end += count;
575 }
576
577 //*********************************************************************
584 //*********************************************************************
585 template <typename TIterator>
587 insert(const_iterator position, TIterator first, TIterator last)
588 {
589 size_t count = etl::distance(first, last);
590
591 iterator position_ = to_iterator(position);
592
593 ETL_ASSERT_OR_RETURN((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
594
595 etl::mem_move(position_, p_end, position_ + count);
596 etl::mem_move((void**)first, (void**)last, position_);
597 p_end += count;
598 }
599
600 //*********************************************************************
604 //*********************************************************************
605 iterator erase(iterator i_element)
606 {
607 etl::mem_move(i_element + 1, end(), i_element);
608 --p_end;
609
610 return i_element;
611 }
612
613 //*********************************************************************
617 //*********************************************************************
618 iterator erase(const_iterator i_element)
619 {
620 iterator i_element_ = to_iterator(i_element);
621
622 etl::mem_move(i_element_ + 1, end(), i_element_);
623 --p_end;
624
625 return i_element_;
626 }
627
628 //*********************************************************************
635 //*********************************************************************
636 iterator erase(const_iterator first, const_iterator last)
637 {
638 iterator first_ = to_iterator(first);
639 iterator last_ = to_iterator(last);
640
641 etl::mem_move(last_, end(), first_);
642 size_t n_delete = static_cast<size_t>(etl::distance(first, last));
643
644 // Just adjust the count.
645 p_end -= n_delete;
646
647 return first_;
648 }
649
650 //*************************************************************************
652 //*************************************************************************
654 {
655 if (&rhs != this)
656 {
657 this->initialise();
658 this->resize(rhs.size());
659 etl::mem_copy(rhs.data(), rhs.size(), this->data());
660 }
661
662 return *this;
663 }
664
665#if ETL_USING_CPP11
666 //*************************************************************************
668 //*************************************************************************
670 {
671 if (&rhs != this)
672 {
673 this->initialise();
674 this->resize(rhs.size());
675 etl::mem_copy(rhs.data(), rhs.size(), this->data());
676 rhs.initialise();
677 }
678
679 return *this;
680 }
681#endif
682
683 //*************************************************************************
686 //*************************************************************************
687 size_type size() const
688 {
689 return size_t(p_end - p_buffer);
690 }
691
692 //*************************************************************************
695 //*************************************************************************
696 bool empty() const
697 {
698 return (p_end == p_buffer);
699 }
700
701 //*************************************************************************
704 //*************************************************************************
705 bool full() const
706 {
707 return size() == CAPACITY;
708 }
709
710 //*************************************************************************
713 //*************************************************************************
714 size_t available() const
715 {
716 return max_size() - size();
717 }
718
719 protected:
720
721 //*********************************************************************
723 //*********************************************************************
724 pvoidvector(void** p_buffer_, size_t MAX_SIZE)
725 : vector_base(MAX_SIZE)
726 , p_buffer(p_buffer_)
727 , p_end(p_buffer_)
728 {
729 }
730
731 //*********************************************************************
733 //*********************************************************************
735 {
736 p_end = p_buffer;
737 }
738
739 //*************************************************************************
741 //*************************************************************************
743 {
744 uintptr_t length = static_cast<uintptr_t>(p_end - p_buffer);
745
746 p_buffer = p_buffer_;
747 p_end = p_buffer_ + length;
748 }
749
750 void** p_buffer;
751 void** p_end;
752
753 private:
754
755 //*************************************************************************
757 //*************************************************************************
758 iterator to_iterator(const_iterator itr) const
759 {
760 return const_cast<iterator>(itr);
761 }
762
763 // Disable copy construction.
764 pvoidvector(const pvoidvector&);
765 };
766
767 //***************************************************************************
773 //***************************************************************************
775 {
776 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
777 }
778
779 //***************************************************************************
785 //***************************************************************************
787 {
788 return !(lhs == rhs);
789 }
790
791 //***************************************************************************
797 //***************************************************************************
799 {
800 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
801 }
802
803 //***************************************************************************
809 //***************************************************************************
811 {
812 return (rhs < lhs);
813 }
814
815 //***************************************************************************
821 //***************************************************************************
823 {
824 return !(lhs > rhs);
825 }
826
827 //***************************************************************************
833 //***************************************************************************
835 {
836 return !(lhs < rhs);
837 }
838}
839
840#include "minmax_pop.h"
841
842#undef ETL_IN_PVOIDVECTOR
843
844#endif
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
enable_if
Definition type_traits_generator.h:1230
bool full() const
Definition pvoidvector.h:705
iterator erase(const_iterator first, const_iterator last)
Definition pvoidvector.h:636
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:605
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:734
bool empty() const
Definition pvoidvector.h:696
etl::enable_if<!etl::is_pointer< TIterator >::value, void >::type insert(const_iterator position, TIterator first, TIterator last)
Definition pvoidvector.h:564
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:653
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
etl::enable_if< etl::is_pointer< TIterator >::value, void >::type insert(const_iterator position, TIterator first, TIterator last)
Definition pvoidvector.h:587
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:742
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:724
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:714
const_iterator cend() const
Definition pvoidvector.h:121
const_iterator begin() const
Definition pvoidvector.h:85
const_iterator cbegin() const
Definition pvoidvector.h:112
size_type size() const
Definition pvoidvector.h:687
iterator erase(const_iterator i_element)
Definition pvoidvector.h:618
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:1148
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1160
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1109
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1097
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1121
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1136
iterator
Definition iterator.h:399