aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/stl_bvector.h
blob: 884256151f431dd78a875bbd4e24307e0487a2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
// bit_vector and vector<bool> specialization -*- C++ -*-

// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996-1999
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 */

/** @file stl_bvector.h
 *  This is an internal header file, included by other library headers.
 *  You should not attempt to use it directly.
 */

#ifndef _BVECTOR_H
#define _BVECTOR_H 1

namespace __gnu_norm
{ 
  typedef unsigned long _Bit_type;
  enum { _S_word_bit = int(CHAR_BIT * sizeof(_Bit_type)) };

struct _Bit_reference {

  _Bit_type * _M_p;
  _Bit_type _M_mask;
  _Bit_reference(_Bit_type * __x, _Bit_type __y) 
    : _M_p(__x), _M_mask(__y) {}

public:
  _Bit_reference() : _M_p(0), _M_mask(0) {}
  operator bool() const { return !!(*_M_p & _M_mask); }
  _Bit_reference& operator=(bool __x)
  {
    if (__x)  *_M_p |= _M_mask;
    else      *_M_p &= ~_M_mask;
    return *this;
  }
  _Bit_reference& operator=(const _Bit_reference& __x) 
    { return *this = bool(__x); }
  bool operator==(const _Bit_reference& __x) const
    { return bool(*this) == bool(__x); }
  bool operator<(const _Bit_reference& __x) const
    { return !bool(*this) && bool(__x); }
  void flip() { *_M_p ^= _M_mask; }
};

struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool>
{
  _Bit_type * _M_p;
  unsigned int _M_offset;

  _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
    : _M_p(__x), _M_offset(__y) {}

  void _M_bump_up() {
    if (_M_offset++ == _S_word_bit - 1) {
      _M_offset = 0;
      ++_M_p;
    }
  }
  void _M_bump_down() {
    if (_M_offset-- == 0) {
      _M_offset = _S_word_bit - 1;
      --_M_p;
    }
  }

  void _M_incr(ptrdiff_t __i) {
    difference_type __n = __i + _M_offset;
    _M_p += __n / _S_word_bit;
    __n = __n % _S_word_bit;
    if (__n < 0) {
      _M_offset = (unsigned int) __n + _S_word_bit;
      --_M_p;
    } else
      _M_offset = (unsigned int) __n;
  }

  bool operator==(const _Bit_iterator_base& __i) const {
    return _M_p == __i._M_p && _M_offset == __i._M_offset;
  }
  bool operator<(const _Bit_iterator_base& __i) const {
    return _M_p < __i._M_p || (_M_p == __i._M_p && _M_offset < __i._M_offset);
  }
  bool operator!=(const _Bit_iterator_base& __i) const {
    return !(*this == __i);
  }
  bool operator>(const _Bit_iterator_base& __i) const {
    return __i < *this;
  }
  bool operator<=(const _Bit_iterator_base& __i) const {
    return !(__i < *this); 
  }
  bool operator>=(const _Bit_iterator_base& __i) const {
    return !(*this < __i);
  }
};

inline ptrdiff_t
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
  return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
}


struct _Bit_iterator : public _Bit_iterator_base
{
  typedef _Bit_reference  reference;
  typedef _Bit_reference* pointer;
  typedef _Bit_iterator   iterator;

  _Bit_iterator() : _Bit_iterator_base(0, 0) {}
  _Bit_iterator(_Bit_type * __x, unsigned int __y) 
    : _Bit_iterator_base(__x, __y) {}

  reference operator*() const { return reference(_M_p, 1UL << _M_offset); }
  iterator& operator++() {
    _M_bump_up();
    return *this;
  }
  iterator operator++(int) {
    iterator __tmp = *this;
    _M_bump_up();
    return __tmp;
  }
  iterator& operator--() {
    _M_bump_down();
    return *this;
  }
  iterator operator--(int) {
    iterator __tmp = *this;
    _M_bump_down();
    return __tmp;
  }
  iterator& operator+=(difference_type __i) {
    _M_incr(__i);
    return *this;
  }
  iterator& operator-=(difference_type __i) {
    *this += -__i;
    return *this;
  }
  iterator operator+(difference_type __i) const {
    iterator __tmp = *this;
    return __tmp += __i;
  }
  iterator operator-(difference_type __i) const {
    iterator __tmp = *this;
    return __tmp -= __i;
  }

