1 // vector<bool> specialization -*- C++ -*- 2 3 // Copyright (C) 2001-2014 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /* 26 * 27 * Copyright (c) 1994 28 * Hewlett-Packard Company 29 * 30 * Permission to use, copy, modify, distribute and sell this software 31 * and its documentation for any purpose is hereby granted without fee, 32 * provided that the above copyright notice appear in all copies and 33 * that both that copyright notice and this permission notice appear 34 * in supporting documentation. Hewlett-Packard Company makes no 35 * representations about the suitability of this software for any 36 * purpose. It is provided "as is" without express or implied warranty. 37 * 38 * 39 * Copyright (c) 1996-1999 40 * Silicon Graphics Computer Systems, Inc. 41 * 42 * Permission to use, copy, modify, distribute and sell this software 43 * and its documentation for any purpose is hereby granted without fee, 44 * provided that the above copyright notice appear in all copies and 45 * that both that copyright notice and this permission notice appear 46 * in supporting documentation. Silicon Graphics makes no 47 * representations about the suitability of this software for any 48 * purpose. It is provided "as is" without express or implied warranty. 49 */ 50 51 /** @file bits/stl_bvector.h 52 * This is an internal header file, included by other library headers. 53 * Do not attempt to use it directly. @headername{vector} 54 */ 55 56 #ifndef _STL_BVECTOR_H 57 #define _STL_BVECTOR_H 1 58 59 #if __cplusplus >= 201103L 60 #include <initializer_list> 61 #endif 62 63 namespace std _GLIBCXX_VISIBILITY(default) 64 { 65 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 66 67 typedef unsigned long _Bit_type; 68 enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) }; 69 70 struct _Bit_reference 71 { 72 _Bit_type * _M_p; 73 _Bit_type _M_mask; 74 75 _Bit_reference(_Bit_type * __x, _Bit_type __y) 76 : _M_p(__x), _M_mask(__y) { } 77 78 _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { } 79 80 operator bool() const _GLIBCXX_NOEXCEPT 81 { return !!(*_M_p & _M_mask); } 82 83 _Bit_reference& 84 operator=(bool __x) _GLIBCXX_NOEXCEPT 85 { 86 if (__x) 87 *_M_p |= _M_mask; 88 else 89 *_M_p &= ~_M_mask; 90 return *this; 91 } 92 93 _Bit_reference& 94 operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT 95 { return *this = bool(__x); } 96 97 bool 98 operator==(const _Bit_reference& __x) const 99 { return bool(*this) == bool(__x); } 100 101 bool 102 operator<(const _Bit_reference& __x) const 103 { return !bool(*this) && bool(__x); } 104 105 void 106 flip() _GLIBCXX_NOEXCEPT 107 { *_M_p ^= _M_mask; } 108 }; 109 110 #if __cplusplus >= 201103L 111 inline void 112 swap(_Bit_reference __x, _Bit_reference __y) noexcept 113 { 114 bool __tmp = __x; 115 __x = __y; 116 __y = __tmp; 117 } 118 119 inline void 120 swap(_Bit_reference __x, bool& __y) noexcept 121 { 122 bool __tmp = __x; 123 __x = __y; 124 __y = __tmp; 125 } 126 127 inline void 128 swap(bool& __x, _Bit_reference __y) noexcept 129 { 130 bool __tmp = __x; 131 __x = __y; 132 __y = __tmp; 133 } 134 #endif 135 136 struct _Bit_iterator_base 137 : public std::iterator<std::random_access_iterator_tag, bool> 138 { 139 _Bit_type * _M_p; 140 unsigned int _M_offset; 141 142 _Bit_iterator_base(_Bit_type * __x, unsigned int __y) 143 : _M_p(__x), _M_offset(__y) { } 144 145 void 146 _M_bump_up() 147 { 148 if (_M_offset++ == int(_S_word_bit) - 1) 149 { 150 _M_offset = 0; 151 ++_M_p; 152 } 153 } 154 155 void 156 _M_bump_down() 157 { 158 if (_M_offset-- == 0) 159 { 160 _M_offset = int(_S_word_bit) - 1; 161 --_M_p; 162 } 163 } 164 165 void 166 _M_incr(ptrdiff_t __i) 167 { 168 difference_type __n = __i + _M_offset; 169 _M_p += __n / int(_S_word_bit); 170 __n = __n % int(_S_word_bit); 171 if (__n < 0) 172 { 173 __n += int(_S_word_bit); 174 --_M_p; 175 } 176 _M_offset = static_cast<unsigned int>(__n); 177 } 178 179 bool 180 operator==(const _Bit_iterator_base& __i) const 181 { return _M_p == __i._M_p && _M_offset == __i._M_offset; } 182 183 bool 184 operator<(const _Bit_iterator_base& __i) const 185 { 186 return _M_p < __i._M_p 187 || (_M_p == __i._M_p && _M_offset < __i._M_offset); 188 } 189 190 bool 191 operator!=(const _Bit_iterator_base& __i) const 192 { return !(*this == __i); } 193 194 bool 195 operator>(const _Bit_iterator_base& __i) const 196 { return __i < *this; } 197 198 bool 199 operator<=(const _Bit_iterator_base& __i) const 200 { return !(__i < *this); } 201 202 bool 203 operator>=(const _Bit_iterator_base& __i) const 204 { return !(*this < __i); } 205 }; 206 207 inline ptrdiff_t 208 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) 209 { 210 return (int(_S_word_bit) * (__x._M_p - __y._M_p) 211 + __x._M_offset - __y._M_offset); 212 } 213 214 struct _Bit_iterator : public _Bit_iterator_base 215 { 216 typedef _Bit_reference reference; 217 typedef _Bit_reference* pointer; 218 typedef _Bit_iterator iterator; 219 220 _Bit_iterator() : _Bit_iterator_base(0, 0) { } 221 222 _Bit_iterator(_Bit_type * __x, unsigned int __y) 223 : _Bit_iterator_base(__x, __y) { } 224 225 iterator 226 _M_const_cast() const 227 { return *this; } 228 229 reference 230 operator*() const 231 { return reference(_M_p, 1UL << _M_offset); } 232 233 iterator& 234 operator++() 235 { 236 _M_bump_up(); 237 return *this; 238 } 239 240 iterator 241 operator++(int) 242 { 243 iterator __tmp = *this; 244 _M_bump_up(); 245 return __tmp; 246 } 247 248 iterator& 249 operator--() 250 { 251 _M_bump_down(); 252 return *this; 253 } 254 255 iterator 256 operator--(int) 257 { 258 iterator __tmp = *this; 259 _M_bump_down(); 260 return __tmp; 261 } 262 263 iterator& 264 operator+=(difference_type __i) 265 { 266 _M_incr(__i); 267 return *this; 268 } 269 270 iterator& 271 operator-=(difference_type __i) 272 { 273 *this += -__i; 274 return *this; 275 } 276 277 iterator 278 operator+(difference_type __i) const 279 { 280 iterator __tmp = *this; 281 return __tmp += __i; 282 } 283 284 iterator 285 operator-(difference_type __i) const 286 { 287 iterator __tmp = *this; 288 return __tmp -= __i; 289 } 290 291 reference 292 operator[](difference_type __i) const 293 { return *(*this + __i); } 294 }; 295 296 inline _Bit_iterator 297 operator+(ptrdiff_t __n, const _Bit_iterator& __x) 298 { return __x + __n; } 299 300 struct _Bit_const_iterator : public _Bit_iterator_base 301 { 302 typedef bool reference; 303 typedef bool const_reference; 304 typedef const bool* pointer; 305 typedef _Bit_const_iterator const_iterator; 306 307 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { } 308 309 _Bit_const_iterator(_Bit_type * __x, unsigned int __y) 310 : _Bit_iterator_base(__x, __y) { } 311 312 _Bit_const_iterator(const _Bit_iterator& __x) 313 : _Bit_iterator_base(__x._M_p, __x._M_offset) { } 314 315 _Bit_iterator 316 _M_const_cast() const 317 { return _Bit_iterator(_M_p, _M_offset); } 318 319 const_reference 320 operator*() const 321 { return _Bit_reference(_M_p, 1UL << _M_offset); } 322 323 const_iterator& 324 operator++() 325 { 326 _M_bump_up(); 327 return *this; 328 } 329 330 const_iterator 331 operator++(int) 332 { 333 const_iterator __tmp = *this; 334 _M_bump_up(); 335 return __tmp; 336 } 337 338 const_iterator& 339 operator--() 340 { 341 _M_bump_down(); 342 return *this; 343 } 344 345 const_iterator 346 operator--(int) 347 { 348 const_iterator __tmp = *this; 349 _M_bump_down(); 350 return __tmp; 351 } 352 353 const_iterator& 354 operator+=(difference_type __i) 355 { 356 _M_incr(__i); 357 return *this; 358 } 359 360 const_iterator& 361 operator-=(difference_type __i) 362 { 363 *this += -__i; 364 return *this; 365 } 366 367 const_iterator 368 operator+(difference_type __i) const 369 { 370 const_iterator __tmp = *this; 371 return __tmp += __i; 372 } 373 374 const_iterator 375 operator-(difference_type __i) const 376 { 377 const_iterator __tmp = *this; 378 return __tmp -= __i; 379 } 380 381 const_reference 382 operator[](difference_type __i) const 383 { return *(*this + __i); } 384 }; 385 386 inline _Bit_const_iterator 387 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) 388 { return __x + __n; } 389 390 inline void 391 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last, bool __x) 392 { 393 for (; __first != __last; ++__first) 394 *__first = __x; 395 } 396 397 inline void 398 fill(_Bit_iterator __first, _Bit_iterator __last, const bool& __x) 399 { 400 if (__first._M_p != __last._M_p) 401 { 402 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0); 403 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x); 404 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x); 405 } 406 else 407 __fill_bvector(__first, __last, __x); 408 } 409 410 template<typename _Alloc> 411 struct _Bvector_base 412 { 413 typedef typename _Alloc::template rebind<_Bit_type>::other 414 _Bit_alloc_type; 415 416 struct _Bvector_impl 417 : public _Bit_alloc_type 418 { 419 _Bit_iterator _M_start; 420 _Bit_iterator _M_finish; 421 _Bit_type* _M_end_of_storage; 422 423 _Bvector_impl() 424 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0) 425 { } 426 427 _Bvector_impl(const _Bit_alloc_type& __a) 428 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0) 429 { } 430 431 #if __cplusplus >= 201103L 432 _Bvector_impl(_Bit_alloc_type&& __a) 433 : _Bit_alloc_type(std::move(__a)), _M_start(), _M_finish(), 434 _M_end_of_storage(0) 435 { } 436 #endif 437 }; 438 439 public: 440 typedef _Alloc allocator_type; 441 442 _Bit_alloc_type& 443 _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT 444 { return *static_cast<_Bit_alloc_type*>(&this->_M_impl); } 445 446 const _Bit_alloc_type& 447 _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT 448 { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); } 449 450 allocator_type 451 get_allocator() const _GLIBCXX_NOEXCEPT 452 { return allocator_type(_M_get_Bit_allocator()); } 453 454 _Bvector_base() 455 : _M_impl() { } 456 457 _Bvector_base(const allocator_type& __a) 458 : _M_impl(__a) { } 459 460 #if __cplusplus >= 201103L 461 _Bvector_base(_Bvector_base&& __x) noexcept 462 : _M_impl(std::move(__x._M_get_Bit_allocator())) 463 { 464 this->_M_impl._M_start = __x._M_impl._M_start; 465 this->_M_impl._M_finish = __x._M_impl._M_finish; 466 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage; 467 __x._M_impl._M_start = _Bit_iterator(); 468 __x._M_impl._M_finish = _Bit_iterator(); 469 __x._M_impl._M_end_of_storage = 0; 470 } 471 #endif 472 473 ~_Bvector_base() 474 { 475 this->_M_deallocate(); 476 #if __google_stl_debug_bvector 477 __builtin_memset(this, 0xcd, sizeof(*this)); 478 #endif 479 } 480 481 protected: 482 _Bvector_impl _M_impl; 483 484 #if __google_stl_debug_bvector 485 bool _M_is_valid() const 486 { 487 return (this->_M_impl._M_start._M_p == 0 488 && this->_M_impl._M_finish._M_p == 0 489 && this->_M_impl._M_end_of_storage == 0) 490 || (this->_M_impl._M_start._M_p <= this->_M_impl._M_finish._M_p 491 && this->_M_impl._M_finish._M_p <= this->_M_impl._M_end_of_storage 492 && (this->_M_impl._M_start._M_p < this->_M_impl._M_end_of_storage 493 || (this->_M_impl._M_start._M_p == this->_M_impl._M_end_of_storage 494 && this->_M_impl._M_start._M_offset == 0 495 && this->_M_impl._M_finish._M_offset == 0))); 496 } 497 #endif 498 499 _Bit_type* 500 _M_allocate(size_t __n) 501 { return _M_impl.allocate(_S_nword(__n)); } 502 503 void 504 _M_deallocate() 505 { 506 if (_M_impl._M_start._M_p) 507 _M_impl.deallocate(_M_impl._M_start._M_p, 508 _M_impl._M_end_of_storage - _M_impl._M_start._M_p); 509 } 510 511 static size_t 512 _S_nword(size_t __n) 513 { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); } 514 }; 515 516 _GLIBCXX_END_NAMESPACE_CONTAINER 517 } // namespace std 518 519 // Declare a partial specialization of vector<T, Alloc>. 520 #include <bits/stl_vector.h> 521 522 namespace std _GLIBCXX_VISIBILITY(default) 523 { 524 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 525 526 /** 527 * @brief A specialization of vector for booleans which offers fixed time 528 * access to individual elements in any order. 529 * 530 * @ingroup sequences 531 * 532 * @tparam _Alloc Allocator type. 533 * 534 * Note that vector<bool> does not actually meet the requirements for being 535 * a container. This is because the reference and pointer types are not 536 * really references and pointers to bool. See DR96 for details. @see 537 * vector for function documentation. 538 * 539 * In some terminology a %vector can be described as a dynamic 540 * C-style array, it offers fast and efficient access to individual 541 * elements in any order and saves the user from worrying about 542 * memory and size allocation. Subscripting ( @c [] ) access is 543 * also provided as with C-style arrays. 544 */ 545 template<typename _Alloc> 546 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc> 547 { 548 typedef _Bvector_base<_Alloc> _Base; 549 550 #if __cplusplus >= 201103L 551 template<typename> friend struct hash; 552 #endif 553 554 public: 555 typedef bool value_type; 556 typedef size_t size_type; 557 typedef ptrdiff_t difference_type; 558 typedef _Bit_reference reference; 559 typedef bool const_reference; 560 typedef _Bit_reference* pointer; 561 typedef const bool* const_pointer; 562 typedef _Bit_iterator iterator; 563 typedef _Bit_const_iterator const_iterator; 564 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 565 typedef std::reverse_iterator<iterator> reverse_iterator; 566 typedef _Alloc allocator_type; 567 568 allocator_type get_allocator() const 569 { return _Base::get_allocator(); } 570 571 protected: 572 using _Base::_M_allocate; 573 using _Base::_M_deallocate; 574 using _Base::_S_nword; 575 using _Base::_M_get_Bit_allocator; 576 577 public: 578 vector() 579 : _Base() { } 580 581 explicit 582 vector(const allocator_type& __a) 583 : _Base(__a) { } 584 585 #if __cplusplus >= 201103L 586 explicit 587 vector(size_type __n, const allocator_type& __a = allocator_type()) 588 : vector(__n, false, __a) 589 { } 590 591 vector(size_type __n, const bool& __value, 592 const allocator_type& __a = allocator_type()) 593 : _Base(__a) 594 { 595 _M_initialize(__n); 596 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage, 597 __value ? ~0 : 0); 598 } 599 #else 600 explicit 601 vector(size_type __n, const bool& __value = bool(), 602 const allocator_type& __a = allocator_type()) 603 : _Base(__a) 604 { 605 _M_initialize(__n); 606 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage, 607 __value ? ~0 : 0); 608 } 609 #endif 610 611 vector(const vector& __x) 612 : _Base(__x._M_get_Bit_allocator()) 613 { 614 _M_initialize(__x.size()); 615 _M_copy_aligned(__x.begin(), __x.end(), this->_M_impl._M_start); 616 } 617 618 #if __cplusplus >= 201103L 619 vector(vector&& __x) noexcept 620 : _Base(std::move(__x)) { } 621 622 vector(initializer_list<bool> __l, 623 const allocator_type& __a = allocator_type()) 624 : _Base(__a) 625 { 626 _M_initialize_range(__l.begin(), __l.end(), 627 random_access_iterator_tag()); 628 } 629 #endif 630 631 #if __cplusplus >= 201103L 632 template<typename _InputIterator, 633 typename = std::_RequireInputIter<_InputIterator>> 634 vector(_InputIterator __first, _InputIterator __last, 635 const allocator_type& __a = allocator_type()) 636 : _Base(__a) 637 { _M_initialize_dispatch(__first, __last, __false_type()); } 638 #else 639 template<typename _InputIterator> 640 vector(_InputIterator __first, _InputIterator __last, 641 const allocator_type& __a = allocator_type()) 642 : _Base(__a) 643 { 644 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 645 _M_initialize_dispatch(__first, __last, _Integral()); 646 } 647 #endif 648 649 ~vector() _GLIBCXX_NOEXCEPT { } 650 651 vector& 652 operator=(const vector& __x) 653 { 654 #if __google_stl_debug_bvector 655 if (!this->_M_is_valid()) 656 __throw_logic_error("op=() on corrupt (dangling?) vector"); 657 #endif 658 if (&__x == this) 659 return *this; 660 if (__x.size() > capacity()) 661 { 662 this->_M_deallocate(); 663 _M_initialize(__x.size()); 664 } 665 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(), 666 begin()); 667 return *this; 668 } 669 670 #if __cplusplus >= 201103L 671 vector& 672 operator=(vector&& __x) 673 { 674 #if __google_stl_debug_bvector 675 if (!this->_M_is_valid()) 676 __throw_logic_error("op=() on corrupt (dangling?) vector"); 677 #endif 678 // NB: DR 1204. 679 // NB: DR 675. 680 this->clear(); 681 this->swap(__x); 682 return *this; 683 } 684 685 vector& 686 operator=(initializer_list<bool> __l) 687 { 688 this->assign (__l.begin(), __l.end()); 689 return *this; 690 } 691 #endif 692 693 // assign(), a generalized assignment member function. Two 694 // versions: one that takes a count, and one that takes a range. 695 // The range version is a member template, so we dispatch on whether 696 // or not the type is an integer. 697 void 698 assign(size_type __n, const bool& __x) 699 { 700 #if __google_stl_debug_bvector 701 if (!this->_M_is_valid()) 702 __throw_logic_error("assign() on corrupt (dangling?) vector"); 703 #endif 704 _M_fill_assign(__n, __x); 705 } 706 707 #if __cplusplus >= 201103L 708 template<typename _InputIterator, 709 typename = std::_RequireInputIter<_InputIterator>> 710 void 711 assign(_InputIterator __first, _InputIterator __last) 712 { 713 #if __google_stl_debug_bvector 714 if (!this->_M_is_valid()) 715 __throw_logic_error("assign() on corrupt (dangling?) vector"); 716 #endif 717 _M_assign_dispatch(__first, __last, __false_type()); 718 } 719 #else 720 template<typename _InputIterator> 721 void 722 assign(_InputIterator __first, _InputIterator __last) 723 { 724 #if __google_stl_debug_bvector 725 if (!this->_M_is_valid()) 726 __throw_logic_error("assign() on corrupt (dangling?) vector"); 727 #endif 728 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 729 _M_assign_dispatch(__first, __last, _Integral()); 730 } 731 #endif 732 733 #if __cplusplus >= 201103L 734 void 735 assign(initializer_list<bool> __l) 736 { this->assign(__l.begin(), __l.end()); } 737 #endif 738 739 iterator 740 begin() _GLIBCXX_NOEXCEPT 741 { 742 #if __google_stl_debug_bvector 743 if (!this->_M_is_valid()) 744 __throw_logic_error("begin() on corrupt (dangling?) vector"); 745 #endif 746 return this->_M_impl._M_start; 747 } 748 749 const_iterator 750 begin() const _GLIBCXX_NOEXCEPT 751 { 752 #if __google_stl_debug_bvector 753 if (!this->_M_is_valid()) 754 __throw_logic_error("begin() on corrupt (dangling?) vector"); 755 #endif 756 return this->_M_impl._M_start; 757 } 758 759 iterator 760 end() _GLIBCXX_NOEXCEPT 761 { 762 #if __google_stl_debug_bvector 763 if (!this->_M_is_valid()) 764 __throw_logic_error("end() on corrupt (dangling?) vector"); 765 #endif 766 return this->_M_impl._M_finish; 767 } 768 769 const_iterator 770 end() const _GLIBCXX_NOEXCEPT 771 { 772 #if __google_stl_debug_bvector 773 if (!this->_M_is_valid()) 774 __throw_logic_error("end() on corrupt (dangling?) vector"); 775 #endif 776 return this->_M_impl._M_finish; 777 } 778 779 reverse_iterator 780 rbegin() _GLIBCXX_NOEXCEPT 781 { return reverse_iterator(end()); } 782 783 const_reverse_iterator 784 rbegin() const _GLIBCXX_NOEXCEPT 785 { return const_reverse_iterator(end()); } 786 787 reverse_iterator 788 rend() _GLIBCXX_NOEXCEPT 789 { return reverse_iterator(begin()); } 790 791 const_reverse_iterator 792 rend() const _GLIBCXX_NOEXCEPT 793 { return const_reverse_iterator(begin()); } 794 795 #if __cplusplus >= 201103L 796 const_iterator 797 cbegin() const noexcept 798 { 799 #if __google_stl_debug_bvector 800 if (!this->_M_is_valid()) 801 __throw_logic_error("cbegin() on corrupt (dangling?) vector"); 802 #endif 803 return this->_M_impl._M_start; 804 } 805 806 const_iterator 807 cend() const noexcept 808 { 809 #if __google_stl_debug_bvector 810 if (!this->_M_is_valid()) 811 __throw_logic_error("cend() on corrupt (dangling?) vector"); 812 #endif 813 return this->_M_impl._M_finish; 814 } 815 816 const_reverse_iterator 817 crbegin() const noexcept 818 { return const_reverse_iterator(end()); } 819 820 const_reverse_iterator 821 crend() const noexcept 822 { return const_reverse_iterator(begin()); } 823 #endif 824 825 size_type 826 size() const _GLIBCXX_NOEXCEPT 827 { return size_type(end() - begin()); } 828 829 size_type 830 max_size() const _GLIBCXX_NOEXCEPT 831 { 832 #if __google_stl_debug_bvector 833 if (!this->_M_is_valid()) 834 __throw_logic_error("max_size() on corrupt (dangling?) vector"); 835 #endif 836 const size_type __isize = 837 __gnu_cxx::__numeric_traits<difference_type>::__max 838 - int(_S_word_bit) + 1; 839 const size_type __asize = _M_get_Bit_allocator().max_size(); 840 return (__asize <= __isize / int(_S_word_bit) 841 ? __asize * int(_S_word_bit) : __isize); 842 } 843 844 size_type 845 capacity() const _GLIBCXX_NOEXCEPT 846 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0) 847 - begin()); } 848 849 bool 850 empty() const _GLIBCXX_NOEXCEPT 851 { return begin() == end(); } 852 853 reference 854 operator[](size_type __n) 855 { 856 #if __google_stl_debug_bvector 857 if (!this->_M_is_valid()) 858 __throw_logic_error("operator[] on corrupt (dangling?) vector"); 859 _M_range_check(__n); 860 #endif 861 return *iterator(this->_M_impl._M_start._M_p 862 + __n / int(_S_word_bit), __n % int(_S_word_bit)); 863 } 864 865 const_reference 866 operator[](size_type __n) const 867 { 868 #if __google_stl_debug_bvector 869 if (!this->_M_is_valid()) 870 __throw_logic_error("operator[] on corrupt (dangling?) vector"); 871 _M_range_check(__n); 872 #endif 873 return *const_iterator(this->_M_impl._M_start._M_p 874 + __n / int(_S_word_bit), __n % int(_S_word_bit)); 875 } 876 877 protected: 878 void 879 _M_range_check(size_type __n) const 880 { 881 if (__n >= this->size()) 882 __throw_out_of_range_fmt(__N("vector<bool>::_M_range_check: __n " 883 "(which is %zu) >= this->size() " 884 "(which is %zu)"), 885 __n, this->size()); 886 } 887 888 public: 889 reference 890 at(size_type __n) 891 { 892 #if __google_stl_debug_bvector 893 if (!this->_M_is_valid()) 894 __throw_logic_error("at() on corrupt (dangling?) vector"); 895 #endif 896 _M_range_check(__n); return (*this)[__n]; } 897 898 const_reference 899 at(size_type __n) const 900 { 901 #if __google_stl_debug_bvector 902 if (!this->_M_is_valid()) 903 __throw_logic_error("at() on corrupt (dangling?) vector"); 904 #endif 905 _M_range_check(__n); return (*this)[__n]; } 906 907 void 908 reserve(size_type __n) 909 { 910 if (__n > max_size()) 911 __throw_length_error(__N("vector::reserve")); 912 if (capacity() < __n) 913 _M_reallocate(__n); 914 } 915 916 reference 917 front() 918 { 919 #if __google_stl_debug_bvector 920 if (!this->_M_is_valid()) 921 __throw_logic_error("front() on corrupt (dangling?) vector"); 922 _M_range_check(0); 923 #endif 924 return *begin(); 925 } 926 927 const_reference 928 front() const 929 { 930 #if __google_stl_debug_bvector 931 if (!this->_M_is_valid()) 932 __throw_logic_error("front() on corrupt (dangling?) vector"); 933 _M_range_check(0); 934 #endif 935 return *begin(); 936 } 937 938 reference 939 back() 940 { 941 #if __google_stl_debug_bvector 942 if (!this->_M_is_valid()) 943 __throw_logic_error("back() on corrupt (dangling?) vector"); 944 _M_range_check(0); 945 #endif 946 return *(end() - 1); 947 } 948 949 const_reference 950 back() const 951 { 952 #if __google_stl_debug_bvector 953 if (!this->_M_is_valid()) 954 __throw_logic_error("back() on corrupt (dangling?) vector"); 955 _M_range_check(0); 956 #endif 957 return *(end() - 1); 958 } 959 960 // _GLIBCXX_RESOLVE_LIB_DEFECTS 961 // DR 464. Suggestion for new member functions in standard containers. 962 // N.B. DR 464 says nothing about vector<bool> but we need something 963 // here due to the way we are implementing DR 464 in the debug-mode 964 // vector class. 965 void 966 data() _GLIBCXX_NOEXCEPT { } 967 968 void 969 push_back(bool __x) 970 { 971 #if __google_stl_debug_bvector 972 if (!this->_M_is_valid()) 973 __throw_logic_error("push_back() on corrupt (dangling?) vector"); 974 #endif 975 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage) 976 *this->_M_impl._M_finish++ = __x; 977 else 978 _M_insert_aux(end(), __x); 979 } 980 981 void 982 swap(vector& __x) 983 { 984 #if __google_stl_debug_bvector 985 if (!this->_M_is_valid() || !__x._M_is_valid()) 986 __throw_logic_error("swap() on corrupt (dangling?) vector"); 987 #endif 988 std::swap(this->_M_impl._M_start, __x._M_impl._M_start); 989 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish); 990 std::swap(this->_M_impl._M_end_of_storage, 991 __x._M_impl._M_end_of_storage); 992 993 // _GLIBCXX_RESOLVE_LIB_DEFECTS 994 // 431. Swapping containers with unequal allocators. 995 std::__alloc_swap<typename _Base::_Bit_alloc_type>:: 996 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator()); 997 } 998 999 // [23.2.5]/1, third-to-last entry in synopsis listing 1000 static void 1001 swap(reference __x, reference __y) _GLIBCXX_NOEXCEPT 1002 { 1003 bool __tmp = __x; 1004 __x = __y; 1005 __y = __tmp; 1006 } 1007 1008 iterator 1009 #if __cplusplus >= 201103L 1010 insert(const_iterator __position, const bool& __x = bool()) 1011 #else 1012 insert(iterator __position, const bool& __x = bool()) 1013 #endif 1014 { 1015 #if __google_stl_debug_bvector 1016 if (!this->_M_is_valid()) 1017 __throw_logic_error("insert() on corrupt (dangling?) vector"); 1018 if (__position < this->begin() || __position > this->end()) 1019 __throw_logic_error("insert() at invalid position"); 1020 #endif 1021 const difference_type __n = __position - begin(); 1022 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage 1023 && __position == end()) 1024 *this->_M_impl._M_finish++ = __x; 1025 else 1026 _M_insert_aux(__position._M_const_cast(), __x); 1027 return begin() + __n; 1028 } 1029 1030 #if __cplusplus >= 201103L 1031 template<typename _InputIterator, 1032 typename = std::_RequireInputIter<_InputIterator>> 1033 iterator 1034 insert(const_iterator __position, 1035 _InputIterator __first, _InputIterator __last) 1036 { 1037 #if __google_stl_debug_bvector 1038 if (!this->_M_is_valid()) 1039 __throw_logic_error("insert() on corrupt (dangling?) vector"); 1040 if (__position < this->begin() || __position > this->end()) 1041 __throw_logic_error("insert() at invalid position"); 1042 #endif 1043 difference_type __offset = __position - cbegin(); 1044 _M_insert_dispatch(__position._M_const_cast(), 1045 __first, __last, __false_type()); 1046 return begin() + __offset; 1047 } 1048 #else 1049 template<typename _InputIterator> 1050 void 1051 insert(iterator __position, 1052 _InputIterator __first, _InputIterator __last) 1053 { 1054 #if __google_stl_debug_bvector 1055 if (!this->_M_is_valid()) 1056 __throw_logic_error("insert() on corrupt (dangling?) vector"); 1057 if (__position < this->begin() || __position > this->end()) 1058 __throw_logic_error("insert() at invalid position"); 1059 #endif 1060 typedef typename std::__is_integer<_InputIterator>::__type _Integral; 1061 _M_insert_dispatch(__position, __first, __last, _Integral()); 1062 } 1063 #endif 1064 1065 #if __cplusplus >= 201103L 1066 iterator 1067 insert(const_iterator __position, size_type __n, const bool& __x) 1068 { 1069 #if __google_stl_debug_bvector 1070 if (!this->_M_is_valid()) 1071 __throw_logic_error("insert() on corrupt (dangling?) vector"); 1072 if (__position < this->begin() || __position > this->end()) 1073 __throw_logic_error("insert() at invalid position"); 1074 #endif 1075 difference_type __offset = __position - cbegin(); 1076 _M_fill_insert(__position._M_const_cast(), __n, __x); 1077 return begin() + __offset; 1078 } 1079 #else 1080 void 1081 insert(iterator __position, size_type __n, const bool& __x) 1082 { 1083 #if __google_stl_debug_bvector 1084 if (!this->_M_is_valid()) 1085 __throw_logic_error("insert() on corrupt (dangling?) vector"); 1086 if (__position < this->begin() || __position > this->end()) 1087 __throw_logic_error("insert() at invalid position"); 1088 #endif 1089 _M_fill_insert(__position, __n, __x); 1090 } 1091 #endif 1092 1093 #if __cplusplus >= 201103L 1094 iterator 1095 insert(const_iterator __p, initializer_list<bool> __l) 1096 { return this->insert(__p, __l.begin(), __l.end()); } 1097 #endif 1098 1099 void 1100 pop_back() 1101 { 1102 #if __google_stl_debug_bvector 1103 if (!this->_M_is_valid()) 1104 __throw_logic_error("pop_back() on corrupt (dangling?) vector"); 1105 _M_range_check(0); 1106 #endif 1107 --this->_M_impl._M_finish; 1108 } 1109 1110 iterator 1111 #if __cplusplus >= 201103L 1112 erase(const_iterator __position) 1113 #else 1114 erase(iterator __position) 1115 #endif 1116 { 1117 #if __google_stl_debug_bvector 1118 if (!this->_M_is_valid()) 1119 __throw_logic_error("erase() on corrupt (dangling?) vector"); 1120 if (__position < this->begin() || __position >= this->end()) 1121 __throw_logic_error("erase() at invalid position"); 1122 #endif 1123 return _M_erase(__position._M_const_cast()); 1124 } 1125 1126 iterator 1127 #if __cplusplus >= 201103L 1128 erase(const_iterator __first, const_iterator __last) 1129 #else 1130 erase(iterator __first, iterator __last) 1131 #endif 1132 { 1133 #if __google_stl_debug_bvector 1134 if (!this->_M_is_valid()) 1135 __throw_logic_error("erase() on corrupt (dangling?) vector"); 1136 if (__first < this->begin() || __first > __last || __last > this->end()) 1137 __throw_logic_error("erase() invalid range"); 1138 #endif 1139 return _M_erase(__first._M_const_cast(), __last._M_const_cast()); 1140 } 1141 1142 void 1143 resize(size_type __new_size, bool __x = bool()) 1144 { 1145 if (__new_size < size()) 1146 _M_erase_at_end(begin() + difference_type(__new_size)); 1147 else 1148 insert(end(), __new_size - size(), __x); 1149 } 1150 1151 #if __cplusplus >= 201103L 1152 void 1153 shrink_to_fit() 1154 { 1155 #if __google_stl_debug_bvector 1156 if (!this->_M_is_valid()) 1157 __throw_logic_error("shrink_to_fit() on corrupt (dangling?) vector"); 1158 #endif 1159 _M_shrink_to_fit(); 1160 } 1161 #endif 1162 1163 void 1164 flip() _GLIBCXX_NOEXCEPT 1165 { 1166 #if __google_stl_debug_bvector 1167 if (!this->_M_is_valid()) 1168 __throw_logic_error("flip() on corrupt (dangling?) vector"); 1169 #endif 1170 for (_Bit_type * __p = this->_M_impl._M_start._M_p; 1171 __p != this->_M_impl._M_end_of_storage; ++__p) 1172 *__p = ~*__p; 1173 } 1174 1175 void 1176 clear() _GLIBCXX_NOEXCEPT 1177 { _M_erase_at_end(begin()); } 1178 1179 #if __cplusplus >= 201103L 1180 template<typename... _Args> 1181 void 1182 emplace_back(_Args&&... __args) 1183 { push_back(bool(__args...)); } 1184 1185 template<typename... _Args> 1186 iterator 1187 emplace(const_iterator __pos, _Args&&... __args) 1188 { return insert(__pos, bool(__args...)); } 1189 #endif 1190 1191 protected: 1192 // Precondition: __first._M_offset == 0 && __result._M_offset == 0. 1193 iterator 1194 _M_copy_aligned(const_iterator __first, const_iterator __last, 1195 iterator __result) 1196 { 1197 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p); 1198 return std::copy(const_iterator(__last._M_p, 0), __last, 1199 iterator(__q, 0)); 1200 } 1201 1202 void 1203 _M_initialize(size_type __n) 1204 { 1205 _Bit_type* __q = this->_M_allocate(__n); 1206 this->_M_impl._M_end_of_storage = __q + _S_nword(__n); 1207 this->_M_impl._M_start = iterator(__q, 0); 1208 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n); 1209 } 1210 1211 void 1212 _M_reallocate(size_type __n); 1213 1214 #if __cplusplus >= 201103L 1215 bool 1216 _M_shrink_to_fit(); 1217 #endif 1218 1219 // Check whether it's an integral type. If so, it's not an iterator. 1220 1221 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1222 // 438. Ambiguity in the "do the right thing" clause 1223 template<typename _Integer> 1224 void 1225 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 1226 { 1227 _M_initialize(static_cast<size_type>(__n)); 1228 std::fill(this->_M_impl._M_start._M_p, 1229 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 1230 } 1231 1232 template<typename _InputIterator> 1233 void 1234 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 1235 __false_type) 1236 { _M_initialize_range(__first, __last, 1237 std::__iterator_category(__first)); } 1238 1239 template<typename _InputIterator> 1240 void 1241 _M_initialize_range(_InputIterator __first, _InputIterator __last, 1242 std::input_iterator_tag) 1243 { 1244 for (; __first != __last; ++__first) 1245 push_back(*__first); 1246 } 1247 1248 template<typename _ForwardIterator> 1249 void 1250 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last, 1251 std::forward_iterator_tag) 1252 { 1253 const size_type __n = std::distance(__first, __last); 1254 _M_initialize(__n); 1255 std::copy(__first, __last, this->_M_impl._M_start); 1256 } 1257 1258 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1259 // 438. Ambiguity in the "do the right thing" clause 1260 template<typename _Integer> 1261 void 1262 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 1263 { _M_fill_assign(__n, __val); } 1264 1265 template<class _InputIterator> 1266 void 1267 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 1268 __false_type) 1269 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); } 1270 1271 void 1272 _M_fill_assign(size_t __n, bool __x) 1273 { 1274 if (__n > size()) 1275 { 1276 std::fill(this->_M_impl._M_start._M_p, 1277 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 1278 insert(end(), __n - size(), __x); 1279 } 1280 else 1281 { 1282 _M_erase_at_end(begin() + __n); 1283 std::fill(this->_M_impl._M_start._M_p, 1284 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 1285 } 1286 } 1287 1288 template<typename _InputIterator> 1289 void 1290 _M_assign_aux(_InputIterator __first, _InputIterator __last, 1291 std::input_iterator_tag) 1292 { 1293 iterator __cur = begin(); 1294 for (; __first != __last && __cur != end(); ++__cur, ++__first) 1295 *__cur = *__first; 1296 if (__first == __last) 1297 _M_erase_at_end(__cur); 1298 else 1299 insert(end(), __first, __last); 1300 } 1301 1302 template<typename _ForwardIterator> 1303 void 1304 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 1305 std::forward_iterator_tag) 1306 { 1307 const size_type __len = std::distance(__first, __last); 1308 if (__len < size()) 1309 _M_erase_at_end(std::copy(__first, __last, begin())); 1310 else 1311 { 1312 _ForwardIterator __mid = __first; 1313 std::advance(__mid, size()); 1314 std::copy(__first, __mid, begin()); 1315 insert(end(), __mid, __last); 1316 } 1317 } 1318 1319 // Check whether it's an integral type. If so, it's not an iterator. 1320 1321 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1322 // 438. Ambiguity in the "do the right thing" clause 1323 template<typename _Integer> 1324 void 1325 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, 1326 __true_type) 1327 { _M_fill_insert(__pos, __n, __x); } 1328 1329 template<typename _InputIterator> 1330 void 1331 _M_insert_dispatch(iterator __pos, 1332 _InputIterator __first, _InputIterator __last, 1333 __false_type) 1334 { _M_insert_range(__pos, __first, __last, 1335 std::__iterator_category(__first)); } 1336 1337 void 1338 _M_fill_insert(iterator __position, size_type __n, bool __x); 1339 1340 template<typename _InputIterator> 1341 void 1342 _M_insert_range(iterator __pos, _InputIterator __first, 1343 _InputIterator __last, std::input_iterator_tag) 1344 { 1345 for (; __first != __last; ++__first) 1346 { 1347 __pos = insert(__pos, *__first); 1348 ++__pos; 1349 } 1350 } 1351 1352 template<typename _ForwardIterator> 1353 void 1354 _M_insert_range(iterator __position, _ForwardIterator __first, 1355 _ForwardIterator __last, std::forward_iterator_tag); 1356 1357 void 1358 _M_insert_aux(iterator __position, bool __x); 1359 1360 size_type 1361 _M_check_len(size_type __n, const char* __s) const 1362 { 1363 if (max_size() - size() < __n) 1364 __throw_length_error(__N(__s)); 1365 1366 const size_type __len = size() + std::max(size(), __n); 1367 return (__len < size() || __len > max_size()) ? max_size() : __len; 1368 } 1369 1370 void 1371 _M_erase_at_end(iterator __pos) 1372 { this->_M_impl._M_finish = __pos; } 1373 1374 iterator 1375 _M_erase(iterator __pos); 1376 1377 iterator 1378 _M_erase(iterator __first, iterator __last); 1379 }; 1380 1381 _GLIBCXX_END_NAMESPACE_CONTAINER 1382 } // namespace std 1383 1384 #if __cplusplus >= 201103L 1385 1386 #include <bits/functional_hash.h> 1387 1388 namespace std _GLIBCXX_VISIBILITY(default) 1389 { 1390 _GLIBCXX_BEGIN_NAMESPACE_VERSION 1391 1392 // DR 1182. 1393 /// std::hash specialization for vector<bool>. 1394 template<typename _Alloc> 1395 struct hash<_GLIBCXX_STD_C::vector<bool, _Alloc>> 1396 : public __hash_base<size_t, _GLIBCXX_STD_C::vector<bool, _Alloc>> 1397 { 1398 size_t 1399 operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>&) const noexcept; 1400 }; 1401 1402 _GLIBCXX_END_NAMESPACE_VERSION 1403 }// namespace std 1404 1405 #endif // C++11 1406 1407 #endif 1408