Whole document tree
    

Whole document tree

stl_iterator_base_funcs.h Source File
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 // Functions used by iterators -*- C++ -*-
00002 
00003 // Copyright (C) 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996-1998
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00056 /* NOTE: This is an internal header file, included by other STL headers.
00057  *   You should not attempt to use it directly.
00058  */
00059 
00060 #ifndef __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
00061 #define __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
00062 
00063 // This file contains all of the general iterator-related utility
00064 // functions, such as distance() and advance().
00065 // The internal file stl_iterator.h contains predefined iterators, 
00066 // such as front_insert_iterator and istream_iterator.
00067 
00068 #pragma GCC system_header
00069 #include <bits/concept_check.h>
00070 
00071 namespace std
00072 {
00073 
00074 // There are two signatures for distance.  In addition to the one taking
00075 // two iterators and returning a result, there is another taking two
00076 // iterators and a reference-to-result variable, and returning nothing.
00077 // The latter seems to be an SGI extension.   -- pedwards
00078 template <class _InputIterator, class _Distance>
00079 inline void __distance(_InputIterator __first, _InputIterator __last,
00080                        _Distance& __n, input_iterator_tag)
00081 {
00082   // concept requirements
00083   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00084   while (__first != __last) { ++__first; ++__n; }
00085 }
00086 
00087 template <class _RandomAccessIterator, class _Distance>
00088 inline void __distance(_RandomAccessIterator __first, 
00089                        _RandomAccessIterator __last, 
00090                        _Distance& __n, random_access_iterator_tag)
00091 {
00092   // concept requirements
00093   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00094   __n += __last - __first;
00095 }
00096 
00097 template <class _InputIterator, class _Distance>
00098 inline void distance(_InputIterator __first, 
00099                      _InputIterator __last, _Distance& __n)
00100 {
00101   // concept requirements -- taken care of in __distance
00102   __distance(__first, __last, __n, iterator_category(__first));
00103 }
00104 
00105 template <class _InputIterator>
00106 inline typename iterator_traits<_InputIterator>::difference_type
00107 __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
00108 {
00109   // concept requirements
00110   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
00111   typename iterator_traits<_InputIterator>::difference_type __n = 0;
00112   while (__first != __last) {
00113     ++__first; ++__n;
00114   }
00115   return __n;
00116 }
00117 
00118 template <class _RandomAccessIterator>
00119 inline typename iterator_traits<_RandomAccessIterator>::difference_type
00120 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00121            random_access_iterator_tag)
00122 {
00123   // concept requirements
00124   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00125   return __last - __first;
00126 }
00127 
00128 template <class _InputIterator>
00129 inline typename iterator_traits<_InputIterator>::difference_type
00130 distance(_InputIterator __first, _InputIterator __last)
00131 {
00132   // concept requirements -- taken care of in __distance
00133   typedef typename iterator_traits<_InputIterator>::iterator_category 
00134     _Category;
00135   return __distance(__first, __last, _Category());
00136 }
00137 
00138 template <class _InputIter, class _Distance>
00139 inline void __advance(_InputIter& __i, _Distance __n, input_iterator_tag)
00140 {
00141   // concept requirements
00142   __glibcpp_function_requires(_InputIteratorConcept<_InputIter>);
00143   while (__n--) ++__i;
00144 }
00145 
00146 template <class _BidirectionalIterator, class _Distance>
00147 inline void __advance(_BidirectionalIterator& __i, _Distance __n, 
00148                       bidirectional_iterator_tag)
00149 {
00150   // concept requirements
00151 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>);
00152   if (__n > 0)
00153     while (__n--) ++__i;
00154   else
00155     while (__n++) --__i;
00156 }
00157 
00158 template <class _RandomAccessIterator, class _Distance>
00159 inline void __advance(_RandomAccessIterator& __i, _Distance __n, 
00160                       random_access_iterator_tag)
00161 {
00162   // concept requirements
00163   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
00164   __i += __n;
00165 }
00166 
00167 template <class _InputIterator, class _Distance>
00168 inline void advance(_InputIterator& __i, _Distance __n)
00169 {
00170   // concept requirements -- taken care of in __advance
00171   __advance(__i, __n, iterator_category(__i));
00172 }
00173 
00174 } // namespace std
00175 
00176 #endif /* __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H */
00177 
00178 
00179 // Local Variables:
00180 // mode:C++
00181 // End:

Generated on Mon Apr 8 03:11:41 2002 for libstdc++-v3 Source by doxygen1.2.15