source: trunk/src/emx/include/Attic/cpp/stl_nume.h@ 18

Last change on this file since 18 was 18, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.2 KB
Line 
1/*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Hewlett-Packard Company makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 *
15 * Copyright (c) 1996,1997
16 * Silicon Graphics Computer Systems, Inc.
17 *
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Silicon Graphics makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
25 */
26
27/* NOTE: This is an internal header file, included by other STL headers.
28 * You should not attempt to use it directly.
29 */
30
31
32#ifndef __SGI_STL_INTERNAL_NUMERIC_H
33#define __SGI_STL_INTERNAL_NUMERIC_H
34
35__STL_BEGIN_NAMESPACE
36
37template <class InputIterator, class T>
38T accumulate(InputIterator first, InputIterator last, T init) {
39 for ( ; first != last; ++first)
40 init = init + *first;
41 return init;
42}
43
44template <class InputIterator, class T, class BinaryOperation>
45T accumulate(InputIterator first, InputIterator last, T init,
46 BinaryOperation binary_op) {
47 for ( ; first != last; ++first)
48 init = binary_op(init, *first);
49 return init;
50}
51
52template <class InputIterator1, class InputIterator2, class T>
53T inner_product(InputIterator1 first1, InputIterator1 last1,
54 InputIterator2 first2, T init) {
55 for ( ; first1 != last1; ++first1, ++first2)
56 init = init + (*first1 * *first2);
57 return init;
58}
59
60template <class InputIterator1, class InputIterator2, class T,
61 class BinaryOperation1, class BinaryOperation2>
62T inner_product(InputIterator1 first1, InputIterator1 last1,
63 InputIterator2 first2, T init, BinaryOperation1 binary_op1,
64 BinaryOperation2 binary_op2) {
65 for ( ; first1 != last1; ++first1, ++first2)
66 init = binary_op1(init, binary_op2(*first1, *first2));
67 return init;
68}
69
70template <class InputIterator, class OutputIterator, class T>
71OutputIterator __partial_sum(InputIterator first, InputIterator last,
72 OutputIterator result, T*) {
73 T value = *first;
74 while (++first != last) {
75 value = value + *first;
76 *++result = value;
77 }
78 return ++result;
79}
80
81template <class InputIterator, class OutputIterator>
82OutputIterator partial_sum(InputIterator first, InputIterator last,
83 OutputIterator result) {
84 if (first == last) return result;
85 *result = *first;
86 return __partial_sum(first, last, result, value_type(first));
87}
88
89template <class InputIterator, class OutputIterator, class T,
90 class BinaryOperation>
91OutputIterator __partial_sum(InputIterator first, InputIterator last,
92 OutputIterator result, T*,
93 BinaryOperation binary_op) {
94 T value = *first;
95 while (++first != last) {
96 value = binary_op(value, *first);
97 *++result = value;
98 }
99 return ++result;
100}
101
102template <class InputIterator, class OutputIterator, class BinaryOperation>
103OutputIterator partial_sum(InputIterator first, InputIterator last,
104 OutputIterator result, BinaryOperation binary_op) {
105 if (first == last) return result;
106 *result = *first;
107 return __partial_sum(first, last, result, value_type(first), binary_op);
108}
109
110template <class InputIterator, class OutputIterator, class T>
111OutputIterator __adjacent_difference(InputIterator first, InputIterator last,
112 OutputIterator result, T*) {
113 T value = *first;
114 while (++first != last) {
115 T tmp = *first;
116 *++result = tmp - value;
117 value = tmp;
118 }
119 return ++result;
120}
121
122template <class InputIterator, class OutputIterator>
123OutputIterator adjacent_difference(InputIterator first, InputIterator last,
124 OutputIterator result) {
125 if (first == last) return result;
126 *result = *first;
127 return __adjacent_difference(first, last, result, value_type(first));
128}
129
130template <class InputIterator, class OutputIterator, class T,
131 class BinaryOperation>
132OutputIterator __adjacent_difference(InputIterator first, InputIterator last,
133 OutputIterator result, T*,
134 BinaryOperation binary_op) {
135 T value = *first;
136 while (++first != last) {
137 T tmp = *first;
138 *++result = binary_op(tmp, value);
139 value = tmp;
140 }
141 return ++result;
142}
143
144template <class InputIterator, class OutputIterator, class BinaryOperation>
145OutputIterator adjacent_difference(InputIterator first, InputIterator last,
146 OutputIterator result,
147 BinaryOperation binary_op) {
148 if (first == last) return result;
149 *result = *first;
150 return __adjacent_difference(first, last, result, value_type(first),
151 binary_op);
152}
153
154// Returns x ** n, where n >= 0. Note that "multiplication"
155// is required to be associative, but not necessarily commutative.
156
157template <class T, class Integer, class MonoidOperation>
158T power(T x, Integer n, MonoidOperation op) {
159 if (n == 0)
160 return identity_element(op);
161 else {
162 while ((n & 1) == 0) {
163 n >>= 1;
164 x = op(x, x);
165 }
166
167 T result = x;
168 n >>= 1;
169 while (n != 0) {
170 x = op(x, x);
171 if ((n & 1) != 0)
172 result = op(result, x);
173 n >>= 1;
174 }
175 return result;
176 }
177}
178
179template <class T, class Integer>
180inline T power(T x, Integer n) {
181 return power(x, n, multiplies<T>());
182}
183
184
185template <class ForwardIterator, class T>
186void iota(ForwardIterator first, ForwardIterator last, T value) {
187 while (first != last) *first++ = value++;
188}
189
190__STL_END_NAMESPACE
191
192#endif /* __SGI_STL_INTERNAL_NUMERIC_H */
193
194// Local Variables:
195// mode:C++
196// End:
Note: See TracBrowser for help on using the repository browser.