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

ComparatorUtils.h (Maintainer: Clemens Groepl)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Clemens Groepl $
00025 // --------------------------------------------------------------------------
00026 
00027 
00028 #ifndef OPENMS_KERNEL_COMPARATORUTILS_H
00029 #define OPENMS_KERNEL_COMPARATORUTILS_H
00030 
00031 #include <functional>
00032 
00046 namespace OpenMS
00047 {
00048 
00062   template < class Cmp >
00063   struct PointerComparator
00064     : public std::binary_function<typename Cmp::first_argument_type *, typename Cmp::second_argument_type *, typename Cmp::result_type>
00065   {
00066     PointerComparator(PointerComparator const& pCmp) : cmp_ ( pCmp.cmp_ ) {}
00067     PointerComparator(Cmp const& cmp = Cmp() ) : cmp_(cmp) {}
00068 
00069     template < typename T1, typename T2 >
00070     typename Cmp::result_type
00071     operator () ( T1 left, T2 right) const
00072     {
00073       return cmp_ ( *left, *right ); // T must have operator* defined
00074     }
00075 
00076    protected:
00077     Cmp const & cmp_;
00078   };
00079 
00092   template < class Cmp >
00093   PointerComparator < Cmp> pointerComparator ( Cmp const & cmp )
00094   { return PointerComparator < Cmp > ( cmp ); }
00095 
00096 
00097 
00098   //======================================================================
00099 
00100 
00101 
00110   template < class Cmp >
00111   struct ReverseComparator
00112     : std::binary_function<typename Cmp::second_argument_type, typename Cmp::first_argument_type, typename Cmp::result_type>
00113   // (Note that here we must reverse the order of template args!)
00114   {
00115     ReverseComparator(ReverseComparator const& cmp) : cmp_(cmp.cmp_) {}
00116 
00117     ReverseComparator(Cmp const& cmp = Cmp()) : cmp_(cmp) {}
00118 
00119     template < typename T1, typename T2 >
00120     typename Cmp::result_type
00121     operator () ( T1 left, T2 right) const
00122     {
00123       return cmp_ ( right, left ); // the other way round
00124     }
00125 
00126    protected:
00127     Cmp const & cmp_;
00128   };
00129 
00142   template < class Cmp >
00143   ReverseComparator < Cmp> reverseComparator ( Cmp const & cmp )
00144   { return ReverseComparator < Cmp > ( cmp ); }
00145 
00146 
00147 
00148   //======================================================================
00149 
00150 
00151 
00161   template < typename Cmp1, typename Cmp2 >
00162   struct LexicographicComparator
00163     : std::binary_function<typename Cmp1::first_argument_type, typename Cmp1::second_argument_type, bool >
00164   {
00165     LexicographicComparator(Cmp1 const& cmp1 = Cmp1(), Cmp2 const& cmp2 = Cmp2()) : cmp1_(cmp1), cmp2_(cmp2) {}
00166 
00167     template < typename T1, typename T2 >
00168     bool
00169     operator () ( T1 left, T2 right) const
00170     {
00171       if (cmp1_(left, right))
00172       {
00173         return true;
00174       }
00175       else
00176       {
00177         if (cmp1_(right, left))
00178         {
00179           return false;
00180         }
00181         else
00182         {
00183           return cmp2_(left, right);
00184         }
00185       }
00186     }
00187 
00188    protected:
00189     Cmp1 const & cmp1_;
00190     Cmp2 const & cmp2_;
00191   };
00192 
00199   template < typename Cmp1, typename Cmp2 >
00200   LexicographicComparator < Cmp1, Cmp2 > lexicographicComparator ( Cmp1 const & cmp1, Cmp2 const & cmp2 )
00201   { return LexicographicComparator < Cmp1, Cmp2 > ( cmp1, cmp2 ); }
00202 
00203 
00204 }
00205 
00206 #endif // OPENMS_KERNEL_COMPARATORUTILS_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1