C++ young 程序库——y_iterator.hpp 和 y_type_traits.hpp

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

文件位置:young/y_iterator.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_ITERATOR_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_ITERATOR_HEADER_FILE__

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

#include <iterator>

#include "y_define.hpp"

#include "y_pair.hpp"

#include "y_type_traits.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

/*

struct input_iterator_tag {};

struct output_iterator_tag {};

struct forward_iterator_tag : public input_iterator_tag,

public output_iterator_tag {};

struct bidirectional_iterator_tag : public forward_iterator_tag {};

struct random_access_iterator_tag : public bidirectional_iterator_tag {};

*/

typedef std::input_iterator_tag input_iterator_tag;

typedef std::output_iterator_tag output_iterator_tag;

typedef std::forward_iterator_tag forward_iterator_tag;

typedef std::bidirectional_iterator_tag bidirectional_iterator_tag;

typedef std::random_access_iterator_tag random_access_iterator_tag;

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

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

template< typename Iterator >

struct iterator_traits

{

typedef false_type is_pointer;

typedef typename Iterator::iterator_category iterator_category;

typedef typename Iterator::value_type value_type;

typedef typename Iterator::reference reference;

typedef typename Iterator::pointer pointer;

typedef typename Iterator::size_type size_type;

typedef typename Iterator::difference_type difference_type;

};

template< typename T >

struct iterator_traits<T*>

{

typedef true_type is_pointer;

typedef random_access_iterator_tag iterator_category;

typedef T value_type;

typedef value_type& reference;

typedef value_type* pointer;

typedef def_size_t size_type;

typedef def_ptrdiff_t difference_type;

};

template< typename T >

struct iterator_traits<const T*>

{

typedef true_type is_pointer;

typedef random_access_iterator_tag iterator_category;

typedef T value_type;

typedef value_type& reference;

typedef value_type* pointer;

typedef def_size_t size_type;

typedef def_ptrdiff_t difference_type;

};

template< typename Pointer >

inline Pointer const_iter_cast( const Pointer citer )

{

return const_cast<Pointer>( citer );

}

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

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

//必须是双向迭代器以上的迭代器

template< typename Iterator >

class Reverse_Iterator

{

public:

typedef typename iterator_traits<Iterator>::iterator_category iterator_category;

typedef typename iterator_traits<Iterator>::value_type value_type;

typedef typename iterator_traits<Iterator>::reference reference;

typedef typename iterator_traits<Iterator>::pointer pointer;

typedef typename iterator_traits<Iterator>::size_type size_type;

typedef typename iterator_traits<Iterator>::difference_type difference_type;

typedef const pointer const_pointer;

typedef const reference const_reference;

typedef Reverse_Iterator<Iterator> self;

typedef Iterator iterator_type;

Reverse_Iterator() {}

Reverse_Iterator( iterator_type it ) : current(it) {}

iterator_type base() const { return current; }

reference operator*() const

{

iterator_type result = current;

return *(--result);

}

pointer operator->() const

{

return &( operator*() );

}

self& operator++() { --current; return this; }

self& operator--() { ++current; return this; }

self operator++(int) { self temp = *this; --current; return temp; }

self operator--(int) { self temp = *this; ++current; return temp; }

self& operator+=( difference_type n ) { current -= n; return *this; }

self& operator-=( difference_type n ) { current += n; return *this; }

self operator+( difference_type n ) const { return self( current - n ); }

self operator-( difference_type n ) const { return self( current + n ); }

reference operator[]( difference_type n ) const

{ return *( *this + n ); }

protected:

iterator_type current;

}; //end class

template< typename Iterator >

inline typename Reverse_Iterator<Iterator>::difference_type

operator-( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return rhs.base() - lhs.base();

}

template< typename Iterator >

inline Reverse_Iterator<Iterator>

operator+( typename Reverse_Iterator<Iterator>::difference_type n,

const Reverse_Iterator<Iterator>& rhs )

{

return Reverse_Iterator<Iterator>( rhs.base() - n );

}

template< typename Iterator >

inline bool operator==( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return ( lhs.base() == rhs.base() );

}

template< typename Iterator >

inline bool operator!=( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return ( lhs.base() != rhs.base() );

}

template< typename Iterator >

inline bool operator<( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return ( rhs.base() < lhs.base() );

}

template< typename Iterator >

inline bool operator>( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return ( rhs < lhs );

}

template< typename Iterator >

inline bool operator<=( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return !( rhs < lhs );

}

template< typename Iterator >

inline bool operator>=( const Reverse_Iterator<Iterator>& lhs,

const Reverse_Iterator<Iterator>& rhs )

{

return !( lhs < rhs );

}

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

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

template< typename ForwardIterator >

inline typename iterator_traits<ForwardIterator>::difference_type

distance( ForwardIterator first, ForwardIterator last )

{

typedef typename iterator_traits<ForwardIterator>::iterator_category cate;

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

return distance_aux( first, last, cate() );

}

template< typename ForwardIterator >

typename iterator_traits<ForwardIterator>::difference_type

distance_aux( ForwardIterator first, ForwardIterator last,

input_iterator_tag )

{

typedef typename iterator_traits<ForwardIterator>::difference_type diff_t;

diff_t len = 0;

while( first != last )

{

++first;

++len;

}

return len;

}

template< typename ForwardIterator >

inline typename iterator_traits<ForwardIterator>::difference_type

distance_aux( ForwardIterator first, ForwardIterator last,

random_access_iterator_tag )

{

return ( last - first );

}

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

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

template< typename InputIterator, typename Integer >

inline void advance( InputIterator& iter, Integer n )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

advance_aux( iter, n, cate() );

}

template< typename InputIterator, typename Integer >

void advance_aux( InputIterator& iter, Integer n, input_iterator_tag )

{

while( n-- )

++iter;

}

template< typename InputIterator, typename Integer >

void advance_aux( InputIterator& iter, Integer n, bidirectional_iterator_tag )

{

if( n >= 0 )

{

while( n-- )

++iter;

}

else

{

while( n++ )

--iter;

}

}

template< typename InputIterator, typename Integer >

inline void advance_aux( InputIterator& iter, Integer n,

random_access_iterator_tag )

{

iter += n;

}

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

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

template< typename InputIterator, typename Integer >

inline

Integer range_length( InputIterator first, InputIterator last, Integer length )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

return range_len_aux( first, last, length, cate() );

}

template< typename InputIterator, typename Integer >

inline Integer range_len_aux( InputIterator first, InputIterator last,

Integer length, input_iterator_tag tag )

{

if( length < 1 )

length = distance_aux( first, last, tag );

return length;

}

template< typename InputIterator, typename Integer >

inline Integer range_len_aux( InputIterator first, InputIterator last,

Integer length, random_access_iterator_tag )

{

return ( last - first );

}

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

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

//返回值为最后一个有效的迭带器和区间长度

template< typename InputIterator >

inline

pair<InputIterator, typename iterator_traits<InputIterator>::difference_type>

pair_iter_len( InputIterator first, InputIterator last )

{

typedef typename iterator_traits<InputIterator>::iterator_category cate;

return iter_len_aux( first, last, cate() );

}

template< typename InputIterator >

pair<InputIterator, typename iterator_traits<InputIterator>::difference_type>

iter_len_aux( InputIterator first, InputIterator last,

input_iterator_tag )

{

typedef typename iterator_traits<InputIterator>::difference_type diff_t;

if( first == last )

return pair<InputIterator, diff_t>( first, 0 );

diff_t len = 1;

InputIterator result = first;

++first;

while( first != last )

{

++first;

++result;

++len;

}

return pair<InputIterator, diff_t>( result, len );

}

template< typename InputIterator >

pair<InputIterator, typename iterator_traits<InputIterator>::difference_type>

iter_len_aux( InputIterator first, InputIterator last,

random_access_iterator_tag )

