0
点赞
收藏
分享

微信扫一扫

198-C++迭代器代码

山竹山竹px 2022-02-18 阅读 85
C++

#ifndef yhp_iterator_h
#define yhp_iterator_h
namespace yhp
{
typedef int ptrdiff_t;

//
template<class _T1, class _T2> 
struct pair 
{
	typedef _T1 first_type;
	typedef _T2 second_type;
public:
	pair(): first(_T1()), second(_T2()) {}

	pair(const _T1& _V1, const _T2& _V2): first(_V1), second(_V2) {}

	//template<class U, class V>
	//pair(const pair<U, V> &p): first(p.first), second(p.second) {}

	_T1 first;
	_T2 second;
};

template<class _T1, class _T2> inline
	bool __cdecl operator==(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (_X.first == _Y.first && _X.second == _Y.second); }

template<class _T1, class _T2> inline
	bool __cdecl operator!=(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (!(_X == _Y)); }

template<class _T1, class _T2> inline
	bool __cdecl operator<(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (_X.first < _Y.first ||
		!(_Y.first < _X.first) && _X.second < _Y.second); }

template<class _T1, class _T2> inline
	bool __cdecl operator>(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (_Y < _X); }

template<class _T1, class _T2> inline
	bool __cdecl operator<=(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (!(_Y < _X)); }

template<class _T1, class _T2> inline
	bool __cdecl operator>=(const pair<_T1, _T2>& _X,
		const pair<_T1, _T2>& _Y)
	{return (!(_X < _Y)); }

template<class _T1, class _T2> inline
	pair<_T1, _T2> __cdecl make_pair(const _T1& _X, const _T2& _Y)
	{return (pair<_T1, _T2>(_X, _Y)); }
///

struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag: public forward_iterator_tag {};
struct random_access_iterator_tag: public bidirectional_iterator_tag  {};

template<class _C, class _Ty, class _D = ptrdiff_t,class _Pointer = _Ty*,
         class _Reference = _Ty &>
struct iterator 
{
	typedef _C          iterator_category;
	typedef _Ty         value_type;
	typedef _D          difference_type;
	typedef _Pointer    pointer;
	typedef _Reference  reference;
};

template<class _Iterator>
struct iterator_traits
{
	//iterator_traits() { }
	typedef typename _Iterator::iterator_category iterator_category;
	typedef typename _Iterator::value_type        value_type;
	typedef typename _Iterator::difference_type   difference_type;
	typedef typename _Iterator::pointer           pointer;
	typedef typename _Iterator::reference         reference;
};

template<class T>
struct iterator_traits<T*>  // Int 
{
	//iterator_traits() { }
	typedef random_access_iterator_tag iterator_category;
	typedef T                          value_type;
	typedef int                        difference_type;
	typedef T *                        pointer;
	typedef T &                        reference;
};

template<class T>
struct iterator_traits<const T*>
{
	//iterator_traits() { }
	typedef random_access_iterator_tag iterator_category;
	typedef T                          value_type;
	typedef int                        difference_type;
	typedef const T *                        pointer;
	typedef const T &                        reference;
};
//
// SGI
template<class _II>
inline typename iterator_traits<_II>::iterator_category
iterator_category(const _II &)
{
	typedef typename iterator_traits<_II>::iterator_category category;
	return category();
}
template<class _II>
inline typename iterator_traits<_II>::value_type *
value_type(const _II &)
{
	 return static_cast< typename iterator_traits<_II>::value_type * > (0);
}

template<class _II>
inline typename iterator_traits<_II>::difference_type *
difference_type(const _II &)
{
	 return static_cast< typename iterator_traits<_II>::difference_type * > (0);
}
///
// 
template<class _C, class _Ty, class _D> inline
_C _Iter_cat(const iterator<_C, _Ty, _D>&)
{
	_C _IterCatTag;
	_C* _pIterCatTag;
	_pIterCatTag = &_IterCatTag;	
	return (_IterCatTag); 
}
template<class _Ty> inline
random_access_iterator_tag __cdecl _Iter_cat(const _Ty *)
{
	random_access_iterator_tag _RandIterTag;
	random_access_iterator_tag* _pRandIterTag;
	_pRandIterTag = &_RandIterTag;	
	return (_RandIterTag); 
}

template<class _Ty,class _D>
struct _Bidit : public iterator<bidirectional_iterator_tag,_Ty,_D> {};
// List // Map  // Set
template<class _Ty,class _D>
struct _Ranit: public iterator<random_access_iterator_tag,_Ty,_D> {};
// dequeu // vector
template<class _Ty,class _D>
struct _Forit: public iterator<forward_iterator_tag,_Ty,_D> {};
// slist //  hashtable;

template<class _II,class _D>
inline void __advance(_II &i,_D n,input_iterator_tag )
{
	while(n--) ++i;
}

template<class _BI,class _D>
inline void __advance(_BI &i,_D n,bidirectional_iterator_tag )
{
	if(n >= 0)
	{
		while(n--) ++i;
	}
	else
	{
		while(n++) --i;
	}
}

template<class _RI,class _D>
inline void __advance(_RI &i,_D n,random_access_iterator_tag )
{
	i += n;
}
template<class _II,class _D>
inline void advance(_II &i,_D n)
{
	//iterator_traits<_II>();
	//typedef typename iterator_traits<_II>::iterator_category  cate;
	//__advance(i,n,cate());
	__advance(i,n,iterator_category(i));
}

template<class _II>
inline typename iterator_traits<_II>::difference_type 
__distance(_II _F , _II _L, input_iterator_tag)
{
	typename iterator_traits<_II>::difference_type  n = 0;
	while(_F != _L)
	{
		++_F;
		++n;
	}
	return n;
}

template<class _II>
inline typename iterator_traits<_II>::difference_type 
__distance(_II _F , _II _L, random_access_iterator_tag)
{
	return _L - _F;
}
template<class _II>
inline typename iterator_traits<_II>::difference_type 
distance(_II _F , _II _L)
{
	typedef typename iterator_traits<_II>::iterator_category  cate;
	return __distance(_F,_L,cate());
}

}
#endif
举报

相关推荐

0 条评论