user.cfg.in: Tweaks.
[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, 2008
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 sequences
471 *
472 * In some terminology a %vector can be described as a dynamic
473 * C-style array, it offers fast and efficient access to individual
474 * elements in any order and saves the user from worrying about
475 * memory and size allocation. Subscripting ( @c [] ) access is
476 * also provided as with C-style arrays.
477 */
478 template<typename _Alloc>
479 class vector<bool, _Alloc> : protected _Bvector_base<_Alloc>
480 {
481 typedef _Bvector_base<_Alloc> _Base;
482
483 public:
484 typedef bool value_type;
485 typedef size_t size_type;
486 typedef ptrdiff_t difference_type;
487 typedef _Bit_reference reference;
488 typedef bool const_reference;
489 typedef _Bit_reference* pointer;
490 typedef const bool* const_pointer;
491 typedef _Bit_iterator iterator;
492 typedef _Bit_const_iterator const_iterator;
493 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
494 typedef std::reverse_iterator<iterator> reverse_iterator;
495 typedef _Alloc allocator_type;
496
497 allocator_type get_allocator() const
498 { return _Base::get_allocator(); }
499
500 protected:
501 using _Base::_M_allocate;
502 using _Base::_M_deallocate;
503 using _Base::_M_get_Bit_allocator;
504
505 public:
506 vector()
507 : _Base() { }
508
509 explicit
510 vector(const allocator_type& __a)
511 : _Base(__a) { }
512
513 explicit
514 vector(size_type __n, const bool& __value = bool(),
515 const allocator_type& __a = allocator_type())
516 : _Base(__a)
517 {
518 _M_initialize(__n);
519 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
520 __value ? ~0 : 0);
521 }
522
523 vector(const vector& __x)
524 : _Base(__x._M_get_Bit_allocator())
525 {
526 _M_initialize(__x.size());
527 _M_copy_aligned(__x.begin(), __x.end(), this->_M_impl._M_start);
528 }
529
530 #ifdef __GXX_EXPERIMENTAL_CXX0X__
531 vector(vector&& __x)
532 : _Base(std::forward<_Base>(__x)) { }
533
534 vector(initializer_list<bool> __l,
535 const allocator_type& __a = allocator_type())
536 : _Base(__a)
537 {
538 _M_initialize_range(__l.begin(), __l.end(),
539 random_access_iterator_tag());
540 }
541 #endif
542
543 template<typename _InputIterator>
544 vector(_InputIterator __first, _InputIterator __last,
545 const allocator_type& __a = allocator_type())
546 : _Base(__a)
547 {
548 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
549 _M_initialize_dispatch(__first, __last, _Integral());
550 }
551
552 ~vector() { }
553
554 vector&
555 operator=(const vector& __x)
556 {
557 if (&__x == this)
558 return *this;
559 if (__x.size() > capacity())
560 {
561 this->_M_deallocate();
562 _M_initialize(__x.size());
563 }
564 this->_M_impl._M_finish = _M_copy_aligned(__x.begin(), __x.end(),
565 begin());
566 return *this;
567 }
568
569 #ifdef __GXX_EXPERIMENTAL_CXX0X__
570 vector&
571 operator=(vector&& __x)
572 {
573 // NB: DR 675.
574 this->clear();
575 this->swap(__x);
576 return *this;
577 }
578
579 vector&
580 operator=(initializer_list<bool> __l)
581 {
582 this->assign (__l.begin(), __l.end());
583 return *this;
584 }
585 #endif
586
587 // assign(), a generalized assignment member function. Two
588 // versions: one that takes a count, and one that takes a range.
589 // The range version is a member template, so we dispatch on whether
590 // or not the type is an integer.
591 void
592 assign(size_type __n, const bool& __x)
593 { _M_fill_assign(__n, __x); }
594
595 template<typename _InputIterator>
596 void
597 assign(_InputIterator __first, _InputIterator __last)
598 {
599 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
600 _M_assign_dispatch(__first, __last, _Integral());
601 }
602
603 #ifdef __GXX_EXPERIMENTAL_CXX0X__
604 void
605 assign(initializer_list<bool> __l)
606 { this->assign(__l.begin(), __l.end()); }
607 #endif
608
609 iterator
610 begin()
611 { return this->_M_impl._M_start; }
612
613 const_iterator
614 begin() const
615 { return this->_M_impl._M_start; }
616
617 iterator
618 end()
619 { return this->_M_impl._M_finish; }
620
621 const_iterator
622 end() const
623 { return this->_M_impl._M_finish; }
624
625 reverse_iterator
626 rbegin()
627 { return reverse_iterator(end()); }
628
629 const_reverse_iterator
630 rbegin() const
631 { return const_reverse_iterator(end()); }
632
633 reverse_iterator
634 rend()
635 { return reverse_iterator(begin()); }
636
637 const_reverse_iterator
638 rend() const
639 { return const_reverse_iterator(begin()); }
640
641 #ifdef __GXX_EXPERIMENTAL_CXX0X__
642 const_iterator
643 cbegin() const
644 { return this->_M_impl._M_start; }
645
646 const_iterator
647 cend() const
648 { return this->_M_impl._M_finish; }
649
650 const_reverse_iterator
651 crbegin() const
652 { return const_reverse_iterator(end()); }
653
654 const_reverse_iterator
655 crend() const
656 { return const_reverse_iterator(begin()); }
657 #endif
658
659 size_type
660 size() const
661 { return size_type(end() - begin()); }
662
663 size_type
664 max_size() const
665 {
666 const size_type __isize =
667 __gnu_cxx::__numeric_traits<difference_type>::__max
668 - int(_S_word_bit) + 1;
669 const size_type __asize = _M_get_Bit_allocator().max_size();
670 return (__asize <= __isize / int(_S_word_bit)
671 ? __asize * int(_S_word_bit) : __isize);
672 }
673
674 size_type
675 capacity() const
676 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
677 - begin()); }
678
679 bool
680 empty() const
681 { return begin() == end(); }
682
683 reference
684 operator[](size_type __n)
685 {
686 return *iterator(this->_M_impl._M_start._M_p
687 + __n / int(_S_word_bit), __n % int(_S_word_bit));
688 }
689
690 const_reference
691 operator[](size_type __n) const
692 {
693 return *const_iterator(this->_M_impl._M_start._M_p
694 + __n / int(_S_word_bit), __n % int(_S_word_bit));
695 }
696
697 protected:
698 void
699 _M_range_check(size_type __n) const
700 {
701 if (__n >= this->size())
702 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
703 }
704
705 public:
706 reference
707 at(size_type __n)
708 { _M_range_check(__n); return (*this)[__n]; }
709
710 const_reference
711 at(size_type __n) const
712 { _M_range_check(__n); return (*this)[__n]; }
713
714 void
715 reserve(size_type __n);
716
717 reference
718 front()
719 { return *begin(); }
720
721 const_reference
722 front() const
723 { return *begin(); }
724
725 reference
726 back()
727 { return *(end() - 1); }
728
729 const_reference
730 back() const
731 { return *(end() - 1); }
732
733 // _GLIBCXX_RESOLVE_LIB_DEFECTS
734 // DR 464. Suggestion for new member functions in standard containers.
735 // N.B. DR 464 says nothing about vector<bool> but we need something
736 // here due to the way we are implementing DR 464 in the debug-mode
737 // vector class.
738 void
739 data() { }
740
741 void
742 push_back(bool __x)
743 {
744 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
745 *this->_M_impl._M_finish++ = __x;
746 else
747 _M_insert_aux(end(), __x);
748 }
749
750 void
751 #ifdef __GXX_EXPERIMENTAL_CXX0X__
752 swap(vector&& __x)
753 #else
754 swap(vector& __x)
755 #endif
756 {
757 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
758 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
759 std::swap(this->_M_impl._M_end_of_storage,
760 __x._M_impl._M_end_of_storage);
761
762 // _GLIBCXX_RESOLVE_LIB_DEFECTS
763 // 431. Swapping containers with unequal allocators.
764 std::__alloc_swap<typename _Base::_Bit_alloc_type>::
765 _S_do_it(_M_get_Bit_allocator(), __x._M_get_Bit_allocator());
766 }
767
768 // [23.2.5]/1, third-to-last entry in synopsis listing
769 static void
770 swap(reference __x, reference __y)
771 {
772 bool __tmp = __x;
773 __x = __y;
774 __y = __tmp;
775 }
776
777 iterator
778 insert(iterator __position, const 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 template<typename _InputIterator>
790 void
791 insert(iterator __position,
792 _InputIterator __first, _InputIterator __last)
793 {
794 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
795 _M_insert_dispatch(__position, __first, __last, _Integral());
796 }
797
798 void
799 insert(iterator __position, size_type __n, const bool& __x)
800 { _M_fill_insert(__position, __n, __x); }
801
802 #ifdef __GXX_EXPERIMENTAL_CXX0X__
803 void insert(iterator __p, initializer_list<bool> __l)
804 { this->insert(__p, __l.begin(), __l.end()); }
805 #endif
806
807 void
808 pop_back()
809 { --this->_M_impl._M_finish; }
810
811 iterator
812 erase(iterator __position)
813 {
814 if (__position + 1 != end())
815 std::copy(__position + 1, end(), __position);
816 --this->_M_impl._M_finish;
817 return __position;
818 }
819
820 iterator
821 erase(iterator __first, iterator __last)
822 {
823 _M_erase_at_end(std::copy(__last, end(), __first));
824 return __first;
825 }
826
827 void
828 resize(size_type __new_size, bool __x = bool())
829 {
830 if (__new_size < size())
831 _M_erase_at_end(begin() + difference_type(__new_size));
832 else
833 insert(end(), __new_size - size(), __x);
834 }
835
836 void
837 flip()
838 {
839 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
840 __p != this->_M_impl._M_end_of_storage; ++__p)
841 *__p = ~*__p;
842 }
843
844 void
845 clear()
846 { _M_erase_at_end(begin()); }
847
848
849 protected:
850 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
851 iterator
852 _M_copy_aligned(const_iterator __first, const_iterator __last,
853 iterator __result)
854 {
855 _Bit_type* __q = std::copy(__first._M_p, __last._M_p, __result._M_p);
856 return std::copy(const_iterator(__last._M_p, 0), __last,
857 iterator(__q, 0));
858 }
859
860 void
861 _M_initialize(size_type __n)
862 {
863 _Bit_type* __q = this->_M_allocate(__n);
864 this->_M_impl._M_end_of_storage = (__q
865 + ((__n + int(_S_word_bit) - 1)
866 / int(_S_word_bit)));
867 this->_M_impl._M_start = iterator(__q, 0);
868 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
869 }
870
871 // Check whether it's an integral type. If so, it's not an iterator.
872
873 // _GLIBCXX_RESOLVE_LIB_DEFECTS
874 // 438. Ambiguity in the "do the right thing" clause
875 template<typename _Integer>
876 void
877 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
878 {
879 _M_initialize(static_cast<size_type>(__n));
880 std::fill(this->_M_impl._M_start._M_p,
881 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
882 }
883
884 template<typename _InputIterator>
885 void
886 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
887 __false_type)
888 { _M_initialize_range(__first, __last,
889 std::__iterator_category(__first)); }
890
891 template<typename _InputIterator>
892 void
893 _M_initialize_range(_InputIterator __first, _InputIterator __last,
894 std::input_iterator_tag)
895 {
896 for (; __first != __last; ++__first)
897 push_back(*__first);
898 }
899
900 template<typename _ForwardIterator>
901 void
902 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
903 std::forward_iterator_tag)
904 {
905 const size_type __n = std::distance(__first, __last);
906 _M_initialize(__n);
907 std::copy(__first, __last, this->_M_impl._M_start);
908 }
909
910 // _GLIBCXX_RESOLVE_LIB_DEFECTS
911 // 438. Ambiguity in the "do the right thing" clause
912 template<typename _Integer>
913 void
914 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
915 { _M_fill_assign(__n, __val); }
916
917 template<class _InputIterator>
918 void
919 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
920 __false_type)
921 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
922
923 void
924 _M_fill_assign(size_t __n, bool __x)
925 {
926 if (__n > size())
927 {
928 std::fill(this->_M_impl._M_start._M_p,
929 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
930 insert(end(), __n - size(), __x);
931 }
932 else
933 {
934 _M_erase_at_end(begin() + __n);
935 std::fill(this->_M_impl._M_start._M_p,
936 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
937 }
938 }
939
940 template<typename _InputIterator>
941 void
942 _M_assign_aux(_InputIterator __first, _InputIterator __last,
943 std::input_iterator_tag)
944 {
945 iterator __cur = begin();
946 for (; __first != __last && __cur != end(); ++__cur, ++__first)
947 *__cur = *__first;
948 if (__first == __last)
949 _M_erase_at_end(__cur);
950 else
951 insert(end(), __first, __last);
952 }
953
954 template<typename _ForwardIterator>
955 void
956 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
957 std::forward_iterator_tag)
958 {
959 const size_type __len = std::distance(__first, __last);
960 if (__len < size())
961 _M_erase_at_end(std::copy(__first, __last, begin()));
962 else
963 {
964 _ForwardIterator __mid = __first;
965 std::advance(__mid, size());
966 std::copy(__first, __mid, begin());
967 insert(end(), __mid, __last);
968 }
969 }
970
971 // Check whether it's an integral type. If so, it's not an iterator.
972
973 // _GLIBCXX_RESOLVE_LIB_DEFECTS
974 // 438. Ambiguity in the "do the right thing" clause
975 template<typename _Integer>
976 void
977 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
978 __true_type)
979 { _M_fill_insert(__pos, __n, __x); }
980
981 template<typename _InputIterator>
982 void
983 _M_insert_dispatch(iterator __pos,
984 _InputIterator __first, _InputIterator __last,
985 __false_type)
986 { _M_insert_range(__pos, __first, __last,
987 std::__iterator_category(__first)); }
988
989 void
990 _M_fill_insert(iterator __position, size_type __n, bool __x);
991
992 template<typename _InputIterator>
993 void
994 _M_insert_range(iterator __pos, _InputIterator __first,
995 _InputIterator __last, std::input_iterator_tag)
996 {
997 for (; __first != __last; ++__first)
998 {
999 __pos = insert(__pos, *__first);
1000 ++__pos;
1001 }
1002 }
1003
1004 template<typename _ForwardIterator>
1005 void
1006 _M_insert_range(iterator __position, _ForwardIterator __first,
1007 _ForwardIterator __last, std::forward_iterator_tag);
1008
1009 void
1010 _M_insert_aux(iterator __position, bool __x);
1011
1012 size_type
1013 _M_check_len(size_type __n, const char* __s) const
1014 {
1015 if (max_size() - size() < __n)
1016 __throw_length_error(__N(__s));
1017
1018 const size_type __len = size() + std::max(size(), __n);
1019 return (__len < size() || __len > max_size()) ? max_size() : __len;
1020 }
1021
1022 void
1023 _M_erase_at_end(iterator __pos)
1024 { this->_M_impl._M_finish = __pos; }
1025 };
1026
1027 _GLIBCXX_END_NESTED_NAMESPACE
1028
1029 #endif