BooleanRedux.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_ALLANDANY_H
26 #define EIGEN_ALLANDANY_H
27 
28 namespace Eigen {
29 
30 namespace internal {
31 
32 template<typename Derived, int UnrollCount>
33 struct all_unroller
34 {
35  enum {
36  col = (UnrollCount-1) / Derived::RowsAtCompileTime,
37  row = (UnrollCount-1) % Derived::RowsAtCompileTime
38  };
39 
40  static inline bool run(const Derived &mat)
41  {
42  return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
43  }
44 };
45 
46 template<typename Derived>
47 struct all_unroller<Derived, 1>
48 {
49  static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
50 };
51 
52 template<typename Derived>
53 struct all_unroller<Derived, Dynamic>
54 {
55  static inline bool run(const Derived &) { return false; }
56 };
57 
58 template<typename Derived, int UnrollCount>
59 struct any_unroller
60 {
61  enum {
62  col = (UnrollCount-1) / Derived::RowsAtCompileTime,
63  row = (UnrollCount-1) % Derived::RowsAtCompileTime
64  };
65 
66  static inline bool run(const Derived &mat)
67  {
68  return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
69  }
70 };
71 
72 template<typename Derived>
73 struct any_unroller<Derived, 1>
74 {
75  static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
76 };
77 
78 template<typename Derived>
79 struct any_unroller<Derived, Dynamic>
80 {
81  static inline bool run(const Derived &) { return false; }
82 };
83 
84 } // end namespace internal
85 
93 template<typename Derived>
94 inline bool DenseBase<Derived>::all() const
95 {
96  enum {
97  unroll = SizeAtCompileTime != Dynamic
98  && CoeffReadCost != Dynamic
100  && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
101  };
102  if(unroll)
103  return internal::all_unroller<Derived,
104  unroll ? int(SizeAtCompileTime) : Dynamic
105  >::run(derived());
106  else
107  {
108  for(Index j = 0; j < cols(); ++j)
109  for(Index i = 0; i < rows(); ++i)
110  if (!coeff(i, j)) return false;
111  return true;
112  }
113 }
114 
119 template<typename Derived>
120 inline bool DenseBase<Derived>::any() const
121 {
122  enum {
123  unroll = SizeAtCompileTime != Dynamic
124  && CoeffReadCost != Dynamic
126  && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
127  };
128  if(unroll)
129  return internal::any_unroller<Derived,
130  unroll ? int(SizeAtCompileTime) : Dynamic
131  >::run(derived());
132  else
133  {
134  for(Index j = 0; j < cols(); ++j)
135  for(Index i = 0; i < rows(); ++i)
136  if (coeff(i, j)) return true;
137  return false;
138  }
139 }
140 
145 template<typename Derived>
147 {
148  return derived().template cast<bool>().template cast<Index>().sum();
149 }
150 
151 } // end namespace Eigen
152 
153 #endif // EIGEN_ALLANDANY_H