basic_string.h: Trivial formatting fixes and/or const-ification of some variables.
[gcc.git] / libstdc++-v3 / include / bits / stl_bvector.h
1 // 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 _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 { 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 struct _Bit_iterator : public _Bit_iterator_base
185 {
186 typedef _Bit_reference reference;
187 typedef _Bit_reference* pointer;
188 typedef _Bit_iterator iterator;
189
190 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
191 _Bit_iterator(_Bit_type * __x, unsigned int __y)
192 : _Bit_iterator_base(__x, __y) { }
193
194 reference
195 operator*() const { return reference(_M_p, 1UL << _M_offset); }
196
197 iterator&
198 operator++()
199 {
200 _M_bump_up();
201 return *this;
202 }
203
204 iterator
205 operator++(int)
206 {
207 iterator __tmp = *this;
208 _M_bump_up();
209 return __tmp;
210 }
211
212 iterator&
213 operator--()
214 {
215 _M_bump_down();
216 return *this;
217 }
218
219 iterator
220 operator--(int)
221 {
222 iterator __tmp = *this;
223 _M_bump_down();
224 return __tmp;
225 }
226
227 iterator&
228 operator+=(difference_type __i)
229 {
230 _M_incr(__i);
231 return *this;
232 }
233
234 iterator&
235 operator-=(difference_type __i)
236 {
237 *this += -__i;
238 return *this;
239 }
240
241 iterator
242 operator+(difference_type __i) const
243 {
244 iterator __tmp = *this;
245 return __tmp += __i;
246 }
247
248 iterator
249 operator-(difference_type __i) const
250 {
251 iterator __tmp = *this;
252 return __tmp -= __i;
253 }
254
255 reference
256 operator[](difference_type __i)
257 { 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
326 operator+(difference_type __i) const
327 {
328 const_iterator __tmp = *this;
329 return __tmp += __i;
330 }
331
332 const_iterator
333 operator-(difference_type __i) const
334 {
335 const_iterator __tmp = *this;
336 return __tmp -= __i;
337 }
338
339 const_reference
340 operator[](difference_type __i)
341 { return *(*this + __i); }
342 };
343
344 inline _Bit_const_iterator
345 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x)
346 { return __x + __n; }
347
348 template<class _Alloc>
349 class _Bvector_base
350 {
351 typedef typename _Alloc::template rebind<_Bit_type>::other
352 _Bit_alloc_type;
353
354 struct _Bvector_impl : public _Bit_alloc_type
355 {
356 _Bit_iterator _M_start;
357 _Bit_iterator _M_finish;
358 _Bit_type* _M_end_of_storage;
359 _Bvector_impl(const _Bit_alloc_type& __a)
360 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
361 { }
362 };
363
364 public:
365 typedef _Alloc allocator_type;
366
367 allocator_type
368 get_allocator() const
369 { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); }
370
371 _Bvector_base(const allocator_type& __a) : _M_impl(__a) { }
372
373 ~_Bvector_base() { this->_M_deallocate(); }
374
375 protected:
376 _Bvector_impl _M_impl;
377
378 _Bit_type*
379 _M_allocate(size_t __n)
380 { return _M_impl.allocate((__n + _S_word_bit - 1) / _S_word_bit); }
381
382 void
383 _M_deallocate()
384 {
385 if (_M_impl._M_start._M_p)
386 _M_impl.deallocate(_M_impl._M_start._M_p,
387 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
388 }
389 };
390 } // namespace std
391
392 // Declare a partial specialization of vector<T, Alloc>.
393 #include <bits/stl_vector.h>
394
395 namespace _GLIBCXX_STD
396 {
397 /**
398 * @brief A specialization of vector for booleans which offers fixed time
399 * access to individual elements in any order.
400 *
401 * Note that vector<bool> does not actually meet the requirements for being
402 * a container. This is because the reference and pointer types are not
403 * really references and pointers to bool. See DR96 for details. @see
404 * vector for function documentation.
405 *
406 * @ingroup Containers
407 * @ingroup Sequences
408 *
409 * In some terminology a %vector can be described as a dynamic
410 * C-style array, it offers fast and efficient access to individual
411 * elements in any order and saves the user from worrying about
412 * memory and size allocation. Subscripting ( @c [] ) access is
413 * also provided as with C-style arrays.
414 */
415 template<typename _Alloc>
416 class vector<bool, _Alloc> : public _Bvector_base<_Alloc>
417 {
418 public:
419 typedef bool value_type;
420 typedef size_t size_type;
421 typedef ptrdiff_t difference_type;
422 typedef _Bit_reference reference;
423 typedef bool const_reference;
424 typedef _Bit_reference* pointer;
425 typedef const bool* const_pointer;
426
427 typedef _Bit_iterator iterator;
428 typedef _Bit_const_iterator const_iterator;
429
430 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
431 typedef std::reverse_iterator<iterator> reverse_iterator;
432
433 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
434
435 allocator_type get_allocator() const
436 { return _Bvector_base<_Alloc>::get_allocator(); }
437
438 protected:
439 using _Bvector_base<_Alloc>::_M_allocate;
440 using _Bvector_base<_Alloc>::_M_deallocate;
441
442 protected:
443 void _M_initialize(size_type __n)
444 {
445 _Bit_type* __q = this->_M_allocate(__n);
446 this->_M_impl._M_end_of_storage = __q
447 + (__n + _S_word_bit - 1) / _S_word_bit;
448 this->_M_impl._M_start = iterator(__q, 0);
449 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
450 }
451
452 void _M_insert_aux(iterator __position, bool __x)
453 {
454 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
455 {
456 std::copy_backward(__position, this->_M_impl._M_finish,
457 this->_M_impl._M_finish + 1);
458 *__position = __x;
459 ++this->_M_impl._M_finish;
460 }
461 else
462 {
463 const size_type __len = size() ? 2 * size()
464 : static_cast<size_type>(_S_word_bit);
465 _Bit_type * __q = this->_M_allocate(__len);
466 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
467 *__i++ = __x;
468 this->_M_impl._M_finish = std::copy(__position, end(), __i);
469 this->_M_deallocate();
470 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1)
471 / _S_word_bit;
472 this->_M_impl._M_start = iterator(__q, 0);
473 }
474 }
475
476 template<class _InputIterator>
477 void _M_initialize_range(_InputIterator __first, _InputIterator __last,
478 input_iterator_tag)
479 {
480 this->_M_impl._M_start = iterator();
481 this->_M_impl._M_finish = iterator();
482 this->_M_impl._M_end_of_storage = 0;
483 for ( ; __first != __last; ++__first)
484 push_back(*__first);
485 }
486
487 template<class _ForwardIterator>
488 void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
489 forward_iterator_tag)
490 {
491 const size_type __n = std::distance(__first, __last);
492 _M_initialize(__n);
493 std::copy(__first, __last, this->_M_impl._M_start);
494 }
495
496 template<class _InputIterator>
497 void _M_insert_range(iterator __pos, _InputIterator __first,
498 _InputIterator __last, input_iterator_tag)
499 {
500 for ( ; __first != __last; ++__first)
501 {
502 __pos = insert(__pos, *__first);
503 ++__pos;
504 }
505 }
506
507 template<class _ForwardIterator>
508 void _M_insert_range(iterator __position, _ForwardIterator __first,
509 _ForwardIterator __last, forward_iterator_tag)
510 {
511 if (__first != __last)
512 {
513 size_type __n = std::distance(__first, __last);
514 if (capacity() - size() >= __n)
515 {
516 std::copy_backward(__position, end(),
517 this->_M_impl._M_finish + difference_type(__n));
518 std::copy(__first, __last, __position);
519 this->_M_impl._M_finish += difference_type(__n);
520 }
521 else
522 {
523 const size_type __len = size() + std::max(size(), __n);
524 _Bit_type * __q = this->_M_allocate(__len);
525 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
526 __i = std::copy(__first, __last, __i);
527 this->_M_impl._M_finish = std::copy(__position, end(), __i);
528 this->_M_deallocate();
529 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1)
530 / _S_word_bit;
531 this->_M_impl._M_start = iterator(__q, 0);
532 }
533 }
534 }
535
536 public:
537 iterator begin()
538 { return this->_M_impl._M_start; }
539
540 const_iterator begin() const
541 { return this->_M_impl._M_start; }
542
543 iterator end()
544 { return this->_M_impl._M_finish; }
545
546 const_iterator end() const
547 { return this->_M_impl._M_finish; }
548
549 reverse_iterator rbegin()
550 { return reverse_iterator(end()); }
551
552 const_reverse_iterator rbegin() const
553 { return const_reverse_iterator(end()); }
554
555 reverse_iterator rend()
556 { return reverse_iterator(begin()); }
557
558 const_reverse_iterator rend() const
559 { return const_reverse_iterator(begin()); }
560
561 size_type size() const
562 { return size_type(end() - begin()); }
563
564 size_type max_size() const
565 { return size_type(-1); }
566
567 size_type capacity() const
568 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
569 - begin()); }
570 bool empty() const
571 { return begin() == end(); }
572
573 reference operator[](size_type __n)
574 { return *(begin() + difference_type(__n)); }
575
576 const_reference operator[](size_type __n) const
577 { return *(begin() + difference_type(__n)); }
578
579 void _M_range_check(size_type __n) const
580 {
581 if (__n >= this->size())
582 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
583 }
584
585 reference at(size_type __n)
586 { _M_range_check(__n); return (*this)[__n]; }
587
588 const_reference at(size_type __n) const
589 { _M_range_check(__n); return (*this)[__n]; }
590
591 explicit vector(const allocator_type& __a = allocator_type())
592 : _Bvector_base<_Alloc>(__a) { }
593
594 vector(size_type __n, bool __value,
595 const allocator_type& __a = allocator_type())
596 : _Bvector_base<_Alloc>(__a)
597 {
598 _M_initialize(__n);
599 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
600 __value ? ~0 : 0);
601 }
602
603 explicit vector(size_type __n)
604 : _Bvector_base<_Alloc>(allocator_type())
605 {
606 _M_initialize(__n);
607 std::fill(this->_M_impl._M_start._M_p,
608 this->_M_impl._M_end_of_storage, 0);
609 }
610
611 vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator())
612 {
613 _M_initialize(__x.size());
614 std::copy(__x.begin(), __x.end(), this->_M_impl._M_start);
615 }
616
617 // Check whether it's an integral type. If so, it's not an iterator.
618 template<class _Integer>
619 void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
620 {
621 _M_initialize(__n);
622 std::fill(this->_M_impl._M_start._M_p,
623 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
624 }
625
626 template<class _InputIterator>
627 void
628 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
629 __false_type)
630 { _M_initialize_range(__first, __last,
631 std::__iterator_category(__first)); }
632
633 template<class _InputIterator>
634 vector(_InputIterator __first, _InputIterator __last,
635 const allocator_type& __a = allocator_type())
636 : _Bvector_base<_Alloc>(__a)
637 {
638 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
639 _M_initialize_dispatch(__first, __last, _Integral());
640 }
641
642 ~vector() { }
643
644 vector& operator=(const vector& __x)
645 {
646 if (&__x == this)
647 return *this;
648 if (__x.size() > capacity())
649 {
650 this->_M_deallocate();
651 _M_initialize(__x.size());
652 }
653 std::copy(__x.begin(), __x.end(), begin());
654 this->_M_impl._M_finish = begin() + difference_type(__x.size());
655 return *this;
656 }
657
658 // assign(), a generalized assignment member function. Two
659 // versions: one that takes a count, and one that takes a range.
660 // The range version is a member template, so we dispatch on whether
661 // or not the type is an integer.
662
663 void _M_fill_assign(size_t __n, bool __x)
664 {
665 if (__n > size())
666 {
667 std::fill(this->_M_impl._M_start._M_p,
668 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
669 insert(end(), __n - size(), __x);
670 }
671 else
672 {
673 erase(begin() + __n, end());
674 std::fill(this->_M_impl._M_start._M_p,
675 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
676 }
677 }
678
679 void assign(size_t __n, bool __x)
680 { _M_fill_assign(__n, __x); }
681
682 template<class _InputIterator>
683 void assign(_InputIterator __first, _InputIterator __last)
684 {
685 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
686 _M_assign_dispatch(__first, __last, _Integral());
687 }
688
689 template<class _Integer>
690 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
691 { _M_fill_assign((size_t) __n, (bool) __val); }
692
693 template<class _InputIterator>
694 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
695 __false_type)
696 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
697
698 template<class _InputIterator>
699 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
700 input_iterator_tag)
701 {
702 iterator __cur = begin();
703 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
704 *__cur = *__first;
705 if (__first == __last)
706 erase(__cur, end());
707 else
708 insert(end(), __first, __last);
709 }
710
711 template<class _ForwardIterator>
712 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
713 forward_iterator_tag)
714 {
715 const size_type __len = std::distance(__first, __last);
716 if (__len < size())
717 erase(std::copy(__first, __last, begin()), end());
718 else
719 {
720 _ForwardIterator __mid = __first;
721 std::advance(__mid, size());
722 std::copy(__first, __mid, begin());
723 insert(end(), __mid, __last);
724 }
725 }
726
727 void reserve(size_type __n)
728 {
729 if (__n > this->max_size())
730 __throw_length_error(__N("vector::reserve"));
731 if (this->capacity() < __n)
732 {
733 _Bit_type* __q = this->_M_allocate(__n);
734 this->_M_impl._M_finish = std::copy(begin(), end(),
735 iterator(__q, 0));
736 this->_M_deallocate();
737 this->_M_impl._M_start = iterator(__q, 0);
738 this->_M_impl._M_end_of_storage = __q + (__n + _S_word_bit - 1) / _S_word_bit;
739 }
740 }
741
742 reference front()
743 { return *begin(); }
744
745 const_reference front() const
746 { return *begin(); }
747
748 reference back()
749 { return *(end() - 1); }
750
751 const_reference back() const
752 { return *(end() - 1); }
753
754 void push_back(bool __x)
755 {
756 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
757 *this->_M_impl._M_finish++ = __x;
758 else
759 _M_insert_aux(end(), __x);
760 }
761
762 void swap(vector<bool, _Alloc>& __x)
763 {
764 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
765 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
766 std::swap(this->_M_impl._M_end_of_storage,
767 __x._M_impl._M_end_of_storage);
768 }
769
770 // [23.2.5]/1, third-to-last entry in synopsis listing
771 static void swap(reference __x, reference __y)
772 {
773 bool __tmp = __x;
774 __x = __y;
775 __y = __tmp;
776 }
777
778 iterator insert(iterator __position, bool __x = bool())
779 {
780 const difference_type __n = __position - begin();
781 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
782 && __position == end())
783 *this->_M_impl._M_finish++ = __x;
784 else
785 _M_insert_aux(__position, __x);
786 return begin() + __n;
787 }
788
789 // Check whether it's an integral type. If so, it's not an iterator.
790
791 template<class _Integer>
792 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
793 __true_type)
794 { _M_fill_insert(__pos, __n, __x); }
795
796 template<class _InputIterator>
797 void _M_insert_dispatch(iterator __pos,
798 _InputIterator __first, _InputIterator __last,
799 __false_type)
800 { _M_insert_range(__pos, __first, __last,
801 std::__iterator_category(__first)); }
802
803 template<class _InputIterator>
804 void insert(iterator __position,
805 _InputIterator __first, _InputIterator __last)
806 {
807 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
808 _M_insert_dispatch(__position, __first, __last, _Integral());
809 }
810
811 void _M_fill_insert(iterator __position, size_type __n, bool __x)
812 {
813 if (__n == 0)
814 return;
815 if (capacity() - size() >= __n)
816 {
817 std::copy_backward(__position, end(),
818 this->_M_impl._M_finish + difference_type(__n));
819 std::fill(__position, __position + difference_type(__n), __x);
820 this->_M_impl._M_finish += difference_type(__n);
821 }
822 else
823 {
824 const size_type __len = size() + std::max(size(), __n);
825 _Bit_type * __q = this->_M_allocate(__len);
826 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
827 std::fill_n(__i, __n, __x);
828 this->_M_impl._M_finish = std::copy(__position, end(),
829 __i + difference_type(__n));
830 this->_M_deallocate();
831 this->_M_impl._M_end_of_storage = __q + (__len + _S_word_bit - 1)
832 / _S_word_bit;
833 this->_M_impl._M_start = iterator(__q, 0);
834 }
835 }
836
837 void insert(iterator __position, size_type __n, bool __x)
838 { _M_fill_insert(__position, __n, __x); }
839
840 void pop_back()
841 { --this->_M_impl._M_finish; }
842
843 iterator erase(iterator __position)
844 {
845 if (__position + 1 != end())
846 std::copy(__position + 1, end(), __position);
847 --this->_M_impl._M_finish;
848 return __position;
849 }
850
851 iterator erase(iterator __first, iterator __last)
852 {
853 this->_M_impl._M_finish = std::copy(__last, end(), __first);
854 return __first;
855 }
856
857 void resize(size_type __new_size, bool __x = bool())
858 {
859 if (__new_size < size())
860 erase(begin() + difference_type(__new_size), end());
861 else
862 insert(end(), __new_size - size(), __x);
863 }
864
865 void flip()
866 {
867 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
868 __p != this->_M_impl._M_end_of_storage; ++__p)
869 *__p = ~*__p;
870 }
871
872 void clear()
873 { erase(begin(), end()); }
874 };
875 } // namespace std
876
877 #endif