xapian-core  2.0.0
terminfo.h
Go to the documentation of this file.
1 
4 /* Copyright 2017,2018,2019 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 #ifndef XAPIAN_INCLUDED_TERMINFO_H
22 #define XAPIAN_INCLUDED_TERMINFO_H
23 
24 #include "api/smallvector.h"
25 #include <xapian/types.h>
26 
28 class TermInfo {
30 
48  mutable unsigned split = 0;
49 
56 
58  void merge() const;
59 
60  public:
65  explicit TermInfo(Xapian::termcount wdf_) : wdf(wdf_) {}
66 
73  positions.push_back(termpos);
74  }
75 
78  if (split) merge();
79  return &positions;
80  }
81 
82  bool has_positions() const { return !positions.empty(); }
83 
84  size_t count_positions() const { return positions.size(); }
85 
87  Xapian::termcount get_wdf() const { return wdf; }
88 
94  if (rare(is_deleted())) {
95  split = 0;
96  wdf = delta;
97  return true;
98  }
99  wdf += delta;
100  return false;
101  }
102 
108  // Saturating arithmetic - don't let the wdf go below zero.
109  if (wdf >= delta) {
110  wdf -= delta;
111  } else {
112  wdf = 0;
113  }
114  if (wdf == 0 && positions.empty()) {
115  // Flag term as deleted if no wdf or positions.
116  split = 1;
117  return true;
118  }
119  return false;
120  }
121 
122  bool remove() {
123  if (is_deleted())
124  return false;
125  positions.clear();
126  split = 1;
127  return true;
128  }
129 
140 
146  positions.push_back(termpos);
147  }
148 
156 
167  Xapian::termpos termpos_last);
168 
174  bool is_deleted() const { return positions.empty() && split > 0; }
175 };
176 
177 #endif // XAPIAN_INCLUDED_TERMINFO_H
Metadata for a term in a document.
Definition: terminfo.h:28
unsigned split
Split point in the position range.
Definition: terminfo.h:48
bool decrease_wdf(Xapian::termcount delta)
Decrease within-document frequency.
Definition: terminfo.h:107
Xapian::termcount wdf
Definition: terminfo.h:29
bool add_position(Xapian::termcount wdf_inc, Xapian::termpos termpos)
Add a position.
Definition: terminfo.cc:43
bool is_deleted() const
Has this term been deleted from this document?
Definition: terminfo.h:174
bool remove()
Definition: terminfo.h:122
bool remove_position(Xapian::termpos termpos)
Remove a position.
Definition: terminfo.cc:110
void merge() const
Merge sorted ranges before and after split.
Definition: terminfo.cc:33
Xapian::termpos remove_positions(Xapian::termpos termpos_first, Xapian::termpos termpos_last)
Remove a range of positions.
Definition: terminfo.cc:145
TermInfo(Xapian::termcount wdf_, Xapian::termpos termpos)
Constructor which also adds an initial position.
Definition: terminfo.h:72
bool increase_wdf(Xapian::termcount delta)
Increase within-document frequency.
Definition: terminfo.h:93
Xapian::termcount get_wdf() const
Get the within-document frequency.
Definition: terminfo.h:87
TermInfo(Xapian::termcount wdf_)
Constructor.
Definition: terminfo.h:65
size_t count_positions() const
Definition: terminfo.h:84
bool has_positions() const
Definition: terminfo.h:82
void append_position(Xapian::termpos termpos)
Append a position.
Definition: terminfo.h:145
const Xapian::VecCOW< Xapian::termpos > * get_positions() const
Get a pointer to the positions.
Definition: terminfo.h:77
Xapian::VecCOW< Xapian::termpos > positions
Positions at which the term occurs.
Definition: terminfo.h:55
Suitable for "simple" type T.
Definition: smallvector.h:62
#define rare(COND)
Definition: config.h:607
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
Custom vector implementations using small vector optimisation.
typedefs for Xapian