2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_bvector.h
blob884256151f431dd78a875bbd4e24307e0487a2e3
1 // bit_vector and vector<bool> specialization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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 _BVECTOR_H
62 #define _BVECTOR_H 1
64 namespace __gnu_norm
66 typedef unsigned long _Bit_type;
67 enum { _S_word_bit = int(CHAR_BIT * sizeof(_Bit_type)) };
69 struct _Bit_reference {
71 _Bit_type * _M_p;
72 _Bit_type _M_mask;
73 _Bit_reference(_Bit_type * __x, _Bit_type __y)
74 : _M_p(__x), _M_mask(__y) {}
76 public:
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;
83 return *this;
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>
96 _Bit_type * _M_p;
97 unsigned int _M_offset;
99 _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
100 : _M_p(__x), _M_offset(__y) {}
102 void _M_bump_up() {
103 if (_M_offset++ == _S_word_bit - 1) {
104 _M_offset = 0;
105 ++_M_p;
108 void _M_bump_down() {
109 if (_M_offset-- == 0) {
110 _M_offset = _S_word_bit - 1;
111 --_M_p;
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;
119 if (__n < 0) {
120 _M_offset = (unsigned int) __n + _S_word_bit;
121 --_M_p;
122 } else
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 {
136 return __i < *this;
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);
146 inline ptrdiff_t
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++() {
164 _M_bump_up();
165 return *this;
167 iterator operator++(int) {
168 iterator __tmp = *this;
169 _M_bump_up();
170 return __tmp;
172 iterator& operator--() {
173 _M_bump_down();
174 return *this;
176 iterator operator--(int) {
177 iterator __tmp = *this;
178 _M_bump_down();
179 return __tmp;
181 iterator& operator+=(difference_type __i) {
182 _M_incr(__i);
183 return *this;
185 iterator& operator-=(difference_type __i) {
186 *this += -__i;
187 return *this;
189 iterator operator+(difference_type __i) const {
190 iterator __tmp = *this;
191 return __tmp += __i;
193 iterator operator-(difference_type __i) const {
194 iterator __tmp = *this;
195 return __tmp -= __i;
198 reference operator[](difference_type __i) { return *(*this + __i); }
201 inline _Bit_iterator
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++() {
222 _M_bump_up();
223 return *this;
225 const_iterator operator++(int) {
226 const_iterator __tmp = *this;
227 _M_bump_up();
228 return __tmp;
230 const_iterator& operator--() {
231 _M_bump_down();
232 return *this;
234 const_iterator operator--(int) {
235 const_iterator __tmp = *this;
236 _M_bump_down();
237 return __tmp;
239 const_iterator& operator+=(difference_type __i) {
240 _M_incr(__i);
241 return *this;
243 const_iterator& operator-=(difference_type __i) {
244 *this += -__i;
245 return *this;
247 const_iterator operator+(difference_type __i) const {
248 const_iterator __tmp = *this;
249 return __tmp += __i;
251 const_iterator operator-(difference_type __i) const {
252 const_iterator __tmp = *this;
253 return __tmp -= __i;
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>
265 class _Bvector_base
266 : public _Alloc::template rebind<_Bit_type>::other
268 typedef typename _Alloc::template rebind<_Bit_type>::other _Bit_alloc_type;
269 public:
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(); }
279 protected:
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() {
284 if (_M_start._M_p)
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>
297 namespace __gnu_norm
299 template <typename _Alloc>
300 class vector<bool, _Alloc> : public _Bvector_base<_Alloc>
302 public:
303 typedef bool value_type;
304 typedef size_t size_type;
305 typedef ptrdiff_t difference_type;
306 typedef _Bit_reference reference;
307 typedef bool const_reference;
308 typedef _Bit_reference* pointer;
309 typedef const bool* const_pointer;
311 typedef _Bit_iterator iterator;
312 typedef _Bit_const_iterator const_iterator;
314 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
315 typedef std::reverse_iterator<iterator> reverse_iterator;
317 typedef typename _Bvector_base<_Alloc>::allocator_type allocator_type;
318 allocator_type get_allocator() const {
319 return _Bvector_base<_Alloc>::get_allocator();
322 protected:
323 using _Bvector_base<_Alloc>::_M_bit_alloc;
324 using _Bvector_base<_Alloc>::_M_deallocate;
325 using _Bvector_base<_Alloc>::_M_start;
326 using _Bvector_base<_Alloc>::_M_finish;
327 using _Bvector_base<_Alloc>::_M_end_of_storage;
329 protected:
330 void _M_initialize(size_type __n) {
331 _Bit_type * __q = this->_M_bit_alloc(__n);
332 this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
333 this->_M_start = iterator(__q, 0);
334 this->_M_finish = this->_M_start + difference_type(__n);
336 void _M_insert_aux(iterator __position, bool __x) {
337 if (this->_M_finish._M_p != this->_M_end_of_storage) {
338 std::copy_backward(__position, this->_M_finish, this->_M_finish + 1);
339 *__position = __x;
340 ++this->_M_finish;
342 else {
343 size_type __len = size()
344 ? 2 * size() : static_cast<size_type>(_S_word_bit);
345 _Bit_type * __q = this->_M_bit_alloc(__len);
346 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
347 *__i++ = __x;
348 this->_M_finish = std::copy(__position, end(), __i);
349 this->_M_deallocate();
350 this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
351 this->_M_start = iterator(__q, 0);
355 template <class _InputIterator>
356 void _M_initialize_range(_InputIterator __first, _InputIterator __last,
357 input_iterator_tag) {
358 this->_M_start = iterator();
359 this->_M_finish = iterator();
360 this->_M_end_of_storage = 0;
361 for ( ; __first != __last; ++__first)
362 push_back(*__first);
365 template <class _ForwardIterator>
366 void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,
367 forward_iterator_tag) {
368 size_type __n = std::distance(__first, __last);
369 _M_initialize(__n);
370 std::copy(__first, __last, this->_M_start);
373 template <class _InputIterator>
374 void _M_insert_range(iterator __pos,
375 _InputIterator __first, _InputIterator __last,
376 input_iterator_tag) {
377 for ( ; __first != __last; ++__first) {
378 __pos = insert(__pos, *__first);
379 ++__pos;
383 template <class _ForwardIterator>
384 void _M_insert_range(iterator __position,
385 _ForwardIterator __first, _ForwardIterator __last,
386 forward_iterator_tag) {
387 if (__first != __last) {
388 size_type __n = std::distance(__first, __last);
389 if (capacity() - size() >= __n) {
390 std::copy_backward(__position, end(),
391 this->_M_finish + difference_type(__n));
392 std::copy(__first, __last, __position);
393 this->_M_finish += difference_type(__n);
395 else {
396 size_type __len = size() + std::max(size(), __n);
397 _Bit_type * __q = this->_M_bit_alloc(__len);
398 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
399 __i = std::copy(__first, __last, __i);
400 this->_M_finish = std::copy(__position, end(), __i);
401 this->_M_deallocate();
402 this->_M_end_of_storage
403 = __q + (__len + _S_word_bit - 1)/_S_word_bit;
404 this->_M_start = iterator(__q, 0);
409 public:
410 iterator begin() { return this->_M_start; }
411 const_iterator begin() const { return this->_M_start; }
412 iterator end() { return this->_M_finish; }
413 const_iterator end() const { return this->_M_finish; }
415 reverse_iterator rbegin() { return reverse_iterator(end()); }
416 const_reverse_iterator rbegin() const {
417 return const_reverse_iterator(end());
419 reverse_iterator rend() { return reverse_iterator(begin()); }
420 const_reverse_iterator rend() const {
421 return const_reverse_iterator(begin());
424 size_type size() const { return size_type(end() - begin()); }
425 size_type max_size() const { return size_type(-1); }
426 size_type capacity() const {
427 return size_type(const_iterator(this->_M_end_of_storage, 0) - begin());
429 bool empty() const { return begin() == end(); }
431 reference operator[](size_type __n)
432 { return *(begin() + difference_type(__n)); }
433 const_reference operator[](size_type __n) const
434 { return *(begin() + difference_type(__n)); }
436 void _M_range_check(size_type __n) const {
437 if (__n >= this->size())
438 __throw_out_of_range(__N("vector<bool>::_M_range_check"));
441 reference at(size_type __n)
442 { _M_range_check(__n); return (*this)[__n]; }
443 const_reference at(size_type __n) const
444 { _M_range_check(__n); return (*this)[__n]; }
446 explicit vector(const allocator_type& __a = allocator_type())
447 : _Bvector_base<_Alloc>(__a) {}
449 vector(size_type __n, bool __value,
450 const allocator_type& __a = allocator_type())
451 : _Bvector_base<_Alloc>(__a)
453 _M_initialize(__n);
454 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __value ? ~0 : 0);
457 explicit vector(size_type __n)
458 : _Bvector_base<_Alloc>(allocator_type())
460 _M_initialize(__n);
461 std::fill(this->_M_start._M_p, this->_M_end_of_storage, 0);
464 vector(const vector& __x) : _Bvector_base<_Alloc>(__x.get_allocator()) {
465 _M_initialize(__x.size());
466 std::copy(__x.begin(), __x.end(), this->_M_start);
469 // Check whether it's an integral type. If so, it's not an iterator.
471 template <class _Integer>
472 void _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type) {
473 _M_initialize(__n);
474 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
477 template <class _InputIterator>
478 void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
479 __false_type) {
480 _M_initialize_range(__first, __last, std::__iterator_category(__first));
483 template <class _InputIterator>
484 vector(_InputIterator __first, _InputIterator __last,
485 const allocator_type& __a = allocator_type())
486 : _Bvector_base<_Alloc>(__a)
488 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
489 _M_initialize_dispatch(__first, __last, _Integral());
492 ~vector() { }
494 vector& operator=(const vector& __x) {
495 if (&__x == this) return *this;
496 if (__x.size() > capacity()) {
497 this->_M_deallocate();
498 _M_initialize(__x.size());
500 std::copy(__x.begin(), __x.end(), begin());
501 this->_M_finish = begin() + difference_type(__x.size());
502 return *this;
505 // assign(), a generalized assignment member function. Two
506 // versions: one that takes a count, and one that takes a range.
507 // The range version is a member template, so we dispatch on whether
508 // or not the type is an integer.
510 void _M_fill_assign(size_t __n, bool __x) {
511 if (__n > size()) {
512 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
513 insert(end(), __n - size(), __x);
515 else {
516 erase(begin() + __n, end());
517 std::fill(this->_M_start._M_p, this->_M_end_of_storage, __x ? ~0 : 0);
521 void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }
523 template <class _InputIterator>
524 void assign(_InputIterator __first, _InputIterator __last) {
525 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
526 _M_assign_dispatch(__first, __last, _Integral());
529 template <class _Integer>
530 void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
531 { _M_fill_assign((size_t) __n, (bool) __val); }
533 template <class _InputIterator>
534 void _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type)
535 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
537 template <class _InputIterator>
538 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
539 input_iterator_tag) {
540 iterator __cur = begin();
541 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
542 *__cur = *__first;
543 if (__first == __last)
544 erase(__cur, end());
545 else
546 insert(end(), __first, __last);
549 template <class _ForwardIterator>
550 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
551 forward_iterator_tag) {
552 size_type __len = std::distance(__first, __last);
553 if (__len < size())
554 erase(std::copy(__first, __last, begin()), end());
555 else {
556 _ForwardIterator __mid = __first;
557 std::advance(__mid, size());
558 std::copy(__first, __mid, begin());
559 insert(end(), __mid, __last);
563 void reserve(size_type __n) {
564 if (__n > this->max_size())
565 __throw_length_error(__N("vector::reserve"));
566 if (this->capacity() < __n) {
567 _Bit_type * __q = this->_M_bit_alloc(__n);
568 this->_M_finish = std::copy(begin(), end(), iterator(__q, 0));
569 this->_M_deallocate();
570 this->_M_start = iterator(__q, 0);
571 this->_M_end_of_storage = __q + (__n + _S_word_bit - 1)/_S_word_bit;
575 reference front() { return *begin(); }
576 const_reference front() const { return *begin(); }
577 reference back() { return *(end() - 1); }
578 const_reference back() const { return *(end() - 1); }
579 void push_back(bool __x) {
580 if (this->_M_finish._M_p != this->_M_end_of_storage)
581 *this->_M_finish++ = __x;
582 else
583 _M_insert_aux(end(), __x);
585 void swap(vector<bool, _Alloc>& __x) {
586 std::swap(this->_M_start, __x._M_start);
587 std::swap(this->_M_finish, __x._M_finish);
588 std::swap(this->_M_end_of_storage, __x._M_end_of_storage);
591 // [23.2.5]/1, third-to-last entry in synopsis listing
592 static void swap(reference __x, reference __y) {
593 bool __tmp = __x;
594 __x = __y;
595 __y = __tmp;
598 iterator insert(iterator __position, bool __x = bool()) {
599 difference_type __n = __position - begin();
600 if (this->_M_finish._M_p != this->_M_end_of_storage
601 && __position == end())
602 *this->_M_finish++ = __x;
603 else
604 _M_insert_aux(__position, __x);
605 return begin() + __n;
608 // Check whether it's an integral type. If so, it's not an iterator.
610 template <class _Integer>
611 void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
612 __true_type) {
613 _M_fill_insert(__pos, __n, __x);
616 template <class _InputIterator>
617 void _M_insert_dispatch(iterator __pos,
618 _InputIterator __first, _InputIterator __last,
619 __false_type) {
620 _M_insert_range(__pos, __first, __last, std::__iterator_category(__first));
623 template <class _InputIterator>
624 void insert(iterator __position,
625 _InputIterator __first, _InputIterator __last) {
626 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
627 _M_insert_dispatch(__position, __first, __last, _Integral());
630 void _M_fill_insert(iterator __position, size_type __n, bool __x) {
631 if (__n == 0) return;
632 if (capacity() - size() >= __n) {
633 std::copy_backward(__position, end(),
634 this->_M_finish + difference_type(__n));
635 std::fill(__position, __position + difference_type(__n), __x);
636 this->_M_finish += difference_type(__n);
638 else {
639 size_type __len = size() + std::max(size(), __n);
640 _Bit_type * __q = this->_M_bit_alloc(__len);
641 iterator __i = std::copy(begin(), __position, iterator(__q, 0));
642 std::fill_n(__i, __n, __x);
643 this->_M_finish = std::copy(__position, end(), __i + difference_type(__n));
644 this->_M_deallocate();
645 this->_M_end_of_storage = __q + (__len + _S_word_bit - 1)/_S_word_bit;
646 this->_M_start = iterator(__q, 0);
650 void insert(iterator __position, size_type __n, bool __x) {
651 _M_fill_insert(__position, __n, __x);
654 void pop_back() { --this->_M_finish; }
655 iterator erase(iterator __position) {
656 if (__position + 1 != end())
657 std::copy(__position + 1, end(), __position);
658 --this->_M_finish;
659 return __position;
661 iterator erase(iterator __first, iterator __last) {
662 this->_M_finish = std::copy(__last, end(), __first);
663 return __first;
665 void resize(size_type __new_size, bool __x = bool()) {
666 if (__new_size < size())
667 erase(begin() + difference_type(__new_size), end());
668 else
669 insert(end(), __new_size - size(), __x);
671 void flip() {
672 for (_Bit_type * __p = this->_M_start._M_p;
673 __p != this->_M_end_of_storage;
674 ++__p)
675 *__p = ~*__p;
678 void clear() { erase(begin(), end()); }
681 // This typedef is non-standard. It is provided for backward compatibility.
682 typedef vector<bool, __alloc> bit_vector;
683 } // namespace __gnu_norm
685 #endif /* _BVECTOR_H */