1 // Vector implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
60 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
62 template<typename _Tp, typename _Alloc>
65 reserve(size_type __n)
67 if (__n > this->max_size())
68 __throw_length_error(__N("vector::reserve"));
69 if (this->capacity() < __n)
71 const size_type __old_size = size();
72 pointer __tmp = _M_allocate_and_copy(__n,
73 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start),
74 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish));
75 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
76 _M_get_Tp_allocator());
77 _M_deallocate(this->_M_impl._M_start,
78 this->_M_impl._M_end_of_storage
79 - this->_M_impl._M_start);
80 this->_M_impl._M_start = __tmp;
81 this->_M_impl._M_finish = __tmp + __old_size;
82 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
86 #ifdef __GXX_EXPERIMENTAL_CXX0X__
87 template<typename _Tp, typename _Alloc>
88 template<typename... _Args>
91 emplace_back(_Args&&... __args)
93 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
95 this->_M_impl.construct(this->_M_impl._M_finish,
96 std::forward<_Args>(__args)...);
97 ++this->_M_impl._M_finish;
100 _M_insert_aux(end(), std::forward<_Args>(__args)...);
104 template<typename _Tp, typename _Alloc>
105 typename vector<_Tp, _Alloc>::iterator
106 vector<_Tp, _Alloc>::
107 insert(iterator __position, const value_type& __x)
109 const size_type __n = __position - begin();
110 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
111 && __position == end())
113 this->_M_impl.construct(this->_M_impl._M_finish, __x);
114 ++this->_M_impl._M_finish;
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
119 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
122 _M_insert_aux(__position, std::move(__x_copy));
126 _M_insert_aux(__position, __x);
128 return iterator(this->_M_impl._M_start + __n);
131 template<typename _Tp, typename _Alloc>
132 typename vector<_Tp, _Alloc>::iterator
133 vector<_Tp, _Alloc>::
134 erase(iterator __position)
136 if (__position + 1 != end())
137 _GLIBCXX_MOVE3(__position + 1, end(), __position);
138 --this->_M_impl._M_finish;
139 this->_M_impl.destroy(this->_M_impl._M_finish);
143 template<typename _Tp, typename _Alloc>
144 typename vector<_Tp, _Alloc>::iterator
145 vector<_Tp, _Alloc>::
146 erase(iterator __first, iterator __last)
149 _GLIBCXX_MOVE3(__last, end(), __first);
150 _M_erase_at_end(__first.base() + (end() - __last));
154 template<typename _Tp, typename _Alloc>
156 vector<_Tp, _Alloc>::
157 operator=(const vector<_Tp, _Alloc>& __x)
161 const size_type __xlen = __x.size();
162 if (__xlen > capacity())
164 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
166 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
167 _M_get_Tp_allocator());
168 _M_deallocate(this->_M_impl._M_start,
169 this->_M_impl._M_end_of_storage
170 - this->_M_impl._M_start);
171 this->_M_impl._M_start = __tmp;
172 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
174 else if (size() >= __xlen)
176 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
177 end(), _M_get_Tp_allocator());
181 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
182 this->_M_impl._M_start);
183 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
184 __x._M_impl._M_finish,
185 this->_M_impl._M_finish,
186 _M_get_Tp_allocator());
188 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
193 template<typename _Tp, typename _Alloc>
195 vector<_Tp, _Alloc>::
196 _M_fill_assign(size_t __n, const value_type& __val)
198 if (__n > capacity())
200 vector __tmp(__n, __val, _M_get_Tp_allocator());
203 else if (__n > size())
205 std::fill(begin(), end(), __val);
206 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
208 _M_get_Tp_allocator());
209 this->_M_impl._M_finish += __n - size();
212 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
215 template<typename _Tp, typename _Alloc>
216 template<typename _InputIterator>
218 vector<_Tp, _Alloc>::
219 _M_assign_aux(_InputIterator __first, _InputIterator __last,
220 std::input_iterator_tag)
222 pointer __cur(this->_M_impl._M_start);
223 for (; __first != __last && __cur != this->_M_impl._M_finish;
226 if (__first == __last)
227 _M_erase_at_end(__cur);
229 insert(end(), __first, __last);
232 template<typename _Tp, typename _Alloc>
233 template<typename _ForwardIterator>
235 vector<_Tp, _Alloc>::
236 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
237 std::forward_iterator_tag)
239 const size_type __len = std::distance(__first, __last);
241 if (__len > capacity())
243 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
244 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
245 _M_get_Tp_allocator());
246 _M_deallocate(this->_M_impl._M_start,
247 this->_M_impl._M_end_of_storage
248 - this->_M_impl._M_start);
249 this->_M_impl._M_start = __tmp;
250 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
251 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
253 else if (size() >= __len)
254 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
257 _ForwardIterator __mid = __first;
258 std::advance(__mid, size());
259 std::copy(__first, __mid, this->_M_impl._M_start);
260 this->_M_impl._M_finish =
261 std::__uninitialized_copy_a(__mid, __last,
262 this->_M_impl._M_finish,
263 _M_get_Tp_allocator());
267 #ifdef __GXX_EXPERIMENTAL_CXX0X__
268 template<typename _Tp, typename _Alloc>
269 template<typename... _Args>
270 typename vector<_Tp, _Alloc>::iterator
271 vector<_Tp, _Alloc>::
272 emplace(iterator __position, _Args&&... __args)
274 const size_type __n = __position - begin();
275 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
276 && __position == end())
278 this->_M_impl.construct(this->_M_impl._M_finish,
279 std::forward<_Args>(__args)...);
280 ++this->_M_impl._M_finish;
283 _M_insert_aux(__position, std::forward<_Args>(__args)...);
284 return iterator(this->_M_impl._M_start + __n);
287 template<typename _Tp, typename _Alloc>
288 template<typename... _Args>
290 vector<_Tp, _Alloc>::
291 _M_insert_aux(iterator __position, _Args&&... __args)
293 template<typename _Tp, typename _Alloc>
295 vector<_Tp, _Alloc>::
296 _M_insert_aux(iterator __position, const _Tp& __x)
299 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
301 this->_M_impl.construct(this->_M_impl._M_finish,
302 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
304 ++this->_M_impl._M_finish;
305 #ifndef __GXX_EXPERIMENTAL_CXX0X__
308 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
309 this->_M_impl._M_finish - 2,
310 this->_M_impl._M_finish - 1);
311 #ifndef __GXX_EXPERIMENTAL_CXX0X__
312 *__position = __x_copy;
314 *__position = _Tp(std::forward<_Args>(__args)...);
319 const size_type __len =
320 _M_check_len(size_type(1), "vector::_M_insert_aux");
321 const size_type __elems_before = __position - begin();
322 pointer __new_start(this->_M_allocate(__len));
323 pointer __new_finish(__new_start);
326 // The order of the three operations is dictated by the C++0x
327 // case, where the moves could alter a new element belonging
328 // to the existing vector. This is an issue only for callers
329 // taking the element by const lvalue ref (see 23.1/13).
330 this->_M_impl.construct(__new_start + __elems_before,
331 #ifdef __GXX_EXPERIMENTAL_CXX0X__
332 std::forward<_Args>(__args)...);
339 std::__uninitialized_move_a(this->_M_impl._M_start,
340 __position.base(), __new_start,
341 _M_get_Tp_allocator());
345 std::__uninitialized_move_a(__position.base(),
346 this->_M_impl._M_finish,
348 _M_get_Tp_allocator());
353 this->_M_impl.destroy(__new_start + __elems_before);
355 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
356 _M_deallocate(__new_start, __len);
357 __throw_exception_again;
359 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
360 _M_get_Tp_allocator());
361 _M_deallocate(this->_M_impl._M_start,
362 this->_M_impl._M_end_of_storage
363 - this->_M_impl._M_start);
364 this->_M_impl._M_start = __new_start;
365 this->_M_impl._M_finish = __new_finish;
366 this->_M_impl._M_end_of_storage = __new_start + __len;
370 template<typename _Tp, typename _Alloc>
372 vector<_Tp, _Alloc>::
373 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
377 if (size_type(this->_M_impl._M_end_of_storage
378 - this->_M_impl._M_finish) >= __n)
380 value_type __x_copy = __x;
381 const size_type __elems_after = end() - __position;
382 pointer __old_finish(this->_M_impl._M_finish);
383 if (__elems_after > __n)
385 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
386 this->_M_impl._M_finish,
387 this->_M_impl._M_finish,
388 _M_get_Tp_allocator());
389 this->_M_impl._M_finish += __n;
390 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
391 __old_finish - __n, __old_finish);
392 std::fill(__position.base(), __position.base() + __n,
397 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
400 _M_get_Tp_allocator());
401 this->_M_impl._M_finish += __n - __elems_after;
402 std::__uninitialized_move_a(__position.base(), __old_finish,
403 this->_M_impl._M_finish,
404 _M_get_Tp_allocator());
405 this->_M_impl._M_finish += __elems_after;
406 std::fill(__position.base(), __old_finish, __x_copy);
411 const size_type __len =
412 _M_check_len(__n, "vector::_M_fill_insert");
413 const size_type __elems_before = __position - begin();
414 pointer __new_start(this->_M_allocate(__len));
415 pointer __new_finish(__new_start);
418 // See _M_insert_aux above.
419 std::__uninitialized_fill_n_a(__new_start + __elems_before,
421 _M_get_Tp_allocator());
425 std::__uninitialized_move_a(this->_M_impl._M_start,
428 _M_get_Tp_allocator());
432 std::__uninitialized_move_a(__position.base(),
433 this->_M_impl._M_finish,
435 _M_get_Tp_allocator());
440 std::_Destroy(__new_start + __elems_before,
441 __new_start + __elems_before + __n,
442 _M_get_Tp_allocator());
444 std::_Destroy(__new_start, __new_finish,
445 _M_get_Tp_allocator());
446 _M_deallocate(__new_start, __len);
447 __throw_exception_again;
449 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
450 _M_get_Tp_allocator());
451 _M_deallocate(this->_M_impl._M_start,
452 this->_M_impl._M_end_of_storage
453 - this->_M_impl._M_start);
454 this->_M_impl._M_start = __new_start;
455 this->_M_impl._M_finish = __new_finish;
456 this->_M_impl._M_end_of_storage = __new_start + __len;
461 #ifdef __GXX_EXPERIMENTAL_CXX0X__
462 template<typename _Tp, typename _Alloc>
464 vector<_Tp, _Alloc>::
465 _M_default_append(size_type __n)
469 if (size_type(this->_M_impl._M_end_of_storage
470 - this->_M_impl._M_finish) >= __n)
472 std::__uninitialized_default_n_a(this->_M_impl._M_finish,
473 __n, _M_get_Tp_allocator());
474 this->_M_impl._M_finish += __n;
478 const size_type __len =
479 _M_check_len(__n, "vector::_M_default_append");
480 const size_type __old_size = this->size();
481 pointer __new_start(this->_M_allocate(__len));
482 pointer __new_finish(__new_start);
486 std::__uninitialized_move_a(this->_M_impl._M_start,
487 this->_M_impl._M_finish,
489 _M_get_Tp_allocator());
490 std::__uninitialized_default_n_a(__new_finish, __n,
491 _M_get_Tp_allocator());
496 std::_Destroy(__new_start, __new_finish,
497 _M_get_Tp_allocator());
498 _M_deallocate(__new_start, __len);
499 __throw_exception_again;
501 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
502 _M_get_Tp_allocator());
503 _M_deallocate(this->_M_impl._M_start,
504 this->_M_impl._M_end_of_storage
505 - this->_M_impl._M_start);
506 this->_M_impl._M_start = __new_start;
507 this->_M_impl._M_finish = __new_finish;
508 this->_M_impl._M_end_of_storage = __new_start + __len;
514 template<typename _Tp, typename _Alloc>
515 template<typename _InputIterator>
517 vector<_Tp, _Alloc>::
518 _M_range_insert(iterator __pos, _InputIterator __first,
519 _InputIterator __last, std::input_iterator_tag)
521 for (; __first != __last; ++__first)
523 __pos = insert(__pos, *__first);
528 template<typename _Tp, typename _Alloc>
529 template<typename _ForwardIterator>
531 vector<_Tp, _Alloc>::
532 _M_range_insert(iterator __position, _ForwardIterator __first,
533 _ForwardIterator __last, std::forward_iterator_tag)
535 if (__first != __last)
537 const size_type __n = std::distance(__first, __last);
538 if (size_type(this->_M_impl._M_end_of_storage
539 - this->_M_impl._M_finish) >= __n)
541 const size_type __elems_after = end() - __position;
542 pointer __old_finish(this->_M_impl._M_finish);
543 if (__elems_after > __n)
545 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
546 this->_M_impl._M_finish,
547 this->_M_impl._M_finish,
548 _M_get_Tp_allocator());
549 this->_M_impl._M_finish += __n;
550 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
551 __old_finish - __n, __old_finish);
552 std::copy(__first, __last, __position);
556 _ForwardIterator __mid = __first;
557 std::advance(__mid, __elems_after);
558 std::__uninitialized_copy_a(__mid, __last,
559 this->_M_impl._M_finish,
560 _M_get_Tp_allocator());
561 this->_M_impl._M_finish += __n - __elems_after;
562 std::__uninitialized_move_a(__position.base(),
564 this->_M_impl._M_finish,
565 _M_get_Tp_allocator());
566 this->_M_impl._M_finish += __elems_after;
567 std::copy(__first, __mid, __position);
572 const size_type __len =
573 _M_check_len(__n, "vector::_M_range_insert");
574 pointer __new_start(this->_M_allocate(__len));
575 pointer __new_finish(__new_start);
579 std::__uninitialized_move_a(this->_M_impl._M_start,
582 _M_get_Tp_allocator());
584 std::__uninitialized_copy_a(__first, __last,
586 _M_get_Tp_allocator());
588 std::__uninitialized_move_a(__position.base(),
589 this->_M_impl._M_finish,
591 _M_get_Tp_allocator());
595 std::_Destroy(__new_start, __new_finish,
596 _M_get_Tp_allocator());
597 _M_deallocate(__new_start, __len);
598 __throw_exception_again;
600 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
601 _M_get_Tp_allocator());
602 _M_deallocate(this->_M_impl._M_start,
603 this->_M_impl._M_end_of_storage
604 - this->_M_impl._M_start);
605 this->_M_impl._M_start = __new_start;
606 this->_M_impl._M_finish = __new_finish;
607 this->_M_impl._M_end_of_storage = __new_start + __len;
615 template<typename _Alloc>
617 vector<bool, _Alloc>::
618 reserve(size_type __n)
620 if (__n > this->max_size())
621 __throw_length_error(__N("vector::reserve"));
622 if (this->capacity() < __n)
624 _Bit_type* __q = this->_M_allocate(__n);
625 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
627 this->_M_deallocate();
628 this->_M_impl._M_start = iterator(__q, 0);
629 this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1)
634 template<typename _Alloc>
636 vector<bool, _Alloc>::
637 _M_fill_insert(iterator __position, size_type __n, bool __x)
641 if (capacity() - size() >= __n)
643 std::copy_backward(__position, end(),
644 this->_M_impl._M_finish + difference_type(__n));
645 std::fill(__position, __position + difference_type(__n), __x);
646 this->_M_impl._M_finish += difference_type(__n);
650 const size_type __len =
651 _M_check_len(__n, "vector<bool>::_M_fill_insert");
652 _Bit_type * __q = this->_M_allocate(__len);
653 iterator __i = _M_copy_aligned(begin(), __position,
655 std::fill(__i, __i + difference_type(__n), __x);
656 this->_M_impl._M_finish = std::copy(__position, end(),
657 __i + difference_type(__n));
658 this->_M_deallocate();
659 this->_M_impl._M_end_of_storage = (__q + ((__len
660 + int(_S_word_bit) - 1)
661 / int(_S_word_bit)));
662 this->_M_impl._M_start = iterator(__q, 0);
666 template<typename _Alloc>
667 template<typename _ForwardIterator>
669 vector<bool, _Alloc>::
670 _M_insert_range(iterator __position, _ForwardIterator __first,
671 _ForwardIterator __last, std::forward_iterator_tag)
673 if (__first != __last)
675 size_type __n = std::distance(__first, __last);
676 if (capacity() - size() >= __n)
678 std::copy_backward(__position, end(),
679 this->_M_impl._M_finish
680 + difference_type(__n));
681 std::copy(__first, __last, __position);
682 this->_M_impl._M_finish += difference_type(__n);
686 const size_type __len =
687 _M_check_len(__n, "vector<bool>::_M_insert_range");
688 _Bit_type * __q = this->_M_allocate(__len);
689 iterator __i = _M_copy_aligned(begin(), __position,
691 __i = std::copy(__first, __last, __i);
692 this->_M_impl._M_finish = std::copy(__position, end(), __i);
693 this->_M_deallocate();
694 this->_M_impl._M_end_of_storage = (__q
696 + int(_S_word_bit) - 1)
697 / int(_S_word_bit)));
698 this->_M_impl._M_start = iterator(__q, 0);
703 template<typename _Alloc>
705 vector<bool, _Alloc>::
706 _M_insert_aux(iterator __position, bool __x)
708 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
710 std::copy_backward(__position, this->_M_impl._M_finish,
711 this->_M_impl._M_finish + 1);
713 ++this->_M_impl._M_finish;
717 const size_type __len =
718 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
719 _Bit_type * __q = this->_M_allocate(__len);
720 iterator __i = _M_copy_aligned(begin(), __position,
723 this->_M_impl._M_finish = std::copy(__position, end(), __i);
724 this->_M_deallocate();
725 this->_M_impl._M_end_of_storage = (__q + ((__len
726 + int(_S_word_bit) - 1)
727 / int(_S_word_bit)));
728 this->_M_impl._M_start = iterator(__q, 0);
732 _GLIBCXX_END_NESTED_NAMESPACE
734 #ifdef __GXX_EXPERIMENTAL_CXX0X__
736 _GLIBCXX_BEGIN_NAMESPACE(std)
738 template<typename _Alloc>
740 hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>::
741 operator()(const _GLIBCXX_STD_D::vector<bool, _Alloc>& __b) const
744 using _GLIBCXX_STD_D::_S_word_bit;
745 using _GLIBCXX_STD_D::_Bit_type;
747 const size_t __words = __b.size() / _S_word_bit;
750 const size_t __clength = __words * sizeof(_Bit_type);
751 __hash = std::_Fnv_hash::hash(__b._M_impl._M_start._M_p, __clength);
754 const size_t __extrabits = __b.size() % _S_word_bit;
757 _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
758 __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
760 const size_t __clength
761 = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
763 __hash = std::_Fnv_hash::hash(&__hiword, __clength, __hash);
765 __hash = std::_Fnv_hash::hash(&__hiword, __clength);
771 _GLIBCXX_END_NAMESPACE
773 #endif // __GXX_EXPERIMENTAL_CXX0X__
775 #endif /* _VECTOR_TCC */