00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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);
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
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);
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