myhtmlparse.cc: Remove unused header.
[xapian.git] / xapian-core / api / decvalwtsource.cc
blobc11432ca18aec46be52b4babc721870490494c65
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 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(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(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 || db.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 (value_it == db.valuestream_end(slot)) 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 value_it.skip_to(range_end + 1);
103 if (value_it != db.valuestream_end(slot))
104 curr_weight = Xapian::ValueWeightPostingSource::get_weight();
106 } else {
107 if (curr_weight < min_wt) {
108 // terminate early.
109 value_it = db.valuestream_end(slot);
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 value_it = db.valuestream_end(slot);
122 started = true;
123 return;
125 Xapian::ValueWeightPostingSource::next(min_wt);
126 skip_if_in_range(min_wt);
129 void
130 DecreasingValueWeightPostingSource::skip_to(Xapian::docid min_docid,
131 double min_wt) {
132 if (get_maxweight() < min_wt) {
133 value_it = db.valuestream_end(slot);
134 started = true;
135 return;
137 Xapian::ValueWeightPostingSource::skip_to(min_docid, min_wt);
138 skip_if_in_range(min_wt);
141 bool
142 DecreasingValueWeightPostingSource::check(Xapian::docid min_docid,
143 double min_wt) {
144 if (get_maxweight() < min_wt) {
145 value_it = db.valuestream_end(slot);
146 started = true;
147 return true;
149 bool valid = Xapian::ValueWeightPostingSource::check(min_docid, min_wt);
150 if (valid) {
151 skip_if_in_range(min_wt);
153 return valid;
156 std::string
157 DecreasingValueWeightPostingSource::get_description() const {
158 return "DecreasingValueWeightPostingSource()";