[ci] Fix clang-santisers job for GHA change
[xapian.git] / xapian-core / matcher / msetcmp.cc
blob6c7d5735274b0596849fbd058612cd32d9f8d116
1 /** @file
2 * @brief Result comparison functions.
3 */
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
19 * USA
22 #include <config.h>
24 #include "msetcmp.h"
26 #include "api/result.h"
27 #include "omassert.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>
35 static inline bool
36 msetcmp_by_docid_inline(const Result& a, const Result& b)
38 if (FORWARD_DID) {
39 return (a.get_docid() < b.get_docid());
40 } else {
41 return (a.get_docid() > b.get_docid());
45 // Order by docid, used when relevance is always 0.
46 template<bool FORWARD_DID>
47 static bool
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>
55 static bool
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>
65 static bool
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>
76 static bool
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>
89 static bool
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);
100 MSetCmp
101 get_msetcmp_function(Xapian::Enquire::Internal::sort_setting sort_by,
102 bool sort_forward,
103 bool sort_val_reverse)
105 switch (sort_by) {
106 case Xapian::Enquire::Internal::DOCID:
107 if (sort_forward)
108 return msetcmp_by_docid<true>;
109 else
110 return msetcmp_by_docid<false>;
111 case Xapian::Enquire::Internal::REL:
112 if (sort_forward)
113 return msetcmp_by_relevance<true>;
114 else
115 return msetcmp_by_relevance<false>;
116 case Xapian::Enquire::Internal::VAL:
117 if (sort_forward) {
118 if (sort_val_reverse) {
119 return msetcmp_by_value<true, true>;
120 } else {
121 return msetcmp_by_value<false, true>;
123 } else {
124 if (sort_val_reverse) {
125 return msetcmp_by_value<true, false>;
126 } else {
127 return msetcmp_by_value<false, false>;
130 case Xapian::Enquire::Internal::VAL_REL:
131 if (sort_forward) {
132 if (sort_val_reverse) {
133 return msetcmp_by_value_then_relevance<true, true>;
134 } else {
135 return msetcmp_by_value_then_relevance<false, true>;
137 } else {
138 if (sort_val_reverse) {
139 return msetcmp_by_value_then_relevance<true, false>;
140 } else {
141 return msetcmp_by_value_then_relevance<false, false>;
144 default:
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);
148 if (sort_forward) {
149 if (sort_val_reverse) {
150 return msetcmp_by_relevance_then_value<true, true>;
151 } else {
152 return msetcmp_by_relevance_then_value<false, true>;
154 } else {
155 if (sort_val_reverse) {
156 return msetcmp_by_relevance_then_value<true, false>;
157 } else {
158 return msetcmp_by_relevance_then_value<false, false>;