Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

more/metanumeric.h

Go to the documentation of this file.
00001 //  Copyright (C) 2001  Petter Urkedal (petter.urkedal@matfys.lth.se)
00002 //
00003 //  This file is free software; you can redistribute it and/or modify
00004 //  it under the terms of the GNU General Public License as published by
00005 //  the Free Software Foundation; either version 2 of the License, or
00006 //  (at your option) any later version.
00007 //
00008 //  This file is distributed in the hope that it will be useful,
00009 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //  GNU General Public License for more details.
00012 //
00013 //  You should have received a copy of the GNU General Public License
00014 //  along with this program; if not, write to the Free Software
00015 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016 //
00017 //  As a special exception, you may use this file as part of a free
00018 //  software library without restriction.  Specifically, if other files
00019 //  instantiate templates or use macros or inline functions from this
00020 //  file, or you compile this file and link it with other files to
00021 //  produce an executable, this file does not by itself cause the
00022 //  resulting executable to be covered by the GNU General Public
00023 //  License.  This exception does not however invalidate any other
00024 //  reasons why the executable file might be covered by the GNU General
00025 //  Public License.
00026 //
00027 //  $Id: metanumeric.h,v 1.1 2002/05/30 22:52:07 petter_urkedal Exp $
00028 
00029 
00030 
00031 #ifndef MORE_METANUMERIC_H
00032 #define MORE_METANUMERIC_H
00033 
00034 namespace more {
00035 
00036   template<int N> struct pow_;
00037   template<int N, int Selector>
00038     struct pow_helper_ {
00039         template<typename T>
00040           static T eval(T const& x) { return T(1)/pow_<-N>::eval(x); }
00041     };
00042   template<int N>
00043     struct pow_helper_<N, 1> {
00044         template<typename T>
00045           static T eval(T const& x) { return x*pow2(pow_<N/2>::eval(x)); }
00046     };
00047   template<int N>
00048     struct pow_helper_<N, 0> {
00049         template<typename T>
00050           static T eval(T const& x) { return pow2(pow_<N/2>::eval(x)); }
00051     };
00052   template<int N> struct pow_ : pow_helper_<N, (N%2 - (N<0)*2)> {};
00053   template<> struct pow_<0>
00054     { template<typename T> static T eval(T const& x) { return one; } };
00055   template<> struct pow_<1>
00056     { template<typename T> static T eval(T const& x) { return x; } };
00057 
00058   // Metaprogram to calculate X^Y for Y >= 0.
00059 
00060   template<int X, int Y> struct power_
00061     { static int const eval = X*power_<X, Y-1>::eval; };
00062   template<int X> struct power_<X, 0> { static int const eval = 1; };
00063 
00064   // Metaprogram to calculate X!/Y! for X >= Y, with default Y=0.
00065   // NOT TESTED
00066   template<int X, int Y=0> struct factorial_
00067     { static int const eval = X*factorial_<X-1, Y>::eval; };
00068   template<int X> struct factorial_<X, X> { static int const eval = 1; };
00069 
00070   // Binominal coefficients; pick out M from N.
00071 
00072   template<int N, int M> struct binominal_ {
00073       static int const eval
00074         = binominal_<N-1, M-1>::eval + binominal_<N-1, M>::eval;
00075   };
00076   template<int N> struct binominal_<N, 0> { static int const eval = 1; };
00077   template<int N> struct binominal_<N, N> { static int const eval = 1; };
00078   template<> struct binominal_<0, 0> { static int const eval = 1; };
00079 
00080 
00081 } // more
00082 
00083 #endif

Generated on Sat Sep 7 19:11:19 2002 for more with Doxygen 1.2.13.1. Doxygen 1.2.13.1 is written and copyright 1997-2002 by Dimitri van Heesch.