Embedded Template Library 1.0
Loading...
Searching...
No Matches
vector.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) 2014 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_VECTOR_INCLUDED
32#define ETL_VECTOR_INCLUDED
33
34#define ETL_IN_VECTOR_H
35
36#include "platform.h"
37#include "algorithm.h"
38#include "alignment.h"
39#include "array.h"
40#include "debug_count.h"
41#include "error_handler.h"
42#include "exception.h"
43#include "initializer_list.h"
44#include "iterator.h"
45#include "memory.h"
46#include "placement_new.h"
47#include "static_assert.h"
48#include "type_traits.h"
49
50#include "private/pvoidvector.h"
51#include "private/vector_base.h"
52
53#include <stddef.h>
54#include <stdint.h>
55
56//*****************************************************************************
60//*****************************************************************************
61
62namespace etl
63{
64 template <typename T, typename Enable = void>
65 class ivector;
66
67 //***************************************************************************
73 //***************************************************************************
74 template <typename T>
75 class ivector<T, typename etl::enable_if<etl::negation<etl::is_object_pointer<T> >::value>::type> : public etl::vector_base
76 {
77 public:
78
79 typedef T value_type;
80 typedef T& reference;
81 typedef const T& const_reference;
82#if ETL_USING_CPP11
83 typedef T&& rvalue_reference;
84#endif
85 typedef T* pointer;
86 typedef const T* const_pointer;
87 typedef T* iterator;
88 typedef const T* const_iterator;
89 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
90 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
91 typedef size_t size_type;
92 typedef typename etl::iterator_traits<iterator>::difference_type difference_type;
93
94 public:
95
96 //*********************************************************************
99 //*********************************************************************
100 iterator begin()
101 {
102 return p_buffer;
103 }
104
105 //*********************************************************************
108 //*********************************************************************
109 const_iterator begin() const
110 {
111 return p_buffer;
112 }
113
114 //*********************************************************************
117 //*********************************************************************
118 iterator end()
119 {
120 return p_end;
121 }
122
123 //*********************************************************************
126 //*********************************************************************
127 const_iterator end() const
128 {
129 return p_end;
130 }
131
132 //*********************************************************************
135 //*********************************************************************
136 const_iterator cbegin() const
137 {
138 return p_buffer;
139 }
140
141 //*********************************************************************
144 //*********************************************************************
145 const_iterator cend() const
146 {
147 return p_end;
148 }
149
150 //*********************************************************************
153 //*********************************************************************
154 reverse_iterator rbegin()
155 {
156 return reverse_iterator(end());
157 }
158
159 //*********************************************************************
162 //*********************************************************************
163 const_reverse_iterator rbegin() const
164 {
165 return const_reverse_iterator(end());
166 }
167
168 //*********************************************************************
171 //*********************************************************************
172 reverse_iterator rend()
173 {
174 return reverse_iterator(begin());
175 }
176
177 //*********************************************************************
180 //*********************************************************************
181 const_reverse_iterator rend() const
182 {
183 return const_reverse_iterator(begin());
184 }
185
186 //*********************************************************************
189 //*********************************************************************
190 const_reverse_iterator crbegin() const
191 {
192 return const_reverse_iterator(cend());
193 }
194
195 //*********************************************************************
198 //*********************************************************************
199 const_reverse_iterator crend() const
200 {
201 return const_reverse_iterator(cbegin());
202 }
203
204 //*********************************************************************
209 //*********************************************************************
210 void resize(size_t new_size)
211 {
212 resize(new_size, T());
213 }
214
215 //*********************************************************************
222 //*********************************************************************
223 void resize(size_t new_size, const_reference value)
224 {
225 ETL_ASSERT_OR_RETURN(new_size <= CAPACITY, ETL_ERROR(vector_full));
226
227 const size_t current_size = size();
228 size_t delta = (current_size < new_size) ? new_size - current_size : current_size - new_size;
229
230 if (current_size < new_size)
231 {
232 etl::uninitialized_fill_n(p_end, delta, value);
233 ETL_ADD_DEBUG_COUNT(delta);
234 }
235 else
236 {
237 etl::destroy_n(p_end - delta, delta);
238 ETL_SUBTRACT_DEBUG_COUNT(delta);
239 }
240
241 p_end = p_buffer + new_size;
242 }
243
244 //*********************************************************************
247 //*********************************************************************
248 void uninitialized_resize(size_t new_size)
249 {
250 ETL_ASSERT_OR_RETURN(new_size <= CAPACITY, ETL_ERROR(vector_full));
251
252#if defined(ETL_DEBUG_COUNT)
253 if (size() < new_size)
254 {
255 ETL_ADD_DEBUG_COUNT(new_size - size());
256 }
257 else
258 {
259 ETL_SUBTRACT_DEBUG_COUNT(size() - new_size);
260 }
261#endif
262
263 p_end = p_buffer + new_size;
264 }
265
266 //*********************************************************************
271 //*********************************************************************
272 void reserve(size_t n)
273 {
274 (void)n; // Stop 'unused parameter' warning in release mode.
275 ETL_ASSERT(n <= CAPACITY, ETL_ERROR(vector_out_of_bounds));
276 }
277
278 //*********************************************************************
282 //*********************************************************************
283 reference operator[](size_t i)
284 {
285 ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(vector_out_of_bounds));
286 return p_buffer[i];
287 }
288
289 //*********************************************************************
293 //*********************************************************************
294 const_reference operator[](size_t i) const
295 {
296 ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(vector_out_of_bounds));
297 return p_buffer[i];
298 }
299
300 //*********************************************************************
306 //*********************************************************************
307 reference at(size_t i)
308 {
309 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
310 return p_buffer[i];
311 }
312
313 //*********************************************************************
319 //*********************************************************************
320 const_reference at(size_t i) const
321 {
322 ETL_ASSERT(i < size(), ETL_ERROR(vector_out_of_bounds));
323 return p_buffer[i];
324 }
325
326 //*********************************************************************
329 //*********************************************************************
330 reference front()
331 {
332 ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(vector_out_of_bounds));
333 return *p_buffer;
334 }
335
336 //*********************************************************************
339 //*********************************************************************
340 const_reference front() const
341 {
342 ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(vector_out_of_bounds));
343 return *p_buffer;
344 }
345
346 //*********************************************************************
349 //*********************************************************************
350 reference back()
351 {
352 ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(vector_out_of_bounds));
353 return *(p_end - 1);
354 }
355
356 //*********************************************************************
359 //*********************************************************************
360 const_reference back() const
361 {
362 ETL_ASSERT_CHECK_EXTRA(size() > 0, ETL_ERROR(vector_out_of_bounds));
363 return *(p_end - 1);
364 }
365
366 //*********************************************************************
369 //*********************************************************************
370 pointer data()
371 {
372 return p_buffer;
373 }
374
375 //*********************************************************************
378 //*********************************************************************
379 ETL_CONSTEXPR const_pointer data() const
380 {
381 return p_buffer;
382 }
383
384 //*********************************************************************
391 //*********************************************************************
392 template <typename TIterator>
393 typename etl::enable_if<!etl::is_integral<TIterator>::value
394 && etl::is_convertible<typename etl::iterator_traits<TIterator>::value_type, T>::value,
395 typename etl::enable_if<!etl::is_integral<TIterator>::value, void>::type>::type
396 assign(TIterator first, TIterator last) ETL_NOEXCEPT_IF((etl::is_nothrow_copy_constructible<T>::value && ETL_NOT_USING_EXCEPTIONS))
397 {
398#if ETL_USING_CPP11
399 ETL_STATIC_ASSERT((etl::is_same<typename etl::remove_cv<T>::type,
400 typename etl::remove_cv<typename etl::remove_reference<decltype(*first)>::type>::type>::value),
401 "Iterator type does not match container type");
402#endif
403
404#if ETL_IS_DEBUG_BUILD
405 difference_type d = etl::distance(first, last);
406 ETL_ASSERT_OR_RETURN(static_cast<size_t>(d) <= CAPACITY, ETL_ERROR(vector_full));
407#endif
408
409 initialise();
410
411 p_end = etl::uninitialized_copy(first, last, p_buffer);
412 ETL_ADD_DEBUG_COUNT(uint32_t(etl::distance(first, last)));
413 }
414
415 //*********************************************************************
421 //*********************************************************************
422 void assign(size_t n, const_reference value)
423 {
424 ETL_ASSERT_OR_RETURN(n <= CAPACITY, ETL_ERROR(vector_full));
425
426 initialise();
427
429 ETL_ADD_DEBUG_COUNT(uint32_t(n));
430 }
431
432 //*************************************************************************
434 //*************************************************************************
435 void clear()
436 {
437 initialise();
438 }
439
440 //*************************************************************************
442 //*************************************************************************
443 void fill(const T& value)
444 {
445 etl::fill(begin(), end(), value);
446 }
447
448 //*********************************************************************
453 //*********************************************************************
454 void push_back(const_reference value)
455 {
456 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(size() != CAPACITY, ETL_ERROR(vector_full));
457
458 create_back(value);
459 }
460
461#if ETL_USING_CPP11
462 //*********************************************************************
467 //*********************************************************************
468 void push_back(rvalue_reference value)
469 {
470 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(size() != CAPACITY, ETL_ERROR(vector_full));
471
472 create_back(etl::move(value));
473 }
474#endif
475
476#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT && !defined(ETL_VECTOR_FORCE_CPP03_IMPLEMENTATION)
477 //*********************************************************************
482 //*********************************************************************
483 template <typename... Args>
484 reference emplace_back(Args&&... args)
485 {
486 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
487
489 ::new (p_end) T(etl::forward<Args>(args)...);
490 #include "private/diagnostic_pop.h"
491 ++p_end;
492 ETL_INCREMENT_DEBUG_COUNT;
493 return back();
494 }
495#else
496 //*********************************************************************
501 //*********************************************************************
502 reference emplace_back()
503 {
504 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
505
506 ::new (p_end) T();
507 ++p_end;
508 ETL_INCREMENT_DEBUG_COUNT;
509 return back();
510 }
511
512 //*********************************************************************
517 //*********************************************************************
518 template <typename T1>
519 reference emplace_back(const T1& value1)
520 {
521 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
522
523 ::new (p_end) T(value1);
524 ++p_end;
525 ETL_INCREMENT_DEBUG_COUNT;
526 return back();
527 }
528
529 //*********************************************************************
534 //*********************************************************************
535 template <typename T1, typename T2>
536 reference emplace_back(const T1& value1, const T2& value2)
537 {
538 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
539
540 ::new (p_end) T(value1, value2);
541 ++p_end;
542 ETL_INCREMENT_DEBUG_COUNT;
543 return back();
544 }
545
546 //*********************************************************************
551 //*********************************************************************
552 template <typename T1, typename T2, typename T3>
553 reference emplace_back(const T1& value1, const T2& value2, const T3& value3)
554 {
555 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
556
557 ::new (p_end) T(value1, value2, value3);
558 ++p_end;
559 ETL_INCREMENT_DEBUG_COUNT;
560 return back();
561 }
562
563 //*********************************************************************
568 //*********************************************************************
569 template <typename T1, typename T2, typename T3, typename T4>
570 reference emplace_back(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
571 {
572 ETL_ASSERT_CHECK_PUSH_POP(size() != CAPACITY, ETL_ERROR(vector_full));
573
574 ::new (p_end) T(value1, value2, value3, value4);
575 ++p_end;
576 ETL_INCREMENT_DEBUG_COUNT;
577 return back();
578 }
579#endif
580
581 //*************************************************************************
586 //*************************************************************************
587 void pop_back()
588 {
589 ETL_ASSERT_CHECK_PUSH_POP_OR_RETURN(size() > 0, ETL_ERROR(vector_empty));
590
591 destroy_back();
592 }
593
594 //*********************************************************************
600 //*********************************************************************
601 iterator insert(const_iterator position, const_reference value)
602 {
603 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
604 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
605
606 iterator position_ = to_iterator(position);
607
608 if (position_ == end())
609 {
610 create_back(value);
611 }
612 else
613 {
614 create_back(back());
615 etl::move_backward(position_, p_end - 2, p_end - 1);
616 *position_ = value;
617 }
618
619 return position_;
620 }
621
622#if ETL_USING_CPP11
623 //*********************************************************************
629 //*********************************************************************
630 iterator insert(const_iterator position, rvalue_reference value)
631 {
632 ETL_ASSERT(size() != CAPACITY, ETL_ERROR(vector_full));
633 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
634
635 iterator position_ = to_iterator(position);
636
637 if (position_ == end())
638 {
639 create_back(etl::move(value));
640 }
641 else
642 {
643 create_back(etl::move(back()));
644 etl::move_backward(position_, p_end - 2, p_end - 1);
645 *position_ = etl::move(value);
646 }
647
648 return position_;
649 }
650#endif
651
652 //*************************************************************************
654 //*************************************************************************
655#if ETL_USING_CPP11 && ETL_NOT_USING_STLPORT
656 template <typename... Args>
657 iterator emplace(const_iterator position, Args&&... args)
658 {
659 ETL_ASSERT(!full(), ETL_ERROR(vector_full));
660 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
661
662 iterator position_ = to_iterator(position);
663
664 void* p;
665
666 if (position_ == end())
667 {
668 p = p_end++;
669 ETL_INCREMENT_DEBUG_COUNT;
670 }
671 else
672 {
673 p = etl::addressof(*position_);
674 create_back(back());
675 etl::move_backward(position_, p_end - 2, p_end - 1);
676 (*position_).~T();
677 }
678
680 ::new (p) T(etl::forward<Args>(args)...);
681 #include "private/diagnostic_pop.h"
682
683 return position_;
684 }
685#else
686 template <typename T1>
687 iterator emplace(const_iterator position, const T1& value1)
688 {
689 ETL_ASSERT(!full(), ETL_ERROR(vector_full));
690 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
691
692 iterator position_ = to_iterator(position);
693
694 void* p;
695
696 if (position_ == end())
697 {
698 p = p_end++;
699 ETL_INCREMENT_DEBUG_COUNT;
700 }
701 else
702 {
703 p = etl::addressof(*position_);
704 create_back(back());
705 etl::move_backward(position_, p_end - 2, p_end - 1);
706 (*position_).~T();
707 }
708
709 ::new (p) T(value1);
710
711 return position_;
712 }
713
714 template <typename T1, typename T2>
715 iterator emplace(const_iterator position, const T1& value1, const T2& value2)
716 {
717 ETL_ASSERT(!full(), ETL_ERROR(vector_full));
718 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
719
720 iterator position_ = to_iterator(position);
721
722 void* p;
723
724 if (position_ == end())
725 {
726 p = p_end++;
727 ETL_INCREMENT_DEBUG_COUNT;
728 }
729 else
730 {
731 p = etl::addressof(*position_);
732 create_back(back());
733 etl::move_backward(position_, p_end - 2, p_end - 1);
734 (*position_).~T();
735 }
736
737 ::new (p) T(value1, value2);
738
739 return position_;
740 }
741
742 template <typename T1, typename T2, typename T3>
743 iterator emplace(const_iterator position, const T1& value1, const T2& value2, const T3& value3)
744 {
745 ETL_ASSERT(!full(), ETL_ERROR(vector_full));
746 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
747
748 iterator position_ = to_iterator(position);
749
750 void* p;
751
752 if (position_ == end())
753 {
754 p = p_end++;
755 ETL_INCREMENT_DEBUG_COUNT;
756 }
757 else
758 {
759 p = etl::addressof(*position_);
760 create_back(back());
761 etl::move_backward(position_, p_end - 2, p_end - 1);
762 (*position_).~T();
763 }
764
765 ::new (p) T(value1, value2, value3);
766
767 return position_;
768 }
769
770 template <typename T1, typename T2, typename T3, typename T4>
771 iterator emplace(const_iterator position, const T1& value1, const T2& value2, const T3& value3, const T4& value4)
772 {
773 ETL_ASSERT(!full(), ETL_ERROR(vector_full));
774 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
775
776 iterator position_ = to_iterator(position);
777
778 void* p;
779
780 if (position_ == end())
781 {
782 p = p_end++;
783 ETL_INCREMENT_DEBUG_COUNT;
784 }
785 else
786 {
787 p = etl::addressof(*position_);
788 create_back(back());
789 etl::move_backward(position_, p_end - 2, p_end - 1);
790 (*position_).~T();
791 }
792
793 ::new (p) T(value1, value2, value3, value4);
794
795 return position_;
796 }
797#endif
798
799 //*********************************************************************
806 //*********************************************************************
807 void insert(const_iterator position, size_t n, const_reference value)
808 {
809 ETL_ASSERT_OR_RETURN((size() + n) <= CAPACITY, ETL_ERROR(vector_full));
810 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
811
812 iterator position_ = to_iterator(position);
813
814 size_t insert_n = n;
815 size_t insert_begin = static_cast<size_t>(etl::distance(begin(), position_));
816 size_t insert_end = insert_begin + insert_n;
817
818 // Copy old data.
819 size_t copy_old_n;
820 size_t construct_old_n;
821 iterator p_construct_old;
822
823 if (insert_end > size())
824 {
825 copy_old_n = 0;
826 construct_old_n = size() - insert_begin;
827 p_construct_old = p_buffer + insert_end;
828 }
829 else
830 {
831 copy_old_n = size() - insert_begin - insert_n;
832 construct_old_n = insert_n;
833 p_construct_old = p_end;
834 }
835
836 size_t copy_new_n = construct_old_n;
837 size_t construct_new_n = insert_n - copy_new_n;
838
839 // Construct old.
840 etl::uninitialized_move(p_end - construct_old_n, p_end, p_construct_old);
841 ETL_ADD_DEBUG_COUNT(construct_old_n);
842
843 // Copy old.
844 etl::move_backward(p_buffer + insert_begin, p_buffer + insert_begin + copy_old_n, p_buffer + insert_end + copy_old_n);
845
846 // Construct new.
847 etl::uninitialized_fill_n(p_end, construct_new_n, value);
848 ETL_ADD_DEBUG_COUNT(construct_new_n);
849
850 // Copy new.
851 etl::fill_n(p_buffer + insert_begin, copy_new_n, value);
852
853 p_end += n;
854 }
855
856 //*********************************************************************
863 //*********************************************************************
864 template <class TIterator>
865 typename etl::enable_if<
866 !etl::is_integral<TIterator>::value && etl::is_convertible<typename etl::iterator_traits<TIterator>::value_type, T>::value, void>::type
867 insert(const_iterator position, TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
868 {
869 size_t count = static_cast<size_t>(etl::distance(first, last));
870
871 ETL_ASSERT_OR_RETURN((size() + count) <= CAPACITY, ETL_ERROR(vector_full));
872 ETL_ASSERT_CHECK_EXTRA(cbegin() <= position && position <= cend(), ETL_ERROR(vector_out_of_bounds));
873
874 size_t insert_n = count;
875 size_t insert_begin = static_cast<size_t>(etl::distance(cbegin(), position));
876 size_t insert_end = insert_begin + insert_n;
877
878 // Move old data.
879 size_t copy_old_n;
880 size_t construct_old_n;
881 iterator p_construct_old;
882
883 if (insert_end > size())
884 {
885 copy_old_n = 0;
886 construct_old_n = size() - insert_begin;
887 p_construct_old = p_buffer + insert_end;
888 }
889 else
890 {
891 copy_old_n = size() - insert_begin - insert_n;
892 construct_old_n = insert_n;
893 p_construct_old = p_end;
894 }
895
896 size_t copy_new_n = construct_old_n;
897 size_t construct_new_n = insert_n - copy_new_n;
898
899 // Move construct old.
900 etl::uninitialized_move(p_end - construct_old_n, p_end, p_construct_old);
901 ETL_ADD_DEBUG_COUNT(construct_old_n);
902
903 // Move old.
904 etl::move_backward(p_buffer + insert_begin, p_buffer + insert_begin + copy_old_n, p_buffer + insert_end + copy_old_n);
905
906 // Copy construct new.
907 etl::uninitialized_copy(first + static_cast<ptrdiff_t>(copy_new_n), first + static_cast<ptrdiff_t>(copy_new_n + construct_new_n), p_end);
908 ETL_ADD_DEBUG_COUNT(construct_new_n);
909
910 // Copy new.
911 etl::copy(first, first + static_cast<ptrdiff_t>(copy_new_n), p_buffer + insert_begin);
912
913 p_end += count;
914 }
915
916 //*********************************************************************
921 //*********************************************************************
922 iterator erase(iterator i_element)
923 {
924 ETL_ASSERT_CHECK_EXTRA(cbegin() <= i_element && i_element < cend(), ETL_ERROR(vector_out_of_bounds));
925
926 etl::move(i_element + 1, end(), i_element);
927 destroy_back();
928
929 return i_element;
930 }
931
932 //*********************************************************************
937 //*********************************************************************
938 iterator erase(const_iterator i_element)
939 {
940 ETL_ASSERT_CHECK_EXTRA(cbegin() <= i_element && i_element < cend(), ETL_ERROR(vector_out_of_bounds));
941
942 iterator i_element_ = to_iterator(i_element);
943
944 etl::move(i_element_ + 1, end(), i_element_);
945 destroy_back();
946
947 return i_element_;
948 }
949
950 //*********************************************************************
958 //*********************************************************************
959 iterator erase(const_iterator first, const_iterator last)
960 {
961 ETL_ASSERT_CHECK_EXTRA(cbegin() <= first && first <= last && last <= cend(), ETL_ERROR(vector_out_of_bounds));
962
963 iterator first_ = to_iterator(first);
964 iterator last_ = to_iterator(last);
965
966 if (first == begin() && last == end())
967 {
968 clear();
969 }
970 else
971 {
972 etl::move(last_, end(), first_);
973 size_t n_delete = static_cast<size_t>(etl::distance(first_, last_));
974
975 // Destroy the elements left over at the end.
976 etl::destroy(p_end - n_delete, p_end);
977 ETL_SUBTRACT_DEBUG_COUNT(n_delete);
978 p_end -= n_delete;
979 }
980
981 return first_;
982 }
983
984 //*********************************************************************
988 //*********************************************************************
989 void swap(ivector<T>& other)
990 {
991 if (this == &other)
992 {
993 return;
994 }
995
996 ETL_ASSERT_OR_RETURN(this->max_size() >= other.size() && other.max_size() >= this->size(), ETL_ERROR(vector_full));
997
998 ivector<T>& smaller = other.size() > this->size() ? *this : other;
999 ivector<T>& larger = other.size() > this->size() ? other : *this;
1000
1001 etl::swap_ranges(smaller.begin(), smaller.end(), larger.begin());
1002
1003 typename ivector<T>::iterator larger_itr =
1004 etl::next(larger.begin(), static_cast<typename etl::iterator_traits< typename ivector<T>::iterator>::difference_type>(smaller.size()));
1005
1006 etl::move(larger_itr, larger.end(), etl::back_inserter(smaller));
1007
1008 larger.erase(larger_itr, larger.end());
1009 }
1010
1011 //*************************************************************************
1013 //*************************************************************************
1015 {
1016 if (&rhs != this)
1017 {
1018 assign(rhs.cbegin(), rhs.cend());
1019 }
1020
1021 return *this;
1022 }
1023
1024#if ETL_USING_CPP11
1025 //*************************************************************************
1027 //*************************************************************************
1028 ivector& operator=(ivector&& rhs) ETL_NOEXCEPT_IF((etl::is_nothrow_move_constructible<T>::value))
1029 {
1030 if (&rhs != this)
1031 {
1032 clear();
1033 iterator itr = rhs.begin();
1034 while (itr != rhs.end())
1035 {
1036 push_back(etl::move(*itr));
1037 ++itr;
1038 }
1039
1040 rhs.initialise();
1041 }
1042
1043 return *this;
1044 }
1045#endif
1046
1047 //*************************************************************************
1050 //*************************************************************************
1051 size_type size() const
1052 {
1053 return size_t(p_end - p_buffer);
1054 }
1055
1056 //*************************************************************************
1059 //*************************************************************************
1060 bool empty() const
1061 {
1062 return (p_end == p_buffer);
1063 }
1064
1065 //*************************************************************************
1068 //*************************************************************************
1069 bool full() const
1070 {
1071 return size() == CAPACITY;
1072 }
1073
1074 //*************************************************************************
1077 //*************************************************************************
1078 size_t available() const
1079 {
1080 return max_size() - size();
1081 }
1082
1083#ifdef ETL_IVECTOR_REPAIR_ENABLE
1084 //*************************************************************************
1086 //*************************************************************************
1087 virtual void repair() = 0;
1088#endif
1089
1090 protected:
1091
1092 //*********************************************************************
1094 //*********************************************************************
1095 ivector(T* p_buffer_, size_t MAX_SIZE) ETL_NOEXCEPT
1096 : vector_base(MAX_SIZE)
1097 , p_buffer(p_buffer_)
1098 , p_end(p_buffer_)
1099 {
1100 }
1101
1102 //*********************************************************************
1104 //*********************************************************************
1106 {
1107 if ETL_IF_CONSTEXPR (etl::is_trivially_destructible<T>::value)
1108 {
1109 ETL_RESET_DEBUG_COUNT;
1110 }
1111 else
1112 {
1114 ETL_SUBTRACT_DEBUG_COUNT(int32_t(etl::distance(p_buffer, p_end)));
1115 }
1116
1117 p_end = p_buffer;
1118 }
1119
1120 //*************************************************************************
1122 //*************************************************************************
1123 void repair_buffer(T* p_buffer_)
1124 {
1125 uintptr_t length = static_cast<uintptr_t>(p_end - p_buffer);
1126 p_buffer = p_buffer_;
1127 p_end = p_buffer_ + length;
1128 }
1129
1130 pointer p_buffer;
1131 pointer p_end;
1132
1133 private:
1134
1135 //*********************************************************************
1137 //*********************************************************************
1138 void create_back(const_reference value)
1139 {
1140 etl::create_copy_at(p_end, value);
1141 ETL_INCREMENT_DEBUG_COUNT;
1142
1143 ++p_end;
1144 }
1145
1146#if ETL_USING_CPP11
1147 //*********************************************************************
1149 //*********************************************************************
1150 void create_back(rvalue_reference value)
1151 {
1152 etl::create_copy_at(p_end, etl::move(value));
1153 ETL_INCREMENT_DEBUG_COUNT;
1154
1155 ++p_end;
1156 }
1157#endif
1158
1159 //*********************************************************************
1161 //*********************************************************************
1162 void destroy_back()
1163 {
1164 --p_end;
1165
1166 etl::destroy_at(p_end);
1167 ETL_DECREMENT_DEBUG_COUNT;
1168 }
1169
1170 // Disable copy construction.
1171 ivector(const ivector&) ETL_DELETE;
1172
1173 private:
1174
1175 //*************************************************************************
1177 //*************************************************************************
1178 ETL_CONSTEXPR iterator to_iterator(const_iterator itr) const
1179 {
1180 return const_cast<iterator>(itr);
1181 }
1182 };
1183
1184 //***************************************************************************
1190 //***************************************************************************
1191 template <typename T>
1192 class ivector<T, typename etl::enable_if<etl::is_object_pointer<T>::value>::type> : public etl::pvoidvector
1193 {
1194 public:
1195
1196 typedef T value_type;
1197 typedef T& reference;
1198 typedef const T& const_reference;
1199#if ETL_USING_CPP11
1200 typedef T&& rvalue_reference;
1201#endif
1202 typedef T* pointer;
1203 typedef const T* const_pointer;
1204 typedef T* iterator;
1205 typedef const T* const_iterator;
1206 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
1207 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
1208 typedef size_t size_type;
1209 typedef typename etl::iterator_traits<iterator>::difference_type difference_type;
1210
1211 private:
1212
1213 typedef pvoidvector base_t;
1214
1216
1217 template <typename TIterator>
1218 struct is_compatible_iterator
1219 : etl::bool_constant<etl::is_pointer<typename etl::iterator_traits<TIterator>::value_type>::value
1220 && etl::is_convertible<typename etl::iterator_traits<TIterator>::value_type, value_type>::value>
1221 {
1222 };
1223
1224 public:
1225
1226 //*********************************************************************
1229 //*********************************************************************
1230 iterator begin()
1231 {
1232 return iterator(base_t::begin());
1233 }
1234
1235 //*********************************************************************
1238 //*********************************************************************
1239 const_iterator begin() const
1240 {
1241 return const_iterator(base_t::begin());
1242 }
1243
1244 //*********************************************************************
1247 //*********************************************************************
1248 iterator end()
1249 {
1250 return iterator(base_t::end());
1251 }
1252
1253 //*********************************************************************
1256 //*********************************************************************
1257 const_iterator end() const
1258 {
1259 return const_iterator(base_t::end());
1260 }
1261
1262 //*********************************************************************
1265 //*********************************************************************
1266 const_iterator cbegin() const
1267 {
1268 return const_iterator(base_t::cbegin());
1269 }
1270
1271 //*********************************************************************
1274 //*********************************************************************
1275 const_iterator cend() const
1276 {
1277 return const_iterator(base_t::cend());
1278 }
1279
1280 //*********************************************************************
1283 //*********************************************************************
1284 reverse_iterator rbegin()
1285 {
1286 return reverse_iterator(iterator(base_t::end()));
1287 }
1288
1289 //*********************************************************************
1292 //*********************************************************************
1293 const_reverse_iterator rbegin() const
1294 {
1295 return const_reverse_iterator(const_iterator(base_t::end()));
1296 }
1297
1298 //*********************************************************************
1301 //*********************************************************************
1302 reverse_iterator rend()
1303 {
1304 return reverse_iterator(iterator(base_t::begin()));
1305 }
1306
1307 //*********************************************************************
1310 //*********************************************************************
1311 const_reverse_iterator rend() const
1312 {
1313 return const_reverse_iterator(const_iterator(base_t::begin()));
1314 }
1315
1316 //*********************************************************************
1319 //*********************************************************************
1320 const_reverse_iterator crbegin() const
1321 {
1322 return const_reverse_iterator(const_iterator(base_t::cend()));
1323 }
1324
1325 //*********************************************************************
1328 //*********************************************************************
1329 const_reverse_iterator crend() const
1330 {
1331 return const_reverse_iterator(const_iterator(base_t::cbegin()));
1332 }
1333
1334 //*********************************************************************
1339 //*********************************************************************
1340 void resize(size_t new_size)
1341 {
1342 base_t::resize(new_size);
1343 }
1344
1345 //*********************************************************************
1352 //*********************************************************************
1353 void resize(size_t new_size, value_type value)
1354 {
1355 base_t::resize(new_size, to_void_ptr(value));
1356 }
1357
1358 //*********************************************************************
1361 //*********************************************************************
1362 void uninitialized_resize(size_t new_size)
1363 {
1365 }
1366
1367 //*********************************************************************
1371 //*********************************************************************
1372 reference operator[](size_t i)
1373 {
1374 return reference(base_t::operator[](i));
1375 }
1376
1377 //*********************************************************************
1381 //*********************************************************************
1382 const_reference operator[](size_t i) const
1383 {
1384 return const_reference(base_t::operator[](i));
1385 }
1386
1387 //*********************************************************************
1393 //*********************************************************************
1394 reference at(size_t i)
1395 {
1396 return reference(base_t::at(i));
1397 }
1398
1399 //*********************************************************************
1405 //*********************************************************************
1406 const_reference at(size_t i) const
1407 {
1408 return const_reference(base_t::at(i));
1409 }
1410
1411 //*********************************************************************
1414 //*********************************************************************
1415 reference front()
1416 {
1417 return reference(base_t::front());
1418 }
1419
1420 //*********************************************************************
1423 //*********************************************************************
1424 const_reference front() const
1425 {
1426 return const_reference(base_t::front());
1427 }
1428
1429 //*********************************************************************
1432 //*********************************************************************
1433 reference back()
1434 {
1435 return reference(base_t::back());
1436 }
1437
1438 //*********************************************************************
1441 //*********************************************************************
1442 const_reference back() const
1443 {
1444 return const_reference(base_t::back());
1445 }
1446
1447 //*********************************************************************
1450 //*********************************************************************
1451 pointer data()
1452 {
1453 return pointer(base_t::data());
1454 }
1455
1456 //*********************************************************************
1459 //*********************************************************************
1460 const_pointer data() const
1461 {
1462 return const_pointer(base_t::data());
1463 }
1464
1465 //*********************************************************************
1472 //*********************************************************************
1473 template <typename TIterator>
1474 typename etl::enable_if<is_compatible_iterator<TIterator>::value, void>::type assign(TIterator first, TIterator last)
1475 {
1476 base_t::assign(first, last);
1477 }
1478
1479 //*********************************************************************
1485 //*********************************************************************
1486 void assign(size_t n, const_reference value)
1487 {
1488 base_t::assign(n, to_void_ptr(value));
1489 }
1490
1491 //*************************************************************************
1493 //*************************************************************************
1494 void clear()
1495 {
1496 base_t::clear();
1497 }
1498
1499 //*********************************************************************
1504 //*********************************************************************
1505 void push_back(const_reference value)
1506 {
1507 base_t::push_back(to_void_ptr(value));
1508 }
1509
1510 //*********************************************************************
1515 //*********************************************************************
1516 reference emplace_back()
1517 {
1518 base_t::emplace_back(ETL_NULLPTR);
1519
1520 return back();
1521 }
1522
1523 //*********************************************************************
1528 //*********************************************************************
1529 reference emplace_back(const_reference value)
1530 {
1531 base_t::emplace_back(to_void_ptr(value));
1532
1533 return back();
1534 }
1535
1536 //*************************************************************************
1539 //*************************************************************************
1541 {
1543 }
1544
1545 //*********************************************************************
1551 //*********************************************************************
1552 iterator insert(const_iterator position, const_reference value)
1553 {
1554 return iterator(base_t::insert(base_t::iterator(position), to_void_ptr(value)));
1555 }
1556
1557 //*************************************************************************
1559 //*************************************************************************
1560 iterator emplace(const_iterator position)
1561 {
1562 return iterator(base_t::emplace(base_t::iterator(position), ETL_NULLPTR));
1563 }
1564
1565 //*************************************************************************
1567 //*************************************************************************
1568 iterator emplace(const_iterator position, const_reference value)
1569 {
1570 return iterator(base_t::emplace(base_t::iterator(position), to_void_ptr(value)));
1571 }
1572
1573 //*********************************************************************
1580 //*********************************************************************
1581 void insert(const_iterator position, size_t n, const_reference value)
1582 {
1583 base_t::insert(base_t::const_iterator(position), n, to_void_ptr(value));
1584 }
1585
1586 //*********************************************************************
1593 //*********************************************************************
1594 template <typename TIterator>
1595 typename etl::enable_if< is_compatible_iterator<TIterator>::value, void>::type insert(const_iterator position, TIterator first, TIterator last)
1596 {
1597 base_t::insert(base_t::const_iterator(position), first, last);
1598 }
1599
1600 //*********************************************************************
1605 //*********************************************************************
1606 iterator erase(iterator i_element)
1607 {
1608 return iterator(base_t::erase(base_t::iterator(i_element)));
1609 }
1610
1611 //*********************************************************************
1616 //*********************************************************************
1617 iterator erase(const_iterator i_element)
1618 {
1619 return iterator(base_t::erase(base_t::const_iterator(i_element)));
1620 }
1621
1622 //*********************************************************************
1630 //*********************************************************************
1631 iterator erase(const_iterator first, const_iterator last)
1632 {
1633 return iterator(base_t::erase(base_t::const_iterator(first), base_t::const_iterator(last)));
1634 }
1635
1636 //*********************************************************************
1640 //*********************************************************************
1641 void swap(ivector<T>& other)
1642 {
1643 if (this == &other)
1644 {
1645 return;
1646 }
1647
1648 ETL_ASSERT_OR_RETURN(this->max_size() >= other.size() && other.max_size() >= this->size(), ETL_ERROR(vector_full));
1649
1650 ivector<T>& smaller = other.size() > this->size() ? *this : other;
1651 ivector<T>& larger = other.size() > this->size() ? other : *this;
1652
1653 etl::swap_ranges(smaller.begin(), smaller.end(), larger.begin());
1654
1655 typename ivector<T>::iterator larger_itr = etl::next(larger.begin(), static_cast<ptrdiff_t>(smaller.size()));
1656
1657 etl::move(larger_itr, larger.end(), etl::back_inserter(smaller));
1658
1659 larger.erase(larger_itr, larger.end());
1660 }
1661
1662 //*************************************************************************
1664 //*************************************************************************
1666 {
1667 base_t::operator=(rhs);
1668
1669 return *this;
1670 }
1671
1672#if ETL_USING_CPP11
1673 //*************************************************************************
1675 //*************************************************************************
1676 ivector& operator=(ivector&& rhs)
1677 {
1678 (void)base_t::operator=(etl::move(rhs));
1679
1680 return *this;
1681 }
1682#endif
1683
1684#ifdef ETL_IVECTOR_REPAIR_ENABLE
1685 //*************************************************************************
1687 //*************************************************************************
1688 virtual void repair() = 0;
1689#endif
1690
1691 protected:
1692
1693 //*********************************************************************
1695 //*********************************************************************
1696 ivector(pointer p_buffer_, size_t MAX_SIZE) ETL_NOEXCEPT
1697 : pvoidvector(to_void_pptr(p_buffer_), MAX_SIZE)
1698 {
1699 }
1700
1701 //*********************************************************************
1703 //*********************************************************************
1705 {
1707 }
1708
1709 //*************************************************************************
1711 //*************************************************************************
1712 void repair_buffer(pointer p_buffer_)
1713 {
1714 base_t::repair_buffer(to_void_pptr(p_buffer_));
1715 }
1716
1717 private:
1718
1719 // Disable copy construction.
1720 ivector(const ivector&) ETL_DELETE;
1721
1722 // Convert from const_reference to void*
1723 static ETL_CONSTEXPR14 void* to_void_ptr(const_reference value) ETL_NOEXCEPT
1724 {
1725 return const_cast<void*>(static_cast<const void*>(value));
1726 }
1727
1728 // Convert from pointer to void**
1729 static ETL_CONSTEXPR14 void** to_void_pptr(pointer p_buffer) ETL_NOEXCEPT
1730 {
1731 return reinterpret_cast<void**>(const_cast<element_type*>(p_buffer));
1732 }
1733 };
1734
1735 //***************************************************************************
1741 //***************************************************************************
1742 template <typename T>
1743 bool operator==(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1744 {
1745 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
1746 }
1747
1748 //***************************************************************************
1754 //***************************************************************************
1755 template <typename T>
1756 bool operator!=(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1757 {
1758 return !(lhs == rhs);
1759 }
1760
1761 //***************************************************************************
1767 //***************************************************************************
1768 template <typename T>
1769 bool operator<(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1770 {
1771 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
1772 }
1773
1774 //***************************************************************************
1780 //***************************************************************************
1781 template <typename T>
1782 bool operator>(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1783 {
1784 return (rhs < lhs);
1785 }
1786
1787 //***************************************************************************
1794 //***************************************************************************
1795 template <typename T>
1796 bool operator<=(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1797 {
1798 return !(lhs > rhs);
1799 }
1800
1801 //***************************************************************************
1808 //***************************************************************************
1809 template <typename T>
1810 bool operator>=(const etl::ivector<T>& lhs, const etl::ivector<T>& rhs)
1811 {
1812 return !(lhs < rhs);
1813 }
1814} // namespace etl
1815
1816namespace etl
1817{
1818 //***************************************************************************
1819 template <typename T, const size_t MAX_SIZE_>
1820 class vector;
1821
1822 //***************************************************************************
1827 //***************************************************************************
1828 template <typename T, const size_t MAX_SIZE_>
1829 class vector : public etl::ivector<T>
1830 {
1831 public:
1832
1833 ETL_STATIC_ASSERT((MAX_SIZE_ > 0U), "Zero capacity etl::vector is not valid");
1834
1835 static const size_t MAX_SIZE = MAX_SIZE_;
1836
1837 //*************************************************************************
1839 //*************************************************************************
1840 vector() ETL_NOEXCEPT
1841 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1842 {
1843 this->initialise();
1844 }
1845
1846 //*************************************************************************
1849 //*************************************************************************
1850 explicit vector(size_t initial_size)
1851 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1852 {
1853 this->initialise();
1854 this->resize(initial_size);
1855 }
1856
1857 //*************************************************************************
1861 //*************************************************************************
1862 vector(size_t initial_size, typename etl::ivector<T>::const_reference value)
1863 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1864 {
1865 this->initialise();
1866 this->resize(initial_size, value);
1867 }
1868
1869 //*************************************************************************
1874 //*************************************************************************
1875 template <typename TIterator>
1876 vector(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
1877 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1878 {
1879 this->assign(first, last);
1880 }
1881
1882#if ETL_HAS_INITIALIZER_LIST
1883 //*************************************************************************
1885 //*************************************************************************
1886 vector(std::initializer_list<T> init)
1887 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1888 {
1889 this->assign(init.begin(), init.end());
1890 }
1891#endif
1892
1893 //*************************************************************************
1895 //*************************************************************************
1896 vector(const vector& other)
1897 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1898 {
1899 this->assign(other.begin(), other.end());
1900 }
1901
1902 //*************************************************************************
1904 //*************************************************************************
1906 {
1907 if (&rhs != this)
1908 {
1909 this->assign(rhs.cbegin(), rhs.cend());
1910 }
1911
1912 return *this;
1913 }
1914
1915#if ETL_USING_CPP11
1916 //*************************************************************************
1918 //*************************************************************************
1919 vector(vector&& other) ETL_NOEXCEPT_IF((etl::is_nothrow_move_constructible<T>::value))
1920 : etl::ivector<T>(reinterpret_cast<T*>(&buffer), MAX_SIZE)
1921 {
1922 if (this != &other)
1923 {
1924 this->initialise();
1925
1926 typename etl::ivector<T>::iterator itr = other.begin();
1927 while (itr != other.end())
1928 {
1929 this->push_back(etl::move(*itr));
1930 ++itr;
1931 }
1932
1933 other.initialise();
1934 }
1935 }
1936
1937 //*************************************************************************
1939 //*************************************************************************
1940 vector& operator=(vector&& rhs) ETL_NOEXCEPT_IF((etl::is_nothrow_move_constructible<T>::value))
1941 {
1942 if (&rhs != this)
1943 {
1944 this->clear();
1945 typename etl::ivector<T>::iterator itr = rhs.begin();
1946 while (itr != rhs.end())
1947 {
1948 this->push_back(etl::move(*itr));
1949 ++itr;
1950 }
1951
1952 rhs.initialise();
1953 }
1954
1955 return *this;
1956 }
1957#endif
1958
1959 //*************************************************************************
1961 //*************************************************************************
1962#ifdef ETL_IVECTOR_REPAIR_ENABLE
1963 virtual
1964#endif
1965 ~vector() ETL_NOEXCEPT
1966 {
1967 this->clear();
1968 }
1969
1970 //*************************************************************************
1972 //*************************************************************************
1973#ifdef ETL_IVECTOR_REPAIR_ENABLE
1974 virtual void repair() ETL_OVERRIDE
1975#else
1976 void repair()
1977#endif
1978 {
1979 ETL_ASSERT_OR_RETURN(etl::is_trivially_copyable<T>::value, ETL_ERROR(etl::vector_incompatible_type));
1980
1982 }
1983
1984 private:
1985
1986 typename etl::aligned_storage<sizeof(T) * MAX_SIZE, etl::alignment_of<T>::value>::type buffer;
1987 };
1988
1989 //*************************************************************************
1991 //*************************************************************************
1992#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST
1993 template <typename... T>
1994 vector(T...) -> vector<typename etl::common_type_t<T...>, sizeof...(T)>;
1995#endif
1996
1997 //*************************************************************************
1999 //*************************************************************************
2000#if ETL_USING_CPP11 && ETL_HAS_INITIALIZER_LIST
2001 template <typename... T>
2002 constexpr auto make_vector(T&&... t) -> etl::vector<typename etl::common_type_t<T...>, sizeof...(T)>
2003 {
2004 return {etl::forward<T>(t)...};
2005 }
2006#endif
2007
2008 //***************************************************************************
2013 //***************************************************************************
2014 template <typename T>
2015 class vector_ext : public etl::ivector<T>
2016 {
2017 public:
2018
2019 //*************************************************************************
2021 //*************************************************************************
2022 vector_ext(void* buffer, size_t max_size) ETL_NOEXCEPT
2023 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2024 {
2025 this->initialise();
2026 }
2027
2028 //*************************************************************************
2031 //*************************************************************************
2032 explicit vector_ext(size_t initial_size, void* buffer, size_t max_size)
2033 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2034 {
2035 this->initialise();
2036 this->resize(initial_size);
2037 }
2038
2039 //*************************************************************************
2043 //*************************************************************************
2044 vector_ext(size_t initial_size, typename etl::ivector<T>::const_reference value, void* buffer, size_t max_size)
2045 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2046 {
2047 this->initialise();
2048 this->resize(initial_size, value);
2049 }
2050
2051 //*************************************************************************
2056 //*************************************************************************
2057 template <typename TIterator>
2058 vector_ext(TIterator first, TIterator last, void* buffer, size_t max_size,
2059 typename etl::enable_if<!etl::is_integral<TIterator>::value, int>::type = 0)
2060 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2061 {
2062 this->assign(first, last);
2063 }
2064
2065#if ETL_HAS_INITIALIZER_LIST
2066 //*************************************************************************
2068 //*************************************************************************
2069 vector_ext(std::initializer_list<T> init, void* buffer, size_t max_size)
2070 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2071 {
2072 this->assign(init.begin(), init.end());
2073 }
2074#endif
2075
2076 //*************************************************************************
2078 //*************************************************************************
2079 vector_ext(const vector_ext& other, void* buffer, size_t max_size)
2080 ETL_NOEXCEPT_IF((etl::is_nothrow_copy_constructible<T>::value && ETL_NOT_USING_EXCEPTIONS))
2081 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2082 {
2083 if (&other != this)
2084 {
2085 this->assign(other.begin(), other.end());
2086 }
2087 }
2088
2089 //*************************************************************************
2091 //*************************************************************************
2092 vector_ext& operator=(const vector_ext& rhs) ETL_NOEXCEPT_IF((etl::is_nothrow_copy_constructible<T>::value && ETL_NOT_USING_EXCEPTIONS))
2093 {
2094 if (&rhs != this)
2095 {
2096 this->assign(rhs.cbegin(), rhs.cend());
2097 }
2098
2099 return *this;
2100 }
2101
2102#if ETL_USING_CPP11
2103 //*************************************************************************
2105 //*************************************************************************
2106 vector_ext(vector_ext&& other, void* buffer, size_t max_size)
2107 ETL_NOEXCEPT_IF((etl::is_nothrow_move_constructible<T>::value && ETL_NOT_USING_EXCEPTIONS))
2108 : etl::ivector<T>(reinterpret_cast<T*>(buffer), max_size)
2109 {
2110 if (&other != this)
2111 {
2112 this->assign(etl::make_move_iterator(other.begin()), etl::make_move_iterator(other.end()));
2113
2114 other.initialise();
2115 }
2116 }
2117
2118 //*************************************************************************
2120 //*************************************************************************
2121 vector_ext& operator=(vector_ext&& rhs) ETL_NOEXCEPT_IF((etl::is_nothrow_move_constructible<T>::value && ETL_NOT_USING_EXCEPTIONS))
2122 {
2123 if (&rhs != this)
2124 {
2125 this->assign(etl::make_move_iterator(rhs.begin()), etl::make_move_iterator(rhs.end()));
2126
2127 rhs.initialise();
2128 }
2129
2130 return *this;
2131 }
2132#endif
2133
2134 //*************************************************************************
2136 //*************************************************************************
2137 ~vector_ext() ETL_NOEXCEPT
2138 {
2139 this->clear();
2140 }
2141
2142 //*************************************************************************
2144 //*************************************************************************
2145#ifdef ETL_IVECTOR_REPAIR_ENABLE
2146 virtual void repair() ETL_OVERRIDE
2147#else
2148 void repair()
2149#endif
2150 {
2151 }
2152 };
2153
2154 //***************************************************************************
2156 //***************************************************************************
2157 template <typename T, typename U>
2159 {
2160 typename etl::ivector<T>::iterator itr = etl::remove(v.begin(), v.end(), value);
2161 typename etl::ivector<T>::difference_type d = etl::distance(itr, v.end());
2162 v.erase(itr, v.end());
2163
2164 return d;
2165 }
2166
2167 //***************************************************************************
2169 //***************************************************************************
2170 template <typename T, typename TPredicate>
2172 {
2173 typename etl::ivector<T>::iterator itr = etl::remove_if(v.begin(), v.end(), predicate);
2174 typename etl::ivector<T>::difference_type d = etl::distance(itr, v.end());
2175 v.erase(itr, v.end());
2176
2177 return d;
2178 }
2179
2180 //*********************************************************************
2184 //*********************************************************************
2185 template <typename T>
2186 void swap(ivector<T>& lhs, ivector<T>& rhs)
2187 {
2188 lhs.swap(rhs);
2189 }
2190} // namespace etl
2191
2192#endif
Definition vector.h:65
ETL_CONSTEXPR14 TIterator remove(TIterator first, TIterator last, const T &value)
Definition algorithm.h:2381
ETL_CONSTEXPR14 TIterator remove_if(TIterator first, TIterator last, TUnaryPredicate predicate)
Definition algorithm.h:2406
Definition alignment.h:251
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_CONSTEXPR17 etl::enable_if<!etl::is_same< T, etl::nullptr_t >::value, T >::type * addressof(T &t)
Definition addressof.h:52
void create_copy_at(T *p, const T &value)
Definition memory.h:2786
etl::enable_if< etl::is_trivially_destructible< typenameetl::iterator_traits< TIterator >::value_type >::value, void >::type destroy(TIterator, TIterator)
Definition memory.h:1893
etl::enable_if< etl::is_trivially_destructible< T >::value, void >::type destroy_at(T *)
Definition memory.h:1819
etl::enable_if< etl::is_trivially_destructible< typenameetl::iterator_traits< TIterator >::value_type >::value, TIterator >::type destroy_n(TIterator i_begin, TSize n)
Definition memory.h:1979
TOutputIterator uninitialized_move(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin)
Definition memory.h:985
TOutputIterator uninitialized_copy(TInputIterator i_begin, TInputIterator i_end, TOutputIterator o_begin)
Definition memory.h:555
TOutputIterator uninitialized_fill_n(TOutputIterator o_begin, TSize n, const T &value)
Definition memory.h:528
etl::enable_if<!etl::is_pointer< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition pvoidvector.h:342
ivector(T *p_buffer_, size_t MAX_SIZE) ETL_NOEXCEPT
Constructor.
Definition vector.h:1095
void resize(size_t new_size, const_reference value)
Definition vector.h:223
iterator erase(const_iterator i_element)
Definition vector.h:1617
iterator emplace(const_iterator position, const_reference value)
Emplaces a value to the vector at the specified position.
Definition vector.h:1568
iterator begin()
Definition pvoidvector.h:76
void resize(size_t new_size, value_type value)
Definition vector.h:1353
void emplace_back(value_type value)
Definition pvoidvector.h:425
iterator erase(iterator i_element)
Definition pvoidvector.h:623
pointer data()
Definition pvoidvector.h:319
size_type max_size() const
Definition vector_base.h:140
ivector & operator=(const ivector &rhs)
Assignment operator.
Definition vector.h:1665
etl::enable_if<!etl::is_integral< TIterator >::value &&etl::is_convertible< typenameetl::iterator_traits< TIterator >::value_type, T >::value, typenameetl::enable_if<!etl::is_integral< TIterator >::value, void >::type >::type assign(TIterator first, TIterator last) ETL_NOEXCEPT_IF((etl
Definition vector.h:396
reference emplace_back(const T1 &value1, const T2 &value2, const T3 &value3, const T4 &value4)
Definition vector.h:570
ivector & operator=(const ivector &rhs)
Assignment operator.
Definition vector.h:1014
void repair_buffer(T *p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition vector.h:1123
vector_ext(TIterator first, TIterator last, void *buffer, size_t max_size, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition vector.h:2058
iterator insert(const_iterator position, const_reference value)
Definition vector.h:1552
vector(size_t initial_size)
Definition vector.h:1850
iterator emplace(const_iterator position, const T1 &value1)
Emplaces a value to the vector at the specified position.
Definition vector.h:687
void initialise()
Initialise the vector.
Definition pvoidvector.h:760
void repair()
Fix the internal pointers after a low level memory copy.
Definition vector.h:1976
vector(const vector &other)
Copy constructor.
Definition vector.h:1896
vector_ext(size_t initial_size, void *buffer, size_t max_size)
Definition vector.h:2032
vector_base(size_t max_size_)
Constructor.
Definition vector_base.h:150
const size_type CAPACITY
The maximum number of elements in the vector.
Definition vector_base.h:170
iterator erase(const_iterator first, const_iterator last)
Definition vector.h:1631
iterator emplace(const_iterator position)
Emplaces a value to the vector at the specified position.
Definition vector.h:1560
iterator emplace(const_iterator position)
Definition pvoidvector.h:488
etl::pvoidvector & operator=(const etl::pvoidvector &rhs)
Assignment operator.
Definition pvoidvector.h:679
vector_ext(void *buffer, size_t max_size) ETL_NOEXCEPT
Constructor.
Definition vector.h:2022
ivector(pointer p_buffer_, size_t MAX_SIZE) ETL_NOEXCEPT
Constructor.
Definition vector.h:1696
void insert(const_iterator position, size_t n, const_reference value)
Definition vector.h:1581
const_reference operator[](size_t i) const
Definition vector.h:1382
vector_ext & operator=(const vector_ext &rhs) ETL_NOEXCEPT_IF((etl
Assignment operator.
Definition vector.h:2092
vector_ext(size_t initial_size, typename etl::ivector< T >::const_reference value, void *buffer, size_t max_size)
Definition vector.h:2044
iterator insert(const_iterator position, value_type value)
Definition pvoidvector.h:453
void clear()
Clears the vector.
Definition pvoidvector.h:401
void assign(size_t n, const_reference value)
Definition vector.h:1486
iterator erase(const_iterator first, const_iterator last)
Definition vector.h:959
etl::enable_if< is_compatible_iterator< TIterator >::value, void >::type assign(TIterator first, TIterator last)
Definition vector.h:1474
void resize(size_t new_size)
Definition pvoidvector.h:186
pointer p_end
Pointer to one past the last element in the buffer.
Definition vector.h:1131
etl::enable_if<!etl::is_integral< TIterator >::value &&etl::is_convertible< typenameetl::iterator_traits< TIterator >::value_type, T >::value, void >::type insert(const_iterator position, TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition vector.h:867
vector() ETL_NOEXCEPT
Constructor.
Definition vector.h:1840
vector(TIterator first, TIterator last, typename etl::enable_if<!etl::is_integral< TIterator >::value, int >::type=0)
Definition vector.h:1876
void pop_back()
Definition pvoidvector.h:436
void repair_buffer(void **p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition pvoidvector.h:768
reference back()
Definition pvoidvector.h:299
iterator end()
Definition pvoidvector.h:94
void repair()
Fix the internal pointers after a low level memory copy.
Definition vector.h:2148
void insert(const_iterator position, size_t n, const_reference value)
Definition vector.h:807
void uninitialized_resize(size_t new_size)
Definition pvoidvector.h:220
etl::enable_if< is_compatible_iterator< TIterator >::value, void >::type insert(const_iterator position, TIterator first, TIterator last)
Definition vector.h:1595
void repair_buffer(pointer p_buffer_)
Fix the internal pointers after a low level memory copy.
Definition vector.h:1712
vector(size_t initial_size, typename etl::ivector< T >::const_reference value)
Definition vector.h:1862
reference front()
Definition pvoidvector.h:279
~vector() ETL_NOEXCEPT
Destructor.
Definition vector.h:1965
pvoidvector(void **p_buffer_, size_t MAX_SIZE)
Constructor.
Definition pvoidvector.h:750
void push_back(value_type value)
Definition pvoidvector.h:412
const_iterator cend() const
Definition pvoidvector.h:121
vector_ext(const vector_ext &other, void *buffer, size_t max_size)
Copy constructor.
Definition vector.h:2079
~vector_ext() ETL_NOEXCEPT
Destructor.
Definition vector.h:2137
const_iterator cbegin() const
Definition pvoidvector.h:112
reference emplace_back(const T1 &value1, const T2 &value2, const T3 &value3)
Definition vector.h:553
iterator insert(const_iterator position, const_reference value)
Definition vector.h:601
reference emplace_back(const_reference value)
Definition vector.h:1529
reference emplace_back(const T1 &value1, const T2 &value2)
Definition vector.h:536
vector & operator=(const vector &rhs)
Assignment operator.
Definition vector.h:1905
reference at(size_t i)
Definition pvoidvector.h:256
Definition pvoidvector.h:55
Definition vector.h:1830
Definition vector_base.h:122
Definition vector_base.h:80
Template deduction guides.
Definition vector.h:2016
Definition vector_base.h:66
Definition vector_base.h:108
Definition vector_base.h:94
Definition absolute.h:40
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1130
TContainer::const_iterator cbegin(const TContainer &container)
Definition iterator.h:1156
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1144
etl::ivector< T >::difference_type erase(etl::ivector< T > &v, const U &value)
erase
Definition vector.h:2158
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
ETL_NODISCARD ETL_CONSTEXPR14 etl::back_insert_iterator< TContainer > back_inserter(TContainer &container)
Creates a back_insert_iterator from a container.
Definition iterator.h:734
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434
TContainer::iterator end(TContainer &container)
Definition iterator.h:1166
TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1186
TContainer::iterator begin(TContainer &container)
Definition iterator.h:1136
etl::ivector< T >::difference_type erase_if(etl::ivector< T > &v, TPredicate predicate)
erase_if
Definition vector.h:2171
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1103
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1117
Definition type_traits.h:97
Definition iterator.h:130
iterator
Definition iterator.h:482
remove_const
Definition type_traits.h:233
remove_cv
Definition type_traits.h:325
remove_reference
Definition type_traits.h:122