注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

SGI STL源码文件stl_iterator_base.h[注释]  

2011-02-16 15:41:35|  分类: c++学习笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

 

/ *

*

* Copyright (c) 1994

* Hewlett- Packard Company

*

* Permission to use, copy, modify, distribute and sell this software

* and its documentation 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. Hewlett- Packard Company makes no

* representations about the suitability of this software for any

* purpose. It is provided "as is" without express or implied warranty.

*

*

* Copyright (c) 1996- 1998

* Silicon Graphics Computer Systems, Inc.

*

* Permission to use, copy, modify, distribute and sell this software

* and its documentation 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. Silicon Graphics makes no

* representations about the suitability of this software for any

* purpose. It is provided "as is" without express or implied warranty.

*/

 

/ * NOTE: This is an internal header file, included by other STL headers.

*    You should not attempt to use it directly.

*/

 

#ifndef SGI_STL_INTERNAL_ITERATOR_BASE_H

#define SGI_STL_INTERNAL_ITERATOR_BASE_H

 

/ / This file contains all of the general iterator- related utilities.

/ / The internal file stl_iterator.h contains predefined iterators,

/ / such as front_insert_iterator and istream_iterator.

 

#include <concept_checks.h>

 

STL_BEGIN_NAMESPACE

 

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 {};

//这些工具类是用来设置迭代器的种类,用这些类作为迭代器的内部category的定义可以方便使用traits功能,这些其实是是C++标准的要求,请记住这个继承关系

 

SGI STL源码文件stl_iterator_base.h[注释] - saturnman - 一路

 

/ / The base classes input_iterator, output_iterator, forward_iterator,

/ / bidirectional_iterator, and random_access_iterator are not part of

/ / the C++ standard. (They have been replaced by struct iterator.)

/ / They are included for backward compatibility with the HP STL.

 

 

 

//下面这些是对五种iterator 的定义,注意这几种iterator的定义并不使用继承关系

template <class _Tp, class _Distance> struct input_iterator {

typedef input_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef _Distance    difference_type;

typedef _Tp*    pointer;

typedef _Tp&    reference;

};

 

struct output_iterator {

typedef output_iterator_tag iterator_category;

typedef void    value_type;

typedef void    difference_type;

typedef void    pointer;

typedef void    reference;

};

 

template <class _Tp, class _Distance> struct forward_iterator {

typedef forward_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef _Distance    difference_type;

typedef _Tp*    pointer;

typedef _Tp&    reference;

};

 

 

template <class _Tp, class _Distance> struct bidirectional_iterator {

typedef bidirectional_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef _Distance    difference_type;

typedef _Tp*    pointer;

typedef _Tp&    reference;

};

 

 

template <class _Tp, class _Distance> struct

random_access_iterator {

typedef random_access_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef _Distance    difference_type;

typedef _Tp*    pointer;

typedef _Tp&    reference;

};

 

#ifdef STL_USE_NAMESPACES

 

 

//这时是定义一个标准的iterator做为接口用,用户如果定义自己的iterator只要继承自这个标准接口就可以使自己定义的iteratorSTL的标准算法想结合,直接调用标准库中的算法,当然如果不使用继承关系,但写法一定要与标标相符合。这里注意一下C++一个不太常用的特性 ,类的内部定义类型可以被子类继承并可以被子类覆盖,这个特性很重要。

template <class _Category, class _Tp, class _Distance = ptrdiff_t,

class _Pointer = _Tp*, class _Reference = _Tp&>

struct iterator {

typedef _Category    iterator_category;

typedef _Tp    value_type;

typedef _Distance    difference_type;

typedef _Pointer    pointer;

typedef _Reference reference;

};

#endif / *     STL_USE_NAMESPACES */

 

#ifdef STL_CLASS_PARTIAL_SPECIALIZATION

//定义萃取器,此处的定义是最通用的,下面会使用各种类型对这人模版类定义进行偏特化

template <class _Iterator>

struct 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 _Tp>

struct iterator_traits<_Tp*> {

typedef random_access_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef ptrdiff_t    difference_type;

typedef _Tp*    pointer;

typedef _Tp&    reference;

};

//常量指针的偏特化

template <class _Tp>

struct iterator_traits<const _Tp*> {

typedef random_access_iterator_tag iterator_category;

typedef _Tp    value_type;

typedef ptrdiff_t    difference_type;

typedef const _Tp*    pointer;

typedef const _Tp&    reference;

};

 

/ / The overloaded functions iterator_category, distance_type, and

/ / value_type are not part of the C++ standard. (They have been

/ / replaced by struct iterator_traits.) They are included for

/ / backward compatibility with the HP STL.

 

