Copyright (C) 2000-2012 |
Whole document tree stl_bvector.hGo to the documentation of this file.00001 // bit_vector and vector<bool> specialization -*- C++ -*- 00002 00003 // Copyright (C) 2001 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 /* NOTE: This is an internal header file, included by other STL headers. 00057 * You should not attempt to use it directly. 00058 */ 00059 00060 #ifndef __SGI_STL_INTERNAL_BVECTOR_H 00061 #define __SGI_STL_INTERNAL_BVECTOR_H 00062 00063 namespace std 00064 { 00065 00066 static const int __WORD_BIT = int(CHAR_BIT*sizeof(unsigned int)); 00067 00068 struct _Bit_reference { 00069 unsigned int* _M_p; 00070 unsigned int _M_mask; 00071 _Bit_reference(unsigned int* __x, unsigned int __y) 00072 : _M_p(__x), _M_mask(__y) {} 00073 00074 public: 00075 _Bit_reference() : _M_p(0), _M_mask(0) {} 00076 operator bool() const { return !(!(*_M_p & _M_mask)); } 00077 _Bit_reference& operator=(bool __x) 00078 { 00079 if (__x) *_M_p |= _M_mask; 00080 else *_M_p &= ~_M_mask; 00081 return *this; 00082 } 00083 _Bit_reference& operator=(const _Bit_reference& __x) 00084 { return *this = bool(__x); } 00085 bool operator==(const _Bit_reference& __x) const 00086 { return bool(*this) == bool(__x); } 00087 bool operator<(const _Bit_reference& __x) const { 00088 return !bool(*this) && bool(__x); 00089 } 00090 void flip() { *_M_p ^= _M_mask; } 00091 }; 00092 00093 inline void swap(_Bit_reference __x, _Bit_reference __y) 00094 { 00095 bool __tmp = __x; 00096 __x = __y; 00097 __y = __tmp; 00098 } 00099 00100 struct _Bit_iterator_base : public random_access_iterator<bool, ptrdiff_t> 00101 { 00102 unsigned int* _M_p; 00103 unsigned int _M_offset; 00104 00105 _Bit_iterator_base(unsigned int* __x, unsigned int __y) 00106 : _M_p(__x), _M_offset(__y) {} 00107 00108 void _M_bump_up() { 00109 if (_M_offset++ == __WORD_BIT - 1) { 00110 _M_offset = 0; 00111 ++_M_p; 00112 } 00113 } 00114 void _M_bump_down() { 00115 if (_M_offset-- == 0) { 00116 _M_offset = __WORD_BIT - 1; 00117 --_M_p; 00118 } 00119 } 00120 00121 void _M_incr(ptrdiff_t __i) { 00122 difference_type __n = __i + _M_offset; 00123 _M_p += __n / __WORD_BIT; 00124 __n = __n % __WORD_BIT; 00125 if (__n < 0) { 00126 _M_offset = (unsigned int) __n + __WORD_BIT; 00127 --_M_p; 00128 } else 00129 _M_offset = (unsigned int) __n; 00130 } 00131 00132 bool operator==(const _Bit_iterator_base& __i) const { 00133 return _M_p == __i._M_p && _M_offset == __i._M_offset; 00134 } 00135 bool operator<(const _Bit_iterator_base& __i) const { 00136 return _M_p < __i._M_p || (_M_p == __i._M_p && _M_offset < __i._M_offset); 00137 } 00138 bool operator!=(const _Bit_iterator_base& __i) const { 00139 return !(*this == __i); 00140 } 00141 bool operator>(const _Bit_iterator_base& __i) const { 00142 return __i < *this; 00143 } 00144 bool operator<=(const _Bit_iterator_base& __i) const { 00145 return !(__i < *this); 00146 } 00147 bool operator>=(const _Bit_iterator_base& __i) const { 00148 return !(*this < __i); 00149 } 00150 }; 00151 00152 inline ptrdiff_t 00153 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) { 00154 return __WORD_BIT * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset; 00155 } 00156 00157 00158 struct _Bit_iterator : public _Bit_iterator_base 00159 { 00160 typedef _Bit_reference reference; 00161 typedef _Bit_reference* pointer; 00162 typedef _Bit_iterator iterator; 00163 00164 _Bit_iterator() : _Bit_iterator_base(0, 0) {} 00165 _Bit_iterator(unsigned int* __x, unsigned int __y) 00166 : _Bit_iterator_base(__x, __y) {} 00167 00168 reference operator*() const { return reference(_M_p, 1U << _M_offset); } 00169 iterator& operator++() { 00170 _M_bump_up(); 00171 return *this; 00172 } 00173 iterator operator++(int) { 00174 iterator __tmp = *this; 00175 _M_bump_up(); 00176 return __tmp; 00177 } 00178 iterator& operator--() { 00179 _M_bump_down(); 00180 return *this; 00181 } 00182 iterator operator--(int) { 00183 iterator __tmp = *this; 00184 _M_bump_down(); 00185 return __tmp; 00186 } 00187 iterator& operator+=(difference_type __i) { 00188 _M_incr(__i); 00189 return *this; 00190 } 00191 iterator& operator-=(difference_type __i) { 00192 *this += -__i; 00193 return *this; 00194 } 00195 iterator operator+(difference_type __i) const { 00196 iterator __tmp = *this; 00197 return __tmp += __i; 00198 } 00199 iterator operator-(difference_type __i) const { 00200 iterator __tmp = *this; 00201 return __tmp -= __i; 00202 } 00203 00204 reference operator[](difference_type __i) { return *(*this + __i); } 00205 }; 00206 00207 inline _Bit_iterator 00208 operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; } 00209 00210 00211 struct _Bit_const_iterator : public _Bit_iterator_base 00212 { 00213 typedef bool reference; 00214 typedef bool const_reference; 00215 typedef const bool* pointer; 00216 typedef _Bit_const_iterator const_iterator; 00217 00218 _Bit_const_iterator() : _Bit_iterator_base(0, 0) {} 00219 _Bit_const_iterator(unsigned int* __x, unsigned int __y) 00220 : _Bit_iterator_base(__x, __y) {} 00221 _Bit_const_iterator(const _Bit_iterator& __x) 00222 : _Bit_iterator_base(__x._M_p, __x._M_offset) {} 00223 00224 const_reference operator*() const { 00225 return _Bit_reference(_M_p, 1U << _M_offset); 00226 } 00227 const_iterator& operator++() { 00228 _M_bump_up(); 00229 return *this; 00230 } 00231 const_iterator operator++(int) { 00232 const_iterator __tmp = *this; 00233 _M_bump_up(); 00234 return __tmp; 00235 } 00236 const_iterator& operator--() { 00237 _M_bump_down(); 00238 return *this; 00239 } 00240 const_iterator operator--(int) { 00241 const_iterator __tmp = *this; 00242 _M_bump_down(); 00243 return __tmp; 00244 } 00245 const_iterator& operator+=(difference_type __i) { 00246 _M_incr(__i); 00247 return *this; 00248 } 00249 const_iterator& operator-=(difference_type __i) { 00250 *this += -__i; 00251 return *this; 00252 } 00253 const_iterator operator+(difference_type __i) const { 00254 const_iterator __tmp = *this; 00255 return __tmp += __i; 00256 } 00257 const_iterator operator-(difference_type __i) const { 00258 const_iterator __tmp = *this; 00259 return __tmp -= __i; 00260 } 00261 const_reference operator[](difference_type __i) { 00262 return *(*this + __i); 00263 } 00264 }; 00265 00266 inline _Bit_const_iterator 00267 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) { return __x + __n; } 00268 00269 00270 // Bit-vector base class, which encapsulates the difference between 00271 // old SGI-style allocators and standard-conforming allocators. 00272 00273 // Base class for ordinary allocators. 00274 template <class _Allocator, bool __is_static> 00275 class _Bvector_alloc_base { 00276 public: 00277 typedef typename _Alloc_traits<bool, _Allocator>::allocator_type 00278 allocator_type; 00279 allocator_type get_allocator() const { return _M_data_allocator; } 00280 00281 _Bvector_alloc_base(const allocator_type& __a) 00282 : _M_data_allocator(__a), _M_start(), _M_finish(), _M_end_of_storage(0) {} 00283 00284 protected: 00285 unsigned int* _M_bit_alloc(size_t __n) 00286 { return _M_data_allocator.allocate((__n + __WORD_BIT - 1)/__WORD_BIT); } 00287 void _M_deallocate() { 00288 if (_M_start._M_p) 00289 _M_data_allocator.deallocate(_M_start._M_p, 00290 _M_end_of_storage - _M_start._M_p); 00291 } 00292 00293 typename _Alloc_traits<unsigned int, _Allocator>::allocator_type 00294 _M_data_allocator; 00295 _Bit_iterator _M_start; 00296 _Bit_iterator _M_finish; 00297 unsigned int* _M_end_of_storage; 00298 }; 00299 00300 // Specialization for instanceless allocators. 00301 template <class _Allocator> 00302 class _Bvector_alloc_base<_Allocator, true> { 00303 public: 00304 typedef typename _Alloc_traits<bool, _Allocator>::allocator_type 00305 allocator_type; 00306 allocator_type get_allocator() const { return allocator_type(); } 00307 00308 _Bvector_alloc_base(const allocator_type&) 00309 : _M_start(), _M_finish(), _M_end_of_storage(0) {} 00310 00311 protected: 00312 typedef typename _Alloc_traits<unsigned int, _Allocator>::_Alloc_type 00313 _Alloc_type; 00314 00315 unsigned int* _M_bit_alloc(size_t __n) 00316 { return _Alloc_type::allocate((__n + __WORD_BIT - 1)/__WORD_BIT); } 00317 void _M_deallocate() { 00318 if (_M_start._M_p) 00319 _Alloc_type::deallocate(_M_start._M_p, 00320 _M_end_of_storage - _M_start._M_p); 00321 } 00322 00323 _Bit_iterator _M_start; 00324 _Bit_iterator _M_finish; 00325 unsigned int* _M_end_of_storage; 00326 }; 00327 00328 template <class _Alloc> 00329 class _Bvector_base 00330 : public _Bvector_alloc_base<_Alloc, 00331 _Alloc_traits<bool, _Alloc>::_S_instanceless> 00332 { 00333 typedef _Bvector_alloc_base<_Alloc, 00334 _Alloc_traits<bool, _Alloc>::_S_instanceless> 00335 _Base; 00336 public: 00337 typedef typename _Base::allocator_type allocator_type; 00338 00339 _Bvector_base(const allocator_type& __a) : _Base(__a) {} 00340 ~_Bvector_base() { _Base::_M_deallocate(); } 00341 }; 00342 00343 } // namespace std 00344 00345 // Declare a partial specialization of vector<T, Alloc>. 00346 #include <bits/stl_vector.h> 00347 namespace std 00348 { 00349 00350 template <typename _Alloc> 00351 class vector<bool, _Alloc> : public _Bvector_base<_Alloc> 00352 { 00353 public: 00354 typedef bool value_type; 00355 typedef size_t size_type; 00356 typedef ptrdiff_t difference_type; 00357 typedef _Bit_reference reference; 00358 typedef bool const_reference; 00359 typedef _Bit_reference* pointer; 00360 typedef const bool* const_pointer; 00361 00362 typedef _Bit_iterator iterator; 00363 typedef _Bit_const_iterator const_iterator; 00364 00365 typedef reverse_iterator<const_iterator> const_reverse_iterator; 00366 typedef reverse_iterator<iterator> reverse_iterator; 00367 00368 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type; 00369 allocator_type get_allocator() const { 00370 return _Bvector_base<_Alloc>::get_allocator(); 00371 } 00372 00373 protected: 00374 using _Bvector_base<_Alloc>::_M_bit_alloc; 00375 using _Bvector_base<_Alloc>::_M_deallocate; 00376 using _Bvector_base<_Alloc>::_M_start; 00377 using _Bvector_base<_Alloc>::_M_finish; 00378 using _Bvector_base<_Alloc>::_M_end_of_storage; 00379 00380 protected: 00381 void _M_initialize(size_type __n) { 00382 unsigned int* __q = _M_bit_alloc(__n); 00383 _M_end_of_storage = __q + (__n + __WORD_BIT - 1)/__WORD_BIT; 00384 _M_start = iterator(__q, 0); 00385 _M_finish = _M_start + difference_type(__n); 00386 } 00387 void _M_insert_aux(iterator __position, bool __x) { 00388 if (_M_finish._M_p != _M_end_of_storage) { 00389 copy_backward(__position, _M_finish, _M_finish + 1); 00390 *__position = __x; 00391 ++_M_finish; 00392 } 00393 else { 00394 size_type __len = size() ? 2 * size() : __WORD_BIT; 00395 unsigned int* __q = _M_bit_alloc(__len); 00396 iterator __i = copy(begin(), __position, iterator(__q, 0)); 00397 *__i++ = __x; 00398 _M_finish = copy(__position, end(), __i); 00399 _M_deallocate(); 00400 _M_end_of_storage = __q + (__len + __WORD_BIT - 1)/__WORD_BIT; 00401 _M_start = iterator(__q, 0); 00402 } 00403 } 00404 00405 template <class _InputIterator> 00406 void _M_initialize_range(_InputIterator __first, _InputIterator __last, 00407 input_iterator_tag) { 00408 _M_start = iterator(); 00409 _M_finish = iterator(); 00410 _M_end_of_storage = 0; 00411 for ( ; __first != __last; ++__first) 00412 push_back(*__first); 00413 } 00414 00415 template <class _ForwardIterator> 00416 void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last, 00417 forward_iterator_tag) { 00418 size_type __n = 0; 00419 distance(__first, __last, __n); 00420 _M_initialize(__n); 00421 copy(__first, __last, _M_start); 00422 } 00423 00424 template <class _InputIterator> 00425 void _M_insert_range(iterator __pos, 00426 _InputIterator __first, _InputIterator __last, 00427 input_iterator_tag) { 00428 for ( ; __first != __last; ++__first) { 00429 __pos = insert(__pos, *__first); 00430 ++__pos; 00431 } 00432 } 00433 00434 template <class _ForwardIterator> 00435 void _M_insert_range(iterator __position, 00436 _ForwardIterator __first, _ForwardIterator __last, 00437 forward_iterator_tag) { 00438 if (__first != __last) { 00439 size_type __n = 0; 00440 distance(__first, __last, __n); 00441 if (capacity() - size() >= __n) { 00442 copy_backward(__position, end(), _M_finish + difference_type(__n)); 00443 copy(__first, __last, __position); 00444 _M_finish += difference_type(__n); 00445 } 00446 else { 00447 size_type __len = size() + max(size(), __n); 00448 unsigned int* __q = _M_bit_alloc(__len); 00449 iterator __i = copy(begin(), __position, iterator(__q, 0)); 00450 __i = copy(__first, __last, __i); 00451 _M_finish = copy(__position, end(), __i); 00452 _M_deallocate(); 00453 _M_end_of_storage = __q + (__len + __WORD_BIT - 1)/__WORD_BIT; 00454 _M_start = iterator(__q, 0); 00455 } 00456 } 00457 } 00458 00459 public: 00460 iterator begin() { return _M_start; } 00461 const_iterator begin() const { return _M_start; } 00462 iterator end() { return _M_finish; } 00463 const_iterator end() const { return _M_finish; } 00464 00465 reverse_iterator rbegin() { return reverse_iterator(end()); } 00466 const_reverse_iterator rbegin() const { 00467 return const_reverse_iterator(end()); 00468 } 00469 reverse_iterator rend() { return reverse_iterator(begin()); } 00470 const_reverse_iterator rend() const { 00471 return const_reverse_iterator(begin()); 00472 } 00473 00474 size_type size() const { return size_type(end() - begin()); } 00475 size_type max_size() const { return size_type(-1); } 00476 size_type capacity() const { 00477 return size_type(const_iterator(_M_end_of_storage, 0) - begin()); 00478 } 00479 bool empty() const { return begin() == end(); } 00480 00481 reference operator[](size_type __n) 00482 { return *(begin() + difference_type(__n)); } 00483 const_reference operator[](size_type __n) const 00484 { return *(begin() + difference_type(__n)); } 00485 00486 void _M_range_check(size_type __n) const { 00487 if (__n >= this->size()) 00488 __throw_range_error("vector<bool>"); 00489 } 00490 00491 reference at(size_type __n) 00492 { _M_range_check(__n); return (*this)[__n]; } 00493 const_reference at(size_type __n) const 00494 { _M_range_check(__n); return (*this)[__n]; } 00495 00496 explicit vector(const allocator_type& __a = allocator_type()) 00497 : _Bvector_base<_Alloc>(__a) {} 00498 00499 vector(size_type __n, bool __value, 00500 const allocator_type& __a = allocator_type()) 00501 : _Bvector_base<_Alloc>(__a) 00502 { 00503 _M_initialize(__n); 00504 fill(_M_start._M_p, _M_end_of_storage, __value ? ~0 : 0); 00505 } 00506 00507 explicit vector(size_type __n) 00508 : _Bvector_base<_Alloc>(allocator_type()) 00509 { 00510 _M_initialize(__n); 00511 fill(_M_start._M_p, _M_end_of_storage, 0); 00512 } 00513 00514 vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) { 00515 _M_initialize(__x.size()); 00516 copy(__x.begin(), __x.end(), _M_start); 00517 } 00518 00519 // Check whether it's an integral type. If so, it's not an iterator. 00520 00521 template <class _Integer> 00522 void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) { 00523 _M_initialize(__n); 00524 fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0); 00525 } 00526 00527 template <class _InputIterator> 00528 void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 00529 __false_type) { 00530 _M_initialize_range(__first, __last, __iterator_category(__first)); 00531 } 00532 00533 template <class _InputIterator> 00534 vector(_InputIterator __first, _InputIterator __last, 00535 const allocator_type& __a = allocator_type()) 00536 : _Bvector_base<_Alloc>(__a) 00537 { 00538 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00539 _M_initialize_dispatch(__first, __last, _Integral()); 00540 } 00541 00542 ~vector() { } 00543 00544 vector& operator=(const vector& __x) { 00545 if (&__x == this) return *this; 00546 if (__x.size() > capacity()) { 00547 _M_deallocate(); 00548 _M_initialize(__x.size()); 00549 } 00550 copy(__x.begin(), __x.end(), begin()); 00551 _M_finish = begin() + difference_type(__x.size()); 00552 return *this; 00553 } 00554 00555 // assign(), a generalized assignment member function. Two 00556 // versions: one that takes a count, and one that takes a range. 00557 // The range version is a member template, so we dispatch on whether 00558 // or not the type is an integer. 00559 00560 void _M_fill_assign(size_t __n, bool __x) { 00561 if (__n > size()) { 00562 fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0); 00563 insert(end(), __n - size(), __x); 00564 } 00565 else { 00566 erase(begin() + __n, end()); 00567 fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0); 00568 } 00569 } 00570 00571 void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); } 00572 00573 template <class _InputIterator> 00574 void assign(_InputIterator __first, _InputIterator __last) { 00575 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00576 _M_assign_dispatch(__first, __last, _Integral()); 00577 } 00578 00579 template <class _Integer> 00580 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 00581 { _M_fill_assign((size_t) __n, (bool) __val); } 00582 00583 template <class _InputIter> 00584 void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type) 00585 { _M_assign_aux(__first, __last, __iterator_category(__first)); } 00586 00587 template <class _InputIterator> 00588 void _M_assign_aux(_InputIterator __first, _InputIterator __last, 00589 input_iterator_tag) { 00590 iterator __cur = begin(); 00591 for ( ; __first != __last && __cur != end(); ++__cur, ++__first) 00592 *__cur = *__first; 00593 if (__first == __last) 00594 erase(__cur, end()); 00595 else 00596 insert(end(), __first, __last); 00597 } 00598 00599 template <class _ForwardIterator> 00600 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 00601 forward_iterator_tag) { 00602 size_type __len = 0; 00603 distance(__first, __last, __len); 00604 if (__len < size()) 00605 erase(copy(__first, __last, begin()), end()); 00606 else { 00607 _ForwardIterator __mid = __first; 00608 advance(__mid, size()); 00609 copy(__first, __mid, begin()); 00610 insert(end(), __mid, __last); 00611 } 00612 } 00613 00614 void reserve(size_type __n) { 00615 if (capacity() < __n) { 00616 unsigned int* __q = _M_bit_alloc(__n); 00617 _M_finish = copy(begin(), end(), iterator(__q, 0)); 00618 _M_deallocate(); 00619 _M_start = iterator(__q, 0); 00620 _M_end_of_storage = __q + (__n + __WORD_BIT - 1)/__WORD_BIT; 00621 } 00622 } 00623 00624 reference front() { return *begin(); } 00625 const_reference front() const { return *begin(); } 00626 reference back() { return *(end() - 1); } 00627 const_reference back() const { return *(end() - 1); } 00628 void push_back(bool __x) { 00629 if (_M_finish._M_p != _M_end_of_storage) 00630 *_M_finish++ = __x; 00631 else 00632 _M_insert_aux(end(), __x); 00633 } 00634 void swap(vector<bool, _Alloc>& __x) { 00635 std::swap(_M_start, __x._M_start); 00636 std::swap(_M_finish, __x._M_finish); 00637 std::swap(_M_end_of_storage, __x._M_end_of_storage); 00638 } 00639 iterator insert(iterator __position, bool __x = bool()) { 00640 difference_type __n = __position - begin(); 00641 if (_M_finish._M_p != _M_end_of_storage && __position == end()) 00642 *_M_finish++ = __x; 00643 else 00644 _M_insert_aux(__position, __x); 00645 return begin() + __n; 00646 } 00647 00648 // Check whether it's an integral type. If so, it's not an iterator. 00649 00650 template <class _Integer> 00651 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, 00652 __true_type) { 00653 _M_fill_insert(__pos, __n, __x); 00654 } 00655 00656 template <class _InputIterator> 00657 void _M_insert_dispatch(iterator __pos, 00658 _InputIterator __first, _InputIterator __last, 00659 __false_type) { 00660 _M_insert_range(__pos, __first, __last, __iterator_category(__first)); 00661 } 00662 00663 template <class _InputIterator> 00664 void insert(iterator __position, 00665 _InputIterator __first, _InputIterator __last) { 00666 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00667 _M_insert_dispatch(__position, __first, __last, _Integral()); 00668 } 00669 00670 void _M_fill_insert(iterator __position, size_type __n, bool __x) { 00671 if (__n == 0) return; 00672 if (capacity() - size() >= __n) { 00673 copy_backward(__position, end(), _M_finish + difference_type(__n)); 00674 fill(__position, __position + difference_type(__n), __x); 00675 _M_finish += difference_type(__n); 00676 } 00677 else { 00678 size_type __len = size() + max(size(), __n); 00679 unsigned int* __q = _M_bit_alloc(__len); 00680 iterator __i = copy(begin(), __position, iterator(__q, 0)); 00681 fill_n(__i, __n, __x); 00682 _M_finish = copy(__position, end(), __i + difference_type(__n)); 00683 _M_deallocate(); 00684 _M_end_of_storage = __q + (__len + __WORD_BIT - 1)/__WORD_BIT; 00685 _M_start = iterator(__q, 0); 00686 } 00687 } 00688 00689 void insert(iterator __position, size_type __n, bool __x) { 00690 _M_fill_insert(__position, __n, __x); 00691 } 00692 00693 void pop_back() { --_M_finish; } 00694 iterator erase(iterator __position) { 00695 if (__position + 1 != end()) 00696 copy(__position + 1, end(), __position); 00697 --_M_finish; 00698 return __position; 00699 } 00700 iterator erase(iterator __first, iterator __last) { 00701 _M_finish = copy(__last, end(), __first); 00702 return __first; 00703 } 00704 void resize(size_type __new_size, bool __x = bool()) { 00705 if (__new_size < size()) 00706 erase(begin() + difference_type(__new_size), end()); 00707 else 00708 insert(end(), __new_size - size(), __x); 00709 } 00710 void flip() { 00711 for (unsigned int* __p = _M_start._M_p; __p != _M_end_of_storage; ++__p) 00712 *__p = ~*__p; 00713 } 00714 00715 void clear() { erase(begin(), end()); } 00716 }; 00717 00718 // This typedef is non-standard. It is provided for backward compatibility. 00719 typedef vector<bool, alloc> bit_vector; 00720 00721 } // namespace std 00722 00723 #endif /* __SGI_STL_INTERNAL_BVECTOR_H */ 00724 00725 // Local Variables: 00726 // mode:C++ 00727 // End: Generated on Mon Apr 8 03:11:38 2002 for libstdc++-v3 Source by 1.2.15 |