分享
 
 
 

C++ young 程序库——y_algorithm_base.hpp、y_algorithm_compare.hpp、y_algorithm_copy.hpp、y_algorithm_fill.hpp、y_algorithm_lower

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

文件位置:young/algorithm/y_algorithm_base.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_ALGORITHM_BASE_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALGORITHM_BASE_HEADER_FILE__

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

#include "../y_define.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

template< typename T >

inline const T& min( const T& lhs, const T& rhs )

{

return ( lhs < rhs ? lhs : rhs );

}

template< typename T, typename StrictWeakOrdering >

inline const T& min( const T& lhs, const T& rhs, StrictWeakOrdering comp )

{

return ( comp(lhs, rhs) ? lhs : rhs );

}

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

template< typename T >

inline const T& max( const T& lhs, const T& rhs )

{

return ( rhs < lhs ? lhs : rhs );

}

template< typename T, typename StrictWeakOrdering >

inline const T& max( const T& lhs, const T& rhs, StrictWeakOrdering comp )

{

return ( comp(rhs, lhs) ? lhs : rhs );

}

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

template< typename T >

inline void swap( T& lhs, T& rhs )

{

data_swap( lhs, rhs );

}

template< typename T >

inline void data_swap( T& lhs, T& rhs )

{

T temp = lhs;

lhs = rhs;

rhs = temp;

}

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

文件位置:young/algorithm/y_algorithm_compare.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_ALGORITHM_COMPARE_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALGORITHM_COMPARE_HEADER_FILE__

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

#include "../y_iterator.hpp"

#include "../y_pair.hpp"

#include "../string/y_char_function.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

//*****************************************************************************

//*****************************************************************************

// 比较算法

//

// equal lexicographical_compare mismatch matching

//*****************************************************************************

//*****************************************************************************

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

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

template< typename InputIterator1, typename InputIterator2 >

inline bool equal( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2 )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate;

return equal_aux( first1, last1, first2, cate() );

}

template< typename InputIterator1, typename InputIterator2 >

bool equal_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, input_iterator_tag )

{

for( ; first1 != last1; ++first1,++first2 )

{

if( !(*first1 == *first2) )

return false;

}

return true;

}

template< typename InputIterator1, typename InputIterator2 >

bool equal_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

for( diff_t1 n = last1 - fitst1; n > 0; --n,++first1,++first2 )

{

if( !(*first1 == *first2) )

return false;

}

return true;

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

inline bool equal( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate bin_pred )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate;

return equal_aux( first1, last1, first2, bin_pred, cate() );

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

bool equal_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate bin_pred,

input_iterator_tag )

{

for( ; first1 != last1; ++first1,++first2 )

{

if( !bin_pred(*first1, *first2) )

return false;

}

return true;

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

bool equal_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, BinaryPredicate bin_pred,

random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

for( diff_t1 n = last1 - fitst1; n > 0; --n,++first1,++first2 )

{

if( !bin_pred(*first1, *first2) )

return false;

}

return true;

}

inline bool equal( const char* first1, const char* last1, const char* first2 )

{

return ( memcmp( first1, first2, last1 - first1 ) == 0 );

}

inline bool equal( const unsigned char* first1, const unsigned char* last1,

const unsigned char* first2 )

{

return ( memcmp( first1, first2, last1 - first1 ) == 0 );

}

inline bool equal( const wchar_t* first1, const wchar_t* last1,

const wchar_t* first2 )

{

return ( wmemcmp( first1, first2, last1 - first1 ) == 0 );

}

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

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

template< typename InputIterator1, typename InputIterator2 >

inline

bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate1;

typedef typename iterator_traits<InputIterator2>::iterator_category cate2;

return lexic_cmp( first1, last1, first2, last2, cate1(), cate2() );

}

template< typename InputIterator1, typename InputIterator2 >

bool lexic_cmp( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

input_iterator_tag, input_iterator_tag )

{

for( ; (first1 != last1) && (first2 != last2); ++first1,++first2 )

{

if( *first1 < *first2 )

return true;

if( *first2 < *first1 )

return false;

}

return ( first1 == last1 && first2 != last2 );

}

template< typename InputIterator1, typename InputIterator2 >

bool lexic_cmp( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

random_access_iterator_tag, random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

typedef typename iterator_traits<InputIterator2>::difference_type diff_t2;

diff_t1 len1 = last1 - first1;

diff_t2 len2 = last2 - first2;

def_ptrdiff_t len = len1 < len2 ? len1 : len2;

for( ; len > 0; --len,++first1,++first2 )

{

if( *first1 < *first2 )

return true;

if( *first2 < *first1 )

return false;

}

return ( len1 < len2 );

}

