57 #define _STL_VECTOR_H 1
62 #if __cplusplus >= 201103L
66 namespace std _GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
71 template<
typename _Tp,
typename _Alloc>
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)
103 std::swap(_M_start, __x._M_start);
104 std::swap(_M_finish, __x._M_finish);
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()); }
132 { _M_create_storage(__n); }
136 { _M_create_storage(__n); }
138 #if __cplusplus >= 201103L
140 : _M_impl(std::move(__a)) { }
143 : _M_impl(std::move(__x._M_get_Tp_allocator()))
144 { this->_M_impl._M_swap_data(__x._M_impl); }
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> >
213 typedef typename _Alloc::value_type _Alloc_value_type;
214 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
215 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
218 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
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>
232 typedef size_t size_type;
233 typedef ptrdiff_t difference_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;
259 #if __cplusplus >= 201103L
271 { _M_default_initialize(__n); }
281 vector(size_type __n,
const value_type& __value,
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); }
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
328 :
_Base(std::move(__x)) { }
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());
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());
368 _M_range_initialize(__l.begin(), __l.end(),
389 #if __cplusplus >= 201103L
390 template<
typename _InputIterator,
391 typename = std::_RequireInputIter<_InputIterator>>
392 vector(_InputIterator __first, _InputIterator __last,
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());
415 {
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
416 _M_get_Tp_allocator()); }
429 #if __cplusplus >= 201103L
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),
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
525 { this->
assign(__l.begin(), __l.end()); }
529 using _Base::get_allocator;
539 {
return iterator(this->_M_impl._M_start); }
548 {
return const_iterator(this->_M_impl._M_start); }
557 {
return iterator(this->_M_impl._M_finish); }
565 end() const _GLIBCXX_NOEXCEPT
566 {
return const_iterator(this->_M_impl._M_finish); }
582 const_reverse_iterator
600 const_reverse_iterator
604 #if __cplusplus >= 201103L
612 {
return const_iterator(this->_M_impl._M_start); }
621 {
return const_iterator(this->_M_impl._M_finish); }
628 const_reverse_iterator
637 const_reverse_iterator
646 {
return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
653 #if __cplusplus >= 201103L
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())
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())
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(); }
726 {
return size_type(this->_M_impl._M_end_of_storage
727 - this->_M_impl._M_start); }
771 {
return *(this->_M_impl._M_start + __n); }
786 {
return *(this->_M_impl._M_start + __n); }
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
881 #if __cplusplus >= 201103L
886 data() const _GLIBCXX_NOEXCEPT
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
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(const_iterator __position, _Args&&... __args);
972 insert(const_iterator __position,
const value_type& __x);
989 #if __cplusplus >= 201103L
1002 insert(const_iterator __position, value_type&& __x)
1003 {
return emplace(__position, std::move(__x)); }
1020 {
return this->
insert(__position, __l.begin(), __l.end()); }
1023 #if __cplusplus >= 201103L
1039 insert(const_iterator __position, size_type __n,
const value_type& __x)
1041 difference_type __offset = __position -
cbegin();
1042 _M_fill_insert(__position._M_const_cast(), __n, __x);
1043 return begin() + __offset;
1060 insert(
iterator __position, size_type __n,
const value_type& __x)
1061 { _M_fill_insert(__position, __n, __x); }
1064 #if __cplusplus >= 201103L
1080 template<
typename _InputIterator,
1081 typename = std::_RequireInputIter<_InputIterator>>
1083 insert(const_iterator __position, _InputIterator __first,
1084 _InputIterator __last)
1086 difference_type __offset = __position -
cbegin();
1087 _M_insert_dispatch(__position._M_const_cast(),
1088 __first, __last, __false_type());
1089 return begin() + __offset;
1106 template<
typename _InputIterator>
1109 _InputIterator __last)
1112 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1113 _M_insert_dispatch(__position, __first, __last, _Integral());
1133 #if __cplusplus >= 201103L
1136 erase(iterator __position)
1138 {
return _M_erase(__position._M_const_cast()); }
1159 #if __cplusplus >= 201103L
1160 erase(const_iterator __first, const_iterator __last)
1162 erase(iterator __first, iterator __last)
1164 {
return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1177 #if __cplusplus >= 201103L
1178 noexcept(_Alloc_traits::_S_nothrow_swap())
1181 this->_M_impl._M_swap_data(__x._M_impl);
1182 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1183 __x._M_get_Tp_allocator());
1194 { _M_erase_at_end(this->_M_impl._M_start); }
1201 template<
typename _ForwardIterator>
1204 _ForwardIterator __first, _ForwardIterator __last)
1206 pointer __result = this->_M_allocate(__n);
1209 std::__uninitialized_copy_a(__first, __last, __result,
1210 _M_get_Tp_allocator());
1215 _M_deallocate(__result, __n);
1216 __throw_exception_again;
1227 template<
typename _Integer>
1229 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1231 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1232 this->_M_impl._M_end_of_storage =
1233 this->_M_impl._M_start +
static_cast<size_type
>(__n);
1234 _M_fill_initialize(static_cast<size_type>(__n), __value);
1238 template<
typename _InputIterator>
1240 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1243 typedef typename std::iterator_traits<_InputIterator>::
1244 iterator_category _IterCategory;
1245 _M_range_initialize(__first, __last, _IterCategory());
1249 template<
typename _InputIterator>
1251 _M_range_initialize(_InputIterator __first,
1254 for (; __first != __last; ++__first)
1255 #
if __cplusplus >= 201103L
1256 emplace_back(*__first);
1263 template<
typename _ForwardIterator>
1265 _M_range_initialize(_ForwardIterator __first,
1269 this->_M_impl._M_start = this->_M_allocate(__n);
1270 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1271 this->_M_impl._M_finish =
1272 std::__uninitialized_copy_a(__first, __last,
1273 this->_M_impl._M_start,
1274 _M_get_Tp_allocator());
1280 _M_fill_initialize(size_type __n,
const value_type& __value)
1282 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1283 _M_get_Tp_allocator());
1284 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1287 #if __cplusplus >= 201103L
1290 _M_default_initialize(size_type __n)
1292 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1293 _M_get_Tp_allocator());
1294 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1305 template<
typename _Integer>
1307 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1308 { _M_fill_assign(__n, __val); }
1311 template<
typename _InputIterator>
1313 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1316 typedef typename std::iterator_traits<_InputIterator>::
1317 iterator_category _IterCategory;
1318 _M_assign_aux(__first, __last, _IterCategory());
1322 template<
typename _InputIterator>
1324 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1328 template<
typename _ForwardIterator>
1330 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1336 _M_fill_assign(size_type __n,
const value_type& __val);
1345 template<
typename _Integer>
1347 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1349 { _M_fill_insert(__pos, __n, __val); }
1352 template<
typename _InputIterator>
1354 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1355 _InputIterator __last, __false_type)
1357 typedef typename std::iterator_traits<_InputIterator>::
1358 iterator_category _IterCategory;
1359 _M_range_insert(__pos, __first, __last, _IterCategory());
1363 template<
typename _InputIterator>
1365 _M_range_insert(iterator __pos, _InputIterator __first,
1369 template<
typename _ForwardIterator>
1371 _M_range_insert(iterator __pos, _ForwardIterator __first,
1377 _M_fill_insert(iterator __pos, size_type __n,
const value_type& __x);
1379 #if __cplusplus >= 201103L
1382 _M_default_append(size_type __n);
1389 #if __cplusplus < 201103L
1391 _M_insert_aux(iterator __position,
const value_type& __x);
1393 template<
typename... _Args>
1395 _M_insert_aux(iterator __position, _Args&&... __args);
1397 template<
typename... _Args>
1399 _M_emplace_back_aux(_Args&&... __args);
1404 _M_check_len(size_type __n,
const char* __s)
const
1407 __throw_length_error(__N(__s));
1418 _M_erase_at_end(pointer __pos)
1420 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1421 this->_M_impl._M_finish = __pos;
1425 _M_erase(iterator __position);
1428 _M_erase(iterator __first, iterator __last);
1430 #if __cplusplus >= 201103L
1438 const vector __tmp(std::move(*
this));
1439 this->_M_impl._M_swap_data(__x._M_impl);
1440 if (_Alloc_traits::_S_propagate_on_move_assign())
1441 std::__alloc_on_move(_M_get_Tp_allocator(),
1442 __x._M_get_Tp_allocator());
1450 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1456 this->
assign(std::__make_move_if_noexcept_iterator(__x.
begin()),
1457 std::__make_move_if_noexcept_iterator(__x.
end()));
1475 template<
typename _Tp,
typename _Alloc>
1492 template<
typename _Tp,
typename _Alloc>
1496 __y.begin(), __y.end()); }
1499 template<
typename _Tp,
typename _Alloc>
1502 {
return !(__x == __y); }
1505 template<
typename _Tp,
typename _Alloc>
1508 {
return __y < __x; }
1511 template<
typename _Tp,
typename _Alloc>
1514 {
return !(__y < __x); }
1517 template<
typename _Tp,
typename _Alloc>
1520 {
return !(__x < __y); }
1523 template<
typename _Tp,
typename _Alloc>
1528 _GLIBCXX_END_NAMESPACE_CONTAINER