Open
Graph Drawing
Framework

 v.2012.05
 

comparer.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00043 #ifdef _MSC_VER
00044 #pragma once
00045 #endif
00046 
00047 #ifndef OGDF_COMPARER_H
00048 #define OGDF_COMPARER_H
00049 
00050 namespace ogdf {
00051 
00052 //--------------------------------------------------------------------
00053 // A comparer interface is has to define
00054 // bool less (const E &x, const E &y);
00055 // bool leq  (const E &x, const E &y);
00056 // bool equal(const E &x, const E &y);
00057 // bool geq (const E &x, const E &y);
00058 // bool greater  (const E &x, const E &y);
00059 //
00060 // "const E &" can be replaced by "E"
00061 //--------------------------------------------------------------------
00062 
00064 
00079 template<typename E> class StdComparer
00080 {
00081 public:
00082     static bool less(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
00083     static bool leq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
00084     static bool greater(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
00085     static bool geq(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
00086     static bool equal(const E &x, const E &y) { OGDF_THROW(NoStdComparerException); }
00087 };
00088 
00090 #define OGDF_STD_COMPARER(type) \
00091     template<> class StdComparer<type> \
00092     { \
00093     public: \
00094         static bool less   (const type &x, const type &y) { return x <  y; } \
00095         static bool leq    (const type &x, const type &y) { return x <= y; } \
00096         static bool greater(const type &x, const type &y) { return x >  y; } \
00097         static bool geq    (const type &x, const type &y) { return x >= y; } \
00098         static bool equal  (const type &x, const type &y) { return x == y; } \
00099     };
00100 
00101 OGDF_STD_COMPARER(int);
00102 OGDF_STD_COMPARER(float);
00103 OGDF_STD_COMPARER(double);
00104 
00106 template<> class StdComparer<bool> {
00107 public:
00108     static bool less   (const bool &x, const bool &y) { return !x &&  y; }
00109     static bool leq    (const bool &x, const bool &y) { return !x ||  y; }
00110     static bool greater(const bool &x, const bool &y) { return  x && !y; }
00111     static bool geq    (const bool &x, const bool &y) { return  x || !y; }
00112     static bool equal  (const bool &x, const bool &y) { return  x ==  y; }
00113 };
00114 
00115 
00117 
00120 template<class CONTENTTYPE, class STATICCONTENTCOMPARER = StdComparer<CONTENTTYPE> > 
00121 class TargetComparer {
00122     typedef CONTENTTYPE* CONTENTPOINTER;
00123 public: 
00124     static bool less   (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::less   (*x,*y); }
00125     static bool leq    (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::leq    (*x,*y); }
00126     static bool greater(const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::greater(*x,*y); }
00127     static bool geq    (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::geq    (*x,*y); }
00128     static bool equal  (const CONTENTPOINTER &x, const CONTENTPOINTER &y) { return STATICCONTENTCOMPARER::equal  (*x,*y); }
00129 };
00130 
00131 
00133 
00170 #define OGDF_AUGMENT_COMPARER(type) \
00171     public: \
00172     bool less(const type &x, const type &y) const { return compare(x,y) < 0; } \
00173     bool leq(const type &x, const type &y) const { return compare(x,y) <= 0; } \
00174     bool greater(const type &x, const type &y) const { return compare(x,y) > 0; } \
00175     bool geq(const type &x, const type &y) const { return compare(x,y) >= 0; } \
00176     bool equal(const type &x, const type &y) const { return compare(x,y) == 0; }
00177 
00179 
00214 #define OGDF_AUGMENT_STATICCOMPARER(type) \
00215     public: \
00216     static bool less(const type &x, const type &y) { return compare(x,y) < 0; } \
00217     static bool leq(const type &x, const type &y) { return compare(x,y) <= 0; } \
00218     static bool greater(const type &x, const type &y) { return compare(x,y) > 0; } \
00219     static bool geq(const type &x, const type &y) { return compare(x,y) >= 0; } \
00220     static bool equal(const type &x, const type &y) { return compare(x,y) == 0; }
00221 
00222 
00224 
00241 template<class E> class VComparer {
00242 public:
00244     VComparer() { }
00245 
00246     virtual ~VComparer() { }
00247 
00249 
00254     virtual int compare(const E &x, const E &y) const = 0;
00255 
00257     virtual bool less(const E &x, const E &y) const {
00258         return compare(x,y) < 0;
00259     }
00260 
00262     virtual bool leq(const E &x, const E &y) const {
00263         return compare(x,y) <= 0;
00264     }
00265 
00267     virtual bool greater(const E &x, const E &y) const {
00268         return compare(x,y) > 0;
00269     }
00270 
00272     virtual bool geq(const E &x, const E &y) const {
00273         return compare(x,y) >= 0;
00274     }
00275 
00277     virtual bool equal(const E &x, const E &y) const {
00278         return compare(x,y) == 0;
00279     }
00280 }; // class VComparer
00281 
00282 } //namespace
00283 
00284 #endif /*OGF_COMPARER_H*/