stl_bvector.h

Go to the documentation of this file.
00001 // vector<bool> specialization -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1996-1999 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00056 /** @file stl_bvector.h 00057 * This is an internal header file, included by other library headers. 00058 * You should not attempt to use it directly. 00059 */ 00060 00061 #ifndef _BVECTOR_H 00062 #define _BVECTOR_H 1 00063 00064 namespace _GLIBCXX_STD 00065 { 00066 typedef unsigned long _Bit_type; 00067 enum { _S_word_bit = int(CHAR_BIT * sizeof(_Bit_type)) }; 00068 00069 struct _Bit_reference 00070 { 00071 _Bit_type * _M_p; 00072 _Bit_type _M_mask; 00073 00074 _Bit_reference(_Bit_type * __x, _Bit_type __y) 00075 : _M_p(__x), _M_mask(__y) { } 00076 00077 _Bit_reference() : _M_p(0), _M_mask(0) { } 00078 00079 operator bool() const { return !!(*_M_p & _M_mask); } 00080 00081 _Bit_reference& 00082 operator=(bool __x) 00083 { 00084 if (__x) 00085 *_M_p |= _M_mask; 00086 else 00087 *_M_p &= ~_M_mask; 00088 return *this; 00089 } 00090 00091 _Bit_reference& 00092 operator=(const _Bit_reference& __x) 00093 { return *this = bool(__x); } 00094 00095 bool 00096 operator==(const _Bit_reference& __x) const 00097 { return bool(*this) == bool(__x); } 00098 00099 bool 00100 operator<(const _Bit_reference& __x) const 00101 { return !bool(*this) && bool(__x); } 00102 00103 void 00104 flip() { *_M_p ^= _M_mask; } 00105 }; 00106 00107 struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool> 00108 { 00109 _Bit_type * _M_p; 00110 unsigned int _M_offset; 00111 00112 _Bit_iterator_base(_Bit_type * __x, unsigned int __y) 00113 : _M_p(__x), _M_offset(__y) { } 00114 00115 void 00116 _M_bump_up() 00117 { 00118 if (_M_offset++ == _S_word_bit - 1) 00119 { 00120 _M_offset = 0; 00121 ++_M_p; 00122 } 00123 } 00124 00125 void 00126 _M_bump_down() 00127 { 00128 if (_M_offset-- == 0) 00129 { 00130 _M_offset = _S_word_bit - 1; 00131 --_M_p; 00132 } 00133 } 00134 00135 void 00136 _M_incr(ptrdiff_t __i) 00137 { 00138 difference_type __n = __i + _M_offset; 00139 _M_p += __n / _S_word_bit; 00140 __n = __n % _S_word_bit; 00141 if (__n < 0) 00142 { 00143 _M_offset = static_cast<unsigned int>(__n + _S_word_bit); 00144 --_M_p; 00145 } 00146 else 00147 _M_offset = static_cast<unsigned int>(__n); 00148 } 00149 00150 bool 00151 operator==(const _Bit_iterator_base& __i) const 00152 { return _M_p == __i._M_p && _M_offset == __i._M_offset; } 00153 00154 bool 00155 operator<(const _Bit_iterator_base& __i) const 00156 { 00157 return _M_p < __i._M_p 00158 || (_M_p == __i._M_p && _M_offset < __i._M_offset); 00159 } 00160 00161 bool 00162 operator!=(const _Bit_iterator_base& __i) const 00163 { return !(*this == __i); } 00164 00165 bool 00166 operator>(const _Bit_iterator_base& __i) const 00167 { return __i < *this; } 00168 00169 bool 00170 operator<=(const _Bit_iterator_base& __i) const 00171 { return !(__i < *this); } 00172 00173 bool 00174 operator>=(const _Bit_iterator_base& __i) const 00175 { return !(*this < __i); } 00176 }; 00177 00178 inline ptrdiff_t 00179 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) 00180 { 00181 return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset; 00182 } 00183 00184 struct _Bit_iterator : public _Bit_iterator_base 00185 { 00186 typedef _Bit_reference reference; 00187 typedef _Bit_reference* pointer; 00188 typedef _Bit_iterator iterator; 00189 00190 _Bit_iterator() : _Bit_iterator_base(0, 0) { } 00191 _Bit_iterator(_Bit_type * __x, unsigned int __y) 00192 : _Bit_iterator_base(__x, __y) { } 00193 00194 reference 00195 operator*() const { return reference(_M_p, 1UL << _M_offset); } 00196 00197 iterator& 00198 operator++() 00199 { 00200 _M_bump_up(); 00201 return *this; 00202 } 00203 00204 iterator 00205 operator++(int) 00206 { 00207 iterator __tmp = *this; 00208 _M_bump_up(); 00209 return __tmp; 00210 } 00211 00212 iterator& 00213 operator--() 00214 { 00215 _M_bump_down(); 00216 return *this; 00217 } 00218 00219 iterator 00220 operator--(int) 00221 { 00222 iterator __tmp = *this; 00223 _M_bump_down(); 00224 return __tmp; 00225 } 00226 00227 iterator& 00228 operator+=(difference_type __i) 00229 { 00230 _M_incr(__i); 00231 return *this; 00232 } 00233 00234 iterator& 00235 operator-=(difference_type __i) 00236 { 00237 *this += -__i; 00238 return *this; 00239 } 00240 00241 iterator 00242 operator+(difference_type __i) const 00243 { 00244 iterator __tmp = *this; 00245 return __tmp += __i; 00246 } 00247 00248 iterator 00249 operator-(difference_type __i) const 00250 { 00251 iterator __tmp = *this; 00252 return __tmp -= __i; 00253 } 00254 00255 reference 00256 operator[](difference_type __i) 00257 { return *(*this + __i); } 00258 }; 00259 00260 inline _Bit_iterator 00261 operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; } 00262 00263 00264 struct _Bit_const_iterator : public _Bit_iterator_base 00265 { 00266 typedef bool reference; 00267 typedef bool const_reference; 00268 typedef const bool* pointer; 00269 typedef _Bit_const_iterator const_iterator; 00270 00271 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { } 00272 _Bit_const_iterator(_Bit_type * __x, unsigned int __y) 00273 : _Bit_iterator_base(__x, __y) { } 00274 _Bit_const_iterator(const _Bit_iterator& __x) 00275 : _Bit_iterator_base(__x._M_p, __x._M_offset) { } 00276 00277 const_reference 00278 operator*() const 00279 { return _Bit_reference(_M_p, 1UL << _M_offset); } 00280 00281 const_iterator& 00282 operator++() 00283 { 00284 _M_bump_up(); 00285 return *this; 00286 } 00287 00288 const_iterator 00289 operator++(int) 00290 { 00291 const_iterator __tmp = *this; 00292 _M_bump_up(); 00293 return __tmp; 00294 } 00295 00296 const_iterator& 00297 operator--() 00298 { 00299 _M_bump_down(); 00300 return *this; 00301 } 00302 00303 const_iterator 00304 operator--(int) 00305 { 00306 const_iterator __tmp = *this; 00307 _M_bump_down(); 00308 return __tmp; 00309 } 00310 00311 const_iterator& 00312 operator+=(difference_type __i) 00313 { 00314 _M_incr(__i); 00315 return *this; 00316 } 00317 00318 const_iterator& 00319 operator-=(difference_type __i) 00320 { 00321 *this += -__i; 00322 return *this; 00323 } 00324 00325 const_iterator 00326 operator+(difference_type __i) const 00327 { 00328 const_iterator __tmp = *this; 00329 return __tmp += __i; 00330 } 00331 00332 const_iterator 00333 operator-(difference_type __i) const 00334 { 00335 const_iterator __tmp = *this; 00336 return __tmp -= __i; 00337 } 00338 00339 const_reference 00340 operator[](difference_type __i) 00341 { return *(*this + __i); } 00342 }; 00343 00344 inline _Bit_const_iterator 00345 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) 00346 { return __x + __n; } 00347 00348 template<class _Alloc> 00349 class _Bvector_base 00350 { 00351 typedef typename _Alloc::template rebind<_Bit_type>::other 00352 _Bit_alloc_type; 00353 00354 struct _Bvector_impl : public _Bit_alloc_type 00355 { 00356 _Bit_iterator _M_start; 00357 _Bit_iterator _M_finish; 00358 _Bit_type* _M_end_of_storage; 00359 _Bvector_impl(const _Bit_alloc_type& __a) 00360 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0) 00361 { } 00362 }; 00363 00364 public: 00365 typedef _Alloc allocator_type; 00366 00367 allocator_type 00368 get_allocator() const 00369 { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); } 00370 00371 _Bvector_base(const allocator_type& __a) : _M_impl(__a) { } 00372 00373 ~_Bvector_base() { this->_M_deallocate(); } 00374 00375 protected: 00376 _Bvector_impl _M_impl; 00377 00378 _Bit_type* 00379 _M_allocate(size_t __n) 00380 { return _M_impl.allocate((__n + _S_word_bit - 1) / _S_word_bit); } 00381 00382 void 00383 _M_deallocate() 00384 { 00385 if (_M_impl._M_start._M_p) 00386 _M_impl.deallocate(_M_impl._M_start._M_p, 00387 _M_impl._M_end_of_storage - _M_impl._M_start._M_p); 00388 } 00389 }; 00390 } // namespace std 00391 00392 // Declare a partial specialization of vector<T, Alloc>. 00393 #include <bits/stl_vector.h> 00394 00395 namespace _GLIBCXX_STD 00396 { 00397 /** 00398 * @brief A specialization of vector for booleans which offers fixed time 00399 * access to individual elements in any order. 00400 * 00401 * Note that vector<bool> does not actually meet the requirements for being 00402 * a container. This is because the reference and pointer types are not 00403 * really references and pointers to bool. See DR96 for details. @see 00404 * vector for function documentation. 00405 * 00406 * @ingroup Containers 00407 * @ingroup Sequences 00408 * 00409 * In some terminology a %vector can be described as a dynamic 00410 * C-style array, it offers fast and efficient access to individual 00411 * elements in any order and saves the user from worrying about 00412 * memory and size allocation. Subscripting ( @c [] ) access is 00413 * also provided as with C-style arrays. 00414 */ 00415 template<typename _Alloc> 00416 class vector<bool, _Alloc> : public _Bvector_base<_Alloc> 00417 { 00418 public: 00419 typedef bool value_type; 00420 typedef size_t size_type; 00421 typedef ptrdiff_t difference_type; 00422 typedef _Bit_reference reference; 00423 typedef bool const_reference; 00424 typedef _Bit_reference* pointer; 00425 typedef const bool* const_pointer; 00426 00427 typedef _Bit_iterator iterator; 00428 typedef _Bit_const_iterator const_iterator; 00429 00430 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00431 typedef std::reverse_iterator<iterator> reverse_iterator; 00432 00433 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type; 00434 00435 allocator_type get_allocator() const 00436 { return _Bvector_base<_Alloc>::get_allocator(); } 00437 00438 protected: 00439 using _Bvector_base<_Alloc>::_M_allocate; 00440 using _Bvector_base<_Alloc>::_M_deallocate; 00441 00442 protected: 00443 void _M_initialize(size_type __n) 00444 { 00445 _Bit_type* __q = this->_M_allocate(__n); 00446 this->_M_impl._M_end_of_storage = __q 00447 + (__n + _S_word_bit - 1) / _S_word_bit; 00448 this->_M_impl._M_start = iterator(__q, 0); 00449 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n); 00450 } 00451 00452 void _M_insert_aux(iterator __position, bool __x) 00453 { 00454 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage) 00455 { 00456 std::copy_backward(__position, this->_M_impl._M_finish, 00457 this->_M_impl._M_finish + 1); 00458 *__position = __x; 00459 ++this->_M_impl._M_finish; 00460 } 00461 else 00462 { 00463 const size_type __len = size() ? 2 * size() 00464 : static_cast<size_type>(_S_word_bit); 00465 _Bit_type * __q = this->_M_allocate(__len); 00466 iterator __i = std::copy(begin(), __position, iterator(__q, 0)); 00467 *__i++ = __x; 00468 this->_M_impl._M_finish = std::copy(__position, end(), __i); 00469 this->_M_deallocate(); 00470 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1) 00471 / _S_word_bit; 00472 this->_M_impl._M_start = iterator(__q, 0); 00473 } 00474 } 00475 00476 template<class _InputIterator> 00477 void _M_initialize_range(_InputIterator __first, _InputIterator __last, 00478 input_iterator_tag) 00479 { 00480 this->_M_impl._M_start = iterator(); 00481 this->_M_impl._M_finish = iterator(); 00482 this->_M_impl._M_end_of_storage = 0; 00483 for ( ; __first != __last; ++__first) 00484 push_back(*__first); 00485 } 00486 00487 template<class _ForwardIterator> 00488 void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last, 00489 forward_iterator_tag) 00490 { 00491 const size_type __n = std::distance(__first, __last); 00492 _M_initialize(__n); 00493 std::copy(__first, __last, this->_M_impl._M_start); 00494 } 00495 00496 template<class _InputIterator> 00497 void _M_insert_range(iterator __pos, _InputIterator __first, 00498 _InputIterator __last, input_iterator_tag) 00499 { 00500 for ( ; __first != __last; ++__first) 00501 { 00502 __pos = insert(__pos, *__first); 00503 ++__pos; 00504 } 00505 } 00506 00507 template<class _ForwardIterator> 00508 void _M_insert_range(iterator __position, _ForwardIterator __first, 00509 _ForwardIterator __last, forward_iterator_tag) 00510 { 00511 if (__first != __last) 00512 { 00513 size_type __n = std::distance(__first, __last); 00514 if (capacity() - size() >= __n) 00515 { 00516 std::copy_backward(__position, end(), 00517 this->_M_impl._M_finish + difference_type(__n)); 00518 std::copy(__first, __last, __position); 00519 this->_M_impl._M_finish += difference_type(__n); 00520 } 00521 else 00522 { 00523 const size_type __len = size() + std::max(size(), __n); 00524 _Bit_type * __q = this->_M_allocate(__len); 00525 iterator __i = std::copy(begin(), __position, iterator(__q, 0)); 00526 __i = std::copy(__first, __last, __i); 00527 this->_M_impl._M_finish = std::copy(__position, end(), __i); 00528 this->_M_deallocate(); 00529 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1) 00530 / _S_word_bit; 00531 this->_M_impl._M_start = iterator(__q, 0); 00532 } 00533 } 00534 } 00535 00536 public: 00537 iterator begin() 00538 { return this->_M_impl._M_start; } 00539 00540 const_iterator begin() const 00541 { return this->_M_impl._M_start; } 00542 00543 iterator end() 00544 { return this->_M_impl._M_finish; } 00545 00546 const_iterator end() const 00547 { return this->_M_impl._M_finish; } 00548 00549 reverse_iterator rbegin() 00550 { return reverse_iterator(end()); } 00551 00552 const_reverse_iterator rbegin() const 00553 { return const_reverse_iterator(end()); } 00554 00555 reverse_iterator rend() 00556 { return reverse_iterator(begin()); } 00557 00558 const_reverse_iterator rend() const 00559 { return const_reverse_iterator(begin()); } 00560 00561 size_type size() const 00562 { return size_type(end() - begin()); } 00563 00564 size_type max_size() const 00565 { return size_type(-1); } 00566 00567 size_type capacity() const 00568 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0) 00569 - begin()); } 00570 bool empty() const 00571 { return begin() == end(); } 00572 00573 reference operator[](size_type __n) 00574 { return *(begin() + difference_type(__n)); } 00575 00576 const_reference operator[](size_type __n) const 00577 { return *(begin() + difference_type(__n)); } 00578 00579 void _M_range_check(size_type __n) const 00580 { 00581 if (__n >= this->size()) 00582 __throw_out_of_range(__N("vector<bool>::_M_range_check")); 00583 } 00584 00585 reference at(size_type __n) 00586 { _M_range_check(__n); return (*this)[__n]; } 00587 00588 const_reference at(size_type __n) const 00589 { _M_range_check(__n); return (*this)[__n]; } 00590 00591 explicit vector(const allocator_type& __a = allocator_type()) 00592 : _Bvector_base<_Alloc>(__a) { } 00593 00594 vector(size_type __n, bool __value, 00595 const allocator_type& __a = allocator_type()) 00596 : _Bvector_base<_Alloc>(__a) 00597 { 00598 _M_initialize(__n); 00599 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage, 00600 __value ? ~0 : 0); 00601 } 00602 00603 explicit vector(size_type __n) 00604 : _Bvector_base<_Alloc>(allocator_type()) 00605 { 00606 _M_initialize(__n); 00607 std::fill(this->_M_impl._M_start._M_p, 00608 this->_M_impl._M_end_of_storage, 0); 00609 } 00610 00611 vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) 00612 { 00613 _M_initialize(__x.size()); 00614 std::copy(__x.begin(), __x.end(), this->_M_impl._M_start); 00615 } 00616 00617 // Check whether it's an integral type. If so, it's not an iterator. 00618 template<class _Integer> 00619 void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) 00620 { 00621 _M_initialize(__n); 00622 std::fill(this->_M_impl._M_start._M_p, 00623 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 00624 } 00625 00626 template<class _InputIterator> 00627 void 00628 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 00629 __false_type) 00630 { _M_initialize_range(__first, __last, 00631 std::__iterator_category(__first)); } 00632 00633 template<class _InputIterator> 00634 vector(_InputIterator __first, _InputIterator __last, 00635 const allocator_type& __a = allocator_type()) 00636 : _Bvector_base<_Alloc>(__a) 00637 { 00638 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00639 _M_initialize_dispatch(__first, __last, _Integral()); 00640 } 00641 00642 ~vector() { } 00643 00644 vector& operator=(const vector& __x) 00645 { 00646 if (&__x == this) 00647 return *this; 00648 if (__x.size() > capacity()) 00649 { 00650 this->_M_deallocate(); 00651 _M_initialize(__x.size()); 00652 } 00653 std::copy(__x.begin(), __x.end(), begin()); 00654 this->_M_impl._M_finish = begin() + difference_type(__x.size()); 00655 return *this; 00656 } 00657 00658 // assign(), a generalized assignment member function. Two 00659 // versions: one that takes a count, and one that takes a range. 00660 // The range version is a member template, so we dispatch on whether 00661 // or not the type is an integer. 00662 00663 void _M_fill_assign(size_t __n, bool __x) 00664 { 00665 if (__n > size()) 00666 { 00667 std::fill(this->_M_impl._M_start._M_p, 00668 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 00669 insert(end(), __n - size(), __x); 00670 } 00671 else 00672 { 00673 erase(begin() + __n, end()); 00674 std::fill(this->_M_impl._M_start._M_p, 00675 this->_M_impl._M_end_of_storage, __x ? ~0 : 0); 00676 } 00677 } 00678 00679 void assign(size_t __n, bool __x) 00680 { _M_fill_assign(__n, __x); } 00681 00682 template<class _InputIterator> 00683 void assign(_InputIterator __first, _InputIterator __last) 00684 { 00685 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00686 _M_assign_dispatch(__first, __last, _Integral()); 00687 } 00688 00689 template<class _Integer> 00690 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 00691 { _M_fill_assign((size_t) __n, (bool) __val); } 00692 00693 template<class _InputIterator> 00694 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 00695 __false_type) 00696 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); } 00697 00698 template<class _InputIterator> 00699 void _M_assign_aux(_InputIterator __first, _InputIterator __last, 00700 input_iterator_tag) 00701 { 00702 iterator __cur = begin(); 00703 for ( ; __first != __last && __cur != end(); ++__cur, ++__first) 00704 *__cur = *__first; 00705 if (__first == __last) 00706 erase(__cur, end()); 00707 else 00708 insert(end(), __first, __last); 00709 } 00710 00711 template<class _ForwardIterator> 00712 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 00713 forward_iterator_tag) 00714 { 00715 const size_type __len = std::distance(__first, __last); 00716 if (__len < size()) 00717 erase(std::copy(__first, __last, begin()), end()); 00718 else 00719 { 00720 _ForwardIterator __mid = __first; 00721 std::advance(__mid, size()); 00722 std::copy(__first, __mid, begin()); 00723 insert(end(), __mid, __last); 00724 } 00725 } 00726 00727 void reserve(size_type __n) 00728 { 00729 if (__n > this->max_size()) 00730 __throw_length_error(__N("vector::reserve")); 00731 if (this->capacity() < __n) 00732 { 00733 _Bit_type* __q = this->_M_allocate(__n); 00734 this->_M_impl._M_finish = std::copy(begin(), end(), 00735 iterator(__q, 0)); 00736 this->_M_deallocate(); 00737 this->_M_impl._M_start = iterator(__q, 0); 00738 this->_M_impl._M_end_of_storage = __q + (__n + _S_word_bit - 1) / _S_word_bit; 00739 } 00740 } 00741 00742 reference front() 00743 { return *begin(); } 00744 00745 const_reference front() const 00746 { return *begin(); } 00747 00748 reference back() 00749 { return *(end() - 1); } 00750 00751 const_reference back() const 00752 { return *(end() - 1); } 00753 00754 void push_back(bool __x) 00755 { 00756 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage) 00757 *this->_M_impl._M_finish++ = __x; 00758 else 00759 _M_insert_aux(end(), __x); 00760 } 00761 00762 void swap(vector<bool, _Alloc>& __x) 00763 { 00764 std::swap(this->_M_impl._M_start, __x._M_impl._M_start); 00765 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish); 00766 std::swap(this->_M_impl._M_end_of_storage, 00767 __x._M_impl._M_end_of_storage); 00768 } 00769 00770 // [23.2.5]/1, third-to-last entry in synopsis listing 00771 static void swap(reference __x, reference __y) 00772 { 00773 bool __tmp = __x; 00774 __x = __y; 00775 __y = __tmp; 00776 } 00777 00778 iterator insert(iterator __position, bool __x = bool()) 00779 { 00780 const difference_type __n = __position - begin(); 00781 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage 00782 && __position == end()) 00783 *this->_M_impl._M_finish++ = __x; 00784 else 00785 _M_insert_aux(__position, __x); 00786 return begin() + __n; 00787 } 00788 00789 // Check whether it's an integral type. If so, it's not an iterator. 00790 00791 template<class _Integer> 00792 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, 00793 __true_type) 00794 { _M_fill_insert(__pos, __n, __x); } 00795 00796 template<class _InputIterator> 00797 void _M_insert_dispatch(iterator __pos, 00798 _InputIterator __first, _InputIterator __last, 00799 __false_type) 00800 { _M_insert_range(__pos, __first, __last, 00801 std::__iterator_category(__first)); } 00802 00803 template<class _InputIterator> 00804 void insert(iterator __position, 00805 _InputIterator __first, _InputIterator __last) 00806 { 00807 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00808 _M_insert_dispatch(__position, __first, __last, _Integral()); 00809 } 00810 00811 void _M_fill_insert(iterator __position, size_type __n, bool __x) 00812 { 00813 if (__n == 0) 00814 return; 00815 if (capacity() - size() >= __n) 00816 { 00817 std::copy_backward(__position, end(), 00818 this->_M_impl._M_finish + difference_type(__n)); 00819 std::fill(__position, __position + difference_type(__n), __x); 00820 this->_M_impl._M_finish += difference_type(__n); 00821 } 00822 else 00823 { 00824 const size_type __len = size() + std::max(size(), __n); 00825 _Bit_type * __q = this->_M_allocate(__len); 00826 iterator __i = std::copy(begin(), __position, iterator(__q, 0)); 00827 std::fill_n(__i, __n, __x); 00828 this->_M_impl._M_finish = std::copy(__position, end(), 00829 __i + difference_type(__n)); 00830 this->_M_deallocate(); 00831 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1) 00832 / _S_word_bit; 00833 this->_M_impl._M_start = iterator(__q, 0); 00834 } 00835 } 00836 00837 void insert(iterator __position, size_type __n, bool __x) 00838 { _M_fill_insert(__position, __n, __x); } 00839 00840 void pop_back() 00841 { --this->_M_impl._M_finish; } 00842 00843 iterator erase(iterator __position) 00844 { 00845 if (__position + 1 != end()) 00846 std::copy(__position + 1, end(), __position); 00847 --this->_M_impl._M_finish; 00848 return __position; 00849 } 00850 00851 iterator erase(iterator __first, iterator __last) 00852 { 00853 this->_M_impl._M_finish = std::copy(__last, end(), __first); 00854 return __first; 00855 } 00856 00857 void resize(size_type __new_size, bool __x = bool()) 00858 { 00859 if (__new_size < size()) 00860 erase(begin() + difference_type(__new_size), end()); 00861 else 00862 insert(end(), __new_size - size(), __x); 00863 } 00864 00865 void flip() 00866 { 00867 for (_Bit_type * __p = this->_M_impl._M_start._M_p; 00868 __p != this->_M_impl._M_end_of_storage; ++__p) 00869 *__p = ~*__p; 00870 } 00871 00872 void clear() 00873 { erase(begin(), end()); } 00874 }; 00875 } // namespace std 00876 00877 #endif

Generated on Sun Jul 25 00:12:34 2004 for libstdc++ source by doxygen 1.3.7