热门标签 | 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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------




推荐阅读
  • vue使用
    关键词: ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
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社区 版权所有