C++ young 程序库——y_pointer.hpp、y_ptr_container.hpp、y_temp_buffer.hpp 和 y_memory.hpp

文件位置:young/y_pointer.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

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_POINTER_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_POINTER_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class smart_ptr
{
public:
    typedef  smart_ptr<T>  self;
    typedef  def_size_t    size_type;

    static const size_type unshareable = size_t_max;

private:
    struct refPtr
    {
        T* pdata;
        size_type ref;

        explicit refPtr( T* p ) : pdata(p), ref(1)  {}

        ~refPtr()  {  delete pdata;  }

    private:
        refPtr( const refPtr& rhs );
        refPtr& operator=( const refPtr& rhs );
    } *m_ptr;

public:
    explicit smart_ptr( T* p = NULL_POINTER ) : m_ptr( new refPtr(p) )  {}

    smart_ptr( const self& rhs )  {  create( rhs );  }

    smart_ptr& operator=( const self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( rhs );
            swap( temp );
        }
        return *this;
    }

    template< typename U >
    smart_ptr( const smart_ptr<U>& rhs )  {  create( rhs );  }

    template< typename U >
    smart_ptr& operator=( const smart_ptr<U>& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( rhs );
            swap( temp );
        }
        return *this;
    }

    ~smart_ptr()  {  release();  }

    bool operator==( const self& rhs ) const
        {  return ( m_ptr == rhs.m_ptr );  }
    bool operator!=( const self& rhs ) const
        {  return ( m_ptr != rhs.m_ptr );  }

    T& operator*()               {  clone(false);  return *(m_ptr->pdata);  }
    T* operator->()              {  clone(false);  return m_ptr->pdata;  }
    const T& operator*() const   {  return *(m_ptr->pdata);  }
    const T* operator->() const  {  return m_ptr->pdata;  }

    bool operator!() const  {  return ( m_ptr == NULL_POINTER );  }

    void reset( T* p = NULL_POINTER )
    {
        self temp( p );
        swap( temp );
    }

    void swap( self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            refPtr* temp = m_ptr;
            m_ptr = rhs.m_ptr;
            rhs.m_ptr = temp;
        }
    }

    void release()
    {
        if( m_ptr->ref == unshareable )
            delete m_ptr;
        else if( --(m_ptr->ref) < 1 )
            delete m_ptr;
        m_ptr = NULL_POINTER;
    }

protected:
    void clone( bool share = true )  //share为true表示可共享
    {
        if( (m_ptr->ref > 1) && (m_ptr->ref != unshareable) )
        {
            self temp( new T(*(m_ptr->pdata)) );
            swap( temp );
        }
        m_ptr->ref = share ? 1 : unshareable;
    }

    void create( const self& rhs )
    {
        if( rhs.m_ptr->ref != unshareable )
        {
            m_ptr = rhs.m_ptr;
            ++( m_ptr->ref );
        }
        else
            m_ptr = ( rhs.m_ptr->pdata )
                    ? new refPtr( new T(*(rhs.m_ptr->pdata)) ) : NULL_POINTER;
    }

    template< typename U >
    void create( const smart_ptr<U>& rhs )
    {
        if( rhs.m_ptr->ref != unshareable )
        {
            m_ptr = rhs.m_ptr;
            ++( m_ptr->ref );
        }
        else
            m_ptr = ( rhs.m_ptr->pdata )
                    ? new refPtr( new T(*(rhs.m_ptr->pdata)) ) : NULL_POINTER;
    }

};  //end smart_ptr

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
class auto_ptr
{
private:
    T* m_ptr;

public:
    typedef  auto_ptr<T>  self;

    explicit auto_ptr( T* p = NULL_POINTER ):m_ptr(p)  {}

    auto_ptr( const self& rhs )
    : m_ptr( (rhs.m_ptr) ? new T(*(rhs.m_ptr)) : NULL_POINTER )  {}

