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.
28 #ifndef _STLP_INTERNAL_NUMERIC_H
29 #define _STLP_INTERNAL_NUMERIC_H
31 #ifndef _STLP_INTERNAL_FUNCTION_H
32 # include <stl/_function_base.h>
35 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
36 # include <stl/_iterator_base.h>
41 template <class _InputIterator, class _Tp>
43 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init)
45 _STLP_DEBUG_CHECK(__check_range(__first, __last))
46 for ( ; __first != __last; ++__first)
48 _Init = _Init + *__first;
55 template <class _InputIterator, class _Tp, class _BinaryOperation>
57 _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init,
58 _BinaryOperation __binary_op)
60 _STLP_DEBUG_CHECK(__check_range(__first, __last))
61 for ( ; __first != __last; ++__first)
62 _Init = __binary_op(_Init, *__first);
66 template <class _InputIterator1, class _InputIterator2, class _Tp>
68 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
69 _InputIterator2 __first2, _Tp _Init)
71 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
72 for ( ; __first1 != __last1; ++__first1, ++__first2)
73 _Init = _Init + (*__first1 * *__first2);
77 template <class _InputIterator1, class _InputIterator2, class _Tp,
78 class _BinaryOperation1, class _BinaryOperation2>
80 _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
81 _InputIterator2 __first2, _Tp _Init,
82 _BinaryOperation1 __binary_op1,
83 _BinaryOperation2 __binary_op2)
85 _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
86 for ( ; __first1 != __last1; ++__first1, ++__first2)
87 _Init = __binary_op1(_Init, __binary_op2(*__first1, *__first2));
91 template <class _InputIterator, class _OutputIterator, class _Tp,
92 class _BinaryOperation>
94 __partial_sum(_InputIterator __first, _InputIterator __last,
95 _OutputIterator __result, _Tp*, _BinaryOperation __binary_op);
98 template <class _InputIterator, class _OutputIterator>
99 inline _OutputIterator
100 partial_sum(_InputIterator __first, _InputIterator __last,
101 _OutputIterator __result) {
102 return __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
103 __plus(_STLP_VALUE_TYPE(__first, _InputIterator)));
106 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
107 inline _OutputIterator
108 partial_sum(_InputIterator __first, _InputIterator __last,
109 _OutputIterator __result, _BinaryOperation __binary_op) {
110 return __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
115 template <class _InputIterator, class _OutputIterator, class _Tp,
116 class _BinaryOperation>
118 __adjacent_difference(_InputIterator __first, _InputIterator __last,
119 _OutputIterator __result, _Tp*,
120 _BinaryOperation __binary_op);
122 template <class _InputIterator, class _OutputIterator>
123 inline _OutputIterator
124 adjacent_difference(_InputIterator __first,
125 _InputIterator __last, _OutputIterator __result) {
126 return __adjacent_difference(__first, __last, __result,
127 _STLP_VALUE_TYPE(__first, _InputIterator),
128 __minus(_STLP_VALUE_TYPE(__first, _InputIterator)));
131 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
133 adjacent_difference(_InputIterator __first, _InputIterator __last,
134 _OutputIterator __result, _BinaryOperation __binary_op) {
135 return __adjacent_difference(__first, __last, __result,
136 _STLP_VALUE_TYPE(__first, _InputIterator),
140 template <class _Tp, class _Integer, class _MonoidOperation>
141 _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr);
143 # ifndef _STLP_NO_EXTENSIONS
145 // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
146 // is required to be associative, but not necessarily commutative.
148 template <class _Tp, class _Integer>
149 inline _Tp __power(_Tp __x, _Integer __n)
151 return __power(__x, __n, multiplies<_Tp>());
154 // Alias for the internal name __power. Note that power is an extension,
155 // not part of the C++ standard.
156 template <class _Tp, class _Integer, class _MonoidOperation>
157 inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
158 return __power(__x, __n, __opr);
162 template <class _Tp, class _Integer>
163 inline _Tp power(_Tp __x, _Integer __n) {
164 return __power(__x, __n, multiplies<_Tp>());
167 // iota is not part of the C++ standard. It is an extension.
169 template <class _ForwardIterator, class _Tp>
172 iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __val)
174 _STLP_DEBUG_CHECK(__check_range(__first, __last))
175 while (__first != __last)
176 *__first++ = __val++;
182 # if !defined (_STLP_LINK_TIME_INSTANTIATION)
183 # include <stl/_numeric.c>
186 #endif /* _STLP_INTERNAL_NUMERIC_H */