1 // vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996-1999
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_bvector.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
66 typedef unsigned long _Bit_type
;
67 enum { _S_word_bit
= int(CHAR_BIT
* sizeof(_Bit_type
)) };
74 _Bit_reference(_Bit_type
* __x
, _Bit_type __y
)
75 : _M_p(__x
), _M_mask(__y
) { }
77 _Bit_reference() : _M_p(0), _M_mask(0) { }
80 { return !!(*_M_p
& _M_mask
); }
93 operator=(const _Bit_reference
& __x
)
94 { return *this = bool(__x
); }
97 operator==(const _Bit_reference
& __x
) const
98 { return bool(*this) == bool(__x
); }
101 operator<(const _Bit_reference
& __x
) const
102 { return !bool(*this) && bool(__x
); }
106 { *_M_p
^= _M_mask
; }
109 struct _Bit_iterator_base
110 : public std::iterator
<std::random_access_iterator_tag
, bool>
113 unsigned int _M_offset
;
115 _Bit_iterator_base(_Bit_type
* __x
, unsigned int __y
)
116 : _M_p(__x
), _M_offset(__y
) { }
121 if (_M_offset
++ == int(_S_word_bit
) - 1)
131 if (_M_offset
-- == 0)
133 _M_offset
= int(_S_word_bit
) - 1;
139 _M_incr(ptrdiff_t __i
)
141 difference_type __n
= __i
+ _M_offset
;
142 _M_p
+= __n
/ int(_S_word_bit
);
143 __n
= __n
% int(_S_word_bit
);
146 _M_offset
= static_cast<unsigned int>(__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
; }
356 template<class _Alloc
>
359 typedef typename
_Alloc::template rebind
<_Bit_type
>::other
362 struct _Bvector_impl
: public _Bit_alloc_type
364 _Bit_iterator _M_start
;
365 _Bit_iterator _M_finish
;
366 _Bit_type
* _M_end_of_storage
;
367 _Bvector_impl(const _Bit_alloc_type
& __a
)
368 : _Bit_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage(0)
373 typedef _Alloc allocator_type
;
376 get_allocator() const
377 { return *static_cast<const _Bit_alloc_type
*>(&this->_M_impl
); }
379 _Bvector_base(const allocator_type
& __a
) : _M_impl(__a
) { }
382 { this->_M_deallocate(); }
385 _Bvector_impl _M_impl
;
388 _M_allocate(size_t __n
)
389 { return _M_impl
.allocate((__n
+ int(_S_word_bit
) - 1)
390 / int(_S_word_bit
)); }
395 if (_M_impl
._M_start
._M_p
)
396 _M_impl
.deallocate(_M_impl
._M_start
._M_p
,
397 _M_impl
._M_end_of_storage
- _M_impl
._M_start
._M_p
);
401 _GLIBCXX_END_NESTED_NAMESPACE
403 // Declare a partial specialization of vector<T, Alloc>.
404 #include <bits/stl_vector.h>
406 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
409 * @brief A specialization of vector for booleans which offers fixed time
410 * access to individual elements in any order.
412 * Note that vector<bool> does not actually meet the requirements for being
413 * a container. This is because the reference and pointer types are not
414 * really references and pointers to bool. See DR96 for details. @see
415 * vector for function documentation.
417 * @ingroup Containers
420 * In some terminology a %vector can be described as a dynamic
421 * C-style array, it offers fast and efficient access to individual
422 * elements in any order and saves the user from worrying about
423 * memory and size allocation. Subscripting ( @c [] ) access is
424 * also provided as with C-style arrays.
426 template<typename _Alloc
>
427 class vector
<bool, _Alloc
> : public _Bvector_base
<_Alloc
>
430 typedef bool value_type
;
431 typedef size_t size_type
;
432 typedef ptrdiff_t difference_type
;
433 typedef _Bit_reference reference
;
434 typedef bool const_reference
;
435 typedef _Bit_reference
* pointer
;
436 typedef const bool* const_pointer
;
438 typedef _Bit_iterator iterator
;
439 typedef _Bit_const_iterator const_iterator
;
441 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
442 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
444 typedef typename _Bvector_base
<_Alloc
>::allocator_type allocator_type
;
446 allocator_type
get_allocator() const
447 { return _Bvector_base
<_Alloc
>::get_allocator(); }
450 using _Bvector_base
<_Alloc
>::_M_allocate
;
451 using _Bvector_base
<_Alloc
>::_M_deallocate
;
455 _M_initialize(size_type __n
)
457 _Bit_type
* __q
= this->_M_allocate(__n
);
458 this->_M_impl
._M_end_of_storage
= (__q
459 + ((__n
+ int(_S_word_bit
) - 1)
460 / int(_S_word_bit
)));
461 this->_M_impl
._M_start
= iterator(__q
, 0);
462 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ difference_type(__n
);
466 _M_insert_aux(iterator __position
, bool __x
)
468 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
470 std::copy_backward(__position
, this->_M_impl
._M_finish
,
471 this->_M_impl
._M_finish
+ 1);
473 ++this->_M_impl
._M_finish
;
477 const size_type __len
= size() ? 2 * size()
478 : static_cast<size_type
>(_S_word_bit
);
479 _Bit_type
* __q
= this->_M_allocate(__len
);
480 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
482 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
483 this->_M_deallocate();
484 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
485 + int(_S_word_bit
) - 1)
486 / int(_S_word_bit
)));
487 this->_M_impl
._M_start
= iterator(__q
, 0);
491 template<class _InputIterator
>
493 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
494 std::input_iterator_tag
)
496 this->_M_impl
._M_start
= iterator();
497 this->_M_impl
._M_finish
= iterator();
498 this->_M_impl
._M_end_of_storage
= 0;
499 for (; __first
!= __last
; ++__first
)
503 template<class _ForwardIterator
>
505 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
506 std::forward_iterator_tag
)
508 const size_type __n
= std::distance(__first
, __last
);
510 std::copy(__first
, __last
, this->_M_impl
._M_start
);
513 template<class _InputIterator
>
515 _M_insert_range(iterator __pos
, _InputIterator __first
,
516 _InputIterator __last
, std::input_iterator_tag
)
518 for (; __first
!= __last
; ++__first
)
520 __pos
= insert(__pos
, *__first
);
525 template<class _ForwardIterator
>
527 _M_insert_range(iterator __position
, _ForwardIterator __first
,
528 _ForwardIterator __last
, std::forward_iterator_tag
)
530 if (__first
!= __last
)
532 size_type __n
= std::distance(__first
, __last
);
533 if (capacity() - size() >= __n
)
535 std::copy_backward(__position
, end(),
536 this->_M_impl
._M_finish
537 + difference_type(__n
));
538 std::copy(__first
, __last
, __position
);
539 this->_M_impl
._M_finish
+= difference_type(__n
);
543 const size_type __len
= size() + std::max(size(), __n
);
544 _Bit_type
* __q
= this->_M_allocate(__len
);
545 iterator __i
= std::copy(begin(), __position
,
547 __i
= std::copy(__first
, __last
, __i
);
548 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
549 this->_M_deallocate();
550 this->_M_impl
._M_end_of_storage
= (__q
552 + int(_S_word_bit
) - 1)
553 / int(_S_word_bit
)));
554 this->_M_impl
._M_start
= iterator(__q
, 0);
562 { return this->_M_impl
._M_start
; }
566 { return this->_M_impl
._M_start
; }
570 { return this->_M_impl
._M_finish
; }
574 { return this->_M_impl
._M_finish
; }
578 { return reverse_iterator(end()); }
580 const_reverse_iterator
582 { return const_reverse_iterator(end()); }
586 { return reverse_iterator(begin()); }
588 const_reverse_iterator
590 { return const_reverse_iterator(begin()); }
594 { return size_type(end() - begin()); }
598 { return size_type(-1); }
602 { return size_type(const_iterator(this->_M_impl
._M_end_of_storage
, 0)
606 { return begin() == end(); }
609 operator[](size_type __n
)
610 { return *(begin() + difference_type(__n
)); }
613 operator[](size_type __n
) const
614 { return *(begin() + difference_type(__n
)); }
617 _M_range_check(size_type __n
) const
619 if (__n
>= this->size())
620 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
625 { _M_range_check(__n
); return (*this)[__n
]; }
628 at(size_type __n
) const
629 { _M_range_check(__n
); return (*this)[__n
]; }
632 vector(const allocator_type
& __a
= allocator_type())
633 : _Bvector_base
<_Alloc
>(__a
) { }
635 vector(size_type __n
, bool __value
,
636 const allocator_type
& __a
= allocator_type())
637 : _Bvector_base
<_Alloc
>(__a
)
640 std::fill(this->_M_impl
._M_start
._M_p
, this->_M_impl
._M_end_of_storage
,
645 vector(size_type __n
)
646 : _Bvector_base
<_Alloc
>(allocator_type())
649 std::fill(this->_M_impl
._M_start
._M_p
,
650 this->_M_impl
._M_end_of_storage
, 0);
653 vector(const vector
& __x
)
654 : _Bvector_base
<_Alloc
>(__x
.get_allocator())
656 _M_initialize(__x
.size());
657 std::copy(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
660 // Check whether it's an integral type. If so, it's not an iterator.
661 template<class _Integer
>
663 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
666 std::fill(this->_M_impl
._M_start
._M_p
,
667 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
670 template<class _InputIterator
>
672 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
674 { _M_initialize_range(__first
, __last
,
675 std::__iterator_category(__first
)); }
677 template<class _InputIterator
>
678 vector(_InputIterator __first
, _InputIterator __last
,
679 const allocator_type
& __a
= allocator_type())
680 : _Bvector_base
<_Alloc
>(__a
)
682 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
683 _M_initialize_dispatch(__first
, __last
, _Integral());
689 operator=(const vector
& __x
)
693 if (__x
.size() > capacity())
695 this->_M_deallocate();
696 _M_initialize(__x
.size());
698 std::copy(__x
.begin(), __x
.end(), begin());
699 this->_M_impl
._M_finish
= begin() + difference_type(__x
.size());
703 // assign(), a generalized assignment member function. Two
704 // versions: one that takes a count, and one that takes a range.
705 // The range version is a member template, so we dispatch on whether
706 // or not the type is an integer.
709 _M_fill_assign(size_t __n
, bool __x
)
713 std::fill(this->_M_impl
._M_start
._M_p
,
714 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
715 insert(end(), __n
- size(), __x
);
719 erase(begin() + __n
, end());
720 std::fill(this->_M_impl
._M_start
._M_p
,
721 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
726 assign(size_t __n
, bool __x
)
727 { _M_fill_assign(__n
, __x
); }
729 template<class _InputIterator
>
731 assign(_InputIterator __first
, _InputIterator __last
)
733 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
734 _M_assign_dispatch(__first
, __last
, _Integral());
737 template<class _Integer
>
739 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
740 { _M_fill_assign((size_t) __n
, (bool) __val
); }
742 template<class _InputIterator
>
744 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
746 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
748 template<class _InputIterator
>
750 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
751 std::input_iterator_tag
)
753 iterator __cur
= begin();
754 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
756 if (__first
== __last
)
759 insert(end(), __first
, __last
);
762 template<class _ForwardIterator
>
764 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
765 std::forward_iterator_tag
)
767 const size_type __len
= std::distance(__first
, __last
);
769 erase(std::copy(__first
, __last
, begin()), end());
772 _ForwardIterator __mid
= __first
;
773 std::advance(__mid
, size());
774 std::copy(__first
, __mid
, begin());
775 insert(end(), __mid
, __last
);
780 reserve(size_type __n
)
782 if (__n
> this->max_size())
783 __throw_length_error(__N("vector::reserve"));
784 if (this->capacity() < __n
)
786 _Bit_type
* __q
= this->_M_allocate(__n
);
787 this->_M_impl
._M_finish
= std::copy(begin(), end(),
789 this->_M_deallocate();
790 this->_M_impl
._M_start
= iterator(__q
, 0);
791 this->_M_impl
._M_end_of_storage
= (__q
+ (__n
+ int(_S_word_bit
) - 1)
806 { return *(end() - 1); }
810 { return *(end() - 1); }
812 // _GLIBCXX_RESOLVE_LIB_DEFECTS
813 // DR 464. Suggestion for new member functions in standard containers.
814 // N.B. DR 464 says nothing about vector<bool> but we need something
815 // here due to the way we are implementing DR 464 in the debug-mode
823 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
824 *this->_M_impl
._M_finish
++ = __x
;
826 _M_insert_aux(end(), __x
);
830 swap(vector
<bool, _Alloc
>& __x
)
832 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
833 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
834 std::swap(this->_M_impl
._M_end_of_storage
,
835 __x
._M_impl
._M_end_of_storage
);
838 // [23.2.5]/1, third-to-last entry in synopsis listing
840 swap(reference __x
, reference __y
)
848 insert(iterator __position
, bool __x
= bool())
850 const difference_type __n
= __position
- begin();
851 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
852 && __position
== end())
853 *this->_M_impl
._M_finish
++ = __x
;
855 _M_insert_aux(__position
, __x
);
856 return begin() + __n
;
859 // Check whether it's an integral type. If so, it's not an iterator.
861 template<class _Integer
>
863 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
865 { _M_fill_insert(__pos
, __n
, __x
); }
867 template<class _InputIterator
>
869 _M_insert_dispatch(iterator __pos
,
870 _InputIterator __first
, _InputIterator __last
,
872 { _M_insert_range(__pos
, __first
, __last
,
873 std::__iterator_category(__first
)); }
875 template<class _InputIterator
>
877 insert(iterator __position
,
878 _InputIterator __first
, _InputIterator __last
)
880 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
881 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
885 _M_fill_insert(iterator __position
, size_type __n
, bool __x
)
889 if (capacity() - size() >= __n
)
891 std::copy_backward(__position
, end(),
892 this->_M_impl
._M_finish
+ difference_type(__n
));
893 std::fill(__position
, __position
+ difference_type(__n
), __x
);
894 this->_M_impl
._M_finish
+= difference_type(__n
);
898 const size_type __len
= size() + std::max(size(), __n
);
899 _Bit_type
* __q
= this->_M_allocate(__len
);
900 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
901 std::fill_n(__i
, __n
, __x
);
902 this->_M_impl
._M_finish
= std::copy(__position
, end(),
903 __i
+ difference_type(__n
));
904 this->_M_deallocate();
905 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
906 + int(_S_word_bit
) - 1)
907 / int(_S_word_bit
)));
908 this->_M_impl
._M_start
= iterator(__q
, 0);
913 insert(iterator __position
, size_type __n
, bool __x
)
914 { _M_fill_insert(__position
, __n
, __x
); }
918 { --this->_M_impl
._M_finish
; }
921 erase(iterator __position
)
923 if (__position
+ 1 != end())
924 std::copy(__position
+ 1, end(), __position
);
925 --this->_M_impl
._M_finish
;
930 erase(iterator __first
, iterator __last
)
932 this->_M_impl
._M_finish
= std::copy(__last
, end(), __first
);
937 resize(size_type __new_size
, bool __x
= bool())
939 if (__new_size
< size())
940 erase(begin() + difference_type(__new_size
), end());
942 insert(end(), __new_size
- size(), __x
);
948 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
;
949 __p
!= this->_M_impl
._M_end_of_storage
; ++__p
)
955 { erase(begin(), end()); }
958 _GLIBCXX_END_NESTED_NAMESPACE