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) _GLIBCXX_NOEXCEPT
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) noexcept
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) _GLIBCXX_NOEXCEPT
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) _GLIBCXX_NOEXCEPT
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;
250 vector(
const allocator_type& __a = allocator_type()) _GLIBCXX_NOEXCEPT
253 #if __cplusplus >= 201103L
263 vector(size_type __n,
const allocator_type& __a = allocator_type())
265 { _M_default_initialize(__n); }
275 vector(size_type __n,
const value_type& __value,
276 const allocator_type& __a = allocator_type())
278 { _M_fill_initialize(__n, __value); }
289 vector(size_type __n,
const value_type& __value = value_type(),
290 const allocator_type& __a = allocator_type())
292 { _M_fill_initialize(__n, __value); }
307 { this->_M_impl._M_finish =
308 std::__uninitialized_copy_a(__x.
begin(), __x.
end(),
309 this->_M_impl._M_start,
310 _M_get_Tp_allocator());
313 #if __cplusplus >= 201103L
322 :
_Base(std::move(__x)) { }
327 { this->_M_impl._M_finish =
328 std::__uninitialized_copy_a(__x.
begin(), __x.
end(),
329 this->_M_impl._M_start,
330 _M_get_Tp_allocator());
335 noexcept(_Alloc_traits::_S_always_equal())
338 if (__rv.get_allocator() != __m)
340 this->_M_impl._M_finish =
341 std::__uninitialized_move_a(__rv.begin(), __rv.end(),
342 this->_M_impl._M_start,
343 _M_get_Tp_allocator());
360 const allocator_type& __a = allocator_type())
363 _M_range_initialize(__l.begin(), __l.end(),
384 #if __cplusplus >= 201103L
385 template<
typename _InputIterator,
386 typename = std::_RequireInputIter<_InputIterator>>
387 vector(_InputIterator __first, _InputIterator __last,
388 const allocator_type& __a = allocator_type())
390 { _M_initialize_dispatch(__first, __last, __false_type()); }
392 template<
typename _InputIterator>
393 vector(_InputIterator __first, _InputIterator __last,
394 const allocator_type& __a = allocator_type())
398 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
399 _M_initialize_dispatch(__first, __last, _Integral());
409 ~vector() _GLIBCXX_NOEXCEPT
410 {
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
411 _M_get_Tp_allocator()); }
424 #if __cplusplus >= 201103L
436 constexpr
bool __move_storage =
437 _Alloc_traits::_S_propagate_on_move_assign()
438 || _Alloc_traits::_S_always_equal();
439 _M_move_assign(std::move(__x),
458 this->
assign(__l.begin(), __l.end());
474 assign(size_type __n,
const value_type& __val)
475 { _M_fill_assign(__n, __val); }
489 #if __cplusplus >= 201103L
490 template<
typename _InputIterator,
491 typename = std::_RequireInputIter<_InputIterator>>
493 assign(_InputIterator __first, _InputIterator __last)
494 { _M_assign_dispatch(__first, __last, __false_type()); }
496 template<
typename _InputIterator>
498 assign(_InputIterator __first, _InputIterator __last)
501 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
502 _M_assign_dispatch(__first, __last, _Integral());
506 #if __cplusplus >= 201103L
520 { this->
assign(__l.begin(), __l.end()); }
524 using _Base::get_allocator;
534 {
return iterator(this->_M_impl._M_start); }
543 {
return const_iterator(this->_M_impl._M_start); }
552 {
return iterator(this->_M_impl._M_finish); }
560 end() const _GLIBCXX_NOEXCEPT
561 {
return const_iterator(this->_M_impl._M_finish); }
577 const_reverse_iterator
595 const_reverse_iterator
599 #if __cplusplus >= 201103L
607 {
return const_iterator(this->_M_impl._M_start); }
616 {
return const_iterator(this->_M_impl._M_finish); }
623 const_reverse_iterator
632 const_reverse_iterator
641 {
return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
648 #if __cplusplus >= 201103L
661 if (__new_size >
size())
662 _M_default_append(__new_size -
size());
663 else if (__new_size <
size())
664 _M_erase_at_end(this->_M_impl._M_start + __new_size);
679 resize(size_type __new_size,
const value_type& __x)
681 if (__new_size >
size())
683 else if (__new_size <
size())
684 _M_erase_at_end(this->_M_impl._M_start + __new_size);
699 resize(size_type __new_size, value_type __x = value_type())
701 if (__new_size >
size())
703 else if (__new_size <
size())
704 _M_erase_at_end(this->_M_impl._M_start + __new_size);
708 #if __cplusplus >= 201103L
712 { _M_shrink_to_fit(); }
721 {
return size_type(this->_M_impl._M_end_of_storage
722 - this->_M_impl._M_start); }
766 {
return *(this->_M_impl._M_start + __n); }
781 {
return *(this->_M_impl._M_start + __n); }
788 if (__n >= this->
size())
789 __throw_out_of_range_fmt(__N(
"vector::_M_range_check: __n "
790 "(which is %zu) >= this->size() "
826 at(size_type __n)
const
854 {
return *(
end() - 1); }
862 {
return *(
end() - 1); }
871 #if __cplusplus >= 201103L
879 #if __cplusplus >= 201103L
884 data() const _GLIBCXX_NOEXCEPT
901 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
903 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
905 ++this->_M_impl._M_finish;
908 #if __cplusplus >= 201103L
909 _M_emplace_back_aux(__x);
911 _M_insert_aux(
end(), __x);
915 #if __cplusplus >= 201103L
918 { emplace_back(std::move(__x)); }
920 template<
typename... _Args>
922 emplace_back(_Args&&... __args);
937 --this->_M_impl._M_finish;
938 _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
941 #if __cplusplus >= 201103L
954 template<
typename... _Args>
956 emplace(const_iterator __position, _Args&&... __args);
970 insert(const_iterator __position,
const value_type& __x);
987 #if __cplusplus >= 201103L
1000 insert(const_iterator __position, value_type&& __x)
1001 {
return emplace(__position, std::move(__x)); }
1018 {
return this->
insert(__position, __l.begin(), __l.end()); }
1021 #if __cplusplus >= 201103L
1037 insert(const_iterator __position, size_type __n,
const value_type& __x)
1039 difference_type __offset = __position -
cbegin();
1040 _M_fill_insert(__position._M_const_cast(), __n, __x);
1041 return begin() + __offset;
1058 insert(
iterator __position, size_type __n,
const value_type& __x)
1059 { _M_fill_insert(__position, __n, __x); }
1062 #if __cplusplus >= 201103L
1078 template<
typename _InputIterator,
1079 typename = std::_RequireInputIter<_InputIterator>>
1081 insert(const_iterator __position, _InputIterator __first,
1082 _InputIterator __last)
1084 difference_type __offset = __position -
cbegin();
1085 _M_insert_dispatch(__position._M_const_cast(),
1086 __first, __last, __false_type());
1087 return begin() + __offset;
1104 template<
typename _InputIterator>
1107 _InputIterator __last)
1110 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1111 _M_insert_dispatch(__position, __first, __last, _Integral());
1131 #if __cplusplus >= 201103L
1132 erase(const_iterator __position)
1134 erase(iterator __position)
1136 {
return _M_erase(__position._M_const_cast()); }
1157 #if __cplusplus >= 201103L
1158 erase(const_iterator __first, const_iterator __last)
1162 {
return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1175 #if __cplusplus >= 201103L
1176 noexcept(_Alloc_traits::_S_nothrow_swap())
1179 this->_M_impl._M_swap_data(__x._M_impl);
1180 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1181 __x._M_get_Tp_allocator());
1192 { _M_erase_at_end(this->_M_impl._M_start); }
1199 template<
typename _ForwardIterator>
1202 _ForwardIterator __first, _ForwardIterator __last)
1204 pointer __result = this->_M_allocate(__n);
1207 std::__uninitialized_copy_a(__first, __last, __result,
1208 _M_get_Tp_allocator());
1213 _M_deallocate(__result, __n);
1214 __throw_exception_again;
1225 template<
typename _Integer>
1227 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1229 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1230 this->_M_impl._M_end_of_storage =
1231 this->_M_impl._M_start +
static_cast<size_type
>(__n);
1232 _M_fill_initialize(static_cast<size_type>(__n), __value);
1236 template<
typename _InputIterator>
1238 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1241 typedef typename std::iterator_traits<_InputIterator>::
1242 iterator_category _IterCategory;
1243 _M_range_initialize(__first, __last, _IterCategory());
1247 template<
typename _InputIterator>
1249 _M_range_initialize(_InputIterator __first,
1252 for (; __first != __last; ++__first)
1253 #
if __cplusplus >= 201103L
1254 emplace_back(*__first);
1261 template<
typename _ForwardIterator>
1263 _M_range_initialize(_ForwardIterator __first,
1267 this->_M_impl._M_start = this->_M_allocate(__n);
1268 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1269 this->_M_impl._M_finish =
1270 std::__uninitialized_copy_a(__first, __last,
1271 this->_M_impl._M_start,
1272 _M_get_Tp_allocator());
1278 _M_fill_initialize(size_type __n,
const value_type& __value)
1280 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1281 _M_get_Tp_allocator());
1282 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1285 #if __cplusplus >= 201103L
1288 _M_default_initialize(size_type __n)
1290 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1291 _M_get_Tp_allocator());
1292 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1303 template<
typename _Integer>
1305 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1306 { _M_fill_assign(__n, __val); }
1309 template<
typename _InputIterator>
1311 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1314 typedef typename std::iterator_traits<_InputIterator>::
1315 iterator_category _IterCategory;
1316 _M_assign_aux(__first, __last, _IterCategory());
1320 template<
typename _InputIterator>
1322 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1326 template<
typename _ForwardIterator>
1328 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1334 _M_fill_assign(size_type __n,
const value_type& __val);
1343 template<
typename _Integer>
1345 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1347 { _M_fill_insert(__pos, __n, __val); }
1350 template<
typename _InputIterator>
1352 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1353 _InputIterator __last, __false_type)
1355 typedef typename std::iterator_traits<_InputIterator>::
1356 iterator_category _IterCategory;
1357 _M_range_insert(__pos, __first, __last, _IterCategory());
1361 template<
typename _InputIterator>
1363 _M_range_insert(iterator __pos, _InputIterator __first,
1367 template<
typename _ForwardIterator>
1369 _M_range_insert(iterator __pos, _ForwardIterator __first,
1375 _M_fill_insert(iterator __pos, size_type __n,
const value_type& __x);
1377 #if __cplusplus >= 201103L
1380 _M_default_append(size_type __n);
1387 #if __cplusplus < 201103L
1389 _M_insert_aux(iterator __position,
const value_type& __x);
1391 template<
typename... _Args>
1393 _M_insert_aux(iterator __position, _Args&&... __args);
1395 template<
typename... _Args>
1397 _M_emplace_back_aux(_Args&&... __args);
1402 _M_check_len(size_type __n,
const char* __s)
const
1405 __throw_length_error(__N(__s));
1416 _M_erase_at_end(pointer __pos) _GLIBCXX_NOEXCEPT
1418 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1419 this->_M_impl._M_finish = __pos;
1423 _M_erase(iterator __position);
1426 _M_erase(iterator __first, iterator __last);
1428 #if __cplusplus >= 201103L
1436 const vector __tmp(std::move(*
this));
1437 this->_M_impl._M_swap_data(__x._M_impl);
1438 if (_Alloc_traits::_S_propagate_on_move_assign())
1439 std::__alloc_on_move(_M_get_Tp_allocator(),
1440 __x._M_get_Tp_allocator());
1448 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1454 this->
assign(std::__make_move_if_noexcept_iterator(__x.begin()),
1455 std::__make_move_if_noexcept_iterator(__x.end()));
1473 template<
typename _Tp,
typename _Alloc>
1490 template<
typename _Tp,
typename _Alloc>
1494 __y.begin(), __y.end()); }
1497 template<
typename _Tp,
typename _Alloc>
1500 {
return !(__x == __y); }
1503 template<
typename _Tp,
typename _Alloc>
1506 {
return __y < __x; }
1509 template<
typename _Tp,
typename _Alloc>
1512 {
return !(__y < __x); }
1515 template<
typename _Tp,
typename _Alloc>
1518 {
return !(__x < __y); }
1521 template<
typename _Tp,
typename _Alloc>
1526 _GLIBCXX_END_NAMESPACE_CONTAINER
reverse_iterator rbegin() noexcept
void resize(size_type __new_size, const value_type &__x)
Resizes the vector to the specified number of elements.
void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a vector.
iterator insert(const_iterator __position, value_type &&__x)
Inserts given rvalue into vector before specified iterator.
reference back() noexcept
See bits/stl_deque.h's _Deque_base for an explanation.
Forward iterators support a superset of input iterator operations.
vector(const allocator_type &__a=allocator_type()) noexcept
Creates a vector with no elements.
Uniform interface to C++98 and C++0x allocators.
vector(vector &&__x) noexcept
Vector move constructor.
const_reference back() const noexcept
vector(const vector &__x, const allocator_type &__a)
Copy constructor with alternative allocator.
void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
const_reverse_iterator rbegin() const noexcept
vector(size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
Creates a vector with copies of an exemplar element.
vector(const vector &__x)
Vector copy constructor.
void assign(initializer_list< value_type > __l)
Assigns an initializer list to a vector.
size_type max_size() const noexcept
reverse_iterator rend() noexcept
const_iterator begin() const noexcept
iterator emplace(const_iterator __position, _Args &&...__args)
Inserts an object in vector before specified iterator.
reference operator[](size_type __n) noexcept
Subscript access to the data contained in the vector.
reference front() noexcept
Random-access iterators support a superset of bidirectional iterator operations.
size_type size() const noexcept
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
iterator insert(const_iterator __position, size_type __n, const value_type &__x)
Inserts a number of copies of given data into the vector.
const_iterator cbegin() const noexcept
const_iterator end() const noexcept
vector & operator=(vector &&__x) noexcept(_Alloc_traits::_S_nothrow_move())
Vector move assignment operator.
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
reference at(size_type __n)
Provides access to the data contained in the vector.
const_reference at(size_type __n) const
Provides access to the data contained in the vector.
iterator begin() noexcept
bool equal(_II1 __first1, _II1 __last1, _II2 __first2)
Tests a range for element-wise equality.
static size_type max_size(const _Tp_alloc_type &__a) noexcept
The maximum supported allocation size.
void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
void _M_range_check(size_type __n) const
Safety check used only from at().
void push_back(const value_type &__x)
Add data to the end of the vector.
pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last)
void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
const_reference front() const noexcept
iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
iterator insert(const_iterator __position, initializer_list< value_type > __l)
Inserts an initializer_list into the vector.
size_type capacity() const noexcept
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
void _Destroy(_Tp *__pointer)
const_iterator cend() const noexcept
const_reference operator[](size_type __n) const noexcept
Subscript access to the data contained in the vector.
vector & operator=(initializer_list< value_type > __l)
Vector list assignment operator.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
const_reverse_iterator crend() const noexcept
const_reverse_iterator crbegin() const noexcept
A standard container which offers fixed time access to individual elements in any order...
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
const_reverse_iterator rend() const noexcept
void pop_back() noexcept
Removes last element.
iterator insert(const_iterator __position, _InputIterator __first, _InputIterator __last)
Inserts a range into the vector.
bool empty() const noexcept