1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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.
62 #ifndef _STL_BVECTOR_H
63 #define _STL_BVECTOR_H 1
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
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 __n
+= int(_S_word_bit
);
150 _M_offset
= static_cast<unsigned int>(__n
);
154 operator==(const _Bit_iterator_base
& __i
) const
155 { return _M_p
== __i
._M_p
&& _M_offset
== __i
._M_offset
; }
158 operator<(const _Bit_iterator_base
& __i
) const
160 return _M_p
< __i
._M_p
161 || (_M_p
== __i
._M_p
&& _M_offset
< __i
._M_offset
);
165 operator!=(const _Bit_iterator_base
& __i
) const
166 { return !(*this == __i
); }
169 operator>(const _Bit_iterator_base
& __i
) const
170 { return __i
< *this; }
173 operator<=(const _Bit_iterator_base
& __i
) const
174 { return !(__i
< *this); }
177 operator>=(const _Bit_iterator_base
& __i
) const
178 { return !(*this < __i
); }
182 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
184 return (int(_S_word_bit
) * (__x
._M_p
- __y
._M_p
)
185 + __x
._M_offset
- __y
._M_offset
);
188 struct _Bit_iterator
: public _Bit_iterator_base
190 typedef _Bit_reference reference
;
191 typedef _Bit_reference
* pointer
;
192 typedef _Bit_iterator iterator
;
194 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
196 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
197 : _Bit_iterator_base(__x
, __y
) { }
201 { return reference(_M_p
, 1UL << _M_offset
); }
213 iterator __tmp
= *this;
228 iterator __tmp
= *this;
234 operator+=(difference_type __i
)
241 operator-=(difference_type __i
)
248 operator+(difference_type __i
) const
250 iterator __tmp
= *this;
255 operator-(difference_type __i
) const
257 iterator __tmp
= *this;
262 operator[](difference_type __i
) const
263 { return *(*this + __i
); }
267 operator+(ptrdiff_t __n
, const _Bit_iterator
& __x
)
268 { return __x
+ __n
; }
270 struct _Bit_const_iterator
: public _Bit_iterator_base
272 typedef bool reference
;
273 typedef bool const_reference
;
274 typedef const bool* pointer
;
275 typedef _Bit_const_iterator const_iterator
;
277 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
279 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
280 : _Bit_iterator_base(__x
, __y
) { }
282 _Bit_const_iterator(const _Bit_iterator
& __x
)
283 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
287 { return _Bit_reference(_M_p
, 1UL << _M_offset
); }
299 const_iterator __tmp
= *this;
314 const_iterator __tmp
= *this;
320 operator+=(difference_type __i
)
327 operator-=(difference_type __i
)
334 operator+(difference_type __i
) const
336 const_iterator __tmp
= *this;
341 operator-(difference_type __i
) const
343 const_iterator __tmp
= *this;
348 operator[](difference_type __i
) const
349 { return *(*this + __i
); }
352 inline _Bit_const_iterator
353 operator+(ptrdiff_t __n
, const _Bit_const_iterator
& __x
)
354 { return __x
+ __n
; }
357 __fill_bvector(_Bit_iterator __first
, _Bit_iterator __last
, bool __x
)
359 for (; __first
!= __last
; ++__first
)
364 fill(_Bit_iterator __first
, _Bit_iterator __last
, const bool& __x
)
366 if (__first
._M_p
!= __last
._M_p
)
368 std::fill(__first
._M_p
+ 1, __last
._M_p
, __x
? ~0 : 0);
369 __fill_bvector(__first
, _Bit_iterator(__first
._M_p
+ 1, 0), __x
);
370 __fill_bvector(_Bit_iterator(__last
._M_p
, 0), __last
, __x
);
373 __fill_bvector(__first
, __last
, __x
);
376 template<typename _Alloc
>
379 typedef typename
_Alloc::template rebind
<_Bit_type
>::other
383 : public _Bit_alloc_type
385 _Bit_iterator _M_start
;
386 _Bit_iterator _M_finish
;
387 _Bit_type
* _M_end_of_storage
;
390 : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
393 _Bvector_impl(const _Bit_alloc_type
& __a
)
394 : _Bit_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage(0)
399 typedef _Alloc allocator_type
;
402 _M_get_Bit_allocator()
403 { return *static_cast<_Bit_alloc_type
*>(&this->_M_impl
); }
405 const _Bit_alloc_type
&
406 _M_get_Bit_allocator() const
407 { return *static_cast<const _Bit_alloc_type
*>(&this->_M_impl
); }
410 get_allocator() const
411 { return allocator_type(_M_get_Bit_allocator()); }
416 _Bvector_base(const allocator_type
& __a
)
420 { this->_M_deallocate(); }
423 _Bvector_impl _M_impl
;
426 _M_allocate(size_t __n
)
427 { return _M_impl
.allocate((__n
+ int(_S_word_bit
) - 1)
428 / int(_S_word_bit
)); }
433 if (_M_impl
._M_start
._M_p
)
434 _M_impl
.deallocate(_M_impl
._M_start
._M_p
,
435 _M_impl
._M_end_of_storage
- _M_impl
._M_start
._M_p
);
439 _GLIBCXX_END_NESTED_NAMESPACE
441 // Declare a partial specialization of vector<T, Alloc>.
442 #include <bits/stl_vector.h>
444 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
447 * @brief A specialization of vector for booleans which offers fixed time
448 * access to individual elements in any order.
450 * Note that vector<bool> does not actually meet the requirements for being
451 * a container. This is because the reference and pointer types are not
452 * really references and pointers to bool. See DR96 for details. @see
453 * vector for function documentation.
455 * @ingroup Containers
458 * In some terminology a %vector can be described as a dynamic
459 * C-style array, it offers fast and efficient access to individual
460 * elements in any order and saves the user from worrying about
461 * memory and size allocation. Subscripting ( @c [] ) access is
462 * also provided as with C-style arrays.
464 template<typename _Alloc
>
465 class vector
<bool, _Alloc
> : protected _Bvector_base
<_Alloc
>
467 typedef _Bvector_base
<_Alloc
> _Base
;
470 typedef bool value_type
;
471 typedef size_t size_type
;
472 typedef ptrdiff_t difference_type
;
473 typedef _Bit_reference reference
;
474 typedef bool const_reference
;
475 typedef _Bit_reference
* pointer
;
476 typedef const bool* const_pointer
;
477 typedef _Bit_iterator iterator
;
478 typedef _Bit_const_iterator const_iterator
;
479 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
480 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
481 typedef _Alloc allocator_type
;
483 allocator_type
get_allocator() const
484 { return _Base::get_allocator(); }
487 using _Base::_M_allocate
;
488 using _Base::_M_deallocate
;
489 using _Base::_M_get_Bit_allocator
;
496 vector(const allocator_type
& __a
)
500 vector(size_type __n
, const bool& __value
= bool(),
501 const allocator_type
& __a
= allocator_type())
505 std::fill(this->_M_impl
._M_start
._M_p
, this->_M_impl
._M_end_of_storage
,
509 vector(const vector
& __x
)
510 : _Base(__x
._M_get_Bit_allocator())
512 _M_initialize(__x
.size());
513 _M_copy_aligned(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
516 #ifdef __GXX_EXPERIMENTAL_CXX0X__
518 : _Base(__x
._M_get_Bit_allocator())
522 template<typename _InputIterator
>
523 vector(_InputIterator __first
, _InputIterator __last
,
524 const allocator_type
& __a
= allocator_type())
527 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
528 _M_initialize_dispatch(__first
, __last
, _Integral());
534 operator=(const vector
& __x
)
538 if (__x
.size() > capacity())
540 this->_M_deallocate();
541 _M_initialize(__x
.size());
543 this->_M_impl
._M_finish
= _M_copy_aligned(__x
.begin(), __x
.end(),
548 #ifdef __GXX_EXPERIMENTAL_CXX0X__
550 operator=(vector
&& __x
)
557 // assign(), a generalized assignment member function. Two
558 // versions: one that takes a count, and one that takes a range.
559 // The range version is a member template, so we dispatch on whether
560 // or not the type is an integer.
562 assign(size_type __n
, const bool& __x
)
563 { _M_fill_assign(__n
, __x
); }
565 template<typename _InputIterator
>
567 assign(_InputIterator __first
, _InputIterator __last
)
569 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
570 _M_assign_dispatch(__first
, __last
, _Integral());
575 { return this->_M_impl
._M_start
; }
579 { return this->_M_impl
._M_start
; }
583 { return this->_M_impl
._M_finish
; }
587 { return this->_M_impl
._M_finish
; }
591 { return reverse_iterator(end()); }
593 const_reverse_iterator
595 { return const_reverse_iterator(end()); }
599 { return reverse_iterator(begin()); }
601 const_reverse_iterator
603 { return const_reverse_iterator(begin()); }
605 #ifdef __GXX_EXPERIMENTAL_CXX0X__
608 { return this->_M_impl
._M_start
; }
612 { return this->_M_impl
._M_finish
; }
614 const_reverse_iterator
616 { return const_reverse_iterator(end()); }
618 const_reverse_iterator
620 { return const_reverse_iterator(begin()); }
625 { return size_type(end() - begin()); }
630 const size_type __isize
=
631 __gnu_cxx::__numeric_traits
<difference_type
>::__max
632 - int(_S_word_bit
) + 1;
633 const size_type __asize
= _M_get_Bit_allocator().max_size();
634 return (__asize
<= __isize
/ int(_S_word_bit
)
635 ? __asize
* int(_S_word_bit
) : __isize
);
640 { return size_type(const_iterator(this->_M_impl
._M_end_of_storage
, 0)
645 { return begin() == end(); }
648 operator[](size_type __n
)
650 return *iterator(this->_M_impl
._M_start
._M_p
651 + __n
/ int(_S_word_bit
), __n
% int(_S_word_bit
));
655 operator[](size_type __n
) const
657 return *const_iterator(this->_M_impl
._M_start
._M_p
658 + __n
/ int(_S_word_bit
), __n
% int(_S_word_bit
));
663 _M_range_check(size_type __n
) const
665 if (__n
>= this->size())
666 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
672 { _M_range_check(__n
); return (*this)[__n
]; }
675 at(size_type __n
) const
676 { _M_range_check(__n
); return (*this)[__n
]; }
679 reserve(size_type __n
)
681 if (__n
> this->max_size())
682 __throw_length_error(__N("vector::reserve"));
683 if (this->capacity() < __n
)
685 _Bit_type
* __q
= this->_M_allocate(__n
);
686 this->_M_impl
._M_finish
= _M_copy_aligned(begin(), end(),
688 this->_M_deallocate();
689 this->_M_impl
._M_start
= iterator(__q
, 0);
690 this->_M_impl
._M_end_of_storage
= (__q
+ (__n
+ int(_S_word_bit
) - 1)
705 { return *(end() - 1); }
709 { return *(end() - 1); }
711 // _GLIBCXX_RESOLVE_LIB_DEFECTS
712 // DR 464. Suggestion for new member functions in standard containers.
713 // N.B. DR 464 says nothing about vector<bool> but we need something
714 // here due to the way we are implementing DR 464 in the debug-mode
722 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
723 *this->_M_impl
._M_finish
++ = __x
;
725 _M_insert_aux(end(), __x
);
729 #ifdef __GXX_EXPERIMENTAL_CXX0X__
735 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
736 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
737 std::swap(this->_M_impl
._M_end_of_storage
,
738 __x
._M_impl
._M_end_of_storage
);
740 // _GLIBCXX_RESOLVE_LIB_DEFECTS
741 // 431. Swapping containers with unequal allocators.
742 std::__alloc_swap
<typename
_Base::_Bit_alloc_type
>::
743 _S_do_it(_M_get_Bit_allocator(), __x
._M_get_Bit_allocator());
746 // [23.2.5]/1, third-to-last entry in synopsis listing
748 swap(reference __x
, reference __y
)
756 insert(iterator __position
, const bool& __x
= bool())
758 const difference_type __n
= __position
- begin();
759 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
760 && __position
== end())
761 *this->_M_impl
._M_finish
++ = __x
;
763 _M_insert_aux(__position
, __x
);
764 return begin() + __n
;
767 template<typename _InputIterator
>
769 insert(iterator __position
,
770 _InputIterator __first
, _InputIterator __last
)
772 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
773 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
777 insert(iterator __position
, size_type __n
, const bool& __x
)
778 { _M_fill_insert(__position
, __n
, __x
); }
782 { --this->_M_impl
._M_finish
; }
785 erase(iterator __position
)
787 if (__position
+ 1 != end())
788 std::copy(__position
+ 1, end(), __position
);
789 --this->_M_impl
._M_finish
;
794 erase(iterator __first
, iterator __last
)
796 _M_erase_at_end(std::copy(__last
, end(), __first
));
801 resize(size_type __new_size
, bool __x
= bool())
803 if (__new_size
< size())
804 _M_erase_at_end(begin() + difference_type(__new_size
));
806 insert(end(), __new_size
- size(), __x
);
812 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
;
813 __p
!= this->_M_impl
._M_end_of_storage
; ++__p
)
819 { _M_erase_at_end(begin()); }
823 // Precondition: __first._M_offset == 0 && __result._M_offset == 0.
825 _M_copy_aligned(const_iterator __first
, const_iterator __last
,
828 _Bit_type
* __q
= std::copy(__first
._M_p
, __last
._M_p
, __result
._M_p
);
829 return std::copy(const_iterator(__last
._M_p
, 0), __last
,
834 _M_initialize(size_type __n
)
836 _Bit_type
* __q
= this->_M_allocate(__n
);
837 this->_M_impl
._M_end_of_storage
= (__q
838 + ((__n
+ int(_S_word_bit
) - 1)
839 / int(_S_word_bit
)));
840 this->_M_impl
._M_start
= iterator(__q
, 0);
841 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ difference_type(__n
);
844 // Check whether it's an integral type. If so, it's not an iterator.
846 // _GLIBCXX_RESOLVE_LIB_DEFECTS
847 // 438. Ambiguity in the "do the right thing" clause
848 template<typename _Integer
>
850 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
852 _M_initialize(static_cast<size_type
>(__n
));
853 std::fill(this->_M_impl
._M_start
._M_p
,
854 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
857 template<typename _InputIterator
>
859 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
861 { _M_initialize_range(__first
, __last
,
862 std::__iterator_category(__first
)); }
864 template<typename _InputIterator
>
866 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
867 std::input_iterator_tag
)
869 for (; __first
!= __last
; ++__first
)
873 template<typename _ForwardIterator
>
875 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
876 std::forward_iterator_tag
)
878 const size_type __n
= std::distance(__first
, __last
);
880 std::copy(__first
, __last
, this->_M_impl
._M_start
);
883 // _GLIBCXX_RESOLVE_LIB_DEFECTS
884 // 438. Ambiguity in the "do the right thing" clause
885 template<typename _Integer
>
887 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
888 { _M_fill_assign(__n
, __val
); }
890 template<class _InputIterator
>
892 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
894 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
897 _M_fill_assign(size_t __n
, bool __x
)
901 std::fill(this->_M_impl
._M_start
._M_p
,
902 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
903 insert(end(), __n
- size(), __x
);
907 _M_erase_at_end(begin() + __n
);
908 std::fill(this->_M_impl
._M_start
._M_p
,
909 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
913 template<typename _InputIterator
>
915 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
916 std::input_iterator_tag
)
918 iterator __cur
= begin();
919 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
921 if (__first
== __last
)
922 _M_erase_at_end(__cur
);
924 insert(end(), __first
, __last
);
927 template<typename _ForwardIterator
>
929 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
930 std::forward_iterator_tag
)
932 const size_type __len
= std::distance(__first
, __last
);
934 _M_erase_at_end(std::copy(__first
, __last
, begin()));
937 _ForwardIterator __mid
= __first
;
938 std::advance(__mid
, size());
939 std::copy(__first
, __mid
, begin());
940 insert(end(), __mid
, __last
);
944 // Check whether it's an integral type. If so, it's not an iterator.
946 // _GLIBCXX_RESOLVE_LIB_DEFECTS
947 // 438. Ambiguity in the "do the right thing" clause
948 template<typename _Integer
>
950 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
952 { _M_fill_insert(__pos
, __n
, __x
); }
954 template<typename _InputIterator
>
956 _M_insert_dispatch(iterator __pos
,
957 _InputIterator __first
, _InputIterator __last
,
959 { _M_insert_range(__pos
, __first
, __last
,
960 std::__iterator_category(__first
)); }
963 _M_fill_insert(iterator __position
, size_type __n
, bool __x
);
965 template<typename _InputIterator
>
967 _M_insert_range(iterator __pos
, _InputIterator __first
,
968 _InputIterator __last
, std::input_iterator_tag
)
970 for (; __first
!= __last
; ++__first
)
972 __pos
= insert(__pos
, *__first
);
977 template<typename _ForwardIterator
>
979 _M_insert_range(iterator __position
, _ForwardIterator __first
,
980 _ForwardIterator __last
, std::forward_iterator_tag
);
983 _M_insert_aux(iterator __position
, bool __x
);
986 _M_check_len(size_type __n
, const char* __s
) const
988 if (max_size() - size() < __n
)
989 __throw_length_error(__N(__s
));
991 const size_type __len
= size() + std::max(size(), __n
);
992 return (__len
< size() || __len
> max_size()) ? max_size() : __len
;
996 _M_erase_at_end(iterator __pos
)
997 { this->_M_impl
._M_finish
= __pos
; }
1000 _GLIBCXX_END_NESTED_NAMESPACE