1 // Vector implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
67 template<typename _Tp, typename _Alloc>
70 reserve(size_type __n)
72 if (__n > this->max_size())
73 __throw_length_error(__N("vector::reserve"));
74 if (this->capacity() < __n)
76 const size_type __old_size = size();
77 pointer __tmp = _M_allocate_and_copy(__n,
78 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start),
79 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish));
80 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
81 _M_get_Tp_allocator());
82 _M_deallocate(this->_M_impl._M_start,
83 this->_M_impl._M_end_of_storage
84 - this->_M_impl._M_start);
85 this->_M_impl._M_start = __tmp;
86 this->_M_impl._M_finish = __tmp + __old_size;
87 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
91 #ifdef __GXX_EXPERIMENTAL_CXX0X__
92 template<typename _Tp, typename _Alloc>
93 template<typename... _Args>
96 emplace_back(_Args&&... __args)
98 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
100 this->_M_impl.construct(this->_M_impl._M_finish,
101 std::forward<_Args>(__args)...);
102 ++this->_M_impl._M_finish;
105 _M_insert_aux(end(), std::forward<_Args>(__args)...);
109 template<typename _Tp, typename _Alloc>
110 typename vector<_Tp, _Alloc>::iterator
111 vector<_Tp, _Alloc>::
112 insert(iterator __position, const value_type& __x)
114 const size_type __n = __position - begin();
115 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
116 && __position == end())
118 this->_M_impl.construct(this->_M_impl._M_finish, __x);
119 ++this->_M_impl._M_finish;
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
127 _M_insert_aux(__position, std::move(__x_copy));
131 _M_insert_aux(__position, __x);
133 return iterator(this->_M_impl._M_start + __n);
136 template<typename _Tp, typename _Alloc>
137 typename vector<_Tp, _Alloc>::iterator
138 vector<_Tp, _Alloc>::
139 erase(iterator __position)
141 if (__position + 1 != end())
142 _GLIBCXX_MOVE3(__position + 1, end(), __position);
143 --this->_M_impl._M_finish;
144 this->_M_impl.destroy(this->_M_impl._M_finish);
148 template<typename _Tp, typename _Alloc>
149 typename vector<_Tp, _Alloc>::iterator
150 vector<_Tp, _Alloc>::
151 erase(iterator __first, iterator __last)
154 _GLIBCXX_MOVE3(__last, end(), __first);
155 _M_erase_at_end(__first.base() + (end() - __last));
159 template<typename _Tp, typename _Alloc>
161 vector<_Tp, _Alloc>::
162 operator=(const vector<_Tp, _Alloc>& __x)
166 const size_type __xlen = __x.size();
167 if (__xlen > capacity())
169 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
171 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
172 _M_get_Tp_allocator());
173 _M_deallocate(this->_M_impl._M_start,
174 this->_M_impl._M_end_of_storage
175 - this->_M_impl._M_start);
176 this->_M_impl._M_start = __tmp;
177 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
179 else if (size() >= __xlen)
181 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
182 end(), _M_get_Tp_allocator());
186 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
187 this->_M_impl._M_start);
188 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
189 __x._M_impl._M_finish,
190 this->_M_impl._M_finish,
191 _M_get_Tp_allocator());
193 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
198 template<typename _Tp, typename _Alloc>
200 vector<_Tp, _Alloc>::
201 _M_fill_assign(size_t __n, const value_type& __val)
203 if (__n > capacity())
205 vector __tmp(__n, __val, _M_get_Tp_allocator());
208 else if (__n > size())
210 std::fill(begin(), end(), __val);
211 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
213 _M_get_Tp_allocator());
214 this->_M_impl._M_finish += __n - size();
217 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
220 template<typename _Tp, typename _Alloc>
221 template<typename _InputIterator>
223 vector<_Tp, _Alloc>::
224 _M_assign_aux(_InputIterator __first, _InputIterator __last,
225 std::input_iterator_tag)
227 pointer __cur(this->_M_impl._M_start);
228 for (; __first != __last && __cur != this->_M_impl._M_finish;
231 if (__first == __last)
232 _M_erase_at_end(__cur);
234 insert(end(), __first, __last);
237 template<typename _Tp, typename _Alloc>
238 template<typename _ForwardIterator>
240 vector<_Tp, _Alloc>::
241 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
242 std::forward_iterator_tag)
244 const size_type __len = std::distance(__first, __last);
246 if (__len > capacity())
248 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
249 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
250 _M_get_Tp_allocator());
251 _M_deallocate(this->_M_impl._M_start,
252 this->_M_impl._M_end_of_storage
253 - this->_M_impl._M_start);
254 this->_M_impl._M_start = __tmp;
255 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
256 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
258 else if (size() >= __len)
259 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
262 _ForwardIterator __mid = __first;
263 std::advance(__mid, size());
264 std::copy(__first, __mid, this->_M_impl._M_start);
265 this->_M_impl._M_finish =
266 std::__uninitialized_copy_a(__mid, __last,
267 this->_M_impl._M_finish,
268 _M_get_Tp_allocator());
272 #ifdef __GXX_EXPERIMENTAL_CXX0X__
273 template<typename _Tp, typename _Alloc>
274 template<typename... _Args>
275 typename vector<_Tp, _Alloc>::iterator
276 vector<_Tp, _Alloc>::
277 emplace(iterator __position, _Args&&... __args)
279 const size_type __n = __position - begin();
280 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
281 && __position == end())
283 this->_M_impl.construct(this->_M_impl._M_finish,
284 std::forward<_Args>(__args)...);
285 ++this->_M_impl._M_finish;
288 _M_insert_aux(__position, std::forward<_Args>(__args)...);
289 return iterator(this->_M_impl._M_start + __n);
292 template<typename _Tp, typename _Alloc>
293 template<typename... _Args>
295 vector<_Tp, _Alloc>::
296 _M_insert_aux(iterator __position, _Args&&... __args)
298 template<typename _Tp, typename _Alloc>
300 vector<_Tp, _Alloc>::
301 _M_insert_aux(iterator __position, const _Tp& __x)
304 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
306 this->_M_impl.construct(this->_M_impl._M_finish,
307 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
309 ++this->_M_impl._M_finish;
310 #ifndef __GXX_EXPERIMENTAL_CXX0X__
313 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
314 this->_M_impl._M_finish - 2,
315 this->_M_impl._M_finish - 1);
316 #ifndef __GXX_EXPERIMENTAL_CXX0X__
317 *__position = __x_copy;
319 *__position = _Tp(std::forward<_Args>(__args)...);
324 const size_type __len =
325 _M_check_len(size_type(1), "vector::_M_insert_aux");
326 const size_type __elems_before = __position - begin();
327 pointer __new_start(this->_M_allocate(__len));
328 pointer __new_finish(__new_start);
331 // The order of the three operations is dictated by the C++0x
332 // case, where the moves could alter a new element belonging
333 // to the existing vector. This is an issue only for callers
334 // taking the element by const lvalue ref (see 23.1/13).
335 this->_M_impl.construct(__new_start + __elems_before,
336 #ifdef __GXX_EXPERIMENTAL_CXX0X__
337 std::forward<_Args>(__args)...);
344 std::__uninitialized_move_a(this->_M_impl._M_start,
345 __position.base(), __new_start,
346 _M_get_Tp_allocator());
350 std::__uninitialized_move_a(__position.base(),
351 this->_M_impl._M_finish,
353 _M_get_Tp_allocator());
358 this->_M_impl.destroy(__new_start + __elems_before);
360 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
361 _M_deallocate(__new_start, __len);
362 __throw_exception_again;
364 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
365 _M_get_Tp_allocator());
366 _M_deallocate(this->_M_impl._M_start,
367 this->_M_impl._M_end_of_storage
368 - this->_M_impl._M_start);
369 this->_M_impl._M_start = __new_start;
370 this->_M_impl._M_finish = __new_finish;
371 this->_M_impl._M_end_of_storage = __new_start + __len;
375 template<typename _Tp, typename _Alloc>
377 vector<_Tp, _Alloc>::
378 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
382 if (size_type(this->_M_impl._M_end_of_storage
383 - this->_M_impl._M_finish) >= __n)
385 value_type __x_copy = __x;
386 const size_type __elems_after = end() - __position;
387 pointer __old_finish(this->_M_impl._M_finish);
388 if (__elems_after > __n)
390 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
391 this->_M_impl._M_finish,
392 this->_M_impl._M_finish,
393 _M_get_Tp_allocator());
394 this->_M_impl._M_finish += __n;
395 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
396 __old_finish - __n, __old_finish);
397 std::fill(__position.base(), __position.base() + __n,
402 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
405 _M_get_Tp_allocator());
406 this->_M_impl._M_finish += __n - __elems_after;
407 std::__uninitialized_move_a(__position.base(), __old_finish,
408 this->_M_impl._M_finish,
409 _M_get_Tp_allocator());
410 this->_M_impl._M_finish += __elems_after;
411 std::fill(__position.base(), __old_finish, __x_copy);
416 const size_type __len =
417 _M_check_len(__n, "vector::_M_fill_insert");
418 const size_type __elems_before = __position - begin();
419 pointer __new_start(this->_M_allocate(__len));
420 pointer __new_finish(__new_start);
423 // See _M_insert_aux above.
424 std::__uninitialized_fill_n_a(__new_start + __elems_before,
426 _M_get_Tp_allocator());
430 std::__uninitialized_move_a(this->_M_impl._M_start,
433 _M_get_Tp_allocator());
437 std::__uninitialized_move_a(__position.base(),
438 this->_M_impl._M_finish,
440 _M_get_Tp_allocator());
445 std::_Destroy(__new_start + __elems_before,
446 __new_start + __elems_before + __n,
447 _M_get_Tp_allocator());
449 std::_Destroy(__new_start, __new_finish,
450 _M_get_Tp_allocator());
451 _M_deallocate(__new_start, __len);
452 __throw_exception_again;
454 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
455 _M_get_Tp_allocator());
456 _M_deallocate(this->_M_impl._M_start,
457 this->_M_impl._M_end_of_storage
458 - this->_M_impl._M_start);
459 this->_M_impl._M_start = __new_start;
460 this->_M_impl._M_finish = __new_finish;
461 this->_M_impl._M_end_of_storage = __new_start + __len;
466 template<typename _Tp, typename _Alloc>
467 template<typename _InputIterator>
469 vector<_Tp, _Alloc>::
470 _M_range_insert(iterator __pos, _InputIterator __first,
471 _InputIterator __last, std::input_iterator_tag)
473 for (; __first != __last; ++__first)
475 __pos = insert(__pos, *__first);
480 template<typename _Tp, typename _Alloc>
481 template<typename _ForwardIterator>
483 vector<_Tp, _Alloc>::
484 _M_range_insert(iterator __position, _ForwardIterator __first,
485 _ForwardIterator __last, std::forward_iterator_tag)
487 if (__first != __last)
489 const size_type __n = std::distance(__first, __last);
490 if (size_type(this->_M_impl._M_end_of_storage
491 - this->_M_impl._M_finish) >= __n)
493 const size_type __elems_after = end() - __position;
494 pointer __old_finish(this->_M_impl._M_finish);
495 if (__elems_after > __n)
497 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
498 this->_M_impl._M_finish,
499 this->_M_impl._M_finish,
500 _M_get_Tp_allocator());
501 this->_M_impl._M_finish += __n;
502 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
503 __old_finish - __n, __old_finish);
504 std::copy(__first, __last, __position);
508 _ForwardIterator __mid = __first;
509 std::advance(__mid, __elems_after);
510 std::__uninitialized_copy_a(__mid, __last,
511 this->_M_impl._M_finish,
512 _M_get_Tp_allocator());
513 this->_M_impl._M_finish += __n - __elems_after;
514 std::__uninitialized_move_a(__position.base(),
516 this->_M_impl._M_finish,
517 _M_get_Tp_allocator());
518 this->_M_impl._M_finish += __elems_after;
519 std::copy(__first, __mid, __position);
524 const size_type __len =
525 _M_check_len(__n, "vector::_M_range_insert");
526 pointer __new_start(this->_M_allocate(__len));
527 pointer __new_finish(__new_start);
531 std::__uninitialized_move_a(this->_M_impl._M_start,
534 _M_get_Tp_allocator());
536 std::__uninitialized_copy_a(__first, __last,
538 _M_get_Tp_allocator());
540 std::__uninitialized_move_a(__position.base(),
541 this->_M_impl._M_finish,
543 _M_get_Tp_allocator());
547 std::_Destroy(__new_start, __new_finish,
548 _M_get_Tp_allocator());
549 _M_deallocate(__new_start, __len);
550 __throw_exception_again;
552 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
553 _M_get_Tp_allocator());
554 _M_deallocate(this->_M_impl._M_start,
555 this->_M_impl._M_end_of_storage
556 - this->_M_impl._M_start);
557 this->_M_impl._M_start = __new_start;
558 this->_M_impl._M_finish = __new_finish;
559 this->_M_impl._M_end_of_storage = __new_start + __len;
567 template<typename _Alloc>
569 vector<bool, _Alloc>::
570 reserve(size_type __n)
572 if (__n > this->max_size())
573 __throw_length_error(__N("vector::reserve"));
574 if (this->capacity() < __n)
576 _Bit_type* __q = this->_M_allocate(__n);
577 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
579 this->_M_deallocate();
580 this->_M_impl._M_start = iterator(__q, 0);
581 this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1)
586 template<typename _Alloc>
588 vector<bool, _Alloc>::
589 _M_fill_insert(iterator __position, size_type __n, bool __x)
593 if (capacity() - size() >= __n)
595 std::copy_backward(__position, end(),
596 this->_M_impl._M_finish + difference_type(__n));
597 std::fill(__position, __position + difference_type(__n), __x);
598 this->_M_impl._M_finish += difference_type(__n);
602 const size_type __len =
603 _M_check_len(__n, "vector<bool>::_M_fill_insert");
604 _Bit_type * __q = this->_M_allocate(__len);
605 iterator __i = _M_copy_aligned(begin(), __position,
607 std::fill(__i, __i + difference_type(__n), __x);
608 this->_M_impl._M_finish = std::copy(__position, end(),
609 __i + difference_type(__n));
610 this->_M_deallocate();
611 this->_M_impl._M_end_of_storage = (__q + ((__len
612 + int(_S_word_bit) - 1)
613 / int(_S_word_bit)));
614 this->_M_impl._M_start = iterator(__q, 0);
618 template<typename _Alloc>
619 template<typename _ForwardIterator>
621 vector<bool, _Alloc>::
622 _M_insert_range(iterator __position, _ForwardIterator __first,
623 _ForwardIterator __last, std::forward_iterator_tag)
625 if (__first != __last)
627 size_type __n = std::distance(__first, __last);
628 if (capacity() - size() >= __n)
630 std::copy_backward(__position, end(),
631 this->_M_impl._M_finish
632 + difference_type(__n));
633 std::copy(__first, __last, __position);
634 this->_M_impl._M_finish += difference_type(__n);
638 const size_type __len =
639 _M_check_len(__n, "vector<bool>::_M_insert_range");
640 _Bit_type * __q = this->_M_allocate(__len);
641 iterator __i = _M_copy_aligned(begin(), __position,
643 __i = std::copy(__first, __last, __i);
644 this->_M_impl._M_finish = std::copy(__position, end(), __i);
645 this->_M_deallocate();
646 this->_M_impl._M_end_of_storage = (__q
648 + int(_S_word_bit) - 1)
649 / int(_S_word_bit)));
650 this->_M_impl._M_start = iterator(__q, 0);
655 template<typename _Alloc>
657 vector<bool, _Alloc>::
658 _M_insert_aux(iterator __position, bool __x)
660 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
662 std::copy_backward(__position, this->_M_impl._M_finish,
663 this->_M_impl._M_finish + 1);
665 ++this->_M_impl._M_finish;
669 const size_type __len =
670 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
671 _Bit_type * __q = this->_M_allocate(__len);
672 iterator __i = _M_copy_aligned(begin(), __position,
675 this->_M_impl._M_finish = std::copy(__position, end(), __i);
676 this->_M_deallocate();
677 this->_M_impl._M_end_of_storage = (__q + ((__len
678 + int(_S_word_bit) - 1)
679 / int(_S_word_bit)));
680 this->_M_impl._M_start = iterator(__q, 0);
684 _GLIBCXX_END_NESTED_NAMESPACE
686 #endif /* _VECTOR_TCC */