vcl_vector.h

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

H
497
字号
/*
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Hewlett-Packard Company makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 *
 * Copyright (c) 1996
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * Exception Handling:
 * Copyright (c) 1997
 * Mark of the Unicorn, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Mark of the Unicorn makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * Adaptation:
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Moscow Center for SPARC Technology makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 */

#ifndef vcl_emulation_vector_h
#define vcl_emulation_vector_h

#include <vcl_new.h>
#include <vcl_cstddef.h>
#include "vcl_algobase.h"
#include "vcl_alloc.h"

template <class T, class Alloc>
class __vector_base
{
    typedef __vector_base<T,Alloc> self;
public:
    typedef T value_type;
    typedef vcl_size_t size_type;
    typedef T* pointer;
    typedef const T* const_pointer;
protected:
    typedef vcl_simple_alloc<value_type, Alloc> data_allocator;
    pointer start;
    pointer finish;
    pointer end_of_storage;

    void deallocate() {
        if (start) {
            __stl_debug_do(invalidate_all());
            data_allocator::deallocate(start, end_of_storage - start);
        }
    }
public:
    __vector_base() : start(0), finish(0), end_of_storage(0) {
        __stl_debug_do(safe_init(this));
    }

    __vector_base(size_type n) :
        start( data_allocator::allocate(n) ),
        finish(start),
        end_of_storage(start + n) {
        __stl_debug_do(safe_init(this));
    }

    ~__vector_base() {
        vcl_destroy(start, finish);
        deallocate();
        __stl_debug_do(invalidate());
    }
};

# if defined ( __STL_NESTED_TYPE_PARAM_BUG )
#  define __pointer__             T*
#  define __const_pointer__       const T*
#  define __size_type__           vcl_size_t
#  define __difference_type__     vcl_ptrdiff_t
# else
#  define __pointer__         pointer
#  define __const_pointer__   const_pointer
#  define __size_type__       size_type
#  define __difference_type__ difference_type
# endif

# define __iterator__       __pointer__
# define __const_iterator__ __const_pointer__

__BEGIN_STL_FULL_NAMESPACE
#  define vcl_vector __WORKAROUND_RENAME(vcl_vector)

template <class T, VCL_DFL_TYPE_PARAM_STLDECL(Alloc,vcl_alloc)>
class vcl_vector : protected __vector_base<T, Alloc> {
    typedef __vector_base<T,Alloc> super;
    typedef vcl_vector<T,Alloc> self;
public:
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef vcl_ptrdiff_t difference_type;
    typedef vcl_size_t size_type;
    typedef __iterator__ iterator;
    typedef __const_iterator__ const_iterator;
    typedef vcl_reverse_iterator<const_iterator, value_type, const_reference,
                                 difference_type>  const_reverse_iterator;
    typedef vcl_reverse_iterator<iterator, value_type, reference,
                                 difference_type> reverse_iterator;
protected:
    inline void insert_aux(pointer position, const T& x);
public:
    pointer begin_() { return start; }
    const_pointer begin_() const { return start; }
    pointer end_() { return finish; }
    const_pointer end_() const { return finish; }
#   define __ptr(x) x
    iterator begin() { return start; }
    const_iterator begin() const { return start; }
    iterator end() { return finish; }
    const_iterator end() const { return 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(T); }
    size_type capacity() const { return size_type(end_of_storage - start); }
    bool empty() const { return begin_() == end_(); }
    reference operator[](size_type n) {
        __stl_verbose_assert(n<size(), __STL_MSG_OUT_OF_BOUNDS);
        return *(start+n);
    }
    const_reference operator[](size_type n) const {
        __stl_verbose_assert(n<size(), __STL_MSG_OUT_OF_BOUNDS);
        return *(start+n);
    }
    vcl_vector() {}
    vcl_vector(size_type n, const T& value) : super(n) {
        finish = vcl_uninitialized_fill_n(start, n, value);
    }
    explicit vcl_vector(size_type n) : super(n) {
        finish = __default_initialize_n(start, n);
    }
    vcl_vector(const self& x) : super(x.end_() - x.begin_()) {
        finish = vcl_uninitialized_copy(x.begin_(), x.end_(), start);
    }
    vcl_vector(const_iterator first, const_iterator last) {
        __stl_debug_check(__check_range(first,last));
        size_type n = 0;
        vcl_distance(__ptr(first), __ptr(last), n);
        start = finish = data_allocator::allocate(n);
        end_of_storage = start+n;
        finish = vcl_uninitialized_copy(__ptr(first), __ptr(last), start);
    }
    ~vcl_vector() {}
    inline self& operator=(const self& x);
    void reserve(size_type n) {
        const size_type old_size(size());
        if (capacity() < n) {
            pointer tmp = data_allocator::allocate(n);
            IUEg__TRY {
                vcl_uninitialized_copy(begin_(), end_(), tmp);
                vcl_destroy(start, finish);
                deallocate();
                finish = tmp + old_size;
                start = tmp;
                end_of_storage = begin_() + n;
            }
# if defined (__STL_USE_EXCEPTIONS)
            catch(...) {
                data_allocator::deallocate(tmp, n);
                throw;
            }
# endif
        }
    }
    reference front() {
        __stl_verbose_assert(!empty(), __STL_MSG_EMPTY_CONTAINER);
        return *begin_();
    }
    const_reference front() const {
        __stl_verbose_assert(!empty(), __STL_MSG_EMPTY_CONTAINER);
        return *begin_();
    }
    reference back() {
        __stl_verbose_assert(!empty(), __STL_MSG_EMPTY_CONTAINER);
        return *(end_() - 1);
    }
    const_reference back() const {
        __stl_verbose_assert(!empty(), __STL_MSG_EMPTY_CONTAINER);
        return *(end_() - 1);
    }
    void push_back(const T& x) {
        if (finish != end_of_storage) {
            vcl_construct(finish, x);
            ++finish;
        } else
            insert_aux(end_(), x);
    }
    void swap(self& x) {
        __stl_debug_do(swap_owners(x));
        vcl_swap(start, x.start);
        vcl_swap(finish, x.finish);
        vcl_swap(end_of_storage, x.end_of_storage);
    }
    iterator insert(iterator position, const T& x) {
        __stl_debug_check(__check_if_owner(this,position));
        size_type n = __ptr(position) - begin_();
        if (finish != end_of_storage && __ptr(position) == end_()) {
            vcl_construct(finish, x);
            ++finish;
        } else
            insert_aux(__ptr(position), x);
        return begin()+n;
    }
    iterator insert(iterator position) { return insert(position, T()); }
    inline void insert (iterator position, size_type n, const T& x);
    inline void insert (iterator position, const_iterator first, const_iterator last);

⌨️ 快捷键说明

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