文件位置:young/y_pair.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_PAIR_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_PAIR_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_define.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename T1, typename T2 >
class pair
{
public:
typedef T1 first_type;
typedef T2 second_type;
typedef pair<T1, T2> self;
T1 first;
T2 second;
pair() : first(T1()), second(T2()) {}
pair( const T1& x, const T2& y ) : first(x), second(y) {}
template< typename U1, typename U2 >
pair( const U1& x, const U2& y ) : first(x), second(y) {}
template< typename U1, typename U2 >
pair( const pair<U1, U2>& rhs ) : first(rhs.first), second(rhs.second) {}
self& operator=( const self& rhs )
{
if( this != &rhs )
{
first = rhs.first;
second = rhs.second;
}
return *this;
}
template< typename U1, typename U2 >
self& operator=( const pair<U1, U2>& rhs )
{
if( this != &rhs )
{
first = rhs.first;
second = rhs.second;
}
return *this;
}
}; //end pair
template< typename T1, typename T2 >
inline pair<T1, T2> make_pair( const T1& x, const T2& y )
{
return pair<T1, T2>( x, y );
}
template< typename T1, typename T2 >
inline bool operator==( const pair<T1, T2>& lhs, const pair<T1, T2>& rhs )
{
return ( lhs.first == rhs.first && lhs.second == rhs.second );
}
template< typename T1, typename T2 >
inline bool operator!=( const pair<T1, T2>& lhs, const pair<T1, T2>& rhs )
{
return ( lhs.first != rhs.first || lhs.second != rhs.second );
}
template< typename T1, typename T2 >
inline bool operator<( const pair<T1, T2>& lhs, const pair<T1, T2>& rhs )
{
return ( lhs.first < rhs.first
|| ( !(rhs.first < lhs.first) && (lhs.second < rhs.second) ) );
}
template< typename T1, typename T2 >
inline bool operator>( const pair<T1,T2>& lhs, const pair<T1, T2>& rhs )
{
return ( rhs < lhs );
}
template< typename T1, typename T2 >
inline bool operator<=( const pair<T1, T2>& lhs, const pair<T1, T2>& rhs )
{
return !( rhs < lhs);
}
template< typename T1, typename T2 >
inline bool operator>=( const pair<T1, T2>& lhs, const pair<T1, T2>& rhs )
{
return !( lhs < rhs );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
文件位置:young/y_initialization.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_INITIALIZATION_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_INITIALIZATION_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_construct.hpp"
#include "string/y_char_function.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename InputIterator, typename Integer, typename ForwardIterator >
inline ForwardIterator init_copy_n( InputIterator first,
Integer count,
ForwardIterator result )
{
typedef typename iterator_traits<ForwardIterator>::value_type value_t;
typedef typename type_traits<value_t>::is_POD_type POD;
if( count > 0 )
return init_copy_n_aux( first, count, result, POD() );
else
return result;
}
template< typename InputIterator, typename Integer, typename ForwardIterator >
ForwardIterator init_copy_n_aux( InputIterator first, Integer count,
ForwardIterator result, false_type )
{
typedef typename primal_type<Integer>::primal_t primal_int;
ForwardIterator current = result;
try
{
for( primal_int i = 0; i < count; ++i,++first,++current )
construct( &(*current), *first );
}
catch(...)
{
destroy( result, current );
throw;
}
return current;
}
template< typename InputIterator, typename Integer, typename ForwardIterator >
ForwardIterator init_copy_n_aux( InputIterator first, Integer count,
ForwardIterator result, true_type )
{
typedef typename primal_type<Integer>::primal_t primal_int;
for( primal_int i = 0; i < count; ++i,++first,++result )
*result = *first;
return result;
}
template< typename T1, typename Integer, typename T2 >
inline T2* init_copy_n( T1* first, Integer count, T2* result )
{
typedef double_types_traits<T1, T2> double_traits;
typedef typename double_traits::matching_result matching;
typedef typename double_traits::has_trivial_copy_constructor_t2 cc_t2;
return ptr_init_copy_n( first, count, result, matching(), cc_t2() );
}
template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
same_type, true_type )
{
memcpy( result, first, count * sizeof(T2) );
return ( result + count );
}
template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
same_type, false_type x )
{
return init_copy_n_aux( first, count, result, x );
}
template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
different_type, true_type x )
{
return init_copy_n_aux( first, count, result, x );
}
template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
different_type, false_type x )
{
return init_copy_n_aux( first, count, result, x );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename InputIterator, typename ForwardIterator >
inline ForwardIterator init_copy( InputIterator first, InputIterator last,
ForwardIterator result )
{
typedef typename iterator_traits<InputIterator>::iterator_category itr;
typedef typename iterator_traits<ForwardIterator>::value_type value_t;
typedef typename type_traits<value_t>::is_POD_type POD;
if( first != last )
return init_copy_aux( first, last, result, itr(), POD() );
else
return result;
}
template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
ForwardIterator result,
input_iterator_tag, false_type )
{
ForwardIterator current = result;
try
{
for( ; first != last; ++first,++current )
construct( &(*current), *first );
}
catch(...)
{
destroy( result, current );
throw;
}
return current;
}
template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
ForwardIterator result,
random_access_iterator_tag, false_type )
{
typedef typename iterator_traits<InputIterator>::difference_type diff_t;
ForwardIterator current = result;
try
{
for( diff_t n = last - first; n > 0; --n,++first,++result )
construct( &(*current), *first );
}
catch(...)
{
destroy( result, current );
throw;
}
return current;
}
template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
ForwardIterator result,
input_iterator_tag, true_type )
{
for( ; first != last; ++first,++result )
*result = *first;
return result;
}
template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
ForwardIterator result,
random_access_iterator_tag, true_type )
{
typedef typename iterator_traits<InputIterator>::difference_type diff_t;
for( diff_t n = last - first; n > 0; --n,++first,++result )
*result = *first;
return result;
}
template< typename T1, typename T2 >
inline T2* init_copy( T1* first, T1* last, T2* result )
{
typedef double_types_traits<T1, T2> double_traits;
typedef typename double_traits::matching_result matching;
typedef typename double_traits::has_trivial_copy_constructor_t2 cc_t2;
return ptr_init_copy_n( first, last - first, result, matching(), cc_t2() );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename ForwardIterator, typename T >
inline void init_fill( ForwardIterator first, ForwardIterator last,
const T& value )
{
typedef typename iterator_traits<ForwardIterator>::iterator_category itr;
typedef typename iterator_traits<ForwardIterator>::value_type value_t;
typedef typename type_traits<value_t>::is_POD_type POD;
if( first != last )
init_fill_aux( first, last, value, itr(), POD() );
}
template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
const T& value, forward_iterator_tag,
false_type )
{
ForwardIterator current = first;
try
{
for( ; current != last; ++current )
construct( &(*current), value );
}
catch(...)
{
destroy( first, current );
throw;
}
}
template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
const T& value, random_access_iterator_tag,
false_type )
{
typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;
ForwardIterator current = first;
try
{
for( diff_t n = last - first; n > 0; --n,++current )
construct( &(*current), value );
}
catch(...)
{
destroy( first, current );
throw;
}
}
template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
const T& value, forward_iterator_tag,
true_type )
{
for( ; first != last; ++first )
*first = value;
}
template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
const T& value, random_access_iterator_tag,
true_type )
{
typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;
for( diff_t n = last - first; n > 0; --n,++first )
*first = value;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename ForwardIterator, typename Integer, typename T >
inline ForwardIterator init_fill_n( ForwardIterator result, Integer count,
const T& value )
{
typedef typename iterator_traits<ForwardIterator>::value_type value_t;
typedef typename type_traits<value_t>::is_POD_type POD;
if( count > 0 )
return init_fill_n_aux( result, count, value, POD() );
else
return result;
}
template< typename ForwardIterator, typename Integer, typename T >
ForwardIterator init_fill_n_aux( ForwardIterator result, Integer count,
const T& value, false_type )
{
typedef typename primal_type<Integer>::primal_t primal_int;
ForwardIterator current = result;
try
{
for( primal_int i = 0; i < count; ++i,++current )
construct( &(*current), value );
}
catch(...)
{
destroy( result, current );
throw;
}
return current;
}
template< typename ForwardIterator, typename Integer, typename T >
ForwardIterator init_fill_n_aux( ForwardIterator result, Integer count,
const T& value, true_type )
{
typedef typename primal_type<Integer>::primal_t primal_int;
for( primal_int i = 0; i < count; ++i,++result )
*result = value;
return result;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename InputIterator1, typename InputIterator2,
typename ForwardIterator >
inline ForwardIterator init_copy_copy( InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
ForwardIterator result )
{
ForwardIterator middle = init_copy( first1, last1, result );
try
{
return init_copy( first2, last2, middle );
}
catch(...)
{
destroy( result, middle );
throw;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename InputIterator, typename ForwardIterator, typename T >
inline void init_copy_fill( InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2,
const T& value )
{
ForwardIterator middle2 = init_copy( first1, last1, first2 );
try
{
init_fill( middle2, last2, value );
}
catch(...)
{
destroy( first2, middle2 );
throw;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename ForwardIterator, typename T, typename InputIterator >
inline ForwardIterator init_fill_copy( ForwardIterator result_first,
ForwardIterator result_middle,
const T& value,
InputIterator first,
InputIterator last )
{
init_fill( result_first, result_middle, value );
try
{
return init_copy( first, last, result_middle );
}
catch(...)
{
destroy( result_first, result_middle );
throw;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
inline char* init_copy( char* first, char* last, char* result )
{
return static_cast<char*>( memcpy(result, first, last - first) );
}
inline char* init_copy_n( char* first, size_t count, char* result )
{
return static_cast<char*>( memcpy(result, first, count) );
}
inline void init_fill( char* first, char* last, char& value )
{
memset( first, value, last - first );
}
inline char* init_fill_n( char* result, size_t count, char& value )
{
return static_cast<char*>( memset(result, value, count) );
}
inline char* init_copy_copy( char* first1, char* last1,
char* first2, char* last2,
char* result )
{
result = init_copy( first1, last1, result );
return init_copy( first2, last2, result );
}
inline void init_copy_fill( char* first1, char* last1,
char* first2, char* last2,
char& value )
{
char* middle = init_copy( first1, last1, first2 );
init_fill( middle, last2, value );
}
inline char* init_fill_copy( char* result_first, char* result_middle,
char& value, char* first, char* last )
{
init_fill( result_first, result_middle, value );
return init_copy( first, last, result_middle );
}
inline wchar_t* init_copy( wchar_t* first, wchar_t* last, wchar_t* result )
{
ptrdiff_t len = last - first;
memcpy( result, first, len * sizeof(wchar_t) );
return ( result + len );
}
inline wchar_t* init_copy_n( wchar_t* first, size_t count, wchar_t* result )
{
memcpy( result, first, count * sizeof(wchar_t) );
return ( result + count );
}
inline void init_fill( wchar_t* first, wchar_t* last, wchar_t& value )
{
wmemset( first, value, last - first );
}
inline wchar_t* init_fill_n( wchar_t* result, size_t count, wchar_t& value )
{
return wmemset( result, value, count );
}
inline wchar_t* init_copy_copy( wchar_t* first1, wchar_t* last1,
wchar_t* first2, wchar_t* last2,
wchar_t* result )
{
result = init_copy( first1, last1, result );
return init_copy( first2, last2, result );
}
inline void init_copy_fill( wchar_t* first1, wchar_t* last1,
wchar_t* first2, wchar_t* last2,
wchar_t& value )
{
wchar_t* middle = init_copy( first1, last1, first2 );
init_fill( middle, last2, value );
}
inline wchar_t* init_fill_copy( wchar_t* result_first, wchar_t* result_middle,
wchar_t& value, wchar_t* first, wchar_t* last )
{
init_fill( result_first, result_middle, value );
return init_copy( first, last, result_middle );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename InputIterator, typename ForwardIterator >
inline ForwardIterator uninitialized_copy( InputIterator first,
InputIterator last,
ForwardIterator result )
{
return init_copy( first, last, result );
}
template< typename InputIterator, typename Integer, typename ForwardIterator >
inline ForwardIterator uninitialized_copy_n( InputIterator first,
Integer count,
ForwardIterator result )
{
return init_copy_n( first, count, result );
}
template< typename ForwardIterator, typename T >
inline void uninitialized_fill( ForwardIterator first, ForwardIterator last,
const T& value )
{
init_fill( first, last, value );
}
template< typename ForwardIterator, typename Integer, typename T >
inline ForwardIterator uninitialized_fill_n( ForwardIterator result,
Integer count,
const T& value )
{
return fill_n( result, count, value );
}
template< typename InputIterator1, typename InputIterator2,
typename ForwardIterator >
inline ForwardIterator uninitialized_copy_copy( InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2,
ForwardIterator result )
{
return init_copy_copy( first1, last1, first2, last2, result );
}
template< typename InputIterator, typename ForwardIterator, typename T >
inline void uninitialized_copy_fill( InputIterator first1,
InputIterator last1,
ForwardIterator first2,
ForwardIterator last2,
const T& value )
{
init_copy_fill( first1, last1, first2, last2, value );
}
template< typename ForwardIterator, typename T, typename InputIterator >
inline ForwardIterator uninitialized_fill_copy( ForwardIterator result_first,
ForwardIterator result_middle,
const T& value,
InputIterator first,
InputIterator last )
{
return init_fill_copy( result_first, result_middle, value, first, last );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------