⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ycpp_vector.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/*
 * The young Library
 * Copyright (c) 2005 by Yang Huan(杨桓)

 * Permission to use, copy, modify, distribute and sell this software 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.
 * The author make no representations about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 */

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_VECTOR_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_VECTOR_HEADER_FILE__
//------------------------------------------------------------------------------
#include <memory>
#include <iterator>
#include <stdexcept>
#include <algorithm>
#include "../youngc/yc_dymemarr.h"
#include "../youngc/yc_dyrscarr.h"
#include "ycpp_memory.hpp"
//------------------------------------------------------------------------------
namespace youngcpp {
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename T, typename Allocator = std::allocator<T> >
class memvector
{
public:
    typedef  memvector<T, Allocator>                self;
    typedef  Allocator                              allocator_type;
    typedef  size_t                                 size_type;
    typedef  ptrdiff_t                              difference_type;
    typedef  T                                      value_type;
    typedef  value_type&                            reference;
    typedef  const value_type&                      const_reference;
    typedef  value_type*                            pointer;
    typedef  const value_type*                      const_pointer;
    typedef  value_type*                            iterator;
    typedef  const value_type*                      const_iterator;
    typedef  std::reverse_iterator<iterator>        reverse_iterator;
    typedef  std::reverse_iterator<const_iterator>  const_reverse_iterator;

    memvector()
    {
        dymemarr_init( &m_arr, sizeof(T), alloc_adapter<Allocator>,
                       dealloc_adapter<Allocator> );
    }

    explicit memvector( size_type n, const T& value = T() )
        {  init_fill( n, value );  }
    explicit memvector( int n, const T& value = T() )
        {  init_fill( static_cast<size_type>(n), value );  }
    explicit memvector( long n, const T& value = T() )
        {  init_fill( static_cast<size_type>(n), value );  }

    template< typename InputIterator >
    memvector( InputIterator first, InputIterator last )
    {
        dymemarr_init( &m_arr, sizeof(T), alloc_adapter<Allocator>,
                       dealloc_adapter<Allocator> );
        insert( end(), first, last );
    }

    memvector( const self& src )
    {
        if( dymemarr_init_copy( &m_arr, &(src.m_arr) ) == 0 )
            throw std::bad_alloc();
    }

    self& operator=( const self& rhs )
    {
        if( dymemarr_assign_copy( &m_arr, &(rhs.m_arr) ) == 0 )
            throw std::bad_alloc();
        return *this;
    }

    ~memvector()  {  dymemarr_destroy( &m_arr );  }

    static void init_move( void* uninit_dst, void* src )
    {
        dymemarr_init_move( &( static_cast<self*>(uninit_dst)->m_arr ),
                            &( static_cast<self*>(src)->m_arr ) );
    }

    static void assign_move( void* dst, void* src )
    {
        dymemarr_assign_move( &( static_cast<self*>(dst)->m_arr ),
                              &( static_cast<self*>(src)->m_arr ) );
    }

    iterator begin()              {  return (T*)dymemarr_begin(&m_arr);  }
    iterator end()                {  return (T*)dymemarr_end(&m_arr);  }
    const_iterator begin() const  {  return (T*)dymemarr_begin(&m_arr);  }
    const_iterator end() const    {  return (T*)dymemarr_end(&m_arr);  }

    reverse_iterator rbegin()
        {  return reverse_iterator( end() );  }
    reverse_iterator rend()
        {  return reverse_iterator( begin() );  }
    const_reverse_iterator rbegin() const
        {  return const_reverse_iterator( end() );  }
    const_reverse_iterator rend() const
        {  return const_reverse_iterator( begin() );  }

    reference front()
        {  return *((pointer)dymemarr_front(&m_arr));  }
    reference back()
        {  return *((pointer)dymemarr_back(&m_arr));  }
    const_reference front() const
        {  return *((const_pointer)dymemarr_front(&m_arr));  }
    const_reference back() const
        {  return *((const_pointer)dymemarr_back(&m_arr));  }

    void clear()                {  dymemarr_erase_range( &m_arr, 0, size() );  }
    bool empty() const          {  return dymemarr_empty(&m_arr);  }
    size_type max_size() const  {  return dymemarr_max_size(&m_arr);  }

#ifdef __GNUC__
    size_type size() const      {  return dymemarr_size(&m_arr);  }
    size_type space() const     {  return dymemarr_space(&m_arr);  }
    size_type capacity() const  {  return dymemarr_capacity(&m_arr);  }
#else
    size_type size() const      {  return DYMEMARR_SIZE(m_arr, T);  }
    size_type space() const     {  return DYMEMARR_SPACE(m_arr, T);  }
    size_type capacity() const  {  return DYMEMARR_CAPACITY(m_arr, T);  }
#endif

    reference operator[]( size_type index )
        {  return DYMEMARR_INDEX(m_arr, index, T);  }
    const_reference operator[]( size_type index ) const
        {  return DYMEMARR_INDEX(m_arr, index, T);  }

    reference at( size_type index )
    {
        if( index >= size() )
            throw std::out_of_range( "young::memvector::at out of range!" );
        return DYMEMARR_INDEX(m_arr, index, T);
    }
    const_reference at( size_type index ) const
    {
        if( index >= size() )
            throw std::out_of_range( "young::memvector::at out of range!" );
        return DYMEMARR_INDEX(m_arr, index, T);
    }

    void swap( self& rhs )
    {
        if( &rhs != this )
            std::swap( m_arr, rhs.m_arr );
    }

    void pop_back()
    {
        dymemarr_pop_back( &m_arr );
    }

