_numeric.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1999
  10. * Boris Fomitchev
  11. *
  12. * This material is provided "as is", with absolutely no warranty expressed
  13. * or implied. Any use is at your own risk.
  14. *
  15. * Permission to use or copy this software for any purpose is hereby granted
  16. * without fee, provided the above notices are retained on all copies.
  17. * Permission to modify the code and to distribute modified code is granted,
  18. * provided the above notices are retained, and a notice that the code was
  19. * modified is included with the above copyright notice.
  20. *
  21. */
  22. /* NOTE: This is an internal header file, included by other STL headers.
  23. * You should not attempt to use it directly.
  24. */
  25. #ifndef _STLP_INTERNAL_NUMERIC_H
  26. #define _STLP_INTERNAL_NUMERIC_H
  27. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  28. # include <stl/_function_base.h>
  29. #endif
  30. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  31. # include <stl/_iterator_base.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. template <class _InputIterator, class _Tp>
  35. _STLP_INLINE_LOOP
  36. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init) {
  37. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  38. for ( ; __first != __last; ++__first)
  39. _Init = _Init + *__first;
  40. return _Init;
  41. }
  42. template <class _InputIterator, class _Tp, class _BinaryOperation>
  43. _STLP_INLINE_LOOP
  44. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init,
  45. _BinaryOperation __binary_op) {
  46. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  47. for ( ; __first != __last; ++__first)
  48. _Init = __binary_op(_Init, *__first);
  49. return _Init;
  50. }
  51. template <class _InputIterator1, class _InputIterator2, class _Tp>
  52. _STLP_INLINE_LOOP
  53. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  54. _InputIterator2 __first2, _Tp _Init) {
  55. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  56. for ( ; __first1 != __last1; ++__first1, ++__first2)
  57. _Init = _Init + (*__first1 * *__first2);
  58. return _Init;
  59. }
  60. template <class _InputIterator1, class _InputIterator2, class _Tp,
  61. class _BinaryOperation1, class _BinaryOperation2>
  62. _STLP_INLINE_LOOP
  63. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  64. _InputIterator2 __first2, _Tp _Init,
  65. _BinaryOperation1 __binary_op1,
  66. _BinaryOperation2 __binary_op2) {
  67. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first1, __last1))
  68. for ( ; __first1 != __last1; ++__first1, ++__first2)
  69. _Init = __binary_op1(_Init, __binary_op2(*__first1, *__first2));
  70. return _Init;
  71. }
  72. _STLP_MOVE_TO_PRIV_NAMESPACE
  73. template <class _InputIterator, class _OutputIterator, class _Tp,
  74. class _BinaryOperation>
  75. _OutputIterator
  76. __partial_sum(_InputIterator __first, _InputIterator __last,
  77. _OutputIterator __result, _Tp*, _BinaryOperation __binary_op);
  78. _STLP_MOVE_TO_STD_NAMESPACE
  79. template <class _InputIterator, class _OutputIterator>
  80. inline _OutputIterator
  81. partial_sum(_InputIterator __first, _InputIterator __last,
  82. _OutputIterator __result) {
  83. return _STLP_PRIV __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
  84. _STLP_PRIV __plus(_STLP_VALUE_TYPE(__first, _InputIterator)));
  85. }
  86. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  87. inline _OutputIterator
  88. partial_sum(_InputIterator __first, _InputIterator __last,
  89. _OutputIterator __result, _BinaryOperation __binary_op) {
  90. return _STLP_PRIV __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
  91. __binary_op);
  92. }
  93. _STLP_MOVE_TO_PRIV_NAMESPACE
  94. template <class _InputIterator, class _OutputIterator, class _Tp,
  95. class _BinaryOperation>
  96. _OutputIterator
  97. __adjacent_difference(_InputIterator __first, _InputIterator __last,
  98. _OutputIterator __result, _Tp*,
  99. _BinaryOperation __binary_op);
  100. _STLP_MOVE_TO_STD_NAMESPACE
  101. template <class _InputIterator, class _OutputIterator>
  102. inline _OutputIterator
  103. adjacent_difference(_InputIterator __first,
  104. _InputIterator __last, _OutputIterator __result) {
  105. return _STLP_PRIV __adjacent_difference(__first, __last, __result,
  106. _STLP_VALUE_TYPE(__first, _InputIterator),
  107. _STLP_PRIV __minus(_STLP_VALUE_TYPE(__first, _InputIterator)));
  108. }
  109. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  110. _OutputIterator
  111. adjacent_difference(_InputIterator __first, _InputIterator __last,
  112. _OutputIterator __result, _BinaryOperation __binary_op) {
  113. return _STLP_PRIV __adjacent_difference(__first, __last, __result,
  114. _STLP_VALUE_TYPE(__first, _InputIterator),
  115. __binary_op);
  116. }
  117. _STLP_MOVE_TO_PRIV_NAMESPACE
  118. template <class _Tp, class _Integer, class _MonoidOperation>
  119. _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr);
  120. _STLP_MOVE_TO_STD_NAMESPACE
  121. #if !defined (_STLP_NO_EXTENSIONS)
  122. // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
  123. // is required to be associative, but not necessarily commutative.
  124. _STLP_MOVE_TO_PRIV_NAMESPACE
  125. template <class _Tp, class _Integer>
  126. inline _Tp __power(_Tp __x, _Integer __n) {
  127. return __power(__x, __n, multiplies<_Tp>());
  128. }
  129. _STLP_MOVE_TO_STD_NAMESPACE
  130. // Alias for the internal name __power. Note that power is an extension,
  131. // not part of the C++ standard.
  132. template <class _Tp, class _Integer, class _MonoidOperation>
  133. inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
  134. return _STLP_PRIV __power(__x, __n, __opr);
  135. }
  136. template <class _Tp, class _Integer>
  137. inline _Tp power(_Tp __x, _Integer __n) {
  138. return _STLP_PRIV __power(__x, __n, multiplies<_Tp>());
  139. }
  140. // iota is not part of the C++ standard. It is an extension.
  141. template <class _ForwardIterator, class _Tp>
  142. _STLP_INLINE_LOOP
  143. void iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __val) {
  144. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
  145. while (__first != __last)
  146. *__first++ = __val++;
  147. }
  148. #endif
  149. _STLP_END_NAMESPACE
  150. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  151. # include <stl/_numeric.c>
  152. #endif
  153. #endif /* _STLP_INTERNAL_NUMERIC_H */
  154. // Local Variables:
  155. // mode:C++
  156. // End: