xapian-core  1.4.20
ortermlist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007-2021 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 USA
19  */
20 
21 #include <config.h>
22 
23 #include "ortermlist.h"
24 
25 #include "debuglog.h"
26 #include "omassert.h"
27 
29 
30 using namespace std;
31 
32 void
34 {
35  Assert(!left_current.empty());
36  Assert(!right_current.empty());
37 }
38 
40 {
41  delete left;
42  delete right;
43 }
44 
47 {
48  LOGCALL(EXPAND, Xapian::termcount, "OrTermList::get_approx_size", NO_ARGS);
49  // This is actually the upper bound, but we only use this to order the
50  // binary tree of OrTermList objects so it's probably not worth trying
51  // to be more sophisticated.
52  RETURN(left->get_approx_size() + right->get_approx_size());
53 }
54 
55 void
57 {
58  LOGCALL_VOID(EXPAND, "OrTermList::accumulate_stats", stats);
59  check_started();
60  if (left_current <= right_current) left->accumulate_stats(stats);
61  if (left_current >= right_current) right->accumulate_stats(stats);
62 }
63 
64 string
66 {
67  LOGCALL(EXPAND, string, "OrTermList::get_termname", NO_ARGS);
68  check_started();
69  if (left_current < right_current) RETURN(left_current);
70  RETURN(right_current);
71 }
72 
75 {
76  LOGCALL(EXPAND, Xapian::termcount, "OrTermList::get_wdf", NO_ARGS);
77  check_started();
78  int cmp = left_current.compare(right_current);
79  if (cmp < 0) RETURN(left->get_wdf());
80  if (cmp > 0) RETURN(right->get_wdf());
81  RETURN(left->get_wdf() + right->get_wdf());
82 }
83 
86 {
87  LOGCALL(EXPAND, Xapian::doccount, "OrTermList::get_termfreq", NO_ARGS);
88  check_started();
89  if (left_current < right_current) RETURN(left->get_termfreq());
90  Assert(left_current > right_current || left->get_termfreq() == right->get_termfreq());
91  RETURN(right->get_termfreq());
92 }
93 
94 // Helper function.
95 static inline void
96 handle_prune(TermList *& old, TermList * result)
97 {
98  if (result) {
99  delete old;
100  old = result;
101  }
102 }
103 
104 TermList *
106 {
107  LOGCALL(EXPAND, TermList *, "OrTermList::next", NO_ARGS);
108  // If we've not started yet, both left_current and right_current will be
109  // empty, so we'll take the third case below which is what we want to do to
110  // get started.
111  int cmp = left_current.compare(right_current);
112  if (cmp < 0) {
113  handle_prune(left, left->next());
114  if (left->at_end()) {
115  TermList *ret = right;
116  right = NULL;
117  RETURN(ret);
118  }
119  left_current = left->get_termname();
120  } else if (cmp > 0) {
121  handle_prune(right, right->next());
122  if (right->at_end()) {
123  TermList *ret = left;
124  left = NULL;
125  RETURN(ret);
126  }
127  right_current = right->get_termname();
128  } else {
129  AssertEq(left_current, right_current);
130  handle_prune(left, left->next());
131  handle_prune(right, right->next());
132  if (left->at_end()) {
133  // right->at_end() may also be true, but our parent will deal with
134  // that.
135  TermList *ret = right;
136  right = NULL;
137  RETURN(ret);
138  }
139  if (right->at_end()) {
140  TermList *ret = left;
141  left = NULL;
142  RETURN(ret);
143  }
144  left_current = left->get_termname();
145  right_current = right->get_termname();
146  }
147  RETURN(NULL);
148 }
149 
150 TermList *
151 OrTermList::skip_to(const string & term)
152 {
153  LOGCALL(EXPAND, TermList *, "OrTermList::skip_to", term);
154  handle_prune(left, left->skip_to(term));
155  handle_prune(right, right->skip_to(term));
156  if (left->at_end()) {
157  // right->at_end() may also be true, but our parent will deal with
158  // that.
159  TermList *ret = right;
160  right = NULL;
161  RETURN(ret);
162  }
163  if (right->at_end()) {
164  TermList *ret = left;
165  left = NULL;
166  RETURN(ret);
167  }
168  left_current = left->get_termname();
169  right_current = right->get_termname();
170  RETURN(NULL);
171 }
172 
173 bool
175 {
176  LOGCALL(EXPAND, bool, "OrTermList::at_end", NO_ARGS);
177  check_started();
178  // next() or skip_to() should have pruned if either child is at_end().
179  Assert(!left->at_end());
180  Assert(!right->at_end());
181  RETURN(false);
182 }
183 
186 {
187  Assert(false);
188  return 0;
189 }
190 
193 {
194  Assert(false);
195  return Xapian::PositionIterator();
196 }
197 
198 
201 {
202  LOGCALL(EXPAND, Xapian::doccount, "FreqAdderOrTermList::get_termfreq", NO_ARGS);
203  check_started();
204  int cmp = left_current.compare(right_current);
205  if (cmp < 0) RETURN(left->get_termfreq());
206  if (cmp > 0) RETURN(right->get_termfreq());
207  RETURN(left->get_termfreq() + right->get_termfreq());
208 }
#define RETURN(A)
Definition: debuglog.h:482
#define Assert(COND)
Definition: omassert.h:122
std::string get_termname() const
Return the termname at the current position.
Definition: ortermlist.cc:65
Xapian::PositionIterator positionlist_begin() const
Return a PositionIterator for the current position.
Definition: ortermlist.cc:192
void accumulate_stats(Xapian::Internal::ExpandStats &stats) const
Collate weighting information for the current term.
Definition: ortermlist.cc:56
Xapian::termcount get_wdf() const
Return the wdf for the term at the current position.
Definition: ortermlist.cc:74
static void handle_prune(TermList *&old, TermList *result)
Definition: ortermlist.cc:96
#define AssertEq(A, B)
Definition: omassert.h:124
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
Definition: ortermlist.cc:85
Merge two TermList objects using an OR operation.
TermList * skip_to(const std::string &term)
Skip forward to the specified term.
Definition: ortermlist.cc:151
#define LOGCALL_VOID(CATEGORY, FUNC, PARAMS)
Definition: debuglog.h:477
Abstract base class for termlists.
Definition: termlist.h:39
STL namespace.
Class for iterating over term positions.
Xapian::doccount get_termfreq() const
Return the term frequency for the term at the current position.
Definition: ortermlist.cc:200
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
Xapian::termcount positionlist_count() const
Return the length of the position list for the current position.
Definition: ortermlist.cc:185
Class for iterating over term positions.
TermList * next()
Advance the current position to the next term in the termlist.
Definition: ortermlist.cc:105
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
Xapian::termcount get_approx_size() const
Return approximate size of this termlist.
Definition: ortermlist.cc:46
Collates statistics while calculating term weight in an ESet.
Definition: expandweight.h:37
void check_started() const
Check that next() has already been called.
Definition: ortermlist.cc:33
Various assertion macros.
Debug logging macros.
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:476
bool at_end() const
Return true if the current position is past the last term in this list.
Definition: ortermlist.cc:174