Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

stl_iterator_base_funcs.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996-1998
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 #ifndef __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
00032 #define __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
00033 
00034 // This file contains all of the general iterator-related utility
00035 // functions, such as distance() and advance().
00036 // The internal file stl_iterator.h contains predefined iterators, 
00037 // such as front_insert_iterator and istream_iterator.
00038 
00039 #pragma GCC system_header
00040 #include <bits/concept_check.h>
00041 
00042 namespace std
00043 {
00044 
00045 // There are two signatures for distance.  In addition to the one taking
00046 // two iterators and returning a result, there is another taking two
00047 // iterators and a reference-to-result variable, and returning nothing.
00048 // The latter seems to be an SGI extension.   -- pedwards
00049 template <class _InputIterator, class _Distance>
00050 inline void __distance(_InputIterator __first, _InputIterator __last,
00051                        _Distance& __n, input_iterator_tag)
00052 {
00053   // concept requirements
00054   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00055   while (__first != __last) { ++__first; ++__n; }
00056 }
00057 
00058 template <class _RandomAccessIterator, class _Distance>
00059 inline void __distance(_RandomAccessIterator __first, 
00060                        _RandomAccessIterator __last, 
00061                        _Distance& __n, random_access_iterator_tag)
00062 {
00063   // concept requirements
00064   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00065   __n += __last - __first;
00066 }
00067 
00068 template <class _InputIterator, class _Distance>
00069 inline void distance(_InputIterator __first, 
00070                      _InputIterator __last, _Distance& __n)
00071 {
00072   // concept requirements -- taken care of in __distance
00073   __distance(__first, __last, __n, iterator_category(__first));
00074 }
00075 
00076 template <class _InputIterator>
00077 inline typename iterator_traits<_InputIterator>::difference_type
00078 __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
00079 {
00080   // concept requirements
00081   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00082   typename iterator_traits<_InputIterator>::difference_type __n = 0;
00083   while (__first != __last) {
00084     ++__first; ++__n;
00085   }
00086   return __n;
00087 }
00088 
00089 template <class _RandomAccessIterator>
00090 inline typename iterator_traits<_RandomAccessIterator>::difference_type
00091 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00092            random_access_iterator_tag)
00093 {
00094   // concept requirements
00095   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00096   return __last - __first;
00097 }
00098 
00099 template <class _InputIterator>
00100 inline typename iterator_traits<_InputIterator>::difference_type
00101 distance(_InputIterator __first, _InputIterator __last)
00102 {
00103   // concept requirements -- taken care of in __distance
00104   typedef typename iterator_traits<_InputIterator>::iterator_category 
00105     _Category;
00106   return __distance(__first, __last, _Category());
00107 }
00108 
00109 template <class _InputIter, class _Distance>
00110 inline void __advance(_InputIter& __i, _Distance __n, input_iterator_tag)
00111 {
00112   // concept requirements
00113   __glibcpp_function_requires(_InputIteratorConcept<_InputIter>);
00114   while (__n--) ++__i;
00115 }
00116 
00117 template <class _BidirectionalIterator, class _Distance>
00118 inline void __advance(_BidirectionalIterator& __i, _Distance __n, 
00119                       bidirectional_iterator_tag)
00120 {
00121   // concept requirements
00122 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>);
00123   if (__n > 0)
00124     while (__n--) ++__i;
00125   else
00126     while (__n++) --__i;
00127 }
00128 
00129 template <class _RandomAccessIterator, class _Distance>
00130 inline void __advance(_RandomAccessIterator& __i, _Distance __n, 
00131                       random_access_iterator_tag)
00132 {
00133   // concept requirements
00134   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00135   __i += __n;
00136 }
00137 
00138 template <class _InputIterator, class _Distance>
00139 inline void advance(_InputIterator& __i, _Distance __n)
00140 {
00141   // concept requirements -- taken care of in __advance
00142   __advance(__i, __n, iterator_category(__i));
00143 }
00144 
00145 } // namespace std
00146 
00147 #endif /* __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H */
00148 
00149 
00150 // Local Variables:
00151 // mode:C++
00152 // End:

Generated at Tue May 1 16:28:39 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001