00001 #ifndef __COMPARATOR_H 00002 #define __COMPARATOR_H 00003 00004 // http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-17.html 00005 00006 #include "plocha.h" 00007 #include <string.h> 00008 00009 #define _INC(i1,i2)i1++;i2++; 00010 00011 class Comparator 00012 { 00013 private: 00014 // funkce resi problem porovnani dvou poli, ktere maji ruzny pocet hodnot 00015 // a rozhoduje ktere znich je vetsi 00016 // vraci true pokud je p1 vetsi nez p2 00017 short index1; 00018 short index2; 00019 bool mycmp(const Plocha * p1, const Plocha * p2) 00020 { 00021 index1 = 0; 00022 index2 = 0; 00023 if (p1->pbp > p2->pbp) 00024 { 00025 while (index1 < p1->pbp && index2 < p2->pbp) 00026 { 00027 if (p1->bodyp[index1] > p2->bodyp[index2]) 00028 return (index1 == index2); 00029 else if ( p1->bodyp[index1] == p2->bodyp[index2] ){ 00030 _INC(index1,index2); 00031 } 00032 else index1++; 00033 } 00034 } 00035 else // p1->pbp < p2->pbp 00036 { 00037 while (index1 < p1->pbp && index2 < p2->pbp) 00038 { 00039 if (p1->bodyp[index1] > p2->bodyp[index2]) 00040 index2++; 00041 else if ( p1->bodyp[index1] == p2->bodyp[index2] ){ 00042 _INC(index1,index2); 00043 } 00044 else return (index1 < index2); 00045 } 00046 } 00047 return (index1 < p1->pbp); // true -> p1 > p2 00048 } 00049 00050 public: 00051 // vrácená hodnota je true pokud je p2 > p1 00052 bool operator()(const Plocha * p1, const Plocha * p2) 00053 { 00054 if (p1->pbp == p2->pbp) 00055 return (memcmp(p2->bodyp, p1->bodyp, p1->pbp * sizeof(long)) > 0); 00056 00057 // pozor na poradi argumentu 00058 return mycmp(p2, p1); 00059 } 00060 }; 00061 #endif 00062 00063