4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
12 * This material is provided "as is", with absolutely no warranty expressed
13 * or implied. Any use is at your own risk.
15 * Permission to use or copy this software for any purpose is hereby granted
16 * without fee, provided the above notices are retained on all copies.
17 * Permission to modify the code and to distribute modified code is granted,
18 * provided the above notices are retained, and a notice that the code was
19 * modified is included with the above copyright notice.
23 /* NOTE: This is an internal header file, included by other STL headers.
24 * You should not attempt to use it directly.
27 #ifndef _STLP_INTERNAL_NUMERIC_H
28 #define _STLP_INTERNAL_NUMERIC_H
30 #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
31 # include <stl/_function_base.h>
34 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
35 # include <stl/_iterator_base.h>
40 template <class _InputIterator
, class _Tp
>
42 _Tp
accumulate(_InputIterator __first
, _InputIterator __last
, _Tp _Init
) {
43 _STLP_DEBUG_CHECK(_STLP_PRIV
__check_range(__first
, __last
))
44 for ( ; __first
!= __last
; ++__first
)
45 _Init
= _Init
+ *__first
;
49 template <class _InputIterator
, class _Tp
, class _BinaryOperation
>
51 _Tp
accumulate(_InputIterator __first
, _InputIterator __last
, _Tp _Init
,
52 _BinaryOperation __binary_op
) {
53 _STLP_DEBUG_CHECK(_STLP_PRIV
__check_range(__first
, __last
))
54 for ( ; __first
!= __last
; ++__first
)
55 _Init
= __binary_op(_Init
, *__first
);
59 template <class _InputIterator1
, class _InputIterator2
, class _Tp
>
61 _Tp
inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
62 _InputIterator2 __first2
, _Tp _Init
) {
63 _STLP_DEBUG_CHECK(_STLP_PRIV
__check_range(__first1
, __last1
))
64 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
65 _Init
= _Init
+ (*__first1
* *__first2
);
69 template <class _InputIterator1
, class _InputIterator2
, class _Tp
,
70 class _BinaryOperation1
, class _BinaryOperation2
>
72 _Tp
inner_product(_InputIterator1 __first1
, _InputIterator1 __last1
,
73 _InputIterator2 __first2
, _Tp _Init
,
74 _BinaryOperation1 __binary_op1
,
75 _BinaryOperation2 __binary_op2
) {
76 _STLP_DEBUG_CHECK(_STLP_PRIV
__check_range(__first1
, __last1
))
77 for ( ; __first1
!= __last1
; ++__first1
, ++__first2
)
78 _Init
= __binary_op1(_Init
, __binary_op2(*__first1
, *__first2
));
82 _STLP_MOVE_TO_PRIV_NAMESPACE
84 template <class _InputIterator
, class _OutputIterator
, class _Tp
,
85 class _BinaryOperation
>
87 __partial_sum(_InputIterator __first
, _InputIterator __last
,
88 _OutputIterator __result
, _Tp
*, _BinaryOperation __binary_op
);
90 _STLP_MOVE_TO_STD_NAMESPACE
92 template <class _InputIterator
, class _OutputIterator
>
93 inline _OutputIterator
94 partial_sum(_InputIterator __first
, _InputIterator __last
,
95 _OutputIterator __result
) {
96 return _STLP_PRIV
__partial_sum(__first
, __last
, __result
, _STLP_VALUE_TYPE(__first
, _InputIterator
),
97 _STLP_PRIV
__plus(_STLP_VALUE_TYPE(__first
, _InputIterator
)));
100 template <class _InputIterator
, class _OutputIterator
, class _BinaryOperation
>
101 inline _OutputIterator
102 partial_sum(_InputIterator __first
, _InputIterator __last
,
103 _OutputIterator __result
, _BinaryOperation __binary_op
) {
104 return _STLP_PRIV
__partial_sum(__first
, __last
, __result
, _STLP_VALUE_TYPE(__first
, _InputIterator
),
108 _STLP_MOVE_TO_PRIV_NAMESPACE
110 template <class _InputIterator
, class _OutputIterator
, class _Tp
,
111 class _BinaryOperation
>
113 __adjacent_difference(_InputIterator __first
, _InputIterator __last
,
114 _OutputIterator __result
, _Tp
*,
115 _BinaryOperation __binary_op
);
117 _STLP_MOVE_TO_STD_NAMESPACE
119 template <class _InputIterator
, class _OutputIterator
>
120 inline _OutputIterator
121 adjacent_difference(_InputIterator __first
,
122 _InputIterator __last
, _OutputIterator __result
) {
123 return _STLP_PRIV
__adjacent_difference(__first
, __last
, __result
,
124 _STLP_VALUE_TYPE(__first
, _InputIterator
),
125 _STLP_PRIV
__minus(_STLP_VALUE_TYPE(__first
, _InputIterator
)));
128 template <class _InputIterator
, class _OutputIterator
, class _BinaryOperation
>
130 adjacent_difference(_InputIterator __first
, _InputIterator __last
,
131 _OutputIterator __result
, _BinaryOperation __binary_op
) {
132 return _STLP_PRIV
__adjacent_difference(__first
, __last
, __result
,
133 _STLP_VALUE_TYPE(__first
, _InputIterator
),
137 _STLP_MOVE_TO_PRIV_NAMESPACE
139 template <class _Tp
, class _Integer
, class _MonoidOperation
>
140 _Tp
__power(_Tp __x
, _Integer __n
, _MonoidOperation __opr
);
142 _STLP_MOVE_TO_STD_NAMESPACE
144 #if !defined (_STLP_NO_EXTENSIONS)
146 // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
147 // is required to be associative, but not necessarily commutative.
149 _STLP_MOVE_TO_PRIV_NAMESPACE
151 template <class _Tp
, class _Integer
>
152 inline _Tp
__power(_Tp __x
, _Integer __n
) {
153 return __power(__x
, __n
, multiplies
<_Tp
>());
156 _STLP_MOVE_TO_STD_NAMESPACE
158 // Alias for the internal name __power. Note that power is an extension,
159 // not part of the C++ standard.
160 template <class _Tp
, class _Integer
, class _MonoidOperation
>
161 inline _Tp
power(_Tp __x
, _Integer __n
, _MonoidOperation __opr
) {
162 return _STLP_PRIV
__power(__x
, __n
, __opr
);
165 template <class _Tp
, class _Integer
>
166 inline _Tp
power(_Tp __x
, _Integer __n
) {
167 return _STLP_PRIV
__power(__x
, __n
, multiplies
<_Tp
>());
170 // iota is not part of the C++ standard. It is an extension.
172 template <class _ForwardIterator
, class _Tp
>
174 void iota(_ForwardIterator __first
, _ForwardIterator __last
, _Tp __val
) {
175 _STLP_DEBUG_CHECK(_STLP_PRIV
__check_range(__first
, __last
))
176 while (__first
!= __last
)
177 *__first
++ = __val
++;
183 #if !defined (_STLP_LINK_TIME_INSTANTIATION)
184 # include <stl/_numeric.c>
187 #endif /* _STLP_INTERNAL_NUMERIC_H */