  reference operator[](difference_type __i) { return *(*this + __i); }
};

inline _Bit_iterator 
operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; }


struct _Bit_const_iterator : public _Bit_iterator_base
{
  typedef bool                 reference;
  typedef bool                 const_reference;
  typedef const bool*          pointer;
  typedef _Bit_const_iterator  const_iterator;

  _Bit_const_iterator() : _Bit_iterator_base(0, 0) {}
  _Bit_const_iterator(_Bit_type * __x, unsigned int __y) 
    : _Bit_iterator_base(__x, __y) {}
  _Bit_const_iterator(const _Bit_iterator& __x) 
    : _Bit_iterator_base(__x._M_p, __x._M_offset) {}

  const_reference operator*() const {
    return _Bit_reference(_M_p, 1UL << _M_offset);
  }
  const_iterator& operator++() {
    _M_bump_up();
    return *this;
  }
  const_iterator operator++(int) {
    const_iterator __tmp = *this;
    _M_bump_up();
    return __tmp;
  }
  const_iterator& operator--() {
    _M_bump_down();
    return *this;
  }
  const_iterator operator--(int) {
    const_iterator __tmp = *this;
    _M_bump_down();
    return __tmp;
  }
  const_iterator& operator+=(difference_type __i) {
    _M_incr(__i);
    return *this;
  }
  const_iterator& operator-=(difference_type __i) {
    *this += -__i;
    return *this;
  }
  const_iterator operator+(difference_type __i) const {
    const_iterator __tmp = *this;
    return __tmp += __i;
  }
  const_iterator operator-(difference_type __i) const {
    const_iterator __tmp = *this;
    return __tmp -= __i;
  }
  const_reference operator[](difference_type __i) { 
    return *(*this + __i); 
  }
};

inline _Bit_const_iterator 
operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) { return __x + __n; }


template <class _Alloc>
class _Bvector_base
  : public _Alloc::template rebind<_Bit_type>::other
{
  typedef typename _Alloc::template rebind<_Bit_type>::other _Bit_alloc_type;
public:
  typedef _Alloc allocator_type;
  allocator_type get_allocator() const {
    return *static_cast<const _Bit_alloc_type*>(this);
  }

  _Bvector_base(const allocator_type& __a)
    : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0) { }
  ~_Bvector_base() { this->_M_deallocate(); }

protected:
  _Bit_type* _M_bit_alloc(size_t __n) {
    return _Bit_alloc_type::allocate((__n + _S_word_bit - 1)/_S_word_bit);
  }
  void _M_deallocate() {
    if (_M_start._M_p)
      _Bit_alloc_type::deallocate(_M_start._M_p, _M_end_of_storage - _M_start._M_p);
  }  

  _Bit_iterator _M_start;
  _Bit_iterator _M_finish;
  _Bit_type * _M_end_of_storage;
};

} // namespace __gnu_norm

// Declare a partial specialization of vector<T, Alloc>.
#include <bits/stl_vector.h>
namespace __gnu_norm
{
template <typename _Alloc> 
  class vector<bool, _Alloc> : public _Bvector_base<_Alloc> 
  {
  public:
    typedef bool value_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type; 
    typedef _Bit_reference reference;
    typedef bool const_reference;
    typedef _Bit_reference* pointer;
    typedef const bool* const_pointer;
  
    typedef _Bit_iterator                iterator;
    typedef _Bit_const_iterator          const_iterator;
  
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    typedef std::reverse_iterator<iterator> reverse_iterator;
  
    typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
    allocator_type get_allocator() const {
      return _Bvector_base<_Alloc>::get_allocator();
    }
  
