vcl_vector.h

来自「DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.」· C头文件 代码 · 共 497 行 · 第 1/2 页

H
497
字号

    void pop_back() {
        __stl_verbose_assert(!empty(), __STL_MSG_EMPTY_CONTAINER);
        --finish;
        vcl_destroy(finish);
    }
    void erase(iterator position) {
        __stl_debug_check(__check_if_owner(this,position));
        __stl_verbose_assert(__ptr(position)!=finish,__STL_MSG_ERASE_PAST_THE_END);
        if (__ptr(position) + 1 != end_())
            vcl_copy(__ptr(position) + 1, end_(), __ptr(position));
        __stl_debug_do(invalidate(__ptr(position),finish));
        --finish;
        vcl_destroy(finish);
    }
    void erase(iterator first, iterator last) {
        __stl_debug_check(__check_if_owner(this,first)
                          &&__check_range(first,last));
        pointer i = vcl_copy(__ptr(last), end_(), __ptr(first));
        vcl_destroy(i, finish);
        __stl_debug_do(invalidate(__ptr(first),finish));
        finish = finish - (__ptr(last) - __ptr(first));
    }
    void resize(size_type new_size, const T& x) {
        if (new_size < size())
            erase(begin() + new_size, end());
        else
            insert(end(), new_size - size(), x);
    }
    void resize(size_type new_size) { resize(new_size, T()); }
    void clear() { erase(begin(), end()); }
};

template <class T, class Alloc>
vcl_vector<T, Alloc>& vcl_vector<T, Alloc>::operator=(const vcl_vector<T, Alloc>& x) {
    if (&x == this) return *this;
    __stl_debug_do(invalidate_all());
    if (x.size() > capacity()) {
        vcl_destroy(start, finish);
        deallocate();
        start = finish = 0;
        start = finish = data_allocator::allocate(x.size());
        end_of_storage = start + (x.size());
        vcl_uninitialized_copy(x.begin_(), x.end_(), start);
    } else if (size() >= x.size()) {
        pointer i = vcl_copy(x.begin_(), x.end_(), begin_());
        vcl_destroy(i, finish);
    } else {
        vcl_copy(x.begin_(), x.begin_() + size(), begin_());
        vcl_uninitialized_copy(x.begin_() + size(), x.end_(), begin_() + size());
    }
    finish = begin_() + x.size();
    return *this;
}

template <class T, class Alloc>
void vcl_vector<T, Alloc>::insert_aux(__pointer__ position, const T& x) {
    if (finish != end_of_storage) {
        vcl_construct(finish, *(finish - 1));
        ++finish;
        T x_copy = x;
        vcl_copy_backward(position, finish - 2, finish-1);
        *position = x_copy;
    } else {
        const size_type old_size = size();
        const size_type len = old_size != 0 ? 2 * old_size : 1;
        pointer tmp = data_allocator::allocate(len);
        pointer tmp_end = tmp;
        IUEg__TRY {
            tmp_end = vcl_uninitialized_copy(begin_(), position, tmp);
            vcl_construct(tmp_end, x);
            ++tmp_end;
            tmp_end = vcl_uninitialized_copy(position, end_(), tmp_end);
            vcl_destroy(begin_(), end_());
            deallocate();
            end_of_storage = tmp + len;
            finish = tmp_end;
            start = tmp;
        }
# if defined (__STL_USE_EXCEPTIONS)
        catch(...) {
            vcl_destroy(tmp, tmp_end);
            data_allocator::deallocate(tmp, len);
            throw;
        }
# endif
    }
}

template <class T, class Alloc>
void vcl_vector<T, Alloc>::insert(__iterator__ position, __size_type__ n, const T& x) {
    if (n == 0) return;
    if (end_of_storage - finish >= (difference_type)n) {
        pointer old_end = end_();
        size_type distance_to_end = end_() - position;
        if (distance_to_end > n) {
            vcl_uninitialized_copy(end_() - n, end_(), end_());
            finish += n;
            vcl_copy_backward(position, old_end - n, old_end);
            vcl_fill(position, position + n, x);
        } else {
            vcl_uninitialized_fill_n(end_(), n - distance_to_end, x);
            finish += n - distance_to_end;
            vcl_uninitialized_copy(position, old_end, end());
            finish += distance_to_end;
            vcl_fill(position, old_end, x);
        }
        __stl_debug_do(invalidate(position,old_end));
    } else {
        const size_type old_size = size();
        const size_type len = old_size + vcl_max(old_size, n);
        pointer tmp = data_allocator::allocate(len);
        pointer tmp_end = tmp;
        IUEg__TRY {
            tmp_end = vcl_uninitialized_copy(begin_(), position, tmp);
            tmp_end = vcl_uninitialized_fill_n(tmp_end, n, x);
            tmp_end = vcl_uninitialized_copy(position, end_(), tmp_end);
            vcl_destroy(begin_(), end_());
            deallocate();
            end_of_storage = tmp + len;
            finish = tmp_end;
            start = tmp;
        }
# if defined (__STL_USE_EXCEPTIONS)
        catch(...) {
            vcl_destroy(tmp, tmp_end);
            data_allocator::deallocate(tmp, len);
            throw;
        }
# endif
    }
}

