00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00061 #ifndef _STL_NUMERIC_H
00062 #define _STL_NUMERIC_H 1
00063
00064 #include <debug/debug.h>
00065
00066 namespace std
00067 {
00068
00080 template<typename _InputIterator, typename _Tp>
00081 _Tp
00082 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
00083 {
00084
00085 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00086 __glibcxx_requires_valid_range(__first, __last);
00087
00088 for ( ; __first != __last; ++__first)
00089 __init = __init + *__first;
00090 return __init;
00091 }
00092
00106 template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
00107 _Tp
00108 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
00109 _BinaryOperation __binary_op)
00110 {
00111
00112 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00113 __glibcxx_requires_valid_range(__first, __last);
00114
00115 for ( ; __first != __last; ++__first)
00116 __init = __binary_op(__init, *__first);
00117 return __init;
00118 }
00119
00134 template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
00135 _Tp
00136 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00137 _InputIterator2 __first2, _Tp __init)
00138 {
00139
00140 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
00141 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
00142 __glibcxx_requires_valid_range(__first1, __last1);
00143
00144 for ( ; __first1 != __last1; ++__first1, ++__first2)
00145 __init = __init + (*__first1 * *__first2);
00146 return __init;
00147 }
00148
00165 template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
00166 typename _BinaryOperation1, typename _BinaryOperation2>
00167 _Tp
00168 inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
00169 _InputIterator2 __first2, _Tp __init,
00170 _BinaryOperation1 __binary_op1,
00171 _BinaryOperation2 __binary_op2)
00172 {
00173
00174 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
00175 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
00176 __glibcxx_requires_valid_range(__first1, __last1);
00177
00178 for ( ; __first1 != __last1; ++__first1, ++__first2)
00179 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
00180 return __init;
00181 }
00182
00197 template<typename _InputIterator, typename _OutputIterator>
00198 _OutputIterator
00199 partial_sum(_InputIterator __first, _InputIterator __last,
00200 _OutputIterator __result)
00201 {
00202 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00203
00204
00205 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00206 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00207 __glibcxx_requires_valid_range(__first, __last);
00208
00209 if (__first == __last) return __result;
00210 *__result = *__first;
00211 _ValueType __value = *__first;
00212 while (++__first != __last) {
00213 __value = __value + *__first;
00214 *++__result = __value;
00215 }
00216 return ++__result;
00217 }
00218
00233 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00234 _OutputIterator
00235 partial_sum(_InputIterator __first, _InputIterator __last,
00236 _OutputIterator __result, _BinaryOperation __binary_op)
00237 {
00238 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00239
00240
00241 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00242 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00243 __glibcxx_requires_valid_range(__first, __last);
00244
00245 if (__first == __last) return __result;
00246 *__result = *__first;
00247 _ValueType __value = *__first;
00248 while (++__first != __last) {
00249 __value = __binary_op(__value, *__first);
00250 *++__result = __value;
00251 }
00252 return ++__result;
00253 }
00254
00266 template<typename _InputIterator, typename _OutputIterator>
00267 _OutputIterator
00268 adjacent_difference(_InputIterator __first,
00269 _InputIterator __last, _OutputIterator __result)
00270 {
00271 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00272
00273
00274 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00275 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00276 __glibcxx_requires_valid_range(__first, __last);
00277
00278 if (__first == __last) return __result;
00279 *__result = *__first;
00280 _ValueType __value = *__first;
00281 while (++__first != __last) {
00282 _ValueType __tmp = *__first;
00283 *++__result = __tmp - __value;
00284 __value = __tmp;
00285 }
00286 return ++__result;
00287 }
00288
00301 template<typename _InputIterator, typename _OutputIterator, typename _BinaryOperation>
00302 _OutputIterator
00303 adjacent_difference(_InputIterator __first, _InputIterator __last,
00304 _OutputIterator __result, _BinaryOperation __binary_op)
00305 {
00306 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00307
00308
00309 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00310 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, _ValueType>)
00311 __glibcxx_requires_valid_range(__first, __last);
00312
00313 if (__first == __last) return __result;
00314 *__result = *__first;
00315 _ValueType __value = *__first;
00316 while (++__first != __last) {
00317 _ValueType __tmp = *__first;
00318 *++__result = __binary_op(__tmp, __value);
00319 __value = __tmp;
00320 }
00321 return ++__result;
00322 }
00323
00324 }
00325
00326 #endif