Import of virgin gcc 4.0.0 distribution.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / bits / stl_bvector.h
1 // vector<bool> specialization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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 _BVECTOR_H
62 #define _BVECTOR_H 1
63
64 namespace _GLIBCXX_STD
65 {
66   typedef unsigned long _Bit_type;
67   enum { _S_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
74     _Bit_reference(_Bit_type * __x, _Bit_type __y)
75     : _M_p(__x), _M_mask(__y) { }
76
77     _Bit_reference() : _M_p(0), _M_mask(0) { }
78
79     operator bool() const
80     { return !!(*_M_p & _M_mask); }
81
82     _Bit_reference&
83     operator=(bool __x)
84     {
85       if (__x)
86         *_M_p |= _M_mask;
87       else
88         *_M_p &= ~_M_mask;
89       return *this;
90     }
91
92     _Bit_reference&
93     operator=(const _Bit_reference& __x)
94     { return *this = bool(__x); }
95
96     bool
97     operator==(const _Bit_reference& __x) const
98     { return bool(*this) == bool(__x); }
99
100     bool
101     operator<(const _Bit_reference& __x) const
102     { return !bool(*this) && bool(__x); }
103
104     void
105     flip()
106     { *_M_p ^= _M_mask; }
107   };
108
109   struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool>
110   {
111     _Bit_type * _M_p;
112     unsigned int _M_offset;
113
114     _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
115     : _M_p(__x), _M_offset(__y) { }
116
117     void
118     _M_bump_up()
119     {
120       if (_M_offset++ == _S_word_bit - 1)
121         {
122           _M_offset = 0;
123           ++_M_p;
124         }
125     }
126
127     void
128     _M_bump_down()
129     {
130       if (_M_offset-- == 0)
131         {
132           _M_offset = _S_word_bit - 1;
133           --_M_p;
134         }
135     }
136
137     void
138     _M_incr(ptrdiff_t __i)
139     {
140       difference_type __n = __i + _M_offset;
141       _M_p += __n / _S_word_bit;
142       __n = __n % _S_word_bit;
143       if (__n < 0)
144         {
145           _M_offset = static_cast<unsigned int>(__n + _S_word_bit);
146           --_M_p;
147         }
148       else
149         _M_offset = static_cast<unsigned int>(__n);
150     }
151
152     bool
153     operator==(const _Bit_iterator_base& __i) const
154     { return _M_p == __i._M_p && _M_offset == __i._M_offset; }
155
156     bool
157     operator<(const _Bit_iterator_base& __i) const
158     {
159       return _M_p < __i._M_p
160              || (_M_p == __i._M_p && _M_offset < __i._M_offset);
161     }
162
163     bool
164     operator!=(const _Bit_iterator_base& __i) const
165     { return !(*this == __i); }
166
167     bool
168     operator>(const _Bit_iterator_base& __i) const
169     { return __i < *this; }
170
171     bool
172     operator<=(const _Bit_iterator_base& __i) const
173     { return !(__i < *this); }
174
175     bool
176     operator>=(const _Bit_iterator_base& __i) const
177     { return !(*this < __i); }
178   };
179
180   inline ptrdiff_t
181   operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
182   {
183     return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
184   }
185
186   struct _Bit_iterator : public _Bit_iterator_base
187   {
188     typedef _Bit_reference  reference;
189     typedef _Bit_reference* pointer;
190     typedef _Bit_iterator   iterator;
191
192     _Bit_iterator() : _Bit_iterator_base(0, 0) { }
193
194     _Bit_iterator(_Bit_type * __x, unsigned int __y)
195     : _Bit_iterator_base(__x, __y) { }
196
197     reference
198     operator*() const
199     { return reference(_M_p, 1UL << _M_offset); }
200
201     iterator&
202     operator++()
203     {
204       _M_bump_up();
205       return *this;
206     }
207
208     iterator
209     operator++(int)
210     {
211       iterator __tmp = *this;
212       _M_bump_up();
213       return __tmp;
214     }
215
216     iterator&
217     operator--()
218     {
219       _M_bump_down();
220       return *this;
221     }
222
223     iterator
224     operator--(int)
225     {
226       iterator __tmp = *this;
227       _M_bump_down();
228       return __tmp;
229     }
230
231     iterator&
232     operator+=(difference_type __i)
233     {
234       _M_incr(__i);
235       return *this;
236     }
237
238     iterator&
239     operator-=(difference_type __i)
240     {
241       *this += -__i;
242       return *this;
243     }
244
245     iterator
246     operator+(difference_type __i) const
247     {
248       iterator __tmp = *this;
249       return __tmp += __i;
250     }
251
252     iterator
253     operator-(difference_type __i) const
254     {
255       iterator __tmp = *this;
256       return __tmp -= __i;
257     }
258
259     reference
260     operator[](difference_type __i)
261     { return *(*this + __i); }
262   };
263
264   inline _Bit_iterator
265   operator+(ptrdiff_t __n, const _Bit_iterator& __x)
266   { return __x + __n; }
267
268   struct _Bit_const_iterator : public _Bit_iterator_base
269   {
270     typedef bool                 reference;
271     typedef bool                 const_reference;
272     typedef const bool*          pointer;
273     typedef _Bit_const_iterator  const_iterator;
274
275     _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
276
277     _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
278     : _Bit_iterator_base(__x, __y) { }
279
280     _Bit_const_iterator(const _Bit_iterator& __x)
281     : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
282
283     const_reference
284     operator*() const
285     { return _Bit_reference(_M_p, 1UL << _M_offset); }
286
287     const_iterator&
288     operator++()
289     {
290       _M_bump_up();
291       return *this;
292     }
293
294     const_iterator
295     operator++(int)
296     {
297       const_iterator __tmp = *this;
298       _M_bump_up();
299       return __tmp;
300     }
301
302     const_iterator&
303     operator--()
304     {
305       _M_bump_down();
306       return *this;
307     }
308
309     const_iterator
310     operator--(int)
311     {
312       const_iterator __tmp = *this;
313       _M_bump_down();
314       return __tmp;
315     }
316
317     const_iterator&
318     operator+=(difference_type __i)
319     {
320       _M_incr(__i);
321       return *this;
322     }
323
324     const_iterator&
325     operator-=(difference_type __i)
326     {
327       *this += -__i;
328       return *this;
329     }
330
331     const_iterator 
332     operator+(difference_type __i) const
333     {
334       const_iterator __tmp = *this;
335       return __tmp += __i;
336     }
337
338     const_iterator
339     operator-(difference_type __i) const
340     {
341       const_iterator __tmp = *this;
342       return __tmp -= __i;
343     }
344
345     const_reference
346     operator[](difference_type __i)
347     { return *(*this + __i); }
348   };
349
350   inline _Bit_const_iterator
351   operator+(ptrdiff_t __n, const _Bit_const_iterator& __x)
352   { return __x + __n; }
353
354   template<class _Alloc>
355     class _Bvector_base
356     {
357       typedef typename _Alloc::template rebind<_Bit_type>::other
358         _Bit_alloc_type;
359       
360       struct _Bvector_impl : public _Bit_alloc_type
361       {
362         _Bit_iterator   _M_start;
363         _Bit_iterator   _M_finish;
364         _Bit_type*      _M_end_of_storage;
365         _Bvector_impl(const _Bit_alloc_type& __a)
366         : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
367         { }
368       };
369
370     public:
371       typedef _Alloc allocator_type;
372
373       allocator_type
374       get_allocator() const
375       { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); }
376
377       _Bvector_base(const allocator_type& __a) : _M_impl(__a) { }
378
379       ~_Bvector_base()
380       { this->_M_deallocate(); }
381
382     protected:
383       _Bvector_impl _M_impl;
384
385       _Bit_type*
386       _M_allocate(size_t __n)
387       { return _M_impl.allocate((__n + _S_word_bit - 1) / _S_word_bit); }
388
389       void
390       _M_deallocate()
391       {
392         if (_M_impl._M_start._M_p)
393           _M_impl.deallocate(_M_impl._M_start._M_p,
394                              _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
395       }
396     };
397 } // namespace std
398
399 // Declare a partial specialization of vector<T, Alloc>.
400 #include <bits/stl_vector.h>
401
402 namespace _GLIBCXX_STD
403 {
404   /**
405    *  @brief  A specialization of vector for booleans which offers fixed time
406    *  access to individual elements in any order.
407    *
408    *  Note that vector<bool> does not actually meet the requirements for being
409    *  a container.  This is because the reference and pointer types are not
410    *  really references and pointers to bool.  See DR96 for details.  @see
411    *  vector for function documentation.
412    *
413    *  @ingroup Containers
414    *  @ingroup Sequences
415    *
416    *  In some terminology a %vector can be described as a dynamic
417    *  C-style array, it offers fast and efficient access to individual
418    *  elements in any order and saves the user from worrying about
419    *  memory and size allocation.  Subscripting ( @c [] ) access is
420    *  also provided as with C-style arrays.
421   */
422 template<typename _Alloc>
423   class vector<bool, _Alloc> : public _Bvector_base<_Alloc>
424   {
425   public:
426     typedef bool value_type;
427     typedef size_t size_type;
428     typedef ptrdiff_t difference_type;
429     typedef _Bit_reference reference;
430     typedef bool const_reference;
431     typedef _Bit_reference* pointer;
432     typedef const bool* const_pointer;
433
434     typedef _Bit_iterator                iterator;
435     typedef _Bit_const_iterator          const_iterator;
436
437     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
438     typedef std::reverse_iterator<iterator> reverse_iterator;
439
440     typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
441
442     allocator_type get_allocator() const
443     { return _Bvector_base<_Alloc>::get_allocator(); }
444
445   protected:
446     using _Bvector_base<_Alloc>::_M_allocate;
447     using _Bvector_base<_Alloc>::_M_deallocate;
448
449   protected:
450     void
451     _M_initialize(size_type __n)
452     {
453       _Bit_type* __q = this->_M_allocate(__n);
454       this->_M_impl._M_end_of_storage = (__q
455                                          + ((__n + _S_word_bit - 1)
456                                             / _S_word_bit));
457       this->_M_impl._M_start = iterator(__q, 0);
458       this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
459     }
460
461     void
462     _M_insert_aux(iterator __position, bool __x)
463     {
464       if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
465         {
466           std::copy_backward(__position, this->_M_impl._M_finish, 
467                              this->_M_impl._M_finish + 1);
468           *__position = __x;
469           ++this->_M_impl._M_finish;
470         }
471       else
472         {
473           const size_type __len = size() ? 2 * size()
474                                          : static_cast<size_type>(_S_word_bit);
475           _Bit_type * __q = this->_M_allocate(__len);
476           iterator __i = std::copy(begin(), __position, iterator(__q, 0));
477           *__i++ = __x;
478           this->_M_impl._M_finish = std::copy(__position, end(), __i);
479           this->_M_deallocate();
480           this->_M_impl._M_end_of_storage = (__q + ((__len + _S_word_bit - 1)
481                                                     / _S_word_bit));
482           this->_M_impl._M_start = iterator(__q, 0);
483         }
484     }
485
486     template<class _InputIterator>
487       void
488       _M_initialize_range(_InputIterator __first, _InputIterator __last,
489                           input_iterator_tag)
490       {
491         this->_M_impl._M_start = iterator();
492         this->_M_impl._M_finish = iterator();
493         this->_M_impl._M_end_of_storage = 0;
494         for (; __first != __last; ++__first)
495           push_back(*__first);
496       }
497
498     template<class _ForwardIterator>
499       void
500       _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
501                           forward_iterator_tag)
502       {
503         const size_type __n = std::distance(__first, __last);
504         _M_initialize(__n);
505         std::copy(__first, __last, this->_M_impl._M_start);
506       }
507
508     template<class _InputIterator>
509       void
510       _M_insert_range(iterator __pos, _InputIterator __first, 
511                       _InputIterator __last, input_iterator_tag)
512       {
513         for (; __first != __last; ++__first)
514           {
515             __pos = insert(__pos, *__first);
516             ++__pos;
517           }
518       }
519
520     template<class _ForwardIterator>
521       void
522       _M_insert_range(iterator __position, _ForwardIterator __first, 
523                       _ForwardIterator __last, forward_iterator_tag)
524       {
525         if (__first != __last)
526           {
527             size_type __n = std::distance(__first, __last);
528             if (capacity() - size() >= __n)
529               {
530                 std::copy_backward(__position, end(),
531                                    this->_M_impl._M_finish
532                                    + difference_type(__n));
533                 std::copy(__first, __last, __position);
534                 this->_M_impl._M_finish += difference_type(__n);
535               }
536             else
537               {
538                 const size_type __len = size() + std::max(size(), __n);
539                 _Bit_type * __q = this->_M_allocate(__len);
540                 iterator __i = std::copy(begin(), __position,
541                                          iterator(__q, 0));
542                 __i = std::copy(__first, __last, __i);
543                 this->_M_impl._M_finish = std::copy(__position, end(), __i);
544                 this->_M_deallocate();
545                 this->_M_impl._M_end_of_storage = (__q
546                                                    + ((__len + _S_word_bit - 1)
547                                                       / _S_word_bit));
548                 this->_M_impl._M_start = iterator(__q, 0);
549               }
550           }
551       }
552
553   public:
554     iterator
555     begin()
556     { return this->_M_impl._M_start; }
557
558     const_iterator
559     begin() const
560     { return this->_M_impl._M_start; }
561
562     iterator
563     end()
564     { return this->_M_impl._M_finish; }
565
566     const_iterator
567     end() const
568     { return this->_M_impl._M_finish; }
569
570     reverse_iterator
571     rbegin()
572     { return reverse_iterator(end()); }
573
574     const_reverse_iterator
575     rbegin() const
576     { return const_reverse_iterator(end()); }
577
578     reverse_iterator
579     rend()
580     { return reverse_iterator(begin()); }
581
582     const_reverse_iterator
583     rend() const
584     { return const_reverse_iterator(begin()); }
585
586     size_type
587     size() const
588     { return size_type(end() - begin()); }
589
590     size_type
591     max_size() const
592     { return size_type(-1); }
593
594     size_type
595     capacity() const
596     { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
597                        - begin()); }
598     bool
599     empty() const
600     { return begin() == end(); }
601
602     reference
603     operator[](size_type __n)
604     { return *(begin() + difference_type(__n)); }
605
606     const_reference
607     operator[](size_type __n) const
608     { return *(begin() + difference_type(__n)); }
609
610     void
611     _M_range_check(size_type __n) const
612     {
613       if (__n >= this->size())
614         __throw_out_of_range(__N("vector<bool>::_M_range_check"));
615     }
616
617     reference
618     at(size_type __n)
619     { _M_range_check(__n); return (*this)[__n]; }
620
621     const_reference
622     at(size_type __n) const
623     { _M_range_check(__n); return (*this)[__n]; }
624
625     explicit
626     vector(const allocator_type& __a = allocator_type())
627     : _Bvector_base<_Alloc>(__a) { }
628
629     vector(size_type __n, bool __value, 
630            const allocator_type& __a = allocator_type())
631     : _Bvector_base<_Alloc>(__a)
632     {
633       _M_initialize(__n);
634       std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage, 
635                 __value ? ~0 : 0);
636     }
637
638     explicit
639     vector(size_type __n)
640     : _Bvector_base<_Alloc>(allocator_type())
641     {
642       _M_initialize(__n);
643       std::fill(this->_M_impl._M_start._M_p, 
644                 this->_M_impl._M_end_of_storage, 0);
645     }
646
647     vector(const vector& __x)
648     : _Bvector_base<_Alloc>(__x.get_allocator())
649     {
650       _M_initialize(__x.size());
651       std::copy(__x.begin(), __x.end(), this->_M_impl._M_start);
652     }
653
654     // Check whether it's an integral type.  If so, it's not an iterator.
655     template<class _Integer>
656       void
657       _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
658       {
659         _M_initialize(__n);
660         std::fill(this->_M_impl._M_start._M_p, 
661                   this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
662       }
663
664     template<class _InputIterator>
665       void 
666       _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
667                              __false_type)
668       { _M_initialize_range(__first, __last, 
669                             std::__iterator_category(__first)); }
670
671     template<class _InputIterator>
672       vector(_InputIterator __first, _InputIterator __last,
673              const allocator_type& __a = allocator_type())
674       : _Bvector_base<_Alloc>(__a)
675       {
676         typedef typename std::__is_integer<_InputIterator>::__type _Integral;
677         _M_initialize_dispatch(__first, __last, _Integral());
678       }
679
680     ~vector() { }
681
682     vector&
683     operator=(const vector& __x)
684     {
685       if (&__x == this)
686         return *this;
687       if (__x.size() > capacity())
688         {
689           this->_M_deallocate();
690           _M_initialize(__x.size());
691         }
692       std::copy(__x.begin(), __x.end(), begin());
693       this->_M_impl._M_finish = begin() + difference_type(__x.size());
694       return *this;
695     }
696
697     // assign(), a generalized assignment member function.  Two
698     // versions: one that takes a count, and one that takes a range.
699     // The range version is a member template, so we dispatch on whether
700     // or not the type is an integer.
701
702     void
703     _M_fill_assign(size_t __n, bool __x)
704     {
705       if (__n > size())
706         {
707           std::fill(this->_M_impl._M_start._M_p, 
708                     this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
709           insert(end(), __n - size(), __x);
710         }
711       else
712         {
713           erase(begin() + __n, end());
714           std::fill(this->_M_impl._M_start._M_p, 
715                     this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
716         }
717     }
718
719     void
720     assign(size_t __n, bool __x)
721     { _M_fill_assign(__n, __x); }
722
723     template<class _InputIterator>
724       void
725       assign(_InputIterator __first, _InputIterator __last)
726       {
727         typedef typename std::__is_integer<_InputIterator>::__type _Integral;
728         _M_assign_dispatch(__first, __last, _Integral());
729       }
730
731     template<class _Integer>
732       void
733       _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
734       { _M_fill_assign((size_t) __n, (bool) __val); }
735
736     template<class _InputIterator>
737       void
738       _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
739                          __false_type)
740       { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
741
742     template<class _InputIterator>
743       void
744       _M_assign_aux(_InputIterator __first, _InputIterator __last,
745                     input_iterator_tag)
746       {
747         iterator __cur = begin();
748         for (; __first != __last && __cur != end(); ++__cur, ++__first)
749           *__cur = *__first;
750         if (__first == __last)
751           erase(__cur, end());
752         else
753           insert(end(), __first, __last);
754       }
755     
756     template<class _ForwardIterator>
757       void
758       _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
759                     forward_iterator_tag)
760       {
761         const size_type __len = std::distance(__first, __last);
762         if (__len < size())
763           erase(std::copy(__first, __last, begin()), end());
764         else
765           {
766             _ForwardIterator __mid = __first;
767             std::advance(__mid, size());
768             std::copy(__first, __mid, begin());
769             insert(end(), __mid, __last);
770           }
771       }
772
773     void
774     reserve(size_type __n)
775     {
776       if (__n > this->max_size())
777         __throw_length_error(__N("vector::reserve"));
778       if (this->capacity() < __n)
779         {
780           _Bit_type* __q = this->_M_allocate(__n);
781           this->_M_impl._M_finish = std::copy(begin(), end(), 
782                                               iterator(__q, 0));
783           this->_M_deallocate();
784           this->_M_impl._M_start = iterator(__q, 0);
785           this->_M_impl._M_end_of_storage = (__q + (__n + _S_word_bit - 1)
786                                              / _S_word_bit);
787         }
788     }
789
790     reference
791     front()
792     { return *begin(); }
793
794     const_reference
795     front() const
796     { return *begin(); }
797
798     reference
799     back()
800     { return *(end() - 1); }
801
802     const_reference
803     back() const
804     { return *(end() - 1); }
805
806     void
807     push_back(bool __x)
808     {
809       if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
810         *this->_M_impl._M_finish++ = __x;
811       else
812         _M_insert_aux(end(), __x);
813     }
814
815     void
816     swap(vector<bool, _Alloc>& __x)
817     {
818       std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
819       std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
820       std::swap(this->_M_impl._M_end_of_storage, 
821                 __x._M_impl._M_end_of_storage);
822     }
823
824     // [23.2.5]/1, third-to-last entry in synopsis listing
825     static void
826     swap(reference __x, reference __y)
827     {
828       bool __tmp = __x;
829       __x = __y;
830       __y = __tmp;
831     }
832
833     iterator
834     insert(iterator __position, bool __x = bool())
835     {
836       const difference_type __n = __position - begin();
837       if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
838           && __position == end())
839         *this->_M_impl._M_finish++ = __x;
840       else
841         _M_insert_aux(__position, __x);
842       return begin() + __n;
843     }
844
845     // Check whether it's an integral type.  If so, it's not an iterator.
846
847     template<class _Integer>
848       void
849       _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
850                          __true_type)
851       { _M_fill_insert(__pos, __n, __x); }
852
853     template<class _InputIterator>
854       void
855       _M_insert_dispatch(iterator __pos,
856                          _InputIterator __first, _InputIterator __last,
857                          __false_type)
858       { _M_insert_range(__pos, __first, __last,
859                         std::__iterator_category(__first)); }
860
861     template<class _InputIterator>
862       void
863       insert(iterator __position,
864              _InputIterator __first, _InputIterator __last)
865       {
866         typedef typename std::__is_integer<_InputIterator>::__type _Integral;
867         _M_insert_dispatch(__position, __first, __last, _Integral());
868       }
869
870     void
871     _M_fill_insert(iterator __position, size_type __n, bool __x)
872     {
873       if (__n == 0)
874         return;
875       if (capacity() - size() >= __n)
876         {
877           std::copy_backward(__position, end(),
878                              this->_M_impl._M_finish + difference_type(__n));
879           std::fill(__position, __position + difference_type(__n), __x);
880           this->_M_impl._M_finish += difference_type(__n);
881         }
882       else
883         {
884           const size_type __len = size() + std::max(size(), __n);
885           _Bit_type * __q = this->_M_allocate(__len);
886           iterator __i = std::copy(begin(), __position, iterator(__q, 0));
887           std::fill_n(__i, __n, __x);
888           this->_M_impl._M_finish = std::copy(__position, end(),
889                                               __i + difference_type(__n));
890           this->_M_deallocate();
891           this->_M_impl._M_end_of_storage = (__q + ((__len + _S_word_bit - 1)
892                                                     / _S_word_bit));
893           this->_M_impl._M_start = iterator(__q, 0);
894         }
895     }
896
897     void
898     insert(iterator __position, size_type __n, bool __x)
899     { _M_fill_insert(__position, __n, __x); }
900
901     void
902     pop_back()
903     { --this->_M_impl._M_finish; }
904
905     iterator
906     erase(iterator __position)
907     {
908       if (__position + 1 != end())
909         std::copy(__position + 1, end(), __position);
910       --this->_M_impl._M_finish;
911       return __position;
912     }
913
914     iterator
915     erase(iterator __first, iterator __last)
916     {
917       this->_M_impl._M_finish = std::copy(__last, end(), __first);
918       return __first;
919     }
920
921     void
922     resize(size_type __new_size, bool __x = bool())
923     {
924       if (__new_size < size())
925         erase(begin() + difference_type(__new_size), end());
926       else
927         insert(end(), __new_size - size(), __x);
928     }
929
930     void
931     flip()
932     {
933       for (_Bit_type * __p = this->_M_impl._M_start._M_p;
934            __p != this->_M_impl._M_end_of_storage; ++__p)
935         *__p = ~*__p;
936     }
937
938     void
939     clear()
940     { erase(begin(), end()); }
941   };
942 } // namespace std
943
944 #endif