Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

ComparatorUtils.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Clemens Groepl $
00032 // $Authors: $
00033 // --------------------------------------------------------------------------
00034 
00035 
00036 #ifndef OPENMS_KERNEL_COMPARATORUTILS_H
00037 #define OPENMS_KERNEL_COMPARATORUTILS_H
00038 
00039 #include <functional>
00040 
00155 namespace OpenMS
00156 {
00157 
00172   template <class Cmp>
00173   struct PointerComparator :
00174     public std::binary_function<typename Cmp::first_argument_type *, typename Cmp::second_argument_type *, typename Cmp::result_type>
00175   {
00176     PointerComparator(PointerComparator const & pCmp) :
00177       cmp_(pCmp.cmp_)
00178     {}
00179     PointerComparator(Cmp const & cmp = Cmp()) :
00180       cmp_(cmp)
00181     {}
00182 
00183     template <typename T1, typename T2>
00184     typename Cmp::result_type
00185     operator()(T1 left, T2 right) const
00186     {
00187       return cmp_(*left, *right);          // T must have operator* defined
00188     }
00189 
00190 protected:
00191     Cmp const & cmp_;
00192   };
00193 
00209   template <class Cmp>
00210   PointerComparator<Cmp> pointerComparator(Cmp const & cmp)
00211   {
00212     return PointerComparator<Cmp>(cmp);
00213   }
00214 
00223   template <class Cmp>
00224   struct ReverseComparator :
00225     std::binary_function<typename Cmp::second_argument_type, typename Cmp::first_argument_type, typename Cmp::result_type>
00226     // (Note that here we must reverse the order of template args!)
00227   {
00228     ReverseComparator(ReverseComparator const & cmp) :
00229       cmp_(cmp.cmp_) {}
00230 
00231     ReverseComparator(Cmp const & cmp = Cmp()) :
00232       cmp_(cmp) {}
00233 
00234     template <typename T1, typename T2>
00235     typename Cmp::result_type
00236     operator()(T1 left, T2 right) const
00237     {
00238       return cmp_(right, left);          // the other way round
00239     }
00240 
00241 protected:
00242     Cmp const & cmp_;
00243   };
00244 
00260   template <class Cmp>
00261   ReverseComparator<Cmp> reverseComparator(Cmp const & cmp)
00262   {
00263     return ReverseComparator<Cmp>(cmp);
00264   }
00265 
00275   template <typename Cmp1, typename Cmp2>
00276   struct LexicographicComparator :
00277     std::binary_function<typename Cmp1::first_argument_type, typename Cmp1::second_argument_type, bool>
00278   {
00279     LexicographicComparator(Cmp1 const & cmp1 = Cmp1(), Cmp2 const & cmp2 = Cmp2()) :
00280       cmp1_(cmp1), cmp2_(cmp2) {}
00281 
00282     template <typename T1, typename T2>
00283     bool
00284     operator()(T1 left, T2 right) const
00285     {
00286       if (cmp1_(left, right))
00287       {
00288         return true;
00289       }
00290       else
00291       {
00292         if (cmp1_(right, left))
00293         {
00294           return false;
00295         }
00296         else
00297         {
00298           return cmp2_(left, right);
00299         }
00300       }
00301     }
00302 
00303 protected:
00304     Cmp1 const & cmp1_;
00305     Cmp2 const & cmp2_;
00306   };
00307 
00316   template <typename Cmp1, typename Cmp2>
00317   LexicographicComparator<Cmp1, Cmp2> lexicographicComparator(Cmp1 const & cmp1, Cmp2 const & cmp2)
00318   {
00319     return LexicographicComparator<Cmp1, Cmp2>(cmp1, cmp2);
00320   }
00321 
00325   template <typename PairType>
00326   struct PairComparatorFirstElement :
00327     std::binary_function<PairType, PairType, bool>
00328   {
00329     bool operator()(const PairType & left, const PairType & right) const
00330     {
00331       return left.first < right.first;
00332     }
00333 
00334   };
00335 
00339   template <typename PairType>
00340   struct PairComparatorSecondElement :
00341     std::binary_function<PairType, PairType, bool>
00342   {
00343     bool operator()(const PairType & left, const PairType & right) const
00344     {
00345       return left.second < right.second;
00346     }
00347 
00348   };
00349 
00353   template <typename PairType>
00354   struct PairComparatorFirstElementMore :
00355     std::binary_function<PairType, PairType, bool>
00356   {
00357     bool operator()(const PairType & left, const PairType & right) const
00358     {
00359       return left.first > right.first;
00360     }
00361 
00362   };
00363 
00367   template <typename PairType>
00368   struct PairComparatorSecondElementMore :
00369     std::binary_function<PairType, PairType, bool>
00370   {
00371     bool operator()(const PairType & left, const PairType & right) const
00372     {
00373       return left.second > right.second;
00374     }
00375 
00376   };
00377 
00381   template <typename PairType>
00382   struct PairMatcherFirstElement :
00383     std::binary_function<PairType, PairType, bool>
00384   {
00385     bool operator()(const PairType & left, const PairType & right) const
00386     {
00387       return left.first == right.first;
00388     }
00389 
00390   };
00391 
00395   template <typename PairType>
00396   struct PairMatcherSecondElement :
00397     std::binary_function<PairType, PairType, bool>
00398   {
00399     bool operator()(const PairType & left, const PairType & right) const
00400     {
00401       return left.second == right.second;
00402     }
00403 
00404   };
00405 
00413   template <typename CompareType>
00414   struct EqualInTolerance :
00415     public std::binary_function<CompareType, CompareType, bool>
00416   {
00417     CompareType & tolerance;
00418 
00419     explicit EqualInTolerance(CompareType & c) :
00420       tolerance(c)
00421     {}
00422 
00423     bool operator()(CompareType i, CompareType j)
00424     {
00425       CompareType diff = fabs(i - j);
00426       return diff <= tolerance;
00427     }
00428 
00429   };
00430 }
00431 
00432 #endif // OPENMS_KERNEL_COMPARATORUTILS_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:37 using doxygen 1.7.1