1 // Numeric functions implementation -*- C++ -*- 2 3 // Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 2, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // You should have received a copy of the GNU General Public License along 17 // with this library; see the file COPYING. If not, write to the Free 18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19 // USA. 20 21 // As a special exception, you may use this file as part of a free software 22 // library without restriction. Specifically, if other files instantiate 23 // templates or use macros or inline functions from this file, or you compile 24 // this file and link it with other files to produce an executable, this 25 // file does not by itself cause the resulting executable to be covered by 26 // the GNU General Public License. This exception does not however 27 // invalidate any other reasons why the executable file might be covered by 28 // the GNU General Public License. 29 30 /* 31 * 32 * Copyright (c) 1994 33 * Hewlett-Packard Company 34 * 35 * Permission to use, copy, modify, distribute and sell this software 36 * and its documentation for any purpose is hereby granted without fee, 37 * provided that the above copyright notice appear in all copies and 38 * that both that copyright notice and this permission notice appear 39 * in supporting documentation. Hewlett-Packard Company makes no 40 * representations about the suitability of this software for any 41 * purpose. It is provided "as is" without express or implied warranty. 42 * 43 * 44 * Copyright (c) 1996,1997 45 * Silicon Graphics Computer Systems, Inc. 46 * 47 * Permission to use, copy, modify, distribute and sell this software 48 * and its documentation for any purpose is hereby granted without fee, 49 * provided that the above copyright notice appear in all copies and 50 * that both that copyright notice and this permission notice appear 51 * in supporting documentation. Silicon Graphics makes no 52 * representations about the suitability of this software for any 53 * purpose. It is provided "as is" without express or implied warranty. 54 */ 55 56 /** @file stl_numeric.h 57 * This is an internal header file, included by other library headers. 58 * You should not attempt to use it directly. 59 */ 60 61 #ifndef _STL_NUMERIC_H 62 #define _STL_NUMERIC_H 1 63 64 #include <debug/debug.h> 65 66 _GLIBCXX_BEGIN_NAMESPACE(std) 67 68 /** 69 * @brief Accumulate values in a range. 70 * 71 * Accumulates the values in the range [first,last) using operator+(). The 72 * initial value is @a init. The values are processed in order. 73 * 74 * @param first Start of range. 75 * @param last End of range. 76 * @param init Starting value to add other values to. 77 * @return The final sum. 78 */ 79 template<typename _InputIterator, typename _Tp> 80 _Tp 81 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init) 82 { 83 // concept requirements 84 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 85 __glibcxx_requires_valid_range(__first, __last); 86 87 for (; __first != __last; ++__first) 88 __init = __init + *__first; 89 return __init; 90 } 91 92 /** 93 * @brief Accumulate values in a range with operation. 94 * 95 * Accumulates the values in the range [first,last) using the function 96 * object @a binary_op. The initial value is @a init. The values are 97 * processed in order. 98 * 99 * @param first Start of range. 100 * @param last End of range. 101 * @param init Starting value to add other values to. 102 * @param binary_op Function object to accumulate with. 103 * @return The final sum. 104 */ 105 template<typename _InputIterator, typename _Tp, typename _BinaryOperation> 106 _Tp 107 accumulate(_InputIterator __first, _InputIterator __last, _Tp __init, 108 _BinaryOperation __binary_op) 109 { 110 // concept requirements 111 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 112 __glibcxx_requires_valid_range(__first, __last); 113 114 for (; __first != __last; ++__first) 115 __init = __binary_op(__init, *__first); 116 return __init; 117 } 118 119 /** 120 * @brief Compute inner product of two ranges. 121 * 122 * Starting with an initial value of @a init, multiplies successive 123 * elements from the two ranges and adds each product into the accumulated 124 * value using operator+(). The values in the ranges are processed in 125 * order. 126 * 127 * @param first1 Start of range 1. 128 * @param last1 End of range 1. 129 * @param first2 Start of range 2. 130 * @param init Starting value to add other values to. 131 * @return The final inner product. 132 */ 133 template<typename _InputIterator1, typename _InputIterator2, typename _Tp> 134 _Tp 135 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, 136 _InputIterator2 __first2, _Tp __init) 137 { 138 // concept requirements 139 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 140 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 141 __glibcxx_requires_valid_range(__first1, __last1); 142 143 for (; __first1 != __last1; ++__first1, ++__first2) 144 __init = __init + (*__first1 * *__first2); 145 return __init; 146 } 147 148 /** 149 * @brief Compute inner product of two ranges. 150 * 151 * Starting with an initial value of @a init, applies @a binary_op2 to 152 * successive elements from the two ranges and accumulates each result into 153 * the accumulated value using @a binary_op1. The values in the ranges are 154 * processed in order. 155 * 156 * @param first1 Start of range 1. 157 * @param last1 End of range 1. 158 * @param first2 Start of range 2. 159 * @param init Starting value to add other values to. 160 * @param binary_op1 Function object to accumulate with. 161 * @param binary_op2 Function object to apply to pairs of input values. 162 * @return The final inner product. 163 */ 164 template<typename _InputIterator1, typename _InputIterator2, typename _Tp, 165 typename _BinaryOperation1, typename _BinaryOperation2> 166 _Tp 167 inner_product(_InputIterator1 __first1, _InputIterator1 __last1, 168 _InputIterator2 __first2, _Tp __init, 169 _BinaryOperation1 __binary_op1, 170 _BinaryOperation2 __binary_op2) 171 { 172 // concept requirements 173 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) 174 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) 175 __glibcxx_requires_valid_range(__first1, __last1); 176 177 for (; __first1 != __last1; ++__first1, ++__first2) 178 __init = __binary_op1(__init, __binary_op2(*__first1, *__first2)); 179 return __init; 180 } 181 182 /** 183 * @brief Return list of partial sums 184 * 185 * Accumulates the values in the range [first,last) using operator+(). 186 * As each successive input value is added into the total, that partial sum 187 * is written to @a result. Therefore, the first value in result is the 188 * first value of the input, the second value in result is the sum of the 189 * first and second input values, and so on. 190 * 191 * @param first Start of input range. 192 * @param last End of input range. 193 * @param result Output to write sums to. 194 * @return Iterator pointing just beyond the values written to result. 195 */ 196 template<typename _InputIterator, typename _OutputIterator> 197 _OutputIterator 198 partial_sum(_InputIterator __first, _InputIterator __last, 199 _OutputIterator __result) 200 { 201 typedef typename iterator_traits<_InputIterator>::value_type _ValueType; 202 203 // concept requirements 204 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 205 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 206 _ValueType>) 207 __glibcxx_requires_valid_range(__first, __last); 208 209 if (__first == __last) 210 return __result; 211 _ValueType __value = *__first; 212 *__result = __value; 213 while (++__first != __last) 214 { 215 __value = __value + *__first; 216 *++__result = __value; 217 } 218 return ++__result; 219 } 220 221 /** 222 * @brief Return list of partial sums 223 * 224 * Accumulates the values in the range [first,last) using operator+(). 225 * As each successive input value is added into the total, that partial sum 226 * is written to @a result. Therefore, the first value in result is the 227 * first value of the input, the second value in result is the sum of the 228 * first and second input values, and so on. 229 * 230 * @param first Start of input range. 231 * @param last End of input range. 232 * @param result Output to write sums to. 233 * @return Iterator pointing just beyond the values written to result. 234 */ 235 template<typename _InputIterator, typename _OutputIterator, 236 typename _BinaryOperation> 237 _OutputIterator 238 partial_sum(_InputIterator __first, _InputIterator __last, 239 _OutputIterator __result, _BinaryOperation __binary_op) 240 { 241 typedef typename iterator_traits<_InputIterator>::value_type _ValueType; 242 243 // concept requirements 244 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 245 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 246 _ValueType>) 247 __glibcxx_requires_valid_range(__first, __last); 248 249 if (__first == __last) 250 return __result; 251 _ValueType __value = *__first; 252 *__result = __value; 253 while (++__first != __last) 254 { 255 __value = __binary_op(__value, *__first); 256 *++__result = __value; 257 } 258 return ++__result; 259 } 260 261 /** 262 * @brief Return differences between adjacent values. 263 * 264 * Computes the difference between adjacent values in the range 265 * [first,last) using operator-() and writes the result to @a result. 266 * 267 * @param first Start of input range. 268 * @param last End of input range. 269 * @param result Output to write sums to. 270 * @return Iterator pointing just beyond the values written to result. 271 */ 272 template<typename _InputIterator, typename _OutputIterator> 273 _OutputIterator 274 adjacent_difference(_InputIterator __first, 275 _InputIterator __last, _OutputIterator __result) 276 { 277 typedef typename iterator_traits<_InputIterator>::value_type _ValueType; 278 279 // concept requirements 280 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 281 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 282 _ValueType>) 283 __glibcxx_requires_valid_range(__first, __last); 284 285 if (__first == __last) 286 return __result; 287 _ValueType __value = *__first; 288 *__result = __value; 289 while (++__first != __last) 290 { 291 _ValueType __tmp = *__first; 292 *++__result = __tmp - __value; 293 __value = __tmp; 294 } 295 return ++__result; 296 } 297 298 /** 299 * @brief Return differences between adjacent values. 300 * 301 * Computes the difference between adjacent values in the range 302 * [first,last) using the function object @a binary_op and writes the 303 * result to @a result. 304 * 305 * @param first Start of input range. 306 * @param last End of input range. 307 * @param result Output to write sums to. 308 * @return Iterator pointing just beyond the values written to result. 309 */ 310 template<typename _InputIterator, typename _OutputIterator, 311 typename _BinaryOperation> 312 _OutputIterator 313 adjacent_difference(_InputIterator __first, _InputIterator __last, 314 _OutputIterator __result, _BinaryOperation __binary_op) 315 { 316 typedef typename iterator_traits<_InputIterator>::value_type _ValueType; 317 318 // concept requirements 319 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) 320 __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, 321 _ValueType>) 322 __glibcxx_requires_valid_range(__first, __last); 323 324 if (__first == __last) 325 return __result; 326 _ValueType __value = *__first; 327 *__result = __value; 328 while (++__first != __last) 329 { 330 _ValueType __tmp = *__first; 331 *++__result = __binary_op(__tmp, __value); 332 __value = __tmp; 333 } 334 return ++__result; 335 } 336 337 _GLIBCXX_END_NAMESPACE 338 339 #endif /* _STL_NUMERIC_H */ 340