_bvector.h

来自「stl的源码」· C头文件 代码 · 共 842 行 · 第 1/2 页

H
842
字号
    for ( ; __first != __last; ++__first)      push_back(*__first);  }  template <class _ForwardIterator>  void _M_initialize_range(_ForwardIterator __first, _ForwardIterator __last,                           const forward_iterator_tag &) {    size_type __n = _STLP_STD::distance(__first, __last);    _M_initialize(__n);    _STLP_STD::copy(__first, __last, this->_M_start);  }  template <class _InputIterator>  void _M_insert_range(iterator __pos,                       _InputIterator __first, _InputIterator __last,                       const input_iterator_tag &) {    for ( ; __first != __last; ++__first) {      __pos = insert(__pos, *__first);      ++__pos;    }  }  template <class _ForwardIterator>  void _M_insert_range(iterator __position,                       _ForwardIterator __first, _ForwardIterator __last,                       const forward_iterator_tag &) {    if (__first != __last) {      size_type __n = _STLP_STD::distance(__first, __last);      if (capacity() - size() >= __n) {        _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + difference_type(__n),                                   random_access_iterator_tag(), (difference_type*)0 );        _STLP_STD::copy(__first, __last, __position);        this->_M_finish += difference_type(__n);      }      else {        size_type __len = size() + (max)(size(), __n);        __chunk_type* __q = this->_M_bit_alloc(__len);        iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));        __i = _STLP_STD::copy(__first, __last, __i);        this->_M_finish = _STLP_STD::copy(__position, end(), __i);        this->_M_deallocate();        this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);        this->_M_start = iterator(__q, 0);      }    }  }#endif /* _STLP_MEMBER_TEMPLATES */public:  iterator begin() { return this->_M_start; }  const_iterator begin() const { return this->_M_start; }  iterator end() { return this->_M_finish; }  const_iterator end() const { return this->_M_finish; }  reverse_iterator rbegin() { return reverse_iterator(end()); }  const_reverse_iterator rbegin() const {    return const_reverse_iterator(end());  }  reverse_iterator rend() { return reverse_iterator(begin()); }  const_reverse_iterator rend() const {    return const_reverse_iterator(begin());  }  size_type size() const { return size_type(end() - begin()); }  size_type max_size() const { return size_type(-1); }  size_type capacity() const {    return size_type(const_iterator(this->_M_end_of_storage._M_data, 0) - begin());  }  bool empty() const { return begin() == end(); }  reference operator[](size_type __n)  { return *(begin() + difference_type(__n)); }  const_reference operator[](size_type __n) const  { return *(begin() + difference_type(__n)); }  void _M_range_check(size_type __n) const {    if (__n >= this->size())      __stl_throw_range_error("vector<bool>");  }  reference at(size_type __n)    { _M_range_check(__n); return (*this)[__n]; }  const_reference at(size_type __n) const    { _M_range_check(__n); return (*this)[__n]; }  explicit __BVECTOR(const allocator_type& __a = allocator_type())    : _STLP_PRIV _Bvector_base<_Alloc >(__a) {}  __BVECTOR(size_type __n, bool __val,            const allocator_type& __a = allocator_type())    : _STLP_PRIV _Bvector_base<_Alloc >(__a) {    _M_initialize(__n);    fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __val ? ~0 : 0);  }  explicit __BVECTOR(size_type __n)    : _STLP_PRIV _Bvector_base<_Alloc >(allocator_type()) {    _M_initialize(__n);    fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), 0);  }  __BVECTOR(const _Self& __x)    : _STLP_PRIV _Bvector_base<_Alloc >(__x.get_allocator()) {    _M_initialize(__x.size());    _STLP_STD::copy(__x.begin(), __x.end(), this->_M_start);  }#if defined (_STLP_MEMBER_TEMPLATES)  template <class _Integer>  void _M_initialize_dispatch(_Integer __n, _Integer __x, const __true_type&) {    _M_initialize(__n);    fill(this->_M_start._M_p, this->_M_end_of_storage._M_data, __x ? ~0 : 0);  }  template <class _InputIterator>  void _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,                              const __false_type&) {    _M_initialize_range(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));  }#  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)  // Check whether it's an integral type.  If so, it's not an iterator.  template <class _InputIterator>  __BVECTOR(_InputIterator __first, _InputIterator __last)    : _STLP_PRIV _Bvector_base<_Alloc >(allocator_type()) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_initialize_dispatch(__first, __last, _Integral());  }#  endif  template <class _InputIterator>  __BVECTOR(_InputIterator __first, _InputIterator __last,            const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)    : _STLP_PRIV _Bvector_base<_Alloc >(__a) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_initialize_dispatch(__first, __last, _Integral());  }#else /* _STLP_MEMBER_TEMPLATES */  __BVECTOR(const_iterator __first, const_iterator __last,            const allocator_type& __a = allocator_type())    : _STLP_PRIV _Bvector_base<_Alloc >(__a) {    size_type __n = _STLP_STD::distance(__first, __last);    _M_initialize(__n);    _STLP_STD::copy(__first, __last, this->_M_start);  }  __BVECTOR(const bool* __first, const bool* __last,            const allocator_type& __a = allocator_type())    : _STLP_PRIV _Bvector_base<_Alloc >(__a) {    size_type __n = _STLP_STD::distance(__first, __last);    _M_initialize(__n);    _STLP_STD::copy(__first, __last, this->_M_start);  }#endif /* _STLP_MEMBER_TEMPLATES */#if !defined (_STLP_NO_MOVE_SEMANTIC)  __BVECTOR(__move_source<_Self> src)    : _STLP_PRIV _Bvector_base<_Alloc >(__move_source<_Base>(src.get())) {}#endif  ~__BVECTOR() {}  __BVECTOR_QUALIFIED& operator=(const __BVECTOR_QUALIFIED& __x) {    if (&__x == this) return *this;    if (__x.size() > capacity()) {      this->_M_deallocate();      _M_initialize(__x.size());    }    _STLP_STD::copy(__x.begin(), __x.end(), begin());    this->_M_finish = begin() + difference_type(__x.size());    return *this;  }  // assign(), a generalized assignment member function.  Two  // versions: one that takes a count, and one that takes a range.  // The range version is a member template, so we dispatch on whether  // or not the type is an integer.  void _M_fill_assign(size_t __n, bool __x) {    if (__n > size()) {      fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __x ? ~0 : 0);      insert(end(), __n - size(), __x);    }    else {      erase(begin() + __n, end());      fill(this->_M_start._M_p, (__chunk_type*)(this->_M_end_of_storage._M_data), __x ? ~0 : 0);    }  }  void assign(size_t __n, bool __x) { _M_fill_assign(__n, __x); }#if defined (_STLP_MEMBER_TEMPLATES)  template <class _InputIterator>  void assign(_InputIterator __first, _InputIterator __last) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_assign_dispatch(__first, __last, _Integral());  }  template <class _Integer>  void _M_assign_dispatch(_Integer __n, _Integer __val, const __true_type&)    { _M_fill_assign((size_t) __n, (bool) __val); }  template <class _InputIter>  void _M_assign_dispatch(_InputIter __first, _InputIter __last, const __false_type&)    { _M_assign_aux(__first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }  template <class _InputIterator>  void _M_assign_aux(_InputIterator __first, _InputIterator __last,                     const input_iterator_tag &) {    iterator __cur = begin();    for ( ; __first != __last && __cur != end(); ++__cur, ++__first)      *__cur = *__first;    if (__first == __last)      erase(__cur, end());    else      insert(end(), __first, __last);  }  template <class _ForwardIterator>  void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,                     const forward_iterator_tag &) {    size_type __len = _STLP_STD::distance(__first, __last);    if (__len < size())      erase(_STLP_STD::copy(__first, __last, begin()), end());    else {      _ForwardIterator __mid = __first;      _STLP_STD::advance(__mid, size());      _STLP_STD::copy(__first, __mid, begin());      insert(end(), __mid, __last);    }  }#endif /* _STLP_MEMBER_TEMPLATES */  void reserve(size_type __n) {    if (capacity() < __n) {      if (max_size() < __n)        __stl_throw_length_error("vector<bool>");      __chunk_type* __q = this->_M_bit_alloc(__n);      _STLP_PRIV _Bit_iterator __z(__q, 0);      this->_M_finish = _STLP_STD::copy(begin(), end(), __z);      this->_M_deallocate();      this->_M_start = iterator(__q, 0);      this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__n);    }  }  reference front() { return *begin(); }  const_reference front() const { return *begin(); }  reference back() { return *(end() - 1); }  const_reference back() const { return *(end() - 1); }  void push_back(bool __x) {    if (this->_M_finish._M_p != this->_M_end_of_storage._M_data) {      *(this->_M_finish) = __x;      ++this->_M_finish;    }    else      _M_insert_aux(end(), __x);  }  void swap(__BVECTOR_QUALIFIED& __x) {    _STLP_STD::swap(this->_M_start, __x._M_start);    _STLP_STD::swap(this->_M_finish, __x._M_finish);    this->_M_end_of_storage.swap(__x._M_end_of_storage);  }#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)  void _M_swap_workaround(__BVECTOR_QUALIFIED& __x) { swap(__x); }#endif  iterator insert(iterator __position, bool __x = bool()) {    difference_type __n = __position - begin();    if (this->_M_finish._M_p != this->_M_end_of_storage._M_data && __position == end()) {      *(this->_M_finish) = __x;      ++this->_M_finish;    }    else      _M_insert_aux(__position, __x);    return begin() + __n;  }#if defined (_STLP_MEMBER_TEMPLATES)  template <class _Integer>  void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,                          const __true_type&) {    _M_fill_insert(__pos, (size_type) __n, (bool) __x);  }  template <class _InputIterator>  void _M_insert_dispatch(iterator __pos,                          _InputIterator __first, _InputIterator __last,                          const __false_type&) {    _M_insert_range(__pos, __first, __last, _STLP_ITERATOR_CATEGORY(__first, _InputIterator));  }  // Check whether it's an integral type.  If so, it's not an iterator.  template <class _InputIterator>  void insert(iterator __position,              _InputIterator __first, _InputIterator __last) {    typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;    _M_insert_dispatch(__position, __first, __last, _Integral());  }#else /* _STLP_MEMBER_TEMPLATES */  void insert(iterator __position,              const_iterator __first, const_iterator __last) {    if (__first == __last) return;    size_type __n = _STLP_STD::distance(__first, __last);    if (capacity() - size() >= __n) {      _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + __n,                                 random_access_iterator_tag(), (difference_type*)0 );      _STLP_STD::copy(__first, __last, __position);      this->_M_finish += __n;    }    else {      size_type __len = size() + (max)(size(), __n);      __chunk_type* __q = this->_M_bit_alloc(__len);      iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));      __i = _STLP_STD::copy(__first, __last, __i);      this->_M_finish = _STLP_STD::copy(__position, end(), __i);      this->_M_deallocate();      this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);      this->_M_start = iterator(__q, 0);    }  }  void insert(iterator __position, const bool* __first, const bool* __last) {    if (__first == __last) return;    size_type __n = _STLP_STD::distance(__first, __last);    if (capacity() - size() >= __n) {      _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + __n,                                 random_access_iterator_tag(), (difference_type*)0 );      _STLP_STD::copy(__first, __last, __position);      this->_M_finish += __n;    }    else {      size_type __len = size() + (max)(size(), __n);      __chunk_type* __q = this->_M_bit_alloc(__len);      iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));      __i = _STLP_STD::copy(__first, __last, __i);      this->_M_finish = _STLP_STD::copy(__position, end(), __i);      this->_M_deallocate();      this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);      this->_M_start = iterator(__q, 0);    }  }#endif /* _STLP_MEMBER_TEMPLATES */  void _M_fill_insert(iterator __position, size_type __n, bool __x) {    if (__n == 0) return;    if (capacity() - size() >= __n) {      _STLP_PRIV __copy_backward(__position, end(), this->_M_finish + difference_type(__n),                                 random_access_iterator_tag(), (difference_type*)0 );      fill(__position, __position + difference_type(__n), __x);      this->_M_finish += difference_type(__n);    }    else {      size_type __len = size() + (max)(size(), __n);      __chunk_type* __q = this->_M_bit_alloc(__len);      iterator __i = _STLP_STD::copy(begin(), __position, iterator(__q, 0));      fill_n(__i, __n, __x);      this->_M_finish = _STLP_STD::copy(__position, end(), __i + difference_type(__n));      this->_M_deallocate();      this->_M_end_of_storage._M_data = __q + _Base::_M_bits_to_chunks(__len);      this->_M_start = iterator(__q, 0);    }  }  void insert(iterator __position, size_type __n, bool __x) {    _M_fill_insert(__position, __n, __x);  }  void pop_back() {    --this->_M_finish;  }  iterator erase(iterator __position) {    if (__position + 1 != end())      _STLP_STD::copy(__position + 1, end(), __position);      --this->_M_finish;    return __position;  }  iterator erase(iterator __first, iterator __last) {    this->_M_finish = _STLP_STD::copy(__last, end(), __first);    return __first;  }  void resize(size_type __new_size, bool __x = bool()) {    if (__new_size < size())      erase(begin() + difference_type(__new_size), end());    else      insert(end(), __new_size - size(), __x);  }  void flip() {    for (__chunk_type* __p = this->_M_start._M_p; __p != this->_M_end_of_storage._M_data; ++__p)      *__p = ~*__p;  }  void clear() { erase(begin(), end()); }};#if defined  (_STLP_NO_BOOL) || defined (__HP_aCC) // fixed soon (03/17/2000)#  define _STLP_TEMPLATE_HEADER __BVEC_TMPL_HEADER#  define _STLP_TEMPLATE_CONTAINER __BVECTOR_QUALIFIED#  include <stl/_relops_cont.h>#  undef _STLP_TEMPLATE_CONTAINER#  undef _STLP_TEMPLATE_HEADER#endif /* NO_BOOL */#if defined (_STLP_DEBUG) && !defined (_STLP_NO_BOOL)_STLP_MOVE_TO_STD_NAMESPACE#endif_STLP_END_NAMESPACE#undef vector#undef _Alloc#undef _STLP_VECBOOL_TEMPLATE#undef __BVECTOR#undef __BVECTOR_QUALIFIED#undef __BVEC_TMPL_HEADER#undef _STLP_WORD_BIT#endif /* _STLP_INTERNAL_BVECTOR_H */// Local Variables:// mode:C++// End:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?