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