Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
numeric.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_NUMERIC_HPP
9#define ADOBE_NUMERIC_HPP
10
11#include <adobe/config.hpp>
12
13#include <functional>
14#include <iterator>
15#include <numeric>
16
17#include <boost/range.hpp>
18
19namespace adobe {
31
36
37
38/**************************************************************************************************/
39
43template <typename ForwardIterator>
44ForwardIterator max_adjacent_difference(ForwardIterator first, ForwardIterator last) {
45 typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
46
47 ForwardIterator result(first);
48
49 if (first == last)
50 return result;
51
52 ForwardIterator previous(first);
53 ++first;
54
55 if (first == last)
56 return result;
57
58 value_type result_difference(*first - *previous);
59 previous = first;
60 ++first;
61
62 while (first != last) {
63 value_type difference(*first - *previous);
64
65 if (result_difference < difference) {
66 result_difference = difference;
67 result = previous;
68 }
69 previous = first;
70 ++first;
71 }
72
73 return result;
74}
75
79template <typename ForwardRange>
80inline typename boost::range_iterator<ForwardRange>::type
81max_adjacent_difference(ForwardRange& range) {
82 return adobe::max_adjacent_difference(boost::begin(range), boost::end(range));
83}
84
88template <typename ForwardRange>
89inline typename boost::range_const_iterator<ForwardRange>::type
90max_adjacent_difference(const ForwardRange& range) {
91 return adobe::max_adjacent_difference(boost::begin(range), boost::end(range));
92}
93
94/**************************************************************************************************/
95
96// standard calls using bind and range
97
98/**************************************************************************************************/
99
104
110template <typename InputRange, typename T>
111inline T accumulate(const InputRange& range, T init) {
112 return std::accumulate(boost::begin(range), boost::end(range), init);
113}
114
120template <typename InputIterator, typename T, typename BinaryOperation>
121inline T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op) {
122 return std::accumulate(first, last, init,
123 std::bind(binary_op, std::placeholders::_1, std::placeholders::_2));
124}
125
131template <typename InputRange, typename T, typename BinaryOperation>
132inline T accumulate(const InputRange& range, T init, BinaryOperation binary_op) {
133 return adobe::accumulate(boost::begin(range), boost::end(range), init, binary_op);
134}
135
140
146template <typename InputRange, typename InputIterator, typename T>
147inline T inner_product(const InputRange& range, InputIterator first, T init) {
148 return std::inner_product(boost::begin(range), boost::end(range), first, init);
149}
150
156template <typename InputIterator1, typename InputIterator2, typename T, typename BinaryOperation1,
157 typename BinaryOperation2>
158inline T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init,
159 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2) {
160 return std::inner_product(first1, last1, first2, init,
161 std::bind(binary_op1, std::placeholders::_1, std::placeholders::_2),
162 std::bind(binary_op2, std::placeholders::_1, std::placeholders::_2));
163}
164
170template <typename InputRange, typename InputIterator, typename T, typename BinaryOperation1,
171 typename BinaryOperation2>
172inline T inner_product(const InputRange& range, InputIterator first, T init,
173 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2) {
174 return adobe::inner_product(boost::begin(range), boost::end(range), first, init, binary_op1,
175 binary_op2);
176}
177
182
188template <typename InputRange, typename OutputIterator>
189inline OutputIterator partial_sum(const InputRange& range, OutputIterator result) {
190 return std::partial_sum(boost::begin(range), boost::end(range), result);
191}
192
198template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
199inline OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result,
200 BinaryOperation binary_op) {
201 return std::partial_sum(first, last, result,
202 std::bind(binary_op, std::placeholders::_1, std::placeholders::_2));
203}
204
210template <typename InputRange, typename OutputIterator, typename BinaryOperation>
211inline OutputIterator partial_sum(const InputRange& range, OutputIterator result,
212 BinaryOperation binary_op) {
213 return adobe::partial_sum(boost::begin(range), boost::end(range), result, binary_op);
214}
215
225template <typename InputRange, typename OutputIterator>
226inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result) {
227 return std::adjacent_difference(boost::begin(range), boost::end(range), result);
228}
229
235template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
236inline OutputIterator adjacent_difference(InputIterator first, InputIterator last,
237 OutputIterator result, BinaryOperation binary_op) {
238 return std::adjacent_difference(
239 first, last, result, std::bind(binary_op, std::placeholders::_1, std::placeholders::_2));
240}
241
247template <typename InputRange, typename OutputIterator, typename BinaryOperation>
248inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result,
249 BinaryOperation binary_op) {
250 return adobe::adjacent_difference(boost::begin(range), boost::end(range), result, binary_op);
251}
252
253/**************************************************************************************************/
254
255} // namespace adobe
256
257/**************************************************************************************************/
258
259#endif
260
261/**************************************************************************************************/
T accumulate(const InputRange &range, T init)
Definition numeric.hpp:111
OutputIterator adjacent_difference(const InputRange &range, OutputIterator result)
Definition numeric.hpp:226
T inner_product(const InputRange &range, InputIterator first, T init)
Definition numeric.hpp:147
ForwardIterator max_adjacent_difference(ForwardIterator first, ForwardIterator last)
Definition numeric.hpp:44
OutputIterator partial_sum(const InputRange &range, OutputIterator result)
Definition numeric.hpp:189