template< typename InputIterator1, typename InputIterator2,

typename StrictWeakOrdering >

inline

bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

StrictWeakOrdering comp )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate1;

typedef typename iterator_traits<InputIterator2>::iterator_category cate2;

return lexic_cmp( first1, las1, first2, last2, comp, cate1(), cate2() );

}

template< typename InputIterator1, typename InputIterator2,

typename StrictWeakOrdering >

bool lexic_cmp( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

StrictWeakOrdering comp,

input_iterator_tag, input_iterator_tag )

{

for( ; (first1 != last1) && (first2 != last2); ++first1,++first2 )

{

if( comp(*first1, *first2) )

return true;

if( comp(*first2, *first1) )

return false;

}

return ( first1 == last1 && first2 != last2 );

}

template< typename InputIterator1, typename InputIterator2,

typename StrictWeakOrdering >

bool lexic_cmp( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

StrictWeakOrdering comp,

random_access_iterator_tag, random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

typedef typename iterator_traits<InputIterator2>::difference_type diff_t2;

diff_t1 len1 = last1 - first1;

diff_t2 len2 = last2 - first2;

def_ptrdiff_t len = len1 < len2 ? len1 : len2;

for( ; len > 0; --len,++first1,++first2 )

{

if( comp(*first1, *first2) )

return true;

if( comp(*first2, *first1) )

return false;

}

return ( len1 < len2 );

}

inline bool lexicographical_compare( const char* first1, const char* last1,

const char* first2, const char* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

size_t len = len1 < len2 ? len1 : len2;

int result = memcmp( first1, first2, len );

return ( result != 0 ? result < 0 : len1 < len2 );

}

inline bool lexicographical_compare( const unsigned char* first1,

const unsigned char* last1,

const unsigned char* first2,

const unsigned char* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

size_t len = len1 < len2 ? len1 : len2;

int result = memcmp( first1, first2, len );

return ( result != 0 ? result < 0 : len1 < len2 );

}

inline bool lexicographical_compare( const wchar_t* first1,

const wchar_t* last1,

const wchar_t* first2,

const wchar_t* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

size_t len = len1 < len2 ? len1 : len2;

int result = wmemcmp( first1, first2, len );

return ( result != 0 ? result < 0 : len1 < len2 );

}

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

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

template< typename InputIterator1, typename InputIterator2 >

pair<InputIterator1, InputIterator2> mismatch( InputIterator1 first1,

InputIterator1 last1,

InputIterator2 first2 )

{

while( first1 != last1 && *first1 == *first2 )

{

++first1;

++first2;

}

return pair<InputIterator1, InputIterator2>( first1, first2 );

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

pair<InputIterator1, InputIterator2> mismatch( InputIterator1 first1,

InputIterator1 last1,

InputIterator2 first2,

BinaryPredicate bin_pred )

{

while( first1 != last1 && bin_pred(*first1, *first2) )

{

++first1;

++first2;

}

return pair<InputIterator1, InputIterator2>( first1, first2 );

}

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

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

template< typename InputIterator1, typename InputIterator2 >

inline bool matching( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2 )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate1;

typedef typename iterator_traits<InputIterator2>::iterator_category cate2;

return matching_aux( first1, last1, first2, last2, cate1(), cate2() );

}

template< typename InputIterator1, typename InputIterator2 >

bool matching_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

input_iterator_tag, input_iterator_tag )

{

for( ; (first1 != last1) && (first2 != last2); ++first1,++first2 )

{

if( !(*first1 == *first2) )

return false;

}

return ( first1 == last1 && first2 == last2 );

}

template< typename InputIterator1, typename InputIterator2 >

bool matching_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

random_access_iterator_tag, random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

typedef typename iterator_traits<InputIterator2>::difference_type diff_t2;

diff_t1 len1 = last1 - first1;

diff_t2 len2 = last2 - first2;

if( len1 != len2 )

return false;

for( ; len1 > 0; --len1,++first1,++first2 )

{

if( !(*first1 == *first2) )

return false;

}

return true;

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

inline bool matching( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

BinaryPredicate bin_pred )

{

typedef typename iterator_traits<InputIterator1>::iterator_category cate1;

typedef typename iterator_traits<InputIterator2>::iterator_category cate2;

return matching_aux( first1, last1, first2, last2, bin_pred,

cate1(), cate2() );

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

bool matching_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

BinaryPredicate bin_pred,

input_iterator_tag, input_iterator_tag )

