分享
 
 
 

C++ young 程序库——y_pair.hpp 和 y_initialization.hpp

王朝c/c++·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文件位置: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

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有