]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/vector.tcc
allocator.h (__shrink_to_fit): Rename to __shrink_to_fit_aux, fix.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / vector.tcc
1 // Vector implementation (out of line) -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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.
20
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/>.
25
26 /*
27 *
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
30 *
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.
38 *
39 *
40 * Copyright (c) 1996
41 * Silicon Graphics Computer Systems, Inc.
42 *
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.
50 */
51
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}
55 */
56
57 #ifndef _VECTOR_TCC
58 #define _VECTOR_TCC 1
59
60 namespace std _GLIBCXX_VISIBILITY(default)
61 {
62 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
63
64 template<typename _Tp, typename _Alloc>
65 void
66 vector<_Tp, _Alloc>::
67 reserve(size_type __n)
68 {
69 if (__n > this->max_size())
70 __throw_length_error(__N("vector::reserve"));
71 if (this->capacity() < __n)
72 {
73 const size_type __old_size = size();
74 pointer __tmp = _M_allocate_and_copy(__n,
75 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
76 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_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;
85 }
86 }
87
88 #ifdef __GXX_EXPERIMENTAL_CXX0X__
89 template<typename _Tp, typename _Alloc>
90 template<typename... _Args>
91 void
92 vector<_Tp, _Alloc>::
93 emplace_back(_Args&&... __args)
94 {
95 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
96 {
97 this->_M_impl.construct(this->_M_impl._M_finish,
98 std::forward<_Args>(__args)...);
99 ++this->_M_impl._M_finish;
100 }
101 else
102 _M_insert_aux(end(), std::forward<_Args>(__args)...);
103 }
104 #endif
105
106 template<typename _Tp, typename _Alloc>
107 typename vector<_Tp, _Alloc>::iterator
108 vector<_Tp, _Alloc>::
109 insert(iterator __position, const value_type& __x)
110 {
111 const size_type __n = __position - begin();
112 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
113 && __position == end())
114 {
115 this->_M_impl.construct(this->_M_impl._M_finish, __x);
116 ++this->_M_impl._M_finish;
117 }
118 else
119 {
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
121 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
122 {
123 _Tp __x_copy = __x;
124 _M_insert_aux(__position, std::move(__x_copy));
125 }
126 else
127 #endif
128 _M_insert_aux(__position, __x);
129 }
130 return iterator(this->_M_impl._M_start + __n);
131 }
132
133 template<typename _Tp, typename _Alloc>
134 typename vector<_Tp, _Alloc>::iterator
135 vector<_Tp, _Alloc>::
136 erase(iterator __position)
137 {
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);
142 return __position;
143 }
144
145 template<typename _Tp, typename _Alloc>
146 typename vector<_Tp, _Alloc>::iterator
147 vector<_Tp, _Alloc>::
148 erase(iterator __first, iterator __last)
149 {
150 if (__last != end())
151 _GLIBCXX_MOVE3(__last, end(), __first);
152 _M_erase_at_end(__first.base() + (end() - __last));
153 return __first;
154 }
155
156 template<typename _Tp, typename _Alloc>
157 vector<_Tp, _Alloc>&
158 vector<_Tp, _Alloc>::
159 operator=(const vector<_Tp, _Alloc>& __x)
160 {
161 if (&__x != this)
162 {
163 const size_type __xlen = __x.size();
164 if (__xlen > capacity())
165 {
166 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
167 __x.end());
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;
175 }
176 else if (size() >= __xlen)
177 {
178 std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
179 end(), _M_get_Tp_allocator());
180 }
181 else
182 {
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());
189 }
190 this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
191 }
192 return *this;
193 }
194
195 template<typename _Tp, typename _Alloc>
196 void
197 vector<_Tp, _Alloc>::
198 _M_fill_assign(size_t __n, const value_type& __val)
199 {
200 if (__n > capacity())
201 {
202 vector __tmp(__n, __val, _M_get_Tp_allocator());
203 __tmp.swap(*this);
204 }
205 else if (__n > size())
206 {
207 std::fill(begin(), end(), __val);
208 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
209 __n - size(), __val,
210 _M_get_Tp_allocator());
211 this->_M_impl._M_finish += __n - size();
212 }
213 else
214 _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
215 }
216
217 template<typename _Tp, typename _Alloc>
218 template<typename _InputIterator>
219 void
220 vector<_Tp, _Alloc>::
221 _M_assign_aux(_InputIterator __first, _InputIterator __last,
222 std::input_iterator_tag)
223 {
224 pointer __cur(this->_M_impl._M_start);
225 for (; __first != __last && __cur != this->_M_impl._M_finish;
226 ++__cur, ++__first)
227 *__cur = *__first;
228 if (__first == __last)
229 _M_erase_at_end(__cur);
230 else
231 insert(end(), __first, __last);
232 }
233
234 template<typename _Tp, typename _Alloc>
235 template<typename _ForwardIterator>
236 void
237 vector<_Tp, _Alloc>::
238 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
239 std::forward_iterator_tag)
240 {
241 const size_type __len = std::distance(__first, __last);
242
243 if (__len > capacity())
244 {
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;
254 }
255 else if (size() >= __len)
256 _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
257 else
258 {
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());
266 }
267 }
268
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)
275 {
276 const size_type __n = __position - begin();
277 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
278 && __position == end())
279 {
280 this->_M_impl.construct(this->_M_impl._M_finish,
281 std::forward<_Args>(__args)...);
282 ++this->_M_impl._M_finish;
283 }
284 else
285 _M_insert_aux(__position, std::forward<_Args>(__args)...);
286 return iterator(this->_M_impl._M_start + __n);
287 }
288
289 template<typename _Tp, typename _Alloc>
290 template<typename... _Args>
291 void
292 vector<_Tp, _Alloc>::
293 _M_insert_aux(iterator __position, _Args&&... __args)
294 #else
295 template<typename _Tp, typename _Alloc>
296 void
297 vector<_Tp, _Alloc>::
298 _M_insert_aux(iterator __position, const _Tp& __x)
299 #endif
300 {
301 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
302 {
303 this->_M_impl.construct(this->_M_impl._M_finish,
304 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
305 - 1)));
306 ++this->_M_impl._M_finish;
307 #ifndef __GXX_EXPERIMENTAL_CXX0X__
308 _Tp __x_copy = __x;
309 #endif
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;
315 #else
316 *__position = _Tp(std::forward<_Args>(__args)...);
317 #endif
318 }
319 else
320 {
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);
326 __try
327 {
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)...);
335 #else
336 __x);
337 #endif
338 __new_finish = 0;
339
340 __new_finish
341 = std::__uninitialized_move_if_noexcept_a
342 (this->_M_impl._M_start, __position.base(),
343 __new_start, _M_get_Tp_allocator());
344
345 ++__new_finish;
346
347 __new_finish
348 = std::__uninitialized_move_if_noexcept_a
349 (__position.base(), this->_M_impl._M_finish,
350 __new_finish, _M_get_Tp_allocator());
351 }
352 __catch(...)
353 {
354 if (!__new_finish)
355 this->_M_impl.destroy(__new_start + __elems_before);
356 else
357 std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
358 _M_deallocate(__new_start, __len);
359 __throw_exception_again;
360 }
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;
369 }
370 }
371
372 template<typename _Tp, typename _Alloc>
373 void
374 vector<_Tp, _Alloc>::
375 _M_fill_insert(iterator __position, size_type __n, const value_type& __x)
376 {
377 if (__n != 0)
378 {
379 if (size_type(this->_M_impl._M_end_of_storage
380 - this->_M_impl._M_finish) >= __n)
381 {
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)
386 {
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,
395 __x_copy);
396 }
397 else
398 {
399 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
400 __n - __elems_after,
401 __x_copy,
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);
409 }
410 }
411 else
412 {
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);
418 __try
419 {
420 // See _M_insert_aux above.
421 std::__uninitialized_fill_n_a(__new_start + __elems_before,
422 __n, __x,
423 _M_get_Tp_allocator());
424 __new_finish = 0;
425
426 __new_finish
427 = std::__uninitialized_move_if_noexcept_a
428 (this->_M_impl._M_start, __position.base(),
429 __new_start, _M_get_Tp_allocator());
430
431 __new_finish += __n;
432
433 __new_finish
434 = std::__uninitialized_move_if_noexcept_a
435 (__position.base(), this->_M_impl._M_finish,
436 __new_finish, _M_get_Tp_allocator());
437 }
438 __catch(...)
439 {
440 if (!__new_finish)
441 std::_Destroy(__new_start + __elems_before,
442 __new_start + __elems_before + __n,
443 _M_get_Tp_allocator());
444 else
445 std::_Destroy(__new_start, __new_finish,
446 _M_get_Tp_allocator());
447 _M_deallocate(__new_start, __len);
448 __throw_exception_again;
449 }
450 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
451 _M_get_Tp_allocator());
452 _M_deallocate(this->_M_impl._M_start,
453 this->_M_impl._M_end_of_storage
454 - this->_M_impl._M_start);
455 this->_M_impl._M_start = __new_start;
456 this->_M_impl._M_finish = __new_finish;
457 this->_M_impl._M_end_of_storage = __new_start + __len;
458 }
459 }
460 }
461
462 #ifdef __GXX_EXPERIMENTAL_CXX0X__
463 template<typename _Tp, typename _Alloc>
464 void
465 vector<_Tp, _Alloc>::
466 _M_default_append(size_type __n)
467 {
468 if (__n != 0)
469 {
470 if (size_type(this->_M_impl._M_end_of_storage
471 - this->_M_impl._M_finish) >= __n)
472 {
473 std::__uninitialized_default_n_a(this->_M_impl._M_finish,
474 __n, _M_get_Tp_allocator());
475 this->_M_impl._M_finish += __n;
476 }
477 else
478 {
479 const size_type __len =
480 _M_check_len(__n, "vector::_M_default_append");
481 const size_type __old_size = this->size();
482 pointer __new_start(this->_M_allocate(__len));
483 pointer __new_finish(__new_start);
484 __try
485 {
486 __new_finish
487 = std::__uninitialized_move_if_noexcept_a
488 (this->_M_impl._M_start, this->_M_impl._M_finish,
489 __new_start, _M_get_Tp_allocator());
490 std::__uninitialized_default_n_a(__new_finish, __n,
491 _M_get_Tp_allocator());
492 __new_finish += __n;
493 }
494 __catch(...)
495 {
496 std::_Destroy(__new_start, __new_finish,
497 _M_get_Tp_allocator());
498 _M_deallocate(__new_start, __len);
499 __throw_exception_again;
500 }
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;
509 }
510 }
511 }
512
513 template<typename _Tp, typename _Alloc>
514 bool
515 vector<_Tp, _Alloc>::
516 _M_shrink_to_fit()
517 {
518 if (capacity() == size())
519 return false;
520 return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
521 }
522 #endif
523
524 template<typename _Tp, typename _Alloc>
525 template<typename _InputIterator>
526 void
527 vector<_Tp, _Alloc>::
528 _M_range_insert(iterator __pos, _InputIterator __first,
529 _InputIterator __last, std::input_iterator_tag)
530 {
531 for (; __first != __last; ++__first)
532 {
533 __pos = insert(__pos, *__first);
534 ++__pos;
535 }
536 }
537
538 template<typename _Tp, typename _Alloc>
539 template<typename _ForwardIterator>
540 void
541 vector<_Tp, _Alloc>::
542 _M_range_insert(iterator __position, _ForwardIterator __first,
543 _ForwardIterator __last, std::forward_iterator_tag)
544 {
545 if (__first != __last)
546 {
547 const size_type __n = std::distance(__first, __last);
548 if (size_type(this->_M_impl._M_end_of_storage
549 - this->_M_impl._M_finish) >= __n)
550 {
551 const size_type __elems_after = end() - __position;
552 pointer __old_finish(this->_M_impl._M_finish);
553 if (__elems_after > __n)
554 {
555 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
556 this->_M_impl._M_finish,
557 this->_M_impl._M_finish,
558 _M_get_Tp_allocator());
559 this->_M_impl._M_finish += __n;
560 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
561 __old_finish - __n, __old_finish);
562 std::copy(__first, __last, __position);
563 }
564 else
565 {
566 _ForwardIterator __mid = __first;
567 std::advance(__mid, __elems_after);
568 std::__uninitialized_copy_a(__mid, __last,
569 this->_M_impl._M_finish,
570 _M_get_Tp_allocator());
571 this->_M_impl._M_finish += __n - __elems_after;
572 std::__uninitialized_move_a(__position.base(),
573 __old_finish,
574 this->_M_impl._M_finish,
575 _M_get_Tp_allocator());
576 this->_M_impl._M_finish += __elems_after;
577 std::copy(__first, __mid, __position);
578 }
579 }
580 else
581 {
582 const size_type __len =
583 _M_check_len(__n, "vector::_M_range_insert");
584 pointer __new_start(this->_M_allocate(__len));
585 pointer __new_finish(__new_start);
586 __try
587 {
588 __new_finish
589 = std::__uninitialized_move_if_noexcept_a
590 (this->_M_impl._M_start, __position.base(),
591 __new_start, _M_get_Tp_allocator());
592 __new_finish
593 = std::__uninitialized_copy_a(__first, __last,
594 __new_finish,
595 _M_get_Tp_allocator());
596 __new_finish
597 = std::__uninitialized_move_if_noexcept_a
598 (__position.base(), this->_M_impl._M_finish,
599 __new_finish, _M_get_Tp_allocator());
600 }
601 __catch(...)
602 {
603 std::_Destroy(__new_start, __new_finish,
604 _M_get_Tp_allocator());
605 _M_deallocate(__new_start, __len);
606 __throw_exception_again;
607 }
608 std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
609 _M_get_Tp_allocator());
610 _M_deallocate(this->_M_impl._M_start,
611 this->_M_impl._M_end_of_storage
612 - this->_M_impl._M_start);
613 this->_M_impl._M_start = __new_start;
614 this->_M_impl._M_finish = __new_finish;
615 this->_M_impl._M_end_of_storage = __new_start + __len;
616 }
617 }
618 }
619
620
621 // vector<bool>
622 template<typename _Alloc>
623 void
624 vector<bool, _Alloc>::
625 _M_reallocate(size_type __n)
626 {
627 _Bit_type* __q = this->_M_allocate(__n);
628 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
629 iterator(__q, 0));
630 this->_M_deallocate();
631 this->_M_impl._M_start = iterator(__q, 0);
632 this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
633 }
634
635 template<typename _Alloc>
636 void
637 vector<bool, _Alloc>::
638 _M_fill_insert(iterator __position, size_type __n, bool __x)
639 {
640 if (__n == 0)
641 return;
642 if (capacity() - size() >= __n)
643 {
644 std::copy_backward(__position, end(),
645 this->_M_impl._M_finish + difference_type(__n));
646 std::fill(__position, __position + difference_type(__n), __x);
647 this->_M_impl._M_finish += difference_type(__n);
648 }
649 else
650 {
651 const size_type __len =
652 _M_check_len(__n, "vector<bool>::_M_fill_insert");
653 _Bit_type * __q = this->_M_allocate(__len);
654 iterator __i = _M_copy_aligned(begin(), __position,
655 iterator(__q, 0));
656 std::fill(__i, __i + difference_type(__n), __x);
657 this->_M_impl._M_finish = std::copy(__position, end(),
658 __i + difference_type(__n));
659 this->_M_deallocate();
660 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
661 this->_M_impl._M_start = iterator(__q, 0);
662 }
663 }
664
665 template<typename _Alloc>
666 template<typename _ForwardIterator>
667 void
668 vector<bool, _Alloc>::
669 _M_insert_range(iterator __position, _ForwardIterator __first,
670 _ForwardIterator __last, std::forward_iterator_tag)
671 {
672 if (__first != __last)
673 {
674 size_type __n = std::distance(__first, __last);
675 if (capacity() - size() >= __n)
676 {
677 std::copy_backward(__position, end(),
678 this->_M_impl._M_finish
679 + difference_type(__n));
680 std::copy(__first, __last, __position);
681 this->_M_impl._M_finish += difference_type(__n);
682 }
683 else
684 {
685 const size_type __len =
686 _M_check_len(__n, "vector<bool>::_M_insert_range");
687 _Bit_type * __q = this->_M_allocate(__len);
688 iterator __i = _M_copy_aligned(begin(), __position,
689 iterator(__q, 0));
690 __i = std::copy(__first, __last, __i);
691 this->_M_impl._M_finish = std::copy(__position, end(), __i);
692 this->_M_deallocate();
693 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
694 this->_M_impl._M_start = iterator(__q, 0);
695 }
696 }
697 }
698
699 template<typename _Alloc>
700 void
701 vector<bool, _Alloc>::
702 _M_insert_aux(iterator __position, bool __x)
703 {
704 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
705 {
706 std::copy_backward(__position, this->_M_impl._M_finish,
707 this->_M_impl._M_finish + 1);
708 *__position = __x;
709 ++this->_M_impl._M_finish;
710 }
711 else
712 {
713 const size_type __len =
714 _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
715 _Bit_type * __q = this->_M_allocate(__len);
716 iterator __i = _M_copy_aligned(begin(), __position,
717 iterator(__q, 0));
718 *__i++ = __x;
719 this->_M_impl._M_finish = std::copy(__position, end(), __i);
720 this->_M_deallocate();
721 this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
722 this->_M_impl._M_start = iterator(__q, 0);
723 }
724 }
725
726 #ifdef __GXX_EXPERIMENTAL_CXX0X__
727 template<typename _Alloc>
728 bool
729 vector<bool, _Alloc>::
730 _M_shrink_to_fit()
731 {
732 if (capacity() - size() < int(_S_word_bit))
733 return false;
734 __try
735 {
736 _M_reallocate(size());
737 return true;
738 }
739 __catch(...)
740 { return false; }
741 }
742 #endif
743
744 _GLIBCXX_END_NAMESPACE_CONTAINER
745 } // namespace std
746
747 #ifdef __GXX_EXPERIMENTAL_CXX0X__
748
749 namespace std _GLIBCXX_VISIBILITY(default)
750 {
751 _GLIBCXX_BEGIN_NAMESPACE_VERSION
752
753 template<typename _Alloc>
754 size_t
755 hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
756 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const
757 {
758 size_t __hash = 0;
759 using _GLIBCXX_STD_C::_S_word_bit;
760 using _GLIBCXX_STD_C::_Bit_type;
761
762 const size_t __words = __b.size() / _S_word_bit;
763 if (__words)
764 {
765 const size_t __clength = __words * sizeof(_Bit_type);
766 __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
767 }
768
769 const size_t __extrabits = __b.size() % _S_word_bit;
770 if (__extrabits)
771 {
772 _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
773 __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
774
775 const size_t __clength
776 = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
777 if (__words)
778 __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
779 else
780 __hash = std::_Hash_impl::hash(&__hiword, __clength);
781 }
782
783 return __hash;
784 }
785
786 _GLIBCXX_END_NAMESPACE_VERSION
787 } // namespace std
788
789 #endif // __GXX_EXPERIMENTAL_CXX0X__
790
791 #endif /* _VECTOR_TCC */