1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
34 * Hewlett-Packard Company
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.
45 * Copyright (c) 1996-1999
46 * Silicon Graphics Computer Systems, Inc.
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.
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.
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
67 typedef unsigned long _Bit_type
;
68 enum { _S_word_bit
= int(CHAR_BIT
* sizeof(_Bit_type
)) };
75 _Bit_reference(_Bit_type
* __x
, _Bit_type __y
)
76 : _M_p(__x
), _M_mask(__y
) { }
78 _Bit_reference() : _M_p(0), _M_mask(0) { }
81 { return !!(*_M_p
& _M_mask
); }
94 operator=(const _Bit_reference
& __x
)
95 { return *this = bool(__x
); }
98 operator==(const _Bit_reference
& __x
) const
99 { return bool(*this) == bool(__x
); }
102 operator<(const _Bit_reference
& __x
) const
103 { return !bool(*this) && bool(__x
); }
107 { *_M_p
^= _M_mask
; }
110 struct _Bit_iterator_base
111 : public std::iterator
<std::random_access_iterator_tag
, bool>
114 unsigned int _M_offset
;
116 _Bit_iterator_base(_Bit_type
* __x
, unsigned int __y
)
117 : _M_p(__x
), _M_offset(__y
) { }
122 if (_M_offset
++ == int(_S_word_bit
) - 1)
132 if (_M_offset
-- == 0)
134 _M_offset
= int(_S_word_bit
) - 1;
140 _M_incr(ptrdiff_t __i
)
142 difference_type __n
= __i
+ _M_offset
;
143 _M_p
+= __n
/ int(_S_word_bit
);
144 __n
= __n
% int(_S_word_bit
);
147 _M_offset
= static_cast<unsigned int>(__n
+ int(_S_word_bit
));
151 _M_offset
= static_cast<unsigned int>(__n
);
155 operator==(const _Bit_iterator_base
& __i
) const
156 { return _M_p
== __i
._M_p
&& _M_offset
== __i
._M_offset
; }
159 operator<(const _Bit_iterator_base
& __i
) const
161 return _M_p
< __i
._M_p
162 || (_M_p
== __i
._M_p
&& _M_offset
< __i
._M_offset
);
166 operator!=(const _Bit_iterator_base
& __i
) const
167 { return !(*this == __i
); }
170 operator>(const _Bit_iterator_base
& __i
) const
171 { return __i
< *this; }
174 operator<=(const _Bit_iterator_base
& __i
) const
175 { return !(__i
< *this); }
178 operator>=(const _Bit_iterator_base
& __i
) const
179 { return !(*this < __i
); }
183 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
185 return (int(_S_word_bit
) * (__x
._M_p
- __y
._M_p
)
186 + __x
._M_offset
- __y
._M_offset
);
189 struct _Bit_iterator
: public _Bit_iterator_base
191 typedef _Bit_reference reference
;
192 typedef _Bit_reference
* pointer
;
193 typedef _Bit_iterator iterator
;
195 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
197 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
198 : _Bit_iterator_base(__x
, __y
) { }
202 { return reference(_M_p
, 1UL << _M_offset
); }
214 iterator __tmp
= *this;
229 iterator __tmp
= *this;
235 operator+=(difference_type __i
)
242 operator-=(difference_type __i
)
249 operator+(difference_type __i
) const
251 iterator __tmp
= *this;
256 operator-(difference_type __i
) const
258 iterator __tmp
= *this;
263 operator[](difference_type __i
) const
264 { return *(*this + __i
); }
268 operator+(ptrdiff_t __n
, const _Bit_iterator
& __x
)
269 { return __x
+ __n
; }
271 struct _Bit_const_iterator
: public _Bit_iterator_base
273 typedef bool reference
;
274 typedef bool const_reference
;
275 typedef const bool* pointer
;
276 typedef _Bit_const_iterator const_iterator
;
278 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
280 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
281 : _Bit_iterator_base(__x
, __y
) { }
283 _Bit_const_iterator(const _Bit_iterator
& __x
)
284 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
288 { return _Bit_reference(_M_p
, 1UL << _M_offset
); }
300 const_iterator __tmp
= *this;
315 const_iterator __tmp
= *this;
321 operator+=(difference_type __i
)
328 operator-=(difference_type __i
)
335 operator+(difference_type __i
) const
337 const_iterator __tmp
= *this;
342 operator-(difference_type __i
) const
344 const_iterator __tmp
= *this;
349 operator[](difference_type __i
) const
350 { return *(*this + __i
); }
353 inline _Bit_const_iterator
354 operator+(ptrdiff_t __n
, const _Bit_const_iterator
& __x
)
355 { return __x
+ __n
; }
357 template<class _Alloc
>
360 typedef typename
_Alloc::template rebind
<_Bit_type
>::other
364 : public _Bit_alloc_type
366 _Bit_iterator _M_start
;
367 _Bit_iterator _M_finish
;
368 _Bit_type
* _M_end_of_storage
;
369 _Bvector_impl(const _Bit_alloc_type
& __a
)
370 : _Bit_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage(0)
375 typedef _Alloc allocator_type
;
378 _M_get_Bit_allocator()
379 { return *static_cast<_Bit_alloc_type
*>(&this->_M_impl
); }
381 const _Bit_alloc_type
&
382 _M_get_Bit_allocator() const
383 { return *static_cast<const _Bit_alloc_type
*>(&this->_M_impl
); }
386 get_allocator() const
387 { return allocator_type(_M_get_Bit_allocator()); }
389 _Bvector_base(const allocator_type
& __a
) : _M_impl(__a
) { }
392 { this->_M_deallocate(); }
395 _Bvector_impl _M_impl
;
398 _M_allocate(size_t __n
)
399 { return _M_impl
.allocate((__n
+ int(_S_word_bit
) - 1)
400 / int(_S_word_bit
)); }
405 if (_M_impl
._M_start
._M_p
)
406 _M_impl
.deallocate(_M_impl
._M_start
._M_p
,
407 _M_impl
._M_end_of_storage
- _M_impl
._M_start
._M_p
);
411 _GLIBCXX_END_NESTED_NAMESPACE
413 // Declare a partial specialization of vector<T, Alloc>.
414 #include <bits/stl_vector.h>
416 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
419 * @brief A specialization of vector for booleans which offers fixed time
420 * access to individual elements in any order.
422 * Note that vector<bool> does not actually meet the requirements for being
423 * a container. This is because the reference and pointer types are not
424 * really references and pointers to bool. See DR96 for details. @see
425 * vector for function documentation.
427 * @ingroup Containers
430 * In some terminology a %vector can be described as a dynamic
431 * C-style array, it offers fast and efficient access to individual
432 * elements in any order and saves the user from worrying about
433 * memory and size allocation. Subscripting ( @c [] ) access is
434 * also provided as with C-style arrays.
436 template<typename _Alloc
>
437 class vector
<bool, _Alloc
> : protected _Bvector_base
<_Alloc
>
439 typedef _Bvector_base
<_Alloc
> _Base
;
442 typedef bool value_type
;
443 typedef size_t size_type
;
444 typedef ptrdiff_t difference_type
;
445 typedef _Bit_reference reference
;
446 typedef bool const_reference
;
447 typedef _Bit_reference
* pointer
;
448 typedef const bool* const_pointer
;
449 typedef _Bit_iterator iterator
;
450 typedef _Bit_const_iterator const_iterator
;
451 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
452 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
453 typedef _Alloc allocator_type
;
455 allocator_type
get_allocator() const
456 { return _Base::get_allocator(); }
459 using _Base::_M_allocate
;
460 using _Base::_M_deallocate
;
461 using _Base::_M_get_Bit_allocator
;
465 vector(const allocator_type
& __a
= allocator_type())
469 vector(size_type __n
, const bool& __value
= bool(),
470 const allocator_type
& __a
= allocator_type())
474 std::fill(this->_M_impl
._M_start
._M_p
, this->_M_impl
._M_end_of_storage
,
478 vector(const vector
& __x
)
479 : _Base(__x
._M_get_Bit_allocator())
481 _M_initialize(__x
.size());
482 std::copy(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
485 template<class _InputIterator
>
486 vector(_InputIterator __first
, _InputIterator __last
,
487 const allocator_type
& __a
= allocator_type())
490 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
491 _M_initialize_dispatch(__first
, __last
, _Integral());
497 operator=(const vector
& __x
)
501 if (__x
.size() > capacity())
503 this->_M_deallocate();
504 _M_initialize(__x
.size());
506 std::copy(__x
.begin(), __x
.end(), begin());
507 this->_M_impl
._M_finish
= begin() + difference_type(__x
.size());
511 // assign(), a generalized assignment member function. Two
512 // versions: one that takes a count, and one that takes a range.
513 // The range version is a member template, so we dispatch on whether
514 // or not the type is an integer.
516 assign(size_type __n
, const bool& __x
)
517 { _M_fill_assign(__n
, __x
); }
519 template<class _InputIterator
>
521 assign(_InputIterator __first
, _InputIterator __last
)
523 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
524 _M_assign_dispatch(__first
, __last
, _Integral());
529 { return this->_M_impl
._M_start
; }
533 { return this->_M_impl
._M_start
; }
537 { return this->_M_impl
._M_finish
; }
541 { return this->_M_impl
._M_finish
; }
545 { return reverse_iterator(end()); }
547 const_reverse_iterator
549 { return const_reverse_iterator(end()); }
553 { return reverse_iterator(begin()); }
555 const_reverse_iterator
557 { return const_reverse_iterator(begin()); }
561 { return size_type(end() - begin()); }
565 { return size_type(-1); }
569 { return size_type(const_iterator(this->_M_impl
._M_end_of_storage
, 0)
574 { return begin() == end(); }
577 operator[](size_type __n
)
578 { return *(begin() + difference_type(__n
)); }
581 operator[](size_type __n
) const
582 { return *(begin() + difference_type(__n
)); }
586 _M_range_check(size_type __n
) const
588 if (__n
>= this->size())
589 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
595 { _M_range_check(__n
); return (*this)[__n
]; }
598 at(size_type __n
) const
599 { _M_range_check(__n
); return (*this)[__n
]; }
602 reserve(size_type __n
)
604 if (__n
> this->max_size())
605 __throw_length_error(__N("vector::reserve"));
606 if (this->capacity() < __n
)
608 _Bit_type
* __q
= this->_M_allocate(__n
);
609 this->_M_impl
._M_finish
= std::copy(begin(), end(),
611 this->_M_deallocate();
612 this->_M_impl
._M_start
= iterator(__q
, 0);
613 this->_M_impl
._M_end_of_storage
= (__q
+ (__n
+ int(_S_word_bit
) - 1)
628 { return *(end() - 1); }
632 { return *(end() - 1); }
634 // _GLIBCXX_RESOLVE_LIB_DEFECTS
635 // DR 464. Suggestion for new member functions in standard containers.
636 // N.B. DR 464 says nothing about vector<bool> but we need something
637 // here due to the way we are implementing DR 464 in the debug-mode
645 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
646 *this->_M_impl
._M_finish
++ = __x
;
648 _M_insert_aux(end(), __x
);
652 swap(vector
<bool, _Alloc
>& __x
)
654 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
655 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
656 std::swap(this->_M_impl
._M_end_of_storage
,
657 __x
._M_impl
._M_end_of_storage
);
659 // _GLIBCXX_RESOLVE_LIB_DEFECTS
660 // 431. Swapping containers with unequal allocators.
661 std::__alloc_swap
<typename
_Base::_Bit_alloc_type
>::
662 _S_do_it(_M_get_Bit_allocator(), __x
._M_get_Bit_allocator());
665 // [23.2.5]/1, third-to-last entry in synopsis listing
667 swap(reference __x
, reference __y
)
675 insert(iterator __position
, const bool& __x
= bool())
677 const difference_type __n
= __position
- begin();
678 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
679 && __position
== end())
680 *this->_M_impl
._M_finish
++ = __x
;
682 _M_insert_aux(__position
, __x
);
683 return begin() + __n
;
686 template<class _InputIterator
>
688 insert(iterator __position
,
689 _InputIterator __first
, _InputIterator __last
)
691 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
692 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
696 insert(iterator __position
, size_type __n
, const bool& __x
)
697 { _M_fill_insert(__position
, __n
, __x
); }
701 { --this->_M_impl
._M_finish
; }
704 erase(iterator __position
)
706 if (__position
+ 1 != end())
707 std::copy(__position
+ 1, end(), __position
);
708 --this->_M_impl
._M_finish
;
713 erase(iterator __first
, iterator __last
)
715 _M_erase_at_end(std::copy(__last
, end(), __first
));
720 resize(size_type __new_size
, bool __x
= bool())
722 if (__new_size
< size())
723 _M_erase_at_end(begin() + difference_type(__new_size
));
725 insert(end(), __new_size
- size(), __x
);
731 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
;
732 __p
!= this->_M_impl
._M_end_of_storage
; ++__p
)
738 { _M_erase_at_end(begin()); }
744 _M_initialize(size_type __n
)
746 _Bit_type
* __q
= this->_M_allocate(__n
);
747 this->_M_impl
._M_end_of_storage
= (__q
748 + ((__n
+ int(_S_word_bit
) - 1)
749 / int(_S_word_bit
)));
750 this->_M_impl
._M_start
= iterator(__q
, 0);
751 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ difference_type(__n
);
754 // Check whether it's an integral type. If so, it's not an iterator.
755 template<class _Integer
>
757 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
760 std::fill(this->_M_impl
._M_start
._M_p
,
761 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
764 template<class _InputIterator
>
766 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
768 { _M_initialize_range(__first
, __last
,
769 std::__iterator_category(__first
)); }
771 template<class _InputIterator
>
773 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
774 std::input_iterator_tag
)
776 this->_M_impl
._M_start
= iterator();
777 this->_M_impl
._M_finish
= iterator();
778 this->_M_impl
._M_end_of_storage
= 0;
779 for (; __first
!= __last
; ++__first
)
783 template<class _ForwardIterator
>
785 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
786 std::forward_iterator_tag
)
788 const size_type __n
= std::distance(__first
, __last
);
790 std::copy(__first
, __last
, this->_M_impl
._M_start
);
793 template<class _Integer
>
795 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
796 { _M_fill_assign((size_t) __n
, (bool) __val
); }
798 template<class _InputIterator
>
800 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
802 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
805 _M_fill_assign(size_t __n
, bool __x
)
809 std::fill(this->_M_impl
._M_start
._M_p
,
810 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
811 insert(end(), __n
- size(), __x
);
815 _M_erase_at_end(begin() + __n
);
816 std::fill(this->_M_impl
._M_start
._M_p
,
817 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
821 template<class _InputIterator
>
823 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
824 std::input_iterator_tag
)
826 iterator __cur
= begin();
827 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
829 if (__first
== __last
)
830 _M_erase_at_end(__cur
);
832 insert(end(), __first
, __last
);
835 template<class _ForwardIterator
>
837 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
838 std::forward_iterator_tag
)
840 const size_type __len
= std::distance(__first
, __last
);
842 _M_erase_at_end(std::copy(__first
, __last
, begin()));
845 _ForwardIterator __mid
= __first
;
846 std::advance(__mid
, size());
847 std::copy(__first
, __mid
, begin());
848 insert(end(), __mid
, __last
);
852 // Check whether it's an integral type. If so, it's not an iterator.
853 template<class _Integer
>
855 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
857 { _M_fill_insert(__pos
, __n
, __x
); }
859 template<class _InputIterator
>
861 _M_insert_dispatch(iterator __pos
,
862 _InputIterator __first
, _InputIterator __last
,
864 { _M_insert_range(__pos
, __first
, __last
,
865 std::__iterator_category(__first
)); }
868 _M_fill_insert(iterator __position
, size_type __n
, bool __x
)
872 if (capacity() - size() >= __n
)
874 std::copy_backward(__position
, end(),
875 this->_M_impl
._M_finish
+ difference_type(__n
));
876 std::fill(__position
, __position
+ difference_type(__n
), __x
);
877 this->_M_impl
._M_finish
+= difference_type(__n
);
881 const size_type __len
= size() + std::max(size(), __n
);
882 _Bit_type
* __q
= this->_M_allocate(__len
);
883 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
884 std::fill_n(__i
, __n
, __x
);
885 this->_M_impl
._M_finish
= std::copy(__position
, end(),
886 __i
+ difference_type(__n
));
887 this->_M_deallocate();
888 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
889 + int(_S_word_bit
) - 1)
890 / int(_S_word_bit
)));
891 this->_M_impl
._M_start
= iterator(__q
, 0);
895 template<class _InputIterator
>
897 _M_insert_range(iterator __pos
, _InputIterator __first
,
898 _InputIterator __last
, std::input_iterator_tag
)
900 for (; __first
!= __last
; ++__first
)
902 __pos
= insert(__pos
, *__first
);
907 template<class _ForwardIterator
>
909 _M_insert_range(iterator __position
, _ForwardIterator __first
,
910 _ForwardIterator __last
, std::forward_iterator_tag
)
912 if (__first
!= __last
)
914 size_type __n
= std::distance(__first
, __last
);
915 if (capacity() - size() >= __n
)
917 std::copy_backward(__position
, end(),
918 this->_M_impl
._M_finish
919 + difference_type(__n
));
920 std::copy(__first
, __last
, __position
);
921 this->_M_impl
._M_finish
+= difference_type(__n
);
925 const size_type __len
= size() + std::max(size(), __n
);
926 _Bit_type
* __q
= this->_M_allocate(__len
);
927 iterator __i
= std::copy(begin(), __position
,
929 __i
= std::copy(__first
, __last
, __i
);
930 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
931 this->_M_deallocate();
932 this->_M_impl
._M_end_of_storage
= (__q
934 + int(_S_word_bit
) - 1)
935 / int(_S_word_bit
)));
936 this->_M_impl
._M_start
= iterator(__q
, 0);
942 _M_insert_aux(iterator __position
, bool __x
)
944 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
946 std::copy_backward(__position
, this->_M_impl
._M_finish
,
947 this->_M_impl
._M_finish
+ 1);
949 ++this->_M_impl
._M_finish
;
953 const size_type __len
= size() ? 2 * size()
954 : static_cast<size_type
>(_S_word_bit
);
955 _Bit_type
* __q
= this->_M_allocate(__len
);
956 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
958 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
959 this->_M_deallocate();
960 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
961 + int(_S_word_bit
) - 1)
962 / int(_S_word_bit
)));
963 this->_M_impl
._M_start
= iterator(__q
, 0);
968 _M_erase_at_end(iterator __pos
)
969 { this->_M_impl
._M_finish
= __pos
; }
972 _GLIBCXX_END_NESTED_NAMESPACE