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,
00207 _ValueType>)
00208 __glibcxx_requires_valid_range(__first, __last);
00209
00210 if (__first == __last)
00211 return __result;
00212 _ValueType __value = *__first;
00213 *__result = __value;
00214 while (++__first != __last)
00215 {
00216 __value = __value + *__first;
00217 *++__result = __value;
00218 }
00219 return ++__result;
00220 }
00221
00236 template<typename _InputIterator, typename _OutputIterator,
00237 typename _BinaryOperation>
00238 _OutputIterator
00239 partial_sum(_InputIterator __first, _InputIterator __last,
00240 _OutputIterator __result, _BinaryOperation __binary_op)
00241 {
00242 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00243
00244
00245 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00246 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00247 _ValueType>)
00248 __glibcxx_requires_valid_range(__first, __last);
00249
00250 if (__first == __last)
00251 return __result;
00252 _ValueType __value = *__first;
00253 *__result = __value;
00254 while (++__first != __last)
00255 {
00256 __value = __binary_op(__value, *__first);
00257 *++__result = __value;
00258 }
00259 return ++__result;
00260 }
00261
00273 template<typename _InputIterator, typename _OutputIterator>
00274 _OutputIterator
00275 adjacent_difference(_InputIterator __first,
00276 _InputIterator __last, _OutputIterator __result)
00277 {
00278 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00279
00280
00281 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00282 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00283 _ValueType>)
00284 __glibcxx_requires_valid_range(__first, __last);
00285
00286 if (__first == __last)
00287 return __result;
00288 _ValueType __value = *__first;
00289 *__result = __value;
00290 while (++__first != __last)
00291 {
00292 _ValueType __tmp = *__first;
00293 *++__result = __tmp - __value;
00294 __value = __tmp;
00295 }
00296 return ++__result;
00297 }
00298
00311 template<typename _InputIterator, typename _OutputIterator,
00312 typename _BinaryOperation>
00313 _OutputIterator
00314 adjacent_difference(_InputIterator __first, _InputIterator __last,
00315 _OutputIterator __result, _BinaryOperation __binary_op)
00316 {
00317 typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
00318
00319
00320 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
00321 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
00322 _ValueType>)
00323 __glibcxx_requires_valid_range(__first, __last);
00324
00325 if (__first == __last)
00326 return __result;
00327 _ValueType __value = *__first;
00328 *__result = __value;
00329 while (++__first != __last)
00330 {
00331 _ValueType __tmp = *__first;
00332 *++__result = __binary_op(__tmp, __value);
00333 __value = __tmp;
00334 }
00335 return ++__result;
00336 }
00337
00338 }
00339
00340 #endif