1227825Stheraven// -*- C++ -*-
2227825Stheraven//===---------------------------- numeric ---------------------------------===//
3227825Stheraven//
4227825Stheraven//                     The LLVM Compiler Infrastructure
5227825Stheraven//
6227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
7227825Stheraven// Source Licenses. See LICENSE.TXT for details.
8227825Stheraven//
9227825Stheraven//===----------------------------------------------------------------------===//
10227825Stheraven
11227825Stheraven#ifndef _LIBCPP_NUMERIC
12227825Stheraven#define _LIBCPP_NUMERIC
13227825Stheraven
14227825Stheraven/*
15227825Stheraven    numeric synopsis
16227825Stheraven
17227825Stheravennamespace std
18227825Stheraven{
19227825Stheraven
20227825Stheraventemplate <class InputIterator, class T>
21227825Stheraven    T
22227825Stheraven    accumulate(InputIterator first, InputIterator last, T init);
23227825Stheraven
24227825Stheraventemplate <class InputIterator, class T, class BinaryOperation>
25227825Stheraven    T
26227825Stheraven    accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
27227825Stheraven
28227825Stheraventemplate <class InputIterator1, class InputIterator2, class T>
29227825Stheraven    T
30227825Stheraven    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init);
31227825Stheraven
32227825Stheraventemplate <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
33227825Stheraven    T
34227825Stheraven    inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
35227825Stheraven                  T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
36227825Stheraven
37227825Stheraventemplate <class InputIterator, class OutputIterator>
38227825Stheraven    OutputIterator
39227825Stheraven    partial_sum(InputIterator first, InputIterator last, OutputIterator result);
40227825Stheraven
41227825Stheraventemplate <class InputIterator, class OutputIterator, class BinaryOperation>
42227825Stheraven    OutputIterator
43227825Stheraven    partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
44227825Stheraven
45227825Stheraventemplate <class InputIterator, class OutputIterator>
46227825Stheraven    OutputIterator
47227825Stheraven    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result);
48227825Stheraven
49227825Stheraventemplate <class InputIterator, class OutputIterator, class BinaryOperation>
50227825Stheraven    OutputIterator
51227825Stheraven    adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
52227825Stheraven
53227825Stheraventemplate <class ForwardIterator, class T>
54227825Stheraven    void iota(ForwardIterator first, ForwardIterator last, T value);
55227825Stheraven
56227825Stheraven}  // std
57227825Stheraven
58227825Stheraven*/
59227825Stheraven
60227825Stheraven#include <__config>
61227825Stheraven#include <iterator>
62227825Stheraven
63227825Stheraven#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
64227825Stheraven#pragma GCC system_header
65227825Stheraven#endif
66227825Stheraven
67227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
68227825Stheraven
69227825Stheraventemplate <class _InputIterator, class _Tp>
70227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
71227825Stheraven_Tp
72227825Stheravenaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
73227825Stheraven{
74227825Stheraven    for (; __first != __last; ++__first)
75227825Stheraven        __init = __init + *__first;
76227825Stheraven    return __init;
77227825Stheraven}
78227825Stheraven
79227825Stheraventemplate <class _InputIterator, class _Tp, class _BinaryOperation>
80227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
81227825Stheraven_Tp
82227825Stheravenaccumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
83227825Stheraven{
84227825Stheraven    for (; __first != __last; ++__first)
85227825Stheraven        __init = __binary_op(__init, *__first);
86227825Stheraven    return __init;
87227825Stheraven}
88227825Stheraven
89227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Tp>
90227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
91227825Stheraven_Tp
92227825Stheraveninner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
93227825Stheraven{
94278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2)
95227825Stheraven        __init = __init + *__first1 * *__first2;
96227825Stheraven    return __init;
97227825Stheraven}
98227825Stheraven
99227825Stheraventemplate <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
100227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
101227825Stheraven_Tp
102227825Stheraveninner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
103227825Stheraven              _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
104227825Stheraven{
105278724Sdim    for (; __first1 != __last1; ++__first1, (void) ++__first2)
106227825Stheraven        __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
107227825Stheraven    return __init;
108227825Stheraven}
109227825Stheraven
110227825Stheraventemplate <class _InputIterator, class _OutputIterator>
111227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
112227825Stheraven_OutputIterator
113227825Stheravenpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
114227825Stheraven{
115227825Stheraven    if (__first != __last)
116227825Stheraven    {
117227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t(*__first);
118227825Stheraven        *__result = __t;
119278724Sdim        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
120227825Stheraven        {
121227825Stheraven            __t = __t + *__first;
122227825Stheraven            *__result = __t;
123227825Stheraven        }
124227825Stheraven    }
125227825Stheraven    return __result;
126227825Stheraven}
127227825Stheraven
128227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
130227825Stheraven_OutputIterator
131227825Stheravenpartial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
132227825Stheraven              _BinaryOperation __binary_op)
133227825Stheraven{
134227825Stheraven    if (__first != __last)
135227825Stheraven    {
136227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t(*__first);
137227825Stheraven        *__result = __t;
138278724Sdim        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
139227825Stheraven        {
140227825Stheraven            __t = __binary_op(__t, *__first);
141227825Stheraven            *__result = __t;
142227825Stheraven        }
143227825Stheraven    }
144227825Stheraven    return __result;
145227825Stheraven}
146227825Stheraven
147227825Stheraventemplate <class _InputIterator, class _OutputIterator>
148227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
149227825Stheraven_OutputIterator
150227825Stheravenadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
151227825Stheraven{
152227825Stheraven    if (__first != __last)
153227825Stheraven    {
154227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
155227825Stheraven        *__result = __t1;
156278724Sdim        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
157227825Stheraven        {
158227825Stheraven            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
159227825Stheraven            *__result = __t2 - __t1;
160262801Sdim            __t1 = _VSTD::move(__t2);
161227825Stheraven        }
162227825Stheraven    }
163227825Stheraven    return __result;
164227825Stheraven}
165227825Stheraven
166227825Stheraventemplate <class _InputIterator, class _OutputIterator, class _BinaryOperation>
167227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
168227825Stheraven_OutputIterator
169227825Stheravenadjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
170227825Stheraven                      _BinaryOperation __binary_op)
171227825Stheraven{
172227825Stheraven    if (__first != __last)
173227825Stheraven    {
174227825Stheraven        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
175227825Stheraven        *__result = __t1;
176278724Sdim        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
177227825Stheraven        {
178227825Stheraven            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
179227825Stheraven            *__result = __binary_op(__t2, __t1);
180262801Sdim            __t1 = _VSTD::move(__t2);
181227825Stheraven        }
182227825Stheraven    }
183227825Stheraven    return __result;
184227825Stheraven}
185227825Stheraven
186227825Stheraventemplate <class _ForwardIterator, class _Tp>
187227825Stheraveninline _LIBCPP_INLINE_VISIBILITY
188227825Stheravenvoid
189227825Stheraveniota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
190227825Stheraven{
191278724Sdim    for (; __first != __last; ++__first, (void) ++__value_)
192227825Stheraven        *__first = __value_;
193227825Stheraven}
194227825Stheraven
195227825Stheraven_LIBCPP_END_NAMESPACE_STD
196227825Stheraven
197227825Stheraven#endif  // _LIBCPP_NUMERIC
198