TPIE

2362a60
comparator.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, 2012, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef _COMPARATOR_H
21 #define _COMPARATOR_H
22 
33 
34 // Get definitions for working with Unix and Windows
35 #include <tpie/config.h>
36 #include <tpie/portability.h>
37 
38 namespace tpie {
39 
40 // In is unlikely that users will need to directly need these classes
41 // except to maybe upgrade old code with minimal changes. In the future it
42 // may be better to make TPIE's compare() method match the syntax of the
43 // STL operator ()
44 
49 template<class T, class STLCMP>
51 public:
52  STL2TPIE_cmp(STLCMP* cmp) : m_isLess(cmp) {
53  // Does nothing.
54  }
55 
60  inline int compare(const T& left, const T& right){
61  if( (*m_isLess)(left, right) ){ return -1; }
62  else { return 0; }
63  }
64 
65 private:
67  STLCMP *m_isLess;
68 };
69 
74 template<class T, class TPCMP>
76 public:
77  TPIE2STL_cmp(TPCMP* cmp) : m_cmpobj(cmp) {
78  // Does nothing.
79  }
80 
81  inline bool operator()(const T& left, const T& right) const{
82  return (m_cmpobj->compare(left, right) < 0);
83  }
84 
85 private:
87  TPCMP* m_cmpobj;
88 };
89 
94 template<class T>
96 public:
97  op2TPIE_cmp() {
98  // Does nothing.
99  }
100 
105  inline int compare(const T& left, const T& right){
106  if( left < right ){ return -1; }
107  else { return 0; }
108  }
109 };
110 
111 } // tpie namespace
112 
113 #endif // _COMPARATOR_H
Convert a TPIE comparison object with a compare() function to STL comparison object with operator()...
Definition: comparator.h:75
Convert a class with a comparison operator < to a TPIE comparison object with a compare() function...
Definition: comparator.h:95
int compare(const T &left, const T &right)
Do not use with applications that test if compare returns +1 Because it never does so...
Definition: comparator.h:105
This file contains a few deprecated definitions for legacy code.
Convert STL comparison object with operator() to a TPIE comparison object with a compare() function...
Definition: comparator.h:50
int compare(const T &left, const T &right)
Do not use with applications that test if compare returns +1 Because it never does so...
Definition: comparator.h:60