{

for( ; (first1 != last1) && (first2 != last2); ++first1,++first2 )

{

if( !bin_pred(*first1, *first2) )

return false;

}

return ( first1 == last1 && first2 == last2 );

}

template< typename InputIterator1, typename InputIterator2,

typename BinaryPredicate >

bool matching_aux( InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, InputIterator2 last2,

BinaryPredicate bin_pred,

random_access_iterator_tag, random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator1>::difference_type diff_t1;

typedef typename iterator_traits<InputIterator2>::difference_type diff_t2;

diff_t1 len1 = last1 - first1;

diff_t2 len2 = last2 - first2;

if( len1 != len2 )

return false;

for( ; len1 > 0; --len1,++first1,++first2 )

{

if( !bin_pred(*first1, *first2) )

return false;

}

return true;

}

inline bool matching( const char* first1, const char* last1,

const char* first2, const char* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

if( len1 != len2 )

return false;

return ( memcmp(first1, first2, len1) == 0 );

}

inline bool matching( const unsigned char* first1, const unsigned char* last1,

const unsigned char* first2, const unsigned char* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

if( len1 != len2 )

return false;

return ( memcmp(first1, first2, len1) == 0 );

}

inline bool matching( const wchar_t* first1, const wchar_t* last1,

const wchar_t* first2, const wchar_t* last2 )

{

size_t len1 = last1 - first1;

size_t len2 = last2 - first2;

if( len1 != len2 )

return false;

return ( wmemcmp(first1, first2, len1) == 0 );

}

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

文件位置:young/algorithm/y_algorithm_copy.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_ALGORITHM_COPY_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALGORITHM_COPY_HEADER_FILE__

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

#include "../y_iterator.hpp"

#include "../y_type_traits.hpp"

#include "../string/y_char_function.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

//*****************************************************************************

//*****************************************************************************

// 复制算法

//

// copy copy_n copy_backward copy_backward_n copy_if copy_range

//*****************************************************************************

//*****************************************************************************

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

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

template< typename InputIterator, typename Integer, typename OutputIterator >

inline OutputIterator copy_n( InputIterator first, Integer count,

OutputIterator result )

{

return copy_n_aux( first, count, result );

}

template< typename InputIterator, typename Integer, typename OutputIterator >

OutputIterator copy_n_aux( InputIterator first, Integer count,

OutputIterator result )

{

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* 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_assignment_operator_t2 assign;

return ptr_copy_n( first, count, result, matching(), assign() );

}

template< typename T1, typename Integer, typename T2 >

inline T2* ptr_copy_n( T1* first, Integer count, T2* result,

same_type, true_type )

{

memmove( result, first, count * sizeof(T2) );

return ( result + count );

}

template< typename T1, typename Integer, typename T2 >

inline T2* ptr_copy_n( T1* first, Integer count, T2* result,

same_type, false_type )

{

return copy_n_aux( first, count, result );

}

template< typename T1, typename Integer, typename T2 >

inline T2* ptr_copy_n( T1* first, Integer count, T2* result,

different_type, true_type )

{

return copy_n_aux( first, count, result );

}

template< typename T1, typename Integer, typename T2 >

inline T2* ptr_copy_n( T1* first, Integer count, T2* result,

different_type, false_type )

{

return copy_n_aux( first, count, result );

}

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

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

template< typename InputIterator, typename OutputIterator >

inline OutputIterator copy( InputIterator first, InputIterator last,

OutputIterator result )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

if( first == last )

return result;

else

return copy_aux( first, last, result, cate() );

}

template< typename InputIterator, typename OutputIterator >

OutputIterator copy_aux( InputIterator first, InputIterator last,

OutputIterator result, input_iterator_tag )

{

for( ; first != last; ++first,++result )

*result = *first;

return result;

}

template< typename InputIterator, typename OutputIterator >

OutputIterator copy_aux( InputIterator first, InputIterator last,

OutputIterator result, random_access_iterator_tag )

{

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* 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_assignment_operator_t2 assign;

return ptr_copy_n( first, last - first, result, matching(), assign() );

}

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

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

template< typename BidirectionalIterator1, typename BidirectionalIterator2 >

inline

BidirectionalIterator2 copy_backward( BidirectionalIterator1 first,

BidirectionalIterator1 last,

BidirectionalIterator2 result_last )

{

typedef typename iterator_traits<BidirectionalIterator1>::iterator_category

cate;

if( first == last )

return result_last;

else

return copy_b_aux( first, last, result_last, cate() );

}

