OSDN Git Service

Merge basic-improvements-branch to trunk
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_bvector.h
1 // bit_vector and vector<bool> specialization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
35  * Permission to use, copy, modify, distribute and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appear in all copies and
38  * that both that copyright notice and this permission notice appear
39  * in supporting documentation.  Hewlett-Packard Company makes no
40  * representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied warranty.
42  *
43  *
44  * Copyright (c) 1996-1999
45  * Silicon Graphics Computer Systems, Inc.
46  *
47  * Permission to use, copy, modify, distribute and sell this software
48  * and its documentation for any purpose is hereby granted without fee,
49  * provided that the above copyright notice appear in all copies and
50  * that both that copyright notice and this permission notice appear
51  * in supporting documentation.  Silicon Graphics makes no
52  * representations about the suitability of this software for any
53  * purpose.  It is provided "as is" without express or implied warranty.
54  */
55
56 /** @file stl_bvector.h
57  *  This is an internal header file, included by other library headers.
58  *  You should not attempt to use it directly.
59  */
60
61 #ifndef __GLIBCPP_INTERNAL_BVECTOR_H
62 #define __GLIBCPP_INTERNAL_BVECTOR_H
63
64 namespace std
65
66   typedef unsigned long _Bit_type;
67   enum { _M_word_bit = int(CHAR_BIT * sizeof(_Bit_type)) };
68
69 struct _Bit_reference {
70
71   _Bit_type * _M_p;
72   _Bit_type _M_mask;
73   _Bit_reference(_Bit_type * __x, _Bit_type __y) 
74     : _M_p(__x), _M_mask(__y) {}
75
76 public:
77   _Bit_reference() : _M_p(0), _M_mask(0) {}
78   operator bool() const { return !!(*_M_p & _M_mask); }
79   _Bit_reference& operator=(bool __x)
80   {
81     if (__x)  *_M_p |= _M_mask;
82     else      *_M_p &= ~_M_mask;
83     return *this;
84   }
85   _Bit_reference& operator=(const _Bit_reference& __x) 
86     { return *this = bool(__x); }
87   bool operator==(const _Bit_reference& __x) const
88     { return bool(*this) == bool(__x); }
89   bool operator<(const _Bit_reference& __x) const
90     { return !bool(*this) && bool(__x); }
91   void flip() { *_M_p ^= _M_mask; }
92 };
93
94 struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool>
95 {
96   _Bit_type * _M_p;
97   unsigned int _M_offset;
98
99   _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
100     : _M_p(__x), _M_offset(__y) {}
101
102   void _M_bump_up() {
103     if (_M_offset++ == _M_word_bit - 1) {
104       _M_offset = 0;
105       ++_M_p;
106     }
107   }
108   void _M_bump_down() {
109     if (_M_offset-- == 0) {
110       _M_offset = _M_word_bit - 1;
111       --_M_p;
112     }
113   }
114
115   void _M_incr(ptrdiff_t __i) {
116     difference_type __n = __i + _M_offset;
117     _M_p += __n / _M_word_bit;
118     __n = __n % _M_word_bit;
119     if (__n < 0) {
120       _M_offset = (unsigned int) __n + _M_word_bit;
121       --_M_p;
122     } else
123       _M_offset = (unsigned int) __n;
124   }
125
126   bool operator==(const _Bit_iterator_base& __i) const {
127     return _M_p == __i._M_p && _M_offset == __i._M_offset;
128   }
129   bool operator<(const _Bit_iterator_base& __i) const {
130     return _M_p < __i._M_p || (_M_p == __i._M_p && _M_offset < __i._M_offset);
131   }
132   bool operator!=(const _Bit_iterator_base& __i) const {
133     return !(*this == __i);
134   }
135   bool operator>(const _Bit_iterator_base& __i) const {
136     return __i < *this;
137   }
138   bool operator<=(const _Bit_iterator_base& __i) const {
139     return !(__i < *this); 
140   }
141   bool operator>=(const _Bit_iterator_base& __i) const {
142     return !(*this < __i);
143   }
144 };
145
146 inline ptrdiff_t
147 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
148   return _M_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
149 }
150
151
152 struct _Bit_iterator : public _Bit_iterator_base
153 {
154   typedef _Bit_reference  reference;
155   typedef _Bit_reference* pointer;
156   typedef _Bit_iterator   iterator;
157
158   _Bit_iterator() : _Bit_iterator_base(0, 0) {}
159   _Bit_iterator(_Bit_type * __x, unsigned int __y) 
160     : _Bit_iterator_base(__x, __y) {}
161
162   reference operator*() const { return reference(_M_p, 1UL << _M_offset); }
163   iterator& operator++() {
164     _M_bump_up();
165     return *this;
166   }
167   iterator operator++(int) {
168     iterator __tmp = *this;
169     _M_bump_up();
170     return __tmp;
171   }
172   iterator& operator--() {
173     _M_bump_down();
174     return *this;
175   }
176   iterator operator--(int) {
177     iterator __tmp = *this;
178     _M_bump_down();
179     return __tmp;
180   }
181   iterator& operator+=(difference_type __i) {
182     _M_incr(__i);
183     return *this;
184   }
185   iterator& operator-=(difference_type __i) {
186     *this += -__i;
187     return *this;
188   }
189   iterator operator+(difference_type __i) const {
190     iterator __tmp = *this;
191     return __tmp += __i;
192   }
193   iterator operator-(difference_type __i) const {
194     iterator __tmp = *this;
195     return __tmp -= __i;
196   }
197
198   reference operator[](difference_type __i) { return *(*this + __i); }
199 };
200
201 inline _Bit_iterator 
202 operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; }
203
204
205 struct _Bit_const_iterator : public _Bit_iterator_base
206 {
207   typedef bool                 reference;
208   typedef bool                 const_reference;
209   typedef const bool*          pointer;
210   typedef _Bit_const_iterator  const_iterator;
211
212   _Bit_const_iterator() : _Bit_iterator_base(0, 0) {}
213   _Bit_const_iterator(_Bit_type * __x, unsigned int __y) 
214     : _Bit_iterator_base(__x, __y) {}
215   _Bit_const_iterator(const _Bit_iterator& __x) 
216     : _Bit_iterator_base(__x._M_p, __x._M_offset) {}
217
218   const_reference operator*() const {
219     return _Bit_reference(_M_p, 1UL << _M_offset);
220   }
221   const_iterator& operator++() {
222     _M_bump_up();
223     return *this;
224   }
225   const_iterator operator++(int) {
226     const_iterator __tmp = *this;
227     _M_bump_up();
228     return __tmp;
229   }
230   const_iterator& operator--() {
231     _M_bump_down();
232     return *this;
233   }
234   const_iterator operator--(int) {
235     const_iterator __tmp = *this;
236     _M_bump_down();
237     return __tmp;
238   }
239   const_iterator& operator+=(difference_type __i) {
240     _M_incr(__i);
241     return *this;
242   }
243   const_iterator& operator-=(difference_type __i) {
244     *this += -__i;
245     return *this;
246   }
247   const_iterator operator+(difference_type __i) const {
248     const_iterator __tmp = *this;
249     return __tmp += __i;
250   }
251   const_iterator operator-(difference_type __i) const {
252     const_iterator __tmp = *this;
253     return __tmp -= __i;
254   }
255   const_reference operator[](difference_type __i) { 
256     return *(*this + __i); 
257   }
258 };
259
260 inline _Bit_const_iterator 
261 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) { return __x + __n; }
262
263
264 // Bit-vector base class, which encapsulates the difference between
265 // old SGI-style allocators and standard-conforming allocators.
266
267 // Base class for ordinary allocators.
268 template <class _Allocator, bool __is_static>
269 class _Bvector_alloc_base {
270 public:
271   typedef typename _Alloc_traits<bool, _Allocator>::allocator_type
272           allocator_type;
273   allocator_type get_allocator() const { return _M_data_allocator; }
274
275   _Bvector_alloc_base(const allocator_type& __a)
276     : _M_data_allocator(__a), _M_start(), _M_finish(), _M_end_of_storage(0) {}
277
278 protected:
279   _Bit_type * _M_bit_alloc(size_t __n) 
280     { return _M_data_allocator.allocate((__n + _M_word_bit - 1)/_M_word_bit); }
281   void _M_deallocate() {
282     if (_M_start._M_p)
283       _M_data_allocator.deallocate(_M_start._M_p, 
284                                    _M_end_of_storage - _M_start._M_p);
285   }  
286
287   typename _Alloc_traits<_Bit_type, _Allocator>::allocator_type 
288           _M_data_allocator;
289   _Bit_iterator _M_start;
290   _Bit_iterator _M_finish;
291   _Bit_type * _M_end_of_storage;
292 };
293
294 // Specialization for instanceless allocators.
295 template <class _Allocator>
296 class _Bvector_alloc_base<_Allocator, true> {
297 public:
298   typedef typename _Alloc_traits<bool, _Allocator>::allocator_type
299           allocator_type;
300   allocator_type get_allocator() const { return allocator_type(); }
301
302   _Bvector_alloc_base(const allocator_type&)
303     : _M_start(), _M_finish(), _M_end_of_storage(0) {}
304
305 protected:
306   typedef typename _Alloc_traits<_Bit_type, _Allocator>::_Alloc_type
307           _Alloc_type;
308           
309   _Bit_type * _M_bit_alloc(size_t __n) 
310     { return _Alloc_type::allocate((__n + _M_word_bit - 1)/_M_word_bit); }
311   void _M_deallocate() {
312     if (_M_start._M_p)
313       _Alloc_type::deallocate(_M_start._M_p,
314                               _M_end_of_storage - _M_start._M_p);
315   }  
316
317   _Bit_iterator _M_start;
318   _Bit_iterator _M_finish;
319   _Bit_type * _M_end_of_storage;
320 };  
321
322 template <class _Alloc>
323 class _Bvector_base
324   : public _Bvector_alloc_base<_Alloc,
325                                _Alloc_traits<bool, _Alloc>::_S_instanceless>
326 {
327   typedef _Bvector_alloc_base<_Alloc,
328                               _Alloc_traits<bool, _Alloc>::_S_instanceless>
329           _Base;
330 public:
331   typedef typename _Base::allocator_type allocator_type;
332
333   _Bvector_base(const allocator_type& __a) : _Base(__a) {}
334   ~_Bvector_base() { _Base::_M_deallocate(); }
335 };
336
337 } // namespace std
338
339 // Declare a partial specialization of vector<T, Alloc>.
340 #include <bits/stl_vector.h>
341 namespace std
342 {
343
344 template <typename _Alloc> 
345   class vector<bool, _Alloc> : public _Bvector_base<_Alloc> 
346   {
347   public:
348     typedef bool value_type;
349     typedef size_t size_type;
350     typedef ptrdiff_t difference_type; 
351     typedef _Bit_reference reference;
352     typedef bool const_reference;
353     typedef _Bit_reference* pointer;
354     typedef const bool* const_pointer;
355   
356     typedef _Bit_iterator                iterator;
357     typedef _Bit_const_iterator          const_iterator;
358   
359     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
360     typedef std::reverse_iterator<iterator> reverse_iterator;
361   
362     typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
363     allocator_type get_allocator() const {
364       return _Bvector_base<_Alloc>::get_allocator();
365     }
366   
367   protected:
368     using _Bvector_base<_Alloc>::_M_bit_alloc;
369     using _Bvector_base<_Alloc>::_M_deallocate;
370     using _Bvector_base<_Alloc>::_M_start;
371     using _Bvector_base<_Alloc>::_M_finish;
372     using _Bvector_base<_Alloc>::_M_end_of_storage;
373   
374   protected:
375     void _M_initialize(size_type __n) {
376       _Bit_type * __q = _M_bit_alloc(__n);
377       _M_end_of_storage = __q + (__n + _M_word_bit - 1)/_M_word_bit;
378       _M_start = iterator(__q, 0);
379       _M_finish = _M_start + difference_type(__n);
380     }
381     void _M_insert_aux(iterator __position, bool __x) {
382       if (_M_finish._M_p != _M_end_of_storage) {
383         copy_backward(__position, _M_finish, _M_finish + 1);
384         *__position = __x;
385         ++_M_finish;
386       }
387       else {
388         size_type __len = size() 
389                           ? 2 * size() : static_cast<size_type>(_M_word_bit);
390         _Bit_type * __q = _M_bit_alloc(__len);
391         iterator __i = copy(begin(), __position, iterator(__q, 0));
392         *__i++ = __x;
393         _M_finish = copy(__position, end(), __i);
394         _M_deallocate();
395         _M_end_of_storage = __q + (__len + _M_word_bit - 1)/_M_word_bit;
396         _M_start = iterator(__q, 0);
397       }
398     }
399   
400     template <class _InputIterator>
401     void _M_initialize_range(_InputIterator __first, _InputIterator __last,
402                              input_iterator_tag) {
403       _M_start = iterator();
404       _M_finish = iterator();
405       _M_end_of_storage = 0;
406       for ( ; __first != __last; ++__first) 
407         push_back(*__first);
408     }
409   
410     template <class _ForwardIterator>
411     void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
412                              forward_iterator_tag) {
413       size_type __n = std::distance(__first, __last);
414       _M_initialize(__n);
415       copy(__first, __last, _M_start);
416     }
417   
418     template <class _InputIterator>
419     void _M_insert_range(iterator __pos,
420                          _InputIterator __first, _InputIterator __last,
421                          input_iterator_tag) {
422       for ( ; __first != __last; ++__first) {
423         __pos = insert(__pos, *__first);
424         ++__pos;
425       }
426     }
427   
428     template <class _ForwardIterator>
429     void _M_insert_range(iterator __position,
430                          _ForwardIterator __first, _ForwardIterator __last,
431                          forward_iterator_tag) {
432       if (__first != __last) {
433         size_type __n = std::distance(__first, __last);
434         if (capacity() - size() >= __n) {
435           copy_backward(__position, end(), _M_finish + difference_type(__n));
436           copy(__first, __last, __position);
437           _M_finish += difference_type(__n);
438         }
439         else {
440           size_type __len = size() + std::max(size(), __n);
441           _Bit_type * __q = _M_bit_alloc(__len);
442           iterator __i = copy(begin(), __position, iterator(__q, 0));
443           __i = copy(__first, __last, __i);
444           _M_finish = copy(__position, end(), __i);
445           _M_deallocate();
446           _M_end_of_storage = __q + (__len + _M_word_bit - 1)/_M_word_bit;
447           _M_start = iterator(__q, 0);
448         }
449       }
450     }      
451   
452   public:
453     iterator begin() { return _M_start; }
454     const_iterator begin() const { return _M_start; }
455     iterator end() { return _M_finish; }
456     const_iterator end() const { return _M_finish; }
457   
458     reverse_iterator rbegin() { return reverse_iterator(end()); }
459     const_reverse_iterator rbegin() const { 
460       return const_reverse_iterator(end()); 
461     }
462     reverse_iterator rend() { return reverse_iterator(begin()); }
463     const_reverse_iterator rend() const { 
464       return const_reverse_iterator(begin()); 
465     }
466   
467     size_type size() const { return size_type(end() - begin()); }
468     size_type max_size() const { return size_type(-1); }
469     size_type capacity() const {
470       return size_type(const_iterator(_M_end_of_storage, 0) - begin());
471     }
472     bool empty() const { return begin() == end(); }
473   
474     reference operator[](size_type __n)
475       { return *(begin() + difference_type(__n)); }
476     const_reference operator[](size_type __n) const
477       { return *(begin() + difference_type(__n)); }
478   
479     void _M_range_check(size_type __n) const {
480       if (__n >= this->size())
481         __throw_out_of_range("vector<bool>");
482     }
483   
484     reference at(size_type __n)
485       { _M_range_check(__n); return (*this)[__n]; }
486     const_reference at(size_type __n) const
487       { _M_range_check(__n); return (*this)[__n]; }
488   
489     explicit vector(const allocator_type& __a = allocator_type())
490       : _Bvector_base<_Alloc>(__a) {}
491   
492     vector(size_type __n, bool __value,
493               const allocator_type& __a = allocator_type())
494       : _Bvector_base<_Alloc>(__a)
495     {
496       _M_initialize(__n);
497       fill(_M_start._M_p, _M_end_of_storage, __value ? ~0 : 0);
498     }
499   
500     explicit vector(size_type __n)
501       : _Bvector_base<_Alloc>(allocator_type())
502     {
503       _M_initialize(__n);
504       fill(_M_start._M_p, _M_end_of_storage, 0);
505     }
506   
507     vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) {
508       _M_initialize(__x.size());
509       copy(__x.begin(), __x.end(), _M_start);
510     }
511   
512     // Check whether it's an integral type.  If so, it's not an iterator.
513   
514     template <class _Integer>
515     void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
516       _M_initialize(__n);
517       fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0);
518     }
519   
520     template <class _InputIterator>
521     void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
522                                 __false_type) {
523       _M_initialize_range(__first, __last, __iterator_category(__first));
524     }
525   
526     template <class _InputIterator>
527     vector(_InputIterator __first, _InputIterator __last,
528              const allocator_type& __a = allocator_type())
529       : _Bvector_base<_Alloc>(__a)
530     {
531       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
532       _M_initialize_dispatch(__first, __last, _Integral());
533     }
534       
535     ~vector() { }
536   
537     vector& operator=(const vector& __x) {
538       if (&__x == this) return *this;
539       if (__x.size() > capacity()) {
540         _M_deallocate();
541         _M_initialize(__x.size());
542       }
543       copy(__x.begin(), __x.end(), begin());
544       _M_finish = begin() + difference_type(__x.size());
545       return *this;
546     }
547   
548     // assign(), a generalized assignment member function.  Two
549     // versions: one that takes a count, and one that takes a range.
550     // The range version is a member template, so we dispatch on whether
551     // or not the type is an integer.
552   
553     void _M_fill_assign(size_t __n, bool __x) {
554       if (__n > size()) {
555         fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0);
556         insert(end(), __n - size(), __x);
557       }
558       else {
559         erase(begin() + __n, end());
560         fill(_M_start._M_p, _M_end_of_storage, __x ? ~0 : 0);
561       }
562     }
563   
564     void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }
565   
566     template <class _InputIterator>
567     void assign(_InputIterator __first, _InputIterator __last) {
568       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
569       _M_assign_dispatch(__first, __last, _Integral());
570     }
571   
572     template <class _Integer>
573     void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
574       { _M_fill_assign((size_t) __n, (bool) __val); }
575   
576     template <class _InputIter>
577     void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
578       { _M_assign_aux(__first, __last, __iterator_category(__first)); }
579   
580     template <class _InputIterator>
581     void _M_assign_aux(_InputIterator __first, _InputIterator __last,
582                        input_iterator_tag) {
583       iterator __cur = begin();
584       for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
585         *__cur = *__first;
586       if (__first == __last)
587         erase(__cur, end());
588       else
589         insert(end(), __first, __last);
590     }
591   
592     template <class _ForwardIterator>
593     void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
594                        forward_iterator_tag) {
595       size_type __len = std::distance(__first, __last);
596       if (__len < size())
597         erase(copy(__first, __last, begin()), end());
598       else {
599         _ForwardIterator __mid = __first;
600         advance(__mid, size());
601         copy(__first, __mid, begin());
602         insert(end(), __mid, __last);
603       }
604     }    
605   
606     void reserve(size_type __n) {
607       if (__n > this->max_size())
608         __throw_length_error("vector::reserve");
609       if (this->capacity() < __n) {
610         _Bit_type * __q = _M_bit_alloc(__n);
611         _M_finish = copy(begin(), end(), iterator(__q, 0));
612         _M_deallocate();
613         _M_start = iterator(__q, 0);
614         _M_end_of_storage = __q + (__n + _M_word_bit - 1)/_M_word_bit;
615       }
616     }
617   
618     reference front() { return *begin(); }
619     const_reference front() const { return *begin(); }
620     reference back() { return *(end() - 1); }
621     const_reference back() const { return *(end() - 1); }
622     void push_back(bool __x) {
623       if (_M_finish._M_p != _M_end_of_storage)
624         *_M_finish++ = __x;
625       else
626         _M_insert_aux(end(), __x);
627     }
628     void swap(vector<bool, _Alloc>& __x) {
629       std::swap(_M_start, __x._M_start);
630       std::swap(_M_finish, __x._M_finish);
631       std::swap(_M_end_of_storage, __x._M_end_of_storage);
632     }
633
634     // [23.2.5]/1, third-to-last entry in synopsis listing
635     static void swap(reference __x, reference __y) {
636       bool __tmp = __x;
637       __x = __y;
638       __y = __tmp;
639     }
640
641     iterator insert(iterator __position, bool __x = bool()) {
642       difference_type __n = __position - begin();
643       if (_M_finish._M_p != _M_end_of_storage && __position == end())
644         *_M_finish++ = __x;
645       else
646         _M_insert_aux(__position, __x);
647       return begin() + __n;
648     }
649   
650     // Check whether it's an integral type.  If so, it's not an iterator.
651   
652     template <class _Integer>
653     void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
654                             __true_type) {
655       _M_fill_insert(__pos, __n, __x);
656     }
657   
658     template <class _InputIterator>
659     void _M_insert_dispatch(iterator __pos,
660                             _InputIterator __first, _InputIterator __last,
661                             __false_type) {
662       _M_insert_range(__pos, __first, __last, __iterator_category(__first));
663     }
664   
665     template <class _InputIterator>
666     void insert(iterator __position,
667                 _InputIterator __first, _InputIterator __last) {
668       typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
669       _M_insert_dispatch(__position, __first, __last, _Integral());
670     }
671   
672     void _M_fill_insert(iterator __position, size_type __n, bool __x) {
673       if (__n == 0) return;
674       if (capacity() - size() >= __n) {
675         copy_backward(__position, end(), _M_finish + difference_type(__n));
676         fill(__position, __position + difference_type(__n), __x);
677         _M_finish += difference_type(__n);
678       }
679       else {
680         size_type __len = size() + std::max(size(), __n);
681         _Bit_type * __q = _M_bit_alloc(__len);
682         iterator __i = copy(begin(), __position, iterator(__q, 0));
683         fill_n(__i, __n, __x);
684         _M_finish = copy(__position, end(), __i + difference_type(__n));
685         _M_deallocate();
686         _M_end_of_storage = __q + (__len + _M_word_bit - 1)/_M_word_bit;
687         _M_start = iterator(__q, 0);
688       }
689     }
690   
691     void insert(iterator __position, size_type __n, bool __x) {
692       _M_fill_insert(__position, __n, __x);
693     }
694   
695     void pop_back() { --_M_finish; }
696     iterator erase(iterator __position) {
697       if (__position + 1 != end())
698         copy(__position + 1, end(), __position);
699         --_M_finish;
700       return __position;
701     }
702     iterator erase(iterator __first, iterator __last) {
703       _M_finish = copy(__last, end(), __first);
704       return __first;
705     }
706     void resize(size_type __new_size, bool __x = bool()) {
707       if (__new_size < size()) 
708         erase(begin() + difference_type(__new_size), end());
709       else
710         insert(end(), __new_size - size(), __x);
711     }
712     void flip() {
713       for (_Bit_type * __p = _M_start._M_p; __p != _M_end_of_storage; ++__p)
714         *__p = ~*__p;
715     }
716   
717     void clear() { erase(begin(), end()); }
718   };
719
720 // This typedef is non-standard.  It is provided for backward compatibility.
721 typedef vector<bool, __alloc> bit_vector;
722
723 } // namespace std 
724
725 #endif /* __GLIBCPP_INTERNAL_BVECTOR_H */
726
727 // Local Variables:
728 // mode:C++
729 // End: