# stl_iterator_base_funcs.h

Go to the documentation of this file.

    00001 // Functions used by iterators -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002 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
    00064 #ifndef __GLIBCPP_INTERNAL_ITERATOR_BASE_FUNCS_H
    00065 #define __GLIBCPP_INTERNAL_ITERATOR_BASE_FUNCS_H
    00066
    00067 #pragma GCC system_header
    00068 #include <bits/concept_check.h>
    00069
    00070 // Since this entire file is within namespace std, there's no reason to
    00071 // waste two spaces along the left column.  Thus the leading indentation is
    00072 // slightly violated from here on.
    00073 namespace std
    00074 {
    00075 template<typename _InputIterator>
    00076   inline typename iterator_traits<_InputIterator>::difference_type
    00077   __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
    00078   {
    00079     // concept requirements
    00080     __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
    00081
    00082     typename iterator_traits<_InputIterator>::difference_type __n = 0;
    00083     while (__first != __last) {
    00084       ++__first; ++__n;
    00085     }
    00086     return __n;
    00087   }
    00088
    00089 template<typename _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
    00111 template<typename _InputIterator>
    00112   inline typename iterator_traits<_InputIterator>::difference_type
    00113   distance(_InputIterator __first, _InputIterator __last)
    00114   {
    00115     // concept requirements -- taken care of in __distance
    00116     return __distance(__first, __last, __iterator_category(__first));
    00117   }
    00118
    00119 template<typename _InputIter, typename _Distance>
    00120   inline void
    00121   __advance(_InputIter& __i, _Distance __n, input_iterator_tag)
    00122   {
    00123     // concept requirements
    00124     __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
    00125     while (__n--) ++__i;
    00126   }
    00127
    00128 template<typename _BidirectionalIterator, typename _Distance>
    00129   inline void
    00130   __advance(_BidirectionalIterator& __i, _Distance __n,
    00131             bidirectional_iterator_tag)
    00132   {
    00133     // concept requirements
    00134     __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
    00135
    00136     if (__n > 0)
    00137       while (__n--) ++__i;
    00138     else
    00139       while (__n++) --__i;
    00140   }
    00141
    00142 template<typename _RandomAccessIterator, typename _Distance>
    00143   inline void
    00144   __advance(_RandomAccessIterator& __i, _Distance __n,
    00145             random_access_iterator_tag)
    00146   {
    00147     // concept requirements
    00148     __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
    00149     __i += __n;
    00150   }
    00151
    00164 template<typename _InputIterator, typename _Distance>
    00165   inline void
    00166   advance(_InputIterator& __i, _Distance __n)
    00167   {
    00168     // concept requirements -- taken care of in __advance
    00169     __advance(__i, __n, __iterator_category(__i));
    00170   }
    00171
    00172 } // namespace std
    00173
    00174 #endif /* __GLIBCPP_INTERNAL_ITERATOR_BASE_FUNCS_H */
    00175
    00176
    00177 // Local Variables:
    00178 // mode:C++
    00179 // End: