stl_iterator_base_funcs.h revision 1.1
1// Functions used by iterators -*- C++ -*- 2 3// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 4// Free Software Foundation, Inc. 5// 6// This file is part of the GNU ISO C++ Library. This library is free 7// software; you can redistribute it and/or modify it under the 8// terms of the GNU General Public License as published by the 9// Free Software Foundation; either version 3, or (at your option) 10// any later version. 11 12// This library is distributed in the hope that it will be useful, 13// but WITHOUT ANY WARRANTY; without even the implied warranty of 14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15// GNU General Public License for more details. 16 17// Under Section 7 of GPL version 3, you are granted additional 18// permissions described in the GCC Runtime Library Exception, version 19// 3.1, as published by the Free Software Foundation. 20 21// You should have received a copy of the GNU General Public License and 22// a copy of the GCC Runtime Library Exception along with this program; 23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24// <http://www.gnu.org/licenses/>. 25 26/* 27 * 28 * Copyright (c) 1994 29 * Hewlett-Packard Company 30 * 31 * Permission to use, copy, modify, distribute and sell this software 32 * and its documentation for any purpose is hereby granted without fee, 33 * provided that the above copyright notice appear in all copies and 34 * that both that copyright notice and this permission notice appear 35 * in supporting documentation. Hewlett-Packard Company makes no 36 * representations about the suitability of this software for any 37 * purpose. It is provided "as is" without express or implied warranty. 38 * 39 * 40 * Copyright (c) 1996-1998 41 * Silicon Graphics Computer Systems, Inc. 42 * 43 * Permission to use, copy, modify, distribute and sell this software 44 * and its documentation for any purpose is hereby granted without fee, 45 * provided that the above copyright notice appear in all copies and 46 * that both that copyright notice and this permission notice appear 47 * in supporting documentation. Silicon Graphics makes no 48 * representations about the suitability of this software for any 49 * purpose. It is provided "as is" without express or implied warranty. 50 */ 51 52/** @file stl_iterator_base_funcs.h 53 * This is an internal header file, included by other library headers. 54 * You should not attempt to use it directly. 55 * 56 * This file contains all of the general iterator-related utility 57 * functions, such as distance() and advance(). 58 */ 59 60#ifndef _STL_ITERATOR_BASE_FUNCS_H 61#define _STL_ITERATOR_BASE_FUNCS_H 1 62 63#pragma GCC system_header 64 65#include <bits/concept_check.h> 66 67_GLIBCXX_BEGIN_NAMESPACE(std) 68 69 template<typename _InputIterator> 70 inline typename iterator_traits<_InputIterator>::difference_type 71 __distance(_InputIterator __first, _InputIterator __last, 72 input_iterator_tag) 73 { 74 // concept requirements 75 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 76 77 typename iterator_traits<_InputIterator>::difference_type __n = 0; 78 while (__first != __last) 79 { 80 ++__first; 81 ++__n; 82 } 83 return __n; 84 } 85 86 template<typename _RandomAccessIterator> 87 inline typename iterator_traits<_RandomAccessIterator>::difference_type 88 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last, 89 random_access_iterator_tag) 90 { 91 // concept requirements 92 __glibcxx_function_requires(_RandomAccessIteratorConcept< 93 _RandomAccessIterator>) 94 return __last - __first; 95 } 96 97 /** 98 * @brief A generalization of pointer arithmetic. 99 * @param first An input iterator. 100 * @param last An input iterator. 101 * @return The distance between them. 102 * 103 * Returns @c n such that first + n == last. This requires that @p last 104 * must be reachable from @p first. Note that @c n may be negative. 105 * 106 * For random access iterators, this uses their @c + and @c - operations 107 * and are constant time. For other %iterator classes they are linear time. 108 */ 109 template<typename _InputIterator> 110 inline typename iterator_traits<_InputIterator>::difference_type 111 distance(_InputIterator __first, _InputIterator __last) 112 { 113 // concept requirements -- taken care of in __distance 114 return std::__distance(__first, __last, 115 std::__iterator_category(__first)); 116 } 117 118 template<typename _InputIterator, typename _Distance> 119 inline void 120 __advance(_InputIterator& __i, _Distance __n, input_iterator_tag) 121 { 122 // concept requirements 123 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 124 while (__n--) 125 ++__i; 126 } 127 128 template<typename _BidirectionalIterator, typename _Distance> 129 inline void 130 __advance(_BidirectionalIterator& __i, _Distance __n, 131 bidirectional_iterator_tag) 132 { 133 // concept requirements 134 __glibcxx_function_requires(_BidirectionalIteratorConcept< 135 _BidirectionalIterator>) 136 if (__n > 0) 137 while (__n--) 138 ++__i; 139 else 140 while (__n++) 141 --__i; 142 } 143 144 template<typename _RandomAccessIterator, typename _Distance> 145 inline void 146 __advance(_RandomAccessIterator& __i, _Distance __n, 147 random_access_iterator_tag) 148 { 149 // concept requirements 150 __glibcxx_function_requires(_RandomAccessIteratorConcept< 151 _RandomAccessIterator>) 152 __i += __n; 153 } 154 155 /** 156 * @brief A generalization of pointer arithmetic. 157 * @param i An input iterator. 158 * @param n The @a delta by which to change @p i. 159 * @return Nothing. 160 * 161 * This increments @p i by @p n. For bidirectional and random access 162 * iterators, @p n may be negative, in which case @p i is decremented. 163 * 164 * For random access iterators, this uses their @c + and @c - operations 165 * and are constant time. For other %iterator classes they are linear time. 166 */ 167 template<typename _InputIterator, typename _Distance> 168 inline void 169 advance(_InputIterator& __i, _Distance __n) 170 { 171 // concept requirements -- taken care of in __advance 172 typename iterator_traits<_InputIterator>::difference_type __d = __n; 173 std::__advance(__i, __d, std::__iterator_category(__i)); 174 } 175 176_GLIBCXX_END_NAMESPACE 177 178#ifdef __GXX_EXPERIMENTAL_CXX0X__ 179 180#include <ext/type_traits.h> // For __enable_if and __is_iterator 181 182_GLIBCXX_BEGIN_NAMESPACE(std) 183 184 template<typename _ForwardIterator> 185 inline typename 186 __gnu_cxx::__enable_if<__is_iterator<_ForwardIterator>::__value, 187 _ForwardIterator>::__type 188 next(_ForwardIterator __x, typename 189 iterator_traits<_ForwardIterator>::difference_type __n = 1) 190 { 191 std::advance(__x, __n); 192 return __x; 193 } 194 195 template<typename _BidirectionalIterator> 196 inline typename 197 __gnu_cxx::__enable_if<__is_iterator<_BidirectionalIterator>::__value, 198 _BidirectionalIterator>::__type 199 prev(_BidirectionalIterator __x, typename 200 iterator_traits<_BidirectionalIterator>::difference_type __n = 1) 201 { 202 std::advance(__x, -__n); 203 return __x; 204 } 205 206_GLIBCXX_END_NAMESPACE 207 208#endif // __GXX_EXPERIMENTAL_CXX0X__ 209 210#endif /* _STL_ITERATOR_BASE_FUNCS_H */ 211