4 * Hewlett-Packard Company
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.
15 * Copyright (c) 1996,1997
16 * Silicon Graphics Computer Systems, Inc.
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.
27 /* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
32 #ifndef __SGI_STL_INTERNAL_NUMERIC_H
33 #define __SGI_STL_INTERNAL_NUMERIC_H
37 template <class _InputIterator
, class _Tp
>
38 _Tp
accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
)
40 for ( ; __first
!= __last
; ++__first
)
41 __init
= __init
+ *__first
;
45 template <class _InputIterator
, class _Tp
, class _BinaryOperation
>
46 _Tp
accumulate(_InputIterator __first
, _InputIterator __last
, _Tp __init
,
47 _BinaryOperation __binary_op
)
49 for ( ; __first
!= __last
; ++__first
)
50 __init
= __binary_op(__init
, *__first
);
54 template <class _InputIterator1
, class _InputIterator2
, class _Tp
>
55 _Tp
inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
56 _InputIterator2 __first2
, _Tp __init
)
58 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
59 __init
= __init
+ (*__first1
* *__first2
);
63 template <class _InputIterator1
, class _InputIterator2
, class _Tp
,
64 class _BinaryOperation1
, class _BinaryOperation2
>
65 _Tp
inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
66 _InputIterator2 __first2
, _Tp __init
,
67 _BinaryOperation1 __binary_op1
,
68 _BinaryOperation2 __binary_op2
)
70 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
71 __init
= __binary_op1(__init
, __binary_op2(*__first1
, *__first2
));
75 template <class _InputIterator
, class _OutputIterator
, class _Tp
>
77 __partial_sum(_InputIterator __first
, _InputIterator __last
,
78 _OutputIterator __result
, _Tp
*)
80 _Tp __value
= *__first
;
81 while (++__first
!= __last
) {
82 __value
= __value
+ *__first
;
83 *++__result
= __value
;
88 template <class _InputIterator
, class _OutputIterator
>
90 partial_sum(_InputIterator __first
, _InputIterator __last
,
91 _OutputIterator __result
)
93 if (__first
== __last
) return __result
;
95 return __partial_sum(__first
, __last
, __result
, __VALUE_TYPE(__first
));
98 template <class _InputIterator
, class _OutputIterator
, class _Tp
,
99 class _BinaryOperation
>
101 __partial_sum(_InputIterator __first
, _InputIterator __last
,
102 _OutputIterator __result
, _Tp
*, _BinaryOperation __binary_op
)
104 _Tp __value
= *__first
;
105 while (++__first
!= __last
) {
106 __value
= __binary_op(__value
, *__first
);
107 *++__result
= __value
;
112 template <class _InputIterator
, class _OutputIterator
, class _BinaryOperation
>
114 partial_sum(_InputIterator __first
, _InputIterator __last
,
115 _OutputIterator __result
, _BinaryOperation __binary_op
)
117 if (__first
== __last
) return __result
;
118 *__result
= *__first
;
119 return __partial_sum(__first
, __last
, __result
, __VALUE_TYPE(__first
),
123 template <class _InputIterator
, class _OutputIterator
, class _Tp
>
125 __adjacent_difference(_InputIterator __first
, _InputIterator __last
,
126 _OutputIterator __result
, _Tp
*)
128 _Tp __value
= *__first
;
129 while (++__first
!= __last
) {
130 _Tp __tmp
= *__first
;
131 *++__result
= __tmp
- __value
;
137 template <class _InputIterator
, class _OutputIterator
>
139 adjacent_difference(_InputIterator __first
,
140 _InputIterator __last
, _OutputIterator __result
)
142 if (__first
== __last
) return __result
;
143 *__result
= *__first
;
144 return __adjacent_difference(__first
, __last
, __result
,
145 __VALUE_TYPE(__first
));
148 template <class _InputIterator
, class _OutputIterator
, class _Tp
,
149 class _BinaryOperation
>
151 __adjacent_difference(_InputIterator __first
, _InputIterator __last
,
152 _OutputIterator __result
, _Tp
*,
153 _BinaryOperation __binary_op
) {
154 _Tp __value
= *__first
;
155 while (++__first
!= __last
) {
156 _Tp __tmp
= *__first
;
157 *++__result
= __binary_op(__tmp
, __value
);
163 template <class _InputIterator
, class _OutputIterator
, class _BinaryOperation
>
165 adjacent_difference(_InputIterator __first
, _InputIterator __last
,
166 _OutputIterator __result
, _BinaryOperation __binary_op
)
168 if (__first
== __last
) return __result
;
169 *__result
= *__first
;
170 return __adjacent_difference(__first
, __last
, __result
,
171 __VALUE_TYPE(__first
),
175 // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
176 // is required to be associative, but not necessarily commutative.
179 template <class _Tp
, class _Integer
, class _MonoidOperation
>
180 _Tp
__power(_Tp __x
, _Integer __n
, _MonoidOperation __opr
)
183 return identity_element(__opr
);
185 while ((__n
& 1) == 0) {
187 __x
= __opr(__x
, __x
);
193 __x
= __opr(__x
, __x
);
195 __result
= __opr(__result
, __x
);
202 template <class _Tp
, class _Integer
>
203 inline _Tp
__power(_Tp __x
, _Integer __n
)
205 return __power(__x
, __n
, multiplies
<_Tp
>());
208 // Alias for the internal name __power. Note that power is an extension,
209 // not part of the C++ standard.
211 template <class _Tp
, class _Integer
, class _MonoidOperation
>
212 inline _Tp
power(_Tp __x
, _Integer __n
, _MonoidOperation __opr
)
214 return __power(__x
, __n
, __opr
);
217 template <class _Tp
, class _Integer
>
218 inline _Tp
power(_Tp __x
, _Integer __n
)
220 return __power(__x
, __n
);
223 // iota is not part of the C++ standard. It is an extension.
225 template <class _ForwardIterator
, class _Tp
>
227 iota(_ForwardIterator __first
, _ForwardIterator __last
, _Tp __value
)
229 while (__first
!= __last
)
230 *__first
++ = __value
++;
235 #endif /* __SGI_STL_INTERNAL_NUMERIC_H */