文件位置: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
#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
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
struct iterator_traits
{
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
struct iterator_traits
{
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
inline T* const_iter_cast( const T* cptr )
{
return const_cast
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//必须是双向迭代器以上的迭代器
template
class Reverse_Iterator
{
public:
typedef typename iterator_traits
typedef typename iterator_traits
typedef typename iterator_traits
typedef typename iterator_traits
typedef typename iterator_traits
typedef typename iterator_traits
typedef const pointer const_pointer;
typedef const reference const_reference;
typedef Reverse_Iterator
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
inline typename Reverse_Iterator
operator-( const Reverse_Iterator
const Reverse_Iterator
{
return rhs.base() - lhs.base();
}
template
inline Reverse_Iterator
operator+( typename Reverse_Iterator
const Reverse_Iterator
{
return Reverse_Iterator
}
template
inline bool operator==( const Reverse_Iterator
const Reverse_Iterator
{
return ( lhs.base() == rhs.base() );
}
template
inline bool operator!=( const Reverse_Iterator
const Reverse_Iterator
{
return ( lhs.base() != rhs.base() );
}
template
inline bool operator<( const Reverse_Iterator
const Reverse_Iterator
{
return ( rhs.base()
template
inline bool operator>( const Reverse_Iterator
const Reverse_Iterator
{
return ( rhs
template
inline bool operator<=( const Reverse_Iterator
const Reverse_Iterator
{
return !( rhs
template
inline bool operator>=( const Reverse_Iterator
const Reverse_Iterator
{
return !( lhs
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template
inline typename iterator_traits
distance( ForwardIterator first, ForwardIterator last )
{
typedef typename iterator_traits
typedef typename iterator_traits
return distance_aux( first, last, cate() );
}
template
typename iterator_traits
distance_aux( ForwardIterator first, ForwardIterator last,
input_iterator_tag )
{
typedef typename iterator_traits
diff_t len = 0;
while( first != last )
{
++first;
++len;
}
return len;
}
template
inline typename iterator_traits
distance_aux( ForwardIterator first, ForwardIterator last,
random_access_iterator_tag )
{
return ( last - first );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template
inline void advance( InputIterator& iter, Integer n )
{
typedef typename iterator_traits
advance_aux( iter, n, cate() );
}
template
void advance_aux( InputIterator& iter, Integer n, input_iterator_tag )
{
while( n-- )
++iter;
}
template
void advance_aux( InputIterator& iter, Integer n, bidirectional_iterator_tag )
{
if( n >= 0 )
{
while( n-- )
++iter;
}
else
{
while( n++ )
--iter;
}
}
template
inline void advance_aux( InputIterator& iter, Integer n,
random_access_iterator_tag )
{
iter += n;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template
inline
Integer range_length( InputIterator first, InputIterator last, Integer length )
{
typedef typename iterator_traits
return range_len_aux( first, last, length, cate() );
}
template
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
inline Integer range_len_aux( InputIterator first, InputIterator last,
Integer length, random_access_iterator_tag )
{
return ( last - first );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//返回值为最后一个有效的迭带器和区间长度
template
inline
pair
pair_iter_len( InputIterator first, InputIterator last )
{
typedef typename iterator_traits
return iter_len_aux( first, last, cate() );
}
template
pair
iter_len_aux( InputIterator first, InputIterator last,
input_iterator_tag )
{
typedef typename iterator_traits
if( first == last )
return pair
diff_t len = 1;
InputIterator result = first;
++first;
while( first != last )
{
++first;
++result;
++len;
}
return pair
}
template
pair
iter_len_aux( InputIterator first, InputIterator last,
random_access_iterator_tag )
{
typedef typename iterator_traits
diff_t len = last - first;
if( len == 0 )
return pair
else
return pair
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__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
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
struct type_traits
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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
{
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 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
struct primal_type
{
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
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
struct primal_type
{
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
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
struct primal_type
{
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
struct types_matching
{
typedef different_type result;
};
template
struct types_matching
{
typedef same_type result;
};
//-----------------------------------------------------------------------------
template
struct double_types_traits
{
typedef typename primal_type
typedef typename primal_type
typedef typename types_matching
matching_result;
typedef typename type_traits
has_trivial_default_constructor_t1;
typedef typename type_traits
has_trivial_copy_constructor_t1;
typedef typename type_traits
has_trivial_assignment_operator_t1;
typedef typename type_traits
has_trivial_destructor_t1;
typedef typename type_traits
is_POD_type_t1;
typedef typename type_traits
has_trivial_default_constructor_t2;
typedef typename type_traits
has_trivial_copy_constructor_t2;
typedef typename type_traits
has_trivial_assignment_operator_t2;
typedef typename type_traits
has_trivial_destructor_t2;
typedef typename type_traits
is_POD_type_t2;
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------