57 #define _STL_VECTOR_H 1
62 #if __cplusplus >= 201103L
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
71 template<
typename _Tp,
typename _Alloc>
74 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
75 rebind<_Tp>::other _Tp_alloc_type;
76 typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer
80 :
public _Tp_alloc_type
84 pointer _M_end_of_storage;
87 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
90 _Vector_impl(_Tp_alloc_type
const& __a)
91 : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
94 #if __cplusplus >= 201103L
95 _Vector_impl(_Tp_alloc_type&& __a)
96 : _Tp_alloc_type(std::move(__a)),
97 _M_start(0), _M_finish(0), _M_end_of_storage(0)
101 void _M_swap_data(_Vector_impl& __x)
105 std::swap(_M_end_of_storage, __x._M_end_of_storage);
110 typedef _Alloc allocator_type;
113 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
114 {
return *
static_cast<_Tp_alloc_type*
>(&this->_M_impl); }
116 const _Tp_alloc_type&
117 _M_get_Tp_allocator()
const _GLIBCXX_NOEXCEPT
118 {
return *
static_cast<const _Tp_alloc_type*
>(&this->_M_impl); }
121 get_allocator()
const _GLIBCXX_NOEXCEPT
122 {
return allocator_type(_M_get_Tp_allocator()); }
127 _Vector_base(
const allocator_type& __a)
130 _Vector_base(
size_t __n)
132 { _M_create_storage(__n); }
134 _Vector_base(
size_t __n,
const allocator_type& __a)
136 { _M_create_storage(__n); }
138 #if __cplusplus >= 201103L
139 _Vector_base(_Tp_alloc_type&& __a)
140 : _M_impl(std::move(__a)) { }
142 _Vector_base(_Vector_base&& __x)
143 : _M_impl(std::move(__x._M_get_Tp_allocator()))
144 { this->_M_impl._M_swap_data(__x._M_impl); }
146 _Vector_base(_Vector_base&& __x,
const allocator_type& __a)
149 if (__x.get_allocator() == __a)
150 this->_M_impl._M_swap_data(__x._M_impl);
153 size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
154 _M_create_storage(__n);
160 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
161 - this->_M_impl._M_start); }
164 _Vector_impl _M_impl;
167 _M_allocate(
size_t __n)
168 {
return __n != 0 ? _M_impl.allocate(__n) : 0; }
171 _M_deallocate(pointer __p,
size_t __n)
174 _M_impl.deallocate(__p, __n);
179 _M_create_storage(
size_t __n)
181 this->_M_impl._M_start = this->_M_allocate(__n);
182 this->_M_impl._M_finish = this->_M_impl._M_start;
183 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
209 template<
typename _Tp,
typename _Alloc = std::allocator<_Tp> >
210 class vector :
protected _Vector_base<_Tp, _Alloc>
213 typedef typename _Alloc::value_type _Alloc_value_type;
217 typedef _Vector_base<_Tp, _Alloc> _Base;
218 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
219 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
222 typedef _Tp value_type;
223 typedef typename _Base::pointer pointer;
224 typedef typename _Alloc_traits::const_pointer const_pointer;
225 typedef typename _Alloc_traits::reference reference;
226 typedef typename _Alloc_traits::const_reference const_reference;
227 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
228 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
230 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
231 typedef std::reverse_iterator<iterator> reverse_iterator;
232 typedef size_t size_type;
234 typedef _Alloc allocator_type;
237 using _Base::_M_allocate;
238 using _Base::_M_deallocate;
239 using _Base::_M_impl;
240 using _Base::_M_get_Tp_allocator;
256 vector(
const allocator_type& __a)
259 #if __cplusplus >= 201103L
269 vector(size_type __n,
const allocator_type& __a = allocator_type())
271 { _M_default_initialize(__n); }
281 vector(size_type __n,
const value_type& __value,
282 const allocator_type& __a = allocator_type())
284 { _M_fill_initialize(__n, __value); }
295 vector(size_type __n,
const value_type& __value = value_type(),
296 const allocator_type& __a = allocator_type())
298 { _M_fill_initialize(__n, __value); }
310 vector(
const vector& __x)
312 _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
313 { this->_M_impl._M_finish =
314 std::__uninitialized_copy_a(__x.begin(), __x.end(),
315 this->_M_impl._M_start,
316 _M_get_Tp_allocator());
319 #if __cplusplus >= 201103L
327 vector(vector&& __x) noexcept
328 : _Base(std::move(__x)) { }
331 vector(
const vector& __x,
const allocator_type& __a)
332 : _Base(__x.size(), __a)
333 { this->_M_impl._M_finish =
334 std::__uninitialized_copy_a(__x.begin(), __x.end(),
335 this->_M_impl._M_start,
336 _M_get_Tp_allocator());
340 vector(vector&& __rv,
const allocator_type& __m)
341 : _Base(std::move(__rv), __m)
343 if (__rv.get_allocator() != __m)
345 this->_M_impl._M_finish =
346 std::__uninitialized_move_a(__rv.begin(), __rv.end(),
347 this->_M_impl._M_start,
348 _M_get_Tp_allocator());
364 vector(initializer_list<value_type> __l,
365 const allocator_type& __a = allocator_type())
368 _M_range_initialize(__l.begin(), __l.end(),
369 random_access_iterator_tag());
389 #if __cplusplus >= 201103L
390 template<
typename _InputIterator,
391 typename = std::_RequireInputIter<_InputIterator>>
392 vector(_InputIterator __first, _InputIterator __last,
393 const allocator_type& __a = allocator_type())
395 { _M_initialize_dispatch(__first, __last, __false_type()); }
397 template<
typename _InputIterator>
398 vector(_InputIterator __first, _InputIterator __last,
399 const allocator_type& __a = allocator_type())
403 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
404 _M_initialize_dispatch(__first, __last, _Integral());
414 ~vector() _GLIBCXX_NOEXCEPT
415 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
416 _M_get_Tp_allocator()); }
427 operator=(
const vector& __x);
429 #if __cplusplus >= 201103L
439 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
441 constexpr
bool __move_storage =
442 _Alloc_traits::_S_propagate_on_move_assign()
443 || _Alloc_traits::_S_always_equal();
444 _M_move_assign(std::move(__x),
445 integral_constant<bool, __move_storage>());
461 operator=(initializer_list<value_type> __l)
463 this->assign(__l.begin(), __l.end());
479 assign(size_type __n,
const value_type& __val)
480 { _M_fill_assign(__n, __val); }
494 #if __cplusplus >= 201103L
495 template<
typename _InputIterator,
496 typename = std::_RequireInputIter<_InputIterator>>
498 assign(_InputIterator __first, _InputIterator __last)
499 { _M_assign_dispatch(__first, __last, __false_type()); }
501 template<
typename _InputIterator>
503 assign(_InputIterator __first, _InputIterator __last)
506 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
507 _M_assign_dispatch(__first, __last, _Integral());
511 #if __cplusplus >= 201103L
524 assign(initializer_list<value_type> __l)
525 { this->assign(__l.begin(), __l.end()); }
529 using _Base::get_allocator;
538 begin() _GLIBCXX_NOEXCEPT
539 {
return iterator(this->_M_impl._M_start); }
547 begin()
const _GLIBCXX_NOEXCEPT
548 {
return const_iterator(this->_M_impl._M_start); }
556 end() _GLIBCXX_NOEXCEPT
557 {
return iterator(this->_M_impl._M_finish); }
565 end()
const _GLIBCXX_NOEXCEPT
566 {
return const_iterator(this->_M_impl._M_finish); }
574 rbegin() _GLIBCXX_NOEXCEPT
575 {
return reverse_iterator(end()); }
582 const_reverse_iterator
583 rbegin()
const _GLIBCXX_NOEXCEPT
584 {
return const_reverse_iterator(end()); }
592 rend() _GLIBCXX_NOEXCEPT
593 {
return reverse_iterator(begin()); }
600 const_reverse_iterator
601 rend()
const _GLIBCXX_NOEXCEPT
602 {
return const_reverse_iterator(begin()); }
604 #if __cplusplus >= 201103L
611 cbegin()
const noexcept
612 {
return const_iterator(this->_M_impl._M_start); }
620 cend()
const noexcept
621 {
return const_iterator(this->_M_impl._M_finish); }
628 const_reverse_iterator
629 crbegin()
const noexcept
630 {
return const_reverse_iterator(end()); }
637 const_reverse_iterator
638 crend()
const noexcept
639 {
return const_reverse_iterator(begin()); }
645 size()
const _GLIBCXX_NOEXCEPT
646 {
return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
650 max_size()
const _GLIBCXX_NOEXCEPT
651 {
return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
653 #if __cplusplus >= 201103L
664 resize(size_type __new_size)
666 if (__new_size > size())
667 _M_default_append(__new_size - size());
668 else if (__new_size < size())
669 _M_erase_at_end(this->_M_impl._M_start + __new_size);
684 resize(size_type __new_size,
const value_type& __x)
686 if (__new_size > size())
687 insert(end(), __new_size - size(), __x);
688 else if (__new_size < size())
689 _M_erase_at_end(this->_M_impl._M_start + __new_size);
704 resize(size_type __new_size, value_type __x = value_type())
706 if (__new_size > size())
707 insert(end(), __new_size - size(), __x);
708 else if (__new_size < size())
709 _M_erase_at_end(this->_M_impl._M_start + __new_size);
713 #if __cplusplus >= 201103L
717 { _M_shrink_to_fit(); }
725 capacity()
const _GLIBCXX_NOEXCEPT
726 {
return size_type(this->_M_impl._M_end_of_storage
727 - this->_M_impl._M_start); }
734 empty()
const _GLIBCXX_NOEXCEPT
735 {
return begin() == end(); }
755 reserve(size_type __n);
770 operator[](size_type __n)
771 {
return *(this->_M_impl._M_start + __n); }
785 operator[](size_type __n)
const
786 {
return *(this->_M_impl._M_start + __n); }
791 _M_range_check(size_type __n)
const
793 if (__n >= this->size())
794 __throw_out_of_range(__N(
"vector::_M_range_check"));
828 at(size_type __n)
const
856 {
return *(end() - 1); }
864 {
return *(end() - 1); }
873 #if __cplusplus >= 201103L
878 data() _GLIBCXX_NOEXCEPT
879 {
return std::__addressof(front()); }
881 #if __cplusplus >= 201103L
886 data()
const _GLIBCXX_NOEXCEPT
887 {
return std::__addressof(front()); }
901 push_back(
const value_type& __x)
903 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
905 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
907 ++this->_M_impl._M_finish;
910 #if __cplusplus >= 201103L
911 _M_emplace_back_aux(__x);
913 _M_insert_aux(end(), __x);
917 #if __cplusplus >= 201103L
919 push_back(value_type&& __x)
920 { emplace_back(std::move(__x)); }
922 template<
typename... _Args>
924 emplace_back(_Args&&... __args);
939 --this->_M_impl._M_finish;
940 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
943 #if __cplusplus >= 201103L
956 template<
typename... _Args>
958 emplace(iterator __position, _Args&&... __args);
973 insert(iterator __position,
const value_type& __x);
975 #if __cplusplus >= 201103L
988 insert(iterator __position, value_type&& __x)
989 {
return emplace(__position, std::move(__x)); }
1005 insert(iterator __position, initializer_list<value_type> __l)
1006 { this->insert(__position, __l.begin(), __l.end()); }
1023 insert(iterator __position, size_type __n,
const value_type& __x)
1024 { _M_fill_insert(__position, __n, __x); }
1040 #if __cplusplus >= 201103L
1041 template<
typename _InputIterator,
1042 typename = std::_RequireInputIter<_InputIterator>>
1044 insert(iterator __position, _InputIterator __first,
1045 _InputIterator __last)
1046 { _M_insert_dispatch(__position, __first, __last, __false_type()); }
1048 template<
typename _InputIterator>
1050 insert(iterator __position, _InputIterator __first,
1051 _InputIterator __last)
1054 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1055 _M_insert_dispatch(__position, __first, __last, _Integral());
1075 erase(iterator __position);
1096 erase(iterator __first, iterator __last);
1109 #if __cplusplus >= 201103L
1110 noexcept(_Alloc_traits::_S_nothrow_swap())
1113 this->_M_impl._M_swap_data(__x._M_impl);
1114 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1115 __x._M_get_Tp_allocator());
1125 clear() _GLIBCXX_NOEXCEPT
1126 { _M_erase_at_end(this->_M_impl._M_start); }
1133 template<
typename _ForwardIterator>
1135 _M_allocate_and_copy(size_type __n,
1136 _ForwardIterator __first, _ForwardIterator __last)
1138 pointer __result = this->_M_allocate(__n);
1141 std::__uninitialized_copy_a(__first, __last, __result,
1142 _M_get_Tp_allocator());
1147 _M_deallocate(__result, __n);
1159 template<
typename _Integer>
1161 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1163 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1164 this->_M_impl._M_end_of_storage =
1165 this->_M_impl._M_start +
static_cast<size_type
>(__n);
1166 _M_fill_initialize(static_cast<size_type>(__n), __value);
1170 template<
typename _InputIterator>
1172 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1175 typedef typename std::iterator_traits<_InputIterator>::
1176 iterator_category _IterCategory;
1177 _M_range_initialize(__first, __last, _IterCategory());
1181 template<
typename _InputIterator>
1183 _M_range_initialize(_InputIterator __first,
1184 _InputIterator __last, std::input_iterator_tag)
1186 for (; __first != __last; ++__first)
1187 #
if __cplusplus >= 201103L
1188 emplace_back(*__first);
1190 push_back(*__first);
1195 template<
typename _ForwardIterator>
1197 _M_range_initialize(_ForwardIterator __first,
1198 _ForwardIterator __last, std::forward_iterator_tag)
1200 const size_type __n = std::distance(__first, __last);
1201 this->_M_impl._M_start = this->_M_allocate(__n);
1202 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1203 this->_M_impl._M_finish =
1204 std::__uninitialized_copy_a(__first, __last,
1205 this->_M_impl._M_start,
1206 _M_get_Tp_allocator());
1212 _M_fill_initialize(size_type __n,
const value_type& __value)
1214 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1215 _M_get_Tp_allocator());
1216 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1219 #if __cplusplus >= 201103L
1222 _M_default_initialize(size_type __n)
1224 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1225 _M_get_Tp_allocator());
1226 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1237 template<
typename _Integer>
1239 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1240 { _M_fill_assign(__n, __val); }
1243 template<
typename _InputIterator>
1245 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1248 typedef typename std::iterator_traits<_InputIterator>::
1249 iterator_category _IterCategory;
1250 _M_assign_aux(__first, __last, _IterCategory());
1254 template<
typename _InputIterator>
1256 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1257 std::input_iterator_tag);
1260 template<
typename _ForwardIterator>
1262 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1263 std::forward_iterator_tag);
1268 _M_fill_assign(size_type __n,
const value_type& __val);
1277 template<
typename _Integer>
1279 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1281 { _M_fill_insert(__pos, __n, __val); }
1284 template<
typename _InputIterator>
1286 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1287 _InputIterator __last, __false_type)
1289 typedef typename std::iterator_traits<_InputIterator>::
1290 iterator_category _IterCategory;
1291 _M_range_insert(__pos, __first, __last, _IterCategory());
1295 template<
typename _InputIterator>
1297 _M_range_insert(iterator __pos, _InputIterator __first,
1298 _InputIterator __last, std::input_iterator_tag);
1301 template<
typename _ForwardIterator>
1303 _M_range_insert(iterator __pos, _ForwardIterator __first,
1304 _ForwardIterator __last, std::forward_iterator_tag);
1309 _M_fill_insert(iterator __pos, size_type __n,
const value_type& __x);
1311 #if __cplusplus >= 201103L
1314 _M_default_append(size_type __n);
1321 #if __cplusplus < 201103L
1323 _M_insert_aux(iterator __position,
const value_type& __x);
1325 template<
typename... _Args>
1327 _M_insert_aux(iterator __position, _Args&&... __args);
1329 template<
typename... _Args>
1331 _M_emplace_back_aux(_Args&&... __args);
1336 _M_check_len(size_type __n,
const char* __s)
const
1338 if (max_size() - size() < __n)
1339 __throw_length_error(__N(__s));
1341 const size_type __len = size() +
std::max(size(), __n);
1342 return (__len < size() || __len > max_size()) ? max_size() : __len;
1350 _M_erase_at_end(pointer __pos)
1352 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1353 this->_M_impl._M_finish = __pos;
1356 #if __cplusplus >= 201103L
1364 vector __tmp(get_allocator());
1365 this->_M_impl._M_swap_data(__tmp._M_impl);
1366 this->_M_impl._M_swap_data(__x._M_impl);
1367 if (_Alloc_traits::_S_propagate_on_move_assign())
1368 std::__alloc_on_move(_M_get_Tp_allocator(),
1369 __x._M_get_Tp_allocator());
1377 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1383 this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
1384 std::__make_move_if_noexcept_iterator(__x.end()));
1402 template<
typename _Tp,
typename _Alloc>
1404 operator==(
const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1405 {
return (__x.size() == __y.size()
1406 && std::equal(__x.begin(), __x.end(), __y.begin())); }
1419 template<
typename _Tp,
typename _Alloc>
1421 operator<(const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1422 {
return std::lexicographical_compare(__x.begin(), __x.end(),
1423 __y.begin(), __y.end()); }
1426 template<
typename _Tp,
typename _Alloc>
1428 operator!=(
const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1429 {
return !(__x == __y); }
1432 template<
typename _Tp,
typename _Alloc>
1434 operator>(
const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1435 {
return __y < __x; }
1438 template<
typename _Tp,
typename _Alloc>
1440 operator<=(const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1441 {
return !(__y < __x); }
1444 template<
typename _Tp,
typename _Alloc>
1446 operator>=(
const vector<_Tp, _Alloc>& __x,
const vector<_Tp, _Alloc>& __y)
1447 {
return !(__x < __y); }
1450 template<
typename _Tp,
typename _Alloc>
1452 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1455 _GLIBCXX_END_NAMESPACE_CONTAINER
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define __try
Definition: exception_defines.h:35
#define __throw_exception_again
Definition: exception_defines.h:37
#define __glibcxx_class_requires(_a, _b)
Definition: concept_check.h:48
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
namespace std _GLIBCXX_VISIBILITY(default)
Definition: stl_vector.h:66
#define __glibcxx_class_requires2(_a, _b, _c)
Definition: concept_check.h:49
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
const _Tp & max(const _Tp &__a, const _Tp &__b)
Equivalent to std::max.
Definition: base.h:150
std::tr1::integral_constant< int, 1 > true_type
Definition: type_utils.hpp:70
#define __catch(X)
Definition: exception_defines.h:36
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160
std::tr1::integral_constant< int, 0 > false_type
Definition: type_utils.hpp:71