/ / We introduce internal names for these functions.

//得到迭代器类型的一个默认值变量的工具函数

template <class _Iter>

inline typename iterator_traits<_Iter>::iterator_category

 

 

 

 

__iterator_category(const

{

 

_Iter&)

 

typedef typename iterator_traits<_Iter>::iterator_category _Category;

return _Category();

}

//得到迭代器的差的类型的一个默认值工具函数

template <class _Iter>

inline typename iterator_traits<_Iter>::difference_type*

 

__distance_type(const

{

 

_Iter&)

 

return static_cast<typename iterator_traits<_Iter>::difference_type*>(

0);

}

//得到迭代器所指类型的指针的默认值的一个工具函数

template <class _Iter>

inline typename iterator_traits<_Iter>::value_type*

 

__value_type(const

{

 

_Iter&)

 

return static_cast<typename iterator_traits<_Iter>::value_type*>(0);

}

//利用工具函数得到传入迭代器类型的一个默认值对象

template <class _Iter>

inline typename iterator_traits<_Iter>::iterator_category

iterator_category(const _Iter& __i) { return iterator_category(__i)

; }

 

//利用工具函数得到传入迭代器类型差的一个默认值对象

 

template <class _Iter>

inline typename iterator_traits<_Iter>::difference_type*

distance_type(const _Iter& __i) { return distance_type(__i); }

 

//利用工具函数得到传入迭代器所指类型的一个空指针

template <class _Iter>

inline typename iterator_traits<_Iter>::value_type*

value_type(const _Iter& __i) { return value_type( i); }

 

#define ITERATOR_CATEGORY( i) __iterator_category( i)

#define DISTANCE_TYPE( i)    __distance_type( i)

#define VALUE_TYPE( i)    __value_type( i)

 

#else / *     STL_CLASS_PARTIAL_SPECIALIZATION */

 

//以下是对上面各种函数是的偏特化

 

//input_iterator 的偏特化

template <class _Tp, class _Distance>

inline input_iterator_tag

iterator_category(const input_iterator<_Tp, _Distance>&)

{ return input_iterator_tag(); }

 

//output_iterator的偏特化

inline output_iterator_tag iterator_category(const output_iterator&)

{ return output_iterator_tag(); }

 

//forward_iterator的偏特化

template <class _Tp, class _Distance>

inline forward_iterator_tag

 

iterator_category(const forward_iterator<_Tp, _Distance>&)

{ return forward_iterator_tag(); }

//bidirectional_iterator 的偏特化

template <class _Tp, class _Distance>

inline bidirectional_iterator_tag

 

 

 

iterator_category(const bidirectional_iterator<_Tp, _Distance>&)

{ return bidirectional_iterator_tag(); }

 

//random_access_iterator的偏特化

template <class _Tp, class _Distance>

inline random_access_iterator_tag

iterator_category(const random_access_iterator<_Tp, _Distance>&)

{ return random_access_iterator_tag(); }

 

//random_access_iterator对原生指针的偏特化

template <class _Tp>

inline random_access_iterator_tag iterator_category(const _Tp*)

{ return random_access_iterator_tag(); }

 

template <class _Tp, class _Distance>

inline _Tp* value_type(const input_iterator<_Tp, _Distance>&)

{ return (_Tp*)(0); }

 

template <class _Tp, class _Distance>

inline _Tp* value_type(const forward_iterator<_Tp, _Distance>&)

{ return (_Tp*)(0); }

 

template <class _Tp, class _Distance>

inline _Tp* value_type(const bidirectional_iterator<_Tp, _Distance>&)

{ return (_Tp*)(0); }

 

template <class _Tp, class _Distance>

inline _Tp* value_type(const random_access_iterator<_Tp, _Distance>&)

{ return (_Tp*)(0); }

 

template <class _Tp>

inline _Tp* value_type(const _Tp*) { return (_Tp*)(0); }

 

template <class _Tp, class _Distance>

inline _Distance* distance_type(const input_iterator<_Tp, _Distance>

&)

{

return (_Distance*)(0);

}

 

template <class _Tp, class _Distance>

inline _Distance* distance_type(const forward_iterator<_Tp,

_Distance>&)

{

return (_Distance*)(0);

}

 

template <class _Tp, class _Distance>

inline _Distance*

distance_type(const bidirectional_iterator<_Tp, _Distance>&)

{

return (_Distance*)(0);

}

 

template <class _Tp, class _Distance>

inline _Distance*

 

 

 

distance_type(const random_access_iterator<_Tp, _Distance>&)

{

return (_Distance*)(0);

}

 

template <class _Tp>

