xapian-core  1.4.20
msetcmp.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2009,2013,2017 Olly Betts
5  *
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.
10  *
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.
15  *
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
20  */
21 
22 #include <config.h>
23 #include "msetcmp.h"
24 
25 #include "omassert.h"
26 
27 /* We use templates to generate the 14 different comparison functions
28  * which we need. This avoids having to write them all out by hand.
29  */
30 
31 // Order by did. Helper comparison template function, which is used as the
32 // last fallback by the others.
33 template<bool FORWARD_DID, bool CHECK_DID_ZERO>
34 static inline bool
37 {
38  if (FORWARD_DID) {
39  if (CHECK_DID_ZERO) {
40  // We want dummy did 0 to compare worse than any other.
41  if (a.did == 0) return false;
42  if (b.did == 0) return true;
43  }
44  return (a.did < b.did);
45  } else {
46  return (a.did > b.did);
47  }
48 }
49 
50 // Order by relevance, then docid.
51 template<bool FORWARD_DID>
52 static bool
55 {
56  if (a.wt > b.wt) return true;
57  if (a.wt < b.wt) return false;
58  return msetcmp_by_did<FORWARD_DID, true>(a, b);
59 }
60 
61 // Order by value, then docid.
62 template<bool FORWARD_VALUE, bool FORWARD_DID>
63 static bool
66 {
67  if (!FORWARD_VALUE) {
68  // We want dummy did 0 to compare worse than any other.
69  if (a.did == 0) return false;
70  if (b.did == 0) return true;
71  }
72  int sort_cmp = a.sort_key.compare(b.sort_key);
73  if (sort_cmp > 0) return FORWARD_VALUE;
74  if (sort_cmp < 0) return !FORWARD_VALUE;
75  return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
76 }
77 
78 // Order by value, then relevance, then docid.
79 template<bool FORWARD_VALUE, bool FORWARD_DID>
80 static bool
83 {
84  if (!FORWARD_VALUE) {
85  // two special cases to make min_item compares work when did == 0
86  if (a.did == 0) return false;
87  if (b.did == 0) return true;
88  }
89  int sort_cmp = a.sort_key.compare(b.sort_key);
90  if (sort_cmp > 0) return FORWARD_VALUE;
91  if (sort_cmp < 0) return !FORWARD_VALUE;
92  if (a.wt > b.wt) return true;
93  if (a.wt < b.wt) return false;
94  return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
95 }
96 
97 // Order by relevance, then value, then docid.
98 template<bool FORWARD_VALUE, bool FORWARD_DID>
99 static bool
102 {
103  if (!FORWARD_VALUE) {
104  // two special cases to make min_item compares work when did == 0
105  if (a.did == 0) return false;
106  if (b.did == 0) return true;
107  }
108  if (a.wt > b.wt) return true;
109  if (a.wt < b.wt) return false;
110  int sort_cmp = a.sort_key.compare(b.sort_key);
111  if (sort_cmp > 0) return FORWARD_VALUE;
112  if (sort_cmp < 0) return !FORWARD_VALUE;
113  return msetcmp_by_did<FORWARD_DID, FORWARD_VALUE>(a, b);
114 }
115 
116 MSetCmp
118  bool sort_forward,
119  bool sort_val_reverse)
120 {
121  switch (sort_by) {
123  if (sort_forward)
124  return msetcmp_by_relevance<true>;
125  else
126  return msetcmp_by_relevance<false>;
128  if (sort_forward) {
129  if (sort_val_reverse) {
130  return msetcmp_by_value<true, true>;
131  } else {
132  return msetcmp_by_value<false, true>;
133  }
134  } else {
135  if (sort_val_reverse) {
136  return msetcmp_by_value<true, false>;
137  } else {
138  return msetcmp_by_value<false, false>;
139  }
140  }
142  if (sort_forward) {
143  if (sort_val_reverse) {
144  return msetcmp_by_value_then_relevance<true, true>;
145  } else {
146  return msetcmp_by_value_then_relevance<false, true>;
147  }
148  } else {
149  if (sort_val_reverse) {
150  return msetcmp_by_value_then_relevance<true, false>;
151  } else {
152  return msetcmp_by_value_then_relevance<false, false>;
153  }
154  }
155  default:
156  // Must be REL_VAL, but handle with "default" to avoid warnings
157  // about falling off the end of the function.
159  if (sort_forward) {
160  if (sort_val_reverse) {
161  return msetcmp_by_relevance_then_value<true, true>;
162  } else {
163  return msetcmp_by_relevance_then_value<false, true>;
164  }
165  } else {
166  if (sort_val_reverse) {
167  return msetcmp_by_relevance_then_value<true, false>;
168  } else {
169  return msetcmp_by_relevance_then_value<false, false>;
170  }
171  }
172  }
173 }
static bool msetcmp_by_value_then_relevance(const Xapian::Internal::MSetItem &a, const Xapian::Internal::MSetItem &b)
Definition: msetcmp.cc:81
#define AssertEq(A, B)
Definition: omassert.h:124
Xapian::docid did
Document id.
string sort_key
Used when sorting by value.
bool(* MSetCmp)(const Xapian::Internal::MSetItem &, const Xapian::Internal::MSetItem &)
Definition: msetcmp.h:28
double wt
Weight calculated.
An item resulting from a query.
static bool msetcmp_by_relevance_then_value(const Xapian::Internal::MSetItem &a, const Xapian::Internal::MSetItem &b)
Definition: msetcmp.cc:100
MSetItem comparison functions.
static bool msetcmp_by_relevance(const Xapian::Internal::MSetItem &a, const Xapian::Internal::MSetItem &b)
Definition: msetcmp.cc:53
MSetCmp get_msetcmp_function(Xapian::Enquire::Internal::sort_setting sort_by, bool sort_forward, bool sort_val_reverse)
Select the appropriate msetcmp function.
Definition: msetcmp.cc:117
Various assertion macros.
static bool msetcmp_by_value(const Xapian::Internal::MSetItem &a, const Xapian::Internal::MSetItem &b)
Definition: msetcmp.cc:64
static bool msetcmp_by_did(const Xapian::Internal::MSetItem &a, const Xapian::Internal::MSetItem &b)
Definition: msetcmp.cc:35