stlab.adobe.com Adobe Systems Incorporated
numeric.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_NUMERIC_HPP
10 #define ADOBE_NUMERIC_HPP
11 
12 #include <adobe/config.hpp>
13 
14 #include <iterator>
15 #include <numeric>
16 
17 #include <boost/bind.hpp>
18 
19 // REVISIT (sparent): dissable warnings for unused arguments in boost 1.32.0
20 #if defined(__MWERKS__)
21 #pragma warn_unusedarg off
22 #endif
23 
24 #include <boost/range.hpp>
25 
26 #if defined(__MWERKS__)
27 #pragma warn_unusedarg reset
28 #endif
29 
30 namespace adobe {
48 /*************************************************************************************************/
49 
53 template <typename ForwardIterator>
54 ForwardIterator max_adjacent_difference(ForwardIterator first, ForwardIterator last)
55 {
56  typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
57 
58  ForwardIterator result(first);
59 
60  if (first == last) return result;
61 
62  ForwardIterator previous(first);
63  ++first;
64 
65  if (first == last) return result;
66 
67  value_type result_difference(*first - *previous);
68  previous = first; ++first;
69 
70  while (first != last)
71  {
72  value_type difference(*first - *previous);
73 
74  if (result_difference < difference)
75  {
76  result_difference = difference;
77  result = previous;
78  }
79  previous = first;
80  ++first;
81  }
82 
83  return result;
84 }
85 
89 template <typename ForwardRange>
90 inline typename boost::range_iterator<ForwardRange>::type
91  max_adjacent_difference(ForwardRange& range)
92 {
93  return adobe::max_adjacent_difference(boost::begin(range), boost::end(range));
94 }
95 
99 template <typename ForwardRange>
100 inline typename boost::range_const_iterator<ForwardRange>::type
101  max_adjacent_difference(const ForwardRange& range)
102 {
103  return adobe::max_adjacent_difference(boost::begin(range), boost::end(range));
104 }
105 
106 /*************************************************************************************************/
107 
108 // standard calls using bind and range
109 
110 /*************************************************************************************************/
111 
122 template <typename InputRange, typename T>
123 inline T accumulate(const InputRange& range, T init)
124 {
125  return std::accumulate(boost::begin(range), boost::end(range), init);
126 }
127 
133 template <typename InputIterator, typename T, typename BinaryOperation>
134 inline T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op)
135 {
136  return std::accumulate(first, last, init, boost::bind(binary_op, _1, _2));
137 }
138 
144 template <typename InputRange, typename T, typename BinaryOperation>
145 inline T accumulate(const InputRange& range, T init, BinaryOperation binary_op)
146 {
147  return adobe::accumulate(boost::begin(range), boost::end(range), init, binary_op);
148 }
149 
160 template <typename InputRange, typename InputIterator, typename T>
161 inline T inner_product(const InputRange& range, InputIterator first, T init)
162 {
163  return std::inner_product(boost::begin(range), boost::end(range), first, init);
164 }
165 
171 template <typename InputIterator1, typename InputIterator2, typename T,
172  typename BinaryOperation1, typename BinaryOperation2>
173 inline T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init,
174  BinaryOperation1 binary_op1, BinaryOperation2 binary_op2)
175 {
176  return std::inner_product(first1, last1, first2, init, boost::bind(binary_op1, _1, _2),
177  boost::bind(binary_op2, _1, _2));
178 }
179 
185 template <typename InputRange, typename InputIterator, typename T,
186  typename BinaryOperation1, typename BinaryOperation2>
187 inline T inner_product(const InputRange& range, InputIterator first, T init,
188  BinaryOperation1 binary_op1, BinaryOperation2 binary_op2)
189 {
190  return adobe::inner_product(boost::begin(range), boost::end(range), first, init, binary_op1,
191  binary_op2);
192 }
193 
204 template <typename InputRange, typename OutputIterator>
205 inline OutputIterator partial_sum(const InputRange& range, OutputIterator result)
206 {
207  return std::partial_sum(boost::begin(range), boost::end(range), result);
208 }
209 
215 template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
216 inline OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result,
217  BinaryOperation binary_op)
218 {
219  return std::partial_sum(first, last, result, boost::bind(binary_op, _1, _2));
220 }
221 
227 template <typename InputRange, typename OutputIterator, typename BinaryOperation>
228 inline OutputIterator partial_sum(const InputRange& range, OutputIterator result,
229  BinaryOperation binary_op)
230 {
231  return adobe::partial_sum(boost::begin(range), boost::end(range), result, binary_op);
232 }
233 
243 template <typename InputRange, typename OutputIterator>
244 inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result)
245 {
246  return std::adjacent_difference(boost::begin(range), boost::end(range), result);
247 }
248 
254 template <typename InputIterator, typename OutputIterator, typename BinaryOperation>
255 inline OutputIterator adjacent_difference(InputIterator first, InputIterator last,
256  OutputIterator result, BinaryOperation binary_op)
257 {
258  return std::adjacent_difference(first, last, result, boost::bind(binary_op, _1, _2));
259 }
260 
266 template <typename InputRange, typename OutputIterator, typename BinaryOperation>
267 inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result,
268  BinaryOperation binary_op)
269 {
270  return adobe::adjacent_difference(boost::begin(range), boost::end(range), result, binary_op);
271 }
272 
273 /*************************************************************************************************/
274 
275 } // namespace adobe
276 
277 /*************************************************************************************************/
278 
279 #endif
280 
281 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google