blob: 4600890d6169b4f3892fbcf43a33b6eb1640bda2 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
3//
4// ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_NUMERIC
12#define _LIBCPP_NUMERIC
13
14/*
15 numeric synopsis
16
17namespace std
18{
19
20template <class InputIterator, class T>
21 T
22 accumulate(InputIterator first, InputIterator last, T init);
23
24template <class InputIterator, class T, class BinaryOperation>
25 T
26 accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27
28template <class InputIterator1, class InputIterator2, class T>
29 T
30 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
31
32template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
33 T
34 inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
35 T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
36
37template <class InputIterator, class OutputIterator>
38 OutputIterator
39 partial_sum(InputIterator first, InputIterator last, OutputIterator result);
40
41template <class InputIterator, class OutputIterator, class BinaryOperation>
42 OutputIterator
43 partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
44
45template <class InputIterator, class OutputIterator>
46 OutputIterator
47 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
48
49template <class InputIterator, class OutputIterator, class BinaryOperation>
50 OutputIterator
51 adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
52
53} // std
54
55*/
56
57#include <__config>
58#include <iterator>
59
60#pragma GCC system_header
61
62_LIBCPP_BEGIN_NAMESPACE_STD
63
64template <class _InputIterator, class _Tp>
65inline _LIBCPP_INLINE_VISIBILITY
66_Tp
67accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
68{
69 for (; __first != __last; ++__first)
70 __init = __init + *__first;
71 return __init;
72}
73
74template <class _InputIterator, class _Tp, class _BinaryOperation>
75inline _LIBCPP_INLINE_VISIBILITY
76_Tp
77accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
78{
79 for (; __first != __last; ++__first)
80 __init = __binary_op(__init, *__first);
81 return __init;
82}
83
84template <class _InputIterator1, class _InputIterator2, class _Tp>
85inline _LIBCPP_INLINE_VISIBILITY
86_Tp
87inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
88{
89 for (; __first1 != __last1; ++__first1, ++__first2)
90 __init = __init + *__first1 * *__first2;
91 return __init;
92}
93
94template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
95inline _LIBCPP_INLINE_VISIBILITY
96_Tp
97inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
98 _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
99{
100 for (; __first1 != __last1; ++__first1, ++__first2)
101 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
102 return __init;
103}
104
105template <class _InputIterator, class _OutputIterator>
106inline _LIBCPP_INLINE_VISIBILITY
107_OutputIterator
108partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
109{
110 if (__first != __last)
111 {
112 typename iterator_traits<_InputIterator>::value_type __t(*__first);
113 *__result = __t;
114 for (++__first, ++__result; __first != __last; ++__first, ++__result)
115 {
116 __t = __t + *__first;
117 *__result = __t;
118 }
119 }
120 return __result;
121}
122
123template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
124inline _LIBCPP_INLINE_VISIBILITY
125_OutputIterator
126partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
127 _BinaryOperation __binary_op)
128{
129 if (__first != __last)
130 {
131 typename iterator_traits<_InputIterator>::value_type __t(*__first);
132 *__result = __t;
133 for (++__first, ++__result; __first != __last; ++__first, ++__result)
134 {
135 __t = __binary_op(__t, *__first);
136 *__result = __t;
137 }
138 }
139 return __result;
140}
141
142template <class _InputIterator, class _OutputIterator>
143inline _LIBCPP_INLINE_VISIBILITY
144_OutputIterator
145adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
146{
147 if (__first != __last)
148 {
149 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
150 *__result = __t1;
151 for (++__first, ++__result; __first != __last; ++__first, ++__result)
152 {
153 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
154 *__result = __t2 - __t1;
155 __t1 = __t2;
156 }
157 }
158 return __result;
159}
160
161template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
162inline _LIBCPP_INLINE_VISIBILITY
163_OutputIterator
164adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
165 _BinaryOperation __binary_op)
166{
167 if (__first != __last)
168 {
169 typename iterator_traits<_InputIterator>::value_type __t1(*__first);
170 *__result = __t1;
171 for (++__first, ++__result; __first != __last; ++__first, ++__result)
172 {
173 typename iterator_traits<_InputIterator>::value_type __t2(*__first);
174 *__result = __binary_op(__t2, __t1);
175 __t1 = __t2;
176 }
177 }
178 return __result;
179}
180
181_LIBCPP_END_NAMESPACE_STD
182
183#endif // _LIBCPP_NUMERIC