OpenMS  2.4.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-2018.
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  public std::binary_function<typename Cmp::first_argument_type *, typename Cmp::second_argument_type *, typename Cmp::result_type>
174  {
176  cmp_(pCmp.cmp_)
177  {}
178  PointerComparator(Cmp const & cmp = Cmp()) :
179  cmp_(cmp)
180  {}
181 
182  template <typename T1, typename T2>
183  typename Cmp::result_type
184  operator()(T1 left, T2 right) const
185  {
186  return cmp_(*left, *right); // T must have operator* defined
187  }
188 
189 protected:
190  Cmp const & cmp_;
191  };
192 
208  template <class Cmp>
210  {
211  return PointerComparator<Cmp>(cmp);
212  }
213 
222  template <class Cmp>
224  std::binary_function<typename Cmp::second_argument_type, typename Cmp::first_argument_type, typename Cmp::result_type>
225  // (Note that here we must reverse the order of template args!)
226  {
228  cmp_(cmp.cmp_) {}
229 
230  ReverseComparator(Cmp const & cmp = Cmp()) :
231  cmp_(cmp) {}
232 
233  template <typename T1, typename T2>
234  typename Cmp::result_type
235  operator()(T1 left, T2 right) const
236  {
237  return cmp_(right, left); // the other way round
238  }
239 
240 protected:
241  Cmp const & cmp_;
242  };
243 
259  template <class Cmp>
261  {
262  return ReverseComparator<Cmp>(cmp);
263  }
264 
274  template <typename Cmp1, typename Cmp2>
276  std::binary_function<typename Cmp1::first_argument_type, typename Cmp1::second_argument_type, bool>
277  {
278  LexicographicComparator(Cmp1 const & cmp1 = Cmp1(), Cmp2 const & cmp2 = Cmp2()) :
279  cmp1_(cmp1), cmp2_(cmp2) {}
280 
281  template <typename T1, typename T2>
282  bool
283  operator()(T1 left, T2 right) const
284  {
285  if (cmp1_(left, right))
286  {
287  return true;
288  }
289  else
290  {
291  if (cmp1_(right, left))
292  {
293  return false;
294  }
295  else
296  {
297  return cmp2_(left, right);
298  }
299  }
300  }
301 
302 protected:
303  Cmp1 const & cmp1_;
304  Cmp2 const & cmp2_;
305  };
306 
315  template <typename Cmp1, typename Cmp2>
316  LexicographicComparator<Cmp1, Cmp2> lexicographicComparator(Cmp1 const & cmp1, Cmp2 const & cmp2)
317  {
318  return LexicographicComparator<Cmp1, Cmp2>(cmp1, cmp2);
319  }
320 
324  template <typename PairType>
326  std::binary_function<PairType, PairType, bool>
327  {
328  bool operator()(const PairType & left, const PairType & right) const
329  {
330  return left.first < right.first;
331  }
332 
333  };
334 
338  template <typename PairType>
340  std::binary_function<PairType, PairType, bool>
341  {
342  bool operator()(const PairType & left, const PairType & right) const
343  {
344  return left.second < right.second;
345  }
346 
347  };
348 
352  template <typename PairType>
354  std::binary_function<PairType, PairType, bool>
355  {
356  bool operator()(const PairType & left, const PairType & right) const
357  {
358  return left.first > right.first;
359  }
360 
361  };
362 
366  template <typename PairType>
368  std::binary_function<PairType, PairType, bool>
369  {
370  bool operator()(const PairType & left, const PairType & right) const
371  {
372  return left.second > right.second;
373  }
374 
375  };
376 
380  template <typename PairType>
382  std::binary_function<PairType, PairType, bool>
383  {
384  bool operator()(const PairType & left, const PairType & right) const
385  {
386  return left.first == right.first;
387  }
388 
389  };
390 
394  template <typename PairType>
396  std::binary_function<PairType, PairType, bool>
397  {
398  bool operator()(const PairType & left, const PairType & right) const
399  {
400  return left.second == right.second;
401  }
402 
403  };
404 
412  template <typename CompareType>
414  public std::binary_function<CompareType, CompareType, bool>
415  {
416  CompareType & tolerance;
417 
418  explicit EqualInTolerance(CompareType & c) :
419  tolerance(c)
420  {}
421 
422  bool operator()(CompareType i, CompareType j)
423  {
424  CompareType diff = fabs(i - j);
425  return diff <= tolerance;
426  }
427 
428  };
429 }
430 
EqualInTolerance(CompareType &c)
Definition: ComparatorUtils.h:418
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:384
LexicographicComparator(Cmp1 const &cmp1=Cmp1(), Cmp2 const &cmp2=Cmp2())
Definition: ComparatorUtils.h:278
A wrapper class that combines two comparators lexicographically. Normally you should use the make-fun...
Definition: ComparatorUtils.h:275
Cmp::result_type operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:235
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:342
Struct for comparison of std::pair using second ONLY e.g. for use with std::sort. ...
Definition: ComparatorUtils.h:395
Cmp2 const & cmp2_
Definition: ComparatorUtils.h:304
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:260
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:353
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
PointerComparator(PointerComparator const &pCmp)
Definition: ComparatorUtils.h:175
CompareType & tolerance
Definition: ComparatorUtils.h:416
Class for comparison of std::pair using second ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:367
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:316
Struct for binary predicate to consider equality with a certain tolerance.
Definition: ComparatorUtils.h:413
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:328
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:381
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:398
Cmp1 const & cmp1_
Definition: ComparatorUtils.h:303
bool operator()(CompareType i, CompareType j)
Definition: ComparatorUtils.h:422
ReverseComparator(Cmp const &cmp=Cmp())
Definition: ComparatorUtils.h:230
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:209
Cmp const & cmp_
Definition: ComparatorUtils.h:190
Class for comparison of std::pair using second ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:339
Wrapper that takes a comparator for `something&#39; and makes a comparator for pointers to `something&#39; ou...
Definition: ComparatorUtils.h:172
ReverseComparator(ReverseComparator const &cmp)
Definition: ComparatorUtils.h:227
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:370
PointerComparator(Cmp const &cmp=Cmp())
Definition: ComparatorUtils.h:178
bool operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:283
Wrapper that reverses (exchanges) the two arguments of a comparator. Normally you should use the make...
Definition: ComparatorUtils.h:223
Class for comparison of std::pair using first ONLY e.g. for use with std::sort.
Definition: ComparatorUtils.h:325
bool operator()(const PairType &left, const PairType &right) const
Definition: ComparatorUtils.h:356
Cmp const & cmp_
Definition: ComparatorUtils.h:241
Cmp::result_type operator()(T1 left, T2 right) const
Definition: ComparatorUtils.h:184