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