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