美文网首页
3-1. 顺序容器-vector

3-1. 顺序容器-vector

作者: db24cc | 来源:发表于2022-02-23 16:50 被阅读0次

    概要

    vector是stl最常用的顺序容器, 使用简单, 动态扩展, 随机访问, 在stg-stl framework下, 代码简洁

    结构

    整体结构

    重要函数

    构造析构

    explicit vector(size_type __n)
        : _Base(__n, allocator_type())   // allocate n*sizeof(Tp)
        { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); // stl_uninitialized }
    
     vector(const vector<_Tp, _Alloc>& __x) 
        : _Base(__x.size(), __x.get_allocator())
        { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); }
    ~vector() { destroy(_M_start, _M_finish); } // stl_uninitialized 析构foreach call, ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); } 归还到内存池
    

    iter

      iterator begin() { return _M_start; }
      const_iterator begin() const { return _M_start; }
      iterator end() { return _M_finish; }
      const_iterator end() const { return _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) / sizeof(_Tp); }
      size_type capacity() const
        { return size_type(_M_end_of_storage - begin()); }
      bool empty() const
        { return begin() == end(); }
    
      reference operator[](size_type __n) { return *(begin() + __n); }
      const_reference operator[](size_type __n) const { return *(begin() + __n); }
    

    重要internal函数

      iterator _M_allocate_and_copy(size_type __n, const_iterator __first, 
                                                   const_iterator __last)
      {
        iterator __result = _M_allocate(__n); // allocate __n*sizeof(Tp)
        __STL_TRY {
          uninitialized_copy(__first, __last, __result); // stl_uninitialized
          return __result;
        }
        __STL_UNWIND(_M_deallocate(__result, __n));
      }
    
    template <class _Tp, class _Alloc>
    void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val) 
    {
      if (__n > capacity()) {
        vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator()); //reuse constructor, construct new one, old is not affected if allocate failed 
        __tmp.swap(*this);
      }
      else if (__n > size()) { //current capacity holds __n element
        fill(begin(), end(), __val); //stl_algo
        _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val); //stl_uninitialized cp rest
      }
      else
        erase(fill_n(begin(), __n, __val), end()); // brilliant & concise, fill front, erase rest
    }
    
    template <class _Tp, class _Alloc>
    void 
    vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
    {
      if (_M_finish != _M_end_of_storage) { //current capacity holds
        construct(_M_finish, *(_M_finish - 1)); //construct old finish
        ++_M_finish;
        _Tp __x_copy = __x;
        copy_backward(__position, _M_finish - 2, _M_finish - 1); // [p, old finish] ->[p + 1, old finish]
        *__position = __x_copy;
      }
      else {
        const size_type __old_size = size();
        const size_type __len = __old_size != 0 ? 2 * __old_size : 1; // double size
        iterator __new_start = _M_allocate(__len); 
        iterator __new_finish = __new_start;
        __STL_TRY {
          __new_finish = uninitialized_copy(_M_start, __position, __new_start); // cp front
          construct(__new_finish, __x); // construct @ p
          ++__new_finish;
          __new_finish = uninitialized_copy(__position, _M_finish, __new_finish); // cp rest
        }
        __STL_UNWIND((destroy(__new_start,__new_finish), 
                      _M_deallocate(__new_start,__len)));
        destroy(begin(), end());
        _M_deallocate(_M_start, _M_end_of_storage - _M_start);
        _M_start = __new_start;
        _M_finish = __new_finish;
        _M_end_of_storage = __new_start + __len;
      }
    }
    
    template <class _Tp, class _Alloc>
    void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n, 
                                             const _Tp& __x)
    {
      if (__n != 0) {
        if (size_type(_M_end_of_storage - _M_finish) >= __n) { //capacity holds __element see image explanation
          _Tp __x_copy = __x;
          const size_type __elems_after = _M_finish - __position;
          iterator __old_finish = _M_finish;
          if (__elems_after > __n) { 
            uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
            _M_finish += __n;
            copy_backward(__position, __old_finish - __n, __old_finish);
            fill(__position, __position + __n, __x_copy);
          }
          else { 
            uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
            _M_finish += __n - __elems_after;
            uninitialized_copy(__position, __old_finish, _M_finish);  
            _M_finish += __elems_after;
            fill(__position, __old_finish, __x_copy);
          }
        }
        else { //capacity can not n
          const size_type __old_size = size();        
          const size_type __len = __old_size + max(__old_size, __n);
          iterator __new_start = _M_allocate(__len);
          iterator __new_finish = __new_start;
          __STL_TRY {
            __new_finish = uninitialized_copy(_M_start, __position, __new_start);
            __new_finish = uninitialized_fill_n(__new_finish, __n, __x);
            __new_finish
              = uninitialized_copy(__position, _M_finish, __new_finish);
          }
          __STL_UNWIND((destroy(__new_start,__new_finish), 
                        _M_deallocate(__new_start,__len)));
          destroy(_M_start, _M_finish);
          _M_deallocate(_M_start, _M_end_of_storage - _M_start);
          _M_start = __new_start;
          _M_finish = __new_finish;
          _M_end_of_storage = __new_start + __len;
        }
      }
    }
    
    template <class _Tp, class _Alloc>
    vector<_Tp,_Alloc>& 
    vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x) //see image explanation
    {
      if (&__x != this) { 
        const size_type __xlen = __x.size();
        if (__xlen > capacity()) { 
          iterator __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
          destroy(_M_start, _M_finish);
          _M_deallocate(_M_start, _M_end_of_storage - _M_start);
          _M_start = __tmp;
          _M_end_of_storage = _M_start + __xlen;
        }
        else if (size() >= __xlen) {
          iterator __i = copy(__x.begin(), __x.end(), begin());
          destroy(__i, _M_finish);
        }
        else {
          copy(__x.begin(), __x.begin() + size(), _M_start);
          uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
        }
        _M_finish = _M_start + __xlen;
      }
      return *this;
    }
    template <class _Tp, class _Alloc>
    void 
    vector<_Tp, _Alloc>::insert(iterator __position, 
                                const_iterator __first, 
                                const_iterator __last)
    {
      if (__first != __last) {
        size_type __n = 0;
        distance(__first, __last, __n);
        if (size_type(_M_end_of_storage - _M_finish) >= __n) {
          const size_type __elems_after = _M_finish - __position;
          iterator __old_finish = _M_finish;
          if (__elems_after > __n) {
            uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
            _M_finish += __n;
            copy_backward(__position, __old_finish - __n, __old_finish);
            copy(__first, __last, __position);
          }
          else {
            uninitialized_copy(__first + __elems_after, __last, _M_finish);
            _M_finish += __n - __elems_after;
            uninitialized_copy(__position, __old_finish, _M_finish);
            _M_finish += __elems_after;
            copy(__first, __first + __elems_after, __position);
          }
        }
        else {
          const size_type __old_size = size();
          const size_type __len = __old_size + max(__old_size, __n);
          iterator __new_start = _M_allocate(__len);
          iterator __new_finish = __new_start;
          __STL_TRY {
            __new_finish = uninitialized_copy(_M_start, __position, __new_start);
            __new_finish = uninitialized_copy(__first, __last, __new_finish);
            __new_finish
              = uninitialized_copy(__position, _M_finish, __new_finish);
          }
          __STL_UNWIND((destroy(__new_start,__new_finish),
                        _M_deallocate(__new_start,__len)));
          destroy(_M_start, _M_finish);
          _M_deallocate(_M_start, _M_end_of_storage - _M_start);
          _M_start = __new_start;
          _M_finish = __new_finish;
          _M_end_of_storage = __new_start + __len;
        }
      }
    }
    

    _M_fill_insert图示



    vector<_Tp, _Alloc>::insert(iterator __position,
    const_iterator __first,
    const_iterator __last)图示



    总结

    在stl_alloc, stl_uninitialized, stl_iter, stl_algo的加持下, vector的代码比较简洁和容易理解, 复用关系也非常清晰

    相关文章

      网友评论

          本文标题:3-1. 顺序容器-vector

          本文链接:https://www.haomeiwen.com/subject/kschlrtx.html