1 // Vector implementation (out of line) -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 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.
52 /** @file bits/vector.tcc
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{vector}
60 namespace std _GLIBCXX_VISIBILITY(default)
62 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
64 template<typename _Tp, typename _Alloc>
67 reserve(size_type __n)
69 if (__n > this->max_size())
70 __throw_length_error(__N("vector::reserve"));
71 if (this->capacity() < __n)
73 const size_type __old_size = size();
74 pointer __tmp = _M_allocate_and_copy(__n,
75 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start),
76 _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish));
77 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
78 _M_get_Tp_allocator());
79 _M_deallocate(this->_M_impl._M_start,
80 this->_M_impl._M_end_of_storage
81 - this->_M_impl._M_start);
82 this->_M_impl._M_start = __tmp;
83 this->_M_impl._M_finish = __tmp + __old_size;
84 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
88 #ifdef __GXX_EXPERIMENTAL_CXX0X__
89 template<typename _Tp, typename _Alloc>
90 template<typename... _Args>
93 emplace_back(_Args&&... __args)
95 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
97 this->_M_impl.construct(this->_M_impl._M_finish,
98 std::forward<_Args>(__args)...);
99 ++this->_M_impl._M_finish;
102 _M_insert_aux(end(), std::forward<_Args>(__args)...);
106 template<typename _Tp, typename _Alloc>
107 typename vector<_Tp, _Alloc>::iterator
108 vector<_Tp, _Alloc>::
109 insert(iterator __position, const value_type& __x)
111 const size_type __n = __position - begin();
112 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
113 && __position == end())
115 this->_M_impl.construct(this->_M_impl._M_finish, __x);
116 ++this->_M_impl._M_finish;
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
121 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
124 _M_insert_aux(__position, std::move(__x_copy));
128 _M_insert_aux(__position, __x);
130 return iterator(this->_M_impl._M_start + __n);
133 template<typename _Tp, typename _Alloc>
134 typename vector<_Tp, _Alloc>::iterator
135 vector<_Tp, _Alloc>::
136 erase(iterator __position)
138 if (__position + 1 != end())
139 _GLIBCXX_MOVE3(__position + 1, end(), __position);
140 --this->_M_impl._M_finish;
141 this->_M_impl.destroy(this->_M_impl._M_finish);
145 template<typename _Tp, typename _Alloc>
146 typename vector<_Tp, _Alloc>::iterator
147 vector<_Tp, _Alloc>::
148 erase(iterator __first, iterator __last)
151 _GLIBCXX_MOVE3(__last, end(), __first);
152 _M_erase_at_end(__first.base() + (end() - __last));
156 template<typename _Tp, typename _Alloc>
158 vector<_Tp, _Alloc>::
159 operator=(const vector<_Tp, _Alloc>& __x)
163 const size_type __xlen = __x.size();
164 if (__xlen > capacity())
166 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
168 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
169 _M_get_Tp_allocator());
170 _M_deallocate(this->_M_impl._M_start,
171 this->_M_impl._M_end_of_storage
172 - this->_M_impl._M_start);
173 this->_M_impl._M_start = __tmp;
174 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __xlen;
176 else if (size() >= __xlen)
178 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
179 end(), _M_get_Tp_allocator());
183 std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
184 this->_M_impl._M_start);
185 std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
186 __x._M_impl._M_finish,
187 this->_M_impl._M_finish,
188 _M_get_Tp_allocator());
190 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
195 template<typename _Tp, typename _Alloc>
197 vector<_Tp, _Alloc>::
198 _M_fill_assign(size_t __n, const value_type& __val)
200 if (__n > capacity())
202 vector __tmp(__n, __val, _M_get_Tp_allocator());
205 else if (__n > size())
207 std::fill(begin(), end(), __val);
208 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
210 _M_get_Tp_allocator());
211 this->_M_impl._M_finish += __n - size();
214 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
217 template<typename _Tp, typename _Alloc>
218 template<typename _InputIterator>
220 vector<_Tp, _Alloc>::
221 _M_assign_aux(_InputIterator __first, _InputIterator __last,
222 std::input_iterator_tag)
224 pointer __cur(this->_M_impl._M_start);
225 for (; __first != __last && __cur != this->_M_impl._M_finish;
228 if (__first == __last)
229 _M_erase_at_end(__cur);
231 insert(end(), __first, __last);
234 template<typename _Tp, typename _Alloc>
235 template<typename _ForwardIterator>
237 vector<_Tp, _Alloc>::
238 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
239 std::forward_iterator_tag)
241 const size_type __len = std::distance(__first, __last);
243 if (__len > capacity())
245 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
246 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
247 _M_get_Tp_allocator());
248 _M_deallocate(this->_M_impl._M_start,
249 this->_M_impl._M_end_of_storage
250 - this->_M_impl._M_start);
251 this->_M_impl._M_start = __tmp;
252 this->_M_impl._M_finish = this->_M_impl._M_start + __len;
253 this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
255 else if (size() >= __len)
256 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
259 _ForwardIterator __mid = __first;
260 std::advance(__mid, size());
261 std::copy(__first, __mid, this->_M_impl._M_start);
262 this->_M_impl._M_finish =
263 std::__uninitialized_copy_a(__mid, __last,
264 this->_M_impl._M_finish,
265 _M_get_Tp_allocator());
269 #ifdef __GXX_EXPERIMENTAL_CXX0X__
270 template<typename _Tp, typename _Alloc>
271 template<typename... _Args>
272 typename vector<_Tp, _Alloc>::iterator
273 vector<_Tp, _Alloc>::
274 emplace(iterator __position, _Args&&... __args)
276 const size_type __n = __position - begin();
277 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
278 && __position == end())
280 this->_M_impl.construct(this->_M_impl._M_finish,
281 std::forward<_Args>(__args)...);
282 ++this->_M_impl._M_finish;
285 _M_insert_aux(__position, std::forward<_Args>(__args)...);
286 return iterator(this->_M_impl._M_start + __n);
289 template<typename _Tp, typename _Alloc>
290 template<typename... _Args>
292 vector<_Tp, _Alloc>::
293 _M_insert_aux(iterator __position, _Args&&... __args)
295 template<typename _Tp, typename _Alloc>
297 vector<_Tp, _Alloc>::
298 _M_insert_aux(iterator __position, const _Tp& __x)
301 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
303 this->_M_impl.construct(this->_M_impl._M_finish,
304 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
306 ++this->_M_impl._M_finish;
307 #ifndef __GXX_EXPERIMENTAL_CXX0X__
310 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
311 this->_M_impl._M_finish - 2,
312 this->_M_impl._M_finish - 1);
313 #ifndef __GXX_EXPERIMENTAL_CXX0X__
314 *__position = __x_copy;
316 *__position = _Tp(std::forward<_Args>(__args)...);
321 const size_type __len =
322 _M_check_len(size_type(1), "vector::_M_insert_aux");
323 const size_type __elems_before = __position - begin();
324 pointer __new_start(this->_M_allocate(__len));
325 pointer __new_finish(__new_start);
328 // The order of the three operations is dictated by the C++0x
329 // case, where the moves could alter a new element belonging
330 // to the existing vector. This is an issue only for callers
331 // taking the element by const lvalue ref (see 23.1/13).
332 this->_M_impl.construct(__new_start + __elems_before,
333 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334 std::forward<_Args>(__args)...);
341 std::__uninitialized_move_a(this->_M_impl._M_start,
342 __position.base(), __new_start,
343 _M_get_Tp_allocator());
347 std::__uninitialized_move_a(__position.base(),
348 this->_M_impl._M_finish,
350 _M_get_Tp_allocator());
355 this->_M_impl.destroy(__new_start + __elems_before);
357 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
358 _M_deallocate(__new_start, __len);
359 __throw_exception_again;
361 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
362 _M_get_Tp_allocator());
363 _M_deallocate(this->_M_impl._M_start,
364 this->_M_impl._M_end_of_storage
365 - this->_M_impl._M_start);
366 this->_M_impl._M_start = __new_start;
367 this->_M_impl._M_finish = __new_finish;
368 this->_M_impl._M_end_of_storage = __new_start + __len;
372 template<typename _Tp, typename _Alloc>
374 vector<_Tp, _Alloc>::
375 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
379 if (size_type(this->_M_impl._M_end_of_storage
380 - this->_M_impl._M_finish) >= __n)
382 value_type __x_copy = __x;
383 const size_type __elems_after = end() - __position;
384 pointer __old_finish(this->_M_impl._M_finish);
385 if (__elems_after > __n)
387 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
388 this->_M_impl._M_finish,
389 this->_M_impl._M_finish,
390 _M_get_Tp_allocator());
391 this->_M_impl._M_finish += __n;
392 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
393 __old_finish - __n, __old_finish);
394 std::fill(__position.base(), __position.base() + __n,
399 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
402 _M_get_Tp_allocator());
403 this->_M_impl._M_finish += __n - __elems_after;
404 std::__uninitialized_move_a(__position.base(), __old_finish,
405 this->_M_impl._M_finish,
406 _M_get_Tp_allocator());
407 this->_M_impl._M_finish += __elems_after;
408 std::fill(__position.base(), __old_finish, __x_copy);
413 const size_type __len =
414 _M_check_len(__n, "vector::_M_fill_insert");
415 const size_type __elems_before = __position - begin();
416 pointer __new_start(this->_M_allocate(__len));
417 pointer __new_finish(__new_start);
420 // See _M_insert_aux above.
421 std::__uninitialized_fill_n_a(__new_start + __elems_before,
423 _M_get_Tp_allocator());
427 std::__uninitialized_move_a(this->_M_impl._M_start,
430 _M_get_Tp_allocator());
434 std::__uninitialized_move_a(__position.base(),
435 this->_M_impl._M_finish,
437 _M_get_Tp_allocator());
442 std::_Destroy(__new_start + __elems_before,
443 __new_start + __elems_before + __n,
444 _M_get_Tp_allocator());
446 std::_Destroy(__new_start, __new_finish,
447 _M_get_Tp_allocator());
448 _M_deallocate(__new_start, __len);
449 __throw_exception_again;
451 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
452 _M_get_Tp_allocator());
453 _M_deallocate(this->_M_impl._M_start,
454 this->_M_impl._M_end_of_storage
455 - this->_M_impl._M_start);
456 this->_M_impl._M_start = __new_start;
457 this->_M_impl._M_finish = __new_finish;
458 this->_M_impl._M_end_of_storage = __new_start + __len;
463 #ifdef __GXX_EXPERIMENTAL_CXX0X__
464 template<typename _Tp, typename _Alloc>
466 vector<_Tp, _Alloc>::
467 _M_default_append(size_type __n)
471 if (size_type(this->_M_impl._M_end_of_storage
472 - this->_M_impl._M_finish) >= __n)
474 std::__uninitialized_default_n_a(this->_M_impl._M_finish,
475 __n, _M_get_Tp_allocator());
476 this->_M_impl._M_finish += __n;
480 const size_type __len =
481 _M_check_len(__n, "vector::_M_default_append");
482 const size_type __old_size = this->size();
483 pointer __new_start(this->_M_allocate(__len));
484 pointer __new_finish(__new_start);
488 std::__uninitialized_move_a(this->_M_impl._M_start,
489 this->_M_impl._M_finish,
491 _M_get_Tp_allocator());
492 std::__uninitialized_default_n_a(__new_finish, __n,
493 _M_get_Tp_allocator());
498 std::_Destroy(__new_start, __new_finish,
499 _M_get_Tp_allocator());
500 _M_deallocate(__new_start, __len);
501 __throw_exception_again;
503 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
504 _M_get_Tp_allocator());
505 _M_deallocate(this->_M_impl._M_start,
506 this->_M_impl._M_end_of_storage
507 - this->_M_impl._M_start);
508 this->_M_impl._M_start = __new_start;
509 this->_M_impl._M_finish = __new_finish;
510 this->_M_impl._M_end_of_storage = __new_start + __len;
516 template<typename _Tp, typename _Alloc>
517 template<typename _InputIterator>
519 vector<_Tp, _Alloc>::
520 _M_range_insert(iterator __pos, _InputIterator __first,
521 _InputIterator __last, std::input_iterator_tag)
523 for (; __first != __last; ++__first)
525 __pos = insert(__pos, *__first);
530 template<typename _Tp, typename _Alloc>
531 template<typename _ForwardIterator>
533 vector<_Tp, _Alloc>::
534 _M_range_insert(iterator __position, _ForwardIterator __first,
535 _ForwardIterator __last, std::forward_iterator_tag)
537 if (__first != __last)
539 const size_type __n = std::distance(__first, __last);
540 if (size_type(this->_M_impl._M_end_of_storage
541 - this->_M_impl._M_finish) >= __n)
543 const size_type __elems_after = end() - __position;
544 pointer __old_finish(this->_M_impl._M_finish);
545 if (__elems_after > __n)
547 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
548 this->_M_impl._M_finish,
549 this->_M_impl._M_finish,
550 _M_get_Tp_allocator());
551 this->_M_impl._M_finish += __n;
552 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
553 __old_finish - __n, __old_finish);
554 std::copy(__first, __last, __position);
558 _ForwardIterator __mid = __first;
559 std::advance(__mid, __elems_after);
560 std::__uninitialized_copy_a(__mid, __last,
561 this->_M_impl._M_finish,
562 _M_get_Tp_allocator());
563 this->_M_impl._M_finish += __n - __elems_after;
564 std::__uninitialized_move_a(__position.base(),
566 this->_M_impl._M_finish,
567 _M_get_Tp_allocator());
568 this->_M_impl._M_finish += __elems_after;
569 std::copy(__first, __mid, __position);
574 const size_type __len =
575 _M_check_len(__n, "vector::_M_range_insert");
576 pointer __new_start(this->_M_allocate(__len));
577 pointer __new_finish(__new_start);
581 std::__uninitialized_move_a(this->_M_impl._M_start,
584 _M_get_Tp_allocator());
586 std::__uninitialized_copy_a(__first, __last,
588 _M_get_Tp_allocator());
590 std::__uninitialized_move_a(__position.base(),
591 this->_M_impl._M_finish,
593 _M_get_Tp_allocator());
597 std::_Destroy(__new_start, __new_finish,
598 _M_get_Tp_allocator());
599 _M_deallocate(__new_start, __len);
600 __throw_exception_again;
602 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
603 _M_get_Tp_allocator());
604 _M_deallocate(this->_M_impl._M_start,
605 this->_M_impl._M_end_of_storage
606 - this->_M_impl._M_start);
607 this->_M_impl._M_start = __new_start;
608 this->_M_impl._M_finish = __new_finish;
609 this->_M_impl._M_end_of_storage = __new_start + __len;
617 template<typename _Alloc>
619 vector<bool, _Alloc>::
620 reserve(size_type __n)
622 if (__n > this->max_size())
623 __throw_length_error(__N("vector::reserve"));
624 if (this->capacity() < __n)
626 _Bit_type* __q = this->_M_allocate(__n);
627 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
629 this->_M_deallocate();
630 this->_M_impl._M_start = iterator(__q, 0);
631 this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1)
636 template<typename _Alloc>
638 vector<bool, _Alloc>::
639 _M_fill_insert(iterator __position, size_type __n, bool __x)
643 if (capacity() - size() >= __n)
645 std::copy_backward(__position, end(),
646 this->_M_impl._M_finish + difference_type(__n));
647 std::fill(__position, __position + difference_type(__n), __x);
648 this->_M_impl._M_finish += difference_type(__n);
652 const size_type __len =
653 _M_check_len(__n, "vector<bool>::_M_fill_insert");
654 _Bit_type * __q = this->_M_allocate(__len);
655 iterator __i = _M_copy_aligned(begin(), __position,
657 std::fill(__i, __i + difference_type(__n), __x);
658 this->_M_impl._M_finish = std::copy(__position, end(),
659 __i + difference_type(__n));
660 this->_M_deallocate();
661 this->_M_impl._M_end_of_storage = (__q + ((__len
662 + int(_S_word_bit) - 1)
663 / int(_S_word_bit)));
664 this->_M_impl._M_start = iterator(__q, 0);
668 template<typename _Alloc>
669 template<typename _ForwardIterator>
671 vector<bool, _Alloc>::
672 _M_insert_range(iterator __position, _ForwardIterator __first,
673 _ForwardIterator __last, std::forward_iterator_tag)
675 if (__first != __last)
677 size_type __n = std::distance(__first, __last);
678 if (capacity() - size() >= __n)
680 std::copy_backward(__position, end(),
681 this->_M_impl._M_finish
682 + difference_type(__n));
683 std::copy(__first, __last, __position);
684 this->_M_impl._M_finish += difference_type(__n);
688 const size_type __len =
689 _M_check_len(__n, "vector<bool>::_M_insert_range");
690 _Bit_type * __q = this->_M_allocate(__len);
691 iterator __i = _M_copy_aligned(begin(), __position,
693 __i = std::copy(__first, __last, __i);
694 this->_M_impl._M_finish = std::copy(__position, end(), __i);
695 this->_M_deallocate();
696 this->_M_impl._M_end_of_storage = (__q
698 + int(_S_word_bit) - 1)
699 / int(_S_word_bit)));
700 this->_M_impl._M_start = iterator(__q, 0);
705 template<typename _Alloc>
707 vector<bool, _Alloc>::
708 _M_insert_aux(iterator __position, bool __x)
710 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
712 std::copy_backward(__position, this->_M_impl._M_finish,
713 this->_M_impl._M_finish + 1);
715 ++this->_M_impl._M_finish;
719 const size_type __len =
720 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
721 _Bit_type * __q = this->_M_allocate(__len);
722 iterator __i = _M_copy_aligned(begin(), __position,
725 this->_M_impl._M_finish = std::copy(__position, end(), __i);
726 this->_M_deallocate();
727 this->_M_impl._M_end_of_storage = (__q + ((__len
728 + int(_S_word_bit) - 1)
729 / int(_S_word_bit)));
730 this->_M_impl._M_start = iterator(__q, 0);
734 _GLIBCXX_END_NAMESPACE_CONTAINER
737 #ifdef __GXX_EXPERIMENTAL_CXX0X__
739 namespace std _GLIBCXX_VISIBILITY(default)
741 _GLIBCXX_BEGIN_NAMESPACE_VERSION
743 template<typename _Alloc>
745 hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
746 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const
749 using _GLIBCXX_STD_C::_S_word_bit;
750 using _GLIBCXX_STD_C::_Bit_type;
752 const size_t __words = __b.size() / _S_word_bit;
755 const size_t __clength = __words * sizeof(_Bit_type);
756 __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
759 const size_t __extrabits = __b.size() % _S_word_bit;
762 _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
763 __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
765 const size_t __clength
766 = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
768 __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
770 __hash = std::_Hash_impl::hash(&__hiword, __clength);
776 _GLIBCXX_END_NAMESPACE_VERSION
779 #endif // __GXX_EXPERIMENTAL_CXX0X__
781 #endif /* _VECTOR_TCC */