BooleanRedux.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ALLANDANY_H
00026 #define EIGEN_ALLANDANY_H
00027 
00028 namespace Eigen { 
00029 
00030 namespace internal {
00031 
00032 template<typename Derived, int UnrollCount>
00033 struct all_unroller
00034 {
00035   enum {
00036     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00037     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00038   };
00039 
00040   static inline bool run(const Derived &mat)
00041   {
00042     return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
00043   }
00044 };
00045 
00046 template<typename Derived>
00047 struct all_unroller<Derived, 1>
00048 {
00049   static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
00050 };
00051 
00052 template<typename Derived>
00053 struct all_unroller<Derived, Dynamic>
00054 {
00055   static inline bool run(const Derived &) { return false; }
00056 };
00057 
00058 template<typename Derived, int UnrollCount>
00059 struct any_unroller
00060 {
00061   enum {
00062     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00063     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00064   };
00065 
00066   static inline bool run(const Derived &mat)
00067   {
00068     return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
00069   }
00070 };
00071 
00072 template<typename Derived>
00073 struct any_unroller<Derived, 1>
00074 {
00075   static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
00076 };
00077 
00078 template<typename Derived>
00079 struct any_unroller<Derived, Dynamic>
00080 {
00081   static inline bool run(const Derived &) { return false; }
00082 };
00083 
00084 } // end namespace internal
00085 
00093 template<typename Derived>
00094 inline bool DenseBase<Derived>::all() const
00095 {
00096   enum {
00097     unroll = SizeAtCompileTime != Dynamic
00098           && CoeffReadCost != Dynamic
00099           && NumTraits<Scalar>::AddCost != Dynamic
00100           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00101   };
00102   if(unroll)
00103     return internal::all_unroller<Derived,
00104                            unroll ? int(SizeAtCompileTime) : Dynamic
00105      >::run(derived());
00106   else
00107   {
00108     for(Index j = 0; j < cols(); ++j)
00109       for(Index i = 0; i < rows(); ++i)
00110         if (!coeff(i, j)) return false;
00111     return true;
00112   }
00113 }
00114 
00119 template<typename Derived>
00120 inline bool DenseBase<Derived>::any() const
00121 {
00122   enum {
00123     unroll = SizeAtCompileTime != Dynamic
00124           && CoeffReadCost != Dynamic
00125           && NumTraits<Scalar>::AddCost != Dynamic
00126           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00127   };
00128   if(unroll)
00129     return internal::any_unroller<Derived,
00130                            unroll ? int(SizeAtCompileTime) : Dynamic
00131            >::run(derived());
00132   else
00133   {
00134     for(Index j = 0; j < cols(); ++j)
00135       for(Index i = 0; i < rows(); ++i)
00136         if (coeff(i, j)) return true;
00137     return false;
00138   }
00139 }
00140 
00145 template<typename Derived>
00146 inline typename DenseBase<Derived>::Index DenseBase<Derived>::count() const
00147 {
00148   return derived().template cast<bool>().template cast<Index>().sum();
00149 }
00150 
00151 } // end namespace Eigen
00152 
00153 #endif // EIGEN_ALLANDANY_H