OpenMS  2.7.0
ComparatorUtils.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 
36 #pragma once
37 
38 #include <functional>
39 
154 namespace OpenMS
155 {
156 
171  template <class Cmp>
173  {
175  cmp_(pCmp.cmp_)
176  {}
177  PointerComparator(Cmp const & cmp = Cmp()) :
178  cmp_(cmp)
179  {}
180 
181  template <typename T1, typename T2>
182  bool operator()(T1 left, T2 right) const
183  {
184  return cmp_(*left, *right); // T must have operator* defined
185  }
186 
187 protected:
188  Cmp const & cmp_;
189  };
190 
206  template <class Cmp>
208  {
209  return PointerComparator<Cmp>(cmp);
210  }
211 
220  template <class Cmp>
222  // (Note that here we must reverse the order of template args!)
223  {
225  cmp_(cmp.cmp_) {}
226 
227  ReverseComparator(Cmp const & cmp = Cmp()) :
228  cmp_(cmp) {}
229 
230  template <typename T1, typename T2>
231  bool operator()(T1 left, T2 right) const
232  {
233  return cmp_(right, left); // the other way round
234  }
235 
236 protected:
237  Cmp const & cmp_;
238  };
239 
255  template <class Cmp>
257  {
258  return ReverseComparator<Cmp>(cmp);
259  }
260 
270  template <typename Cmp1, typename Cmp2>
272  {
273  LexicographicComparator(Cmp1 const & cmp1 = Cmp1(), Cmp2 const & cmp2 = Cmp2()) :
274  cmp1_(cmp1), cmp2_(cmp2) {}
275 
276  template <typename T1, typename T2>
277  bool
278  operator()(T1 left, T2 right) const
279  {
280  if (cmp1_(left, right))
281  {
282  return true;
283  }
284  else
285  {
286  if (cmp1_(right, left))
287  {
288  return false;
289  }
290  else
291  {
292  return cmp2_(left, right);
293  }
294  }
295  }
296 
297 protected:
298  Cmp1 const & cmp1_;
299  Cmp2 const & cmp2_;
300  };
301 
310  template <typename Cmp1, typename Cmp2>
311  LexicographicComparator<Cmp1, Cmp2> lexicographicComparator(Cmp1 const & cmp1, Cmp2 const & cmp2)
312  {
313  return LexicographicComparator<Cmp1, Cmp2>(cmp1, cmp2);
314  }
315 
319  template <typename PairType>
321  {
322  bool operator()(const PairType & left, const PairType & right) const
323  {
324  return left.first < right.first;
325  }
326 
327  };
328 
332  template <typename PairType>
334  {
335  bool operator()(const PairType & left, const PairType & right) const
336  {
337  return left.second < right.second;
338  }
339 
340  };
341 
345  template <typename PairType>
347  {
348  bool operator()(const PairType & left, const PairType & right) const
349  {
350  return left.first > right.first;
351  }
352 
353  };
354 
358  template <typename PairType>
360  {
361  bool operator()(const PairType & left, const PairType & right) const
362  {
363  return left.second > right.second;
364  }
365 
366  };
367 
371  template <typename PairType>
373  {
374  bool operator()(const PairType & left, const PairType & right) const
375  {
376  return left.first == right.first;
377  }
378 
379  };
380 
384  template <typename PairType>
386  {
387  bool operator()(const PairType & left, const PairType & right) const
388  {
389  return left.second == right.second;
390  }
391 
392  };
393 
401  template <typename CompareType>
403  {
404  CompareType & tolerance;
405 
406  explicit EqualInTolerance(CompareType & c) :
407  tolerance(c)
408  {}
409 
410  bool operator()(CompareType i, CompareType j)
411  {
412  CompareType diff = fabs(i - j);
413  return diff <= tolerance;
414  }
415 
416  };
417 }
418 
const double c
Definition: Constants.h:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Struct for binary predicate to consider equality with a certain tolerance.
Definition: ComparatorUtils.h:403
EqualInTolerance(CompareType &c)
Definition: ComparatorUtils.h:406
bool operator()(CompareType i, CompareType j)
Definition: ComparatorUtils.h:410
CompareType & tolerance
Definition: ComparatorUtils.h:404
A wrapper class that combines two comparators lexicographically. Normally you should use the make-fun...
Definition: ComparatorUtils.h:272
bool operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:278
Cmp1 const & cmp1_
Definition: ComparatorUtils.h:298
Cmp2 const & cmp2_
Definition: ComparatorUtils.h:299
LexicographicComparator< Cmp1, Cmp2 > lexicographicComparator(Cmp1 const &cmp1, Cmp2 const &cmp2)
Make-function to create a LexicographicComparator from two other comparators without the need to spec...
Definition: ComparatorUtils.h:311
LexicographicComparator(Cmp1 const &cmp1=Cmp1(), Cmp2 const &cmp2=Cmp2())
Definition: ComparatorUtils.h:273
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:347
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:348
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:321
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:322
Class for comparison of std::pair using second ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:360
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:361
Class for comparison of std::pair using second ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:334
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:335
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:373
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:374
Struct for comparison of std::pair using second ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:386
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:387
Wrapper that takes a comparator for `something' and makes a comparator for pointers to `something' ou...
Definition: ComparatorUtils.h:173
bool operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:182
Cmp const & cmp_
Definition: ComparatorUtils.h:188
PointerComparator(PointerComparator const &pCmp)
Definition: ComparatorUtils.h:174
PointerComparator(Cmp const &cmp=Cmp())
Definition: ComparatorUtils.h:177
PointerComparator< Cmp > pointerComparator(Cmp const &cmp)
Make-function to create a PointerComparator from another comparator without the need to specify the t...
Definition: ComparatorUtils.h:207
Wrapper that reverses (exchanges) the two arguments of a comparator. Normally you should use the make...
Definition: ComparatorUtils.h:223
bool operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:231
ReverseComparator(Cmp const &cmp=Cmp())
Definition: ComparatorUtils.h:227
Cmp const & cmp_
Definition: ComparatorUtils.h:237
ReverseComparator< Cmp > reverseComparator(Cmp const &cmp)
Make-function to create a ReverseComparator from another comparator without the need to specify the t...
Definition: ComparatorUtils.h:256
ReverseComparator(ReverseComparator const &cmp)
Definition: ComparatorUtils.h:224