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)) };
69 struct _Bit_reference {
73 _Bit_reference(_Bit_type * __x, _Bit_type __y)
74 : _M_p(__x), _M_mask(__y) {}
77 _Bit_reference() : _M_p(0), _M_mask(0) {}
78 operator bool() const { return !!(*_M_p & _M_mask); }
79 _Bit_reference& operator=(bool __x)
81 if (__x) *_M_p |= _M_mask;
82 else *_M_p &= ~_M_mask;
85 _Bit_reference& operator=(const _Bit_reference& __x)
86 { return *this = bool(__x); }
87 bool operator==(const _Bit_reference& __x) const
88 { return bool(*this) == bool(__x); }
89 bool operator<(const _Bit_reference& __x) const
90 { return !bool(*this) && bool(__x); }
91 void flip() { *_M_p ^= _M_mask; }
94 struct _Bit_iterator_base : public iterator<random_access_iterator_tag, bool>
97 unsigned int _M_offset;
99 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
100 : _M_p(__x), _M_offset(__y) {}
103 if (_M_offset++ == _S_word_bit - 1) {
108 void _M_bump_down() {
109 if (_M_offset-- == 0) {
110 _M_offset = _S_word_bit - 1;
115 void _M_incr(ptrdiff_t __i) {
116 difference_type __n = __i + _M_offset;
117 _M_p += __n / _S_word_bit;
118 __n = __n % _S_word_bit;
120 _M_offset = (unsigned int) __n + _S_word_bit;
123 _M_offset = (unsigned int) __n;
126 bool operator==(const _Bit_iterator_base& __i) const {
127 return _M_p == __i._M_p && _M_offset == __i._M_offset;
129 bool operator<(const _Bit_iterator_base& __i) const {
130 return _M_p < __i._M_p || (_M_p == __i._M_p && _M_offset < __i._M_offset);
132 bool operator!=(const _Bit_iterator_base& __i) const {
133 return !(*this == __i);
135 bool operator>(const _Bit_iterator_base& __i) const {
138 bool operator<=(const _Bit_iterator_base& __i) const {
139 return !(__i < *this);
141 bool operator>=(const _Bit_iterator_base& __i) const {
142 return !(*this < __i);
147 operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y) {
148 return _S_word_bit * (__x._M_p - __y._M_p) + __x._M_offset - __y._M_offset;
152 struct _Bit_iterator : public _Bit_iterator_base
154 typedef _Bit_reference reference;
155 typedef _Bit_reference* pointer;
156 typedef _Bit_iterator iterator;
158 _Bit_iterator() : _Bit_iterator_base(0, 0) {}
159 _Bit_iterator(_Bit_type * __x, unsigned int __y)
160 : _Bit_iterator_base(__x, __y) {}
162 reference operator*() const { return reference(_M_p, 1UL << _M_offset); }
163 iterator& operator++() {
167 iterator operator++(int) {
168 iterator __tmp = *this;
172 iterator& operator--() {
176 iterator operator--(int) {
177 iterator __tmp = *this;
181 iterator& operator+=(difference_type __i) {
185 iterator& operator-=(difference_type __i) {
189 iterator operator+(difference_type __i) const {
190 iterator __tmp = *this;
193 iterator operator-(difference_type __i) const {
194 iterator __tmp = *this;
198 reference operator[](difference_type __i) { return *(*this + __i); }
202 operator+(ptrdiff_t __n, const _Bit_iterator& __x) { return __x + __n; }
205 struct _Bit_const_iterator : public _Bit_iterator_base
207 typedef bool reference;
208 typedef bool const_reference;
209 typedef const bool* pointer;
210 typedef _Bit_const_iterator const_iterator;
212 _Bit_const_iterator() : _Bit_iterator_base(0, 0) {}
213 _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
214 : _Bit_iterator_base(__x, __y) {}
215 _Bit_const_iterator(const _Bit_iterator& __x)
216 : _Bit_iterator_base(__x._M_p, __x._M_offset) {}
218 const_reference operator*() const {
219 return _Bit_reference(_M_p, 1UL << _M_offset);
221 const_iterator& operator++() {
225 const_iterator operator++(int) {
226 const_iterator __tmp = *this;
230 const_iterator& operator--() {
234 const_iterator operator--(int) {
235 const_iterator __tmp = *this;
239 const_iterator& operator+=(difference_type __i) {
243 const_iterator& operator-=(difference_type __i) {
247 const_iterator operator+(difference_type __i) const {
248 const_iterator __tmp = *this;
251 const_iterator operator-(difference_type __i) const {
252 const_iterator __tmp = *this;
255 const_reference operator[](difference_type __i) {
256 return *(*this + __i);
260 inline _Bit_const_iterator
261 operator+(ptrdiff_t __n, const _Bit_const_iterator& __x) { return __x + __n; }
264 template <class _Alloc>
266 : public _Alloc::template rebind<_Bit_type>::other
268 typedef typename _Alloc::template rebind<_Bit_type>::other _Bit_alloc_type;
270 typedef _Alloc allocator_type;
271 allocator_type get_allocator() const {
272 return *static_cast<const _Bit_alloc_type*>(this);
275 _Bvector_base(const allocator_type& __a)
276 : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0) { }
277 ~_Bvector_base() { this->_M_deallocate(); }
280 _Bit_type* _M_bit_alloc(size_t __n) {
281 return _Bit_alloc_type::allocate((__n + _S_word_bit - 1)/_S_word_bit);
283 void _M_deallocate() {
285 _Bit_alloc_type::deallocate(_M_start._M_p, _M_end_of_storage - _M_start._M_p);
288 _Bit_iterator _M_start;
289 _Bit_iterator _M_finish;
290 _Bit_type * _M_end_of_storage;
293 } // namespace __gnu_norm
295 // Declare a partial specialization of vector<T, Alloc>.
296 #include <bits/stl_vector.h>
301 * @brief A specialization of vector for booleans which offers fixed time
302 * access to individual elements in any order.
304 * Note that vector<bool> does not actually meet the requirements for being
305 * a container. This is because the reference and pointer types are not
306 * really references and pointers to bool. See DR96 for details. @see
307 * vector for function documentation.
309 * @ingroup Containers
312 * In some terminology a %vector can be described as a dynamic C-style array,
313 * it offers fast and efficient access to individual elements in any order
314 * and saves the user from worrying about memory and size allocation.
315 * Subscripting ( @c [] ) access is also provided as with C-style arrays.
317 template <typename _Alloc>
318 class vector<bool, _Alloc> : public _Bvector_base<_Alloc>
321 typedef bool value_type;
322 typedef size_t size_type;
323 typedef ptrdiff_t difference_type;
324 typedef _Bit_reference reference;
325 typedef bool const_reference;
326 typedef _Bit_reference* pointer;
327 typedef const bool* const_pointer;
329 typedef _Bit_iterator iterator;
330 typedef _Bit_const_iterator const_iterator;
332 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
333 typedef std::reverse_iterator<iterator> reverse_iterator;
335 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
336 allocator_type get_allocator() const {
337 return _Bvector_base<_Alloc>::get_allocator();
341 using _Bvector_base<_Alloc>::_M_bit_alloc;
342 using _Bvector_base<_Alloc>::_M_deallocate;
343 using _Bvector_base<_Alloc>::_M_start;
344 using _Bvector_base<_Alloc>::_M_finish;
345 using _Bvector_base<_Alloc>::_M_end_of_storage;
348 void _M_initialize(size_type __n) {
349 _Bit_type * __q = this->_M_bit_alloc(__n);
350 this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
351 this->_M_start = iterator(__q, 0);
352 this->_M_finish = this->_M_start + difference_type(__n);
354 void _M_insert_aux(iterator __position, bool __x) {
355 if (this->_M_finish._M_p != this->_M_end_of_storage) {
356 std::copy_backward(__position, this->_M_finish, this->_M_finish + 1);
361 size_type __len = size()
362 ? 2 * size() : static_cast<size_type>(_S_word_bit);
363 _Bit_type * __q = this->_M_bit_alloc(__len);
364 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
366 this->_M_finish = std::copy(__position, end(), __i);
367 this->_M_deallocate();
368 this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
369 this->_M_start = iterator(__q, 0);
373 template <class _InputIterator>
374 void _M_initialize_range(_InputIterator __first, _InputIterator __last,
375 input_iterator_tag) {
376 this->_M_start = iterator();
377 this->_M_finish = iterator();
378 this->_M_end_of_storage = 0;
379 for ( ; __first != __last; ++__first)
383 template <class _ForwardIterator>
384 void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
385 forward_iterator_tag) {
386 size_type __n = std::distance(__first, __last);
388 std::copy(__first, __last, this->_M_start);
391 template <class _InputIterator>
392 void _M_insert_range(iterator __pos,
393 _InputIterator __first, _InputIterator __last,
394 input_iterator_tag) {
395 for ( ; __first != __last; ++__first) {
396 __pos = insert(__pos, *__first);
401 template <class _ForwardIterator>
402 void _M_insert_range(iterator __position,
403 _ForwardIterator __first, _ForwardIterator __last,
404 forward_iterator_tag) {
405 if (__first != __last) {
406 size_type __n = std::distance(__first, __last);
407 if (capacity() - size() >= __n) {
408 std::copy_backward(__position, end(),
409 this->_M_finish + difference_type(__n));
410 std::copy(__first, __last, __position);
411 this->_M_finish += difference_type(__n);
414 size_type __len = size() + std::max(size(), __n);
415 _Bit_type * __q = this->_M_bit_alloc(__len);
416 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
417 __i = std::copy(__first, __last, __i);
418 this->_M_finish = std::copy(__position, end(), __i);
419 this->_M_deallocate();
420 this->_M_end_of_storage
421 = __q + (__len + _S_word_bit - 1)/_S_word_bit;
422 this->_M_start = iterator(__q, 0);
428 iterator begin() { return this->_M_start; }
429 const_iterator begin() const { return this->_M_start; }
430 iterator end() { return this->_M_finish; }
431 const_iterator end() const { return this->_M_finish; }
433 reverse_iterator rbegin() { return reverse_iterator(end()); }
434 const_reverse_iterator rbegin() const {
435 return const_reverse_iterator(end());
437 reverse_iterator rend() { return reverse_iterator(begin()); }
438 const_reverse_iterator rend() const {
439 return const_reverse_iterator(begin());
442 size_type size() const { return size_type(end() - begin()); }
443 size_type max_size() const { return size_type(-1); }
444 size_type capacity() const {
445 return size_type(const_iterator(this->_M_end_of_storage, 0) - begin());
447 bool empty() const { return begin() == end(); }
449 reference operator[](size_type __n)
450 { return *(begin() + difference_type(__n)); }
451 const_reference operator[](size_type __n) const
452 { return *(begin() + difference_type(__n)); }
454 void _M_range_check(size_type __n) const {
455 if (__n >= this->size())
456 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
459 reference at(size_type __n)
460 { _M_range_check(__n); return (*this)[__n]; }
461 const_reference at(size_type __n) const
462 { _M_range_check(__n); return (*this)[__n]; }
464 explicit vector(const allocator_type& __a = allocator_type())
465 : _Bvector_base<_Alloc>(__a) {}
467 vector(size_type __n, bool __value,
468 const allocator_type& __a = allocator_type())
469 : _Bvector_base<_Alloc>(__a)
472 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __value ? ~0 : 0);
475 explicit vector(size_type __n)
476 : _Bvector_base<_Alloc>(allocator_type())
479 std::fill(this->_M_start._M_p, this->_M_end_of_storage, 0);
482 vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) {
483 _M_initialize(__x.size());
484 std::copy(__x.begin(), __x.end(), this->_M_start);
487 // Check whether it's an integral type. If so, it's not an iterator.
489 template <class _Integer>
490 void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
492 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
495 template <class _InputIterator>
496 void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
498 _M_initialize_range(__first, __last, std::__iterator_category(__first));
501 template <class _InputIterator>
502 vector(_InputIterator __first, _InputIterator __last,
503 const allocator_type& __a = allocator_type())
504 : _Bvector_base<_Alloc>(__a)
506 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
507 _M_initialize_dispatch(__first, __last, _Integral());
512 vector& operator=(const vector& __x) {
513 if (&__x == this) return *this;
514 if (__x.size() > capacity()) {
515 this->_M_deallocate();
516 _M_initialize(__x.size());
518 std::copy(__x.begin(), __x.end(), begin());
519 this->_M_finish = begin() + difference_type(__x.size());
523 // assign(), a generalized assignment member function. Two
524 // versions: one that takes a count, and one that takes a range.
525 // The range version is a member template, so we dispatch on whether
526 // or not the type is an integer.
528 void _M_fill_assign(size_t __n, bool __x) {
530 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
531 insert(end(), __n - size(), __x);
534 erase(begin() + __n, end());
535 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
539 void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }
541 template <class _InputIterator>
542 void assign(_InputIterator __first, _InputIterator __last) {
543 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
544 _M_assign_dispatch(__first, __last, _Integral());
547 template <class _Integer>
548 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
549 { _M_fill_assign((size_t) __n, (bool) __val); }
551 template <class _InputIterator>
552 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
553 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
555 template <class _InputIterator>
556 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
557 input_iterator_tag) {
558 iterator __cur = begin();
559 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
561 if (__first == __last)
564 insert(end(), __first, __last);
567 template <class _ForwardIterator>
568 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
569 forward_iterator_tag) {
570 size_type __len = std::distance(__first, __last);
572 erase(std::copy(__first, __last, begin()), end());
574 _ForwardIterator __mid = __first;
575 std::advance(__mid, size());
576 std::copy(__first, __mid, begin());
577 insert(end(), __mid, __last);
581 void reserve(size_type __n) {
582 if (__n > this->max_size())
583 __throw_length_error(__N("vector::reserve"));
584 if (this->capacity() < __n) {
585 _Bit_type * __q = this->_M_bit_alloc(__n);
586 this->_M_finish = std::copy(begin(), end(), iterator(__q, 0));
587 this->_M_deallocate();
588 this->_M_start = iterator(__q, 0);
589 this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
593 reference front() { return *begin(); }
594 const_reference front() const { return *begin(); }
595 reference back() { return *(end() - 1); }
596 const_reference back() const { return *(end() - 1); }
597 void push_back(bool __x) {
598 if (this->_M_finish._M_p != this->_M_end_of_storage)
599 *this->_M_finish++ = __x;
601 _M_insert_aux(end(), __x);
603 void swap(vector<bool, _Alloc>& __x) {
604 std::swap(this->_M_start, __x._M_start);
605 std::swap(this->_M_finish, __x._M_finish);
606 std::swap(this->_M_end_of_storage, __x._M_end_of_storage);
609 // [23.2.5]/1, third-to-last entry in synopsis listing
610 static void swap(reference __x, reference __y) {
616 iterator insert(iterator __position, bool __x = bool()) {
617 difference_type __n = __position - begin();
618 if (this->_M_finish._M_p != this->_M_end_of_storage
619 && __position == end())
620 *this->_M_finish++ = __x;
622 _M_insert_aux(__position, __x);
623 return begin() + __n;
626 // Check whether it's an integral type. If so, it's not an iterator.
628 template <class _Integer>
629 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
631 _M_fill_insert(__pos, __n, __x);
634 template <class _InputIterator>
635 void _M_insert_dispatch(iterator __pos,
636 _InputIterator __first, _InputIterator __last,
638 _M_insert_range(__pos, __first, __last, std::__iterator_category(__first));
641 template <class _InputIterator>
642 void insert(iterator __position,
643 _InputIterator __first, _InputIterator __last) {
644 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
645 _M_insert_dispatch(__position, __first, __last, _Integral());
648 void _M_fill_insert(iterator __position, size_type __n, bool __x) {
649 if (__n == 0) return;
650 if (capacity() - size() >= __n) {
651 std::copy_backward(__position, end(),
652 this->_M_finish + difference_type(__n));
653 std::fill(__position, __position + difference_type(__n), __x);
654 this->_M_finish += difference_type(__n);
657 size_type __len = size() + std::max(size(), __n);
658 _Bit_type * __q = this->_M_bit_alloc(__len);
659 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
660 std::fill_n(__i, __n, __x);
661 this->_M_finish = std::copy(__position, end(), __i + difference_type(__n));
662 this->_M_deallocate();
663 this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
664 this->_M_start = iterator(__q, 0);
668 void insert(iterator __position, size_type __n, bool __x) {
669 _M_fill_insert(__position, __n, __x);
672 void pop_back() { --this->_M_finish; }
673 iterator erase(iterator __position) {
674 if (__position + 1 != end())
675 std::copy(__position + 1, end(), __position);
679 iterator erase(iterator __first, iterator __last) {
680 this->_M_finish = std::copy(__last, end(), __first);
683 void resize(size_type __new_size, bool __x = bool()) {
684 if (__new_size < size())
685 erase(begin() + difference_type(__new_size), end());
687 insert(end(), __new_size - size(), __x);
690 for (_Bit_type * __p = this->_M_start._M_p;
691 __p != this->_M_end_of_storage;
696 void clear() { erase(begin(), end()); }
699 // This typedef is non-standard. It is provided for backward compatibility.
700 typedef vector<bool, __alloc> bit_vector;
701 } // namespace __gnu_norm
703 #endif /* _BVECTOR_H */