00001 // Numeric extensions -*- C++ -*- 00002 00003 // Copyright (C) 2002, 2004, 2005, 2009 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file ext/numeric 00052 * This file is a GNU extension to the Standard C++ Library (possibly 00053 * containing extensions from the HP/SGI STL subset). 00054 */ 00055 00056 #ifndef _EXT_NUMERIC 00057 #define _EXT_NUMERIC 1 00058 00059 #pragma GCC system_header 00060 00061 #include <bits/concept_check.h> 00062 #include <numeric> 00063 00064 #include <ext/functional> // For identity_element 00065 00066 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 00067 00068 // Returns __x ** __n, where __n >= 0. _Note that "multiplication" 00069 // is required to be associative, but not necessarily commutative. 00070 template<typename _Tp, typename _Integer, typename _MonoidOperation> 00071 _Tp 00072 __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op) 00073 { 00074 if (__n == 0) 00075 return identity_element(__monoid_op); 00076 else 00077 { 00078 while ((__n & 1) == 0) 00079 { 00080 __n >>= 1; 00081 __x = __monoid_op(__x, __x); 00082 } 00083 00084 _Tp __result = __x; 00085 __n >>= 1; 00086 while (__n != 0) 00087 { 00088 __x = __monoid_op(__x, __x); 00089 if ((__n & 1) != 0) 00090 __result = __monoid_op(__result, __x); 00091 __n >>= 1; 00092 } 00093 return __result; 00094 } 00095 } 00096 00097 template<typename _Tp, typename _Integer> 00098 inline _Tp 00099 __power(_Tp __x, _Integer __n) 00100 { return __power(__x, __n, std::multiplies<_Tp>()); } 00101 00102 /** 00103 * This is an SGI extension. 00104 * @ingroup SGIextensions 00105 * @doctodo 00106 */ 00107 // Alias for the internal name __power. Note that power is an extension, 00108 // not part of the C++ standard. 00109 template<typename _Tp, typename _Integer, typename _MonoidOperation> 00110 inline _Tp 00111 power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op) 00112 { return __power(__x, __n, __monoid_op); } 00113 00114 /** 00115 * This is an SGI extension. 00116 * @ingroup SGIextensions 00117 * @doctodo 00118 */ 00119 template<typename _Tp, typename _Integer> 00120 inline _Tp 00121 power(_Tp __x, _Integer __n) 00122 { return __power(__x, __n); } 00123 00124 /** 00125 * This is an SGI extension. 00126 * @ingroup SGIextensions 00127 * @doctodo 00128 */ 00129 // iota is not part of the C++ standard. It is an extension. 00130 template<typename _ForwardIter, typename _Tp> 00131 void 00132 iota(_ForwardIter __first, _ForwardIter __last, _Tp __value) 00133 { 00134 // concept requirements 00135 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>) 00136 __glibcxx_function_requires(_ConvertibleConcept<_Tp, 00137 typename std::iterator_traits<_ForwardIter>::value_type>) 00138 00139 while (__first != __last) 00140 *__first++ = __value++; 00141 } 00142 00143 _GLIBCXX_END_NAMESPACE 00144 00145 #endif 00146