template< typename BidirectionalIterator1, typename BidirectionalIterator2 >

BidirectionalIterator2 copy_b_aux( BidirectionalIterator1 first,

BidirectionalIterator1 last,

BidirectionalIterator2 result_last,

bidirectional_iterator_tag )

{

while( first != last )

*(--result_last) = *(--last);

return result_last;

}

template< typename BidirectionalIterator1, typename BidirectionalIterator2 >

BidirectionalIterator2 copy_b_aux( BidirectionalIterator1 first,

BidirectionalIterator1 last,

BidirectionalIterator2 result_last,

random_access_iterator_tag )

{

typedef typename iterator_traits<BidirectionalIterator1>::difference_type

diff_t1;

for( diff_t1 n = last - first; n > 0; --n )

*(--result_last) = *(--last);

return result_last;

}

template< typename T1, typename T2 >

inline T2* copy_backward( T1* first, T1* last, T2* result_last )

{

typedef double_types_traits<T1, T2> double_traits;

typedef typename double_traits::matching_result matching;

typedef typename double_traits::has_trivial_assignment_operator_t2 assign;

return ptr_copy_n( first, last - first, result_last - (last - first),

matching(), assign() );

}

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

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

template< typename BidirectionalIterator1, typename Integer,

typename BidirectionalIterator2 >

BidirectionalIterator2 copy_backward_n( BidirectionalIterator1 last,

Integer count,

BidirectionalIterator2 result_last )

{

typedef typename primal_type<Integer>::primal_t primal_int;

for( primal_int i = 0; i < count; ++i )

*(--result_last) = *(--last);

return result_last;

}

template< typename T1, typename Integer, typename T2 >

inline T2* copy_backward_n( T1* last, Integer count, T2* result_last )

{

typedef double_types_traits<T1, T2> double_traits;

typedef typename double_traits::matching_result matching;

typedef typename double_traits::has_trivial_assignment_operator_t2 assign;

return ptr_copy_n( first, count, result_last - count,

matching(), assign() );

}

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

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

template< typename InputIterator, typename UnaryPredicate,

typename OutputIterator >

inline OutputIterator copy_if( InputIterator first, InputIterator last,

OutputIterator result, UnaryPredicate pred )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

if( first == last )

return result;

else

return copy_if_aux( first, last, result, cate() );

}

template< typename InputIterator, typename UnaryPredicate,

typename OutputIterator >

OutputIterator copy_if_aux( InputIterator first, InputIterator last,

OutputIterator result, UnaryPredicate pred,

input_iterator_tag )

{

for( ; first != last; ++first )

{

if( pred(*first) )

{

*result = *first;

++result;

}

}

return result;

}

template< typename InputIterator, typename UnaryPredicate,

typename OutputIterator >

OutputIterator copy_if_aux( InputIterator first, InputIterator last,

OutputIterator result, UnaryPredicate pred,

random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator>::difference_type diff_t;

for( diff_t n = last - first; n > 0; --n,++first )

{

if( pred(*first) )

{

*result = *first;

++result;

}

}

return result;

}

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

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

template< typename InputIterator, typename OutputIterator >

OutputIterator copy_range( InputIterator first,

InputIterator last,

OutputIterator result_first,

OutputIterator result_last )

{

while( first != last && result_first != result_last )

{

*result_first = *first;

++first;

++result_first;

}

return result_first;

}

template< typename T1, typename T2 >

inline T2* copy_range( T1* first, T1* last,

T2* result_first, T2* result_last )

{

typedef double_types_traits<T1, T2> double_traits;

typedef typename double_traits::matching_result matching;

typedef typename double_traits::has_trivial_assignment_operator_t2 assign;

ptrdiff_t len1 = last - first;

ptrdiff_t len2 = result_last - result_first;

ptrdiff_t len = len1 < len2 ? len1 : len2;

return ptr_copy_n( first, len, result_first, matching(), assign() );

}

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

文件位置:young/algorithm/y_algorithm_fill.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_ALGORITHM_FILL_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALGORITHM_FILL_HEADER_FILE__

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

#include "../y_iterator.hpp"

#include "../y_type_traits.hpp"

#include "../string/y_char_function.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

template< typename OutputIterator, typename T >

inline void fill( OutputIterator first, OutputIterator last, const T& value )

{

typedef typename iterator_traits<OutputIterator>::iterator_category cate;

fill_aux( first, last, value, cate() );

}

template< typename OutputIterator, typename T >

void fill_aux( OutputIterator first, OutputIterator last,

const T& value, forward_iterator_tag )