    auto_ptr& operator=( const self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( new T(*(rhs.m_ptr)) );
            swap( temp );
        }
        return *this;
    }

    template< typename U >
    auto_ptr( const auto_ptr<U>& rhs )
    : m_ptr( (rhs.m_ptr) ? new T(*(rhs.m_ptr)) : NULL_POINTER )  {}

    template< typename U >
    auto_ptr& operator=( const auto_ptr<U>& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            self temp( new T(*(rhs.m_ptr)) );
            swap( temp );
        }
        return *this;
    }

    ~auto_ptr()
    {
        if( m_ptr )
            delete m_ptr;
    }

    T& operator*()               {  return *m_ptr;  }
    T* operator->()              {  return m_ptr;  }
    const T& operator*() const   {  return *m_ptr;  }
    const T* operator->() const  {  return m_ptr;  }

    bool operator!() const  {  return ( m_ptr == NULL_POINTER );  }

    T* get() const  {  return m_ptr;  }

    void release()
    {
        if( m_ptr )
        {
            delete m_ptr;
            m_ptr = NULL_POINTER;
        }
    }

    void reset( T* p = NULL_POINTER )
    {
        if( m_ptr )
            delete m_ptr;
        m_ptr = p;
    }

    void swap( self& rhs )
    {
        if( m_ptr != rhs.m_ptr )
        {
            T* temp = m_ptr;
            m_ptr = rhs.m_ptr;
            rhs.m_ptr = temp;
        }
    }

    bool operator==( const self& rhs ) const
        {  return ( m_ptr == rhs.m_ptr );  }
    bool operator!=( const self& rhs ) const
        {  return ( m_ptr != rhs.m_ptr );  }

};  //end auto_ptr

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template<typename T>
inline void swap( smart_ptr<T>& lhs, smart_ptr<T>& rhs )
{
    lhs.swap( rhs );
}

