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

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:21:59 using doxygen 1.8.13