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