OSDN Git Service

2001-06-22 Phil Edwards <pme@sources.redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_iterator_base_funcs.h
1 /*
2  *
3  * Copyright (c) 1994
4  * Hewlett-Packard Company
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.  Hewlett-Packard Company makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  *
15  * Copyright (c) 1996-1998
16  * Silicon Graphics Computer Systems, Inc.
17  *
18  * Permission to use, copy, modify, distribute and sell this software
19  * and its documentation for any purpose is hereby granted without fee,
20  * provided that the above copyright notice appear in all copies and
21  * that both that copyright notice and this permission notice appear
22  * in supporting documentation.  Silicon Graphics makes no
23  * representations about the suitability of this software for any
24  * purpose.  It is provided "as is" without express or implied warranty.
25  */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28  *   You should not attempt to use it directly.
29  */
30
31 #ifndef __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
32 #define __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H
33
34 // This file contains all of the general iterator-related utility
35 // functions, such as distance() and advance().
36 // The internal file stl_iterator.h contains predefined iterators, 
37 // such as front_insert_iterator and istream_iterator.
38
39 #pragma GCC system_header
40 #include <bits/concept_check.h>
41
42 namespace std
43 {
44
45 // There are two signatures for distance.  In addition to the one taking
46 // two iterators and returning a result, there is another taking two
47 // iterators and a reference-to-result variable, and returning nothing.
48 // The latter seems to be an SGI extension.   -- pedwards
49 template <class _InputIterator, class _Distance>
50 inline void __distance(_InputIterator __first, _InputIterator __last,
51                        _Distance& __n, input_iterator_tag)
52 {
53   // concept requirements
54   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
55   while (__first != __last) { ++__first; ++__n; }
56 }
57
58 template <class _RandomAccessIterator, class _Distance>
59 inline void __distance(_RandomAccessIterator __first, 
60                        _RandomAccessIterator __last, 
61                        _Distance& __n, random_access_iterator_tag)
62 {
63   // concept requirements
64   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
65   __n += __last - __first;
66 }
67
68 template <class _InputIterator, class _Distance>
69 inline void distance(_InputIterator __first, 
70                      _InputIterator __last, _Distance& __n)
71 {
72   // concept requirements -- taken care of in __distance
73   __distance(__first, __last, __n, iterator_category(__first));
74 }
75
76 template <class _InputIterator>
77 inline typename iterator_traits<_InputIterator>::difference_type
78 __distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
79 {
80   // concept requirements
81   __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>);
82   typename iterator_traits<_InputIterator>::difference_type __n = 0;
83   while (__first != __last) {
84     ++__first; ++__n;
85   }
86   return __n;
87 }
88
89 template <class _RandomAccessIterator>
90 inline typename iterator_traits<_RandomAccessIterator>::difference_type
91 __distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
92            random_access_iterator_tag)
93 {
94   // concept requirements
95   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
96   return __last - __first;
97 }
98
99 template <class _InputIterator>
100 inline typename iterator_traits<_InputIterator>::difference_type
101 distance(_InputIterator __first, _InputIterator __last)
102 {
103   // concept requirements -- taken care of in __distance
104   typedef typename iterator_traits<_InputIterator>::iterator_category 
105     _Category;
106   return __distance(__first, __last, _Category());
107 }
108
109 template <class _InputIter, class _Distance>
110 inline void __advance(_InputIter& __i, _Distance __n, input_iterator_tag)
111 {
112   // concept requirements
113   __glibcpp_function_requires(_InputIteratorConcept<_InputIter>);
114   while (__n--) ++__i;
115 }
116
117 template <class _BidirectionalIterator, class _Distance>
118 inline void __advance(_BidirectionalIterator& __i, _Distance __n, 
119                       bidirectional_iterator_tag)
120 {
121   // concept requirements
122 __glibcpp_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>);
123   if (__n > 0)
124     while (__n--) ++__i;
125   else
126     while (__n++) --__i;
127 }
128
129 template <class _RandomAccessIterator, class _Distance>
130 inline void __advance(_RandomAccessIterator& __i, _Distance __n, 
131                       random_access_iterator_tag)
132 {
133   // concept requirements
134   __glibcpp_function_requires(_RandomAccessIteratorConcept<_RandomAccessIterator>);
135   __i += __n;
136 }
137
138 template <class _InputIterator, class _Distance>
139 inline void advance(_InputIterator& __i, _Distance __n)
140 {
141   // concept requirements -- taken care of in __advance
142   __advance(__i, __n, iterator_category(__i));
143 }
144
145 } // namespace std
146
147 #endif /* __SGI_STL_INTERNAL_ITERATOR_BASE_FUNCS_H */
148
149
150 // Local Variables:
151 // mode:C++
152 // End: