文件位置: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__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T, typename Container = vector<T*> >
class ptr_container
{
private:
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) {}
ptr_container( size_type size, const_reference value ) : con(size, value) {}
ptr_container( int size, const_reference value ) : con(size, value) {}
ptr_container( long size, const_reference value ) : con(size, value) {}
template< typename InputIterator >
ptr_container( InputIterator first, InputIterator last ) : con()
{
//判断迭代器的值类型和指针容器的值类型是否相同
typedef typename iterator_traits<InputIterator>::value_type value_t;
typedef typename double_types_traits<T, value_t>::matching_result matching;
range_init( first, last, matching() );
}
ptr_container( const self& rhs ) : con(rhs.begin(), rhs.end()) {}
self& operator=( const self& rhs )
{
if( this != &rhs )
con = rhs.con;
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, const_reference value = value_type() )
{
iterator first = begin();
iterator last = end();
for( ; first != last; ++first )
delete *first;
con.assign( new_size, value );
}
void assign( int new_size, const_reference value = value_type() )
{ assign( static_cast<size_type>( new_size ), value ); }
void assign( long new_size, const_reference value = value_type() )
{ assign( static_cast<size_type>( new_size ), value ); }
template< typename InputIterator >
void assign( InputIterator first, InputIterator last )
{
typedef typename iterator_traits<InputIterator>::value_type value_t;
typedef typename double_types_traits<T, value_t>::compare_result cmp;
range_assign( first, last, cmp() );
}
void insert( iterator position, size_type count, const_reference value )
{ con.insert( position, count, value ); }
void insert( iterator position, int count, const_reference value )
{ insert( position, static_cast<size_type>( count ), value ); }
void insert( iterator position, long count, const_reference value )
{ insert( position, static_cast<size_type>( count ), value ); }
iterator insert( iterator position, const_reference value = value_type() )
{ con.insert( position, value ); }
template< typename InputIterator >
void insert( iterator position, InputIterator first, InputIterator last )
{
typedef typename iterator_traits<InputIterator>::value_type value_t;
typedef typename double_types_traits<T, value_t>::compare_result cmp;
range_insert( position, first, last, cmp() );
}
protected:
template< typename InputIterator >
void range_init( InputIterator first, InputIterator last, different_type )
{
con.assign( first, last );
}
template< typename InputIterator >
void range_init( InputIterator first, InputIterator last, same_type )
{
for( ; first != last; ++first )
con.push_back( new data_type(*first) );
}
template< typename InputIterator >
void range_assign( InputIterator first, InputIterator last, different_type );
template< typename InputIterator >
void range_assign( InputIterator first, InputIterator last, same_type );
template< typename InputIterator >
void range_insert( iterator position, InputIterator first,
InputIterator last, different_type )
{
con.insert( position, first, last );
}
template< typename InputIterator >
void range_insert( iterator position, InputIterator first,
InputIterator last, same_type );
}; //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>::range_assign( InputIterator first,
InputIterator last,
different_type )
{
iterator iter1 = begin();
iterator iter2 = end();
InputIterator temp = first;
for( ; iter1 != iter2 && temp != last; ++iter1,++temp )
delete *iter1;
con.assign( first, last );
}
//-----------------------------------------------------------------------------
template< typename T, typename Container >
template< typename InputIterator >
void ptr_container<T, Container>::range_assign( InputIterator first,
InputIterator last,
same_type )
{
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>::range_insert( iterator position,
InputIterator first,
InputIterator last,
same_type )
{
typedef typename iterator_traits<InputIterator>::difference_type diff_t;
diff_t len = distance( first, last );
insert( position, len, value_type() );
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 "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 )
{
if( len > def_ptrdiff_t(int_max / sizeof(T)) )
len = int_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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------