inline ptrdiff_t* distance_type(const _Tp*) { return (ptrdiff_t*)(0)

; }

 

/ / Without partial specialization we can't use iterator_traits, so

/ / we must keep the old iterator query functions around.

 

#define ITERATOR_CATEGORY( i) iterator_category( i)

#define DISTANCE_TYPE( i)    distance_type( i)

#define VALUE_TYPE( i)    value_type( i)

 

#endif / *     STL_CLASS_PARTIAL_SPECIALIZATION */

//以下的__distance工具函数很重要,它们对不同类型的迭代器选择最快的到达方式,如果迭代器是random access的,直接加上距离就可以了,其它的都要一步一步移动,这个函数在distance函数中被用到,其中具体调用哪个函数就由traits萃取出的类型在编译期决定。

template <class _InputIterator, class _Distance>

 

 

inline void

__distance(_InputIterator

 

__first,

 

_InputIterator

 

__last,

 

_Distance& n, input_iterator_tag)

{

while ( first != last) { ++ first; ++ n; }

}

 

template <class _RandomAccessIterator, class _Distance>

 

 

inline void

__distance(_RandomAccessIterator

 

__first,

 

_RandomAccessIterator last,

_Distance& n, random_access_iterator_tag)

{

STL_REQUIRES(_RandomAccessIterator, _RandomAccessIterator);

n += last - first;

}

 

template <class _InputIterator, class _Distance>

 

inline void distance(_InputIterator

 

__first,

 

_InputIterator last, _Distance& n)

{

STL_REQUIRES(_InputIterator, _InputIterator);

//启动类型萃取,哈哈

distance(__first, __last, __n, iterator_category(__first));

}

 

#ifdef STL_CLASS_PARTIAL_SPECIALIZATION

 

template <class _InputIterator>

inline typename iterator_traits<_InputIterator>::difference_type

 

__distance(_InputIterator

input_iterator_tag)

{

 

__first,

 

_InputIterator

 

__last,

 

typename iterator_traits<_InputIterator>::difference_type n = 0;

while ( first != last) {

++ first; ++ n;

}

return n;

}

 

template <class _RandomAccessIterator>

 

 

 

inline typename iterator_traits<_RandomAccessIterator>::difference_type

 

__distance(_RandomAccessIterator

last,

 

__first,

 

_RandomAccessIterator

 

random_access_iterator_tag) {

STL_REQUIRES(_RandomAccessIterator, _RandomAccessIterator);

return last - first;

}

 

template <class _InputIterator>

inline typename iterator_traits<_InputIterator>::difference_type

 

distance(_InputIterator

 

__first,

 

_InputIterator

 

__last) {

 

typedef typename iterator_traits<_InputIterator>::iterator_category

_Category;

STL_REQUIRES(_InputIterator, _InputIterator);

return distance(__first, __last, _Category());

}

 

#endif / *     STL_CLASS_PARTIAL_SPECIALIZATION */

 

template <class _InputIter, class _Distance>

 

 

inline void

__advance(_InputIter&

 

__i,

 

_Distance

 

__n,

 

input_iterator_tag) {

while ( n--) ++ i;

}

 

#if defined( sgi) && !defined(__GNUC__) && (_MIPS_SIM !=

_MIPS_SIM_ABI32)

#pragma set woff 1183

#endif

 

template <class _BidirectionalIterator, class _Distance>

 

 

inline void

__advance(_BidirectionalIterator&

 

__i,

 

_Distance

 

__n,

 

bidirectional_iterator_tag) {

STL_REQUIRES(_BidirectionalIterator, _BidirectionalIterator);

if ( n >= 0)

while ( n--) ++ i;

else

while ( n++) -- i;

}

 

#if defined( sgi) && !defined(__GNUC__) && (_MIPS_SIM !=

_MIPS_SIM_ABI32)

#pragma reset woff 1183

#endif

 

template <class _RandomAccessIterator, class _Distance>

 

 

inline void

__advance(_RandomAccessIterator&

 

__i,

 

_Distance

 

__n,

 

random_access_iterator_tag) {

STL_REQUIRES(_RandomAccessIterator, _RandomAccessIterator);

i += n;

}

 

 

 

template <class _InputIterator, class _Distance>

 

inline void advance(_InputIterator&

 

__i,

 

_Distance

 

__n) {

 

STL_REQUIRES(_InputIterator, _InputIterator);

advance(__i, __n, iterator_category(__i));

}

 

STL_END_NAMESPACE

 

#endif / *     SGI_STL_INTERNAL_ITERATOR_BASE_H */

 

 

 

 

/ / Local Variables:

/ / mode:C++

/ / End:

  评论这张
 
阅读(1053)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017