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 
40 template <class _InputIterator, class _Tp>
41 _STLP_INLINE_LOOP
accumulate(_InputIterator __first,_InputIterator __last,_Tp _Init)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 
49 template <class _InputIterator, class _Tp, class _BinaryOperation>
50 _STLP_INLINE_LOOP
accumulate(_InputIterator __first,_InputIterator __last,_Tp _Init,_BinaryOperation __binary_op)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 
59 template <class _InputIterator1, class _InputIterator2, class _Tp>
60 _STLP_INLINE_LOOP
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp _Init)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 
69 template <class _InputIterator1, class _InputIterator2, class _Tp,
70           class _BinaryOperation1, class _BinaryOperation2>
71 _STLP_INLINE_LOOP
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp _Init,_BinaryOperation1 __binary_op1,_BinaryOperation2 __binary_op2)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 
84 template <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 
92 template <class _InputIterator, class _OutputIterator>
93 inline _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result)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)));
98 }
99 
100 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
101 inline _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)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),
105                                   __binary_op);
106 }
107 
108 _STLP_MOVE_TO_PRIV_NAMESPACE
109 
110 template <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 
119 template <class _InputIterator, class _OutputIterator>
120 inline _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result)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)));
126 }
127 
128 template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129 _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)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),
134                                           __binary_op);
135 }
136 
137 _STLP_MOVE_TO_PRIV_NAMESPACE
138 
139 template <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 
151 template <class _Tp, class _Integer>
__power(_Tp __x,_Integer __n)152 inline _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.
160 template <class _Tp, class _Integer, class _MonoidOperation>
power(_Tp __x,_Integer __n,_MonoidOperation __opr)161 inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
162   return _STLP_PRIV __power(__x, __n, __opr);
163 }
164 
165 template <class _Tp, class _Integer>
power(_Tp __x,_Integer __n)166 inline _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 
172 template <class _ForwardIterator, class _Tp>
173 _STLP_INLINE_LOOP
iota(_ForwardIterator __first,_ForwardIterator __last,_Tp __val)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++;
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