libstdc++
|
00001 // Functions used by iterators -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996-1998 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_iterator_base_funcs.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{iterator} 00054 * 00055 * This file contains all of the general iterator-related utility 00056 * functions, such as distance() and advance(). 00057 */ 00058 00059 #ifndef _STL_ITERATOR_BASE_FUNCS_H 00060 #define _STL_ITERATOR_BASE_FUNCS_H 1 00061 00062 #pragma GCC system_header 00063 00064 #include <bits/concept_check.h> 00065 #include <debug/debug.h> 00066 00067 namespace std _GLIBCXX_VISIBILITY(default) 00068 { 00069 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00070 00071 template<typename _InputIterator> 00072 inline typename iterator_traits<_InputIterator>::difference_type 00073 __distance(_InputIterator __first, _InputIterator __last, 00074 input_iterator_tag) 00075 { 00076 // concept requirements 00077 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 00078 00079 typename iterator_traits<_InputIterator>::difference_type __n = 0; 00080 while (__first != __last) 00081 { 00082 ++__first; 00083 ++__n; 00084 } 00085 return __n; 00086 } 00087 00088 template<typename _RandomAccessIterator> 00089 inline typename iterator_traits<_RandomAccessIterator>::difference_type 00090 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, 00091 random_access_iterator_tag) 00092 { 00093 // concept requirements 00094 __glibcxx_function_requires(_RandomAccessIteratorConcept< 00095 _RandomAccessIterator>) 00096 return __last - __first; 00097 } 00098 00099 /** 00100 * @brief A generalization of pointer arithmetic. 00101 * @param __first An input iterator. 00102 * @param __last An input iterator. 00103 * @return The distance between them. 00104 * 00105 * Returns @c n such that __first + n == __last. This requires 00106 * that @p __last must be reachable from @p __first. Note that @c 00107 * 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 _GLIBCXX_DEBUG_ASSERT(__n >= 0); 00128 while (__n--) 00129 ++__i; 00130 } 00131 00132 template<typename _BidirectionalIterator, typename _Distance> 00133 inline void 00134 __advance(_BidirectionalIterator& __i, _Distance __n, 00135 bidirectional_iterator_tag) 00136 { 00137 // concept requirements 00138 __glibcxx_function_requires(_BidirectionalIteratorConcept< 00139 _BidirectionalIterator>) 00140 if (__n > 0) 00141 while (__n--) 00142 ++__i; 00143 else 00144 while (__n++) 00145 --__i; 00146 } 00147 00148 template<typename _RandomAccessIterator, typename _Distance> 00149 inline void 00150 __advance(_RandomAccessIterator& __i, _Distance __n, 00151 random_access_iterator_tag) 00152 { 00153 // concept requirements 00154 __glibcxx_function_requires(_RandomAccessIteratorConcept< 00155 _RandomAccessIterator>) 00156 __i += __n; 00157 } 00158 00159 /** 00160 * @brief A generalization of pointer arithmetic. 00161 * @param __i An input iterator. 00162 * @param __n The @a delta by which to change @p __i. 00163 * @return Nothing. 00164 * 00165 * This increments @p i by @p n. For bidirectional and random access 00166 * iterators, @p __n may be negative, in which case @p __i is decremented. 00167 * 00168 * For random access iterators, this uses their @c + and @c - operations 00169 * and are constant time. For other %iterator classes they are linear time. 00170 */ 00171 template<typename _InputIterator, typename _Distance> 00172 inline void 00173 advance(_InputIterator& __i, _Distance __n) 00174 { 00175 // concept requirements -- taken care of in __advance 00176 typename iterator_traits<_InputIterator>::difference_type __d = __n; 00177 std::__advance(__i, __d, std::__iterator_category(__i)); 00178 } 00179 00180 #if __cplusplus >= 201103L 00181 00182 template<typename _ForwardIterator> 00183 inline _ForwardIterator 00184 next(_ForwardIterator __x, typename 00185 iterator_traits<_ForwardIterator>::difference_type __n = 1) 00186 { 00187 std::advance(__x, __n); 00188 return __x; 00189 } 00190 00191 template<typename _BidirectionalIterator> 00192 inline _BidirectionalIterator 00193 prev(_BidirectionalIterator __x, typename 00194 iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 00195 { 00196 std::advance(__x, -__n); 00197 return __x; 00198 } 00199 00200 #endif // C++11 00201 00202 _GLIBCXX_END_NAMESPACE_VERSION 00203 } // namespace 00204 00205 #endif /* _STL_ITERATOR_BASE_FUNCS_H */