pow.hh 2.56 KB
Newer Older
1
2
// Copyright (C) 2007, 2008, 2009, 2013 EPITA Research and Development
// Laboratory (LRDE)
3
//
4
// This file is part of Olena.
5
//
6
7
8
9
10
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
11
12
13
14
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
16
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
17
18
//
// As a special exception, you may use this file as part of a free
19
// software project without restriction.  Specifically, if other files
20
// instantiate templates or use macros or inline functions from this
21
22
23
24
25
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
26

Thierry Geraud's avatar
Thierry Geraud committed
27
28
#ifndef MLN_METAL_MATH_POW_HH
# define MLN_METAL_MATH_POW_HH
29

30
/*! \file
31
 *
Thierry Geraud's avatar
Thierry Geraud committed
32
 * \brief Definition of the 'power' static function.
33
34
 */

Thierry Geraud's avatar
Thierry Geraud committed
35
36
37
# include <mln/metal/bool.hh>
# include <mln/metal/int.hh>

38

39
40
# define mlc_pow(X, N)      typename mln::metal::math::pow< X, N >::ret
# define mlc_pow_int(x, n)           mln::metal::math::pow_int< x, n >::value
41
42
43



44
45
46
47
48
49
namespace mln
{

  namespace metal
  {

Thierry Geraud's avatar
Thierry Geraud committed
50
    namespace math
51
52
    {

Thierry Geraud's avatar
Thierry Geraud committed
53
54
55
56
57
58
59
60
      // pow_int<x, n>

      namespace impl
      {

	template <int x, int n>
	struct pow_int_
	{
61
	  static const int value = x * pow_int_<x, n-1>::value;
Thierry Geraud's avatar
Thierry Geraud committed
62
63
64
65
66
	};

	template <int x>
	struct pow_int_< x, 0 >
	{
67
	  static const int value = 1;
Thierry Geraud's avatar
Thierry Geraud committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	};

	template <>
	struct pow_int_< 0, 0 >;
      

	// Entry.

	template <int x, int n, bool b>
	struct pow_int_if_ : pow_int_<x, n>
	{
	};

	template <int x, int n>
	struct pow_int_if_< x, n, false >
	{
	};

      }

      template <int x, int n>
      struct pow_int : impl::pow_int_if_< x, n,
					  (n >= 0 && ! (x == 0 && n == 0)) >
      {
      };


      // pow<X, N>

      template <typename X, typename N>
      struct pow;

      template <int x, int n>
101
      struct pow< int_<x>, int_<n> >
Thierry Geraud's avatar
Thierry Geraud committed
102
      {
103
	typedef int_< pow_int<x, n>::value > ret;
Thierry Geraud's avatar
Thierry Geraud committed
104
105
      };

106

107
    } // end of namespace mln::metal::math
108
109
110
111
112
113

  } // end of namespace mln::metal

} // end of namespace mln


Thierry Geraud's avatar
Thierry Geraud committed
114
#endif // ! MLN_METAL_MATH_POW_HH