libstdc++
ext/numeric
Go to the documentation of this file.
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 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00067 {
00068 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00069 
00070   // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
00071   // is required to be associative, but not necessarily commutative.
00072   template<typename _Tp, typename _Integer, typename _MonoidOperation>
00073     _Tp
00074     __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00075     {
00076       if (__n == 0)
00077     return identity_element(__monoid_op);
00078       else
00079     {
00080       while ((__n & 1) == 0)
00081         {
00082           __n >>= 1;
00083           __x = __monoid_op(__x, __x);
00084         }
00085 
00086       _Tp __result = __x;
00087       __n >>= 1;
00088       while (__n != 0)
00089         {
00090           __x = __monoid_op(__x, __x);
00091           if ((__n & 1) != 0)
00092         __result = __monoid_op(__result, __x);
00093           __n >>= 1;
00094         }
00095       return __result;
00096     }
00097     }
00098 
00099   template<typename _Tp, typename _Integer>
00100     inline _Tp
00101     __power(_Tp __x, _Integer __n)
00102     { return __power(__x, __n, std::multiplies<_Tp>()); }
00103 
00104   /**
00105    *  This is an SGI extension.
00106    *  @ingroup SGIextensions
00107    *  @doctodo
00108   */
00109   // Alias for the internal name __power.  Note that power is an extension,
00110   // not part of the C++ standard.
00111   template<typename _Tp, typename _Integer, typename _MonoidOperation>
00112     inline _Tp
00113     power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op)
00114     { return __power(__x, __n, __monoid_op); }
00115 
00116   /**
00117    *  This is an SGI extension.
00118    *  @ingroup SGIextensions
00119    *  @doctodo
00120   */
00121   template<typename _Tp, typename _Integer>
00122     inline _Tp
00123     power(_Tp __x, _Integer __n)
00124     { return __power(__x, __n); }
00125 
00126 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00127   using std::iota;
00128 #else
00129   /**
00130    *  This is an SGI extension.
00131    *  @ingroup SGIextensions
00132    *  @doctodo
00133   */
00134   // iota is not part of the C++ standard.  It is an extension.
00135   template<typename _ForwardIter, typename _Tp>
00136     void
00137     iota(_ForwardIter __first, _ForwardIter __last, _Tp __value)
00138     {
00139       // concept requirements
00140       __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>)
00141       __glibcxx_function_requires(_ConvertibleConcept<_Tp,
00142         typename std::iterator_traits<_ForwardIter>::value_type>)
00143 
00144       while (__first != __last)
00145     *__first++ = __value++;
00146     }
00147 #endif  // __GXX_EXPERIMENTAL_CXX0X__
00148 
00149 _GLIBCXX_END_NAMESPACE_VERSION
00150 } // namespace
00151 
00152 #endif