  protected:
    using _Bvector_base<_Alloc>::_M_bit_alloc;
    using _Bvector_base<_Alloc>::_M_deallocate;
    using _Bvector_base<_Alloc>::_M_start;
    using _Bvector_base<_Alloc>::_M_finish;
    using _Bvector_base<_Alloc>::_M_end_of_storage;
  
  protected:
    void _M_initialize(size_type __n) {
      _Bit_type * __q = this->_M_bit_alloc(__n);
      this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
      this->_M_start = iterator(__q, 0);
      this->_M_finish = this->_M_start + difference_type(__n);
    }
    void _M_insert_aux(iterator __position, bool __x) {
      if (this->_M_finish._M_p != this->_M_end_of_storage) {
        std::copy_backward(__position, this->_M_finish, this->_M_finish + 1);
        *__position = __x;
        ++this->_M_finish;
      }
      else {
        size_type __len = size() 
	                  ? 2 * size() : static_cast<size_type>(_S_word_bit);
        _Bit_type * __q = this->_M_bit_alloc(__len);
        iterator __i = std::copy(begin(), __position, iterator(__q, 0));
        *__i++ = __x;
        this->_M_finish = std::copy(__position, end(), __i);
        this->_M_deallocate();
        this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
        this->_M_start = iterator(__q, 0);
      }
    }
  
    template <class _InputIterator>
    void _M_initialize_range(_InputIterator __first, _InputIterator __last,
                             input_iterator_tag) {
      this->_M_start = iterator();
      this->_M_finish = iterator();
      this->_M_end_of_storage = 0;
      for ( ; __first != __last; ++__first) 
        push_back(*__first);
    }
  
    template <class _ForwardIterator>
    void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
                             forward_iterator_tag) {
      size_type __n = std::distance(__first, __last);
      _M_initialize(__n);
      std::copy(__first, __last, this->_M_start);
    }
  
    template <class _InputIterator>
    void _M_insert_range(iterator __pos,
                         _InputIterator __first, _InputIterator __last,
                         input_iterator_tag) {
      for ( ; __first != __last; ++__first) {
        __pos = insert(__pos, *__first);
        ++__pos;
      }
    }
  
    template <class _ForwardIterator>
    void _M_insert_range(iterator __position,
                         _ForwardIterator __first, _ForwardIterator __last,
                         forward_iterator_tag) {
      if (__first != __last) {
        size_type __n = std::distance(__first, __last);
        if (capacity() - size() >= __n) {
          std::copy_backward(__position, end(),
			     this->_M_finish + difference_type(__n));
          std::copy(__first, __last, __position);
          this->_M_finish += difference_type(__n);
        }
        else {
          size_type __len = size() + std::max(size(), __n);
          _Bit_type * __q = this->_M_bit_alloc(__len);
          iterator __i = std::copy(begin(), __position, iterator(__q, 0));
          __i = std::copy(__first, __last, __i);
          this->_M_finish = std::copy(__position, end(), __i);
          this->_M_deallocate();
          this->_M_end_of_storage
	    = __q + (__len + _S_word_bit - 1)/_S_word_bit;
          this->_M_start = iterator(__q, 0);
        }
      }
    }      
  
  public:
    iterator begin() { return this->_M_start; }
    const_iterator begin() const { return this->_M_start; }
    iterator end() { return this->_M_finish; }
    const_iterator end() const { return this->_M_finish; }
  
    reverse_iterator rbegin() { return reverse_iterator(end()); }
    const_reverse_iterator rbegin() const { 
      return const_reverse_iterator(end()); 
    }
    reverse_iterator rend() { return reverse_iterator(begin()); }
    const_reverse_iterator rend() const { 
      return const_reverse_iterator(begin()); 
    }
  
    size_type size() const { return size_type(end() - begin()); }
    size_type max_size() const { return size_type(-1); }
    size_type capacity() const {
      return size_type(const_iterator(this->_M_end_of_storage, 0) - begin());
    }
    bool empty() const { return begin() == end(); }
  
    reference operator[](size_type __n)
      { return *(begin() + difference_type(__n)); }
    const_reference operator[](size_type __n) const
      { return *(begin() + difference_type(__n)); }
  
    void _M_range_check(size_type __n) const {
      if (__n >= this->size())
        __throw_out_of_range(__N("vector<bool>::_M_range_check"));
    }
  
    reference at(size_type __n)
      { _M_range_check(__n); return (*this)[__n]; }
    const_reference at(size_type __n) const
      { _M_range_check(__n); return (*this)[__n]; }
  
    explicit vector(const allocator_type& __a = allocator_type())
      : _Bvector_base<_Alloc>(__a) {}
  
    vector(size_type __n, bool __value,
              const allocator_type& __a = allocator_type())
      : _Bvector_base<_Alloc>(__a)
    {
      _M_initialize(__n);
      std::fill(this->_M_start._M_p, this->_M_end_of_storage, __value ? ~0 : 0);
    }
  
    explicit vector(size_type __n)
      : _Bvector_base<_Alloc>(allocator_type())
    {
      _M_initialize(__n);
      std::fill(this->_M_start._M_p, this->_M_end_of_storage, 0);
    }
  
    vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) {
      _M_initialize(__x.size());
      std::copy(__x.begin(), __x.end(), this->_M_start);
    }
  
    // Check whether it's an integral type.  If so, it's not an iterator.
  
    template <class _Integer>
    void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
      _M_initialize(__n);
      std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
    }
  
    template <class _InputIterator>
    void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
                                __false_type) {
      _M_initialize_range(__first, __last, std::__iterator_category(__first));
    }
  
    template <class _InputIterator>
    vector(_InputIterator __first, _InputIterator __last,
             const allocator_type& __a = allocator_type())
      : _Bvector_base<_Alloc>(__a)
    {
      typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
      _M_initialize_dispatch(__first, __last, _Integral());
    }
      
    ~vector() { }
  
    vector& operator=(const vector& __x) {
      if (&__x == this) return *this;
      if (__x.size() > capacity()) {
        this->_M_deallocate();
        _M_initialize(__x.size());
      }
      std::copy(__x.begin(), __x.end(), begin());
      this->_M_finish = begin() + difference_type(__x.size());
      return *this;
    }
  
    // assign(), a generalized assignment member function.  Two
    // versions: one that takes a count, and one that takes a range.
    // The range version is a member template, so we dispatch on whether
    // or not the type is an integer.
  
    void _M_fill_assign(size_t __n, bool __x) {
      if (__n > size()) {
        std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
        insert(end(), __n - size(), __x);
      }
      else {
        erase(begin() + __n, end());
        std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
      }
    }
  
    void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }
  
    template <class _InputIterator>
    void assign(_InputIterator __first, _InputIterator __last) {
      typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
      _M_assign_dispatch(__first, __last, _Integral());
    }
  
    template <class _Integer>
    void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
      { _M_fill_assign((size_t) __n, (bool) __val); }
  
    template <class _InputIterator>
    void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
      { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
  
    template <class _InputIterator>
    void _M_assign_aux(_InputIterator __first, _InputIterator __last,
                       input_iterator_tag) {
      iterator __cur = begin();
      for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
        *__cur = *__first;
      if (__first == __last)
        erase(__cur, end());
      else
        insert(end(), __first, __last);
    }
  
    template <class _ForwardIterator>
    void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
                       forward_iterator_tag) {
      size_type __len = std::distance(__first, __last);
      if (__len < size())
        erase(std::copy(__first, __last, begin()), end());
      else {
        _ForwardIterator __mid = __first;
        std::advance(__mid, size());
        std::copy(__first, __mid, begin());
        insert(end(), __mid, __last);
      }
    }    
  
    void reserve(size_type __n) {
      if (__n > this->max_size())
	__throw_length_error(__N("vector::reserve"));
      if (this->capacity() < __n) {
        _Bit_type * __q = this->_M_bit_alloc(__n);
        this->_M_finish = std::copy(begin(), end(), iterator(__q, 0));
        this->_M_deallocate();
        this->_M_start = iterator(__q, 0);
        this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
      }
    }
  
    reference front() { return *begin(); }
    const_reference front() const { return *begin(); }
    reference back() { return *(end() - 1); }
    const_reference back() const { return *(end() - 1); }
    void push_back(bool __x) {
      if (this->_M_finish._M_p != this->_M_end_of_storage)
        *this->_M_finish++ = __x;
      else
        _M_insert_aux(end(), __x);
    }
    void swap(vector<bool, _Alloc>& __x) {
      std::swap(this->_M_start, __x._M_start);
      std::swap(this->_M_finish, __x._M_finish);
      std::swap(this->_M_end_of_storage, __x._M_end_of_storage);
    }

    // [23.2.5]/1, third-to-last entry in synopsis listing
    static void swap(reference __x, reference __y) {
      bool __tmp = __x;
      __x = __y;
      __y = __tmp;
    }

    iterator insert(iterator __position, bool __x = bool()) {
      difference_type __n = __position - begin();
      if (this->_M_finish._M_p != this->_M_end_of_storage
	  && __position == end())
        *this->_M_finish++ = __x;
      else
        _M_insert_aux(__position, __x);
      return begin() + __n;
    }
  
    // Check whether it's an integral type.  If so, it's not an iterator.
  
    template <class _Integer>
    void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
                            __true_type) {
      _M_fill_insert(__pos, __n, __x);
    }
  
    template <class _InputIterator>
    void _M_insert_dispatch(iterator __pos,
                            _InputIterator __first, _InputIterator __last,
                            __false_type) {
      _M_insert_range(__pos, __first, __last, std::__iterator_category(__first));
    }
  
    template <class _InputIterator>
    void insert(iterator __position,
                _InputIterator __first, _InputIterator __last) {
      typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
      _M_insert_dispatch(__position, __first, __last, _Integral());
    }
  
    void _M_fill_insert(iterator __position, size_type __n, bool __x) {
      if (__n == 0) return;
      if (capacity() - size() >= __n) {
        std::copy_backward(__position, end(),
			   this->_M_finish + difference_type(__n));
        std::fill(__position, __position + difference_type(__n), __x);
        this->_M_finish += difference_type(__n);
      }
      else {
        size_type __len = size() + std::max(size(), __n);
        _Bit_type * __q = this->_M_bit_alloc(__len);
        iterator __i = std::copy(begin(), __position, iterator(__q, 0));
        std::fill_n(__i, __n, __x);
        this->_M_finish = std::copy(__position, end(), __i + difference_type(__n));
        this->_M_deallocate();
        this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
        this->_M_start = iterator(__q, 0);
      }
    }
  
    void insert(iterator __position, size_type __n, bool __x) {
      _M_fill_insert(__position, __n, __x);
    }
  
    void pop_back() { --this->_M_finish; }
    iterator erase(iterator __position) {
      if (__position + 1 != end())
        std::copy(__position + 1, end(), __position);
        --this->_M_finish;
      return __position;
    }
    iterator erase(iterator __first, iterator __last) {
      this->_M_finish = std::copy(__last, end(), __first);
      return __first;
    }
    void resize(size_type __new_size, bool __x = bool()) {
      if (__new_size < size()) 
        erase(begin() + difference_type(__new_size), end());
      else
        insert(end(), __new_size - size(), __x);
    }
    void flip() {
      for (_Bit_type * __p = this->_M_start._M_p;
	   __p != this->_M_end_of_storage;
	   ++__p)
        *__p = ~*__p;
    }
  
    void clear() { erase(begin(), end()); }
  };

  // This typedef is non-standard.  It is provided for backward compatibility.
  typedef vector<bool, __alloc> bit_vector;
} // namespace __gnu_norm

#endif /* _BVECTOR_H */