{

typedef typename iterator_traits<InputIterator>::difference_type diff_t;

diff_t len = last - first;

if( len == 0 )

return pair<InputIterator, diff_t>( first, 0 );

else

return pair<InputIterator, diff_t>( first + (len - 1), len );

}

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

文件位置:young/y_type_traits.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_TYPE_TRAITS_HEADER_FILE__

#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_TYPE_TRAITS_HEADER_FILE__

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

#include "y_define.hpp"

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__

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

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

struct true_type {};

struct false_type {};

template< typename T >

struct type_traits

{

typedef false_type has_trivial_default_constructor;

typedef false_type has_trivial_copy_constructor;

typedef false_type has_trivial_assignment_operator;

typedef false_type has_trivial_destructor;

typedef false_type is_POD_type;

};

template< typename T >

struct type_traits<T*>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<void*>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<void>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<bool>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<char>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<signed char>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<unsigned char>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<short>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<unsigned short>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<int>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<unsigned int>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<long>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<unsigned long>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<float>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<double>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<long double>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

/*

template<>

struct type_traits<long long>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

template<>

struct type_traits<unsigned long long>

{

typedef true_type has_trivial_default_constructor;

typedef true_type has_trivial_copy_constructor;

typedef true_type has_trivial_assignment_operator;

typedef true_type has_trivial_destructor;

typedef true_type is_POD_type;

};

*/

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

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

template< typename T >

struct primal_type

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef const T contrary_const;

typedef const T* contrary_const_ptr;

typedef const T& contrary_const_ref;

};

template< typename T >

struct primal_type<const T>

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef T contrary_const;

typedef T* contrary_const_ptr;

typedef T& contrary_const_ref;

};

template< typename T >

struct primal_type<T*>

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef const T contrary_const;

typedef const T* contrary_const_ptr;

typedef const T& contrary_const_ref;

};

template< typename T >

struct primal_type<const T*>

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef T contrary_const;

typedef T* contrary_const_ptr;

typedef T& contrary_const_ref;

};

template< typename T >

struct primal_type<T&>

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef const T contrary_const;

typedef const T* contrary_const_ptr;

typedef const T& contrary_const_ref;

};

template< typename T >

struct primal_type<const T&>

{

typedef T primal_t;

typedef const T const_t;

typedef const T* const_ptr;

typedef const T& const_ref;

typedef T contrary_const;

typedef T* contrary_const_ptr;

typedef T& contrary_const_ref;

};

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

struct same_type {};

struct different_type {};

template< typename T1, typename T2 >

struct types_matching

{

typedef different_type result;

};

template< typename T >

struct types_matching<T, T>

{

typedef same_type result;

};

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

template< typename T1, typename T2 >

struct double_types_traits

{

typedef typename primal_type<T1>::primal_t primal_t1;

typedef typename primal_type<T2>::primal_t primal_t2;

typedef typename types_matching<primal_t1, primal_t2>::result

matching_result;

typedef typename type_traits<primal_t1>::has_trivial_default_constructor

has_trivial_default_constructor_t1;

typedef typename type_traits<primal_t1>::has_trivial_copy_constructor

has_trivial_copy_constructor_t1;

typedef typename type_traits<primal_t1>::has_trivial_assignment_operator

has_trivial_assignment_operator_t1;

typedef typename type_traits<primal_t1>::has_trivial_destructor

has_trivial_destructor_t1;

typedef typename type_traits<primal_t1>::is_POD_type

is_POD_type_t1;

typedef typename type_traits<primal_t2>::has_trivial_default_constructor

has_trivial_default_constructor_t2;

typedef typename type_traits<primal_t2>::has_trivial_copy_constructor

has_trivial_copy_constructor_t2;

typedef typename type_traits<primal_t2>::has_trivial_assignment_operator

has_trivial_assignment_operator_t2;

typedef typename type_traits<primal_t2>::has_trivial_destructor

has_trivial_destructor_t2;

typedef typename type_traits<primal_t2>::is_POD_type

is_POD_type_t2;

};

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

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

__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__

#endif

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航