Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
compare.h
1 /*
2  * $Id$
3  * Comparator class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2008, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_COMPARATOR_H_
23 #define ELM_COMPARATOR_H_
24 
25 #include <elm/string.h>
26 #include <elm/type_info.h>
27 #include <elm/util/Pair.h>
28 #include <elm/util/Equiv.h>
29 
30 namespace elm {
31 
32 // Comparator class
33 template <class T>
34 class Comparator {
35 public:
36  static inline int compare(const T& v1, const T& v2)
37  { if(v1 == v2) return 0; else if(v1 > v2) return 1; else return -1; }
38  int doCompare(const T& v1, const T& v2) const { return compare(v1, v2); }
39 };
40 
41 // DelegateComparator class
42 template <class T, class C>
44 public:
45  static inline int compare(const T& v1, const T& v2) { return C::compare(v1, v2); }
46  inline int doCompare(const T& v1, const T& v2) const { return compare(v1, v2); }
47 };
48 
49 // CompareComparator class
50 template <class T>
52 public:
53  static inline int compare(const T& v1, const T& v2) { return v1.compare(v2); }
54 };
55 template <> class Comparator<cstring>: public CompareComparator<cstring> { };
56 template <> class Comparator<string>: public CompareComparator<string> { };
57 
58 
59 // AssocComparator class
60 template <class K, class T, class C = Comparator<K> >
62 public:
63  typedef Pair<K, T> pair_t;
64  static inline int compare(const pair_t& v1, const pair_t& v2)
65  { return C::compare(v1.fst, v2. fst); }
66 };
67 template <class K, class T> class Comparator<Pair<K, T> >
68  : public AssocComparator<K, T> { };
69 
70 
71 // ReverseComparator class
72 template <class T, class C>
74 public:
75  static inline int compare(const T& v1, const T& v2)
76  { return -C::compare(v1, v2); }
77 };
78 
79 
80 // CompareEquiv class
81 template <class T, class C = Comparator<T> >
82 class CompareEquiv {
83 public:
84  static inline bool equals(const T& v1, const T& v2)
85  { return C::compare(v1, v2) == 0; }
86 };
87 
88 
89 // Useful inlines
90 template <class T> inline const T& min(const T& x, const T& y)
91  { if(Comparator<T>::compare(x, y) >= 0) return y; else return x; }
92 template <class T> inline const T& max(const T& x, const T& y)
93  { if(Comparator<T>::compare(x, y) >= 0) return x; else return y; }
94 
95 } // elm
96 
97 #endif /* ELM_COMPARATOR_H_ */
const T & max(const T &x, const T &y)
Definition: compare.h:92
Definition: CString.h:17
static int compare(const T &v1, const T &v2)
Definition: compare.h:75
Definition: compare.h:61
static int compare(const T &v1, const T &v2)
Definition: compare.h:53
static int compare(const pair_t &v1, const pair_t &v2)
Definition: compare.h:64
T1 fst
Definition: Pair.h:18
Definition: compare.h:51
static int compare(const T &v1, const T &v2)
Definition: compare.h:45
static int compare(const T &v1, const T &v2)
Definition: compare.h:36
static bool equals(const T &v1, const T &v2)
Definition: compare.h:84
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:38
Definition: compare.h:82
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:46
Pair< K, T > pair_t
Definition: compare.h:63
Definition: String.h:31
Definition: compare.h:73
Definition: compare.h:34
Definition: Pair.h:16
Definition: compare.h:43
const T & min(const T &x, const T &y)
Definition: compare.h:90