numeric 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Numeric extensions -*- C++ -*-
  2. // Copyright (C) 2002, 2004, 2005, 2009 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file ext/numeric
  46. * This file is a GNU extension to the Standard C++ Library (possibly
  47. * containing extensions from the HP/SGI STL subset).
  48. */
  49. #ifndef _EXT_NUMERIC
  50. #define _EXT_NUMERIC 1
  51. #pragma GCC system_header
  52. #include <bits/concept_check.h>
  53. #include <numeric>
  54. #include <ext/functional> // For identity_element
  55. _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
  56. // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
  57. // is required to be associative, but not necessarily commutative.
  58. template<typename _Tp, typename _Integer, typename _MonoidOperation>
  59. _Tp
  60. __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
  61. {
  62. if (__n == 0)
  63. return identity_element(__monoid_op);
  64. else
  65. {
  66. while ((__n & 1) == 0)
  67. {
  68. __n >>= 1;
  69. __x = __monoid_op(__x, __x);
  70. }
  71. _Tp __result = __x;
  72. __n >>= 1;
  73. while (__n != 0)
  74. {
  75. __x = __monoid_op(__x, __x);
  76. if ((__n & 1) != 0)
  77. __result = __monoid_op(__result, __x);
  78. __n >>= 1;
  79. }
  80. return __result;
  81. }
  82. }
  83. template<typename _Tp, typename _Integer>
  84. inline _Tp
  85. __power(_Tp __x, _Integer __n)
  86. { return __power(__x, __n, std::multiplies<_Tp>()); }
  87. /**
  88. * This is an SGI extension.
  89. * @ingroup SGIextensions
  90. * @doctodo
  91. */
  92. // Alias for the internal name __power. Note that power is an extension,
  93. // not part of the C++ standard.
  94. template<typename _Tp, typename _Integer, typename _MonoidOperation>
  95. inline _Tp
  96. power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
  97. { return __power(__x, __n, __monoid_op); }
  98. /**
  99. * This is an SGI extension.
  100. * @ingroup SGIextensions
  101. * @doctodo
  102. */
  103. template<typename _Tp, typename _Integer>
  104. inline _Tp
  105. power(_Tp __x, _Integer __n)
  106. { return __power(__x, __n); }
  107. /**
  108. * This is an SGI extension.
  109. * @ingroup SGIextensions
  110. * @doctodo
  111. */
  112. // iota is not part of the C++ standard. It is an extension.
  113. template<typename _ForwardIter, typename _Tp>
  114. void
  115. iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
  116. {
  117. // concept requirements
  118. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
  119. __glibcxx_function_requires(_ConvertibleConcept<_Tp,
  120. typename std::iterator_traits<_ForwardIter>::value_type>)
  121. while (__first != __last)
  122. *__first++ = __value++;
  123. }
  124. _GLIBCXX_END_NAMESPACE
  125. #endif