STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_numeric.h
Go to the documentation of this file.
1 // Numeric functions implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996,1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
56 #ifndef _STL_NUMERIC_H
57 #define _STL_NUMERIC_H 1
58 
59 #include <bits/concept_check.h>
60 #include <debug/debug.h>
61 #include <bits/move.h> // For _GLIBCXX_MOVE
62 
63 #if __cplusplus >= 201103L
64 
65 namespace std _GLIBCXX_VISIBILITY(default)
66 {
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
68 
80  template<typename _ForwardIterator, typename _Tp>
81  void
82  iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __value)
83  {
84  // concept requirements
85  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
86  _ForwardIterator>)
87  __glibcxx_function_requires(_ConvertibleConcept<_Tp,
88  typename iterator_traits<_ForwardIterator>::value_type>)
89  __glibcxx_requires_valid_range(__first, __last);
90 
91  for (; __first != __last; ++__first)
92  {
93  *__first = __value;
94  ++__value;
95  }
96  }
97 
98 _GLIBCXX_END_NAMESPACE_VERSION
99 } // namespace std
100 
101 #endif
102 
103 namespace std _GLIBCXX_VISIBILITY(default)
104 {
105 _GLIBCXX_BEGIN_NAMESPACE_ALGO
106 
118  template<typename _InputIterator, typename _Tp>
119  inline _Tp
120  accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
121  {
122  // concept requirements
123  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
124  __glibcxx_requires_valid_range(__first, __last);
125 
126  for (; __first != __last; ++__first)
127  __init = __init + *__first;
128  return __init;
129  }
130 
144  template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
145  inline _Tp
146  accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
147  _BinaryOperation __binary_op)
148  {
149  // concept requirements
150  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
151  __glibcxx_requires_valid_range(__first, __last);
152 
153  for (; __first != __last; ++__first)
154  __init = __binary_op(__init, *__first);
155  return __init;
156  }
157 
172  template<typename _InputIterator1, typename _InputIterator2, typename _Tp>
173  inline _Tp
174  inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
175  _InputIterator2 __first2, _Tp __init)
176  {
177  // concept requirements
178  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
179  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
180  __glibcxx_requires_valid_range(__first1, __last1);
181 
182  for (; __first1 != __last1; ++__first1, ++__first2)
183  __init = __init + (*__first1 * *__first2);
184  return __init;
185  }
186 
203  template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
204  typename _BinaryOperation1, typename _BinaryOperation2>
205  inline _Tp
206  inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
207  _InputIterator2 __first2, _Tp __init,
208  _BinaryOperation1 __binary_op1,
209  _BinaryOperation2 __binary_op2)
210  {
211  // concept requirements
212  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
213  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
214  __glibcxx_requires_valid_range(__first1, __last1);
215 
216  for (; __first1 != __last1; ++__first1, ++__first2)
217  __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
218  return __init;
219  }
220 
235  template<typename _InputIterator, typename _OutputIterator>
236  _OutputIterator
237  partial_sum(_InputIterator __first, _InputIterator __last,
238  _OutputIterator __result)
239  {
240  typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
241 
242  // concept requirements
243  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
244  __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
245  _ValueType>)
246  __glibcxx_requires_valid_range(__first, __last);
247 
248  if (__first == __last)
249  return __result;
250  _ValueType __value = *__first;
251  *__result = __value;
252  while (++__first != __last)
253  {
254  __value = __value + *__first;
255  *++__result = __value;
256  }
257  return ++__result;
258  }
259 
275  template<typename _InputIterator, typename _OutputIterator,
276  typename _BinaryOperation>
277  _OutputIterator
278  partial_sum(_InputIterator __first, _InputIterator __last,
279  _OutputIterator __result, _BinaryOperation __binary_op)
280  {
281  typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
282 
283  // concept requirements
284  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
285  __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
286  _ValueType>)
287  __glibcxx_requires_valid_range(__first, __last);
288 
289  if (__first == __last)
290  return __result;
291  _ValueType __value = *__first;
292  *__result = __value;
293  while (++__first != __last)
294  {
295  __value = __binary_op(__value, *__first);
296  *++__result = __value;
297  }
298  return ++__result;
299  }
300 
315  template<typename _InputIterator, typename _OutputIterator>
316  _OutputIterator
317  adjacent_difference(_InputIterator __first,
318  _InputIterator __last, _OutputIterator __result)
319  {
320  typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
321 
322  // concept requirements
323  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
324  __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
325  _ValueType>)
326  __glibcxx_requires_valid_range(__first, __last);
327 
328  if (__first == __last)
329  return __result;
330  _ValueType __value = *__first;
331  *__result = __value;
332  while (++__first != __last)
333  {
334  _ValueType __tmp = *__first;
335  *++__result = __tmp - __value;
336  __value = _GLIBCXX_MOVE(__tmp);
337  }
338  return ++__result;
339  }
340 
357  template<typename _InputIterator, typename _OutputIterator,
358  typename _BinaryOperation>
359  _OutputIterator
360  adjacent_difference(_InputIterator __first, _InputIterator __last,
361  _OutputIterator __result, _BinaryOperation __binary_op)
362  {
363  typedef typename iterator_traits<_InputIterator>::value_type _ValueType;
364 
365  // concept requirements
366  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
367  __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
368  _ValueType>)
369  __glibcxx_requires_valid_range(__first, __last);
370 
371  if (__first == __last)
372  return __result;
373  _ValueType __value = *__first;
374  *__result = __value;
375  while (++__first != __last)
376  {
377  _ValueType __tmp = *__first;
378  *++__result = __binary_op(__tmp, __value);
379  __value = _GLIBCXX_MOVE(__tmp);
380  }
381  return ++__result;
382  }
383 
384 _GLIBCXX_END_NAMESPACE_ALGO
385 } // namespace std
386 
387 #endif /* _STL_NUMERIC_H */
#define __glibcxx_function_requires(...)
Definition: concept_check.h:47
#define _GLIBCXX_MOVE(__val)
Definition: move.h:145
namespace std _GLIBCXX_VISIBILITY(default)
Definition: stl_numeric.h:103
#define __glibcxx_requires_valid_range(_First, _Last)
Definition: debug.h:65