2 * @brief Result comparison functions.
4 /* Copyright (C) 2006,2009,2013,2017 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26 #include "api/result.h"
29 /* We use templates to generate all the different comparison functions which we
30 * need, which avoids having to write them all out by hand.
33 // Order by docid, inlined. Used as the last fallback by the others.
34 template<bool FORWARD_DID
>
36 msetcmp_by_docid_inline(const Result
& a
, const Result
& b
)
39 return (a
.get_docid() < b
.get_docid());
41 return (a
.get_docid() > b
.get_docid());
45 // Order by docid, used when relevance is always 0.
46 template<bool FORWARD_DID
>
48 msetcmp_by_docid(const Result
& a
, const Result
& b
)
50 return msetcmp_by_docid_inline
<FORWARD_DID
>(a
, b
);
53 // Order by relevance, then docid.
54 template<bool FORWARD_DID
>
56 msetcmp_by_relevance(const Result
& a
, const Result
& b
)
58 if (a
.get_weight() > b
.get_weight()) return true;
59 if (a
.get_weight() < b
.get_weight()) return false;
60 return msetcmp_by_docid_inline
<FORWARD_DID
>(a
, b
);
63 // Order by value, then docid.
64 template<bool FORWARD_VALUE
, bool FORWARD_DID
>
66 msetcmp_by_value(const Result
& a
, const Result
& b
)
68 int sort_cmp
= a
.get_sort_key().compare(b
.get_sort_key());
69 if (sort_cmp
> 0) return FORWARD_VALUE
;
70 if (sort_cmp
< 0) return !FORWARD_VALUE
;
71 return msetcmp_by_docid_inline
<FORWARD_DID
>(a
, b
);
74 // Order by value, then relevance, then docid.
75 template<bool FORWARD_VALUE
, bool FORWARD_DID
>
77 msetcmp_by_value_then_relevance(const Result
& a
, const Result
& b
)
79 int sort_cmp
= a
.get_sort_key().compare(b
.get_sort_key());
80 if (sort_cmp
> 0) return FORWARD_VALUE
;
81 if (sort_cmp
< 0) return !FORWARD_VALUE
;
82 if (a
.get_weight() > b
.get_weight()) return true;
83 if (a
.get_weight() < b
.get_weight()) return false;
84 return msetcmp_by_docid_inline
<FORWARD_DID
>(a
, b
);
87 // Order by relevance, then value, then docid.
88 template<bool FORWARD_VALUE
, bool FORWARD_DID
>
90 msetcmp_by_relevance_then_value(const Result
& a
, const Result
& b
)
92 if (a
.get_weight() > b
.get_weight()) return true;
93 if (a
.get_weight() < b
.get_weight()) return false;
94 int sort_cmp
= a
.get_sort_key().compare(b
.get_sort_key());
95 if (sort_cmp
> 0) return FORWARD_VALUE
;
96 if (sort_cmp
< 0) return !FORWARD_VALUE
;
97 return msetcmp_by_docid_inline
<FORWARD_DID
>(a
, b
);
101 get_msetcmp_function(Xapian::Enquire::Internal::sort_setting sort_by
,
103 bool sort_val_reverse
)
106 case Xapian::Enquire::Internal::DOCID
:
108 return msetcmp_by_docid
<true>;
110 return msetcmp_by_docid
<false>;
111 case Xapian::Enquire::Internal::REL
:
113 return msetcmp_by_relevance
<true>;
115 return msetcmp_by_relevance
<false>;
116 case Xapian::Enquire::Internal::VAL
:
118 if (sort_val_reverse
) {
119 return msetcmp_by_value
<true, true>;
121 return msetcmp_by_value
<false, true>;
124 if (sort_val_reverse
) {
125 return msetcmp_by_value
<true, false>;
127 return msetcmp_by_value
<false, false>;
130 case Xapian::Enquire::Internal::VAL_REL
:
132 if (sort_val_reverse
) {
133 return msetcmp_by_value_then_relevance
<true, true>;
135 return msetcmp_by_value_then_relevance
<false, true>;
138 if (sort_val_reverse
) {
139 return msetcmp_by_value_then_relevance
<true, false>;
141 return msetcmp_by_value_then_relevance
<false, false>;
145 // Must be REL_VAL, but handle with "default" to avoid warnings
146 // about falling off the end of the function.
147 AssertEq(sort_by
, Xapian::Enquire::Internal::REL_VAL
);
149 if (sort_val_reverse
) {
150 return msetcmp_by_relevance_then_value
<true, true>;
152 return msetcmp_by_relevance_then_value
<false, true>;
155 if (sort_val_reverse
) {
156 return msetcmp_by_relevance_then_value
<true, false>;
158 return msetcmp_by_relevance_then_value
<false, false>;