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 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_ALLANDANY_H
00011 #define EIGEN_ALLANDANY_H
00012 
00013 namespace Eigen { 
00014 
00015 namespace internal {
00016 
00017 template<typename Derived, int UnrollCount>
00018 struct all_unroller
00019 {
00020   enum {
00021     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00022     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00023   };
00024 
00025   static inline bool run(const Derived &mat)
00026   {
00027     return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
00028   }
00029 };
00030 
00031 template<typename Derived>
00032 struct all_unroller<Derived, 1>
00033 {
00034   static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
00035 };
00036 
00037 template<typename Derived>
00038 struct all_unroller<Derived, Dynamic>
00039 {
00040   static inline bool run(const Derived &) { return false; }
00041 };
00042 
00043 template<typename Derived, int UnrollCount>
00044 struct any_unroller
00045 {
00046   enum {
00047     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00048     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00049   };
00050 
00051   static inline bool run(const Derived &mat)
00052   {
00053     return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
00054   }
00055 };
00056 
00057 template<typename Derived>
00058 struct any_unroller<Derived, 1>
00059 {
00060   static inline bool run(const Derived &mat) { return mat.coeff(0, 0); }
00061 };
00062 
00063 template<typename Derived>
00064 struct any_unroller<Derived, Dynamic>
00065 {
00066   static inline bool run(const Derived &) { return false; }
00067 };
00068 
00069 } // end namespace internal
00070 
00078 template<typename Derived>
00079 inline bool DenseBase<Derived>::all() const
00080 {
00081   enum {
00082     unroll = SizeAtCompileTime != Dynamic
00083           && CoeffReadCost != Dynamic
00084           && NumTraits<Scalar>::AddCost != Dynamic
00085           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00086   };
00087   if(unroll)
00088     return internal::all_unroller<Derived,
00089                            unroll ? int(SizeAtCompileTime) : Dynamic
00090      >::run(derived());
00091   else
00092   {
00093     for(Index j = 0; j < cols(); ++j)
00094       for(Index i = 0; i < rows(); ++i)
00095         if (!coeff(i, j)) return false;
00096     return true;
00097   }
00098 }
00099 
00104 template<typename Derived>
00105 inline bool DenseBase<Derived>::any() const
00106 {
00107   enum {
00108     unroll = SizeAtCompileTime != Dynamic
00109           && CoeffReadCost != Dynamic
00110           && NumTraits<Scalar>::AddCost != Dynamic
00111           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00112   };
00113   if(unroll)
00114     return internal::any_unroller<Derived,
00115                            unroll ? int(SizeAtCompileTime) : Dynamic
00116            >::run(derived());
00117   else
00118   {
00119     for(Index j = 0; j < cols(); ++j)
00120       for(Index i = 0; i < rows(); ++i)
00121         if (coeff(i, j)) return true;
00122     return false;
00123   }
00124 }
00125 
00130 template<typename Derived>
00131 inline typename DenseBase<Derived>::Index DenseBase<Derived>::count() const
00132 {
00133   return derived().template cast<bool>().template cast<Index>().sum();
00134 }
00135 
00136 } // end namespace Eigen
00137 
00138 #endif // EIGEN_ALLANDANY_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:24:12