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 60namespace 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_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; 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_a(this->_M_impl._M_start, 342 __position.base(), __new_start, 343 _M_get_Tp_allocator()); 344 ++__new_finish; 345 346 __new_finish = 347 std::__uninitialized_move_a(__position.base(), 348 this->_M_impl._M_finish, 349 __new_finish, 350 _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_a(this->_M_impl._M_start, 428 __position.base(), 429 __new_start, 430 _M_get_Tp_allocator()); 431 __new_finish += __n; 432 433 __new_finish = 434 std::__uninitialized_move_a(__position.base(), 435 this->_M_impl._M_finish, 436 __new_finish, 437 _M_get_Tp_allocator()); 438 } 439 __catch(...) 440 { 441 if (!__new_finish) 442 std::_Destroy(__new_start + __elems_before, 443 __new_start + __elems_before + __n, 444 _M_get_Tp_allocator()); 445 else 446 std::_Destroy(__new_start, __new_finish, 447 _M_get_Tp_allocator()); 448 _M_deallocate(__new_start, __len); 449 __throw_exception_again; 450 } 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; 459 } 460 } 461 } 462 463#ifdef __GXX_EXPERIMENTAL_CXX0X__ 464 template<typename _Tp, typename _Alloc> 465 void 466 vector<_Tp, _Alloc>:: 467 _M_default_append(size_type __n) 468 { 469 if (__n != 0) 470 { 471 if (size_type(this->_M_impl._M_end_of_storage 472 - this->_M_impl._M_finish) >= __n) 473 { 474 std::__uninitialized_default_n_a(this->_M_impl._M_finish, 475 __n, _M_get_Tp_allocator()); 476 this->_M_impl._M_finish += __n; 477 } 478 else 479 { 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); 485 __try 486 { 487 __new_finish = 488 std::__uninitialized_move_a(this->_M_impl._M_start, 489 this->_M_impl._M_finish, 490 __new_start, 491 _M_get_Tp_allocator()); 492 std::__uninitialized_default_n_a(__new_finish, __n, 493 _M_get_Tp_allocator()); 494 __new_finish += __n; 495 } 496 __catch(...) 497 { 498 std::_Destroy(__new_start, __new_finish, 499 _M_get_Tp_allocator()); 500 _M_deallocate(__new_start, __len); 501 __throw_exception_again; 502 } 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; 511 } 512 } 513 } 514#endif 515 516 template<typename _Tp, typename _Alloc> 517 template<typename _InputIterator> 518 void 519 vector<_Tp, _Alloc>:: 520 _M_range_insert(iterator __pos, _InputIterator __first, 521 _InputIterator __last, std::input_iterator_tag) 522 { 523 for (; __first != __last; ++__first) 524 { 525 __pos = insert(__pos, *__first); 526 ++__pos; 527 } 528 } 529 530 template<typename _Tp, typename _Alloc> 531 template<typename _ForwardIterator> 532 void 533 vector<_Tp, _Alloc>:: 534 _M_range_insert(iterator __position, _ForwardIterator __first, 535 _ForwardIterator __last, std::forward_iterator_tag) 536 { 537 if (__first != __last) 538 { 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) 542 { 543 const size_type __elems_after = end() - __position; 544 pointer __old_finish(this->_M_impl._M_finish); 545 if (__elems_after > __n) 546 { 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); 555 } 556 else 557 { 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(), 565 __old_finish, 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); 570 } 571 } 572 else 573 { 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); 578 __try 579 { 580 __new_finish = 581 std::__uninitialized_move_a(this->_M_impl._M_start, 582 __position.base(), 583 __new_start, 584 _M_get_Tp_allocator()); 585 __new_finish = 586 std::__uninitialized_copy_a(__first, __last, 587 __new_finish, 588 _M_get_Tp_allocator()); 589 __new_finish = 590 std::__uninitialized_move_a(__position.base(), 591 this->_M_impl._M_finish, 592 __new_finish, 593 _M_get_Tp_allocator()); 594 } 595 __catch(...) 596 { 597 std::_Destroy(__new_start, __new_finish, 598 _M_get_Tp_allocator()); 599 _M_deallocate(__new_start, __len); 600 __throw_exception_again; 601 } 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; 610 } 611 } 612 } 613 614 615 // vector<bool> 616 617 template<typename _Alloc> 618 void 619 vector<bool, _Alloc>:: 620 reserve(size_type __n) 621 { 622 if (__n > this->max_size()) 623 __throw_length_error(__N("vector::reserve")); 624 if (this->capacity() < __n) 625 { 626 _Bit_type* __q = this->_M_allocate(__n); 627 this->_M_impl._M_finish = _M_copy_aligned(begin(), end(), 628 iterator(__q, 0)); 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) 632 / int(_S_word_bit)); 633 } 634 } 635 636 template<typename _Alloc> 637 void 638 vector<bool, _Alloc>:: 639 _M_fill_insert(iterator __position, size_type __n, bool __x) 640 { 641 if (__n == 0) 642 return; 643 if (capacity() - size() >= __n) 644 { 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); 649 } 650 else 651 { 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, 656 iterator(__q, 0)); 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); 665 } 666 } 667 668 template<typename _Alloc> 669 template<typename _ForwardIterator> 670 void 671 vector<bool, _Alloc>:: 672 _M_insert_range(iterator __position, _ForwardIterator __first, 673 _ForwardIterator __last, std::forward_iterator_tag) 674 { 675 if (__first != __last) 676 { 677 size_type __n = std::distance(__first, __last); 678 if (capacity() - size() >= __n) 679 { 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); 685 } 686 else 687 { 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, 692 iterator(__q, 0)); 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 697 + ((__len 698 + int(_S_word_bit) - 1) 699 / int(_S_word_bit))); 700 this->_M_impl._M_start = iterator(__q, 0); 701 } 702 } 703 } 704 705 template<typename _Alloc> 706 void 707 vector<bool, _Alloc>:: 708 _M_insert_aux(iterator __position, bool __x) 709 { 710 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage) 711 { 712 std::copy_backward(__position, this->_M_impl._M_finish, 713 this->_M_impl._M_finish + 1); 714 *__position = __x; 715 ++this->_M_impl._M_finish; 716 } 717 else 718 { 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, 723 iterator(__q, 0)); 724 *__i++ = __x; 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); 731 } 732 } 733 734_GLIBCXX_END_NAMESPACE_CONTAINER 735} // namespace std 736 737#ifdef __GXX_EXPERIMENTAL_CXX0X__ 738 739namespace std _GLIBCXX_VISIBILITY(default) 740{ 741_GLIBCXX_BEGIN_NAMESPACE_VERSION 742 743 template<typename _Alloc> 744 size_t 745 hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>:: 746 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const 747 { 748 size_t __hash = 0; 749 using _GLIBCXX_STD_C::_S_word_bit; 750 using _GLIBCXX_STD_C::_Bit_type; 751 752 const size_t __words = __b.size() / _S_word_bit; 753 if (__words) 754 { 755 const size_t __clength = __words * sizeof(_Bit_type); 756 __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength); 757 } 758 759 const size_t __extrabits = __b.size() % _S_word_bit; 760 if (__extrabits) 761 { 762 _Bit_type __hiword = *__b._M_impl._M_finish._M_p; 763 __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits); 764 765 const size_t __clength 766 = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__; 767 if (__words) 768 __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash); 769 else 770 __hash = std::_Hash_impl::hash(&__hiword, __clength); 771 } 772 773 return __hash; 774 } 775 776_GLIBCXX_END_NAMESPACE_VERSION 777} // namespace std 778 779#endif // __GXX_EXPERIMENTAL_CXX0X__ 780 781#endif /* _VECTOR_TCC */ 782