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