{

for( ; first != last; ++first )

*first = value;

}

template< typename OutputIterator, typename T >

void fill_aux( OutputIterator first, OutputIterator last,

const T& value, random_access_iterator_tag )

{

typedef typename iterator_traits<OutputIterator>::difference_type diff_t;

for( diff_t n = last - first; n > 0; --n,++first )

*first = value;

}

inline void fill( char* first, char* last, const char& value )

{

memset( first, value, (last - first) );

}

inline void fill( signed char* first, signed char* last,

const signed char& value )

{

memset( first, value, (last - first) );

}

inline void fill( unsigned char* first, unsigned char* last,

const unsigned char& value )

{

memset( first, value, (last - first) );

}

inline void fill( wchar_t* first, wchar_t* last, const wchar_t& value )

{

wmemset( first, value, (last - first) );

}

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

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

template< typename OutputIterator, typename Integer, typename T >

OutputIterator fill_n( OutputIterator result, Integer count, const T& value )

{

typedef typename primal_type<Integer>::primal_t primal_int;

for( primal_int i = 0; i < count; ++i,++result )

*result = value;

return result;

}

inline char* fill_n( char* result, def_size_t n, const char& value )

{

memset( result, value, n );

return ( result + n );

}

inline wchar_t* fill_n( wchar_t* result, def_size_t n, const wchar_t& value )

{

wmemset( result, value, n );

return ( result + n );

}

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

文件位置:young/algorithm/y_algorithm_lower_bound.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_ALGORITHM_LOWER_BOUND_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ALGORITHM_LOWER_BOUND_HEADER_FILE__

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

#include "../y_iterator.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

template< typename ForwardIterator, typename T >

inline

ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last,

const T& value )

{

typedef typename iterator_traits<ForwardIterator>::iterator_category cate;

if( first == last )

return first;

else

return lower_b_aux( first, last, value, cate() );

}

template< typename ForwardIterator, typename T >

ForwardIterator lower_b_aux( ForwardIterator first, ForwardIterator last,

const T& value, forward_iterator_tag )

{

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

diff_t len = distance( first, last );

diff_t half;

ForwardIterator middle;

while( len > 0 )

{

half = len / 2;

middle = first;

advance( middle, half );

if( *middle < value ) //要查找的值在后半部分

{

first = middle;

++first;

len = len - half - 1;

}

else //要查找的值在前半部分

len = half;

}

return first;

}

template< typename ForwardIterator, typename T >

ForwardIterator lower_b_aux( ForwardIterator first, ForwardIterator last,

const T& value, random_access_iterator_tag )

{

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

diff_t len = last - first;

diff_t half;

ForwardIterator middle;

while( len > 0 )

{

half = len / 2;

middle = first + half;

if( *middle < value ) //要查找的值在后半部分

{

first = middle + 1;

len = len - half - 1;

}

else //要查找的值在前半部分

len = half;

}

return first;

}

template< typename ForwardIterator, typename T, typename StrictWeakOrdering >

inline

ForwardIterator lower_bound( ForwardIterator first, ForwardIterator last,

const T& value, StrictWeakOrdering comp )

{

typedef typename iterator_traits<ForwardIterator>::iterator_category cate;

if( first == last )

return first;

else

return lower_b_aux( first, last, value, comp, cate() );

}

template< typename ForwardIterator, typename T, typename StrictWeakOrdering >

ForwardIterator lower_b_aux( ForwardIterator first, ForwardIterator last,

const T& value, StrictWeakOrdering comp,

forward_iterator_tag )

{

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

diff_t len = distance( first, last );

diff_t half;

ForwardIterator middle;

while( len > 0 )

{

half = len / 2;

middle = first;

advance( middle, half );

if( comp(*middle, value) ) //要查找的值在后半部分

{

first = middle;

++first;

len = len - half - 1;

}

else //要查找的值在前半部分

len = half;

}

return first;

}

template< typename ForwardIterator, typename T, typename StrictWeakOrdering >

ForwardIterator lower_b_aux( ForwardIterator first, ForwardIterator last,

const T& value, StrictWeakOrdering comp,

random_access_iterator_tag )

{

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

diff_t len = last - first;

diff_t half;

ForwardIterator middle;

while( len > 0 )

{

half = len / 2;

middle = first + half;

if( comp(*middle, value) ) //要查找的值在后半部分

{

first = middle + 1;

len = len - half - 1;

}

else //要查找的值在前半部分

len = half;

}

return first;

}

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

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

__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- 王朝網路 版權所有