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 internal {
00029 
00030 template<typename Derived, int UnrollCount>
00031 struct all_unroller
00032 {
00033   enum {
00034     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00035     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00036   };
00037 
00038   inline static bool run(const Derived &mat)
00039   {
00040     return all_unroller<Derived, UnrollCount-1>::run(mat) && mat.coeff(row, col);
00041   }
00042 };
00043 
00044 template<typename Derived>
00045 struct all_unroller<Derived, 1>
00046 {
00047   inline static bool run(const Derived &mat) { return mat.coeff(0, 0); }
00048 };
00049 
00050 template<typename Derived>
00051 struct all_unroller<Derived, Dynamic>
00052 {
00053   inline static bool run(const Derived &) { return false; }
00054 };
00055 
00056 template<typename Derived, int UnrollCount>
00057 struct any_unroller
00058 {
00059   enum {
00060     col = (UnrollCount-1) / Derived::RowsAtCompileTime,
00061     row = (UnrollCount-1) % Derived::RowsAtCompileTime
00062   };
00063 
00064   inline static bool run(const Derived &mat)
00065   {
00066     return any_unroller<Derived, UnrollCount-1>::run(mat) || mat.coeff(row, col);
00067   }
00068 };
00069 
00070 template<typename Derived>
00071 struct any_unroller<Derived, 1>
00072 {
00073   inline static bool run(const Derived &mat) { return mat.coeff(0, 0); }
00074 };
00075 
00076 template<typename Derived>
00077 struct any_unroller<Derived, Dynamic>
00078 {
00079   inline static bool run(const Derived &) { return false; }
00080 };
00081 
00082 } // end namespace internal
00083 
00091 template<typename Derived>
00092 inline bool DenseBase<Derived>::all() const
00093 {
00094   enum {
00095     unroll = SizeAtCompileTime != Dynamic
00096           && CoeffReadCost != Dynamic
00097           && NumTraits<Scalar>::AddCost != Dynamic
00098           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00099   };
00100   if(unroll)
00101     return internal::all_unroller<Derived,
00102                            unroll ? int(SizeAtCompileTime) : Dynamic
00103      >::run(derived());
00104   else
00105   {
00106     for(Index j = 0; j < cols(); ++j)
00107       for(Index i = 0; i < rows(); ++i)
00108         if (!coeff(i, j)) return false;
00109     return true;
00110   }
00111 }
00112 
00117 template<typename Derived>
00118 inline bool DenseBase<Derived>::any() const
00119 {
00120   enum {
00121     unroll = SizeAtCompileTime != Dynamic
00122           && CoeffReadCost != Dynamic
00123           && NumTraits<Scalar>::AddCost != Dynamic
00124           && SizeAtCompileTime * (CoeffReadCost + NumTraits<Scalar>::AddCost) <= EIGEN_UNROLLING_LIMIT
00125   };
00126   if(unroll)
00127     return internal::any_unroller<Derived,
00128                            unroll ? int(SizeAtCompileTime) : Dynamic
00129            >::run(derived());
00130   else
00131   {
00132     for(Index j = 0; j < cols(); ++j)
00133       for(Index i = 0; i < rows(); ++i)
00134         if (coeff(i, j)) return true;
00135     return false;
00136   }
00137 }
00138 
00143 template<typename Derived>
00144 inline typename DenseBase<Derived>::Index DenseBase<Derived>::count() const
00145 {
00146   return derived().template cast<bool>().template cast<Index>().sum();
00147 }
00148 
00149 #endif // EIGEN_ALLANDANY_H


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:31