OSDN Git Service

2001-11-01 Phil Edwards <pme@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_numeric.h
1 // Numeric functions implementation -*- C++ -*-
2
3 // Copyright (C) 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
35  * Permission to use, copy, modify, distribute and sell this software
36  * and its documentation for any purpose is hereby granted without fee,
37  * provided that the above copyright notice appear in all copies and
38  * that both that copyright notice and this permission notice appear
39  * in supporting documentation.  Hewlett-Packard Company makes no
40  * representations about the suitability of this software for any
41  * purpose.  It is provided "as is" without express or implied warranty.
42  *
43  *
44  * Copyright (c) 1996,1997
45  * Silicon Graphics Computer Systems, Inc.
46  *
47  * Permission to use, copy, modify, distribute and sell this software
48  * and its documentation for any purpose is hereby granted without fee,
49  * provided that the above copyright notice appear in all copies and
50  * that both that copyright notice and this permission notice appear
51  * in supporting documentation.  Silicon Graphics makes no
52  * representations about the suitability of this software for any
53  * purpose.  It is provided "as is" without express or implied warranty.
54  */
55
56 /* NOTE: This is an internal header file, included by other STL headers.
57  *   You should not attempt to use it directly.
58  */
59
60
61 #ifndef _CPP_BITS_STL_NUMERIC_H
62 #define _CPP_BITS_STL_NUMERIC_H 1
63
64 namespace std
65 {
66
67   template<typename _InputIterator, typename _Tp>
68     _Tp
69     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
70     {
71       // concept requirements
72       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
73
74       for ( ; __first != __last; ++__first)
75         __init = __init + *__first;
76       return __init;
77     }
78
79   template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
80     _Tp
81     accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
82                _BinaryOperation __binary_op)
83     {
84       // concept requirements
85       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
86
87       for ( ; __first != __last; ++__first)
88         __init = __binary_op(__init, *__first);
89       return __init;
90     }
91
92   template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
93     _Tp
94     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
95                   _InputIterator2 __first2, _Tp __init)
96     {
97       // concept requirements
98       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
99       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
100
101       for ( ; __first1 != __last1; ++__first1, ++__first2)
102         __init = __init + (*__first1 * *__first2);
103       return __init;
104     }
105
106   template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
107             typename _BinaryOperation1, typename _BinaryOperation2>
108     _Tp
109     inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
110                   _InputIterator2 __first2, _Tp __init, 
111                   _BinaryOperation1 __binary_op1,
112                   _BinaryOperation2 __binary_op2)
113     {
114       // concept requirements
115       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
116       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
117
118       for ( ; __first1 != __last1; ++__first1, ++__first2)
119         __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
120       return __init;
121     }
122
123   template<typename _InputIterator, typename _OutputIterator>
124     _OutputIterator 
125     partial_sum(_InputIterator __first, _InputIterator __last,
126                 _OutputIterator __result)
127     {
128       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
129
130       // concept requirements
131       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
132       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
133
134       if (__first == __last) return __result;
135       *__result = *__first;
136       _ValueType __value = *__first;
137       while (++__first != __last) {
138         __value = __value + *__first;
139         *++__result = __value;
140       }
141       return ++__result;
142     }
143
144   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
145     _OutputIterator 
146     partial_sum(_InputIterator __first, _InputIterator __last,
147                 _OutputIterator __result, _BinaryOperation __binary_op)
148     {
149       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
150
151       // concept requirements
152       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
153       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
154
155       if (__first == __last) return __result;
156       *__result = *__first;
157       _ValueType __value = *__first;
158       while (++__first != __last) {
159         __value = __binary_op(__value, *__first);
160         *++__result = __value;
161       }
162       return ++__result;
163     }
164
165   template<typename _InputIterator, typename _OutputIterator>
166     _OutputIterator
167     adjacent_difference(_InputIterator __first,
168                         _InputIterator __last, _OutputIterator __result)
169     {
170       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
171
172       // concept requirements
173       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
174       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
175
176       if (__first == __last) return __result;
177       *__result = *__first;
178       _ValueType __value = *__first;
179       while (++__first != __last) {
180         _ValueType __tmp = *__first;
181         *++__result = __tmp - __value;
182         __value = __tmp;
183       }
184       return ++__result;
185     }
186
187   template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
188     _OutputIterator 
189     adjacent_difference(_InputIterator __first, _InputIterator __last,
190                         _OutputIterator __result, _BinaryOperation __binary_op)
191     {
192       typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
193
194       // concept requirements
195       __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
196       __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
197
198       if (__first == __last) return __result;
199       *__result = *__first;
200       _ValueType __value = *__first;
201       while (++__first != __last) {
202         _ValueType __tmp = *__first;
203         *++__result = __binary_op(__tmp, __value);
204         __value = __tmp;
205       }
206       return ++__result;
207     }
208
209   // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
210   // is required to be associative, but not necessarily commutative.
211
212    
213   template<typename _Tp, typename _Integer, typename _MonoidOperation>
214     _Tp
215     __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
216     {
217       if (__n == 0)
218         return identity_element(__monoid_op);
219       else {
220         while ((__n & 1) == 0) {
221           __n >>= 1;
222           __x = __monoid_op(__x, __x);
223         }
224
225         _Tp __result = __x;
226         __n >>= 1;
227         while (__n != 0) {
228           __x = __monoid_op(__x, __x);
229           if ((__n & 1) != 0)
230             __result = __monoid_op(__result, __x);
231           __n >>= 1;
232         }
233         return __result;
234       }
235     }
236
237   template<typename _Tp, typename _Integer>
238     inline _Tp
239     __power(_Tp __x, _Integer __n)
240     { return __power(__x, __n, multiplies<_Tp>()); }
241
242   // Alias for the internal name __power.  Note that power is an extension,
243   // not part of the C++ standard.
244
245   template<typename _Tp, typename _Integer, typename _MonoidOperation>
246     inline _Tp
247     power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
248     { return __power(__x, __n, __monoid_op); }
249
250   template<typename _Tp, typename _Integer>
251     inline _Tp
252     power(_Tp __x, _Integer __n)
253     { return __power(__x, __n); }
254
255   // iota is not part of the C++ standard.  It is an extension.
256
257   template<typename _ForwardIter, typename _Tp>
258     void 
259     iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
260     {
261       // concept requirements
262       __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
263       __glibcpp_function_requires(_ConvertibleConcept<_Tp,
264             typename iterator_traits<_ForwardIter>::value_type>)
265
266       while (__first != __last)
267         *__first++ = __value++;
268     }
269
270 } // namespace std
271
272 #endif /* _CPP_BITS_STL_NUMERIC_H */
273
274 // Local Variables:
275 // mode:C++
276 // End: