_numeric.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. *
  3. *
  4. * Copyright (c) 1994
  5. * Hewlett-Packard Company
  6. *
  7. * Copyright (c) 1996,1997
  8. * Silicon Graphics Computer Systems, Inc.
  9. *
  10. * Copyright (c) 1997
  11. * Moscow Center for SPARC Technology
  12. *
  13. * Copyright (c) 1999
  14. * Boris Fomitchev
  15. *
  16. * This material is provided "as is", with absolutely no warranty expressed
  17. * or implied. Any use is at your own risk.
  18. *
  19. * Permission to use or copy this software for any purpose is hereby granted
  20. * without fee, provided the above notices are retained on all copies.
  21. * Permission to modify the code and to distribute modified code is granted,
  22. * provided the above notices are retained, and a notice that the code was
  23. * modified is included with the above copyright notice.
  24. *
  25. */
  26. #ifndef _STLP_NUMERIC_C
  27. #define _STLP_NUMERIC_C
  28. #ifndef _STLP_INTERNAL_NUMERIC_H
  29. # include <stl/_numeric.h>
  30. #endif
  31. _STLP_BEGIN_NAMESPACE
  32. _STLP_MOVE_TO_PRIV_NAMESPACE
  33. template <class _InputIterator, class _OutputIterator, class _Tp,
  34. class _BinaryOperation>
  35. _OutputIterator
  36. __partial_sum(_InputIterator __first, _InputIterator __last,
  37. _OutputIterator __result, _Tp*, _BinaryOperation __binary_op) {
  38. _STLP_DEBUG_CHECK(__check_range(__first, __last))
  39. if (__first == __last) return __result;
  40. *__result = *__first;
  41. _Tp __val = *__first;
  42. while (++__first != __last) {
  43. __val = __binary_op(__val, *__first);
  44. *++__result = __val;
  45. }
  46. return ++__result;
  47. }
  48. template <class _InputIterator, class _OutputIterator, class _Tp,
  49. class _BinaryOperation>
  50. _OutputIterator
  51. __adjacent_difference(_InputIterator __first, _InputIterator __last,
  52. _OutputIterator __result, _Tp*,
  53. _BinaryOperation __binary_op) {
  54. _STLP_DEBUG_CHECK(__check_range(__first, __last))
  55. if (__first == __last) return __result;
  56. *__result = *__first;
  57. _Tp __val = *__first;
  58. while (++__first != __last) {
  59. _Tp __tmp = *__first;
  60. *++__result = __binary_op(__tmp, __val);
  61. __val = __tmp;
  62. }
  63. return ++__result;
  64. }
  65. template <class _Tp, class _Integer, class _MonoidOperation>
  66. _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
  67. _STLP_MPWFIX_TRY
  68. if (__n == 0)
  69. return __identity_element(__opr);
  70. else {
  71. while ((__n & 1) == 0) {
  72. __n >>= 1;
  73. __x = __opr(__x, __x);
  74. }
  75. _Tp __result = __x;
  76. _STLP_MPWFIX_TRY
  77. __n >>= 1;
  78. while (__n != 0) {
  79. __x = __opr(__x, __x);
  80. if ((__n & 1) != 0)
  81. __result = __opr(__result, __x);
  82. __n >>= 1;
  83. }
  84. return __result;
  85. _STLP_MPWFIX_CATCH
  86. }
  87. _STLP_MPWFIX_CATCH_ACTION(__x = _Tp())
  88. }
  89. _STLP_MOVE_TO_STD_NAMESPACE
  90. _STLP_END_NAMESPACE
  91. #endif /* _STLP_NUMERIC_C */
  92. // Local Variables:
  93. // mode:C++
  94. // End: