1 // Numeric functions implementation -*- C++ -*-
3 // Copyright (C) 2001 Free Software Foundation, Inc.
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)
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.
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,
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.
33 * Hewlett-Packard Company
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.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
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.
56 /** @file stl_numeric.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef _STL_NUMERIC_H
62 #define _STL_NUMERIC_H 1
64 #include <debug/debug.h>
69 template<typename _InputIterator
, typename _Tp
>
71 accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
)
73 // concept requirements
74 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
75 __glibcxx_requires_valid_range(__first
, __last
);
77 for ( ; __first
!= __last
; ++__first
)
78 __init
= __init
+ *__first
;
82 template<typename _InputIterator
, typename _Tp
, typename _BinaryOperation
>
84 accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
,
85 _BinaryOperation __binary_op
)
87 // concept requirements
88 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
89 __glibcxx_requires_valid_range(__first
, __last
);
91 for ( ; __first
!= __last
; ++__first
)
92 __init
= __binary_op(__init
, *__first
);
96 template<typename _InputIterator1
, typename _InputIterator2
, typename _Tp
>
98 inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
99 _InputIterator2 __first2
, _Tp __init
)
101 // concept requirements
102 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
103 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
104 __glibcxx_requires_valid_range(__first1
, __last1
);
106 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
107 __init
= __init
+ (*__first1
* *__first2
);
111 template<typename _InputIterator1
, typename _InputIterator2
, typename _Tp
,
112 typename _BinaryOperation1
, typename _BinaryOperation2
>
114 inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
115 _InputIterator2 __first2
, _Tp __init
,
116 _BinaryOperation1 __binary_op1
,
117 _BinaryOperation2 __binary_op2
)
119 // concept requirements
120 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
121 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
122 __glibcxx_requires_valid_range(__first1
, __last1
);
124 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
125 __init
= __binary_op1(__init
, __binary_op2(*__first1
, *__first2
));
129 template<typename _InputIterator
, typename _OutputIterator
>
131 partial_sum(_InputIterator __first
, _InputIterator __last
,
132 _OutputIterator __result
)
134 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
136 // concept requirements
137 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
138 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _ValueType
>)
139 __glibcxx_requires_valid_range(__first
, __last
);
141 if (__first
== __last
) return __result
;
142 *__result
= *__first
;
143 _ValueType __value
= *__first
;
144 while (++__first
!= __last
) {
145 __value
= __value
+ *__first
;
146 *++__result
= __value
;
151 template<typename _InputIterator
, typename _OutputIterator
, typename _BinaryOperation
>
153 partial_sum(_InputIterator __first
, _InputIterator __last
,
154 _OutputIterator __result
, _BinaryOperation __binary_op
)
156 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
158 // concept requirements
159 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
160 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _ValueType
>)
161 __glibcxx_requires_valid_range(__first
, __last
);
163 if (__first
== __last
) return __result
;
164 *__result
= *__first
;
165 _ValueType __value
= *__first
;
166 while (++__first
!= __last
) {
167 __value
= __binary_op(__value
, *__first
);
168 *++__result
= __value
;
173 template<typename _InputIterator
, typename _OutputIterator
>
175 adjacent_difference(_InputIterator __first
,
176 _InputIterator __last
, _OutputIterator __result
)
178 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
180 // concept requirements
181 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
182 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _ValueType
>)
183 __glibcxx_requires_valid_range(__first
, __last
);
185 if (__first
== __last
) return __result
;
186 *__result
= *__first
;
187 _ValueType __value
= *__first
;
188 while (++__first
!= __last
) {
189 _ValueType __tmp
= *__first
;
190 *++__result
= __tmp
- __value
;
196 template<typename _InputIterator
, typename _OutputIterator
, typename _BinaryOperation
>
198 adjacent_difference(_InputIterator __first
, _InputIterator __last
,
199 _OutputIterator __result
, _BinaryOperation __binary_op
)
201 typedef typename iterator_traits
<_InputIterator
>::value_type _ValueType
;
203 // concept requirements
204 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator
>)
205 __glibcxx_function_requires(_OutputIteratorConcept
<_OutputIterator
, _ValueType
>)
206 __glibcxx_requires_valid_range(__first
, __last
);
208 if (__first
== __last
) return __result
;
209 *__result
= *__first
;
210 _ValueType __value
= *__first
;
211 while (++__first
!= __last
) {
212 _ValueType __tmp
= *__first
;
213 *++__result
= __binary_op(__tmp
, __value
);
221 #endif /* _STL_NUMERIC_H */