1 // Implementation of comparison and sorting functions
2 // Copyright © 2009 The University of Chicago
3 #include "CompareFunc.h"
11 #include "MiniLexicon.h"
12 #include "Signature.h"
17 #include "StringFunc.h"
19 int CompareAlphabetically(const void *pA
, const void *pB
)
21 CLParse
* pS1
= *static_cast<CLParse
* const*>(pA
);
22 CLParse
* pS2
= *static_cast<CLParse
* const*>(pB
);
27 if ( pS1
->GetMyMini() && pS2
->GetMyMini() )
29 QString str1
= pS1
->Display( QChar(0),
30 pS1
->GetMyMini()->GetOutFilter() ),
31 str2
= pS2
->Display( QChar(0),
32 pS2
->GetMyMini()->GetOutFilter() );
34 return LxStrCmp( str1
, str2
, str1
.length(), str2
.length(),
38 return LxStrCmp( pS1
->GetKeyPointer(),
41 pS2
->GetKeyLength() );
44 int CompareReverseAlphabetically(const void *pA
, const void *pB
)
46 CLParse
* pS1
= *static_cast<CLParse
* const*>(pA
);
47 CLParse
* pS2
= *static_cast<CLParse
* const*>(pB
);
53 QChar
* S1
= new QChar
[pS1
->GetKeyLength()];
54 QChar
* S2
= new QChar
[pS2
->GetKeyLength()];
55 LxStrCpy_R( pS1
->GetKeyPointer(), S1
, pS1
->GetKeyLength() );
56 LxStrCpy_R( pS2
->GetKeyPointer(), S2
, pS2
->GetKeyLength() );
57 int comp
= LxStrCmp( S1
, S2
, pS1
->GetKeyLength(), pS2
->GetKeyLength() );
65 int CompareSortingQuantity (const void *pA
, const void *pB
)
67 CLParse
* pS1
= *static_cast<CLParse
* const*>(pA
);
68 CLParse
* pS2
= *static_cast<CLParse
* const*>(pB
);
70 float dif
= pS2
->GetSortingQuantity() - pS1
->GetSortingQuantity();
72 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
75 int CompareSortingString (const void *pA
, const void *pB
)
77 CLParse
* pS1
= *static_cast<CLParse
* const*>(pA
);
78 CLParse
* pS2
= *static_cast<CLParse
* const*>(pB
);
80 return LxStrCmp( pS1
->GetSortingString(), pS2
->GetSortingString(), pS1
->GetSortingString().length(), pS2
->GetSortingString().length() );
84 int CompareNumberOfStems (const void *pA
, const void *pB
)
86 CSignature
* pS1
= *static_cast<CSignature
* const*>(pA
);
87 CSignature
* pS2
= *static_cast<CSignature
* const*>(pB
);
89 int dif
= pS2
->GetNumberOfStems() - pS1
->GetNumberOfStems();
91 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
95 int CompareSigRemark (const void *pA
, const void *pB
)
97 CSignature
* pS1
= *static_cast<CSignature
* const*>(pA
);
98 CSignature
* pS2
= *static_cast<CSignature
* const*>(pB
);
99 return QString::compare( pS1
->GetRemark(), pS2
->GetRemark() );
102 int CompareStemSource (const void *pA
, const void *pB
)
104 CStem
* pS1
= *static_cast<CStem
* const*>(pA
);
105 CStem
* pS2
= *static_cast<CStem
* const*>(pB
);
106 return QString::compare( pS1
->GetConfidence(), pS2
->GetConfidence() );
109 int CompareCorpusCount (const void *pM1
, const void *pM2
)
111 CLParse
* pS1
= *static_cast<CLParse
* const*>(pM1
);
112 CLParse
* pS2
= *static_cast<CLParse
* const*>(pM2
);
114 int dif
= pS2
->GetCorpusCount() - pS1
->GetCorpusCount();
116 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
119 int CompareSize (const void *pM1
, const void *pM2
)
121 CLParse
* pS1
= *static_cast<CLParse
* const*>(pM1
);
122 CLParse
* pS2
= *static_cast<CLParse
* const*>(pM2
);
124 int dif
= pS2
->Size() - pS1
->Size();
126 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
129 int CompareLength (const void *pM1
, const void *pM2
)
131 CLParse
* pS1
= *static_cast<CLParse
* const*>(pM1
);
132 CLParse
* pS2
= *static_cast<CLParse
* const*>(pM2
);
134 int dif
= pS2
->GetKeyLength() - pS1
->GetKeyLength();
136 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
139 int CompareDLSavings (const void *pM1
, const void *pM2
)
141 CMorpheme
* pS1
= *static_cast<CMorpheme
* const*>(pM1
);
142 CMorpheme
* pS2
= *static_cast<CMorpheme
* const*>(pM2
);
144 double dif
= pS2
->GetDLSavings() - pS1
->GetDLSavings();
146 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
149 int CompareFrequency (const void *pM1
, const void *pM2
)
151 CMorpheme
* pS1
= *static_cast<CMorpheme
* const*>(pM1
);
152 CMorpheme
* pS2
= *static_cast<CMorpheme
* const*>(pM2
);
154 double dif
= pS2
->GetFrequency() - pS1
->GetFrequency();
156 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
159 int CompareMorphemeCount (const void *pM1
, const void *pM2
)
161 CMorpheme
* pS1
= *static_cast<CMorpheme
* const*>(pM1
);
162 CMorpheme
* pS2
= *static_cast<CMorpheme
* const*>(pM2
);
164 double dif
= pS2
->GetMorphemeCount() - pS1
->GetMorphemeCount();
166 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
169 int CompareUseCount (const void *pM1
, const void *pM2
)
171 CAffix
* pS1
= *static_cast<CAffix
* const*>(pM1
);
172 CAffix
* pS2
= *static_cast<CAffix
* const*>(pM2
);
174 double dif
= pS2
->GetUseCount() - pS1
->GetUseCount();
176 if (dif
> 0) {return 1;} else if (dif
< 0) {return -1;} else {return 0;}
179 // compare() for basic types
180 int MakeComparable(int a
, int b
)
182 const int difference
= a
- b
;
184 if (difference
< 0) return -1;
185 else if (difference
== 0) return 0;
189 int MakeComparable(double a
, double b
)
191 const double difference
= a
- b
;
193 if (difference
< 0) return -1;
194 else if (difference
== 0) return 0;