_vector.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. *
  3. *
  4. * Copyright (c) 1994
  5. * Hewlett-Packard Company
  6. *
  7. * Copyright (c) 1996,1997
  8. * Silicon Graphics Computer Systems, Inc.
  9. *
  10. * Copyright (c) 1997
  11. * Moscow Center for SPARC Technology
  12. *
  13. * Copyright (c) 1999
  14. * Boris Fomitchev
  15. *
  16. * This material is provided "as is", with absolutely no warranty expressed
  17. * or implied. Any use is at your own risk.
  18. *
  19. * Permission to use or copy this software for any purpose is hereby granted
  20. * without fee, provided the above notices are retained on all copies.
  21. * Permission to modify the code and to distribute modified code is granted,
  22. * provided the above notices are retained, and a notice that the code was
  23. * modified is included with the above copyright notice.
  24. *
  25. */
  26. #ifndef _STLP_VECTOR_C
  27. #define _STLP_VECTOR_C
  28. #if !defined (_STLP_INTERNAL_VECTOR_H)
  29. # include <stl/_vector.h>
  30. #endif
  31. #include <stl/_range_errors.h>
  32. _STLP_BEGIN_NAMESPACE
  33. _STLP_MOVE_TO_PRIV_NAMESPACE
  34. template <class _Tp, class _Alloc>
  35. void _Vector_base<_Tp,_Alloc>::_M_throw_length_error() const
  36. { __stl_throw_length_error("vector"); }
  37. template <class _Tp, class _Alloc>
  38. void _Vector_base<_Tp, _Alloc>::_M_throw_out_of_range() const
  39. { __stl_throw_out_of_range("vector"); }
  40. #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
  41. # define vector _STLP_PTR_IMPL_NAME(vector)
  42. #elif defined (_STLP_DEBUG)
  43. # define vector _STLP_NON_DBG_NAME(vector)
  44. #else
  45. _STLP_MOVE_TO_STD_NAMESPACE
  46. #endif
  47. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  48. # define __iterator__ _Tp*
  49. #else
  50. # define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE vector<_Tp, _Alloc>::iterator
  51. #endif
  52. template <class _Tp, class _Alloc>
  53. void vector<_Tp, _Alloc>::reserve(size_type __n) {
  54. if (capacity() < __n) {
  55. if (max_size() < __n) {
  56. this->_M_throw_length_error();
  57. }
  58. const size_type __old_size = size();
  59. pointer __tmp;
  60. if (this->_M_start) {
  61. __tmp = _M_allocate_and_copy(__n, this->_M_start, this->_M_finish);
  62. _M_clear();
  63. } else {
  64. __tmp = this->_M_end_of_storage.allocate(__n, __n);
  65. }
  66. _M_set(__tmp, __tmp + __old_size, __tmp + __n);
  67. }
  68. }
  69. template <class _Tp, class _Alloc>
  70. void vector<_Tp, _Alloc>::_M_insert_overflow_aux(pointer __pos, const _Tp& __x, const __false_type& /*DO NOT USE!!*/,
  71. size_type __fill_len, bool __atend ) {
  72. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  73. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  74. typedef typename __move_traits<_Tp>::implemented _Movable;
  75. #endif
  76. size_type __len = _M_compute_next_size(__fill_len);
  77. pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
  78. pointer __new_finish = __new_start;
  79. _STLP_TRY {
  80. __new_finish = _STLP_PRIV __uninitialized_move(this->_M_start, __pos, __new_start, _TrivialUCopy(), _Movable());
  81. // handle insertion
  82. if (__fill_len == 1) {
  83. _Copy_Construct(__new_finish, __x);
  84. ++__new_finish;
  85. } else
  86. __new_finish = _STLP_PRIV __uninitialized_fill_n(__new_finish, __fill_len, __x);
  87. if (!__atend)
  88. __new_finish = _STLP_PRIV __uninitialized_move(__pos, this->_M_finish, __new_finish, _TrivialUCopy(), _Movable()); // copy remainder
  89. }
  90. _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
  91. this->_M_end_of_storage.deallocate(__new_start,__len)))
  92. _M_clear_after_move();
  93. _M_set(__new_start, __new_finish, __new_start + __len);
  94. }
  95. template <class _Tp, class _Alloc>
  96. void vector<_Tp, _Alloc>::_M_insert_overflow(pointer __pos, const _Tp& __x, const __true_type& /*_TrivialCopy*/,
  97. size_type __fill_len, bool __atend ) {
  98. size_type __len = _M_compute_next_size(__fill_len);
  99. pointer __new_start = this->_M_end_of_storage.allocate(__len, __len);
  100. pointer __new_finish = __STATIC_CAST(pointer, _STLP_PRIV __copy_trivial(this->_M_start, __pos, __new_start));
  101. // handle insertion
  102. __new_finish = _STLP_PRIV __fill_n(__new_finish, __fill_len, __x);
  103. if (!__atend)
  104. __new_finish = __STATIC_CAST(pointer, _STLP_PRIV __copy_trivial(__pos, this->_M_finish, __new_finish)); // copy remainder
  105. _M_clear();
  106. _M_set(__new_start, __new_finish, __new_start + __len);
  107. }
  108. template <class _Tp, class _Alloc>
  109. void vector<_Tp, _Alloc>::_M_fill_insert_aux(iterator __pos, size_type __n,
  110. const _Tp& __x, const __true_type& /*_Movable*/) {
  111. if (_M_is_inside(__x)) {
  112. _Tp __x_copy = __x;
  113. _M_fill_insert_aux(__pos, __n, __x_copy, __true_type());
  114. return;
  115. }
  116. iterator __src = this->_M_finish - 1;
  117. iterator __dst = __src + __n;
  118. for (; __src >= __pos; --__dst, --__src) {
  119. _STLP_STD::_Move_Construct(__dst, *__src);
  120. _STLP_STD::_Destroy_Moved(__src);
  121. }
  122. _STLP_PRIV __uninitialized_fill_n(__pos, __n, __x);
  123. this->_M_finish += __n;
  124. }
  125. template <class _Tp, class _Alloc>
  126. void vector<_Tp, _Alloc>::_M_fill_insert_aux (iterator __pos, size_type __n,
  127. const _Tp& __x, const __false_type& /*_Movable*/) {
  128. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  129. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  130. //Here self referencing needs to be checked even for non movable types.
  131. if (_M_is_inside(__x)) {
  132. _Tp __x_copy = __x;
  133. _M_fill_insert_aux(__pos, __n, __x_copy, __false_type());
  134. return;
  135. }
  136. const size_type __elems_after = this->_M_finish - __pos;
  137. pointer __old_finish = this->_M_finish;
  138. if (__elems_after > __n) {
  139. _STLP_PRIV __ucopy_ptrs(this->_M_finish - __n, this->_M_finish, this->_M_finish, _TrivialUCopy());
  140. this->_M_finish += __n;
  141. _STLP_PRIV __copy_backward_ptrs(__pos, __old_finish - __n, __old_finish, _TrivialCopy());
  142. _STLP_STD::fill(__pos, __pos + __n, __x);
  143. } else {
  144. this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_finish, __n - __elems_after, __x);
  145. _STLP_PRIV __ucopy_ptrs(__pos, __old_finish, this->_M_finish, _TrivialUCopy());
  146. this->_M_finish += __elems_after;
  147. _STLP_STD::fill(__pos, __old_finish, __x);
  148. }
  149. }
  150. template <class _Tp, class _Alloc>
  151. void vector<_Tp, _Alloc>::_M_fill_insert(iterator __pos,
  152. size_type __n, const _Tp& __x) {
  153. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  154. typedef typename __move_traits<_Tp>::implemented _Movable;
  155. #endif
  156. if (__n != 0) {
  157. if (size_type(this->_M_end_of_storage._M_data - this->_M_finish) >= __n) {
  158. _M_fill_insert_aux(__pos, __n, __x, _Movable());
  159. } else {
  160. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  161. _M_insert_overflow(__pos, __x, _TrivialCopy(), __n);
  162. }
  163. }
  164. }
  165. template <class _Tp, class _Alloc>
  166. vector<_Tp, _Alloc>& vector<_Tp, _Alloc>::operator = (const vector<_Tp, _Alloc>& __x) {
  167. typedef typename __type_traits<_Tp>::has_trivial_assignment_operator _TrivialCopy;
  168. typedef typename __type_traits<_Tp>::has_trivial_copy_constructor _TrivialUCopy;
  169. if (&__x != this) {
  170. const size_type __xlen = __x.size();
  171. if (__xlen > capacity()) {
  172. size_type __len = __xlen;
  173. pointer __tmp = _M_allocate_and_copy(__len, __CONST_CAST(const_pointer, __x._M_start) + 0,
  174. __CONST_CAST(const_pointer, __x._M_finish) + 0);
  175. _M_clear();
  176. this->_M_start = __tmp;
  177. this->_M_end_of_storage._M_data = this->_M_start + __len;
  178. } else if (size() >= __xlen) {
  179. pointer __i = _STLP_PRIV __copy_ptrs(__CONST_CAST(const_pointer, __x._M_start) + 0,
  180. __CONST_CAST(const_pointer, __x._M_finish) + 0, this->_M_start, _TrivialCopy());
  181. _STLP_STD::_Destroy_Range(__i, this->_M_finish);
  182. } else {
  183. _STLP_PRIV __copy_ptrs(__CONST_CAST(const_pointer, __x._M_start),
  184. __CONST_CAST(const_pointer, __x._M_start) + size(), this->_M_start, _TrivialCopy());
  185. _STLP_PRIV __ucopy_ptrs(__CONST_CAST(const_pointer, __x._M_start) + size(),
  186. __CONST_CAST(const_pointer, __x._M_finish) + 0, this->_M_finish, _TrivialUCopy());
  187. }
  188. this->_M_finish = this->_M_start + __xlen;
  189. }
  190. return *this;
  191. }
  192. template <class _Tp, class _Alloc>
  193. void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const _Tp& __val) {
  194. if (__n > capacity()) {
  195. vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator());
  196. __tmp.swap(*this);
  197. } else if (__n > size()) {
  198. fill(begin(), end(), __val);
  199. this->_M_finish = _STLP_PRIV __uninitialized_fill_n(this->_M_finish, __n - size(), __val);
  200. } else
  201. erase(_STLP_PRIV __fill_n(begin(), __n, __val), end());
  202. }
  203. template <class _Tp, class _Alloc>
  204. __iterator__
  205. vector<_Tp, _Alloc>::insert(iterator __pos, const _Tp& __x) {
  206. size_type __n = __pos - begin();
  207. _M_fill_insert(__pos, 1, __x);
  208. return begin() + __n;
  209. }
  210. #undef __iterator__
  211. #if defined (vector)
  212. # undef vector
  213. _STLP_MOVE_TO_STD_NAMESPACE
  214. #endif
  215. _STLP_END_NAMESPACE
  216. #endif /* _STLP_VECTOR_C */
  217. // Local Variables:
  218. // mode:C++
  219. // End: