1/*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
8 *
9 * Copyright (c) 1999
10 * Boris Fomitchev
11 *
12 * This material is provided "as is", with absolutely no warranty expressed
13 * or implied. Any use is at your own risk.
14 *
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.
20 *
21 */
22
23/* NOTE: This is an internal header file, included by other STL headers.
24 *   You should not attempt to use it directly.
25 */
26
27#ifndef _STLP_INTERNAL_NUMERIC_H
28#define _STLP_INTERNAL_NUMERIC_H
29
30#ifndef _STLP_INTERNAL_FUNCTION_BASE_H
31# include <stl/_function_base.h>
32#endif
33
34#ifndef _STLP_INTERNAL_ITERATOR_BASE_H
35#  include <stl/_iterator_base.h>
36#endif
37
38_STLP_BEGIN_NAMESPACE
39
40template <class _InputIterator, class _Tp>
41_STLP_INLINE_LOOP
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;
46  return _Init;
47}
48
49template <class _InputIterator, class _Tp, class _BinaryOperation>
50_STLP_INLINE_LOOP
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);
56  return _Init;
57}
58
59template <class _InputIterator1, class _InputIterator2, class _Tp>
60_STLP_INLINE_LOOP
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);
66  return _Init;
67}
68
69template <class _InputIterator1, class _InputIterator2, class _Tp,
70          class _BinaryOperation1, class _BinaryOperation2>
71_STLP_INLINE_LOOP
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));
79  return _Init;
80}
81
82_STLP_MOVE_TO_PRIV_NAMESPACE
83
84template <class _InputIterator, class _OutputIterator, class _Tp,
85          class _BinaryOperation>
86_OutputIterator
87__partial_sum(_InputIterator __first, _InputIterator __last,
88              _OutputIterator __result, _Tp*, _BinaryOperation __binary_op);
89
90_STLP_MOVE_TO_STD_NAMESPACE
91
92template <class _InputIterator, class _OutputIterator>
93inline _OutputIterator
94partial_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)));
98}
99
100template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
101inline _OutputIterator
102partial_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),
105                                  __binary_op);
106}
107
108_STLP_MOVE_TO_PRIV_NAMESPACE
109
110template <class _InputIterator, class _OutputIterator, class _Tp,
111          class _BinaryOperation>
112_OutputIterator
113__adjacent_difference(_InputIterator __first, _InputIterator __last,
114                      _OutputIterator __result, _Tp*,
115                      _BinaryOperation __binary_op);
116
117_STLP_MOVE_TO_STD_NAMESPACE
118
119template <class _InputIterator, class _OutputIterator>
120inline _OutputIterator
121adjacent_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)));
126}
127
128template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129_OutputIterator
130adjacent_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),
134                                          __binary_op);
135}
136
137_STLP_MOVE_TO_PRIV_NAMESPACE
138
139template <class _Tp, class _Integer, class _MonoidOperation>
140_Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr);
141
142_STLP_MOVE_TO_STD_NAMESPACE
143
144#if !defined (_STLP_NO_EXTENSIONS)
145
146// Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
147// is required to be associative, but not necessarily commutative.
148
149_STLP_MOVE_TO_PRIV_NAMESPACE
150
151template <class _Tp, class _Integer>
152inline _Tp __power(_Tp __x, _Integer __n) {
153  return __power(__x, __n, multiplies<_Tp>());
154}
155
156_STLP_MOVE_TO_STD_NAMESPACE
157
158// Alias for the internal name __power.  Note that power is an extension,
159// not part of the C++ standard.
160template <class _Tp, class _Integer, class _MonoidOperation>
161inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
162  return _STLP_PRIV __power(__x, __n, __opr);
163}
164
165template <class _Tp, class _Integer>
166inline _Tp power(_Tp __x, _Integer __n) {
167  return _STLP_PRIV __power(__x, __n, multiplies<_Tp>());
168}
169
170// iota is not part of the C++ standard.  It is an extension.
171
172template <class _ForwardIterator, class _Tp>
173_STLP_INLINE_LOOP
174void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __val) {
175  _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
176  while (__first != __last)
177    *__first++ = __val++;
178}
179#endif
180
181_STLP_END_NAMESPACE
182
183#if !defined (_STLP_LINK_TIME_INSTANTIATION)
184#  include <stl/_numeric.c>
185#endif
186
187#endif /* _STLP_INTERNAL_NUMERIC_H */
188
189// Local Variables:
190// mode:C++
191// End:
192