58 #define _STL_VECTOR_H 1
65 namespace std _GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 template<
typename _Tp,
typename _Alloc>
73 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
76 :
public _Tp_alloc_type
78 typename _Tp_alloc_type::pointer _M_start;
79 typename _Tp_alloc_type::pointer _M_finish;
80 typename _Tp_alloc_type::pointer _M_end_of_storage;
83 : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
86 _Vector_impl(_Tp_alloc_type
const& __a)
87 : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92 typedef _Alloc allocator_type;
96 {
return *
static_cast<_Tp_alloc_type*
>(&this->_M_impl); }
99 _M_get_Tp_allocator()
const
100 {
return *
static_cast<const _Tp_alloc_type*
>(&this->_M_impl); }
103 get_allocator()
const
104 {
return allocator_type(_M_get_Tp_allocator()); }
115 this->_M_impl._M_start = this->_M_allocate(__n);
116 this->_M_impl._M_finish = this->_M_impl._M_start;
117 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
123 this->_M_impl._M_start = this->_M_allocate(__n);
124 this->_M_impl._M_finish = this->_M_impl._M_start;
125 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
128 #ifdef __GXX_EXPERIMENTAL_CXX0X__
130 : _M_impl(__x._M_get_Tp_allocator())
132 this->_M_impl._M_start = __x._M_impl._M_start;
133 this->_M_impl._M_finish = __x._M_impl._M_finish;
134 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
135 __x._M_impl._M_start = 0;
136 __x._M_impl._M_finish = 0;
137 __x._M_impl._M_end_of_storage = 0;
142 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
143 - this->_M_impl._M_start); }
146 _Vector_impl _M_impl;
148 typename _Tp_alloc_type::pointer
149 _M_allocate(
size_t __n)
150 {
return __n != 0 ? _M_impl.allocate(__n) : 0; }
153 _M_deallocate(
typename _Tp_alloc_type::pointer __p,
size_t __n)
156 _M_impl.deallocate(__p, __n);
179 template<
typename _Tp,
typename _Alloc = std::allocator<_Tp> >
183 typedef typename _Alloc::value_type _Alloc_value_type;
184 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
185 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
188 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
191 typedef _Tp value_type;
192 typedef typename _Tp_alloc_type::pointer pointer;
193 typedef typename _Tp_alloc_type::const_pointer const_pointer;
194 typedef typename _Tp_alloc_type::reference reference;
195 typedef typename _Tp_alloc_type::const_reference const_reference;
196 typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
197 typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
201 typedef size_t size_type;
202 typedef ptrdiff_t difference_type;
203 typedef _Alloc allocator_type;
206 using _Base::_M_allocate;
207 using _Base::_M_deallocate;
208 using _Base::_M_impl;
209 using _Base::_M_get_Tp_allocator;
228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
239 { _M_default_initialize(__n); }
249 vector(size_type __n,
const value_type& __value,
252 { _M_fill_initialize(__n, __value); }
263 vector(size_type __n,
const value_type& __value = value_type(),
264 const allocator_type& __a = allocator_type())
266 { _M_fill_initialize(__n, __value); }
279 :
_Base(__x.
size(), __x._M_get_Tp_allocator())
280 { this->_M_impl._M_finish =
281 std::__uninitialized_copy_a(__x.
begin(), __x.
end(),
282 this->_M_impl._M_start,
283 _M_get_Tp_allocator());
286 #ifdef __GXX_EXPERIMENTAL_CXX0X__
295 :
_Base(std::move(__x)) { }
312 _M_range_initialize(__l.begin(), __l.end(),
333 template<
typename _InputIterator>
334 vector(_InputIterator __first, _InputIterator __last,
339 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
340 _M_initialize_dispatch(__first, __last, _Integral());
350 {
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
351 _M_get_Tp_allocator()); }
364 #ifdef __GXX_EXPERIMENTAL_CXX0X__
396 this->
assign(__l.begin(), __l.end());
412 assign(size_type __n,
const value_type& __val)
413 { _M_fill_assign(__n, __val); }
427 template<
typename _InputIterator>
429 assign(_InputIterator __first, _InputIterator __last)
432 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
433 _M_assign_dispatch(__first, __last, _Integral());
436 #ifdef __GXX_EXPERIMENTAL_CXX0X__
450 { this->
assign(__l.begin(), __l.end()); }
454 using _Base::get_allocator;
464 {
return iterator(this->_M_impl._M_start); }
473 {
return const_iterator(this->_M_impl._M_start); }
482 {
return iterator(this->_M_impl._M_finish); }
491 {
return const_iterator(this->_M_impl._M_finish); }
507 const_reverse_iterator
525 const_reverse_iterator
529 #ifdef __GXX_EXPERIMENTAL_CXX0X__
537 {
return const_iterator(this->_M_impl._M_start); }
546 {
return const_iterator(this->_M_impl._M_finish); }
553 const_reverse_iterator
562 const_reverse_iterator
571 {
return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
576 {
return _M_get_Tp_allocator().max_size(); }
578 #ifdef __GXX_EXPERIMENTAL_CXX0X__
591 if (__new_size >
size())
592 _M_default_append(__new_size -
size());
593 else if (__new_size <
size())
594 _M_erase_at_end(this->_M_impl._M_start + __new_size);
609 resize(size_type __new_size,
const value_type& __x)
611 if (__new_size >
size())
613 else if (__new_size <
size())
614 _M_erase_at_end(this->_M_impl._M_start + __new_size);
629 resize(size_type __new_size, value_type __x = value_type())
631 if (__new_size >
size())
633 else if (__new_size <
size())
634 _M_erase_at_end(this->_M_impl._M_start + __new_size);
638 #ifdef __GXX_EXPERIMENTAL_CXX0X__
642 { std::__shrink_to_fit<vector>::_S_do_it(*
this); }
651 {
return size_type(this->_M_impl._M_end_of_storage
652 - this->_M_impl._M_start); }
696 {
return *(this->_M_impl._M_start + __n); }
711 {
return *(this->_M_impl._M_start + __n); }
718 if (__n >= this->
size())
719 __throw_out_of_range(__N(
"vector::_M_range_check"));
753 at(size_type __n)
const
781 {
return *(
end() - 1); }
789 {
return *(
end() - 1); }
798 #ifdef __GXX_EXPERIMENTAL_CXX0X__
804 {
return std::__addressof(
front()); }
806 #ifdef __GXX_EXPERIMENTAL_CXX0X__
812 {
return std::__addressof(
front()); }
828 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
830 this->_M_impl.construct(this->_M_impl._M_finish, __x);
831 ++this->_M_impl._M_finish;
834 _M_insert_aux(
end(), __x);
837 #ifdef __GXX_EXPERIMENTAL_CXX0X__
840 { emplace_back(std::move(__x)); }
842 template<
typename... _Args>
844 emplace_back(_Args&&... __args);
859 --this->_M_impl._M_finish;
860 this->_M_impl.destroy(this->_M_impl._M_finish);
863 #ifdef __GXX_EXPERIMENTAL_CXX0X__
876 template<
typename... _Args>
895 #ifdef __GXX_EXPERIMENTAL_CXX0X__
908 insert(iterator __position, value_type&& __x)
909 {
return emplace(__position, std::move(__x)); }
926 { this->
insert(__position, __l.begin(), __l.end()); }
943 insert(iterator __position, size_type __n,
const value_type& __x)
944 { _M_fill_insert(__position, __n, __x); }
960 template<
typename _InputIterator>
962 insert(iterator __position, _InputIterator __first,
963 _InputIterator __last)
966 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
967 _M_insert_dispatch(__position, __first, __last, _Integral());
1021 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1022 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1023 std::swap(this->_M_impl._M_end_of_storage,
1024 __x._M_impl._M_end_of_storage);
1028 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1029 __x._M_get_Tp_allocator());
1040 { _M_erase_at_end(this->_M_impl._M_start); }
1047 template<
typename _ForwardIterator>
1050 _ForwardIterator __first, _ForwardIterator __last)
1052 pointer __result = this->_M_allocate(__n);
1055 std::__uninitialized_copy_a(__first, __last, __result,
1056 _M_get_Tp_allocator());
1061 _M_deallocate(__result, __n);
1062 __throw_exception_again;
1073 template<
typename _Integer>
1075 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1077 this->_M_impl._M_start = _M_allocate(static_cast<size_type>(__n));
1078 this->_M_impl._M_end_of_storage =
1079 this->_M_impl._M_start +
static_cast<size_type
>(__n);
1080 _M_fill_initialize(static_cast<size_type>(__n), __value);
1084 template<
typename _InputIterator>
1086 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1089 typedef typename std::iterator_traits<_InputIterator>::
1090 iterator_category _IterCategory;
1091 _M_range_initialize(__first, __last, _IterCategory());
1095 template<
typename _InputIterator>
1097 _M_range_initialize(_InputIterator __first,
1100 for (; __first != __last; ++__first)
1105 template<
typename _ForwardIterator>
1107 _M_range_initialize(_ForwardIterator __first,
1111 this->_M_impl._M_start = this->_M_allocate(__n);
1112 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
1113 this->_M_impl._M_finish =
1114 std::__uninitialized_copy_a(__first, __last,
1115 this->_M_impl._M_start,
1116 _M_get_Tp_allocator());
1122 _M_fill_initialize(size_type __n,
const value_type& __value)
1124 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value,
1125 _M_get_Tp_allocator());
1126 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1129 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1132 _M_default_initialize(size_type __n)
1134 std::__uninitialized_default_n_a(this->_M_impl._M_start, __n,
1135 _M_get_Tp_allocator());
1136 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage;
1147 template<
typename _Integer>
1149 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1150 { _M_fill_assign(__n, __val); }
1153 template<
typename _InputIterator>
1155 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1158 typedef typename std::iterator_traits<_InputIterator>::
1159 iterator_category _IterCategory;
1160 _M_assign_aux(__first, __last, _IterCategory());
1164 template<
typename _InputIterator>
1166 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1170 template<
typename _ForwardIterator>
1172 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1178 _M_fill_assign(size_type __n,
const value_type& __val);
1187 template<
typename _Integer>
1189 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1191 { _M_fill_insert(__pos, __n, __val); }
1194 template<
typename _InputIterator>
1196 _M_insert_dispatch(iterator __pos, _InputIterator __first,
1197 _InputIterator __last, __false_type)
1199 typedef typename std::iterator_traits<_InputIterator>::
1200 iterator_category _IterCategory;
1201 _M_range_insert(__pos, __first, __last, _IterCategory());
1205 template<
typename _InputIterator>
1207 _M_range_insert(iterator __pos, _InputIterator __first,
1211 template<
typename _ForwardIterator>
1213 _M_range_insert(iterator __pos, _ForwardIterator __first,
1219 _M_fill_insert(iterator __pos, size_type __n,
const value_type& __x);
1221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1224 _M_default_append(size_type __n);
1228 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1230 _M_insert_aux(iterator __position,
const value_type& __x);
1232 template<
typename... _Args>
1234 _M_insert_aux(iterator __position, _Args&&... __args);
1239 _M_check_len(size_type __n,
const char* __s)
const
1242 __throw_length_error(__N(__s));
1253 _M_erase_at_end(pointer __pos)
1255 std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1256 this->_M_impl._M_finish = __pos;
1271 template<
typename _Tp,
typename _Alloc>
1288 template<
typename _Tp,
typename _Alloc>
1292 __y.begin(), __y.end()); }
1295 template<
typename _Tp,
typename _Alloc>
1298 {
return !(__x == __y); }
1301 template<
typename _Tp,
typename _Alloc>
1304 {
return __y < __x; }
1307 template<
typename _Tp,
typename _Alloc>
1310 {
return !(__y < __x); }
1313 template<
typename _Tp,
typename _Alloc>
1316 {
return !(__x < __y); }
1319 template<
typename _Tp,
typename _Alloc>
1324 _GLIBCXX_END_NAMESPACE_CONTAINER
void assign(initializer_list< value_type > __l)
Assigns an initializer list to a vector.
vector()
Default constructor creates no elements.
const_iterator cbegin() const
void insert(iterator __position, _InputIterator __first, _InputIterator __last)
Inserts a range into the vector.
void _Destroy(_Tp *__pointer)
const_iterator cend() const
void insert(iterator __position, initializer_list< value_type > __l)
Inserts an initializer_list into the vector.
vector & operator=(initializer_list< value_type > __l)
Vector list assignment operator.
const_reverse_iterator rbegin() const
void resize(size_type __new_size, const value_type &__x)
Resizes the vector to the specified number of elements.
vector(_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
Builds a vector from a range.
void push_back(const value_type &__x)
Add data to the end of the vector.
const_reference at(size_type __n) const
Provides access to the data contained in the vector.
See bits/stl_deque.h's _Deque_base for an explanation.
const_reverse_iterator crend() const
void _M_range_check(size_type __n) const
Safety check used only from at().
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
vector & operator=(const vector &__x)
Vector assignment operator.
iterator insert(iterator __position, const value_type &__x)
Inserts given value into vector before specified iterator.
pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first, _ForwardIterator __last)
iterator insert(iterator __position, value_type &&__x)
Inserts given rvalue into vector before specified iterator.
vector(vector &&__x)
Vector move constructor.
Forward iterators support a superset of input iterator operations.
vector(const vector &__x)
Vector copy constructor.
iterator erase(iterator __position)
Remove element at given position.
void insert(iterator __position, size_type __n, const value_type &__x)
Inserts a number of copies of given data into the vector.
vector & operator=(vector &&__x)
Vector move assignment operator.
const_iterator begin() const
vector(const allocator_type &__a)
Creates a vector with no elements.
void assign(size_type __n, const value_type &__val)
Assigns a given value to a vector.
bool equal(_II1 __first1, _II1 __last1, _II2 __first2)
Tests a range for element-wise equality.
vector(size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
Creates a vector with copies of an exemplar element.
void reserve(size_type __n)
Attempt to preallocate enough memory for specified number of elements.
void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a vector.
size_type max_size() const
const_reverse_iterator crbegin() const
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
void swap(vector &__x)
Swaps data with another vector.
void pop_back()
Removes last element.
void resize(size_type __new_size)
Resizes the vector to the specified number of elements.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
A standard container which offers fixed time access to individual elements in any order...
reverse_iterator rbegin()
const_reverse_iterator rend() const
reference at(size_type __n)
Provides access to the data contained in the vector.
const_iterator end() const
iterator emplace(iterator __position, _Args &&...__args)
Inserts an object in vector before specified iterator.
vector(size_type __n)
Creates a vector with default constructed elements.
Random-access iterators support a superset of bidirectional iterator operations.
const_reference front() const
reference operator[](size_type __n)
Subscript access to the data contained in the vector.
The standard allocator, as per [20.4].Further details: http://gcc.gnu.org/onlinedocs/libstdc++/manual...
vector(initializer_list< value_type > __l, const allocator_type &__a=allocator_type())
Builds a vector from an initializer list.
size_type capacity() const
const_reference operator[](size_type __n) const
Subscript access to the data contained in the vector.
const_reference back() const