template<typename T>
inline void swap( auto_ptr<T>& lhs, auto_ptr<T>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

文件位置:young/y_ptr_container.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

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_POINTER_CONTAINER_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_POINTER_CONTAINER_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_vector.hpp"
#include "y_functional.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

//指针容器的迭代器数值类型是T**
//指针容器的数值类型是T*

template< typename T, typename Container = vector<T*> >
class ptr_container
{
protected:
    Container con;

public:
    typedef  T  data_type;
    typedef  ptr_container<T, Container>  self;

    typedef  typename Container::allocator_type          allocator_type;
    typedef  typename Container::value_type              value_type;
    typedef  typename Container::reference               reference;
    typedef  typename Container::const_reference         const_reference;
    typedef  typename Container::pointer                 pointer;
    typedef  typename Container::const_pointer           cons_pointer;
    typedef  typename Container::iterator                iterator;
    typedef  typename Container::const_iterator          const_iterator;
    typedef  typename Container::reverse_iterator        reverse_iterator;
    typedef  typename Container::const_reverse_iterator  const_reverse_iterator;
    typedef  typename Container::size_type               size_type;
    typedef  typename Container::difference_type         difference_type;


    class value_compare : public binary_function<value_type, value_type, bool>
    {
    public:
        bool operator()( const value_type& lhs, const value_type& rhs ) const
        {
            return ( *lhs < *rhs );
        }
    };

    value_compare value_comp() const  {  return value_compare();  }


    ptr_container() : con()  {}

    explicit ptr_container( size_type size ) : con(size)  {}

    template< typename InputIterator >
    ptr_container( InputIterator first, InputIterator last ) : con()
    {
        range_init( first, last );
    }

    ptr_container( const self& rhs )
    {
        con.reserve( rhs.size() );
        range_init( rhs.begin(), rhs.end() );
    }

    self& operator=( const self& rhs )
    {
        if( this != &rhs )
            assign( rhs.begin(), rhs.end() );
        return *this;
    }

    ~ptr_container()
    {
        iterator first = begin();
        iterator last = end();
        for( ; first != last; ++first )
            delete *first;
    }

    iterator begin()              {  return con.begin();  }
    iterator end()                {  return con.end();  }
    const_iterator begin() const  {  return con.begin();  }
    const_iterator end() const    {  return con.end();  }

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

    reference front()              {  return *begin();  }
    reference back()               {  return *(--end());  }
    const_reference front() const  {  return *begin();  }
    const_reference back() const   {  return *(--end());  }

    bool empty() const          {  return con.empty();  }
    size_type size() const      {  return con.size();  }
    size_type space() const     {  return con.space();  }
    size_type capacity() const  {  return con.capacity();  }
    size_type max_size() const  {  return con.max_size();  }

    void reverse()          {  con.reverse();  }
    void swap( self& rhs )  {  con.swap( rhs );  }
    void reserve( size_type new_size )  {  con.reserve( new_size );  }
    void resize( size_type new_size, const_reference value = value_type() );

    reference operator[]( size_type index )
        {  return con[index];  }
    const_reference operator[]( size_type index ) const
        {  return con[index];  }

    reference at( size_type index )
        {  return con.at( index );  }
    const_reference at( size_type index ) const
        {  return con.at( index );  }

    void push_back( const data_type& value )
        {  con.push_back( new data_type(value) );  }
    void push_back( const_reference value )
        {  con.push_back( value );  }

    void push_front( const data_type& value )
        {  con.push_front( new data_type(value) );  }
    void push_front( const_reference value )
        {  con.push_front( value );  }

    void pop_back()
        {  delete *( --end() );  con.pop_back();  }
    void pop_front()
        {  delete *( begin() );  con.pop_front();  }

    void clear()
    {
        iterator first = begin();
        iterator last = end();
        for( ; first != last; ++first )
            delete *first;
        con.clear();
    }

    iterator erase( iterator position )
    {
        if( position != end() )
            delete *position;
        con.erase( position );
    }
    iterator erase( iterator first, iterator last )
    {
        iterator temp = first;
        for( ; temp != last; ++temp )
            delete *temp;
        con.erase( first, last );
    }


    void assign( size_type new_size )
    {
        iterator first = begin();
        iterator last = end();
        for( ; first != last; ++first )
            delete *first;
        con.assign( new_size, NULL_POINTER );
    }

    template< typename InputIterator >
    void assign( InputIterator first, InputIterator last );


    iterator insert( iterator position, const_reference value = value_type() )
        {  con.insert( position, value );  }

    template< typename InputIterator >
    void insert( iterator position, InputIterator first, InputIterator last );

protected:
    template< typename InputIterator >
    void range_init( InputIterator first, InputIterator last )
    {
        for( ; first != last; ++first )
            con.push_back( new data_type(*first) );
    }

};  //end ptr_container

//-----------------------------------------------------------------------------

template< typename T, typename Container >
void ptr_container<T, Container>::resize( size_type new_size,
                                          const_reference value )
{
    if( new_size < size() )
    {
        iterator first = begin() + new_size;
        iterator last = end();
        for( ; first != last; ++first )
            delete *first;
    }
    con.resize( new_size, value );
}
//-----------------------------------------------------------------------------

template< typename T, typename Container >
template< typename InputIterator >
void ptr_container<T, Container>::assign( InputIterator first,
                                          InputIterator last )
{
    iterator iter1 = begin();
    iterator iter2 = end();
    for( ; iter1 != iter2 && first != last; ++iter1,++first )
        **iter1 = *first;
    if( iter1 != iter2 )
        erase( iter1, iter2 );
    else
    {
        for( ; first != last; ++first )
            con.push_back( new data_type(*first) );
    }
}
//-----------------------------------------------------------------------------

template< typename T, typename Container >
template< typename InputIterator >
void ptr_container<T, Container>::insert( iterator position,
                                          InputIterator first,
                                          InputIterator last )
{
    typedef  typename iterator_traits<InputIterator>::difference_type  diff_t;

    diff_t len = distance( first, last );
    diff_t pos = position - begin();  //迭代器可能失效,需记住偏移位置
    con.insert( position, len, value_type() );
    position = begin() + pos;
    for( ; len > 0; ++first,++position,--len )
        *position = new data_type( *first );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container >
inline bool operator==( const ptr_container<T, Container>& lhs,
                        const ptr_container<T, Container>& rhs )
{
    return ( lhs.size() == rhs.size()
             && equal( lhs.begin(), lhs.end(),
                       rhs.begin(), lhs.value_comp() ) );
}

template< typename T, typename Container >
inline bool operator!=( const ptr_container<T, Container>& lhs,
                        const ptr_container<T, Container>& rhs )
{
    return !( lhs == rhs );
}

template< typename T, typename Container >
inline bool operator<( const ptr_container<T, Container>& lhs,
                       const ptr_container<T, Container>& rhs )
{
    if( lhs.begin() == rhs.begin() || lhs.size() > rhs.size() )
        return false;
    return lexicographical_compare( lhs.begin(), lhs.end(),
                                    rhs.begin(), rhs.end(), lhs.value_comp() );
}

template< typename T, typename Container >
inline bool operator>( const ptr_container<T, Container>& lhs,
                       const ptr_container<T, Container>& rhs )
{
    return ( rhs < lhs );
}

template< typename T, typename Container >
inline bool operator<=( const ptr_container<T, Container>& lhs,
                        const ptr_container<T, Container>& rhs )
{
    return !( rhs < lhs );
}

template< typename T, typename Container >
inline bool operator>=( const ptr_container<T, Container>& lhs,
                        const ptr_container<T, Container>& rhs )
{
    return !( lhs < rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

文件位置:young/y_temp_buffer.hpp

基本上是SGI STL的翻版

/*
The young Library
Copyright (c) 2005 by 杨桓

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_TEMPORARY_BUFFER_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_TEMPORARY_BUFFER_HEADER_FILE__
//-----------------------------------------------------------------------------
#include <cstdlib>
#include <limits>
#include "y_pair.hpp"
#include "y_type_traits.hpp"
#include "y_initialization.hpp"

//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T >
pair<T*, def_ptrdiff_t> get_temporary_buffer( def_ptrdiff_t len, T *p )
{
    def_ptrdiff_t max = std:: numeric_limits<def_ptrdiff_t>::max();
    if( len > def_ptrdiff_t(max / sizeof(T)) )
        len = max / sizeof(T);

    while( len > 0 )
    {
        T* temp = (T*)std::malloc( len * sizeof(T) );
        if( temp )
            return pair<T*, def_ptrdiff_t>( temp, len );
        len /= 2;
    }

    return pair<T*, def_ptrdiff_t>( (T*)NULL_POINTER, 0 );
}

template< typename T >
inline void return_temporary_buffer( T *p )
{
    if( p )
        std::free(p);
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename ForwardIterator, typename T >
class temporary_buffer
{
public:
    temporary_buffer( ForwardIterator first, ForwardIterator last );

    ~temporary_buffer()
    {
        destroy( buffer, buffer + len );
        std::free( buffer );
    }

    ptrdiff_t size() const            {  return len;  }
    ptrdiff_t requested_size() const  {  return original_len;  }

    T* begin()  {  return buffer;  }
    T* end()    {  return buffer + len;  }

private:
    def_ptrdiff_t  original_len;
    def_ptrdiff_t  len;
    T  *buffer;

    temporary_buffer( const temporary_buffer& );
    temporary_buffer& operator=( const temporary_buffer& );

    void init_buffer( const T&, true_type )  {}
    void init_buffer( const T& value, false_type )
    {  init_fill_n( buffer, len, value );  }

    void allocate_buffer();

};  //end class

template< typename ForwardIterator, typename T >
temporary_buffer<ForwardIterator, T>::
temporary_buffer( ForwardIterator first, ForwardIterator last )
: original_len(0)
{
    typedef  typename type_traits<T>::has_trivial_default_constructor  dc;

    len = distance( first, last );

    try
    {
        allocate_buffer();
        if( len > 0 )
            init_buffer( *first, dc() );
    }
    catch(...)
    {
        std::free( buffer );
        buffer = NULL_POINTER;
        len = 0;
    }
}

template< typename ForwardIterator, typename T >
void temporary_buffer<ForwardIterator, T>::allocate_buffer()
{
    original_len = len;
    buffer = NULL_POINTER;

    if( len > def_ptrdiff_t(INT_MAX / sizeof(T)) )
        len = INT_MAX / sizeof(T);

    while( len > 0 )
    {
        buffer = (T*)std::malloc( len * sizeof(T) );
        if( buffer )
            return;
        len /= 2;
    }
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


文件位置:young/y_memory.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

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_MEMORY_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_MEMORY_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_allocator.hpp"
#include "y_initialization.hpp"
#include "y_pointer.hpp"
#include "y_temp_buffer.hpp"
//-----------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值