numeric revision 97403
197403Sobrien// Numeric extensions -*- C++ -*- 297403Sobrien 397403Sobrien// Copyright (C) 2002 Free Software Foundation, Inc. 497403Sobrien// 597403Sobrien// This file is part of the GNU ISO C++ Library. This library is free 697403Sobrien// software; you can redistribute it and/or modify it under the 797403Sobrien// terms of the GNU General Public License as published by the 897403Sobrien// Free Software Foundation; either version 2, or (at your option) 997403Sobrien// any later version. 1097403Sobrien 1197403Sobrien// This library is distributed in the hope that it will be useful, 1297403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1397403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1497403Sobrien// GNU General Public License for more details. 1597403Sobrien 1697403Sobrien// You should have received a copy of the GNU General Public License along 1797403Sobrien// with this library; see the file COPYING. If not, write to the Free 1897403Sobrien// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 1997403Sobrien// USA. 2097403Sobrien 2197403Sobrien// As a special exception, you may use this file as part of a free software 2297403Sobrien// library without restriction. Specifically, if other files instantiate 2397403Sobrien// templates or use macros or inline functions from this file, or you compile 2497403Sobrien// this file and link it with other files to produce an executable, this 2597403Sobrien// file does not by itself cause the resulting executable to be covered by 2697403Sobrien// the GNU General Public License. This exception does not however 2797403Sobrien// invalidate any other reasons why the executable file might be covered by 2897403Sobrien// the GNU General Public License. 2997403Sobrien 3097403Sobrien/* 3197403Sobrien * 3297403Sobrien * Copyright (c) 1994 3397403Sobrien * Hewlett-Packard Company 3497403Sobrien * 3597403Sobrien * Permission to use, copy, modify, distribute and sell this software 3697403Sobrien * and its documentation for any purpose is hereby granted without fee, 3797403Sobrien * provided that the above copyright notice appear in all copies and 3897403Sobrien * that both that copyright notice and this permission notice appear 3997403Sobrien * in supporting documentation. Hewlett-Packard Company makes no 4097403Sobrien * representations about the suitability of this software for any 4197403Sobrien * purpose. It is provided "as is" without express or implied warranty. 4297403Sobrien * 4397403Sobrien * 4497403Sobrien * Copyright (c) 1996 4597403Sobrien * Silicon Graphics Computer Systems, Inc. 4697403Sobrien * 4797403Sobrien * Permission to use, copy, modify, distribute and sell this software 4897403Sobrien * and its documentation for any purpose is hereby granted without fee, 4997403Sobrien * provided that the above copyright notice appear in all copies and 5097403Sobrien * that both that copyright notice and this permission notice appear 5197403Sobrien * in supporting documentation. Silicon Graphics makes no 5297403Sobrien * representations about the suitability of this software for any 5397403Sobrien * purpose. It is provided "as is" without express or implied warranty. 5497403Sobrien */ 5597403Sobrien 5697403Sobrien/** @file ext/numeric 5797403Sobrien * This file is a GNU extension to the Standard C++ Library (possibly 5897403Sobrien * containing extensions from the HP/SGI STL subset). You should only 5997403Sobrien * include this header if you are using GCC 3 or later. 6097403Sobrien */ 6197403Sobrien 6297403Sobrien#ifndef _EXT_NUMERIC 6397403Sobrien#define _EXT_NUMERIC 6497403Sobrien 6597403Sobrien#pragma GCC system_header 6697403Sobrien#include <bits/concept_check.h> 6797403Sobrien#include <numeric> 6897403Sobrien 6997403Sobrien#include <ext/functional> // For identity_element 7097403Sobrien 7197403Sobriennamespace __gnu_cxx 7297403Sobrien{ 7397403Sobrien // Returns __x ** __n, where __n >= 0. _Note that "multiplication" 7497403Sobrien // is required to be associative, but not necessarily commutative. 7597403Sobrien 7697403Sobrien template<typename _Tp, typename _Integer, typename _MonoidOperation> 7797403Sobrien _Tp 7897403Sobrien __power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op) 7997403Sobrien { 8097403Sobrien if (__n == 0) 8197403Sobrien return identity_element(__monoid_op); 8297403Sobrien else { 8397403Sobrien while ((__n & 1) == 0) { 8497403Sobrien __n >>= 1; 8597403Sobrien __x = __monoid_op(__x, __x); 8697403Sobrien } 8797403Sobrien 8897403Sobrien _Tp __result = __x; 8997403Sobrien __n >>= 1; 9097403Sobrien while (__n != 0) { 9197403Sobrien __x = __monoid_op(__x, __x); 9297403Sobrien if ((__n & 1) != 0) 9397403Sobrien __result = __monoid_op(__result, __x); 9497403Sobrien __n >>= 1; 9597403Sobrien } 9697403Sobrien return __result; 9797403Sobrien } 9897403Sobrien } 9997403Sobrien 10097403Sobrien template<typename _Tp, typename _Integer> 10197403Sobrien inline _Tp 10297403Sobrien __power(_Tp __x, _Integer __n) 10397403Sobrien { return __power(__x, __n, std::multiplies<_Tp>()); } 10497403Sobrien 10597403Sobrien // Alias for the internal name __power. Note that power is an extension, 10697403Sobrien // not part of the C++ standard. 10797403Sobrien 10897403Sobrien template<typename _Tp, typename _Integer, typename _MonoidOperation> 10997403Sobrien inline _Tp 11097403Sobrien power(_Tp __x, _Integer __n, _MonoidOperation __monoid_op) 11197403Sobrien { return __power(__x, __n, __monoid_op); } 11297403Sobrien 11397403Sobrien template<typename _Tp, typename _Integer> 11497403Sobrien inline _Tp 11597403Sobrien power(_Tp __x, _Integer __n) 11697403Sobrien { return __power(__x, __n); } 11797403Sobrien 11897403Sobrien // iota is not part of the C++ standard. It is an extension. 11997403Sobrien 12097403Sobrien template<typename _ForwardIter, typename _Tp> 12197403Sobrien void 12297403Sobrien iota(_ForwardIter __first, _ForwardIter __last, _Tp __value) 12397403Sobrien { 12497403Sobrien // concept requirements 12597403Sobrien __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIter>) 12697403Sobrien __glibcpp_function_requires(_ConvertibleConcept<_Tp, 12797403Sobrien typename std::iterator_traits<_ForwardIter>::value_type>) 12897403Sobrien 12997403Sobrien while (__first != __last) 13097403Sobrien *__first++ = __value++; 13197403Sobrien } 13297403Sobrien 13397403Sobrien} // namespace __gnu_cxx 13497403Sobrien 13597403Sobrien#endif /* _EXT_NUMERIC */ 13697403Sobrien 137