_vector.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_VECTOR_H
  29. #define _STLP_INTERNAL_VECTOR_H
  30. #ifndef _STLP_INTERNAL_ALGOBASE_H
  31. # include <stl/_algobase.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_ALLOC_H
  34. # include <stl/_alloc.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_ITERATOR_H
  37. # include <stl/_iterator.h>
  38. #endif
  39. #ifndef _STLP_INTERNAL_UNINITIALIZED_H
  40. # include <stl/_uninitialized.h>
  41. #endif
  42. _STLP_BEGIN_NAMESPACE
  43. // The vector base class serves one purpose, its constructor and
  44. // destructor allocate (but don't initialize) storage. This makes
  45. // exception safety easier.
  46. _STLP_MOVE_TO_PRIV_NAMESPACE
  47. template <class _Tp, class _Alloc>
  48. class _Vector_base {
  49. public:
  50. typedef _Vector_base<_Tp, _Alloc> _Self;
  51. _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  52. typedef _Alloc allocator_type;
  53. typedef _Tp* pointer;
  54. typedef _STLP_alloc_proxy<pointer, _Tp, allocator_type> _AllocProxy;
  55. _Vector_base(const _Alloc& __a)
  56. : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {}
  57. _Vector_base(size_t __n, const _Alloc& __a)
  58. : _M_start(0), _M_finish(0), _M_end_of_storage(__a, 0) {
  59. _M_start = _M_end_of_storage.allocate(__n, __n);
  60. _M_finish = _M_start;
  61. _M_end_of_storage._M_data = _M_start + __n;
  62. _STLP_MPWFIX_TRY _STLP_MPWFIX_CATCH
  63. }
  64. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  65. _Vector_base(__move_source<_Self> src)
  66. : _M_start(src.get()._M_start), _M_finish(src.get()._M_finish),
  67. _M_end_of_storage(__move_source<_AllocProxy>(src.get()._M_end_of_storage)) {
  68. //Set the source as empty:
  69. src.get()._M_finish = src.get()._M_end_of_storage._M_data = src.get()._M_start = 0;
  70. }
  71. #endif
  72. ~_Vector_base() {
  73. if (_M_start != _STLP_DEFAULT_CONSTRUCTED(pointer))
  74. _M_end_of_storage.deallocate(_M_start, _M_end_of_storage._M_data - _M_start);
  75. }
  76. protected:
  77. void _STLP_FUNCTION_THROWS _M_throw_length_error() const;
  78. void _STLP_FUNCTION_THROWS _M_throw_out_of_range() const;
  79. pointer _M_start;
  80. pointer _M_finish;
  81. _AllocProxy _M_end_of_storage;
  82. };
  83. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  84. # define vector _STLP_PTR_IMPL_NAME(vector)
  85. #elif defined (_STLP_DEBUG)
  86. # define vector _STLP_NON_DBG_NAME(vector)
  87. #else
  88. _STLP_MOVE_TO_STD_NAMESPACE
  89. #endif
  90. template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
  91. class vector : protected _STLP_PRIV _Vector_base<_Tp, _Alloc>
  92. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (vector)
  93. , public __stlport_class<vector<_Tp, _Alloc> >
  94. #endif
  95. {
  96. private:
  97. typedef _STLP_PRIV _Vector_base<_Tp, _Alloc> _Base;
  98. typedef vector<_Tp, _Alloc> _Self;
  99. public:
  100. _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  101. typedef typename _Base::allocator_type allocator_type;
  102. typedef _Tp value_type;
  103. typedef value_type* pointer;
  104. typedef const value_type* const_pointer;
  105. typedef value_type* iterator;
  106. typedef const value_type* const_iterator;
  107. typedef value_type& reference;
  108. typedef const value_type& const_reference;
  109. typedef size_t size_type;
  110. typedef ptrdiff_t difference_type;
  111. typedef random_access_iterator_tag _Iterator_category;
  112. _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
  113. allocator_type get_allocator() const
  114. { return _STLP_CONVERT_ALLOCATOR((const allocator_type&)this->_M_end_of_storage, _Tp); }
  115. private:
  116. #if defined (_STLP_NO_MOVE_SEMANTIC)
  117. typedef __false_type _Movable;
  118. #endif
  119. // handles insertions on overflow
  120. void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __false_type& /*_Movable*/,
  121. size_type __fill_len, bool __atend);
  122. void _M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __true_type& /*_Movable*/,
  123. size_type __fill_len, bool __atend) {
  124. //We need to take care of self referencing here:
  125. if (_M_is_inside(__x)) {
  126. value_type __x_copy = __x;
  127. _M_insert_overflow_aux(__pos, __x_copy, __false_type(), __fill_len, __atend);
  128. return;
  129. }
  130. _M_insert_overflow_aux(__pos, __x, __false_type(), __fill_len, __atend);
  131. }
  132. void _M_insert_overflow(pointer __pos, const _Tp& __x, const __false_type& /*_TrivialCopy*/,
  133. size_type __fill_len, bool __atend = false) {
  134. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  135. typedef typename __move_traits<_Tp>::implemented _Movable;
  136. #endif
  137. _M_insert_overflow_aux(__pos, __x, _Movable(), __fill_len, __atend);
  138. }
  139. void _M_insert_overflow(pointer __pos, const _Tp& __x, const __true_type& /*_TrivialCopy*/,
  140. size_type __fill_len, bool __atend = false);
  141. void _M_range_check(size_type __n) const {
  142. if (__n >= size_type(this->_M_finish - this->_M_start))
  143. this->_M_throw_out_of_range();
  144. }
  145. size_type _M_compute_next_size(size_type __n) {
  146. const size_type __size = size();
  147. if (__n > max_size() - __size)
  148. this->_M_throw_length_error();
  149. size_type __len = __size + (max)(__n, __size);
  150. if (__len > max_size() || __len < __size)
  151. __len = max_size(); // overflow
  152. return __len;
  153. }
  154. public:
  155. iterator begin() { return this->_M_start; }
  156. const_iterator begin() const { return this->_M_start; }
  157. iterator end() { return this->_M_finish; }
  158. const_iterator end() const { return this->_M_finish; }
  159. reverse_iterator rbegin() { return reverse_iterator(end()); }
  160. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  161. reverse_iterator rend() { return reverse_iterator(begin()); }
  162. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  163. size_type size() const { return size_type(this->_M_finish - this->_M_start); }
  164. size_type max_size() const {
  165. size_type __vector_max_size = size_type(-1) / sizeof(_Tp);
  166. typename allocator_type::size_type __alloc_max_size = this->_M_end_of_storage.max_size();
  167. return (__alloc_max_size < __vector_max_size)?__alloc_max_size:__vector_max_size;
  168. }
  169. size_type capacity() const { return size_type(this->_M_end_of_storage._M_data - this->_M_start); }
  170. bool empty() const { return this->_M_start == this->_M_finish; }
  171. reference operator[](size_type __n) { return *(begin() + __n); }
  172. const_reference operator[](size_type __n) const { return *(begin() + __n); }
  173. reference front() { return *begin(); }
  174. const_reference front() const { return *begin(); }
  175. reference back() { return *(end() - 1); }
  176. const_reference back() const { return *(end() - 1); }
  177. reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
  178. const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
  179. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  180. explicit vector(const allocator_type& __a = allocator_type())
  181. #else
  182. vector()
  183. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) {}
  184. vector(const allocator_type& __a)
  185. #endif
  186. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) {}
  187. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  188. private:
  189. //We always call _M_initialize with only 1 parameter. Default parameter
  190. //is used to allow explicit instanciation of vector with types with no
  191. //default constructor.
  192. void _M_initialize(size_type __n, const _Tp& __val = _STLP_DEFAULT_CONSTRUCTED(_Tp))
  193. { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, __val); }
  194. public:
  195. explicit vector(size_type __n)
  196. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
  197. { _M_initialize(__n); }
  198. vector(size_type __n, const _Tp& __val, const allocator_type& __a = allocator_type())
  199. #else
  200. explicit vector(size_type __n)
  201. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
  202. { this->_M_finish = _STLP_PRIV __uninitialized_init(this->_M_start, __n, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  203. vector(size_type __n, const _Tp& __val)
  204. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, allocator_type())
  205. { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); }
  206. vector(size_type __n, const _Tp& __val, const allocator_type& __a)
  207. #endif
  208. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__n, __a)
  209. { this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val); }
  210. vector(const _Self& __x)
  211. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__x.size(), __x.get_allocator()) {
  212. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  213. this->_M_finish = _STLP_PRIV __ucopy_ptrs(__x.begin(), __x.end(), this->_M_start, _TrivialUCopy());
  214. }
  215. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  216. vector(__move_source<_Self> src)
  217. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__move_source<_Base>(src.get()))
  218. {}
  219. #endif
  220. #if defined (_STLP_MEMBER_TEMPLATES)
  221. private:
  222. template <class _Integer>
  223. void _M_initialize_aux(_Integer __n, _Integer __val,
  224. const __true_type& /*_IsIntegral*/) {
  225. size_type __real_n = __n;
  226. this->_M_start = this->_M_end_of_storage.allocate(__n, __real_n);
  227. this->_M_end_of_storage._M_data = this->_M_start + __real_n;
  228. this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_start, __n, __val);
  229. }
  230. template <class _InputIterator>
  231. void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
  232. const __false_type& /*_IsIntegral*/)
  233. { _M_range_initialize(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator)); }
  234. public:
  235. // Check whether it's an integral type. If so, it's not an iterator.
  236. template <class _InputIterator>
  237. vector(_InputIterator __first, _InputIterator __last,
  238. const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL )
  239. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__a) {
  240. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  241. _M_initialize_aux(__first, __last, _Integral());
  242. }
  243. # if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
  244. template <class _InputIterator>
  245. vector(_InputIterator __first, _InputIterator __last)
  246. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(allocator_type()) {
  247. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  248. _M_initialize_aux(__first, __last, _Integral());
  249. }
  250. # endif /* _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS */
  251. #else /* _STLP_MEMBER_TEMPLATES */
  252. vector(const _Tp* __first, const _Tp* __last,
  253. const allocator_type& __a = allocator_type())
  254. : _STLP_PRIV _Vector_base<_Tp, _Alloc>(__last - __first, __a) {
  255. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  256. this->_M_finish = _STLP_PRIV __ucopy_ptrs(__first, __last, this->_M_start, _TrivialUCopy());
  257. }
  258. #endif /* _STLP_MEMBER_TEMPLATES */
  259. //As the vector container is a back insert oriented container it
  260. //seems rather logical to destroy elements in reverse order.
  261. ~vector() { _STLP_STD::_Destroy_Range(rbegin(), rend()); }
  262. _Self& operator=(const _Self& __x);
  263. void reserve(size_type __n);
  264. // assign(), a generalized assignment member function. Two
  265. // versions: one that takes a count, and one that takes a range.
  266. // The range version is a member template, so we dispatch on whether
  267. // or not the type is an integer.
  268. void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
  269. void _M_fill_assign(size_type __n, const _Tp& __val);
  270. #if defined (_STLP_MEMBER_TEMPLATES)
  271. template <class _ForwardIter>
  272. void _M_assign_aux(_ForwardIter __first, _ForwardIter __last, const forward_iterator_tag &) {
  273. #else
  274. void assign(const_iterator __first, const_iterator __last) {
  275. typedef const_iterator _ForwardIter;
  276. #endif
  277. const size_type __len = _STLP_STD::distance(__first, __last);
  278. if (__len > capacity()) {
  279. size_type __n = __len;
  280. iterator __tmp = _M_allocate_and_copy(__n, __first, __last);
  281. _M_clear();
  282. _M_set(__tmp, __tmp + __len, __tmp + __n);
  283. }
  284. else if (size() >= __len) {
  285. iterator __new_finish = copy(__first, __last, this->_M_start);
  286. _STLP_STD::_Destroy_Range(__new_finish, this->_M_finish);
  287. this->_M_finish = __new_finish;
  288. }
  289. else {
  290. _ForwardIter __mid = __first;
  291. _STLP_STD::advance(__mid, size());
  292. _STLP_STD::copy(__first, __mid, this->_M_start);
  293. this->_M_finish = _STLP_STD::uninitialized_copy(__mid, __last, this->_M_finish);
  294. }
  295. }
  296. #if defined (_STLP_MEMBER_TEMPLATES)
  297. template <class _InputIter>
  298. void _M_assign_aux(_InputIter __first, _InputIter __last,
  299. const input_iterator_tag &) {
  300. iterator __cur = begin();
  301. for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
  302. *__cur = *__first;
  303. if (__first == __last)
  304. erase(__cur, end());
  305. else
  306. insert(end(), __first, __last);
  307. }
  308. template <class _Integer>
  309. void _M_assign_dispatch(_Integer __n, _Integer __val,
  310. const __true_type& /*_IsIntegral*/)
  311. { _M_fill_assign(__n, __val); }
  312. template <class _InputIter>
  313. void _M_assign_dispatch(_InputIter __first, _InputIter __last,
  314. const __false_type& /*_IsIntegral*/)
  315. { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
  316. template <class _InputIterator>
  317. void assign(_InputIterator __first, _InputIterator __last) {
  318. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  319. _M_assign_dispatch(__first, __last, _Integral());
  320. }
  321. #endif
  322. #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
  323. void push_back(const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
  324. #else
  325. void push_back(const _Tp& __x) {
  326. #endif
  327. if (this->_M_finish != this->_M_end_of_storage._M_data) {
  328. _Copy_Construct(this->_M_finish, __x);
  329. ++this->_M_finish;
  330. }
  331. else {
  332. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  333. _M_insert_overflow(this->_M_finish, __x, _TrivialCopy(), 1, true);
  334. }
  335. }
  336. #if !defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
  337. iterator insert(iterator __pos, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp));
  338. #else
  339. iterator insert(iterator __pos, const _Tp& __x);
  340. #endif
  341. #if defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
  342. void push_back() { push_back(_STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  343. iterator insert(iterator __pos) { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  344. #endif
  345. void swap(_Self& __x) {
  346. _STLP_STD::swap(this->_M_start, __x._M_start);
  347. _STLP_STD::swap(this->_M_finish, __x._M_finish);
  348. this->_M_end_of_storage.swap(__x._M_end_of_storage);
  349. }
  350. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  351. void _M_swap_workaround(_Self& __x) { swap(__x); }
  352. #endif
  353. private:
  354. void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __true_type& /*_Movable*/);
  355. void _M_fill_insert_aux (iterator __pos, size_type __n, const _Tp& __x, const __false_type& /*_Movable*/);
  356. void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
  357. bool _M_is_inside(const value_type& __x) const {
  358. return (&__x >= this->_M_start && &__x < this->_M_finish);
  359. }
  360. #if defined (_STLP_MEMBER_TEMPLATES)
  361. template <class _ForwardIterator>
  362. void _M_range_insert_realloc(iterator __pos,
  363. _ForwardIterator __first, _ForwardIterator __last,
  364. #else
  365. void _M_range_insert_realloc(iterator __pos,
  366. const_iterator __first, const_iterator __last,
  367. #endif
  368. size_type __n) {
  369. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  370. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  371. typedef typename __move_traits<_Tp>::implemented _Movable;
  372. #endif
  373. size_type __len = _M_compute_next_size(__n);
  374. pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
  375. pointer __new_finish = __new_start;
  376. _STLP_TRY {
  377. __new_finish = _STLP_PRIV __uninitialized_move(this->_M_start, __pos, __new_start, _TrivialUCopy(), _Movable());
  378. __new_finish = uninitialized_copy(__first, __last, __new_finish);
  379. __new_finish = _STLP_PRIV __uninitialized_move(__pos, this->_M_finish, __new_finish, _TrivialUCopy(), _Movable());
  380. }
  381. _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
  382. this->_M_end_of_storage.deallocate(__new_start,__len)))
  383. _M_clear_after_move();
  384. _M_set(__new_start, __new_finish, __new_start + __len);
  385. }
  386. #if defined (_STLP_MEMBER_TEMPLATES)
  387. template <class _ForwardIterator>
  388. void _M_range_insert_aux(iterator __pos,
  389. _ForwardIterator __first, _ForwardIterator __last,
  390. #else
  391. void _M_range_insert_aux(iterator __pos,
  392. const_iterator __first, const_iterator __last,
  393. #endif
  394. size_type __n, const __true_type& /*_Movable*/) {
  395. iterator __src = this->_M_finish - 1;
  396. iterator __dst = __src + __n;
  397. for (; __src >= __pos; --__dst, --__src) {
  398. _STLP_STD::_Move_Construct(__dst, *__src);
  399. _STLP_STD::_Destroy_Moved(__src);
  400. }
  401. uninitialized_copy(__first, __last, __pos);
  402. this->_M_finish += __n;
  403. }
  404. #if defined (_STLP_MEMBER_TEMPLATES)
  405. template <class _ForwardIterator>
  406. void _M_range_insert_aux(iterator __pos,
  407. _ForwardIterator __first, _ForwardIterator __last,
  408. #else
  409. void _M_range_insert_aux(iterator __pos,
  410. const_iterator __first, const_iterator __last,
  411. #endif
  412. size_type __n, const __false_type& /*_Movable*/) {
  413. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  414. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  415. const size_type __elems_after = this->_M_finish - __pos;
  416. pointer __old_finish = this->_M_finish;
  417. if (__elems_after > __n) {
  418. _STLP_PRIV __ucopy_ptrs(this->_M_finish - __n, this->_M_finish, this->_M_finish, _TrivialUCopy());
  419. this->_M_finish += __n;
  420. _STLP_PRIV __copy_backward_ptrs(__pos, __old_finish - __n, __old_finish, _TrivialCopy());
  421. copy(__first, __last, __pos);
  422. }
  423. else {
  424. #if defined ( _STLP_MEMBER_TEMPLATES )
  425. _ForwardIterator __mid = __first;
  426. _STLP_STD::advance(__mid, __elems_after);
  427. #else
  428. const_pointer __mid = __first + __elems_after;
  429. #endif
  430. uninitialized_copy(__mid, __last, this->_M_finish);
  431. this->_M_finish += __n - __elems_after;
  432. _STLP_PRIV __ucopy_ptrs(__pos, __old_finish, this->_M_finish, _TrivialUCopy());
  433. this->_M_finish += __elems_after;
  434. copy(__first, __mid, __pos);
  435. } /* elems_after */
  436. }
  437. #if defined (_STLP_MEMBER_TEMPLATES)
  438. template <class _Integer>
  439. void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
  440. const __true_type&)
  441. { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); }
  442. template <class _InputIterator>
  443. void _M_insert_dispatch(iterator __pos,
  444. _InputIterator __first, _InputIterator __last,
  445. const __false_type&)
  446. { _M_range_insert(__pos, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator)); }
  447. public:
  448. // Check whether it's an integral type. If so, it's not an iterator.
  449. template <class _InputIterator>
  450. void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
  451. typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
  452. _M_insert_dispatch(__pos, __first, __last, _Integral());
  453. }
  454. private:
  455. template <class _InputIterator>
  456. void _M_range_insert(iterator __pos,
  457. _InputIterator __first, _InputIterator __last,
  458. const input_iterator_tag &) {
  459. for ( ; __first != __last; ++__first) {
  460. __pos = insert(__pos, *__first);
  461. ++__pos;
  462. }
  463. }
  464. template <class _ForwardIterator>
  465. void _M_range_insert(iterator __pos,
  466. _ForwardIterator __first, _ForwardIterator __last,
  467. const forward_iterator_tag &) {
  468. #else
  469. public:
  470. void insert(iterator __pos,
  471. const_iterator __first, const_iterator __last) {
  472. #endif
  473. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  474. typedef typename __move_traits<_Tp>::implemented _Movable;
  475. #endif
  476. /* This method do not check self referencing.
  477. * Standard forbids it, checked by the debug mode.
  478. */
  479. if (__first != __last) {
  480. size_type __n = _STLP_STD::distance(__first, __last);
  481. if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) {
  482. _M_range_insert_aux(__pos, __first, __last, __n, _Movable());
  483. }
  484. else {
  485. _M_range_insert_realloc(__pos, __first, __last, __n);
  486. }
  487. }
  488. }
  489. public:
  490. void insert (iterator __pos, size_type __n, const _Tp& __x)
  491. { _M_fill_insert(__pos, __n, __x); }
  492. void pop_back() {
  493. --this->_M_finish;
  494. _STLP_STD::_Destroy(this->_M_finish);
  495. }
  496. private:
  497. iterator _M_erase(iterator __pos, const __true_type& /*_Movable*/) {
  498. _STLP_STD::_Destroy(__pos);
  499. iterator __dst = __pos, __src = __dst + 1;
  500. iterator __end = end();
  501. for (; __src != __end; ++__dst, ++__src) {
  502. _STLP_STD::_Move_Construct(__dst, *__src);
  503. _STLP_STD::_Destroy_Moved(__src);
  504. }
  505. this->_M_finish = __dst;
  506. return __pos;
  507. }
  508. iterator _M_erase(iterator __pos, const __false_type& /*_Movable*/) {
  509. if (__pos + 1 != end()) {
  510. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  511. _STLP_PRIV __copy_ptrs(__pos + 1, this->_M_finish, __pos, _TrivialCopy());
  512. }
  513. --this->_M_finish;
  514. _STLP_STD::_Destroy(this->_M_finish);
  515. return __pos;
  516. }
  517. iterator _M_erase(iterator __first, iterator __last, const __true_type& /*_Movable*/) {
  518. iterator __dst = __first, __src = __last;
  519. iterator __end = end();
  520. for (; __dst != __last && __src != __end; ++__dst, ++__src) {
  521. _STLP_STD::_Destroy(__dst);
  522. _STLP_STD::_Move_Construct(__dst, *__src);
  523. }
  524. if (__dst != __last) {
  525. //There is more elements to erase than element to move:
  526. _STLP_STD::_Destroy_Range(__dst, __last);
  527. _STLP_STD::_Destroy_Moved_Range(__last, __end);
  528. }
  529. else {
  530. //There is more element to move than element to erase:
  531. for (; __src != __end; ++__dst, ++__src) {
  532. _STLP_STD::_Destroy_Moved(__dst);
  533. _STLP_STD::_Move_Construct(__dst, *__src);
  534. }
  535. _STLP_STD::_Destroy_Moved_Range(__dst, __end);
  536. }
  537. this->_M_finish = __dst;
  538. return __first;
  539. }
  540. iterator _M_erase(iterator __first, iterator __last, const __false_type& /*_Movable*/) {
  541. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  542. pointer __i = _STLP_PRIV __copy_ptrs(__last, this->_M_finish, __first, _TrivialCopy());
  543. _STLP_STD::_Destroy_Range(__i, this->_M_finish);
  544. this->_M_finish = __i;
  545. return __first;
  546. }
  547. public:
  548. iterator erase(iterator __pos) {
  549. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  550. typedef typename __move_traits<_Tp>::implemented _Movable;
  551. #endif
  552. return _M_erase(__pos, _Movable());
  553. }
  554. iterator erase(iterator __first, iterator __last) {
  555. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  556. typedef typename __move_traits<_Tp>::implemented _Movable;
  557. #endif
  558. if (__first == __last)
  559. return __first;
  560. return _M_erase(__first, __last, _Movable());
  561. }
  562. #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
  563. void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
  564. #else
  565. void resize(size_type __new_size, const _Tp& __x) {
  566. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  567. if (__new_size < size())
  568. erase(begin() + __new_size, end());
  569. else
  570. insert(end(), __new_size - size(), __x);
  571. }
  572. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  573. void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
  574. #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
  575. void clear() {
  576. erase(begin(), end());
  577. }
  578. private:
  579. void _M_clear() {
  580. _STLP_STD::_Destroy_Range(rbegin(), rend());
  581. this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start);
  582. }
  583. void _M_clear_after_move() {
  584. _STLP_STD::_Destroy_Moved_Range(rbegin(), rend());
  585. this->_M_end_of_storage.deallocate(this->_M_start, this->_M_end_of_storage._M_data - this->_M_start);
  586. }
  587. void _M_set(pointer __s, pointer __f, pointer __e) {
  588. this->_M_start = __s;
  589. this->_M_finish = __f;
  590. this->_M_end_of_storage._M_data = __e;
  591. }
  592. #if defined (_STLP_MEMBER_TEMPLATES)
  593. template <class _ForwardIterator>
  594. pointer _M_allocate_and_copy(size_type& __n,
  595. _ForwardIterator __first, _ForwardIterator __last)
  596. #else /* _STLP_MEMBER_TEMPLATES */
  597. pointer _M_allocate_and_copy(size_type& __n,
  598. const_pointer __first, const_pointer __last)
  599. #endif /* _STLP_MEMBER_TEMPLATES */
  600. {
  601. pointer __result = this->_M_end_of_storage.allocate(__n, __n);
  602. _STLP_TRY {
  603. uninitialized_copy(__first, __last, __result);
  604. return __result;
  605. }
  606. _STLP_UNWIND(this->_M_end_of_storage.deallocate(__result, __n))
  607. _STLP_RET_AFTER_THROW(__result)
  608. }
  609. #if defined (_STLP_MEMBER_TEMPLATES)
  610. template <class _InputIterator>
  611. void _M_range_initialize(_InputIterator __first, _InputIterator __last,
  612. const input_iterator_tag &) {
  613. for ( ; __first != __last; ++__first)
  614. push_back(*__first);
  615. }
  616. // This function is only called by the constructor.
  617. template <class _ForwardIterator>
  618. void _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
  619. const forward_iterator_tag &) {
  620. size_type __n = _STLP_STD::distance(__first, __last);
  621. this->_M_start = this->_M_end_of_storage.allocate(__n, __n);
  622. this->_M_end_of_storage._M_data = this->_M_start + __n;
  623. this->_M_finish = uninitialized_copy(__first, __last, this->_M_start);
  624. }
  625. #endif /* _STLP_MEMBER_TEMPLATES */
  626. };
  627. #if defined (vector)
  628. # undef vector
  629. _STLP_MOVE_TO_STD_NAMESPACE
  630. #endif
  631. _STLP_END_NAMESPACE
  632. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  633. # include <stl/_vector.c>
  634. #endif
  635. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  636. # include <stl/pointers/_vector.h>
  637. #endif
  638. //We define the bool specialization before the debug interfave
  639. //to benefit of the debug version of vector even for the bool
  640. //specialization.
  641. #if !defined (_STLP_NO_BOOL) || !defined (_STLP_NO_EXTENSIONS)
  642. # if !defined (_STLP_INTERNAL_BVECTOR_H)
  643. # include <stl/_bvector.h>
  644. # endif
  645. #endif
  646. #if defined (_STLP_DEBUG)
  647. # include <stl/debug/_vector.h>
  648. #endif
  649. _STLP_BEGIN_NAMESPACE
  650. #if !defined (_STLP_NO_BOOL) && !defined (_STLP_NO_EXTENSIONS)
  651. // This typedef is non-standard. It is provided for backward compatibility.
  652. typedef vector<bool, allocator<bool> > bit_vector;
  653. #endif
  654. #define _STLP_TEMPLATE_HEADER template <class _Tp, class _Alloc>
  655. #define _STLP_TEMPLATE_CONTAINER vector<_Tp, _Alloc>
  656. #include <stl/_relops_cont.h>
  657. #undef _STLP_TEMPLATE_CONTAINER
  658. #undef _STLP_TEMPLATE_HEADER
  659. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  660. # if !defined (_STLP_NO_MOVE_SEMANTIC)
  661. template <class _Tp, class _Alloc>
  662. struct __move_traits<vector<_Tp, _Alloc> > {
  663. typedef __true_type implemented;
  664. typedef typename __move_traits<_Alloc>::complete complete;
  665. };
  666. # endif
  667. # if !defined (_STLP_DEBUG)
  668. template <class _Tp, class _Alloc>
  669. struct _DefaultZeroValue<vector<_Tp, _Alloc> >
  670. { typedef typename __type_traits<_Alloc>::has_trivial_default_constructor _Ret; };
  671. # endif
  672. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  673. _STLP_END_NAMESPACE
  674. #endif /* _STLP_VECTOR_H */
  675. // Local Variables:
  676. // mode:C++
  677. // End: