xref: /reactos/sdk/include/c++/stlport/stl/_numeric.h (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  *
3*c2c66affSColin Finck  * Copyright (c) 1994
4*c2c66affSColin Finck  * Hewlett-Packard Company
5*c2c66affSColin Finck  *
6*c2c66affSColin Finck  * Copyright (c) 1996,1997
7*c2c66affSColin Finck  * Silicon Graphics Computer Systems, Inc.
8*c2c66affSColin Finck  *
9*c2c66affSColin Finck  * Copyright (c) 1999
10*c2c66affSColin Finck  * Boris Fomitchev
11*c2c66affSColin Finck  *
12*c2c66affSColin Finck  * This material is provided "as is", with absolutely no warranty expressed
13*c2c66affSColin Finck  * or implied. Any use is at your own risk.
14*c2c66affSColin Finck  *
15*c2c66affSColin Finck  * Permission to use or copy this software for any purpose is hereby granted
16*c2c66affSColin Finck  * without fee, provided the above notices are retained on all copies.
17*c2c66affSColin Finck  * Permission to modify the code and to distribute modified code is granted,
18*c2c66affSColin Finck  * provided the above notices are retained, and a notice that the code was
19*c2c66affSColin Finck  * modified is included with the above copyright notice.
20*c2c66affSColin Finck  *
21*c2c66affSColin Finck  */
22*c2c66affSColin Finck 
23*c2c66affSColin Finck /* NOTE: This is an internal header file, included by other STL headers.
24*c2c66affSColin Finck  *   You should not attempt to use it directly.
25*c2c66affSColin Finck  */
26*c2c66affSColin Finck 
27*c2c66affSColin Finck #ifndef _STLP_INTERNAL_NUMERIC_H
28*c2c66affSColin Finck #define _STLP_INTERNAL_NUMERIC_H
29*c2c66affSColin Finck 
30*c2c66affSColin Finck #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
31*c2c66affSColin Finck # include <stl/_function_base.h>
32*c2c66affSColin Finck #endif
33*c2c66affSColin Finck 
34*c2c66affSColin Finck #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
35*c2c66affSColin Finck #  include <stl/_iterator_base.h>
36*c2c66affSColin Finck #endif
37*c2c66affSColin Finck 
38*c2c66affSColin Finck _STLP_BEGIN_NAMESPACE
39*c2c66affSColin Finck 
40*c2c66affSColin Finck template <class _InputIterator, class _Tp>
41*c2c66affSColin Finck _STLP_INLINE_LOOP
accumulate(_InputIterator __first,_InputIterator __last,_Tp _Init)42*c2c66affSColin Finck _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init) {
43*c2c66affSColin Finck   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
44*c2c66affSColin Finck   for ( ; __first != __last; ++__first)
45*c2c66affSColin Finck     _Init = _Init + *__first;
46*c2c66affSColin Finck   return _Init;
47*c2c66affSColin Finck }
48*c2c66affSColin Finck 
49*c2c66affSColin Finck template <class _InputIterator, class _Tp, class _BinaryOperation>
50*c2c66affSColin Finck _STLP_INLINE_LOOP
accumulate(_InputIterator __first,_InputIterator __last,_Tp _Init,_BinaryOperation __binary_op)51*c2c66affSColin Finck _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init,
52*c2c66affSColin Finck                _BinaryOperation __binary_op) {
53*c2c66affSColin Finck   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
54*c2c66affSColin Finck   for ( ; __first != __last; ++__first)
55*c2c66affSColin Finck     _Init = __binary_op(_Init, *__first);
56*c2c66affSColin Finck   return _Init;
57*c2c66affSColin Finck }
58*c2c66affSColin Finck 
59*c2c66affSColin Finck template <class _InputIterator1, class _InputIterator2, class _Tp>
60*c2c66affSColin Finck _STLP_INLINE_LOOP
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp _Init)61*c2c66affSColin Finck _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
62*c2c66affSColin Finck                   _InputIterator2 __first2, _Tp _Init) {
63*c2c66affSColin Finck   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
64*c2c66affSColin Finck   for ( ; __first1 != __last1; ++__first1, ++__first2)
65*c2c66affSColin Finck     _Init = _Init + (*__first1 * *__first2);
66*c2c66affSColin Finck   return _Init;
67*c2c66affSColin Finck }
68*c2c66affSColin Finck 
69*c2c66affSColin Finck template <class _InputIterator1, class _InputIterator2, class _Tp,
70*c2c66affSColin Finck           class _BinaryOperation1, class _BinaryOperation2>
71*c2c66affSColin Finck _STLP_INLINE_LOOP
inner_product(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2,_Tp _Init,_BinaryOperation1 __binary_op1,_BinaryOperation2 __binary_op2)72*c2c66affSColin Finck _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
73*c2c66affSColin Finck                   _InputIterator2 __first2, _Tp _Init,
74*c2c66affSColin Finck                   _BinaryOperation1 __binary_op1,
75*c2c66affSColin Finck                   _BinaryOperation2 __binary_op2) {
76*c2c66affSColin Finck   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
77*c2c66affSColin Finck   for ( ; __first1 != __last1; ++__first1, ++__first2)
78*c2c66affSColin Finck     _Init = __binary_op1(_Init, __binary_op2(*__first1, *__first2));
79*c2c66affSColin Finck   return _Init;
80*c2c66affSColin Finck }
81*c2c66affSColin Finck 
82*c2c66affSColin Finck _STLP_MOVE_TO_PRIV_NAMESPACE
83*c2c66affSColin Finck 
84*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator, class _Tp,
85*c2c66affSColin Finck           class _BinaryOperation>
86*c2c66affSColin Finck _OutputIterator
87*c2c66affSColin Finck __partial_sum(_InputIterator __first, _InputIterator __last,
88*c2c66affSColin Finck               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op);
89*c2c66affSColin Finck 
90*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
91*c2c66affSColin Finck 
92*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator>
93*c2c66affSColin Finck inline _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result)94*c2c66affSColin Finck partial_sum(_InputIterator __first, _InputIterator __last,
95*c2c66affSColin Finck             _OutputIterator __result) {
96*c2c66affSColin Finck   return _STLP_PRIV __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
97*c2c66affSColin Finck                                   _STLP_PRIV __plus(_STLP_VALUE_TYPE(__first, _InputIterator)));
98*c2c66affSColin Finck }
99*c2c66affSColin Finck 
100*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
101*c2c66affSColin Finck inline _OutputIterator
partial_sum(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)102*c2c66affSColin Finck partial_sum(_InputIterator __first, _InputIterator __last,
103*c2c66affSColin Finck             _OutputIterator __result, _BinaryOperation __binary_op) {
104*c2c66affSColin Finck   return _STLP_PRIV __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
105*c2c66affSColin Finck                                   __binary_op);
106*c2c66affSColin Finck }
107*c2c66affSColin Finck 
108*c2c66affSColin Finck _STLP_MOVE_TO_PRIV_NAMESPACE
109*c2c66affSColin Finck 
110*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator, class _Tp,
111*c2c66affSColin Finck           class _BinaryOperation>
112*c2c66affSColin Finck _OutputIterator
113*c2c66affSColin Finck __adjacent_difference(_InputIterator __first, _InputIterator __last,
114*c2c66affSColin Finck                       _OutputIterator __result, _Tp*,
115*c2c66affSColin Finck                       _BinaryOperation __binary_op);
116*c2c66affSColin Finck 
117*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
118*c2c66affSColin Finck 
119*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator>
120*c2c66affSColin Finck inline _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result)121*c2c66affSColin Finck adjacent_difference(_InputIterator __first,
122*c2c66affSColin Finck                     _InputIterator __last, _OutputIterator __result) {
123*c2c66affSColin Finck   return _STLP_PRIV __adjacent_difference(__first, __last, __result,
124*c2c66affSColin Finck                                           _STLP_VALUE_TYPE(__first, _InputIterator),
125*c2c66affSColin Finck                                           _STLP_PRIV __minus(_STLP_VALUE_TYPE(__first, _InputIterator)));
126*c2c66affSColin Finck }
127*c2c66affSColin Finck 
128*c2c66affSColin Finck template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129*c2c66affSColin Finck _OutputIterator
adjacent_difference(_InputIterator __first,_InputIterator __last,_OutputIterator __result,_BinaryOperation __binary_op)130*c2c66affSColin Finck adjacent_difference(_InputIterator __first, _InputIterator __last,
131*c2c66affSColin Finck                     _OutputIterator __result, _BinaryOperation __binary_op) {
132*c2c66affSColin Finck   return _STLP_PRIV __adjacent_difference(__first, __last, __result,
133*c2c66affSColin Finck                                           _STLP_VALUE_TYPE(__first, _InputIterator),
134*c2c66affSColin Finck                                           __binary_op);
135*c2c66affSColin Finck }
136*c2c66affSColin Finck 
137*c2c66affSColin Finck _STLP_MOVE_TO_PRIV_NAMESPACE
138*c2c66affSColin Finck 
139*c2c66affSColin Finck template <class _Tp, class _Integer, class _MonoidOperation>
140*c2c66affSColin Finck _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr);
141*c2c66affSColin Finck 
142*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
143*c2c66affSColin Finck 
144*c2c66affSColin Finck #if !defined (_STLP_NO_EXTENSIONS)
145*c2c66affSColin Finck 
146*c2c66affSColin Finck // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
147*c2c66affSColin Finck // is required to be associative, but not necessarily commutative.
148*c2c66affSColin Finck 
149*c2c66affSColin Finck _STLP_MOVE_TO_PRIV_NAMESPACE
150*c2c66affSColin Finck 
151*c2c66affSColin Finck template <class _Tp, class _Integer>
__power(_Tp __x,_Integer __n)152*c2c66affSColin Finck inline _Tp __power(_Tp __x, _Integer __n) {
153*c2c66affSColin Finck   return __power(__x, __n, multiplies<_Tp>());
154*c2c66affSColin Finck }
155*c2c66affSColin Finck 
156*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
157*c2c66affSColin Finck 
158*c2c66affSColin Finck // Alias for the internal name __power.  Note that power is an extension,
159*c2c66affSColin Finck // not part of the C++ standard.
160*c2c66affSColin Finck template <class _Tp, class _Integer, class _MonoidOperation>
power(_Tp __x,_Integer __n,_MonoidOperation __opr)161*c2c66affSColin Finck inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
162*c2c66affSColin Finck   return _STLP_PRIV __power(__x, __n, __opr);
163*c2c66affSColin Finck }
164*c2c66affSColin Finck 
165*c2c66affSColin Finck template <class _Tp, class _Integer>
power(_Tp __x,_Integer __n)166*c2c66affSColin Finck inline _Tp power(_Tp __x, _Integer __n) {
167*c2c66affSColin Finck   return _STLP_PRIV __power(__x, __n, multiplies<_Tp>());
168*c2c66affSColin Finck }
169*c2c66affSColin Finck 
170*c2c66affSColin Finck // iota is not part of the C++ standard.  It is an extension.
171*c2c66affSColin Finck 
172*c2c66affSColin Finck template <class _ForwardIterator, class _Tp>
173*c2c66affSColin Finck _STLP_INLINE_LOOP
iota(_ForwardIterator __first,_ForwardIterator __last,_Tp __val)174*c2c66affSColin Finck void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __val) {
175*c2c66affSColin Finck   _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
176*c2c66affSColin Finck   while (__first != __last)
177*c2c66affSColin Finck     *__first++ = __val++;
178*c2c66affSColin Finck }
179*c2c66affSColin Finck #endif
180*c2c66affSColin Finck 
181*c2c66affSColin Finck _STLP_END_NAMESPACE
182*c2c66affSColin Finck 
183*c2c66affSColin Finck #if !defined (_STLP_LINK_TIME_INSTANTIATION)
184*c2c66affSColin Finck #  include <stl/_numeric.c>
185*c2c66affSColin Finck #endif
186*c2c66affSColin Finck 
187*c2c66affSColin Finck #endif /* _STLP_INTERNAL_NUMERIC_H */
188*c2c66affSColin Finck 
189*c2c66affSColin Finck // Local Variables:
190*c2c66affSColin Finck // mode:C++
191*c2c66affSColin Finck // End:
192