STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
stl_bvector.h File Reference
#include <bits/stl_vector.h>

Go to the source code of this file.

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {vector}

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

A specialization of vector for booleans which offers fixed time access to individual elements in any order.

Template Parameters
_AllocAllocator type.

Note that vector<bool> does not actually meet the requirements for being a container. This is because the reference and pointer types are not really references and pointers to bool. See DR96 for details.

See Also
vector for function documentation.

In some terminology a vector can be described as a dynamic C-style array, it offers fast and efficient access to individual elements in any order and saves the user from worrying about memory and size allocation. Subscripting ( [] ) access is also provided as with C-style arrays.

64 {
65 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
66 
67  typedef unsigned long _Bit_type;
68  enum { _S_word_bit = int(__CHAR_BIT__ * sizeof(_Bit_type)) };
69 
70  struct _Bit_reference
71  {
72  _Bit_type * _M_p;
73  _Bit_type _M_mask;
74 
75  _Bit_reference(_Bit_type * __x, _Bit_type __y)
76  : _M_p(__x), _M_mask(__y) { }
77 
78  _Bit_reference() _GLIBCXX_NOEXCEPT : _M_p(0), _M_mask(0) { }
79 
80  operator bool() const _GLIBCXX_NOEXCEPT
81  { return !!(*_M_p & _M_mask); }
82 
83  _Bit_reference&
84  operator=(bool __x) _GLIBCXX_NOEXCEPT
85  {
86  if (__x)
87  *_M_p |= _M_mask;
88  else
89  *_M_p &= ~_M_mask;
90  return *this;
91  }
92 
93  _Bit_reference&
94  operator=(const _Bit_reference& __x) _GLIBCXX_NOEXCEPT
95  { return *this = bool(__x); }
96 
97  bool
98  operator==(const _Bit_reference& __x) const
99  { return bool(*this) == bool(__x); }
100 
101  bool
102  operator<(const _Bit_reference& __x) const
103  { return !bool(*this) && bool(__x); }
104 
105  void
106  flip() _GLIBCXX_NOEXCEPT
107  { *_M_p ^= _M_mask; }
108  };
109 
110 #if __cplusplus >= 201103L
111  inline void
112  swap(_Bit_reference __x, _Bit_reference __y) noexcept
113  {
114  bool __tmp = __x;
115  __x = __y;
116  __y = __tmp;
117  }
118 
119  inline void
120  swap(_Bit_reference __x, bool& __y) noexcept
121  {
122  bool __tmp = __x;
123  __x = __y;
124  __y = __tmp;
125  }
126 
127  inline void
128  swap(bool& __x, _Bit_reference __y) noexcept
129  {
130  bool __tmp = __x;
131  __x = __y;
132  __y = __tmp;
133  }
134 #endif
135 
136  struct _Bit_iterator_base
137  : public std::iterator<std::random_access_iterator_tag, bool>
138  {
139  _Bit_type * _M_p;
140  unsigned int _M_offset;
141 
142  _Bit_iterator_base(_Bit_type * __x, unsigned int __y)
143  : _M_p(__x), _M_offset(__y) { }
144 
145  void
146  _M_bump_up()
147  {
148  if (_M_offset++ == int(_S_word_bit) - 1)
149  {
150  _M_offset = 0;
151  ++_M_p;
152  }
153  }
154 
155  void
156  _M_bump_down()
157  {
158  if (_M_offset-- == 0)
159  {
160  _M_offset = int(_S_word_bit) - 1;
161  --_M_p;
162  }
163  }
164 
165  void
166  _M_incr(ptrdiff_t __i)
167  {
168  difference_type __n = __i + _M_offset;
169  _M_p += __n / int(_S_word_bit);
170  __n = __n % int(_S_word_bit);
171  if (__n < 0)
172  {
173  __n += int(_S_word_bit);
174  --_M_p;
175  }
176  _M_offset = static_cast<unsigned int>(__n);
177  }
178 
179  bool
180  operator==(const _Bit_iterator_base& __i) const
181  { return _M_p == __i._M_p && _M_offset == __i._M_offset; }
182 
183  bool
184  operator<(const _Bit_iterator_base& __i) const
185  {
186  return _M_p < __i._M_p
187  || (_M_p == __i._M_p && _M_offset < __i._M_offset);
188  }
189 
190  bool
191  operator!=(const _Bit_iterator_base& __i) const
192  { return !(*this == __i); }
193 
194  bool
195  operator>(const _Bit_iterator_base& __i) const
196  { return __i < *this; }
197 
198  bool
199  operator<=(const _Bit_iterator_base& __i) const
200  { return !(__i < *this); }
201 
202  bool
203  operator>=(const _Bit_iterator_base& __i) const
204  { return !(*this < __i); }
205  };
206 
207  inline ptrdiff_t
208  operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
209  {
210  return (int(_S_word_bit) * (__x._M_p - __y._M_p)
211  + __x._M_offset - __y._M_offset);
212  }
213 
214  struct _Bit_iterator : public _Bit_iterator_base
215  {
216  typedef _Bit_reference reference;
217  typedef _Bit_reference* pointer;
218  typedef _Bit_iterator iterator;
219 
220  _Bit_iterator() : _Bit_iterator_base(0, 0) { }
221 
222  _Bit_iterator(_Bit_type * __x, unsigned int __y)
223  : _Bit_iterator_base(__x, __y) { }
224 
225  reference
226  operator*() const
227  { return reference(_M_p, 1UL << _M_offset); }
228 
229  iterator&
230  operator++()
231  {
232  _M_bump_up();
233  return *this;
234  }
235 
236  iterator
237  operator++(int)
238  {
239  iterator __tmp = *this;
240  _M_bump_up();
241  return __tmp;
242  }
243 
244  iterator&
245  operator--()
246  {
247  _M_bump_down();
248  return *this;
249  }
250 
251  iterator
252  operator--(int)
253  {
254  iterator __tmp = *this;
255  _M_bump_down();
256  return __tmp;
257  }
258 
259  iterator&
260  operator+=(difference_type __i)
261  {
262  _M_incr(__i);
263  return *this;
264  }
265 
266  iterator&
267  operator-=(difference_type __i)
268  {
269  *this += -__i;
270  return *this;
271  }
272 
273  iterator
274  operator+(difference_type __i) const
275  {
276  iterator __tmp = *this;
277  return __tmp += __i;
278  }
279 
280  iterator
281  operator-(difference_type __i) const
282  {
283  iterator __tmp = *this;
284  return __tmp -= __i;
285  }
286 
287  reference
288  operator[](difference_type __i) const
289  { return *(*this + __i); }
290  };
291 
292  inline _Bit_iterator
293  operator+(ptrdiff_t __n, const _Bit_iterator& __x)
294  { return __x + __n; }
295 
296  struct _Bit_const_iterator : public _Bit_iterator_base
297  {
298  typedef bool reference;
299  typedef bool const_reference;
300  typedef const bool* pointer;
301  typedef _Bit_const_iterator const_iterator;
302 
303  _Bit_const_iterator() : _Bit_iterator_base(0, 0) { }
304 
305  _Bit_const_iterator(_Bit_type * __x, unsigned int __y)
306  : _Bit_iterator_base(__x, __y) { }
307 
308  _Bit_const_iterator(const _Bit_iterator& __x)
309  : _Bit_iterator_base(__x._M_p, __x._M_offset) { }
310 
311  const_reference
312  operator*() const
313  { return _Bit_reference(_M_p, 1UL << _M_offset); }
314 
315  const_iterator&
316  operator++()
317  {
318  _M_bump_up();
319  return *this;
320  }
321 
322  const_iterator
323  operator++(int)
324  {
325  const_iterator __tmp = *this;
326  _M_bump_up();
327  return __tmp;
328  }
329 
330  const_iterator&
331  operator--()
332  {
333  _M_bump_down();
334  return *this;
335  }
336 
337  const_iterator
338  operator--(int)
339  {
340  const_iterator __tmp = *this;
341  _M_bump_down();
342  return __tmp;
343  }
344 
345  const_iterator&
346  operator+=(difference_type __i)
347  {
348  _M_incr(__i);
349  return *this;
350  }
351 
352  const_iterator&
353  operator-=(difference_type __i)
354  {
355  *this += -__i;
356  return *this;
357  }
358 
359  const_iterator
360  operator+(difference_type __i) const
361  {
362  const_iterator __tmp = *this;
363  return __tmp += __i;
364  }
365 
366  const_iterator
367  operator-(difference_type __i) const
368  {
369  const_iterator __tmp = *this;
370  return __tmp -= __i;
371  }
372 
373  const_reference
374  operator[](difference_type __i) const
375  { return *(*this + __i); }
376  };
377 
378  inline _Bit_const_iterator
379  operator+(ptrdiff_t __n, const _Bit_const_iterator& __x)
380  { return __x + __n; }
381 
382  inline void
383  __fill_bvector(_Bit_iterator __first, _Bit_iterator __last, bool __x)
384  {
385  for (; __first != __last; ++__first)
386  *__first = __x;
387  }
388 
389  inline void
390  fill(_Bit_iterator __first, _Bit_iterator __last, const bool& __x)
391  {
392  if (__first._M_p != __last._M_p)
393  {
394  std::fill(__first._M_p + 1, __last._M_p, __x ? ~0 : 0);
395  __fill_bvector(__first, _Bit_iterator(__first._M_p + 1, 0), __x);
396  __fill_bvector(_Bit_iterator(__last._M_p, 0), __last, __x);
397  }
398  else
399  __fill_bvector(__first, __last, __x);
400  }
401 
402  template<typename _Alloc>
403  struct _Bvector_base
404  {
405  typedef typename _Alloc::template rebind<_Bit_type>::other
406  _Bit_alloc_type;
407 
408  struct _Bvector_impl
409  : public _Bit_alloc_type
410  {
411  _Bit_iterator _M_start;
412  _Bit_iterator _M_finish;
413  _Bit_type* _M_end_of_storage;
414 
415  _Bvector_impl()
416  : _Bit_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage(0)
417  { }
418 
419  _Bvector_impl(const _Bit_alloc_type& __a)
420  : _Bit_alloc_type(__a), _M_start(), _M_finish(), _M_end_of_storage(0)
421  { }
422 
423 #if __cplusplus >= 201103L
424  _Bvector_impl(_Bit_alloc_type&& __a)
425  : _Bit_alloc_type(std::move(__a)), _M_start(), _M_finish(),
426  _M_end_of_storage(0)
427  { }
428 #endif
429  };
430 
431  public:
432  typedef _Alloc allocator_type;
433 
434  _Bit_alloc_type&
435  _M_get_Bit_allocator() _GLIBCXX_NOEXCEPT
436  { return *static_cast<_Bit_alloc_type*>(&this->_M_impl); }
437 
438  const _Bit_alloc_type&
439  _M_get_Bit_allocator() const _GLIBCXX_NOEXCEPT
440  { return *static_cast<const _Bit_alloc_type*>(&this->_M_impl); }
441 
442  allocator_type
443  get_allocator() const _GLIBCXX_NOEXCEPT
444  { return allocator_type(_M_get_Bit_allocator()); }
445 
446  _Bvector_base()
447  : _M_impl() { }
448 
449  _Bvector_base(const allocator_type& __a)
450  : _M_impl(__a) { }
451 
452 #if __cplusplus >= 201103L
453  _Bvector_base(_Bvector_base&& __x) noexcept
454  : _M_impl(std::move(__x._M_get_Bit_allocator()))
455  {
456  this->_M_impl._M_start = __x._M_impl._M_start;
457  this->_M_impl._M_finish = __x._M_impl._M_finish;
458  this->_M_impl._M_end_of_storage = __x._M_impl._M_end_of_storage;
459  __x._M_impl._M_start = _Bit_iterator();
460  __x._M_impl._M_finish = _Bit_iterator();
461  __x._M_impl._M_end_of_storage = 0;
462  }
463 #endif
464 
465  ~_Bvector_base()
466  { this->_M_deallocate(); }
467 
468  protected:
469  _Bvector_impl _M_impl;
470 
471  _Bit_type*
472  _M_allocate(size_t __n)
473  { return _M_impl.allocate(_S_nword(__n)); }
474 
475  void
476  _M_deallocate()
477  {
478  if (_M_impl._M_start._M_p)
479  _M_impl.deallocate(_M_impl._M_start._M_p,
480  _M_impl._M_end_of_storage - _M_impl._M_start._M_p);
481  }
482 
483  static size_t
484  _S_nword(size_t __n)
485  { return (__n + int(_S_word_bit) - 1) / int(_S_word_bit); }
486  };
487 
488 _GLIBCXX_END_NAMESPACE_CONTAINER
489 } // namespace std
bool operator>=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:644
bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
bool operator<=(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:580
bool operator<(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:548
#define bool
Definition: stdbool.h:33
_Safe_iterator< _Iterator, _Sequence > operator+(typename _Safe_iterator< _Iterator, _Sequence >::difference_type __n, const _Safe_iterator< _Iterator, _Sequence > &__i)
Definition: safe_iterator.h:712
bool operator>(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:612
_Safe_iterator< _IteratorL, _Sequence >::difference_type operator-(const _Safe_iterator< _IteratorL, _Sequence > &__lhs, const _Safe_iterator< _IteratorR, _Sequence > &__rhs)
Definition: safe_iterator.h:680
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
__PTRDIFF_TYPE__ ptrdiff_t
Definition: stddef.h:147
void swap(exception_ptr &__lhs, exception_ptr &__rhs)
Definition: exception_ptr.h:160