numeric.hpp
Go to the documentation of this file.
00001 /* 00002 Copyright 2005-2007 Adobe Systems Incorporated 00003 Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt 00004 or a copy at http://stlab.adobe.com/licenses.html) 00005 */ 00006 00007 /*************************************************************************************************/ 00008 00009 #ifndef ADOBE_NUMERIC_HPP 00010 #define ADOBE_NUMERIC_HPP 00011 00012 #include <adobe/config.hpp> 00013 00014 #include <iterator> 00015 #include <numeric> 00016 00017 #include <boost/bind.hpp> 00018 00019 // REVISIT (sparent): dissable warnings for unused arguments in boost 1.32.0 00020 #if defined(__MWERKS__) 00021 #pragma warn_unusedarg off 00022 #endif 00023 00024 #include <boost/range.hpp> 00025 00026 #if defined(__MWERKS__) 00027 #pragma warn_unusedarg reset 00028 #endif 00029 00030 namespace adobe { 00048 /*************************************************************************************************/ 00049 00053 template <typename ForwardIterator> 00054 ForwardIterator max_adjacent_difference(ForwardIterator first, ForwardIterator last) 00055 { 00056 typedef typename std::iterator_traits<ForwardIterator>::value_type value_type; 00057 00058 ForwardIterator result(first); 00059 00060 if (first == last) return result; 00061 00062 ForwardIterator previous(first); 00063 ++first; 00064 00065 if (first == last) return result; 00066 00067 value_type result_difference(*first - *previous); 00068 previous = first; ++first; 00069 00070 while (first != last) 00071 { 00072 value_type difference(*first - *previous); 00073 00074 if (result_difference < difference) 00075 { 00076 result_difference = difference; 00077 result = previous; 00078 } 00079 previous = first; 00080 ++first; 00081 } 00082 00083 return result; 00084 } 00085 00089 template <typename ForwardRange> 00090 inline typename boost::range_iterator<ForwardRange>::type 00091 max_adjacent_difference(ForwardRange& range) 00092 { 00093 return adobe::max_adjacent_difference(boost::begin(range), boost::end(range)); 00094 } 00095 00099 template <typename ForwardRange> 00100 inline typename boost::range_const_iterator<ForwardRange>::type 00101 max_adjacent_difference(const ForwardRange& range) 00102 { 00103 return adobe::max_adjacent_difference(boost::begin(range), boost::end(range)); 00104 } 00105 00106 /*************************************************************************************************/ 00107 00108 // standard calls using bind and range 00109 00110 /*************************************************************************************************/ 00111 00122 template <typename InputRange, typename T> 00123 inline T accumulate(const InputRange& range, T init) 00124 { 00125 return std::accumulate(boost::begin(range), boost::end(range), init); 00126 } 00127 00133 template <typename InputIterator, typename T, typename BinaryOperation> 00134 inline T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op) 00135 { 00136 return std::accumulate(first, last, init, boost::bind(binary_op, _1, _2)); 00137 } 00138 00144 template <typename InputRange, typename T, typename BinaryOperation> 00145 inline T accumulate(const InputRange& range, T init, BinaryOperation binary_op) 00146 { 00147 return adobe::accumulate(boost::begin(range), boost::end(range), init, binary_op); 00148 } 00149 00160 template <typename InputRange, typename InputIterator, typename T> 00161 inline T inner_product(const InputRange& range, InputIterator first, T init) 00162 { 00163 return std::inner_product(boost::begin(range), boost::end(range), first, init); 00164 } 00165 00171 template <typename InputIterator1, typename InputIterator2, typename T, 00172 typename BinaryOperation1, typename BinaryOperation2> 00173 inline T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, 00174 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2) 00175 { 00176 return std::inner_product(first1, last1, first2, init, boost::bind(binary_op1, _1, _2), 00177 boost::bind(binary_op2, _1, _2)); 00178 } 00179 00185 template <typename InputRange, typename InputIterator, typename T, 00186 typename BinaryOperation1, typename BinaryOperation2> 00187 inline T inner_product(const InputRange& range, InputIterator first, T init, 00188 BinaryOperation1 binary_op1, BinaryOperation2 binary_op2) 00189 { 00190 return adobe::inner_product(boost::begin(range), boost::end(range), first, init, binary_op1, 00191 binary_op2); 00192 } 00193 00204 template <typename InputRange, typename OutputIterator> 00205 inline OutputIterator partial_sum(const InputRange& range, OutputIterator result) 00206 { 00207 return std::partial_sum(boost::begin(range), boost::end(range), result); 00208 } 00209 00215 template <typename InputIterator, typename OutputIterator, typename BinaryOperation> 00216 inline OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result, 00217 BinaryOperation binary_op) 00218 { 00219 return std::partial_sum(first, last, result, boost::bind(binary_op, _1, _2)); 00220 } 00221 00227 template <typename InputRange, typename OutputIterator, typename BinaryOperation> 00228 inline OutputIterator partial_sum(const InputRange& range, OutputIterator result, 00229 BinaryOperation binary_op) 00230 { 00231 return adobe::partial_sum(boost::begin(range), boost::end(range), result, binary_op); 00232 } 00233 00243 template <typename InputRange, typename OutputIterator> 00244 inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result) 00245 { 00246 return std::adjacent_difference(boost::begin(range), boost::end(range), result); 00247 } 00248 00254 template <typename InputIterator, typename OutputIterator, typename BinaryOperation> 00255 inline OutputIterator adjacent_difference(InputIterator first, InputIterator last, 00256 OutputIterator result, BinaryOperation binary_op) 00257 { 00258 return std::adjacent_difference(first, last, result, boost::bind(binary_op, _1, _2)); 00259 } 00260 00266 template <typename InputRange, typename OutputIterator, typename BinaryOperation> 00267 inline OutputIterator adjacent_difference(const InputRange& range, OutputIterator result, 00268 BinaryOperation binary_op) 00269 { 00270 return adobe::adjacent_difference(boost::begin(range), boost::end(range), result, binary_op); 00271 } 00272 00273 /*************************************************************************************************/ 00274 00275 } // namespace adobe 00276 00277 /*************************************************************************************************/ 00278 00279 #endif 00280 00281 /*************************************************************************************************/ |