    void push_back( const T& value )
    {
        if( space() > 0 )
        {
            *end() = value;
            m_arr.m_finish = static_cast<T*>(m_arr.m_finish) + 1;
        }
        else if( !dymemarr_insert_value(&m_arr, size(), &value, 1) )
            throw std::bad_alloc();
    }

    void reserve( size_type new_capa )
    {
        if( !dymemarr_reserve(&m_arr, new_capa) )
            throw std::bad_alloc();
    }

    void resize( size_type new_size, const T& value = T() )
    {
        if( !dymemarr_resize(&m_arr, new_size, &value) )
            throw std::bad_alloc();
    }

    iterator erase( iterator pos )
    {
        size_t i = pos - begin();
        dymemarr_erase_pos( &m_arr, i );
        return begin() + i;
    }

    iterator erase( iterator first, iterator last )
    {
        size_t f = first - begin();
        dymemarr_erase_range( &m_arr, f, last - begin() );
        return begin() + f;
    }

    void insert( iterator pos, int count, const T& value )
        {  insert( pos, static_cast<size_type>( count ), value );  }
    void insert( iterator pos, long count, const T& value )
        {  insert( pos, static_cast<size_type>( count ), value );  }
    void insert( iterator pos, size_type count, const T& value )
    {
        size_t i = pos - begin();
        if( !dymemarr_insert_value(&m_arr, pos - begin(), NULL, count) )
            throw std::bad_alloc();
        std::fill( begin() + i, begin() + i + count, value );
    }

    iterator insert( iterator pos, const T& value = T() )
    {
        size_t i = pos - begin();
        if( !dymemarr_insert_value(&m_arr, i, &value, 1) )
            throw std::bad_alloc();
        return begin() + i;
    }

    template< typename InputIterator >
    void insert( iterator pos, InputIterator first, InputIterator last )
    {
        size_t i = pos - begin();
        if( !dymemarr_insert_array(&m_arr, i, NULL, std::distance(first, last)) )
            throw std::bad_alloc();
        std::copy( first, last, begin() + i );
    }

    void assign( int new_size, const T& value = T() )
        {  assign( static_cast<size_type>(new_size), value );  }
    void assign( long new_size, const T& value = T() )
        {  assign( static_cast<size_type>(new_size), value );  }
    void assign( size_type new_size, const T& value = T() )
    {
        if( !dymemarr_replace_fill(&m_arr, 0, SIZE_MAX, NULL, new_size) )
            throw std::bad_alloc();
        std::fill( begin(), end(), value );
    }

    template< typename InputIterator >
    void assign( InputIterator first, InputIterator last )
    {
        if( !dymemarr_replace_array( &m_arr, 0, SIZE_MAX, NULL,
                                     std::distance(first, last) ) )
            throw std::bad_alloc();
        std::copy( first, last, begin() );
    }

private:
    mutable youngc::dymemarray  m_arr;

    void init_fill( size_type n, const T& value )
    {
        dymemarr_init( &m_arr, sizeof(T), alloc_adapter<Allocator>,
                       dealloc_adapter<Allocator> );
        if( !dymemarr_resize(&m_arr, static_cast<size_t>(n), &value) )
            throw std::bad_alloc();
    }
};  //end class memvector



template< typename T, typename Allocator = std::allocator<T> >
class rscvector
{
public:
    typedef  rscvector<T, Allocator>                self;
    typedef  Allocator                              allocator_type;
    typedef  size_t                                 size_type;
    typedef  ptrdiff_t                              difference_type;
    typedef  T                                      value_type;
    typedef  value_type&                            reference;
    typedef  const value_type&                      const_reference;
    typedef  value_type*                            pointer;
    typedef  const value_type*                      const_pointer;
    typedef  value_type*                            iterator;
    typedef  const value_type*                      const_iterator;
    typedef  std::reverse_iterator<iterator>        reverse_iterator;
    typedef  std::reverse_iterator<const_iterator>  const_reverse_iterator;

    rscvector()  {  init();  }

    explicit rscvector( size_type n, const T& value = T() )
        {  init_fill( n, value );  }
    explicit rscvector( int n, const T& value = T() )
        {  init_fill( static_cast<size_type>(n), value );  }
    explicit rscvector( long n, const T& value = T() )
        {  init_fill( static_cast<size_type>(n), value );  }

    template< typename InputIterator >
    rscvector( InputIterator first, InputIterator last )
    {
        init();
        insert( end(), first, last );
    }

    rscvector( const self& src )
    {
        if( dyrscarr_init_copy( &m_arr, &(src.m_arr) ) == 0 )
            throw std::bad_alloc();
    }

    self& operator=( const self& rhs )
    {
        if( dyrscarr_assign_copy( &m_arr, &(rhs.m_arr) ) == 0 )
            throw std::bad_alloc();
        return *this;
    }

    ~rscvector()  {  dyrscarr_destroy( &m_arr );  }

    static void init_move( void* uninit_dst, void* src )
    {
        dyrscarr_init_move( &( static_cast<self*>(uninit_dst)->m_arr ),
                            &( static_cast<self*>(src)->m_arr ) );
    }

    static void assign_move( void* dst, void* src )
    {
        dyrscarr_assign_move( &( static_cast<self*>(dst)->m_arr ),
                              &( static_cast<self*>(src)->m_arr ) );
    }

    iterator begin()              {  return (T*)dyrscarr_begin(&m_arr);  }
    iterator end()                {  return (T*)dyrscarr_end(&m_arr);  }
    const_iterator begin() const  {  return (T*)dyrscarr_begin(&m_arr);  }
    const_iterator end() const    {  return (T*)dyrscarr_end(&m_arr);  }

⌨️ 快捷键说明

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