Remove unused header include
[xapian.git] / xapian-core / api / decvalwtsource.cc
blob9eb29651fbb52937c8a85ca5d625a62c4988b733
1 /** @file decvalwtsource.cc
2 * @brief A posting source which returns decreasing weights from a value.
3 */
4 /* Copyright (C) 2009 Lemur Consulting Ltd
5 * Copyright (C) 2011,2012,2015,2016 Olly Betts
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <config.h>
24 #include "xapian/postingsource.h"
25 #include "xapian/error.h"
26 #include "net/length.h"
27 #include "serialise-double.h"
28 #include <cmath>
30 using namespace Xapian;
32 DecreasingValueWeightPostingSource::DecreasingValueWeightPostingSource(
33 Xapian::valueno slot_,
34 Xapian::docid range_start_,
35 Xapian::docid range_end_)
36 : Xapian::ValueWeightPostingSource(slot_),
37 range_start(range_start_),
38 range_end(range_end_)
42 double
43 DecreasingValueWeightPostingSource::get_weight() const {
44 return curr_weight;
47 Xapian::DecreasingValueWeightPostingSource *
48 DecreasingValueWeightPostingSource::clone() const {
49 return new DecreasingValueWeightPostingSource(get_slot(), range_start,
50 range_end);
53 std::string
54 DecreasingValueWeightPostingSource::name() const {
55 return "Xapian::DecreasingValueWeightPostingSource";
58 std::string
59 DecreasingValueWeightPostingSource::serialise() const {
60 std::string result;
61 result += encode_length(get_slot());
62 result += encode_length(range_start);
63 result += encode_length(range_end);
64 return result;
67 Xapian::DecreasingValueWeightPostingSource *
68 DecreasingValueWeightPostingSource::unserialise(const std::string &s) const {
69 const char * pos = s.data();
70 const char * end = pos + s.size();
71 Xapian::valueno new_slot;
72 Xapian::docid new_range_start, new_range_end;
73 decode_length(&pos, end, new_slot);
74 decode_length(&pos, end, new_range_start);
75 decode_length(&pos, end, new_range_end);
76 if (pos != end)
77 throw Xapian::NetworkError("Junk at end of serialised "
78 "DecreasingValueWeightPostingSource");
79 return new DecreasingValueWeightPostingSource(new_slot, new_range_start,
80 new_range_end);
83 void
84 DecreasingValueWeightPostingSource::init(const Xapian::Database & db_) {
85 Xapian::ValueWeightPostingSource::init(db_);
86 if (range_end == 0 || get_database().get_doccount() <= range_end)
87 items_at_end = false;
88 else
89 items_at_end = true;
92 void
93 DecreasingValueWeightPostingSource::skip_if_in_range(double min_wt)
95 if (ValueWeightPostingSource::at_end()) return;
96 curr_weight = Xapian::ValueWeightPostingSource::get_weight();
97 Xapian::docid docid = Xapian::ValueWeightPostingSource::get_docid();
98 if (docid >= range_start && (range_end == 0 || docid <= range_end)) {
99 if (items_at_end) {
100 if (curr_weight < min_wt) {
101 // skip to end of range.
102 ValueWeightPostingSource::skip_to(range_end + 1, min_wt);
103 if (!ValueWeightPostingSource::at_end())
104 curr_weight = Xapian::ValueWeightPostingSource::get_weight();
106 } else {
107 if (curr_weight < min_wt) {
108 // terminate early.
109 done();
110 } else {
111 // Update max_weight.
112 set_maxweight(curr_weight);
118 void
119 DecreasingValueWeightPostingSource::next(double min_wt) {
120 if (get_maxweight() < min_wt) {
121 done();
122 return;
124 Xapian::ValueWeightPostingSource::next(min_wt);
125 skip_if_in_range(min_wt);
128 void
129 DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
130 double min_wt) {
131 if (get_maxweight() < min_wt) {
132 done();
133 return;
135 Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
136 skip_if_in_range(min_wt);
139 bool
140 DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
141 double min_wt) {
142 if (get_maxweight() < min_wt) {
143 done();
144 return true;
146 bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
147 if (valid) {
148 skip_if_in_range(min_wt);
150 return valid;
153 std::string
154 DecreasingValueWeightPostingSource::get_description() const {
155 return "DecreasingValueWeightPostingSource()";