DeSiGNAR  0.5a
Data Structures General Library
intutilities.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGINTUTILITIES_H
26 # define DSGINTUTILITIES_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
32  template <typename T>
33  T forward_prod(T, T);
34 
35  template <typename T>
36  T backward_prod(T, T);
37 
38  template <typename T>
39  T factorial(T);
40 
41  template <typename T>
42  T count_permutations(T, T);
43 
44  template <typename T>
45  T count_combinations(T, T);
46 
47  template <typename T>
48  T forward_prod(T a, T b)
49  {
50  static_assert(std::is_integral<T>::value,
51  "Argument must be an integral type");
52 
53  T ret_val = 1;
54 
55  while (a <= b)
56  ret_val *= a++;
57 
58  return ret_val;
59  }
60 
61  template <typename T>
62  T backward_prod(T a, T b)
63  {
64  static_assert(std::is_integral<T>::value,
65  "Argument must be an integral type");
66 
67  T ret_val = 1;
68 
69  while (a >= b)
70  ret_val *= a--;
71 
72  return ret_val;
73  }
74 
75  template <typename T>
76  T factorial(T n)
77  {
78  if (n < 0)
79  throw std::domain_error("Argument must be positive");
80 
81  return forward_prod(T(1), n);
82  }
83 
84  template <typename T>
85  T count_permutations(T n, T r)
86  {
87  if (n < 0 or r < 0)
88  throw std::domain_error("Arguments must be positive numbers");
89 
90  if (r > n)
91  throw std::logic_error("r > n");
92 
93  return backward_prod(n, n - r + T(1));
94  }
95 
96  template <typename T>
97  T count_combinations(T n, T r)
98  {
99  if (n < 0 or r < 0)
100  throw std::domain_error("Arguments must be positive numbers");
101 
102  if (r > n)
103  throw std::logic_error("r > n");
104 
105  return backward_prod(n, n - r + T(1)) / factorial(r);
106  }
107 
108 } // end namespace Designar
109 
110 # endif // DSGINTUTILITIES_H
T forward_prod(T, T)
Definition: intutilities.H:48
T count_combinations(T, T)
Definition: intutilities.H:97
T factorial(T)
Definition: intutilities.H:76
T count_permutations(T, T)
Definition: intutilities.H:85
Definition: array.H:32
Value & value(MapKey< Key, Value > &item)
Definition: map.H:77
T backward_prod(T, T)
Definition: intutilities.H:62