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 
00056 /** @file stl_iterator_base_funcs.h
00057  *  This is an internal header file, included by other library headers.
00058  *  You should not attempt to use it directly.
00059  *
00060  *  This file contains all of the general iterator-related utility
00061  *  functions, such as distance() and advance().
00062  */
00063 
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 namespace std
00071 {
00072   template<typename _InputIterator>
00073     inline typename iterator_traits<_InputIterator>::difference_type
00074     __distance(_InputIterator __first, _InputIterator __last,
00075                input_iterator_tag)
00076     {
00077       // concept requirements
00078       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
00079   
00080       typename iterator_traits<_InputIterator>::difference_type __n = 0;
00081       while (__first != __last) {
00082         ++__first; ++__n;
00083       }
00084       return __n;
00085     }
00086   
00087   template<typename _RandomAccessIterator>
00088     inline typename iterator_traits<_RandomAccessIterator>::difference_type
00089     __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
00090                random_access_iterator_tag)
00091     {
00092       // concept requirements
00093       __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
00094       return __last - __first;
00095     }
00096   
00097   /**
00098    *  @brief A generalization of pointer arithmetic.
00099    *  @param  first  An input iterator.
00100    *  @param  last  An input iterator.
00101    *  @return  The distance between them.
00102    *
00103    *  Returns @c n such that first + n == last.  This requires that @p last
00104    *  must be reachable from @p first.  Note that @c n may be negative.
00105    *
00106    *  For random access iterators, this uses their @c + and @c - operations
00107    *  and are constant time.  For other %iterator classes they are linear time.
00108   */
00109   template<typename _InputIterator>
00110     inline typename iterator_traits<_InputIterator>::difference_type
00111     distance(_InputIterator __first, _InputIterator __last)
00112     {
00113       // concept requirements -- taken care of in __distance
00114       return __distance(__first, __last, __iterator_category(__first));
00115     }
00116   
00117   template<typename _InputIter, typename _Distance>
00118     inline void
00119     __advance(_InputIter& __i, _Distance __n, input_iterator_tag)
00120     {
00121       // concept requirements
00122       __glibcpp_function_requires(_InputIteratorConcept<_InputIter>)
00123       while (__n--) ++__i;
00124     }
00125   
00126   template<typename _BidirectionalIterator, typename _Distance>
00127     inline void
00128     __advance(_BidirectionalIterator& __i, _Distance __n,
00129               bidirectional_iterator_tag)
00130     {
00131       // concept requirements
00132       __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
00133   
00134       if (__n > 0)
00135         while (__n--) ++__i;
00136       else
00137         while (__n++) --__i;
00138     }
00139   
00140   template<typename _RandomAccessIterator, typename _Distance>
00141     inline void
00142     __advance(_RandomAccessIterator& __i, _Distance __n,
00143               random_access_iterator_tag)
00144     {
00145       // concept requirements
00146       __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>)
00147       __i += __n;
00148     }
00149   
00150   /**
00151    *  @brief A generalization of pointer arithmetic.
00152    *  @param  i  An input iterator.
00153    *  @param  n  The "delta" by which to change @p i.
00154    *  @return  Nothing.
00155    *
00156    *  This increments @p i by @p n.  For bidirectional and random access
00157    *  iterators, @p n may be negative, in which case @p i is decremented.
00158    *
00159    *  For random access iterators, this uses their @c + and @c - operations
00160    *  and are constant time.  For other %iterator classes they are linear time.
00161   */
00162   template<typename _InputIterator, typename _Distance>
00163     inline void
00164     advance(_InputIterator& __i, _Distance __n)
00165     {
00166       // concept requirements -- taken care of in __advance
00167       __advance(__i, __n, __iterator_category(__i));
00168     }
00169 } // namespace std
00170 
00171 #endif /* __GLIBCPP_INTERNAL_ITERATOR_BASE_FUNCS_H */

Generated on Thu Feb 10 23:22:58 2005 for libstdc++-v3 Source by  doxygen 1.4.0