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, 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.
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 : public iterator<random_access_iterator_tag, bool>
112 unsigned int _M_offset;
114 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
115 : _M_p(__x), _M_offset(__y) { }
120 if (_M_offset++ == _S_word_bit - 1)
130 if (_M_offset-- == 0)
132 _M_offset = _S_word_bit - 1;
138 _M_incr(ptrdiff_t __i)
140 difference_type __n = __i + _M_offset;
141 _M_p += __n / _S_word_bit;
142 __n = __n % _S_word_bit;
145 _M_offset = static_cast<unsigned int>(__n + _S_word_bit);
149 _M_offset = static_cast<unsigned int>(__n);
153 operator==(const _Bit_iterator_base& __i) const
154 { return _M_p == __i._M_p && _M_offset == __i._M_offset; }
157 operator<(const _Bit_iterator_base& __i) const
159 return _M_p < __i._M_p
160 || (_M_p == __i._M_p && _M_offset < __i._M_offset);
164 operator!=(const _Bit_iterator_base& __i) const
165 { return !(*this == __i); }
168 operator>(const _Bit_iterator_base& __i) const
169 { return __i < *this; }
172 operator<=(const _Bit_iterator_base& __i) const
173 { return !(__i < *this); }
176 operator>=(const _Bit_iterator_base& __i) const
177 { return !(*this < __i); }
181 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
183 return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
186 struct _Bit_iterator : public _Bit_iterator_base
188 typedef _Bit_reference reference;
189 typedef _Bit_reference* pointer;
190 typedef _Bit_iterator iterator;
192 _Bit_iterator() : _Bit_iterator_base(0, 0) { }
194 _Bit_iterator(_Bit_type * __x, unsigned int __y)
195 : _Bit_iterator_base(__x, __y) { }
199 { return reference(_M_p, 1UL << _M_offset); }
211 iterator __tmp = *this;
226 iterator __tmp = *this;
232 operator+=(difference_type __i)
239 operator-=(difference_type __i)
246 operator+(difference_type __i) const
248 iterator __tmp = *this;
253 operator-(difference_type __i) const
255 iterator __tmp = *this;
260 operator[](difference_type __i)
261 { return *(*this + __i); }
265 operator+(ptrdiff_t __n, const _Bit_iterator& __x)
266 { return __x + __n; }
268 struct _Bit_const_iterator : public _Bit_iterator_base
270 typedef bool reference;
271 typedef bool const_reference;
272 typedef const bool* pointer;
273 typedef _Bit_const_iterator const_iterator;
275 _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
277 _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
278 : _Bit_iterator_base(__x, __y) { }
280 _Bit_const_iterator(const _Bit_iterator& __x)
281 : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
285 { return _Bit_reference(_M_p, 1UL << _M_offset); }
297 const_iterator __tmp = *this;
312 const_iterator __tmp = *this;
318 operator+=(difference_type __i)
325 operator-=(difference_type __i)
332 operator+(difference_type __i) const
334 const_iterator __tmp = *this;
339 operator-(difference_type __i) const
341 const_iterator __tmp = *this;
346 operator[](difference_type __i)
347 { return *(*this + __i); }
350 inline _Bit_const_iterator
351 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x)
352 { return __x + __n; }
354 template<class _Alloc>
357 typedef typename _Alloc::template rebind<_Bit_type>::other
360 struct _Bvector_impl : public _Bit_alloc_type
362 _Bit_iterator _M_start;
363 _Bit_iterator _M_finish;
364 _Bit_type* _M_end_of_storage;
365 _Bvector_impl(const _Bit_alloc_type& __a)
366 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
371 typedef _Alloc allocator_type;
374 get_allocator() const
375 { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); }
377 _Bvector_base(const allocator_type& __a) : _M_impl(__a) { }
380 { this->_M_deallocate(); }
383 _Bvector_impl _M_impl;
386 _M_allocate(size_t __n)
387 { return _M_impl.allocate((__n + _S_word_bit - 1) / _S_word_bit); }
392 if (_M_impl._M_start._M_p)
393 _M_impl.deallocate(_M_impl._M_start._M_p,
394 _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
399 // Declare a partial specialization of vector<T, Alloc>.
400 #include <bits/stl_vector.h>
402 namespace _GLIBCXX_STD
405 * @brief A specialization of vector for booleans which offers fixed time
406 * access to individual elements in any order.
408 * Note that vector<bool> does not actually meet the requirements for being
409 * a container. This is because the reference and pointer types are not
410 * really references and pointers to bool. See DR96 for details. @see
411 * vector for function documentation.
413 * @ingroup Containers
416 * In some terminology a %vector can be described as a dynamic
417 * C-style array, it offers fast and efficient access to individual
418 * elements in any order and saves the user from worrying about
419 * memory and size allocation. Subscripting ( @c [] ) access is
420 * also provided as with C-style arrays.
422 template<typename _Alloc>
423 class vector<bool, _Alloc> : public _Bvector_base<_Alloc>
426 typedef bool value_type;
427 typedef size_t size_type;
428 typedef ptrdiff_t difference_type;
429 typedef _Bit_reference reference;
430 typedef bool const_reference;
431 typedef _Bit_reference* pointer;
432 typedef const bool* const_pointer;
434 typedef _Bit_iterator iterator;
435 typedef _Bit_const_iterator const_iterator;
437 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
438 typedef std::reverse_iterator<iterator> reverse_iterator;
440 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
442 allocator_type get_allocator() const
443 { return _Bvector_base<_Alloc>::get_allocator(); }
446 using _Bvector_base<_Alloc>::_M_allocate;
447 using _Bvector_base<_Alloc>::_M_deallocate;
451 _M_initialize(size_type __n)
453 _Bit_type* __q = this->_M_allocate(__n);
454 this->_M_impl._M_end_of_storage = (__q
455 + ((__n + _S_word_bit - 1)
457 this->_M_impl._M_start = iterator(__q, 0);
458 this->_M_impl._M_finish = this->_M_impl._M_start + difference_type(__n);
462 _M_insert_aux(iterator __position, bool __x)
464 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
466 std::copy_backward(__position, this->_M_impl._M_finish,
467 this->_M_impl._M_finish + 1);
469 ++this->_M_impl._M_finish;
473 const size_type __len = size() ? 2 * size()
474 : static_cast<size_type>(_S_word_bit);
475 _Bit_type * __q = this->_M_allocate(__len);
476 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
478 this->_M_impl._M_finish = std::copy(__position, end(), __i);
479 this->_M_deallocate();
480 this->_M_impl._M_end_of_storage = (__q + ((__len + _S_word_bit - 1)
482 this->_M_impl._M_start = iterator(__q, 0);
486 template<class _InputIterator>
488 _M_initialize_range(_InputIterator __first, _InputIterator __last,
491 this->_M_impl._M_start = iterator();
492 this->_M_impl._M_finish = iterator();
493 this->_M_impl._M_end_of_storage = 0;
494 for (; __first != __last; ++__first)
498 template<class _ForwardIterator>
500 _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
501 forward_iterator_tag)
503 const size_type __n = std::distance(__first, __last);
505 std::copy(__first, __last, this->_M_impl._M_start);
508 template<class _InputIterator>
510 _M_insert_range(iterator __pos, _InputIterator __first,
511 _InputIterator __last, input_iterator_tag)
513 for (; __first != __last; ++__first)
515 __pos = insert(__pos, *__first);
520 template<class _ForwardIterator>
522 _M_insert_range(iterator __position, _ForwardIterator __first,
523 _ForwardIterator __last, forward_iterator_tag)
525 if (__first != __last)
527 size_type __n = std::distance(__first, __last);
528 if (capacity() - size() >= __n)
530 std::copy_backward(__position, end(),
531 this->_M_impl._M_finish
532 + difference_type(__n));
533 std::copy(__first, __last, __position);
534 this->_M_impl._M_finish += difference_type(__n);
538 const size_type __len = size() + std::max(size(), __n);
539 _Bit_type * __q = this->_M_allocate(__len);
540 iterator __i = std::copy(begin(), __position,
542 __i = std::copy(__first, __last, __i);
543 this->_M_impl._M_finish = std::copy(__position, end(), __i);
544 this->_M_deallocate();
545 this->_M_impl._M_end_of_storage = (__q
546 + ((__len + _S_word_bit - 1)
548 this->_M_impl._M_start = iterator(__q, 0);
556 { return this->_M_impl._M_start; }
560 { return this->_M_impl._M_start; }
564 { return this->_M_impl._M_finish; }
568 { return this->_M_impl._M_finish; }
572 { return reverse_iterator(end()); }
574 const_reverse_iterator
576 { return const_reverse_iterator(end()); }
580 { return reverse_iterator(begin()); }
582 const_reverse_iterator
584 { return const_reverse_iterator(begin()); }
588 { return size_type(end() - begin()); }
592 { return size_type(-1); }
596 { return size_type(const_iterator(this->_M_impl._M_end_of_storage, 0)
600 { return begin() == end(); }
603 operator[](size_type __n)
604 { return *(begin() + difference_type(__n)); }
607 operator[](size_type __n) const
608 { return *(begin() + difference_type(__n)); }
611 _M_range_check(size_type __n) const
613 if (__n >= this->size())
614 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
619 { _M_range_check(__n); return (*this)[__n]; }
622 at(size_type __n) const
623 { _M_range_check(__n); return (*this)[__n]; }
626 vector(const allocator_type& __a = allocator_type())
627 : _Bvector_base<_Alloc>(__a) { }
629 vector(size_type __n, bool __value,
630 const allocator_type& __a = allocator_type())
631 : _Bvector_base<_Alloc>(__a)
634 std::fill(this->_M_impl._M_start._M_p, this->_M_impl._M_end_of_storage,
639 vector(size_type __n)
640 : _Bvector_base<_Alloc>(allocator_type())
643 std::fill(this->_M_impl._M_start._M_p,
644 this->_M_impl._M_end_of_storage, 0);
647 vector(const vector& __x)
648 : _Bvector_base<_Alloc>(__x.get_allocator())
650 _M_initialize(__x.size());
651 std::copy(__x.begin(), __x.end(), this->_M_impl._M_start);
654 // Check whether it's an integral type. If so, it's not an iterator.
655 template<class _Integer>
657 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
660 std::fill(this->_M_impl._M_start._M_p,
661 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
664 template<class _InputIterator>
666 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
668 { _M_initialize_range(__first, __last,
669 std::__iterator_category(__first)); }
671 template<class _InputIterator>
672 vector(_InputIterator __first, _InputIterator __last,
673 const allocator_type& __a = allocator_type())
674 : _Bvector_base<_Alloc>(__a)
676 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
677 _M_initialize_dispatch(__first, __last, _Integral());
683 operator=(const vector& __x)
687 if (__x.size() > capacity())
689 this->_M_deallocate();
690 _M_initialize(__x.size());
692 std::copy(__x.begin(), __x.end(), begin());
693 this->_M_impl._M_finish = begin() + difference_type(__x.size());
697 // assign(), a generalized assignment member function. Two
698 // versions: one that takes a count, and one that takes a range.
699 // The range version is a member template, so we dispatch on whether
700 // or not the type is an integer.
703 _M_fill_assign(size_t __n, bool __x)
707 std::fill(this->_M_impl._M_start._M_p,
708 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
709 insert(end(), __n - size(), __x);
713 erase(begin() + __n, end());
714 std::fill(this->_M_impl._M_start._M_p,
715 this->_M_impl._M_end_of_storage, __x ? ~0 : 0);
720 assign(size_t __n, bool __x)
721 { _M_fill_assign(__n, __x); }
723 template<class _InputIterator>
725 assign(_InputIterator __first, _InputIterator __last)
727 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
728 _M_assign_dispatch(__first, __last, _Integral());
731 template<class _Integer>
733 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
734 { _M_fill_assign((size_t) __n, (bool) __val); }
736 template<class _InputIterator>
738 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
740 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
742 template<class _InputIterator>
744 _M_assign_aux(_InputIterator __first, _InputIterator __last,
747 iterator __cur = begin();
748 for (; __first != __last && __cur != end(); ++__cur, ++__first)
750 if (__first == __last)
753 insert(end(), __first, __last);
756 template<class _ForwardIterator>
758 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
759 forward_iterator_tag)
761 const size_type __len = std::distance(__first, __last);
763 erase(std::copy(__first, __last, begin()), end());
766 _ForwardIterator __mid = __first;
767 std::advance(__mid, size());
768 std::copy(__first, __mid, begin());
769 insert(end(), __mid, __last);
774 reserve(size_type __n)
776 if (__n > this->max_size())
777 __throw_length_error(__N("vector::reserve"));
778 if (this->capacity() < __n)
780 _Bit_type* __q = this->_M_allocate(__n);
781 this->_M_impl._M_finish = std::copy(begin(), end(),
783 this->_M_deallocate();
784 this->_M_impl._M_start = iterator(__q, 0);
785 this->_M_impl._M_end_of_storage = (__q + (__n + _S_word_bit - 1)
800 { return *(end() - 1); }
804 { return *(end() - 1); }
809 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
810 *this->_M_impl._M_finish++ = __x;
812 _M_insert_aux(end(), __x);
816 swap(vector<bool, _Alloc>& __x)
818 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
819 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
820 std::swap(this->_M_impl._M_end_of_storage,
821 __x._M_impl._M_end_of_storage);
824 // [23.2.5]/1, third-to-last entry in synopsis listing
826 swap(reference __x, reference __y)
834 insert(iterator __position, bool __x = bool())
836 const difference_type __n = __position - begin();
837 if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage
838 && __position == end())
839 *this->_M_impl._M_finish++ = __x;
841 _M_insert_aux(__position, __x);
842 return begin() + __n;
845 // Check whether it's an integral type. If so, it's not an iterator.
847 template<class _Integer>
849 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
851 { _M_fill_insert(__pos, __n, __x); }
853 template<class _InputIterator>
855 _M_insert_dispatch(iterator __pos,
856 _InputIterator __first, _InputIterator __last,
858 { _M_insert_range(__pos, __first, __last,
859 std::__iterator_category(__first)); }
861 template<class _InputIterator>
863 insert(iterator __position,
864 _InputIterator __first, _InputIterator __last)
866 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
867 _M_insert_dispatch(__position, __first, __last, _Integral());
871 _M_fill_insert(iterator __position, size_type __n, bool __x)
875 if (capacity() - size() >= __n)
877 std::copy_backward(__position, end(),
878 this->_M_impl._M_finish + difference_type(__n));
879 std::fill(__position, __position + difference_type(__n), __x);
880 this->_M_impl._M_finish += difference_type(__n);
884 const size_type __len = size() + std::max(size(), __n);
885 _Bit_type * __q = this->_M_allocate(__len);
886 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
887 std::fill_n(__i, __n, __x);
888 this->_M_impl._M_finish = std::copy(__position, end(),
889 __i + difference_type(__n));
890 this->_M_deallocate();
891 this->_M_impl._M_end_of_storage = (__q + ((__len + _S_word_bit - 1)
893 this->_M_impl._M_start = iterator(__q, 0);
898 insert(iterator __position, size_type __n, bool __x)
899 { _M_fill_insert(__position, __n, __x); }
903 { --this->_M_impl._M_finish; }
906 erase(iterator __position)
908 if (__position + 1 != end())
909 std::copy(__position + 1, end(), __position);
910 --this->_M_impl._M_finish;
915 erase(iterator __first, iterator __last)
917 this->_M_impl._M_finish = std::copy(__last, end(), __first);
922 resize(size_type __new_size, bool __x = bool())
924 if (__new_size < size())
925 erase(begin() + difference_type(__new_size), end());
927 insert(end(), __new_size - size(), __x);
933 for (_Bit_type * __p = this->_M_impl._M_start._M_p;
934 __p != this->_M_impl._M_end_of_storage; ++__p)
940 { erase(begin(), end()); }