Handle removing doc term at current TermIterator position
[xapian.git] / xapian-core / api / terminfo.h
blob761f84cbb1f8c726d534e058c04a82a6155ccaa9
1 /** @file terminfo.h
2 * @brief Metadata for a term in a document
3 */
4 /* Copyright 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 USA
21 #ifndef XAPIAN_INCLUDED_TERMINFO_H
22 #define XAPIAN_INCLUDED_TERMINFO_H
24 #include "api/smallvector.h"
25 #include <xapian/types.h>
27 using namespace std;
29 /// Metadata for a term in a document
30 class TermInfo {
31 Xapian::termcount wdf;
33 /** Flag to indicate if this term was deleted from this document.
35 * We flag entries as deleted instead of actually deleting them to avoid
36 * invalidating existing TermIterator objects.
38 bool deleted = false;
40 /** Positions at which the term occurs.
42 * The entries are sorted in strictly increasing order (so duplicate
43 * entries are not allowed).
45 Xapian::VecCOW<Xapian::termpos> positions;
47 public:
48 /** Constructor.
50 * @param wdf_ Within-document frequency
52 explicit TermInfo(Xapian::termcount wdf_) : wdf(wdf_) {}
54 /** Constructor which also adds an initial position.
56 * @param wdf_ Within-document frequency
57 * @param termpos Position to add
59 TermInfo(Xapian::termcount wdf_, Xapian::termpos termpos) : wdf(wdf_) {
60 positions.push_back(termpos);
63 /// Get a pointer to the positions.
64 const Xapian::VecCOW<Xapian::termpos>* get_positions() const {
65 return &positions;
68 /// Get the within-document frequency.
69 Xapian::termcount get_wdf() const { return wdf; }
71 /** Increase within-document frequency.
73 * @return true if the term was flagged as deleted before the operation.
75 bool increase_wdf(Xapian::termcount delta) {
76 if (rare(deleted)) {
77 deleted = false;
78 wdf = delta;
79 return true;
81 wdf += delta;
82 return false;
85 /// Decrease within-document frequency.
86 void decrease_wdf(Xapian::termcount delta) {
87 // Saturating arithmetic - don't let the wdf go below zero.
88 if (wdf >= delta) {
89 wdf -= delta;
90 } else {
91 wdf = 0;
95 bool remove() {
96 if (deleted)
97 return false;
98 positions.clear();
99 deleted = true;
100 return true;
103 /** Add a position.
105 * If @a termpos is already present, this is a no-op.
107 * @param wdf_inc wdf increment
108 * @param termpos Position to add
110 * @return true if the term was flagged as deleted before the operation.
112 bool add_position(Xapian::termcount wdf_inc, Xapian::termpos termpos);
114 /** Append a position.
116 * The position must be >= the largest currently in the list.
118 void append_position(Xapian::termpos termpos) {
119 positions.push_back(termpos);
122 /** Remove a position.
124 * @param termpos Position to remove
126 * @return If @a termpos wasn't present, returns false.
128 bool remove_position(Xapian::termpos tpos);
130 /// Is this term flagged as deleted?
131 bool is_deleted() const { return deleted; }
134 #endif // XAPIAN_INCLUDED_TERMINFO_H