OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WCompileTimeFunctions.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCOMPILETIMEFUNCTIONS_H
26 #define WCOMPILETIMEFUNCTIONS_H
27 
28 #include <string>
29 
30 /**
31  * Implements compile-time calculation of binomial coefficients.
32  *
33  *
34  * WBinom< n, k >::value = n! / ( k!(n-k)! ).
35  *
36  * \note For k > n or n == k == 0, compilation fails.
37  */
38 template< std::size_t n, std::size_t k >
39 struct WBinom
40 {
41  /**
42  * Using an enum here instead of a static constant.
43  */
44  enum
45  {
46  /**
47  * The computed value.
48  */
49  value = WBinom< n - 1, k - 1 >::value + WBinom< n - 1, k >::value
50  };
51 };
52 
53 /**
54  * Specialization for n = k.
55  */
56 template< std::size_t n >
57 struct WBinom< n, n >
58 {
59  /**
60  * Using an enum here instead of a static constant.
61  */
62  enum
63  {
64  /**
65  * The computed value.
66  */
67  value = 1
68  };
69 };
70 
71 /**
72  * Specialization for k = 0.
73  */
74 template< std::size_t n >
75 struct WBinom< n, 0 >
76 {
77  /**
78  * Using an enum here instead of a static constant.
79  */
80  enum
81  {
82  /**
83  * The computed value.
84  */
85  value = 1
86  };
87 };
88 
89 /**
90  * This specialization of the WBinom struct is needed to avoid
91  * infinite recursion in case of k > n. The compiler should abort
92  * compilation with an error message.
93  */
94 template< std::size_t k >
95 struct WBinom< 0, k >
96 {
97 };
98 
99 /**
100  * Compute the nth power of a value.
101  *
102  * For base == exponent == 0, compilation fails.
103  */
104 template< std::size_t base, std::size_t exponent >
105 struct WPower
106 {
107  /**
108  * Using an enum here instead of a static constant.
109  */
110  enum
111  {
112  /**
113  * The computed value.
114  */
115  value = base * WPower< base, exponent - 1 >::value
116  };
117 };
118 
119 /**
120  * Compute the nth power of a value.
121  *
122  * Specialization for exponent = 0.
123  */
124 template< std::size_t base >
125 struct WPower< base, 0 >
126 {
127  /**
128  * Using an enum here instead of a static constant.
129  */
130  enum
131  {
132  /**
133  * The computed value.
134  */
135  value = 1
136  };
137 };
138 
139 /**
140  * Compute the nth power of a value.
141  *
142  * Specialization for exponent = 0.
143  */
144 template< std::size_t exponent >
145 struct WPower< 0, exponent >
146 {
147  /**
148  * Using an enum here instead of a static constant.
149  */
150  enum
151  {
152  /**
153  * The computed value.
154  */
155  value = 0
156  };
157 };
158 
159 #endif // WCOMPILETIMEFUNCTIONS_H