1 // bit_vector and vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001 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.
61 #ifndef __SGI_STL_INTERNAL_BVECTOR_H
62 #define __SGI_STL_INTERNAL_BVECTOR_H
67 static const int __WORD_BIT
= int(CHAR_BIT
*sizeof(unsigned int));
69 struct _Bit_reference
{
72 _Bit_reference(unsigned int* __x
, unsigned int __y
)
73 : _M_p(__x
), _M_mask(__y
) {}
76 _Bit_reference() : _M_p(0), _M_mask(0) {}
77 operator bool() const { return !(!(*_M_p
& _M_mask
)); }
78 _Bit_reference
& operator=(bool __x
)
80 if (__x
) *_M_p
|= _M_mask
;
81 else *_M_p
&= ~_M_mask
;
84 _Bit_reference
& operator=(const _Bit_reference
& __x
)
85 { return *this = bool(__x
); }
86 bool operator==(const _Bit_reference
& __x
) const
87 { return bool(*this) == bool(__x
); }
88 bool operator<(const _Bit_reference
& __x
) const {
89 return !bool(*this) && bool(__x
);
91 void flip() { *_M_p
^= _M_mask
; }
94 inline void swap(_Bit_reference __x
, _Bit_reference __y
)
101 struct _Bit_iterator_base
: public random_access_iterator
<bool, ptrdiff_t>
104 unsigned int _M_offset
;
106 _Bit_iterator_base(unsigned int* __x
, unsigned int __y
)
107 : _M_p(__x
), _M_offset(__y
) {}
110 if (_M_offset
++ == __WORD_BIT
- 1) {
115 void _M_bump_down() {
116 if (_M_offset
-- == 0) {
117 _M_offset
= __WORD_BIT
- 1;
122 void _M_incr(ptrdiff_t __i
) {
123 difference_type __n
= __i
+ _M_offset
;
124 _M_p
+= __n
/ __WORD_BIT
;
125 __n
= __n
% __WORD_BIT
;
127 _M_offset
= (unsigned int) __n
+ __WORD_BIT
;
130 _M_offset
= (unsigned int) __n
;
133 bool operator==(const _Bit_iterator_base
& __i
) const {
134 return _M_p
== __i
._M_p
&& _M_offset
== __i
._M_offset
;
136 bool operator<(const _Bit_iterator_base
& __i
) const {
137 return _M_p
< __i
._M_p
|| (_M_p
== __i
._M_p
&& _M_offset
< __i
._M_offset
);
139 bool operator!=(const _Bit_iterator_base
& __i
) const {
140 return !(*this == __i
);
142 bool operator>(const _Bit_iterator_base
& __i
) const {
145 bool operator<=(const _Bit_iterator_base
& __i
) const {
146 return !(__i
< *this);
148 bool operator>=(const _Bit_iterator_base
& __i
) const {
149 return !(*this < __i
);
154 operator-(const _Bit_iterator_base
& __x
, const _Bit_iterator_base
& __y
) {
155 return __WORD_BIT
* (__x
._M_p
- __y
._M_p
) + __x
._M_offset
- __y
._M_offset
;
159 struct _Bit_iterator
: public _Bit_iterator_base
161 typedef _Bit_reference reference
;
162 typedef _Bit_reference
* pointer
;
163 typedef _Bit_iterator iterator
;
165 _Bit_iterator() : _Bit_iterator_base(0, 0) {}
166 _Bit_iterator(unsigned int* __x
, unsigned int __y
)
167 : _Bit_iterator_base(__x
, __y
) {}
169 reference
operator*() const { return reference(_M_p
, 1U << _M_offset
); }
170 iterator
& operator++() {
174 iterator
operator++(int) {
175 iterator __tmp
= *this;
179 iterator
& operator--() {
183 iterator
operator--(int) {
184 iterator __tmp
= *this;
188 iterator
& operator+=(difference_type __i
) {
192 iterator
& operator-=(difference_type __i
) {
196 iterator
operator+(difference_type __i
) const {
197 iterator __tmp
= *this;
200 iterator
operator-(difference_type __i
) const {
201 iterator __tmp
= *this;
205 reference
operator[](difference_type __i
) { return *(*this + __i
); }
209 operator+(ptrdiff_t __n
, const _Bit_iterator
& __x
) { return __x
+ __n
; }
212 struct _Bit_const_iterator
: public _Bit_iterator_base
214 typedef bool reference
;
215 typedef bool const_reference
;
216 typedef const bool* pointer
;
217 typedef _Bit_const_iterator const_iterator
;
219 _Bit_const_iterator() : _Bit_iterator_base(0, 0) {}
220 _Bit_const_iterator(unsigned int* __x
, unsigned int __y
)
221 : _Bit_iterator_base(__x
, __y
) {}
222 _Bit_const_iterator(const _Bit_iterator
& __x
)
223 : _Bit_iterator_base(__x
._M_p
, __x
._M_offset
) {}
225 const_reference
operator*() const {
226 return _Bit_reference(_M_p
, 1U << _M_offset
);
228 const_iterator
& operator++() {
232 const_iterator
operator++(int) {
233 const_iterator __tmp
= *this;
237 const_iterator
& operator--() {
241 const_iterator
operator--(int) {
242 const_iterator __tmp
= *this;
246 const_iterator
& operator+=(difference_type __i
) {
250 const_iterator
& operator-=(difference_type __i
) {
254 const_iterator
operator+(difference_type __i
) const {
255 const_iterator __tmp
= *this;
258 const_iterator
operator-(difference_type __i
) const {
259 const_iterator __tmp
= *this;
262 const_reference
operator[](difference_type __i
) {
263 return *(*this + __i
);
267 inline _Bit_const_iterator
268 operator+(ptrdiff_t __n
, const _Bit_const_iterator
& __x
) { return __x
+ __n
; }
271 // Bit-vector base class, which encapsulates the difference between
272 // old SGI-style allocators and standard-conforming allocators.
274 // Base class for ordinary allocators.
275 template <class _Allocator
, bool __is_static
>
276 class _Bvector_alloc_base
{
278 typedef typename _Alloc_traits
<bool, _Allocator
>::allocator_type
280 allocator_type
get_allocator() const { return _M_data_allocator
; }
282 _Bvector_alloc_base(const allocator_type
& __a
)
283 : _M_data_allocator(__a
), _M_start(), _M_finish(), _M_end_of_storage(0) {}
286 unsigned int* _M_bit_alloc(size_t __n
)
287 { return _M_data_allocator
.allocate((__n
+ __WORD_BIT
- 1)/__WORD_BIT
); }
288 void _M_deallocate() {
290 _M_data_allocator
.deallocate(_M_start
._M_p
,
291 _M_end_of_storage
- _M_start
._M_p
);
294 typename _Alloc_traits
<unsigned int, _Allocator
>::allocator_type
296 _Bit_iterator _M_start
;
297 _Bit_iterator _M_finish
;
298 unsigned int* _M_end_of_storage
;
301 // Specialization for instanceless allocators.
302 template <class _Allocator
>
303 class _Bvector_alloc_base
<_Allocator
, true> {
305 typedef typename _Alloc_traits
<bool, _Allocator
>::allocator_type
307 allocator_type
get_allocator() const { return allocator_type(); }
309 _Bvector_alloc_base(const allocator_type
&)
310 : _M_start(), _M_finish(), _M_end_of_storage(0) {}
313 typedef typename _Alloc_traits
<unsigned int, _Allocator
>::_Alloc_type
316 unsigned int* _M_bit_alloc(size_t __n
)
317 { return _Alloc_type::allocate((__n
+ __WORD_BIT
- 1)/__WORD_BIT
); }
318 void _M_deallocate() {
320 _Alloc_type::deallocate(_M_start
._M_p
,
321 _M_end_of_storage
- _M_start
._M_p
);
324 _Bit_iterator _M_start
;
325 _Bit_iterator _M_finish
;
326 unsigned int* _M_end_of_storage
;
329 template <class _Alloc
>
331 : public _Bvector_alloc_base
<_Alloc
,
332 _Alloc_traits
<bool, _Alloc
>::_S_instanceless
>
334 typedef _Bvector_alloc_base
<_Alloc
,
335 _Alloc_traits
<bool, _Alloc
>::_S_instanceless
>
338 typedef typename
_Base::allocator_type allocator_type
;
340 _Bvector_base(const allocator_type
& __a
) : _Base(__a
) {}
341 ~_Bvector_base() { _Base::_M_deallocate(); }
346 // Declare a partial specialization of vector<T, Alloc>.
347 #include <bits/stl_vector.h>
351 template <typename _Alloc
>
352 class vector
<bool, _Alloc
> : public _Bvector_base
<_Alloc
>
355 typedef bool value_type
;
356 typedef size_t size_type
;
357 typedef ptrdiff_t difference_type
;
358 typedef _Bit_reference reference
;
359 typedef bool const_reference
;
360 typedef _Bit_reference
* pointer
;
361 typedef const bool* const_pointer
;
363 typedef _Bit_iterator iterator
;
364 typedef _Bit_const_iterator const_iterator
;
366 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
367 typedef reverse_iterator
<iterator
> reverse_iterator
;
369 typedef typename _Bvector_base
<_Alloc
>::allocator_type allocator_type
;
370 allocator_type
get_allocator() const {
371 return _Bvector_base
<_Alloc
>::get_allocator();
375 using _Bvector_base
<_Alloc
>::_M_bit_alloc
;
376 using _Bvector_base
<_Alloc
>::_M_deallocate
;
377 using _Bvector_base
<_Alloc
>::_M_start
;
378 using _Bvector_base
<_Alloc
>::_M_finish
;
379 using _Bvector_base
<_Alloc
>::_M_end_of_storage
;
382 void _M_initialize(size_type __n
) {
383 unsigned int* __q
= _M_bit_alloc(__n
);
384 _M_end_of_storage
= __q
+ (__n
+ __WORD_BIT
- 1)/__WORD_BIT
;
385 _M_start
= iterator(__q
, 0);
386 _M_finish
= _M_start
+ difference_type(__n
);
388 void _M_insert_aux(iterator __position
, bool __x
) {
389 if (_M_finish
._M_p
!= _M_end_of_storage
) {
390 copy_backward(__position
, _M_finish
, _M_finish
+ 1);
395 size_type __len
= size() ? 2 * size() : __WORD_BIT
;
396 unsigned int* __q
= _M_bit_alloc(__len
);
397 iterator __i
= copy(begin(), __position
, iterator(__q
, 0));
399 _M_finish
= copy(__position
, end(), __i
);
401 _M_end_of_storage
= __q
+ (__len
+ __WORD_BIT
- 1)/__WORD_BIT
;
402 _M_start
= iterator(__q
, 0);
406 template <class _InputIterator
>
407 void _M_initialize_range(_InputIterator __first
, _InputIterator __last
,
408 input_iterator_tag
) {
409 _M_start
= iterator();
410 _M_finish
= iterator();
411 _M_end_of_storage
= 0;
412 for ( ; __first
!= __last
; ++__first
)
416 template <class _ForwardIterator
>
417 void _M_initialize_range(_ForwardIterator __first
, _ForwardIterator __last
,
418 forward_iterator_tag
) {
420 distance(__first
, __last
, __n
);
422 copy(__first
, __last
, _M_start
);
425 template <class _InputIterator
>
426 void _M_insert_range(iterator __pos
,
427 _InputIterator __first
, _InputIterator __last
,
428 input_iterator_tag
) {
429 for ( ; __first
!= __last
; ++__first
) {
430 __pos
= insert(__pos
, *__first
);
435 template <class _ForwardIterator
>
436 void _M_insert_range(iterator __position
,
437 _ForwardIterator __first
, _ForwardIterator __last
,
438 forward_iterator_tag
) {
439 if (__first
!= __last
) {
441 distance(__first
, __last
, __n
);
442 if (capacity() - size() >= __n
) {
443 copy_backward(__position
, end(), _M_finish
+ difference_type(__n
));
444 copy(__first
, __last
, __position
);
445 _M_finish
+= difference_type(__n
);
448 size_type __len
= size() + max(size(), __n
);
449 unsigned int* __q
= _M_bit_alloc(__len
);
450 iterator __i
= copy(begin(), __position
, iterator(__q
, 0));
451 __i
= copy(__first
, __last
, __i
);
452 _M_finish
= copy(__position
, end(), __i
);
454 _M_end_of_storage
= __q
+ (__len
+ __WORD_BIT
- 1)/__WORD_BIT
;
455 _M_start
= iterator(__q
, 0);
461 iterator
begin() { return _M_start
; }
462 const_iterator
begin() const { return _M_start
; }
463 iterator
end() { return _M_finish
; }
464 const_iterator
end() const { return _M_finish
; }
466 reverse_iterator
rbegin() { return reverse_iterator(end()); }
467 const_reverse_iterator
rbegin() const {
468 return const_reverse_iterator(end());
470 reverse_iterator
rend() { return reverse_iterator(begin()); }
471 const_reverse_iterator
rend() const {
472 return const_reverse_iterator(begin());
475 size_type
size() const { return size_type(end() - begin()); }
476 size_type
max_size() const { return size_type(-1); }
477 size_type
capacity() const {
478 return size_type(const_iterator(_M_end_of_storage
, 0) - begin());
480 bool empty() const { return begin() == end(); }
482 reference
operator[](size_type __n
)
483 { return *(begin() + difference_type(__n
)); }
484 const_reference
operator[](size_type __n
) const
485 { return *(begin() + difference_type(__n
)); }
487 void _M_range_check(size_type __n
) const {
488 if (__n
>= this->size())
489 __throw_out_of_range("vector<bool>");
492 reference
at(size_type __n
)
493 { _M_range_check(__n
); return (*this)[__n
]; }
494 const_reference
at(size_type __n
) const
495 { _M_range_check(__n
); return (*this)[__n
]; }
497 explicit vector(const allocator_type
& __a
= allocator_type())
498 : _Bvector_base
<_Alloc
>(__a
) {}
500 vector(size_type __n
, bool __value
,
501 const allocator_type
& __a
= allocator_type())
502 : _Bvector_base
<_Alloc
>(__a
)
505 fill(_M_start
._M_p
, _M_end_of_storage
, __value
? ~0 : 0);
508 explicit vector(size_type __n
)
509 : _Bvector_base
<_Alloc
>(allocator_type())
512 fill(_M_start
._M_p
, _M_end_of_storage
, 0);
515 vector(const vector
& __x
) : _Bvector_base
<_Alloc
>(__x
.get_allocator()) {
516 _M_initialize(__x
.size());
517 copy(__x
.begin(), __x
.end(), _M_start
);
520 // Check whether it's an integral type. If so, it's not an iterator.
522 template <class _Integer
>
523 void _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
) {
525 fill(_M_start
._M_p
, _M_end_of_storage
, __x
? ~0 : 0);
528 template <class _InputIterator
>
529 void _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
531 _M_initialize_range(__first
, __last
, __iterator_category(__first
));
534 template <class _InputIterator
>
535 vector(_InputIterator __first
, _InputIterator __last
,
536 const allocator_type
& __a
= allocator_type())
537 : _Bvector_base
<_Alloc
>(__a
)
539 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
540 _M_initialize_dispatch(__first
, __last
, _Integral());
545 vector
& operator=(const vector
& __x
) {
546 if (&__x
== this) return *this;
547 if (__x
.size() > capacity()) {
549 _M_initialize(__x
.size());
551 copy(__x
.begin(), __x
.end(), begin());
552 _M_finish
= begin() + difference_type(__x
.size());
556 // assign(), a generalized assignment member function. Two
557 // versions: one that takes a count, and one that takes a range.
558 // The range version is a member template, so we dispatch on whether
559 // or not the type is an integer.
561 void _M_fill_assign(size_t __n
, bool __x
) {
563 fill(_M_start
._M_p
, _M_end_of_storage
, __x
? ~0 : 0);
564 insert(end(), __n
- size(), __x
);
567 erase(begin() + __n
, end());
568 fill(_M_start
._M_p
, _M_end_of_storage
, __x
? ~0 : 0);
572 void assign(size_t __n
, bool __x
) { _M_fill_assign(__n
, __x
); }
574 template <class _InputIterator
>
575 void assign(_InputIterator __first
, _InputIterator __last
) {
576 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
577 _M_assign_dispatch(__first
, __last
, _Integral());
580 template <class _Integer
>
581 void _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
582 { _M_fill_assign((size_t) __n
, (bool) __val
); }
584 template <class _InputIter
>
585 void _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
)
586 { _M_assign_aux(__first
, __last
, __iterator_category(__first
)); }
588 template <class _InputIterator
>
589 void _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
590 input_iterator_tag
) {
591 iterator __cur
= begin();
592 for ( ; __first
!= __last
&& __cur
!= end(); ++__cur
, ++__first
)
594 if (__first
== __last
)
597 insert(end(), __first
, __last
);
600 template <class _ForwardIterator
>
601 void _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
602 forward_iterator_tag
) {
604 distance(__first
, __last
, __len
);
606 erase(copy(__first
, __last
, begin()), end());
608 _ForwardIterator __mid
= __first
;
609 advance(__mid
, size());
610 copy(__first
, __mid
, begin());
611 insert(end(), __mid
, __last
);
615 void reserve(size_type __n
) {
616 if (capacity() < __n
) {
617 unsigned int* __q
= _M_bit_alloc(__n
);
618 _M_finish
= copy(begin(), end(), iterator(__q
, 0));
620 _M_start
= iterator(__q
, 0);
621 _M_end_of_storage
= __q
+ (__n
+ __WORD_BIT
- 1)/__WORD_BIT
;
625 reference
front() { return *begin(); }
626 const_reference
front() const { return *begin(); }
627 reference
back() { return *(end() - 1); }
628 const_reference
back() const { return *(end() - 1); }
629 void push_back(bool __x
) {
630 if (_M_finish
._M_p
!= _M_end_of_storage
)
633 _M_insert_aux(end(), __x
);
635 void swap(vector
<bool, _Alloc
>& __x
) {
636 std::swap(_M_start
, __x
._M_start
);
637 std::swap(_M_finish
, __x
._M_finish
);
638 std::swap(_M_end_of_storage
, __x
._M_end_of_storage
);
640 iterator
insert(iterator __position
, bool __x
= bool()) {
641 difference_type __n
= __position
- begin();
642 if (_M_finish
._M_p
!= _M_end_of_storage
&& __position
== end())
645 _M_insert_aux(__position
, __x
);
646 return begin() + __n
;
649 // Check whether it's an integral type. If so, it's not an iterator.
651 template <class _Integer
>
652 void _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
654 _M_fill_insert(__pos
, __n
, __x
);
657 template <class _InputIterator
>
658 void _M_insert_dispatch(iterator __pos
,
659 _InputIterator __first
, _InputIterator __last
,
661 _M_insert_range(__pos
, __first
, __last
, __iterator_category(__first
));
664 template <class _InputIterator
>
665 void insert(iterator __position
,
666 _InputIterator __first
, _InputIterator __last
) {
667 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
668 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
671 void _M_fill_insert(iterator __position
, size_type __n
, bool __x
) {
672 if (__n
== 0) return;
673 if (capacity() - size() >= __n
) {
674 copy_backward(__position
, end(), _M_finish
+ difference_type(__n
));
675 fill(__position
, __position
+ difference_type(__n
), __x
);
676 _M_finish
+= difference_type(__n
);
679 size_type __len
= size() + max(size(), __n
);
680 unsigned int* __q
= _M_bit_alloc(__len
);
681 iterator __i
= copy(begin(), __position
, iterator(__q
, 0));
682 fill_n(__i
, __n
, __x
);
683 _M_finish
= copy(__position
, end(), __i
+ difference_type(__n
));
685 _M_end_of_storage
= __q
+ (__len
+ __WORD_BIT
- 1)/__WORD_BIT
;
686 _M_start
= iterator(__q
, 0);
690 void insert(iterator __position
, size_type __n
, bool __x
) {
691 _M_fill_insert(__position
, __n
, __x
);
694 void pop_back() { --_M_finish
; }
695 iterator
erase(iterator __position
) {
696 if (__position
+ 1 != end())
697 copy(__position
+ 1, end(), __position
);
701 iterator
erase(iterator __first
, iterator __last
) {
702 _M_finish
= copy(__last
, end(), __first
);
705 void resize(size_type __new_size
, bool __x
= bool()) {
706 if (__new_size
< size())
707 erase(begin() + difference_type(__new_size
), end());
709 insert(end(), __new_size
- size(), __x
);
712 for (unsigned int* __p
= _M_start
._M_p
; __p
!= _M_end_of_storage
; ++__p
)
716 void clear() { erase(begin(), end()); }
719 // This typedef is non-standard. It is provided for backward compatibility.
720 typedef vector
<bool, alloc
> bit_vector
;
724 #endif /* __SGI_STL_INTERNAL_BVECTOR_H */