xref: /reactos/sdk/include/c++/stlport/stl/_numeric.c (revision 8a978a17)
1 /*
2  *
3  *
4  * Copyright (c) 1994
5  * Hewlett-Packard Company
6  *
7  * Copyright (c) 1996,1997
8  * Silicon Graphics Computer Systems, Inc.
9  *
10  * Copyright (c) 1997
11  * Moscow Center for SPARC Technology
12  *
13  * Copyright (c) 1999
14  * Boris Fomitchev
15  *
16  * This material is provided "as is", with absolutely no warranty expressed
17  * or implied. Any use is at your own risk.
18  *
19  * Permission to use or copy this software for any purpose is hereby granted
20  * without fee, provided the above notices are retained on all copies.
21  * Permission to modify the code and to distribute modified code is granted,
22  * provided the above notices are retained, and a notice that the code was
23  * modified is included with the above copyright notice.
24  *
25  */
26 #ifndef _STLP_NUMERIC_C
27 #define _STLP_NUMERIC_C
28 
29 #ifndef _STLP_INTERNAL_NUMERIC_H
30 # include <stl/_numeric.h>
31 #endif
32 
33 _STLP_BEGIN_NAMESPACE
34 
35 _STLP_MOVE_TO_PRIV_NAMESPACE
36 
37 template <class _InputIterator, class _OutputIterator, class _Tp,
38           class _BinaryOperation>
39 _OutputIterator
40 __partial_sum(_InputIterator __first, _InputIterator __last,
41               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op) {
42   _STLP_DEBUG_CHECK(__check_range(__first, __last))
43   if (__first == __last) return __result;
44   *__result = *__first;
45 
46   _Tp __val = *__first;
47   while (++__first != __last) {
48     __val = __binary_op(__val, *__first);
49     *++__result = __val;
50   }
51   return ++__result;
52 }
53 
54 template <class _InputIterator, class _OutputIterator, class _Tp,
55           class _BinaryOperation>
56 _OutputIterator
57 __adjacent_difference(_InputIterator __first, _InputIterator __last,
58                       _OutputIterator __result, _Tp*,
59                       _BinaryOperation __binary_op) {
60   _STLP_DEBUG_CHECK(__check_range(__first, __last))
61   if (__first == __last) return __result;
62   *__result = *__first;
63   _Tp __val = *__first;
64   while (++__first != __last) {
65     _Tp __tmp = *__first;
66     *++__result = __binary_op(__tmp, __val);
67     __val = __tmp;
68   }
69   return ++__result;
70 }
71 
72 
73 template <class _Tp, class _Integer, class _MonoidOperation>
74 _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
75   _STLP_MPWFIX_TRY
76   if (__n == 0)
77     return __identity_element(__opr);
78   else {
79     while ((__n & 1) == 0) {
80       __n >>= 1;
81       __x = __opr(__x, __x);
82     }
83     _Tp __result = __x;
84   _STLP_MPWFIX_TRY
85     __n >>= 1;
86     while (__n != 0) {
87       __x = __opr(__x, __x);
88       if ((__n & 1) != 0)
89         __result = __opr(__result, __x);
90       __n >>= 1;
91     }
92     return __result;
93   _STLP_MPWFIX_CATCH
94   }
95   _STLP_MPWFIX_CATCH_ACTION(__x = _Tp())
96 }
97 
98 _STLP_MOVE_TO_STD_NAMESPACE
99 
100 _STLP_END_NAMESPACE
101 
102 #endif /*  _STLP_NUMERIC_C */
103 
104 // Local Variables:
105 // mode:C++
106 // End:
107