热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

文件位置:youngy_iterator.hpp*TheyoungLibraryCopyright(c)2005by杨桓Permissiontouse,copy,modify,

文件位置: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( cptr );
}

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

//必须是双向迭代器以上的迭代器
template
class Reverse_Iterator
{
public:
    typedef  typename iterator_traits::iterator_category  iterator_category;
    typedef  typename iterator_traits::value_type         value_type;
    typedef  typename iterator_traits::reference          reference;
    typedef  typename iterator_traits::pointer            pointer;
    typedef  typename iterator_traits::size_type          size_type;
    typedef  typename iterator_traits::difference_type    difference_type;
    typedef  const pointer    const_pointer;
    typedef  const reference  const_reference;

    typedef  Reverse_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
inline typename Reverse_Iterator::difference_type
operator-( const Reverse_Iterator& lhs,
           const Reverse_Iterator& rhs )
{
    return rhs.base() - lhs.base();
}

template
inline Reverse_Iterator
operator+( typename Reverse_Iterator::difference_type n,
           const Reverse_Iterator& rhs )
{
    return Reverse_Iterator( rhs.base() - n );
}

template
inline bool operator==( const Reverse_Iterator& lhs,
                        const Reverse_Iterator& rhs )
{
    return ( lhs.base() == rhs.base() );
}

template
inline bool operator!=( const Reverse_Iterator& lhs,
                        const Reverse_Iterator& rhs )
{
    return ( lhs.base() != rhs.base() );
}

template
inline bool operator<( const Reverse_Iterator& lhs,
                       const Reverse_Iterator& rhs )
{
    return ( rhs.base() }

template
inline bool operator>( const Reverse_Iterator& lhs,
                       const Reverse_Iterator& rhs )
{
    return ( rhs }

template
inline bool operator<=( const Reverse_Iterator& lhs,
                        const Reverse_Iterator& rhs )
{
    return !( rhs }

template
inline bool operator>=( const Reverse_Iterator& lhs,
                        const Reverse_Iterator& rhs )
{
    return !( lhs }

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

template
inline typename iterator_traits::difference_type
distance( ForwardIterator first, ForwardIterator last )
{
    typedef  typename iterator_traits::iterator_category  cate;
    typedef  typename iterator_traits::difference_type  diff_t;
    return distance_aux( first, last, cate() );
}

template
typename iterator_traits::difference_type
distance_aux( ForwardIterator first, ForwardIterator last,
              input_iterator_tag )
{
    typedef  typename iterator_traits::difference_type  diff_t;

    diff_t len = 0;
    while( first != last )
    {
     ++first;
        ++len;
    }
    return len;
}

template
inline typename iterator_traits::difference_type
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::iterator_category  cate;
    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::iterator_category  cate;
    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::difference_type>
pair_iter_len( InputIterator first, InputIterator last )
{
    typedef  typename iterator_traits::iterator_category  cate;
    return iter_len_aux( first, last, cate() );
}

template
pair::difference_type>
iter_len_aux( InputIterator first, InputIterator last,
              input_iterator_tag )
{
    typedef  typename iterator_traits::difference_type  diff_t;

    if( first == last )
        return pair( first, 0 );

    diff_t len = 1;
    InputIterator result = first;
    ++first;
    while( first != last )
    {
     ++first;
     ++result;
        ++len;
    }
    return pair( result, len );
}

template
pair::difference_type>
iter_len_aux( InputIterator first, InputIterator last,
              random_access_iterator_tag )
{
    typedef  typename iterator_traits::difference_type  diff_t;

    diff_t len = last - first;
    if( len == 0 )
        return pair( first, 0 );
    else
        return pair( 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
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::primal_t  primal_t1;
    typedef  typename primal_type::primal_t  primal_t2;

    typedef  typename types_matching::result
             matching_result;

    typedef  typename type_traits::has_trivial_default_constructor
             has_trivial_default_constructor_t1;
    typedef  typename type_traits::has_trivial_copy_constructor
             has_trivial_copy_constructor_t1;
    typedef  typename type_traits::has_trivial_assignment_operator
             has_trivial_assignment_operator_t1;
    typedef  typename type_traits::has_trivial_destructor
             has_trivial_destructor_t1;
    typedef  typename type_traits::is_POD_type
             is_POD_type_t1;

    typedef  typename type_traits::has_trivial_default_constructor
             has_trivial_default_constructor_t2;
    typedef  typename type_traits::has_trivial_copy_constructor
             has_trivial_copy_constructor_t2;
    typedef  typename type_traits::has_trivial_assignment_operator
             has_trivial_assignment_operator_t2;
    typedef  typename type_traits::has_trivial_destructor
             has_trivial_destructor_t2;
    typedef  typename type_traits::is_POD_type
             is_POD_type_t2;
};

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------




推荐阅读
author-avatar
zongnaxxl240
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有