1 // bit_vector and vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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.
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) { }
79 operator bool() const { return !!(*_M_p
& _M_mask
); }
92 operator=(const _Bit_reference
& __x
)
93 { return *this = bool(__x
); }
96 operator==(const _Bit_reference
& __x
) const
97 { return bool(*this) == bool(__x
); }
100 operator<(const _Bit_reference
& __x
) const
101 { return !bool(*this) && bool(__x
); }
104 flip() { *_M_p
^= _M_mask
; }
107 struct _Bit_iterator_base
: public iterator
<random_access_iterator_tag
, bool>
110 unsigned int _M_offset
;
112 _Bit_iterator_base(_Bit_type
* __x
, unsigned int __y
)
113 : _M_p(__x
), _M_offset(__y
) { }
118 if (_M_offset
++ == _S_word_bit
- 1)
128 if (_M_offset
-- == 0)
130 _M_offset
= _S_word_bit
- 1;
136 _M_incr(ptrdiff_t __i
)
138 difference_type __n
= __i
+ _M_offset
;
139 _M_p
+= __n
/ _S_word_bit
;
140 __n
= __n
% _S_word_bit
;
143 _M_offset
= static_cast<unsigned int>(__n
+ _S_word_bit
);
147 _M_offset
= static_cast<unsigned int>(__n
);
151 operator==(const _Bit_iterator_base
& __i
) const
152 { return _M_p
== __i
._M_p
&& _M_offset
== __i
._M_offset
; }
155 operator<(const _Bit_iterator_base
& __i
) const
157 return _M_p
< __i
._M_p
158 || (_M_p
== __i
._M_p
&& _M_offset
< __i
._M_offset
);
162 operator!=(const _Bit_iterator_base
& __i
) const
163 { return !(*this == __i
); }
166 operator>(const _Bit_iterator_base
& __i
) const
167 { return __i
< *this; }
170 operator<=(const _Bit_iterator_base
& __i
) const
171 { return !(__i
< *this); }
174 operator>=(const _Bit_iterator_base
& __i
) const
175 { return !(*this < __i
); }
179 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
)
181 return _S_word_bit
* (__x
._M_p
- __y
._M_p
) + __x
._M_offset
- __y
._M_offset
;
184 struct _Bit_iterator
: public _Bit_iterator_base
186 typedef _Bit_reference reference
;
187 typedef _Bit_reference
* pointer
;
188 typedef _Bit_iterator iterator
;
190 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
191 _Bit_iterator(_Bit_type
* __x
, unsigned int __y
)
192 : _Bit_iterator_base(__x
, __y
) { }
195 operator*() const { return reference(_M_p
, 1UL << _M_offset
); }
207 iterator __tmp
= *this;
222 iterator __tmp
= *this;
228 operator+=(difference_type __i
)
235 operator-=(difference_type __i
)
242 operator+(difference_type __i
) const
244 iterator __tmp
= *this;
249 operator-(difference_type __i
) const
251 iterator __tmp
= *this;
256 operator[](difference_type __i
)
257 { return *(*this + __i
); }
261 operator+(ptrdiff_t __n
, const _Bit_iterator
& __x
) { return __x
+ __n
; }
264 struct _Bit_const_iterator
: public _Bit_iterator_base
266 typedef bool reference
;
267 typedef bool const_reference
;
268 typedef const bool* pointer
;
269 typedef _Bit_const_iterator const_iterator
;
271 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
272 _Bit_const_iterator(_Bit_type
* __x
, unsigned int __y
)
273 : _Bit_iterator_base(__x
, __y
) { }
274 _Bit_const_iterator(const _Bit_iterator
& __x
)
275 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) { }
279 { return _Bit_reference(_M_p
, 1UL << _M_offset
); }
291 const_iterator __tmp
= *this;
306 const_iterator __tmp
= *this;
312 operator+=(difference_type __i
)
319 operator-=(difference_type __i
)
325 const_iterator
operator+(difference_type __i
) const {
326 const_iterator __tmp
= *this;
331 operator-(difference_type __i
) const
333 const_iterator __tmp
= *this;
338 operator[](difference_type __i
)
339 { return *(*this + __i
); }
342 inline _Bit_const_iterator
343 operator+(ptrdiff_t __n
, const _Bit_const_iterator
& __x
)
344 { return __x
+ __n
; }
346 template<class _Alloc
>
348 : public _Alloc::template rebind
<_Bit_type
>::other
350 typedef typename
_Alloc::template rebind
<_Bit_type
>::other
354 typedef _Alloc allocator_type
;
357 get_allocator() const
358 { return *static_cast<const _Bit_alloc_type
*>(this); }
360 _Bvector_base(const allocator_type
& __a
)
361 : _Bit_alloc_type(__a
), _M_start(), _M_finish(), _M_end_of_storage(0) { }
363 ~_Bvector_base() { this->_M_deallocate(); }
367 _M_bit_alloc(size_t __n
)
368 { return _Bit_alloc_type::allocate((__n
+ _S_word_bit
- 1)
375 _Bit_alloc_type::deallocate(_M_start
._M_p
,
376 _M_end_of_storage
- _M_start
._M_p
);
379 _Bit_iterator _M_start
;
380 _Bit_iterator _M_finish
;
381 _Bit_type
* _M_end_of_storage
;
383 } // namespace __gnu_norm
385 // Declare a partial specialization of vector<T, Alloc>.
386 #include <bits/stl_vector.h>
391 * @brief A specialization of vector for booleans which offers fixed time
392 * access to individual elements in any order.
394 * Note that vector<bool> does not actually meet the requirements for being
395 * a container. This is because the reference and pointer types are not
396 * really references and pointers to bool. See DR96 for details. @see
397 * vector for function documentation.
399 * @ingroup Containers
402 * In some terminology a %vector can be described as a dynamic
403 * C-style array, it offers fast and efficient access to individual
404 * elements in any order and saves the user from worrying about
405 * memory and size allocation. Subscripting ( @c [] ) access is
406 * also provided as with C-style arrays.
408 template<typename _Alloc
>
409 class vector
<bool, _Alloc
> : public _Bvector_base
<_Alloc
>
412 typedef bool value_type
;
413 typedef size_t size_type
;
414 typedef ptrdiff_t difference_type
;
415 typedef _Bit_reference reference
;
416 typedef bool const_reference
;
417 typedef _Bit_reference
* pointer
;
418 typedef const bool* const_pointer
;
420 typedef _Bit_iterator iterator
;
421 typedef _Bit_const_iterator const_iterator
;
423 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
424 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
426 typedef typename _Bvector_base
<_Alloc
>::allocator_type allocator_type
;
428 allocator_type
get_allocator() const
429 { return _Bvector_base
<_Alloc
>::get_allocator(); }
432 using _Bvector_base
<_Alloc
>::_M_bit_alloc
;
433 using _Bvector_base
<_Alloc
>::_M_deallocate
;
434 using _Bvector_base
<_Alloc
>::_M_start
;
435 using _Bvector_base
<_Alloc
>::_M_finish
;
436 using _Bvector_base
<_Alloc
>::_M_end_of_storage
;
439 void _M_initialize(size_type __n
)
441 _Bit_type
* __q
= this->_M_bit_alloc(__n
);
442 this->_M_end_of_storage
= __q
+ (__n
+ _S_word_bit
- 1) / _S_word_bit
;
443 this->_M_start
= iterator(__q
, 0);
444 this->_M_finish
= this->_M_start
+ difference_type(__n
);
447 void _M_insert_aux(iterator __position
, bool __x
)
449 if (this->_M_finish
._M_p
!= this->_M_end_of_storage
)
451 std::copy_backward(__position
, this->_M_finish
, this->_M_finish
+ 1);
457 const size_type __len
= size() ? 2 * size()
458 : static_cast<size_type
>(_S_word_bit
);
459 _Bit_type
* __q
= this->_M_bit_alloc(__len
);
460 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
462 this->_M_finish
= std::copy(__position
, end(), __i
);
463 this->_M_deallocate();
464 this->_M_end_of_storage
= __q
+ (__len
+ _S_word_bit
- 1)
466 this->_M_start
= iterator(__q
, 0);
470 template<class _InputIterator
>
471 void _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
474 this->_M_start
= iterator();
475 this->_M_finish
= iterator();
476 this->_M_end_of_storage
= 0;
477 for ( ; __first
!= __last
; ++__first
)
481 template<class _ForwardIterator
>
482 void _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
483 forward_iterator_tag
)
485 const size_type __n
= std::distance(__first
, __last
);
487 std::copy(__first
, __last
, this->_M_start
);
490 template<class _InputIterator
>
491 void _M_insert_range(iterator __pos
,
492 _InputIterator __first
, _InputIterator __last
,
495 for ( ; __first
!= __last
; ++__first
)
497 __pos
= insert(__pos
, *__first
);
502 template<class _ForwardIterator
>
503 void _M_insert_range(iterator __position
,
504 _ForwardIterator __first
, _ForwardIterator __last
,
505 forward_iterator_tag
)
507 if (__first
!= __last
)
509 size_type __n
= std::distance(__first
, __last
);
510 if (capacity() - size() >= __n
)
512 std::copy_backward(__position
, end(),
513 this->_M_finish
+ difference_type(__n
));
514 std::copy(__first
, __last
, __position
);
515 this->_M_finish
+= difference_type(__n
);
519 const size_type __len
= size() + std::max(size(), __n
);
520 _Bit_type
* __q
= this->_M_bit_alloc(__len
);
521 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
522 __i
= std::copy(__first
, __last
, __i
);
523 this->_M_finish
= std::copy(__position
, end(), __i
);
524 this->_M_deallocate();
525 this->_M_end_of_storage
= __q
+ (__len
+ _S_word_bit
- 1)
527 this->_M_start
= iterator(__q
, 0);
534 { return this->_M_start
; }
536 const_iterator
begin() const
537 { return this->_M_start
; }
540 { return this->_M_finish
; }
542 const_iterator
end() const
543 { return this->_M_finish
; }
545 reverse_iterator
rbegin()
546 { return reverse_iterator(end()); }
548 const_reverse_iterator
rbegin() const
549 { return const_reverse_iterator(end()); }
551 reverse_iterator
rend()
552 { return reverse_iterator(begin()); }
554 const_reverse_iterator
rend() const
555 { return const_reverse_iterator(begin()); }
557 size_type
size() const
558 { return size_type(end() - begin()); }
560 size_type
max_size() const
561 { return size_type(-1); }
563 size_type
capacity() const
564 { return size_type(const_iterator(this->_M_end_of_storage
, 0)
567 { return begin() == end(); }
569 reference
operator[](size_type __n
)
570 { return *(begin() + difference_type(__n
)); }
572 const_reference
operator[](size_type __n
) const
573 { return *(begin() + difference_type(__n
)); }
575 void _M_range_check(size_type __n
) const
577 if (__n
>= this->size())
578 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
581 reference
at(size_type __n
)
582 { _M_range_check(__n
); return (*this)[__n
]; }
584 const_reference
at(size_type __n
) const
585 { _M_range_check(__n
); return (*this)[__n
]; }
587 explicit vector(const allocator_type
& __a
= allocator_type())
588 : _Bvector_base
<_Alloc
>(__a
) { }
590 vector(size_type __n
, bool __value
,
591 const allocator_type
& __a
= allocator_type())
592 : _Bvector_base
<_Alloc
>(__a
)
595 std::fill(this->_M_start
._M_p
, this->_M_end_of_storage
, __value
? ~0 : 0);
598 explicit vector(size_type __n
)
599 : _Bvector_base
<_Alloc
>(allocator_type())
602 std::fill(this->_M_start
._M_p
, this->_M_end_of_storage
, 0);
605 vector(const vector
& __x
) : _Bvector_base
<_Alloc
>(__x
.get_allocator())
607 _M_initialize(__x
.size());
608 std::copy(__x
.begin(), __x
.end(), this->_M_start
);
611 // Check whether it's an integral type. If so, it's not an iterator.
613 template<class _Integer
>
614 void _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
617 std::fill(this->_M_start
._M_p
, this->_M_end_of_storage
, __x
? ~0 : 0);
620 template<class _InputIterator
>
621 void _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
623 { _M_initialize_range(__first
, __last
, std::__iterator_category(__first
)); }
625 template<class _InputIterator
>
626 vector(_InputIterator __first
, _InputIterator __last
,
627 const allocator_type
& __a
= allocator_type())
628 : _Bvector_base
<_Alloc
>(__a
)
630 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
631 _M_initialize_dispatch(__first
, __last
, _Integral());
636 vector
& operator=(const vector
& __x
)
640 if (__x
.size() > capacity())
642 this->_M_deallocate();
643 _M_initialize(__x
.size());
645 std::copy(__x
.begin(), __x
.end(), begin());
646 this->_M_finish
= begin() + difference_type(__x
.size());
650 // assign(), a generalized assignment member function. Two
651 // versions: one that takes a count, and one that takes a range.
652 // The range version is a member template, so we dispatch on whether
653 // or not the type is an integer.
655 void _M_fill_assign(size_t __n
, bool __x
)
659 std::fill(this->_M_start
._M_p
, this->_M_end_of_storage
, __x
? ~0 : 0);
660 insert(end(), __n
- size(), __x
);
664 erase(begin() + __n
, end());
665 std::fill(this->_M_start
._M_p
, this->_M_end_of_storage
, __x
? ~0 : 0);
669 void assign(size_t __n
, bool __x
)
670 { _M_fill_assign(__n
, __x
); }
672 template<class _InputIterator
>
673 void assign(_InputIterator __first
, _InputIterator __last
)
675 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
676 _M_assign_dispatch(__first
, __last
, _Integral());
679 template<class _Integer
>
680 void _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
681 { _M_fill_assign((size_t) __n
, (bool) __val
); }
683 template<class _InputIterator
>
684 void _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
686 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
688 template<class _InputIterator
>
689 void _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
692 iterator __cur
= begin();
693 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
695 if (__first
== __last
)
698 insert(end(), __first
, __last
);
701 template<class _ForwardIterator
>
702 void _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
703 forward_iterator_tag
)
705 const size_type __len
= std::distance(__first
, __last
);
707 erase(std::copy(__first
, __last
, begin()), end());
710 _ForwardIterator __mid
= __first
;
711 std::advance(__mid
, size());
712 std::copy(__first
, __mid
, begin());
713 insert(end(), __mid
, __last
);
717 void reserve(size_type __n
)
719 if (__n
> this->max_size())
720 __throw_length_error(__N("vector::reserve"));
721 if (this->capacity() < __n
)
723 _Bit_type
* __q
= this->_M_bit_alloc(__n
);
724 this->_M_finish
= std::copy(begin(), end(), iterator(__q
, 0));
725 this->_M_deallocate();
726 this->_M_start
= iterator(__q
, 0);
727 this->_M_end_of_storage
= __q
+ (__n
+ _S_word_bit
- 1) / _S_word_bit
;
734 const_reference
front() const
738 { return *(end() - 1); }
740 const_reference
back() const
741 { return *(end() - 1); }
743 void push_back(bool __x
)
745 if (this->_M_finish
._M_p
!= this->_M_end_of_storage
)
746 *this->_M_finish
++ = __x
;
748 _M_insert_aux(end(), __x
);
751 void swap(vector
<bool, _Alloc
>& __x
)
753 std::swap(this->_M_start
, __x
._M_start
);
754 std::swap(this->_M_finish
, __x
._M_finish
);
755 std::swap(this->_M_end_of_storage
, __x
._M_end_of_storage
);
758 // [23.2.5]/1, third-to-last entry in synopsis listing
759 static void swap(reference __x
, reference __y
)
766 iterator
insert(iterator __position
, bool __x
= bool())
768 const difference_type __n
= __position
- begin();
769 if (this->_M_finish
._M_p
!= this->_M_end_of_storage
770 && __position
== end())
771 *this->_M_finish
++ = __x
;
773 _M_insert_aux(__position
, __x
);
774 return begin() + __n
;
777 // Check whether it's an integral type. If so, it's not an iterator.
779 template<class _Integer
>
780 void _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
782 { _M_fill_insert(__pos
, __n
, __x
); }
784 template<class _InputIterator
>
785 void _M_insert_dispatch(iterator __pos
,
786 _InputIterator __first
, _InputIterator __last
,
788 { _M_insert_range(__pos
, __first
, __last
,
789 std::__iterator_category(__first
)); }
791 template<class _InputIterator
>
792 void insert(iterator __position
,
793 _InputIterator __first
, _InputIterator __last
)
795 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
796 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
799 void _M_fill_insert(iterator __position
, size_type __n
, bool __x
)
803 if (capacity() - size() >= __n
)
805 std::copy_backward(__position
, end(),
806 this->_M_finish
+ difference_type(__n
));
807 std::fill(__position
, __position
+ difference_type(__n
), __x
);
808 this->_M_finish
+= difference_type(__n
);
812 const size_type __len
= size() + std::max(size(), __n
);
813 _Bit_type
* __q
= this->_M_bit_alloc(__len
);
814 iterator __i
= std::copy(begin(), __position
, iterator(__q
, 0));
815 std::fill_n(__i
, __n
, __x
);
816 this->_M_finish
= std::copy(__position
, end(),
817 __i
+ difference_type(__n
));
818 this->_M_deallocate();
819 this->_M_end_of_storage
= __q
+ (__len
+ _S_word_bit
- 1)
821 this->_M_start
= iterator(__q
, 0);
825 void insert(iterator __position
, size_type __n
, bool __x
)
826 { _M_fill_insert(__position
, __n
, __x
); }
829 { --this->_M_finish
; }
831 iterator
erase(iterator __position
)
833 if (__position
+ 1 != end())
834 std::copy(__position
+ 1, end(), __position
);
839 iterator
erase(iterator __first
, iterator __last
)
841 this->_M_finish
= std::copy(__last
, end(), __first
);
845 void resize(size_type __new_size
, bool __x
= bool())
847 if (__new_size
< size())
848 erase(begin() + difference_type(__new_size
), end());
850 insert(end(), __new_size
- size(), __x
);
855 for (_Bit_type
* __p
= this->_M_start
._M_p
;
856 __p
!= this->_M_end_of_storage
; ++__p
)
861 { erase(begin(), end()); }
863 } // namespace __gnu_norm