RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
zero.hh
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the C++-library RFFGen. */
4 /* Copyright 2015 Lars Lubkoll */
5 /* */
6 /* RFFGen is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* RFFGen is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with RFFGen. If not, see <http://www.gnu.org/licenses/>. */
18 /* */
19 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20 
21 #ifndef RFFGEN_UTIL_ZERO_HH
22 #define RFFGEN_UTIL_ZERO_HH
23 
24 #include <utility>
25 
26 #include "staticChecks.hh"
27 
28 namespace RFFGen
29 {
33  namespace Checks
34  {
35  template <class Matrix>
36  using TryCallToZeroes = decltype(std::declval<Matrix>().zeros());
37 
38  template <class Matrix>
39  using TryCallToFill = decltype(std::declval<Matrix>().fill(0));
40  }
45  template <class Matrix, class = void>
47  struct Zero
48  {
52  Matrix operator()() const
53  {
54  return Matrix(0.);
55  }
56  };
57 
59  template <class Matrix>
60  struct Zero< Matrix , void_t<Checks::TryCallToFill<Matrix> > >
61  {
65  Matrix operator()() const
66  {
67  Matrix m;
68  m.fill(0);
69  return m;
70  }
71 
75  Matrix& operator()(Matrix& m) const
76  {
77  m.fill(0);
78  return m;
79  }
80  };
81 
86  template <class Matrix, class = std::enable_if_t<Checks::isConstantSizeMatrix<Matrix>()> >
87  Matrix zero()
88  {
89  return Zero<Matrix>()();
90  }
91 
96  template <class Matrix, class = std::enable_if_t<!Checks::isConstantSizeMatrix<Matrix>()> >
97  constexpr Matrix zero(int rows, int cols)
98  {
99  Matrix m(rows,cols);
100  return Zero<Matrix>()(m);
101  }
102 
103 }
104 
105 #endif // RFFGEN_UTIL_ZERO_HH
typename Detail::voider< Types...>::type void_t
Most fascinating type ever. Is always void.
Definition: voider.hh:40
Static checks for detection of presence of different operators and functions.
Specialize this struct for your matrix type if a zero matrix cannot be generated via Matrix(0...
Definition: zero.hh:47
Matrix & operator()(Matrix &m) const
Set all entries of m to 0.
Definition: zero.hh:75
Matrix zero()
Definition: zero.hh:87
Matrix operator()() const
Definition: zero.hh:52