Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
compare.h
1 /*
2  * Comparator class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2008, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_COMPARATOR_H_
22 #define ELM_COMPARATOR_H_
23 
24 #include <elm/compat.h>
25 #include <elm/string.h>
26 #include <elm/type_info.h>
27 #include <elm/util/Pair.h>
28 #include "equiv.h"
29 
30 namespace elm {
31 
32 // Comparator class
33 template <class T>
34 class Comparator {
35 public:
36  typedef T t;
37  static inline int compare(const T& v1, const T& v2)
38  { if(v1 == v2) return 0; else if(v1 > v2) return 1; else return -1; }
39  int doCompare(const T& v1, const T& v2) const { return compare(v1, v2); }
40 };
41 
42 // StaticComparator class
43 template <class T, class C>
45 public:
46  typedef T t;
47  static inline int compare(const T& v1, const T& v2) { return C::compare(v1, v2); }
48  inline int doCompare(const T& v1, const T& v2) const { return compare(v1, v2); }
49 };
50 
51 // DynamicComparator class
52 template <class T>
54 public:
55  typedef T t;
56  static inline int compare(const T& v1, const T& v2) { return v1.compare(v2); }
57  inline int doCompare(const T& v1, const T& v2) const { return v1.compare(v2); }
58 };
59 template <> class Comparator<cstring>: public DynamicComparator<cstring> { };
60 template <> class Comparator<string>: public DynamicComparator<string> { };
61 
62 // AssocComparator class
63 template <class K, class T, class C = Comparator<K> >
64 class AssocComparator: public C {
65 public:
66  typedef Pair<K, T> pair_t;
67  typedef pair_t t;
68  inline AssocComparator(const C& c = single<C>()): _c(c) { }
69  static inline int compare(const pair_t& v1, const pair_t& v2)
70  { return C::compare(v1.fst, v2. fst); }
71  inline int doCompare(const pair_t& v1, const pair_t& v2) const
72  { return _c.compare(v1.fst, v2. fst); }
73  inline int compareKey(const K& k1, const K& k2) const { return _c.doCompare(k1, k2); }
74 private:
75  const C& _c;
76 };
77 template <class K, class T> class Comparator<Pair<K, T> >
78  : public AssocComparator<K, T> { };
79 
80 // ReverseComparator class
81 template <class T, class C>
83 public:
84  typedef T t;
85  inline ReverseComparator(const C& c = single<C>()): _c(c) { }
86  static inline int compare(const T& v1, const T& v2)
87  { return -C::compare(v1, v2); }
88  inline int doCompare(const T& v1, const T& v2) const
89  { return -_c.doCompare(v1, v2); }
90 private:
91  const C& _c;
92 };
93 
94 // GlobalComparator class
95 template <class T>
97 public:
98  typedef T t;
99  static inline int compare(const T& v1, const T& v2)
100  { return compare(v1, v2); }
101 };
102 
103 // Useful inlines
104 template <class T> inline const T& min(const T& x, const T& y)
105  { if(Comparator<T>::compare(x, y) >= 0) return y; else return x; }
106 template <class T, class C> inline const T& min(const T& x, const T& y, const C& c = single<C>())
107  { if(c.doCompare(x, y) >= 0) return y; else return x; }
108 template <class T> inline const T& max(const T& x, const T& y)
109  { if(Comparator<T>::compare(x, y) >= 0) return x; else return y; }
110 template <class T, class C> inline const T& max(const T& x, const T& y, const C& c = single<C>())
111  { if(c.doCompare(x, y) >= 0) return x; else return y; }
112 
113 template <class C> inline typename C::t min(const C& c)
114  { typename C::Iter i(c); typename C::t m = *i;
115  for(i++; i(); i++) if(Comparator<typename C::t>::compare(*i, m) < 0) m = *i; return m; }
116 template <class C, class CC> inline typename C::t min(const C& c, const CC& cc)
117  { typename C::Iter i(c); typename C::t m = *i;
118  for(i++; i; i++) if(cc.doCompare(*i, m) < 0) m = *i; return m; }
119 template <class C> inline typename C::t max(const C& c)
120  { typename C::Iter i(c); typename C::t m = *i;
121  for(i++; i(); i++) if(Comparator<typename C::t>::compare(*i, m) > 0) m = *i; return m; }
122 
123 } // elm
124 
125 #endif /* ELM_COMPARATOR_H_ */
elm::ReverseComparator::ReverseComparator
ReverseComparator(const C &c=single< C >())
Definition: compare.h:85
elm::Comparator::doCompare
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:39
elm::AssocComparator::doCompare
int doCompare(const pair_t &v1, const pair_t &v2) const
Definition: compare.h:71
elm::ReverseComparator::doCompare
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:88
elm::DynamicComparator::compare
static int compare(const T &v1, const T &v2)
Definition: compare.h:56
elm::max
const T & max(const T &x, const T &y)
Definition: compare.h:108
elm::Pair< K, T >
elm::AssocComparator::compare
static int compare(const pair_t &v1, const pair_t &v2)
Definition: compare.h:69
elm::StaticComparator::compare
static int compare(const T &v1, const T &v2)
Definition: compare.h:47
elm::StaticComparator::t
T t
Definition: compare.h:46
elm::CString
Definition: CString.h:17
elm::min
const T & min(const T &x, const T &y)
Definition: compare.h:104
elm::GlobalComparator::t
T t
Definition: compare.h:98
elm::Comparator::t
T t
Definition: compare.h:36
elm::Comparator::compare
static int compare(const T &v1, const T &v2)
Definition: compare.h:37
elm::AssocComparator::AssocComparator
AssocComparator(const C &c=single< C >())
Definition: compare.h:68
elm::ReverseComparator::compare
static int compare(const T &v1, const T &v2)
Definition: compare.h:86
elm::StaticComparator
Definition: compare.h:44
elm
Definition: adapter.h:26
elm::AssocComparator::pair_t
Pair< K, T > pair_t
Definition: compare.h:66
elm::StaticComparator::doCompare
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:48
elm::AssocComparator::compareKey
int compareKey(const K &k1, const K &k2) const
Definition: compare.h:73
elm::ReverseComparator::t
T t
Definition: compare.h:84
elm::GlobalComparator::compare
static int compare(const T &v1, const T &v2)
Definition: compare.h:99
elm::DynamicComparator::doCompare
int doCompare(const T &v1, const T &v2) const
Definition: compare.h:57
elm::Comparator
Definition: compare.h:34
elm::AssocComparator
Definition: compare.h:64
elm::DynamicComparator::t
T t
Definition: compare.h:55
elm::String
Definition: String.h:30
elm::AssocComparator::t
pair_t t
Definition: compare.h:67
elm::GlobalComparator
Definition: compare.h:96
elm::ReverseComparator
Definition: compare.h:82
elm::DynamicComparator
Definition: compare.h:53
elm::Pair::fst
T1 fst
Definition: Pair.h:35