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 namespace _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
);
402 // Declare a partial specialization of vector<T, Alloc>.
403 #include <bits/stl_vector.h>
405 namespace _GLIBCXX_STD
408 * @brief A specialization of vector for booleans which offers fixed time
409 * access to individual elements in any order.
411 * Note that vector<bool> does not actually meet the requirements for being
412 * a container. This is because the reference and pointer types are not
413 * really references and pointers to bool. See DR96 for details. @see
414 * vector for function documentation.
416 * @ingroup Containers
419 * In some terminology a %vector can be described as a dynamic
420 * C-style array, it offers fast and efficient access to individual
421 * elements in any order and saves the user from worrying about
422 * memory and size allocation. Subscripting ( @c [] ) access is
423 * also provided as with C-style arrays.
425 template<typename _Alloc
>
426 class vector
<bool, _Alloc
> : public _Bvector_base
<_Alloc
>
429 typedef bool value_type
;
430 typedef size_t size_type
;
431 typedef ptrdiff_t difference_type
;
432 typedef _Bit_reference reference
;
433 typedef bool const_reference
;
434 typedef _Bit_reference
* pointer
;
435 typedef const bool* const_pointer
;
437 typedef _Bit_iterator iterator
;
438 typedef _Bit_const_iterator const_iterator
;
440 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
441 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
443 typedef typename _Bvector_base
<_Alloc
>::allocator_type allocator_type
;
445 allocator_type
get_allocator() const
446 { return _Bvector_base
<_Alloc
>::get_allocator(); }
449 using _Bvector_base
<_Alloc
>::_M_allocate
;
450 using _Bvector_base
<_Alloc
>::_M_deallocate
;
454 _M_initialize(size_type __n
)
456 _Bit_type
* __q
= this->_M_allocate(__n
);
457 this->_M_impl
._M_end_of_storage
= (__q
458 + ((__n
+ int(_S_word_bit
) - 1)
459 / int(_S_word_bit
)));
460 this->_M_impl
._M_start
= iterator(__q
, 0);
461 this->_M_impl
._M_finish
= this->_M_impl
._M_start
+ difference_type(__n
);
465 _M_insert_aux(iterator __position
, bool __x
)
467 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
469 std::copy_backward(__position
, this->_M_impl
._M_finish
,
470 this->_M_impl
._M_finish
+ 1);
472 ++this->_M_impl
._M_finish
;
476 const size_type __len
= size() ? 2 * size()
477 : static_cast<size_type
>(_S_word_bit
);
478 _Bit_type
* __q
= this->_M_allocate(__len
);
479 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
481 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
482 this->_M_deallocate();
483 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
484 + int(_S_word_bit
) - 1)
485 / int(_S_word_bit
)));
486 this->_M_impl
._M_start
= iterator(__q
, 0);
490 template<class _InputIterator
>
492 _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
493 std::input_iterator_tag
)
495 this->_M_impl
._M_start
= iterator();
496 this->_M_impl
._M_finish
= iterator();
497 this->_M_impl
._M_end_of_storage
= 0;
498 for (; __first
!= __last
; ++__first
)
502 template<class _ForwardIterator
>
504 _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
505 std::forward_iterator_tag
)
507 const size_type __n
= std::distance(__first
, __last
);
509 std::copy(__first
, __last
, this->_M_impl
._M_start
);
512 template<class _InputIterator
>
514 _M_insert_range(iterator __pos
, _InputIterator __first
,
515 _InputIterator __last
, std::input_iterator_tag
)
517 for (; __first
!= __last
; ++__first
)
519 __pos
= insert(__pos
, *__first
);
524 template<class _ForwardIterator
>
526 _M_insert_range(iterator __position
, _ForwardIterator __first
,
527 _ForwardIterator __last
, std::forward_iterator_tag
)
529 if (__first
!= __last
)
531 size_type __n
= std::distance(__first
, __last
);
532 if (capacity() - size() >= __n
)
534 std::copy_backward(__position
, end(),
535 this->_M_impl
._M_finish
536 + difference_type(__n
));
537 std::copy(__first
, __last
, __position
);
538 this->_M_impl
._M_finish
+= difference_type(__n
);
542 const size_type __len
= size() + std::max(size(), __n
);
543 _Bit_type
* __q
= this->_M_allocate(__len
);
544 iterator __i
= std::copy(begin(), __position
,
546 __i
= std::copy(__first
, __last
, __i
);
547 this->_M_impl
._M_finish
= std::copy(__position
, end(), __i
);
548 this->_M_deallocate();
549 this->_M_impl
._M_end_of_storage
= (__q
551 + int(_S_word_bit
) - 1)
552 / int(_S_word_bit
)));
553 this->_M_impl
._M_start
= iterator(__q
, 0);
561 { return this->_M_impl
._M_start
; }
565 { return this->_M_impl
._M_start
; }
569 { return this->_M_impl
._M_finish
; }
573 { return this->_M_impl
._M_finish
; }
577 { return reverse_iterator(end()); }
579 const_reverse_iterator
581 { return const_reverse_iterator(end()); }
585 { return reverse_iterator(begin()); }
587 const_reverse_iterator
589 { return const_reverse_iterator(begin()); }
593 { return size_type(end() - begin()); }
597 { return size_type(-1); }
601 { return size_type(const_iterator(this->_M_impl
._M_end_of_storage
, 0)
605 { return begin() == end(); }
608 operator[](size_type __n
)
609 { return *(begin() + difference_type(__n
)); }
612 operator[](size_type __n
) const
613 { return *(begin() + difference_type(__n
)); }
616 _M_range_check(size_type __n
) const
618 if (__n
>= this->size())
619 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
624 { _M_range_check(__n
); return (*this)[__n
]; }
627 at(size_type __n
) const
628 { _M_range_check(__n
); return (*this)[__n
]; }
631 vector(const allocator_type
& __a
= allocator_type())
632 : _Bvector_base
<_Alloc
>(__a
) { }
634 vector(size_type __n
, bool __value
,
635 const allocator_type
& __a
= allocator_type())
636 : _Bvector_base
<_Alloc
>(__a
)
639 std::fill(this->_M_impl
._M_start
._M_p
, this->_M_impl
._M_end_of_storage
,
644 vector(size_type __n
)
645 : _Bvector_base
<_Alloc
>(allocator_type())
648 std::fill(this->_M_impl
._M_start
._M_p
,
649 this->_M_impl
._M_end_of_storage
, 0);
652 vector(const vector
& __x
)
653 : _Bvector_base
<_Alloc
>(__x
.get_allocator())
655 _M_initialize(__x
.size());
656 std::copy(__x
.begin(), __x
.end(), this->_M_impl
._M_start
);
659 // Check whether it's an integral type. If so, it's not an iterator.
660 template<class _Integer
>
662 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
665 std::fill(this->_M_impl
._M_start
._M_p
,
666 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
669 template<class _InputIterator
>
671 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
673 { _M_initialize_range(__first
, __last
,
674 std::__iterator_category(__first
)); }
676 template<class _InputIterator
>
677 vector(_InputIterator __first
, _InputIterator __last
,
678 const allocator_type
& __a
= allocator_type())
679 : _Bvector_base
<_Alloc
>(__a
)
681 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
682 _M_initialize_dispatch(__first
, __last
, _Integral());
688 operator=(const vector
& __x
)
692 if (__x
.size() > capacity())
694 this->_M_deallocate();
695 _M_initialize(__x
.size());
697 std::copy(__x
.begin(), __x
.end(), begin());
698 this->_M_impl
._M_finish
= begin() + difference_type(__x
.size());
702 // assign(), a generalized assignment member function. Two
703 // versions: one that takes a count, and one that takes a range.
704 // The range version is a member template, so we dispatch on whether
705 // or not the type is an integer.
708 _M_fill_assign(size_t __n
, bool __x
)
712 std::fill(this->_M_impl
._M_start
._M_p
,
713 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
714 insert(end(), __n
- size(), __x
);
718 erase(begin() + __n
, end());
719 std::fill(this->_M_impl
._M_start
._M_p
,
720 this->_M_impl
._M_end_of_storage
, __x
? ~0 : 0);
725 assign(size_t __n
, bool __x
)
726 { _M_fill_assign(__n
, __x
); }
728 template<class _InputIterator
>
730 assign(_InputIterator __first
, _InputIterator __last
)
732 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
733 _M_assign_dispatch(__first
, __last
, _Integral());
736 template<class _Integer
>
738 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
739 { _M_fill_assign((size_t) __n
, (bool) __val
); }
741 template<class _InputIterator
>
743 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
745 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
747 template<class _InputIterator
>
749 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
750 std::input_iterator_tag
)
752 iterator __cur
= begin();
753 for (; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
755 if (__first
== __last
)
758 insert(end(), __first
, __last
);
761 template<class _ForwardIterator
>
763 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
764 std::forward_iterator_tag
)
766 const size_type __len
= std::distance(__first
, __last
);
768 erase(std::copy(__first
, __last
, begin()), end());
771 _ForwardIterator __mid
= __first
;
772 std::advance(__mid
, size());
773 std::copy(__first
, __mid
, begin());
774 insert(end(), __mid
, __last
);
779 reserve(size_type __n
)
781 if (__n
> this->max_size())
782 __throw_length_error(__N("vector::reserve"));
783 if (this->capacity() < __n
)
785 _Bit_type
* __q
= this->_M_allocate(__n
);
786 this->_M_impl
._M_finish
= std::copy(begin(), end(),
788 this->_M_deallocate();
789 this->_M_impl
._M_start
= iterator(__q
, 0);
790 this->_M_impl
._M_end_of_storage
= (__q
+ (__n
+ int(_S_word_bit
) - 1)
805 { return *(end() - 1); }
809 { return *(end() - 1); }
811 // _GLIBCXX_RESOLVE_LIB_DEFECTS
812 // DR 464. Suggestion for new member functions in standard containers.
813 // N.B. DR 464 says nothing about vector<bool> but we need something
814 // here due to the way we are implementing DR 464 in the debug-mode
822 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
)
823 *this->_M_impl
._M_finish
++ = __x
;
825 _M_insert_aux(end(), __x
);
829 swap(vector
<bool, _Alloc
>& __x
)
831 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
832 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
833 std::swap(this->_M_impl
._M_end_of_storage
,
834 __x
._M_impl
._M_end_of_storage
);
837 // [23.2.5]/1, third-to-last entry in synopsis listing
839 swap(reference __x
, reference __y
)
847 insert(iterator __position
, bool __x
= bool())
849 const difference_type __n
= __position
- begin();
850 if (this->_M_impl
._M_finish
._M_p
!= this->_M_impl
._M_end_of_storage
851 && __position
== end())
852 *this->_M_impl
._M_finish
++ = __x
;
854 _M_insert_aux(__position
, __x
);
855 return begin() + __n
;
858 // Check whether it's an integral type. If so, it's not an iterator.
860 template<class _Integer
>
862 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
864 { _M_fill_insert(__pos
, __n
, __x
); }
866 template<class _InputIterator
>
868 _M_insert_dispatch(iterator __pos
,
869 _InputIterator __first
, _InputIterator __last
,
871 { _M_insert_range(__pos
, __first
, __last
,
872 std::__iterator_category(__first
)); }
874 template<class _InputIterator
>
876 insert(iterator __position
,
877 _InputIterator __first
, _InputIterator __last
)
879 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
880 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
884 _M_fill_insert(iterator __position
, size_type __n
, bool __x
)
888 if (capacity() - size() >= __n
)
890 std::copy_backward(__position
, end(),
891 this->_M_impl
._M_finish
+ difference_type(__n
));
892 std::fill(__position
, __position
+ difference_type(__n
), __x
);
893 this->_M_impl
._M_finish
+= difference_type(__n
);
897 const size_type __len
= size() + std::max(size(), __n
);
898 _Bit_type
* __q
= this->_M_allocate(__len
);
899 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
900 std::fill_n(__i
, __n
, __x
);
901 this->_M_impl
._M_finish
= std::copy(__position
, end(),
902 __i
+ difference_type(__n
));
903 this->_M_deallocate();
904 this->_M_impl
._M_end_of_storage
= (__q
+ ((__len
905 + int(_S_word_bit
) - 1)
906 / int(_S_word_bit
)));
907 this->_M_impl
._M_start
= iterator(__q
, 0);
912 insert(iterator __position
, size_type __n
, bool __x
)
913 { _M_fill_insert(__position
, __n
, __x
); }
917 { --this->_M_impl
._M_finish
; }
920 erase(iterator __position
)
922 if (__position
+ 1 != end())
923 std::copy(__position
+ 1, end(), __position
);
924 --this->_M_impl
._M_finish
;
929 erase(iterator __first
, iterator __last
)
931 this->_M_impl
._M_finish
= std::copy(__last
, end(), __first
);
936 resize(size_type __new_size
, bool __x
= bool())
938 if (__new_size
< size())
939 erase(begin() + difference_type(__new_size
), end());
941 insert(end(), __new_size
- size(), __x
);
947 for (_Bit_type
* __p
= this->_M_impl
._M_start
._M_p
;
948 __p
!= this->_M_impl
._M_end_of_storage
; ++__p
)
954 { erase(begin(), end()); }