Add initializer_list support as per N2679.
[gcc.git] / libstdc++-v3 / include / bits / stl_bvector.h
1 // vector<bool> specialization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996-1999
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_bvector.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_BVECTOR_H
63 #define _STL_BVECTOR_H 1
64
65 #include <initializer_list>
66
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68
69 typedef unsigned long _Bit_type;
70 enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
71
72 struct _Bit_reference
73 {
74 _Bit_type * _M_p;
75 _Bit_type _M_mask;
76
77 _Bit_reference(_Bit_type * __x, _Bit_type __y)
78 : _M_p(__x), _M_mask(__y) { }
79
80 _Bit_reference() : _M_p(0), _M_mask(0) { }
81
82 operator bool() const
83 { return !!(*_M_p & _M_mask); }
84
85 _Bit_reference&
86 operator=(bool __x)
87 {
88 if (__x)
89 *_M_p |= _M_mask;
90 else
91 *_M_p &= ~_M_mask;
92 return *this;
93 }
94
95 _Bit_reference&
96 operator=(const _Bit_reference& __x)
97 { return *this = bool(__x); }
98
99 bool
100 operator==(const _Bit_reference& __x) const
101 { return bool(*this) == bool(__x); }
102
103 bool
104 operator<(const _Bit_reference& __x) const
105 { return !bool(*this) && bool(__x); }
106
107 void
108 flip()
109 { *_M_p ^= _M_mask; }
110 };
111
112 struct _Bit_iterator_base
113 : public std::iterator<std::random_access_iterator_tag, bool>
114 {
115 _Bit_type * _M_p;
116 unsigned int _M_offset;
117
118 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
119 : _M_p(__x), _M_offset(__y) { }
120
121 void
122 _M_bump_up()
123 {
124 if (_M_offset++ == int(_S_word_bit) - 1)
125 {
126 _M_offset = 0;
127 ++_M_p;
128 }
129 }
130
131 void
132 _M_bump_down()
133 {
134 if (_M_offset-- == 0)
135 {
136 _M_offset = int(_S_word_bit) - 1;
137 --_M_p;
138 }
139 }
140
141 void
142 _M_incr(ptrdiff_t __i)
143 {
144 difference_type __n = __i + _M_offset;
145 _M_p += __n / int(_S_word_bit);
146 __n = __n % int(_S_word_bit);
147 if (__n < 0)
148 {
149 __n += int(_S_word_bit);
150 --_M_p;
151 }
152 _M_offset = static_cast<unsigned int>(__n);
153 }
154
155 bool
156 operator==(const _Bit_iterator_base& __i) const
157 { return _M_p == __i._M_p && _M_offset == __i._M_offset; }
158
159 bool
160 operator<(const _Bit_iterator_base& __i) const
161 {
162 return _M_p < __i._M_p
163 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
164 }
165
166 bool
167 operator!=(const _Bit_iterator_base& __i) const
168 { return !(*this == __i); }
169
170 bool
171 operator>(const _Bit_iterator_base& __i) const
172 { return __i < *this; }
173
174 bool
175 operator<=(const _Bit_iterator_base& __i) const
176 { return !(__i < *this); }
177
178 bool
179 operator>=(const _Bit_iterator_base& __i) const
180 { return !(*this < __i); }
181 };
182
183 inline ptrdiff_t
184 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
185 {
186 return (int(_S_word_bit) * (__x._M_p - __y._M_p)
187 + __x._M_offset - __y._M_offset);
188 }
189
190 struct _Bit_iterator : public _Bit_iterator_base
191 {
192 typedef _Bit_reference reference;
193 typedef _Bit_reference* pointer;
194 typedef _Bit_iterator iterator;
195
196 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
197
198 _Bit_iterator(_Bit_type * __x, unsigned int __y)
199 : _Bit_iterator_base(__x, __y) { }
200
201 reference
202 operator*() const
203 { return reference(_M_p, 1UL << _M_offset); }
204
205 iterator&
206 operator++()
207 {
208 _M_bump_up();
209 return *this;
210 }
211
212 iterator
213 operator++(int)
214 {
215 iterator __tmp = *this;
216 _M_bump_up();
217 return __tmp;
218 }
219
220 iterator&
221 operator--()
222 {
223 _M_bump_down();
224 return *this;
225 }
226
227 iterator
228 operator--(int)
229 {
230 iterator __tmp = *this;
231 _M_bump_down();
232 return __tmp;
233 }
234
235 iterator&
236 operator+=(difference_type __i)
237 {
238 _M_incr(__i);
239 return *this;
240 }
241
242 iterator&
243 operator-=(difference_type __i)
244 {
245 *this += -__i;
246 return *this;
247 }
248
249 iterator
250 operator+(difference_type __i) const
251 {
252 iterator __tmp = *this;
253 return __tmp += __i;
254 }
255
256 iterator
257 operator-(difference_type __i) const
258 {
259 iterator __tmp = *this;
260 return __tmp -= __i;
261 }
262
263 reference
264 operator[](difference_type __i) const
265 { return *(*this + __i); }
266 };
267
268 inline _Bit_iterator
269 operator+(ptrdiff_t __n, const _Bit_iterator& __x)
270 { return __x + __n; }
271
272 struct _Bit_const_iterator : public _Bit_iterator_base
273 {
274 typedef bool reference;
275 typedef bool const_reference;
276 typedef const bool* pointer;
277 typedef _Bit_const_iterator const_iterator;
278
279 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
280
281 _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
282 : _Bit_iterator_base(__x, __y) { }
283
284 _Bit_const_iterator(const _Bit_iterator& __x)
285 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
286
287 const_reference
288 operator*() const
289 { return _Bit_reference(_M_p, 1UL << _M_offset); }
290
291 const_iterator&
292 operator++()
293 {
294 _M_bump_up();
295 return *this;
296 }
297
298 const_iterator
299 operator++(int)
300 {
301 const_iterator __tmp = *this;
302 _M_bump_up();
303 return __tmp;
304 }
305
306 const_iterator&
307 operator--()
308 {
309 _M_bump_down();
310 return *this;
311 }
312
313 const_iterator
314 operator--(int)
315 {
316 const_iterator __tmp = *this;
317 _M_bump_down();
318 return __tmp;
319 }
320
321 const_iterator&
322 operator+=(difference_type __i)
323 {
324 _M_incr(__i);
325 return *this;
326 }
327
328 const_iterator&
329 operator-=(difference_type __i)
330 {
331 *this += -__i;
332 return *this;
333 }
334
335 const_iterator
336 operator+(difference_type __i) const
337 {
338 const_iterator __tmp = *this;
339 return __tmp += __i;
340 }
341
342 const_iterator
343 operator-(difference_type __i) const
344 {
345 const_iterator __tmp = *this;
346 return __tmp -= __i;
347 }
348
349 const_reference
350 operator[](difference_type __i) const
351 { return *(*this + __i); }
352 };
353
354 inline _Bit_const_iterator
355 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x)
356 { return __x + __n; }
357
358 inline void
359 __fill_bvector(_Bit_iterator __first, _Bit_iterator __last, bool __x)
360 {
361 for (; __first != __last; ++__first)
362 *__first = __x;
363 }
364
365 inline void
366 fill(_Bit_iterator __first, _Bit_iterator __last, const bool& __x)
367 {
368 if (__first._M_p != __last._M_p)
369 {
370 std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
371 __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
372 __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
373 }
374 else
375 __fill_bvector(__first, __last, __x);
376 }
377
378 template<typename _Alloc>
379 struct _Bvector_base
380 {
381 typedef typename _Alloc::template rebind<_Bit_type>::other
382 _Bit_alloc_type;
383
384 struct _Bvector_impl
385 : public _Bit_alloc_type
386 {
387 _Bit_iterator _M_start;
388 _Bit_iterator _M_finish;
389 _Bit_type* _M_end_of_storage;
390
391 _Bvector_impl()
392 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
393 { }
394
395 _Bvector_impl(const _Bit_alloc_type& __a)
396 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
397 { }
398 };
399
400 public:
401 typedef _Alloc allocator_type;
402
403 _Bit_alloc_type&
404 _M_get_Bit_allocator()
405 { return *static_cast<_Bit_alloc_type*>(&this->_M_impl); }
406
407 const _Bit_alloc_type&
408 _M_get_Bit_allocator() const
409 { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); }
410
411 allocator_type
412 get_allocator() const
413 { return allocator_type(_M_get_Bit_allocator()); }
414
415 _Bvector_base()
416 : _M_impl() { }
417
418 _Bvector_base(const allocator_type& __a)
419 : _M_impl(__a) { }
420
421 #ifdef __GXX_EXPERIMENTAL_CXX0X__
422 _Bvector_base(_Bvector_base&& __x)
423 : _M_impl(__x._M_get_Bit_allocator())
424 {
425 this->_M_impl._M_start = __x._M_impl._M_start;
426 this->_M_impl._M_finish = __x._M_impl._M_finish;
427 this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
428 __x._M_impl._M_start = _Bit_iterator();
429 __x._M_impl._M_finish = _Bit_iterator();
430 __x._M_impl._M_end_of_storage = 0;
431 }
432 #endif
433
434 ~_Bvector_base()
435 { this->_M_deallocate(); }
436
437 protected:
438 _Bvector_impl _M_impl;
439
440 _Bit_type*
441 _M_allocate(size_t __n)
442 { return _M_impl.allocate((__n + int(_S_word_bit) - 1)
443 / int(_S_word_bit)); }
444
445 void
446 _M_deallocate()
447 {
448 if (_M_impl._M_start._M_p)
449 _M_impl.deallocate(_M_impl._M_start._M_p,
450 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
451 }
452 };
453
454 _GLIBCXX_END_NESTED_NAMESPACE
455
456 // Declare a partial specialization of vector<T, Alloc>.
457 #include <bits/stl_vector.h>
458
459 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
460
461 /**
462 * @brief A specialization of vector for booleans which offers fixed time
463 * access to individual elements in any order.
464 *
465 * Note that vector<bool> does not actually meet the requirements for being
466 * a container. This is because the reference and pointer types are not
467 * really references and pointers to bool. See DR96 for details. @see
468 * vector for function documentation.
469 *
470 * @ingroup Containers
471 * @ingroup Sequences
472 *
473 * In some terminology a %vector can be described as a dynamic
474 * C-style array, it offers fast and efficient access to individual
475 * elements in any order and saves the user from worrying about
476 * memory and size allocation. Subscripting ( @c [] ) access is
477 * also provided as with C-style arrays.
478 */
479 template<typename _Alloc>
480 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
481 {
482 typedef _Bvector_base<_Alloc> _Base;
483
484 public:
485 typedef bool value_type;
486 typedef size_t size_type;
487 typedef ptrdiff_t difference_type;
488 typedef _Bit_reference reference;
489 typedef bool const_reference;
490 typedef _Bit_reference* pointer;
491 typedef const bool* const_pointer;
492 typedef _Bit_iterator iterator;
493 typedef _Bit_const_iterator const_iterator;
494 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
495 typedef std::reverse_iterator<iterator> reverse_iterator;
496 typedef _Alloc allocator_type;
497
498 allocator_type get_allocator() const
499 { return _Base::get_allocator(); }
500
501 protected:
502 using _Base::_M_allocate;
503 using _Base::_M_deallocate;
504 using _Base::_M_get_Bit_allocator;
505
506 public:
507 vector()
508 : _Base() { }
509
510 explicit
511 vector(const allocator_type& __a)
512 : _Base(__a) { }
513
514 explicit
515 vector(size_type __n, const bool& __value = bool(),
516 const allocator_type& __a = allocator_type())
517 : _Base(__a)
518 {
519 _M_initialize(__n);
520 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
521 __value ? ~0 : 0);
522 }
523
524 vector(const vector& __x)
525 : _Base(__x._M_get_Bit_allocator())
526 {
527 _M_initialize(__x.size());
528 _M_copy_aligned(__x.begin(), __x.end(), this->_M_impl._M_start);
529 }
530
531 #ifdef __GXX_EXPERIMENTAL_CXX0X__
532 vector(vector&& __x)
533 : _Base(std::forward<_Base>(__x)) { }
534
535 vector(initializer_list<bool> __l,
536 const allocator_type& __a = allocator_type())
537 : _Base(__a)
538 {
539 _M_initialize_range(__l.begin(), __l.end(),
540 random_access_iterator_tag());
541 }
542 #endif
543
544 template<typename _InputIterator>
545 vector(_InputIterator __first, _InputIterator __last,
546 const allocator_type& __a = allocator_type())
547 : _Base(__a)
548 {
549 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
550 _M_initialize_dispatch(__first, __last, _Integral());
551 }
552
553 ~vector() { }
554
555 vector&
556 operator=(const vector& __x)
557 {
558 if (&__x == this)
559 return *this;
560 if (__x.size() > capacity())
561 {
562 this->_M_deallocate();
563 _M_initialize(__x.size());
564 }
565 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
566 begin());
567 return *this;
568 }
569
570 #ifdef __GXX_EXPERIMENTAL_CXX0X__
571 vector&
572 operator=(vector&& __x)
573 {
574 // NB: DR 675.
575 this->clear();
576 this->swap(__x);
577 return *this;
578 }
579
580 vector&
581 operator=(initializer_list<bool> __l)
582 {
583 this->assign (__l.begin(), __l.end());
584 return *this;
585 }
586 #endif
587
588 // assign(), a generalized assignment member function. Two
589 // versions: one that takes a count, and one that takes a range.
590 // The range version is a member template, so we dispatch on whether
591 // or not the type is an integer.
592 void
593 assign(size_type __n, const bool& __x)
594 { _M_fill_assign(__n, __x); }
595
596 template<typename _InputIterator>
597 void
598 assign(_InputIterator __first, _InputIterator __last)
599 {
600 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
601 _M_assign_dispatch(__first, __last, _Integral());
602 }
603
604 #ifdef __GXX_EXPERIMENTAL_CXX0X__
605 void
606 assign(initializer_list<bool> __l)
607 { this->assign(__l.begin(), __l.end()); }
608 #endif
609
610 iterator
611 begin()
612 { return this->_M_impl._M_start; }
613
614 const_iterator
615 begin() const
616 { return this->_M_impl._M_start; }
617
618 iterator
619 end()
620 { return this->_M_impl._M_finish; }
621
622 const_iterator
623 end() const
624 { return this->_M_impl._M_finish; }
625
626 reverse_iterator
627 rbegin()
628 { return reverse_iterator(end()); }
629
630 const_reverse_iterator
631 rbegin() const
632 { return const_reverse_iterator(end()); }
633
634 reverse_iterator
635 rend()
636 { return reverse_iterator(begin()); }
637
638 const_reverse_iterator
639 rend() const
640 { return const_reverse_iterator(begin()); }
641
642 #ifdef __GXX_EXPERIMENTAL_CXX0X__
643 const_iterator
644 cbegin() const
645 { return this->_M_impl._M_start; }
646
647 const_iterator
648 cend() const
649 { return this->_M_impl._M_finish; }
650
651 const_reverse_iterator
652 crbegin() const
653 { return const_reverse_iterator(end()); }
654
655 const_reverse_iterator
656 crend() const
657 { return const_reverse_iterator(begin()); }
658 #endif
659
660 size_type
661 size() const
662 { return size_type(end() - begin()); }
663
664 size_type
665 max_size() const
666 {
667 const size_type __isize =
668 __gnu_cxx::__numeric_traits<difference_type>::__max
669 - int(_S_word_bit) + 1;
670 const size_type __asize = _M_get_Bit_allocator().max_size();
671 return (__asize <= __isize / int(_S_word_bit)
672 ? __asize * int(_S_word_bit) : __isize);
673 }
674
675 size_type
676 capacity() const
677 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
678 - begin()); }
679
680 bool
681 empty() const
682 { return begin() == end(); }
683
684 reference
685 operator[](size_type __n)
686 {
687 return *iterator(this->_M_impl._M_start._M_p
688 + __n / int(_S_word_bit), __n % int(_S_word_bit));
689 }
690
691 const_reference
692 operator[](size_type __n) const
693 {
694 return *const_iterator(this->_M_impl._M_start._M_p
695 + __n / int(_S_word_bit), __n % int(_S_word_bit));
696 }
697
698 protected:
699 void
700 _M_range_check(size_type __n) const
701 {
702 if (__n >= this->size())
703 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
704 }
705
706 public:
707 reference
708 at(size_type __n)
709 { _M_range_check(__n); return (*this)[__n]; }
710
711 const_reference
712 at(size_type __n) const
713 { _M_range_check(__n); return (*this)[__n]; }
714
715 void
716 reserve(size_type __n);
717
718 reference
719 front()
720 { return *begin(); }
721
722 const_reference
723 front() const
724 { return *begin(); }
725
726 reference
727 back()
728 { return *(end() - 1); }
729
730 const_reference
731 back() const
732 { return *(end() - 1); }
733
734 // _GLIBCXX_RESOLVE_LIB_DEFECTS
735 // DR 464. Suggestion for new member functions in standard containers.
736 // N.B. DR 464 says nothing about vector<bool> but we need something
737 // here due to the way we are implementing DR 464 in the debug-mode
738 // vector class.
739 void
740 data() { }
741
742 void
743 push_back(bool __x)
744 {
745 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
746 *this->_M_impl._M_finish++ = __x;
747 else
748 _M_insert_aux(end(), __x);
749 }
750
751 void
752 #ifdef __GXX_EXPERIMENTAL_CXX0X__
753 swap(vector&& __x)
754 #else
755 swap(vector& __x)
756 #endif
757 {
758 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
759 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
760 std::swap(this->_M_impl._M_end_of_storage,
761 __x._M_impl._M_end_of_storage);
762
763 // _GLIBCXX_RESOLVE_LIB_DEFECTS
764 // 431. Swapping containers with unequal allocators.
765 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
766 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
767 }
768
769 // [23.2.5]/1, third-to-last entry in synopsis listing
770 static void
771 swap(reference __x, reference __y)
772 {
773 bool __tmp = __x;
774 __x = __y;
775 __y = __tmp;
776 }
777
778 iterator
779 insert(iterator __position, const bool& __x = bool())
780 {
781 const difference_type __n = __position - begin();
782 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
783 && __position == end())
784 *this->_M_impl._M_finish++ = __x;
785 else
786 _M_insert_aux(__position, __x);
787 return begin() + __n;
788 }
789
790 template<typename _InputIterator>
791 void
792 insert(iterator __position,
793 _InputIterator __first, _InputIterator __last)
794 {
795 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
796 _M_insert_dispatch(__position, __first, __last, _Integral());
797 }
798
799 void
800 insert(iterator __position, size_type __n, const bool& __x)
801 { _M_fill_insert(__position, __n, __x); }
802
803 #ifdef __GXX_EXPERIMENTAL_CXX0X__
804 void insert(iterator __p, initializer_list<bool> __l)
805 { this->insert(__p, __l.begin(), __l.end()); }
806 #endif
807
808 void
809 pop_back()
810 { --this->_M_impl._M_finish; }
811
812 iterator
813 erase(iterator __position)
814 {
815 if (__position + 1 != end())
816 std::copy(__position + 1, end(), __position);
817 --this->_M_impl._M_finish;
818 return __position;
819 }
820
821 iterator
822 erase(iterator __first, iterator __last)
823 {
824 _M_erase_at_end(std::copy(__last, end(), __first));
825 return __first;
826 }
827
828 void
829 resize(size_type __new_size, bool __x = bool())
830 {
831 if (__new_size < size())
832 _M_erase_at_end(begin() + difference_type(__new_size));
833 else
834 insert(end(), __new_size - size(), __x);
835 }
836
837 void
838 flip()
839 {
840 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
841 __p != this->_M_impl._M_end_of_storage; ++__p)
842 *__p = ~*__p;
843 }
844
845 void
846 clear()
847 { _M_erase_at_end(begin()); }
848
849
850 protected:
851 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
852 iterator
853 _M_copy_aligned(const_iterator __first, const_iterator __last,
854 iterator __result)
855 {
856 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
857 return std::copy(const_iterator(__last._M_p, 0), __last,
858 iterator(__q, 0));
859 }
860
861 void
862 _M_initialize(size_type __n)
863 {
864 _Bit_type* __q = this->_M_allocate(__n);
865 this->_M_impl._M_end_of_storage = (__q
866 + ((__n + int(_S_word_bit) - 1)
867 / int(_S_word_bit)));
868 this->_M_impl._M_start = iterator(__q, 0);
869 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
870 }
871
872 // Check whether it's an integral type. If so, it's not an iterator.
873
874 // _GLIBCXX_RESOLVE_LIB_DEFECTS
875 // 438. Ambiguity in the "do the right thing" clause
876 template<typename _Integer>
877 void
878 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
879 {
880 _M_initialize(static_cast<size_type>(__n));
881 std::fill(this->_M_impl._M_start._M_p,
882 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
883 }
884
885 template<typename _InputIterator>
886 void
887 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
888 __false_type)
889 { _M_initialize_range(__first, __last,
890 std::__iterator_category(__first)); }
891
892 template<typename _InputIterator>
893 void
894 _M_initialize_range(_InputIterator __first, _InputIterator __last,
895 std::input_iterator_tag)
896 {
897 for (; __first != __last; ++__first)
898 push_back(*__first);
899 }
900
901 template<typename _ForwardIterator>
902 void
903 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
904 std::forward_iterator_tag)
905 {
906 const size_type __n = std::distance(__first, __last);
907 _M_initialize(__n);
908 std::copy(__first, __last, this->_M_impl._M_start);
909 }
910
911 // _GLIBCXX_RESOLVE_LIB_DEFECTS
912 // 438. Ambiguity in the "do the right thing" clause
913 template<typename _Integer>
914 void
915 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
916 { _M_fill_assign(__n, __val); }
917
918 template<class _InputIterator>
919 void
920 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
921 __false_type)
922 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
923
924 void
925 _M_fill_assign(size_t __n, bool __x)
926 {
927 if (__n > size())
928 {
929 std::fill(this->_M_impl._M_start._M_p,
930 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
931 insert(end(), __n - size(), __x);
932 }
933 else
934 {
935 _M_erase_at_end(begin() + __n);
936 std::fill(this->_M_impl._M_start._M_p,
937 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
938 }
939 }
940
941 template<typename _InputIterator>
942 void
943 _M_assign_aux(_InputIterator __first, _InputIterator __last,
944 std::input_iterator_tag)
945 {
946 iterator __cur = begin();
947 for (; __first != __last && __cur != end(); ++__cur, ++__first)
948 *__cur = *__first;
949 if (__first == __last)
950 _M_erase_at_end(__cur);
951 else
952 insert(end(), __first, __last);
953 }
954
955 template<typename _ForwardIterator>
956 void
957 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
958 std::forward_iterator_tag)
959 {
960 const size_type __len = std::distance(__first, __last);
961 if (__len < size())
962 _M_erase_at_end(std::copy(__first, __last, begin()));
963 else
964 {
965 _ForwardIterator __mid = __first;
966 std::advance(__mid, size());
967 std::copy(__first, __mid, begin());
968 insert(end(), __mid, __last);
969 }
970 }
971
972 // Check whether it's an integral type. If so, it's not an iterator.
973
974 // _GLIBCXX_RESOLVE_LIB_DEFECTS
975 // 438. Ambiguity in the "do the right thing" clause
976 template<typename _Integer>
977 void
978 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
979 __true_type)
980 { _M_fill_insert(__pos, __n, __x); }
981
982 template<typename _InputIterator>
983 void
984 _M_insert_dispatch(iterator __pos,
985 _InputIterator __first, _InputIterator __last,
986 __false_type)
987 { _M_insert_range(__pos, __first, __last,
988 std::__iterator_category(__first)); }
989
990 void
991 _M_fill_insert(iterator __position, size_type __n, bool __x);
992
993 template<typename _InputIterator>
994 void
995 _M_insert_range(iterator __pos, _InputIterator __first,
996 _InputIterator __last, std::input_iterator_tag)
997 {
998 for (; __first != __last; ++__first)
999 {
1000 __pos = insert(__pos, *__first);
1001 ++__pos;
1002 }
1003 }
1004
1005 template<typename _ForwardIterator>
1006 void
1007 _M_insert_range(iterator __position, _ForwardIterator __first,
1008 _ForwardIterator __last, std::forward_iterator_tag);
1009
1010 void
1011 _M_insert_aux(iterator __position, bool __x);
1012
1013 size_type
1014 _M_check_len(size_type __n, const char* __s) const
1015 {
1016 if (max_size() - size() < __n)
1017 __throw_length_error(__N(__s));
1018
1019 const size_type __len = size() + std::max(size(), __n);
1020 return (__len < size() || __len > max_size()) ? max_size() : __len;
1021 }
1022
1023 void
1024 _M_erase_at_end(iterator __pos)
1025 { this->_M_impl._M_finish = __pos; }
1026 };
1027
1028 _GLIBCXX_END_NESTED_NAMESPACE
1029
1030 #endif