template <class T, class Alloc>
void vcl_vector<T, Alloc>::insert(__iterator__ position, __const_iterator__ first,
                                  __const_iterator__ last) {
    if (first == last) return;
    size_type n = 0;
    vcl_distance(first, last, n);
    if (end_of_storage - finish >= (difference_type)n) {
        pointer old_end = end_();
        size_type distance_to_end = end_() - position;
        if (end_() - position > (difference_type)n) {
            vcl_uninitialized_copy(end_() - n, end_(), end_());
            finish += n;
            vcl_copy_backward(position, old_end - n, old_end);
            vcl_copy(first, last, position);
        } else {
            vcl_uninitialized_copy(first + distance_to_end, last, end_());
            finish += n - distance_to_end;
            vcl_uninitialized_copy(position, old_end, end_());
            finish += distance_to_end;
            vcl_copy(first, first + distance_to_end, position);
        }
        __stl_debug_do(invalidate(position,old_end));
    } else {
        const size_type old_size = size();
        const size_type len = old_size + vcl_max(old_size, n);
        pointer tmp = data_allocator::allocate(len);
        pointer tmp_end = tmp;
        IUEg__TRY {
            tmp_end = vcl_uninitialized_copy(begin_(), position, tmp);
            tmp_end = vcl_uninitialized_copy(first, last, tmp_end);
            tmp_end = vcl_uninitialized_copy(position, end_(), tmp_end);
            vcl_destroy(begin_(), end_());
            deallocate();
            end_of_storage = tmp + len;
            finish = tmp_end;
            start = tmp;
        }
# if defined (__STL_USE_EXCEPTIONS)
        catch(...) {
            vcl_destroy(tmp, tmp_end);
            data_allocator::deallocate(tmp, len);
            throw;
        }
# endif
    }
}

// do a cleanup
# undef  vcl_vector
# undef  __iterator__
# undef  __const_iterator__
# undef  __pointer__
# undef  __const_pointer__
# undef  __size_type__
# undef  __ptr
# undef  __difference_type__

// provide a uniform way to access full functionality
#  define __vector__ __FULL_NAME(vcl_vector)
__END_STL_FULL_NAMESPACE

# if !defined(VECTOR_H) && !defined(__STL_DEFAULT_TYPE_PARAM) && ( !defined(__STL_NAMESPACES) || defined(__STL_NO_NAMESPACES))
// provide a "default" vcl_vector adaptor
template <class T>
class vcl_vector : public __vector__<T,vcl_alloc>
{
    typedef vcl_vector<T> self;
public:
    typedef __vector__<T,vcl_alloc> super;
    __CONTAINER_SUPER_TYPEDEFS
    __IMPORT_SUPER_COPY_ASSIGNMENT(vcl_vector)
    vcl_vector() {}
    explicit vcl_vector(size_type n, const T& value) : super(n, value) { }
    explicit vcl_vector(size_type n) : super(n) { }
    vcl_vector(const_iterator first, const_iterator last) : super(first,last) { }
    ~vcl_vector() {}
};

#  if defined (__STL_BASE_MATCH_BUG)
template <class T>
    inline bool operator==(const vcl_vector<T>& x, const vcl_vector<T>& y) {
    typedef  __vector__<T,vcl_alloc> super;
    return operator == ((const super&)x,(const super&)y);
}

template <class T>
    inline bool operator<(const vcl_vector<T>& x, const vcl_vector<T>& y) {
    typedef  __vector__<T,vcl_alloc> super;
    return operator < ((const super&)x,(const super&)y);
}
#  endif /* __STL_BASE_MATCH_BUG */
# endif /* __STL_DEFAULT_TYPE_PARAM */

template <class T, class Alloc>
    inline bool operator==(const __vector__<T, Alloc>& x, const __vector__<T, Alloc>& y) {
    return x.size() == y.size() && vcl_equal(x.begin_(), x.end_(), y.begin_());
}

template <class T, class Alloc>
    inline bool operator!=(const __vector__<T, Alloc>& x, const __vector__<T, Alloc>& y) {
    return !(x == y);
}

template <class T, class Alloc>
    inline bool operator<(const __vector__<T, Alloc>& x, const __vector__<T, Alloc>& y) {
    return lexicographical_compare(x.begin_(), x.end_(), y.begin_(), y.end_());
}

# if defined (__STL_CLASS_PARTIAL_SPECIALIZATION )
template <class T, class Alloc>
    inline void vcl_swap(__vector__<T,Alloc>& a, __vector__<T,Alloc>& b) { a.swap(b); }
# endif

#endif // vcl_emulation_vector_h

⌨️ 快捷键说明

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