xapian-core  2.0.0
orpositionlist.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2010,2016,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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "orpositionlist.h"
24 
25 #include "debuglog.h"
26 
27 using namespace std;
28 
31 {
32  LOGCALL(EXPAND, Xapian::termcount, "OrPositionList::get_approx_size", NO_ARGS);
33  // This is actually the upper bound, but generally there's only one term
34  // at each position, so it'll usually be correct too.
35  Xapian::termcount size = 0;
36  for (auto pl : pls) size += pl->get_approx_size();
37  RETURN(size);
38 }
39 
42 {
43  LOGCALL(EXPAND, Xapian::termpos, "OrPositionList::back", NO_ARGS);
44  Xapian::termpos result = 0;
45  for (auto pl : pls) {
46  result = max(result, pl->back());
47  }
48  RETURN(result);
49 }
50 
53 {
54  LOGCALL(EXPAND, Xapian::termpos, "OrPositionList::get_position", NO_ARGS);
55  RETURN(current_pos);
56 }
57 
58 // PositionList::next() is actually rarely used - ExactPhrasePostList will
59 // never call it, while PhrasePostList will only call it once for the first
60 // subquery and NearPostList will call it to start subqueries if we're near
61 // the start of the document, and also if a candidate match has two subqueries
62 // at the same position.
63 bool
65 {
66  LOGCALL(EXPAND, bool, "OrPositionList::next", NO_ARGS);
67  bool first = current.empty();
68  if (first) current.resize(pls.size());
69  Xapian::termpos old_pos = current_pos;
70  current_pos = Xapian::termpos(-1);
71  size_t j = 0;
72  for (size_t i = 0; i != pls.size(); ++i) {
73  PositionList* pl = pls[i];
75  if (first || current[i] <= old_pos) {
76  if (!pl->next())
77  continue;
78  pos = pl->get_position();
79  } else {
80  pos = current[i];
81  }
82  current_pos = min(current_pos, pos);
83  current[j] = pos;
84  if (i != j) pls[j] = pls[i];
85  ++j;
86  }
87  pls.resize(j);
88  RETURN(j != 0);
89 }
90 
91 // A min-heap seems like an obvious optimisation here, but is only useful when
92 // handling clumps of terms - in particular when skip_to() advances all the
93 // sublists, the heap doesn't help (but we have the cost of rebuilding it, or N
94 // pop+push calls which has a worse complexity than rebuilding).
95 bool
97 {
98  LOGCALL(EXPAND, bool, "OrPositionList::skip_to", termpos);
99  bool first = current.empty();
100  if (!first && termpos <= current_pos)
101  RETURN(true);
102  if (first) current.resize(pls.size());
103  current_pos = Xapian::termpos(-1);
104  size_t j = 0;
105  for (size_t i = 0; i != pls.size(); ++i) {
106  PositionList* pl = pls[i];
108  if (first || termpos > current[i]) {
109  if (!pl->skip_to(termpos))
110  continue;
111  pos = pl->get_position();
112  } else {
113  pos = current[i];
114  }
115  current_pos = min(current_pos, pos);
116  current[j] = pos;
117  if (i != j) pls[j] = pls[i];
118  ++j;
119  }
120  pls.resize(j);
121  RETURN(j != 0);
122 }
bool next()
Advance to the next entry in the positionlist.
Xapian::termcount get_approx_size() const
Return approximate size of this positionlist.
Xapian::termpos get_position() const
Return the current position.
Xapian::termpos back() const
Return the final entry in this positionlist.
bool skip_to(Xapian::termpos termpos)
Skip forward to the specified position.
Abstract base class for iterating term positions in a document.
Definition: positionlist.h:32
virtual bool next()=0
Advance to the next entry in the positionlist.
virtual bool skip_to(Xapian::termpos termpos)=0
Skip forward to the specified position.
virtual Xapian::termpos get_position() const =0
Return the current position.
Xapian::termpos pos
Debug logging macros.
#define RETURN(...)
Definition: debuglog.h:484
#define LOGCALL(CATEGORY, TYPE, FUNC, PARAMS)
Definition: debuglog.h:478
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:75
Merge two PositionList objects using an OR operation.