OpenWalnut  1.4.0
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 evaluation of factorials.
32  */
33 template< std::size_t n >
34 struct WFactorial
35 {
36  enum
37  {
38  value = n * WFactorial< n - 1 >::value
39  };
40 };
41 
42 /**
43  * Specialization for n = 0.
44  */
45 template<>
46 struct WFactorial< 0 >
47 {
48  enum
49  {
50  value = 1
51  };
52 };
53 
54 /**
55  * Implements compile-time calculation of binomial coefficients.
56  *
57  *
58  * WBinom< n, k >::value = n! / ( k!(n-k)! ).
59  *
60  * \note For k > n or n == k == 0, compilation fails.
61  */
62 template< std::size_t n, std::size_t k >
63 struct WBinom
64 {
65  /**
66  * Using an enum here instead of a static constant.
67  */
68  enum
69  {
70  /**
71  * The computed value.
72  */
73  value = WBinom< n - 1, k - 1 >::value + WBinom< n - 1, k >::value
74  };
75 };
76 
77 /**
78  * Specialization for n = k.
79  */
80 template< std::size_t n >
81 struct WBinom< n, n >
82 {
83  /**
84  * Using an enum here instead of a static constant.
85  */
86  enum
87  {
88  /**
89  * The computed value.
90  */
91  value = 1
92  };
93 };
94 
95 /**
96  * Specialization for k = 0.
97  */
98 template< std::size_t n >
99 struct WBinom< n, 0 >
100 {
101  /**
102  * Using an enum here instead of a static constant.
103  */
104  enum
105  {
106  /**
107  * The computed value.
108  */
109  value = 1
110  };
111 };
112 
113 /**
114  * This specialization of the WBinom struct is needed to avoid
115  * infinite recursion in case of k > n. The compiler should abort
116  * compilation with an error message.
117  */
118 template< std::size_t k >
119 struct WBinom< 0, k >
120 {
121 };
122 
123 /**
124  * Compute the nth power of a value.
125  *
126  * For base == exponent == 0, compilation fails.
127  */
128 template< std::size_t base, std::size_t exponent >
129 struct WPower
130 {
131  /**
132  * Using an enum here instead of a static constant.
133  */
134  enum
135  {
136  /**
137  * The computed value.
138  */
139  value = base * WPower< base, exponent - 1 >::value
140  };
141 };
142 
143 /**
144  * Compute the nth power of a value.
145  *
146  * Specialization for exponent = 0.
147  */
148 template< std::size_t base >
149 struct WPower< base, 0 >
150 {
151  /**
152  * Using an enum here instead of a static constant.
153  */
154  enum
155  {
156  /**
157  * The computed value.
158  */
159  value = 1
160  };
161 };
162 
163 /**
164  * Compute the nth power of a value.
165  *
166  * Specialization for exponent = 0.
167  */
168 template< std::size_t exponent >
169 struct WPower< 0, exponent >
170 {
171  /**
172  * Using an enum here instead of a static constant.
173  */
174  enum
175  {
176  /**
177  * The computed value.
178  */
179  value = 0
180  };
181 };
182 
183 #endif // WCOMPILETIMEFUNCTIONS_H
The computed value.
Compute the nth power of a value.
Implements compile-time calculation of binomial coefficients.
Implements compile-time evaluation of factorials.
The computed value.