STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_vector.h
Go to the documentation of this file.
1 // Vector implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
56 #ifndef _STL_VECTOR_H
57 #define _STL_VECTOR_H 1
58 
60 #include <bits/functexcept.h>
61 #include <bits/concept_check.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
64 #endif
65 
66 namespace std _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 
71  template<typename _Tp, typename _Alloc>
72  struct _Vector_base
73  {
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
77  pointer;
78 
79  struct _Vector_impl
80  : public _Tp_alloc_type
81  {
82  pointer _M_start;
83  pointer _M_finish;
84  pointer _M_end_of_storage;
85 
86  _Vector_impl()
87  : _Tp_alloc_type(), _M_start(0), _M_finish(0), _M_end_of_storage(0)
88  { }
89 
90  _Vector_impl(_Tp_alloc_type const& __a)
91  : _Tp_alloc_type(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92  { }
93 
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)
98  { }
99 #endif
100 
101  void _M_swap_data(_Vector_impl& __x)
102  {
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);
106  }
107  };
108 
109  public:
110  typedef _Alloc allocator_type;
111 
112  _Tp_alloc_type&
113  _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
114  { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
115 
116  const _Tp_alloc_type&
117  _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
118  { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
119 
120  allocator_type
121  get_allocator() const _GLIBCXX_NOEXCEPT
122  { return allocator_type(_M_get_Tp_allocator()); }
123 
124  _Vector_base()
125  : _M_impl() { }
126 
127  _Vector_base(const allocator_type& __a)
128  : _M_impl(__a) { }
129 
130  _Vector_base(size_t __n)
131  : _M_impl()
132  { _M_create_storage(__n); }
133 
134  _Vector_base(size_t __n, const allocator_type& __a)
135  : _M_impl(__a)
136  { _M_create_storage(__n); }
137 
138 #if __cplusplus >= 201103L
139  _Vector_base(_Tp_alloc_type&& __a)
140  : _M_impl(std::move(__a)) { }
141 
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); }
145 
146  _Vector_base(_Vector_base&& __x, const allocator_type& __a)
147  : _M_impl(__a)
148  {
149  if (__x.get_allocator() == __a)
150  this->_M_impl._M_swap_data(__x._M_impl);
151  else
152  {
153  size_t __n = __x._M_impl._M_finish - __x._M_impl._M_start;
154  _M_create_storage(__n);
155  }
156  }
157 #endif
158 
159  ~_Vector_base()
160  { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
161  - this->_M_impl._M_start); }
162 
163  public:
164  _Vector_impl _M_impl;
165 
166  pointer
167  _M_allocate(size_t __n)
168  { return __n != 0 ? _M_impl.allocate(__n) : 0; }
169 
170  void
171  _M_deallocate(pointer __p, size_t __n)
172  {
173  if (__p)
174  _M_impl.deallocate(__p, __n);
175  }
176 
177  private:
178  void
179  _M_create_storage(size_t __n)
180  {
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;
184  }
185  };
186 
187 
209  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
210  class vector : protected _Vector_base<_Tp, _Alloc>
211  {
212  // Concept requirements.
213  typedef typename _Alloc::value_type _Alloc_value_type;
214  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
215  __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
216 
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;
220 
221  public:
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>
229  const_iterator;
230  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
231  typedef std::reverse_iterator<iterator> reverse_iterator;
232  typedef size_t size_type;
233  typedef ptrdiff_t difference_type;
234  typedef _Alloc allocator_type;
235 
236  protected:
237  using _Base::_M_allocate;
238  using _Base::_M_deallocate;
239  using _Base::_M_impl;
240  using _Base::_M_get_Tp_allocator;
241 
242  public:
243  // [23.2.4.1] construct/copy/destroy
244  // (assign() and get_allocator() are also listed in this section)
248  vector()
249  : _Base() { }
250 
255  explicit
256  vector(const allocator_type& __a)
257  : _Base(__a) { }
258 
259 #if __cplusplus >= 201103L
260 
268  explicit
269  vector(size_type __n, const allocator_type& __a = allocator_type())
270  : _Base(__n, __a)
271  { _M_default_initialize(__n); }
272 
281  vector(size_type __n, const value_type& __value,
282  const allocator_type& __a = allocator_type())
283  : _Base(__n, __a)
284  { _M_fill_initialize(__n, __value); }
285 #else
286 
294  explicit
295  vector(size_type __n, const value_type& __value = value_type(),
296  const allocator_type& __a = allocator_type())
297  : _Base(__n, __a)
298  { _M_fill_initialize(__n, __value); }
299 #endif
300 
310  vector(const vector& __x)
311  : _Base(__x.size(),
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());
317  }
318 
319 #if __cplusplus >= 201103L
320 
327  vector(vector&& __x) noexcept
328  : _Base(std::move(__x)) { }
329 
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());
337  }
338 
340  vector(vector&& __rv, const allocator_type& __m)
341  : _Base(std::move(__rv), __m)
342  {
343  if (__rv.get_allocator() != __m)
344  {
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());
349  __rv.clear();
350  }
351  }
352 
364  vector(initializer_list<value_type> __l,
365  const allocator_type& __a = allocator_type())
366  : _Base(__a)
367  {
368  _M_range_initialize(__l.begin(), __l.end(),
369  random_access_iterator_tag());
370  }
371 #endif
372 
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())
394  : _Base(__a)
395  { _M_initialize_dispatch(__first, __last, __false_type()); }
396 #else
397  template<typename _InputIterator>
398  vector(_InputIterator __first, _InputIterator __last,
399  const allocator_type& __a = allocator_type())
400  : _Base(__a)
401  {
402  // Check whether it's an integral type. If so, it's not an iterator.
403  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
404  _M_initialize_dispatch(__first, __last, _Integral());
405  }
406 #endif
407 
414  ~vector() _GLIBCXX_NOEXCEPT
415  { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
416  _M_get_Tp_allocator()); }
417 
426  vector&
427  operator=(const vector& __x);
428 
429 #if __cplusplus >= 201103L
430 
438  vector&
439  operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
440  {
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>());
446  return *this;
447  }
448 
460  vector&
461  operator=(initializer_list<value_type> __l)
462  {
463  this->assign(__l.begin(), __l.end());
464  return *this;
465  }
466 #endif
467 
478  void
479  assign(size_type __n, const value_type& __val)
480  { _M_fill_assign(__n, __val); }
481 
494 #if __cplusplus >= 201103L
495  template<typename _InputIterator,
496  typename = std::_RequireInputIter<_InputIterator>>
497  void
498  assign(_InputIterator __first, _InputIterator __last)
499  { _M_assign_dispatch(__first, __last, __false_type()); }
500 #else
501  template<typename _InputIterator>
502  void
503  assign(_InputIterator __first, _InputIterator __last)
504  {
505  // Check whether it's an integral type. If so, it's not an iterator.
506  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
507  _M_assign_dispatch(__first, __last, _Integral());
508  }
509 #endif
510 
511 #if __cplusplus >= 201103L
512 
523  void
524  assign(initializer_list<value_type> __l)
525  { this->assign(__l.begin(), __l.end()); }
526 #endif
527 
529  using _Base::get_allocator;
530 
531  // iterators
537  iterator
538  begin() _GLIBCXX_NOEXCEPT
539  { return iterator(this->_M_impl._M_start); }
540 
546  const_iterator
547  begin() const _GLIBCXX_NOEXCEPT
548  { return const_iterator(this->_M_impl._M_start); }
549 
555  iterator
556  end() _GLIBCXX_NOEXCEPT
557  { return iterator(this->_M_impl._M_finish); }
558 
564  const_iterator
565  end() const _GLIBCXX_NOEXCEPT
566  { return const_iterator(this->_M_impl._M_finish); }
567 
573  reverse_iterator
574  rbegin() _GLIBCXX_NOEXCEPT
575  { return reverse_iterator(end()); }
576 
582  const_reverse_iterator
583  rbegin() const _GLIBCXX_NOEXCEPT
584  { return const_reverse_iterator(end()); }
585 
591  reverse_iterator
592  rend() _GLIBCXX_NOEXCEPT
593  { return reverse_iterator(begin()); }
594 
600  const_reverse_iterator
601  rend() const _GLIBCXX_NOEXCEPT
602  { return const_reverse_iterator(begin()); }
603 
604 #if __cplusplus >= 201103L
605 
610  const_iterator
611  cbegin() const noexcept
612  { return const_iterator(this->_M_impl._M_start); }
613 
619  const_iterator
620  cend() const noexcept
621  { return const_iterator(this->_M_impl._M_finish); }
622 
628  const_reverse_iterator
629  crbegin() const noexcept
630  { return const_reverse_iterator(end()); }
631 
637  const_reverse_iterator
638  crend() const noexcept
639  { return const_reverse_iterator(begin()); }
640 #endif
641 
642  // [23.2.4.2] capacity
644  size_type
645  size() const _GLIBCXX_NOEXCEPT
646  { return size_type(this->_M_impl._M_finish - this->_M_impl._M_start); }
647 
649  size_type
650  max_size() const _GLIBCXX_NOEXCEPT
651  { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
652 
653 #if __cplusplus >= 201103L
654 
663  void
664  resize(size_type __new_size)
665  {
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);
670  }
671 
683  void
684  resize(size_type __new_size, const value_type& __x)
685  {
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);
690  }
691 #else
692 
703  void
704  resize(size_type __new_size, value_type __x = value_type())
705  {
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);
710  }
711 #endif
712 
713 #if __cplusplus >= 201103L
714 
715  void
716  shrink_to_fit()
717  { _M_shrink_to_fit(); }
718 #endif
719 
724  size_type
725  capacity() const _GLIBCXX_NOEXCEPT
726  { return size_type(this->_M_impl._M_end_of_storage
727  - this->_M_impl._M_start); }
728 
733  bool
734  empty() const _GLIBCXX_NOEXCEPT
735  { return begin() == end(); }
736 
754  void
755  reserve(size_type __n);
756 
757  // element access
769  reference
770  operator[](size_type __n)
771  { return *(this->_M_impl._M_start + __n); }
772 
784  const_reference
785  operator[](size_type __n) const
786  { return *(this->_M_impl._M_start + __n); }
787 
788  protected:
790  void
791  _M_range_check(size_type __n) const
792  {
793  if (__n >= this->size())
794  __throw_out_of_range(__N("vector::_M_range_check"));
795  }
796 
797  public:
809  reference
810  at(size_type __n)
811  {
812  _M_range_check(__n);
813  return (*this)[__n];
814  }
815 
827  const_reference
828  at(size_type __n) const
829  {
830  _M_range_check(__n);
831  return (*this)[__n];
832  }
833 
838  reference
839  front()
840  { return *begin(); }
841 
846  const_reference
847  front() const
848  { return *begin(); }
849 
854  reference
855  back()
856  { return *(end() - 1); }
857 
862  const_reference
863  back() const
864  { return *(end() - 1); }
865 
866  // _GLIBCXX_RESOLVE_LIB_DEFECTS
867  // DR 464. Suggestion for new member functions in standard containers.
868  // data access
873 #if __cplusplus >= 201103L
874  _Tp*
875 #else
876  pointer
877 #endif
878  data() _GLIBCXX_NOEXCEPT
879  { return std::__addressof(front()); }
880 
881 #if __cplusplus >= 201103L
882  const _Tp*
883 #else
884  const_pointer
885 #endif
886  data() const _GLIBCXX_NOEXCEPT
887  { return std::__addressof(front()); }
888 
889  // [23.2.4.3] modifiers
900  void
901  push_back(const value_type& __x)
902  {
903  if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
904  {
905  _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
906  __x);
907  ++this->_M_impl._M_finish;
908  }
909  else
910 #if __cplusplus >= 201103L
911  _M_emplace_back_aux(__x);
912 #else
913  _M_insert_aux(end(), __x);
914 #endif
915  }
916 
917 #if __cplusplus >= 201103L
918  void
919  push_back(value_type&& __x)
920  { emplace_back(std::move(__x)); }
921 
922  template<typename... _Args>
923  void
924  emplace_back(_Args&&... __args);
925 #endif
926 
936  void
937  pop_back()
938  {
939  --this->_M_impl._M_finish;
940  _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
941  }
942 
943 #if __cplusplus >= 201103L
944 
956  template<typename... _Args>
957  iterator
958  emplace(iterator __position, _Args&&... __args);
959 #endif
960 
972  iterator
973  insert(iterator __position, const value_type& __x);
974 
975 #if __cplusplus >= 201103L
976 
987  iterator
988  insert(iterator __position, value_type&& __x)
989  { return emplace(__position, std::move(__x)); }
990 
1004  void
1005  insert(iterator __position, initializer_list<value_type> __l)
1006  { this->insert(__position, __l.begin(), __l.end()); }
1007 #endif
1008 
1022  void
1023  insert(iterator __position, size_type __n, const value_type& __x)
1024  { _M_fill_insert(__position, __n, __x); }
1025 
1040 #if __cplusplus >= 201103L
1041  template<typename _InputIterator,
1042  typename = std::_RequireInputIter<_InputIterator>>
1043  void
1044  insert(iterator __position, _InputIterator __first,
1045  _InputIterator __last)
1046  { _M_insert_dispatch(__position, __first, __last, __false_type()); }
1047 #else
1048  template<typename _InputIterator>
1049  void
1050  insert(iterator __position, _InputIterator __first,
1051  _InputIterator __last)
1052  {
1053  // Check whether it's an integral type. If so, it's not an iterator.
1054  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1055  _M_insert_dispatch(__position, __first, __last, _Integral());
1056  }
1057 #endif
1058 
1074  iterator
1075  erase(iterator __position);
1076 
1095  iterator
1096  erase(iterator __first, iterator __last);
1097 
1107  void
1108  swap(vector& __x)
1109 #if __cplusplus >= 201103L
1110  noexcept(_Alloc_traits::_S_nothrow_swap())
1111 #endif
1112  {
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());
1116  }
1117 
1124  void
1125  clear() _GLIBCXX_NOEXCEPT
1126  { _M_erase_at_end(this->_M_impl._M_start); }
1127 
1128  protected:
1133  template<typename _ForwardIterator>
1134  pointer
1135  _M_allocate_and_copy(size_type __n,
1136  _ForwardIterator __first, _ForwardIterator __last)
1137  {
1138  pointer __result = this->_M_allocate(__n);
1139  __try
1140  {
1141  std::__uninitialized_copy_a(__first, __last, __result,
1142  _M_get_Tp_allocator());
1143  return __result;
1144  }
1145  __catch(...)
1146  {
1147  _M_deallocate(__result, __n);
1149  }
1150  }
1151 
1152 
1153  // Internal constructor functions follow.
1154 
1155  // Called by the range constructor to implement [23.1.1]/9
1156 
1157  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1158  // 438. Ambiguity in the "do the right thing" clause
1159  template<typename _Integer>
1160  void
1161  _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
1162  {
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);
1167  }
1168 
1169  // Called by the range constructor to implement [23.1.1]/9
1170  template<typename _InputIterator>
1171  void
1172  _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1173  __false_type)
1174  {
1175  typedef typename std::iterator_traits<_InputIterator>::
1176  iterator_category _IterCategory;
1177  _M_range_initialize(__first, __last, _IterCategory());
1178  }
1179 
1180  // Called by the second initialize_dispatch above
1181  template<typename _InputIterator>
1182  void
1183  _M_range_initialize(_InputIterator __first,
1184  _InputIterator __last, std::input_iterator_tag)
1185  {
1186  for (; __first != __last; ++__first)
1187 #if __cplusplus >= 201103L
1188  emplace_back(*__first);
1189 #else
1190  push_back(*__first);
1191 #endif
1192  }
1193 
1194  // Called by the second initialize_dispatch above
1195  template<typename _ForwardIterator>
1196  void
1197  _M_range_initialize(_ForwardIterator __first,
1198  _ForwardIterator __last, std::forward_iterator_tag)
1199  {
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());
1207  }
1208 
1209  // Called by the first initialize_dispatch above and by the
1210  // vector(n,value,a) constructor.
1211  void
1212  _M_fill_initialize(size_type __n, const value_type& __value)
1213  {
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;
1217  }
1218 
1219 #if __cplusplus >= 201103L
1220  // Called by the vector(n) constructor.
1221  void
1222  _M_default_initialize(size_type __n)
1223  {
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;
1227  }
1228 #endif
1229 
1230  // Internal assign functions follow. The *_aux functions do the actual
1231  // assignment work for the range versions.
1232 
1233  // Called by the range assign to implement [23.1.1]/9
1234 
1235  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1236  // 438. Ambiguity in the "do the right thing" clause
1237  template<typename _Integer>
1238  void
1239  _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1240  { _M_fill_assign(__n, __val); }
1241 
1242  // Called by the range assign to implement [23.1.1]/9
1243  template<typename _InputIterator>
1244  void
1245  _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1246  __false_type)
1247  {
1248  typedef typename std::iterator_traits<_InputIterator>::
1249  iterator_category _IterCategory;
1250  _M_assign_aux(__first, __last, _IterCategory());
1251  }
1252 
1253  // Called by the second assign_dispatch above
1254  template<typename _InputIterator>
1255  void
1256  _M_assign_aux(_InputIterator __first, _InputIterator __last,
1257  std::input_iterator_tag);
1258 
1259  // Called by the second assign_dispatch above
1260  template<typename _ForwardIterator>
1261  void
1262  _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1263  std::forward_iterator_tag);
1264 
1265  // Called by assign(n,t), and the range assign when it turns out
1266  // to be the same thing.
1267  void
1268  _M_fill_assign(size_type __n, const value_type& __val);
1269 
1270 
1271  // Internal insert functions follow.
1272 
1273  // Called by the range insert to implement [23.1.1]/9
1274 
1275  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1276  // 438. Ambiguity in the "do the right thing" clause
1277  template<typename _Integer>
1278  void
1279  _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
1280  __true_type)
1281  { _M_fill_insert(__pos, __n, __val); }
1282 
1283  // Called by the range insert to implement [23.1.1]/9
1284  template<typename _InputIterator>
1285  void
1286  _M_insert_dispatch(iterator __pos, _InputIterator __first,
1287  _InputIterator __last, __false_type)
1288  {
1289  typedef typename std::iterator_traits<_InputIterator>::
1290  iterator_category _IterCategory;
1291  _M_range_insert(__pos, __first, __last, _IterCategory());
1292  }
1293 
1294  // Called by the second insert_dispatch above
1295  template<typename _InputIterator>
1296  void
1297  _M_range_insert(iterator __pos, _InputIterator __first,
1298  _InputIterator __last, std::input_iterator_tag);
1299 
1300  // Called by the second insert_dispatch above
1301  template<typename _ForwardIterator>
1302  void
1303  _M_range_insert(iterator __pos, _ForwardIterator __first,
1304  _ForwardIterator __last, std::forward_iterator_tag);
1305 
1306  // Called by insert(p,n,x), and the range insert when it turns out to be
1307  // the same thing.
1308  void
1309  _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1310 
1311 #if __cplusplus >= 201103L
1312  // Called by resize(n).
1313  void
1314  _M_default_append(size_type __n);
1315 
1316  bool
1317  _M_shrink_to_fit();
1318 #endif
1319 
1320  // Called by insert(p,x)
1321 #if __cplusplus < 201103L
1322  void
1323  _M_insert_aux(iterator __position, const value_type& __x);
1324 #else
1325  template<typename... _Args>
1326  void
1327  _M_insert_aux(iterator __position, _Args&&... __args);
1328 
1329  template<typename... _Args>
1330  void
1331  _M_emplace_back_aux(_Args&&... __args);
1332 #endif
1333 
1334  // Called by the latter.
1335  size_type
1336  _M_check_len(size_type __n, const char* __s) const
1337  {
1338  if (max_size() - size() < __n)
1339  __throw_length_error(__N(__s));
1340 
1341  const size_type __len = size() + std::max(size(), __n);
1342  return (__len < size() || __len > max_size()) ? max_size() : __len;
1343  }
1344 
1345  // Internal erase functions follow.
1346 
1347  // Called by erase(q1,q2), clear(), resize(), _M_fill_assign,
1348  // _M_assign_aux.
1349  void
1350  _M_erase_at_end(pointer __pos)
1351  {
1352  std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
1353  this->_M_impl._M_finish = __pos;
1354  }
1355 
1356 #if __cplusplus >= 201103L
1357  private:
1358  // Constant-time move assignment when source object's memory can be
1359  // moved, either because the source's allocator will move too
1360  // or because the allocators are equal.
1361  void
1362  _M_move_assign(vector&& __x, std::true_type) noexcept
1363  {
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());
1370  }
1371 
1372  // Do move assignment when it might not be possible to move source
1373  // object's memory, resulting in a linear-time operation.
1374  void
1375  _M_move_assign(vector&& __x, std::false_type)
1376  {
1377  if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
1378  _M_move_assign(std::move(__x), std::true_type());
1379  else
1380  {
1381  // The rvalue's allocator cannot be moved and is not equal,
1382  // so we need to individually move each element.
1383  this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
1384  std::__make_move_if_noexcept_iterator(__x.end()));
1385  __x.clear();
1386  }
1387  }
1388 #endif
1389  };
1390 
1391 
1402  template<typename _Tp, typename _Alloc>
1403  inline bool
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())); }
1407 
1419  template<typename _Tp, typename _Alloc>
1420  inline bool
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()); }
1424 
1426  template<typename _Tp, typename _Alloc>
1427  inline bool
1428  operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1429  { return !(__x == __y); }
1430 
1432  template<typename _Tp, typename _Alloc>
1433  inline bool
1434  operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1435  { return __y < __x; }
1436 
1438  template<typename _Tp, typename _Alloc>
1439  inline bool
1440  operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1441  { return !(__y < __x); }
1442 
1444  template<typename _Tp, typename _Alloc>
1445  inline bool
1446  operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
1447  { return !(__x < __y); }
1448 
1450  template<typename _Tp, typename _Alloc>
1451  inline void
1452  swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1453  { __x.swap(__y); }
1454 
1455 _GLIBCXX_END_NAMESPACE_CONTAINER
1456 } // namespace std
1457 
1458 #endif /* _STL_VECTOR_H */
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