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