OpenWalnut 1.2.5

WCompileTimeFunctions.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WCOMPILETIMEFUNCTIONS_H
00026 #define WCOMPILETIMEFUNCTIONS_H
00027 
00028 #include <string>
00029 
00030 /**
00031  * Implements compile-time calculation of binomial coefficients.
00032  *
00033  *
00034  * WBinom< n, k >::value = n! / ( k!(n-k)! ).
00035  *
00036  * \note For k > n or n == k == 0, compilation fails.
00037  */
00038 template< std::size_t n, std::size_t k >
00039 struct WBinom
00040 {
00041     /**
00042      * Using an enum here instead of a static constant.
00043      */
00044     enum
00045     {
00046         /**
00047          * The computed value.
00048          */
00049         value = WBinom< n - 1, k - 1 >::value + WBinom< n - 1, k >::value
00050     };
00051 };
00052 
00053 /**
00054  * Specialization for n = k.
00055  */
00056 template< std::size_t n >
00057 struct WBinom< n, n >
00058 {
00059     /**
00060      * Using an enum here instead of a static constant.
00061      */
00062     enum
00063     {
00064         /**
00065          * The computed value.
00066          */
00067         value = 1
00068     };
00069 };
00070 
00071 /**
00072  * Specialization for k = 0.
00073  */
00074 template< std::size_t n >
00075 struct WBinom< n, 0 >
00076 {
00077     /**
00078      * Using an enum here instead of a static constant.
00079      */
00080     enum
00081     {
00082         /**
00083          * The computed value.
00084          */
00085         value = 1
00086     };
00087 };
00088 
00089 /**
00090  * This specialization of the WBinom struct is needed to avoid
00091  * infinite recursion in case of k > n. The compiler should abort
00092  * compilation with an error message.
00093  */
00094 template< std::size_t k >
00095 struct WBinom< 0, k >
00096 {
00097 };
00098 
00099 /**
00100  * Compute the nth power of a value.
00101  *
00102  * For base == exponent == 0, compilation fails.
00103  */
00104 template< std::size_t base, std::size_t exponent >
00105 struct WPower
00106 {
00107     /**
00108      * Using an enum here instead of a static constant.
00109      */
00110     enum
00111     {
00112         /**
00113          * The computed value.
00114          */
00115         value = base * WPower< base, exponent - 1 >::value
00116     };
00117 };
00118 
00119 /**
00120  * Compute the nth power of a value.
00121  *
00122  * Specialization for exponent = 0.
00123  */
00124 template< std::size_t base >
00125 struct WPower< base, 0 >
00126 {
00127     /**
00128      * Using an enum here instead of a static constant.
00129      */
00130     enum
00131     {
00132         /**
00133          * The computed value.
00134          */
00135         value = 1
00136     };
00137 };
00138 
00139 /**
00140  * Compute the nth power of a value.
00141  *
00142  * Specialization for exponent = 0.
00143  */
00144 template< std::size_t exponent >
00145 struct WPower< 0, exponent >
00146 {
00147     /**
00148      * Using an enum here instead of a static constant.
00149      */
00150     enum
00151     {
00152         /**
00153          * The computed value.
00154          */
00155         value = 0
00156     };
00157 };
00158 
00159 #endif  // WCOMPILETIMEFUNCTIONS_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends