You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 satır
4.6KB

  1. // Numeric extensions -*- C++ -*-
  2. // Copyright (C) 2002-2020 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. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  56. {
  57. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  58. // Returns __x ** __n, where __n >= 0. _Note that "multiplication"
  59. // is required to be associative, but not necessarily commutative.
  60. template<typename _Tp, typename _Integer, typename _MonoidOperation>
  61. _Tp
  62. __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
  63. {
  64. if (__n == 0)
  65. return identity_element(__monoid_op);
  66. else
  67. {
  68. while ((__n & 1) == 0)
  69. {
  70. __n >>= 1;
  71. __x = __monoid_op(__x, __x);
  72. }
  73. _Tp __result = __x;
  74. __n >>= 1;
  75. while (__n != 0)
  76. {
  77. __x = __monoid_op(__x, __x);
  78. if ((__n & 1) != 0)
  79. __result = __monoid_op(__result, __x);
  80. __n >>= 1;
  81. }
  82. return __result;
  83. }
  84. }
  85. template<typename _Tp, typename _Integer>
  86. inline _Tp
  87. __power(_Tp __x, _Integer __n)
  88. { return __power(__x, __n, std::multiplies<_Tp>()); }
  89. /**
  90. * This is an SGI extension.
  91. * @ingroup SGIextensions
  92. * @doctodo
  93. */
  94. // Alias for the internal name __power. Note that power is an extension,
  95. // not part of the C++ standard.
  96. template<typename _Tp, typename _Integer, typename _MonoidOperation>
  97. inline _Tp
  98. power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
  99. { return __power(__x, __n, __monoid_op); }
  100. /**
  101. * This is an SGI extension.
  102. * @ingroup SGIextensions
  103. * @doctodo
  104. */
  105. template<typename _Tp, typename _Integer>
  106. inline _Tp
  107. power(_Tp __x, _Integer __n)
  108. { return __power(__x, __n); }
  109. #if __cplusplus >= 201103L
  110. using std::iota;
  111. #else
  112. /**
  113. * This is an SGI extension.
  114. * @ingroup SGIextensions
  115. * @doctodo
  116. */
  117. // iota is not part of the standard until C++11. It is an extension.
  118. template<typename _ForwardIter, typename _Tp>
  119. void
  120. iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
  121. {
  122. // concept requirements
  123. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
  124. __glibcxx_function_requires(_ConvertibleConcept<_Tp,
  125. typename std::iterator_traits<_ForwardIter>::value_type>)
  126. while (__first != __last)
  127. *__first++ = __value++;
  128. }
  129. #endif // C++11
  130. _GLIBCXX_END_NAMESPACE_VERSION
  131. } // namespace
  132. #endif