00001 // Functions used by iterators -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004 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 _ITERATOR_BASE_FUNCS_H 00065 #define _ITERATOR_BASE_FUNCS_H 1 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 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00079 00080 typename iterator_traits<_InputIterator>::difference_type __n = 0; 00081 while (__first != __last) 00082 { 00083 ++__first; 00084 ++__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 __glibcxx_function_requires(_RandomAccessIteratorConcept< 00096 _RandomAccessIterator>) 00097 return __last - __first; 00098 } 00099 00100 /** 00101 * @brief A generalization of pointer arithmetic. 00102 * @param first An input iterator. 00103 * @param last An input iterator. 00104 * @return The distance between them. 00105 * 00106 * Returns @c n such that first + n == last. This requires that @p last 00107 * must be reachable from @p first. Note that @c n may be negative. 00108 * 00109 * For random access iterators, this uses their @c + and @c - operations 00110 * and are constant time. For other %iterator classes they are linear time. 00111 */ 00112 template<typename _InputIterator> 00113 inline typename iterator_traits<_InputIterator>::difference_type 00114 distance(_InputIterator __first, _InputIterator __last) 00115 { 00116 // concept requirements -- taken care of in __distance 00117 return std::__distance(__first, __last, 00118 std::__iterator_category(__first)); 00119 } 00120 00121 template<typename _InputIterator, typename _Distance> 00122 inline void 00123 __advance(_InputIterator& __i, _Distance __n, input_iterator_tag) 00124 { 00125 // concept requirements 00126 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00127 while (__n--) 00128 ++__i; 00129 } 00130 00131 template<typename _BidirectionalIterator, typename _Distance> 00132 inline void 00133 __advance(_BidirectionalIterator& __i, _Distance __n, 00134 bidirectional_iterator_tag) 00135 { 00136 // concept requirements 00137 __glibcxx_function_requires(_BidirectionalIteratorConcept< 00138 _BidirectionalIterator>) 00139 if (__n > 0) 00140 while (__n--) 00141 ++__i; 00142 else 00143 while (__n++) 00144 --__i; 00145 } 00146 00147 template<typename _RandomAccessIterator, typename _Distance> 00148 inline void 00149 __advance(_RandomAccessIterator& __i, _Distance __n, 00150 random_access_iterator_tag) 00151 { 00152 // concept requirements 00153 __glibcxx_function_requires(_RandomAccessIteratorConcept< 00154 _RandomAccessIterator>) 00155 __i += __n; 00156 } 00157 00158 /** 00159 * @brief A generalization of pointer arithmetic. 00160 * @param i An input iterator. 00161 * @param n The "delta" by which to change @p i. 00162 * @return Nothing. 00163 * 00164 * This increments @p i by @p n. For bidirectional and random access 00165 * iterators, @p n may be negative, in which case @p i is decremented. 00166 * 00167 * For random access iterators, this uses their @c + and @c - operations 00168 * and are constant time. For other %iterator classes they are linear time. 00169 */ 00170 template<typename _InputIterator, typename _Distance> 00171 inline void 00172 advance(_InputIterator& __i, _Distance __n) 00173 { 00174 // concept requirements -- taken care of in __advance 00175 std::__advance(__i, __n, std::__iterator_category(__i)); 00176 } 00177 } // namespace std 00178 00179 #endif /* _ITERATOR_BASE_FUNCS_H */