• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- C++ -*-
2//===---------------------------- numeric ---------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. 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
53template <class ForwardIterator, class T>
54    void iota(ForwardIterator first, ForwardIterator last, T value);
55
56}  // std
57
58*/
59
60#include <__config>
61#include <iterator>
62
63#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
64#pragma GCC system_header
65#endif
66
67_LIBCPP_BEGIN_NAMESPACE_STD
68
69template <class _InputIterator, class _Tp>
70inline _LIBCPP_INLINE_VISIBILITY
71_Tp
72accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
73{
74    for (; __first != __last; ++__first)
75        __init = __init + *__first;
76    return __init;
77}
78
79template <class _InputIterator, class _Tp, class _BinaryOperation>
80inline _LIBCPP_INLINE_VISIBILITY
81_Tp
82accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, _BinaryOperation __binary_op)
83{
84    for (; __first != __last; ++__first)
85        __init = __binary_op(__init, *__first);
86    return __init;
87}
88
89template <class _InputIterator1, class _InputIterator2, class _Tp>
90inline _LIBCPP_INLINE_VISIBILITY
91_Tp
92inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _Tp __init)
93{
94    for (; __first1 != __last1; ++__first1, (void) ++__first2)
95        __init = __init + *__first1 * *__first2;
96    return __init;
97}
98
99template <class _InputIterator1, class _InputIterator2, class _Tp, class _BinaryOperation1, class _BinaryOperation2>
100inline _LIBCPP_INLINE_VISIBILITY
101_Tp
102inner_product(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
103              _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
104{
105    for (; __first1 != __last1; ++__first1, (void) ++__first2)
106        __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
107    return __init;
108}
109
110template <class _InputIterator, class _OutputIterator>
111inline _LIBCPP_INLINE_VISIBILITY
112_OutputIterator
113partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
114{
115    if (__first != __last)
116    {
117        typename iterator_traits<_InputIterator>::value_type __t(*__first);
118        *__result = __t;
119        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
120        {
121            __t = __t + *__first;
122            *__result = __t;
123        }
124    }
125    return __result;
126}
127
128template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
129inline _LIBCPP_INLINE_VISIBILITY
130_OutputIterator
131partial_sum(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
132              _BinaryOperation __binary_op)
133{
134    if (__first != __last)
135    {
136        typename iterator_traits<_InputIterator>::value_type __t(*__first);
137        *__result = __t;
138        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
139        {
140            __t = __binary_op(__t, *__first);
141            *__result = __t;
142        }
143    }
144    return __result;
145}
146
147template <class _InputIterator, class _OutputIterator>
148inline _LIBCPP_INLINE_VISIBILITY
149_OutputIterator
150adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result)
151{
152    if (__first != __last)
153    {
154        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
155        *__result = __t1;
156        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
157        {
158            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
159            *__result = __t2 - __t1;
160            __t1 = _VSTD::move(__t2);
161        }
162    }
163    return __result;
164}
165
166template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
167inline _LIBCPP_INLINE_VISIBILITY
168_OutputIterator
169adjacent_difference(_InputIterator __first, _InputIterator __last, _OutputIterator __result,
170                      _BinaryOperation __binary_op)
171{
172    if (__first != __last)
173    {
174        typename iterator_traits<_InputIterator>::value_type __t1(*__first);
175        *__result = __t1;
176        for (++__first, (void) ++__result; __first != __last; ++__first, (void) ++__result)
177        {
178            typename iterator_traits<_InputIterator>::value_type __t2(*__first);
179            *__result = __binary_op(__t2, __t1);
180            __t1 = _VSTD::move(__t2);
181        }
182    }
183    return __result;
184}
185
186template <class _ForwardIterator, class _Tp>
187inline _LIBCPP_INLINE_VISIBILITY
188void
189iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value_)
190{
191    for (; __first != __last; ++__first, (void) ++__value_)
192        *__first = __value_;
193}
194
195_LIBCPP_END_NAMESPACE_STD
196
197#endif  // _LIBCPP_NUMERIC
198