Remove unused header include
[xapian.git] / xapian-core / include / xapian / keymaker.h
blob572a50cdd1e947635945a5baba35626fe65f0b6e
1 /** @file keymaker.h
2 * @brief Build key strings for MSet ordering or collapsing.
3 */
4 /* Copyright (C) 2007,2009,2011,2013,2014,2015,2016 Olly Betts
5 * Copyright (C) 2010 Richard Boulton
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (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 #ifndef XAPIAN_INCLUDED_KEYMAKER_H
23 #define XAPIAN_INCLUDED_KEYMAKER_H
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error "Never use <xapian/keymaker.h> directly; include <xapian.h> instead."
27 #endif
29 #include <string>
30 #include <vector>
32 #include <xapian/intrusive_ptr.h>
33 #include <xapian/types.h>
34 #include <xapian/visibility.h>
36 namespace Xapian {
38 class Document;
40 /** Virtual base class for key making functors. */
41 class XAPIAN_VISIBILITY_DEFAULT KeyMaker
42 : public Xapian::Internal::opt_intrusive_base {
43 /// Don't allow assignment.
44 void operator=(const KeyMaker &);
46 /// Don't allow copying.
47 KeyMaker(const KeyMaker &);
49 public:
50 /// Default constructor.
51 KeyMaker() { }
53 /** Build a key string for a Document.
55 * These keys can be used for sorting or collapsing matching documents.
57 * @param doc Document object to build a key for.
59 virtual std::string operator()(const Xapian::Document & doc) const = 0;
61 /** Virtual destructor, because we have virtual methods. */
62 virtual ~KeyMaker();
64 KeyMaker * release() {
65 opt_intrusive_base::release();
66 return this;
69 const KeyMaker * release() const {
70 opt_intrusive_base::release();
71 return this;
75 /** KeyMaker subclass which combines several values.
77 * When the result is used for sorting, results are ordered by the first
78 * value. In the event of a tie, the second is used. If this is the same for
79 * both, the third is used, and so on. If @a reverse is true for a value,
80 * then the sort order for that value is reversed.
82 * When used for collapsing, the documents will only be considered equal if
83 * all the values specified match. If none of the specified values are set
84 * then the generated key will be empty, so such documents won't be collapsed
85 * (which is consistent with the behaviour in the "collapse on a value" case).
86 * If you'd prefer that documents with none of the keys set are collapsed
87 * together, then you can set @a reverse for at least one of the values.
88 * Other than this, it isn't useful to set @a reverse for collapsing.
90 class XAPIAN_VISIBILITY_DEFAULT MultiValueKeyMaker : public KeyMaker {
91 struct KeySpec {
92 Xapian::valueno slot;
93 bool reverse;
94 std::string defvalue;
95 KeySpec(Xapian::valueno slot_, bool reverse_,
96 const std::string & defvalue_)
97 : slot(slot_), reverse(reverse_), defvalue(defvalue_)
100 std::vector<KeySpec> slots;
102 public:
103 MultiValueKeyMaker() { }
105 template <class Iterator>
106 MultiValueKeyMaker(Iterator begin, Iterator end) {
107 while (begin != end) add_value(*begin++);
110 virtual std::string operator()(const Xapian::Document & doc) const;
112 /** Add a value slot to the list to build a key from.
114 * @param slot The value slot to add
115 * @param reverse Adjust values from this slot to reverse their sort
116 * order (default: false)
117 * @param defvalue Value to use for documents which don't have a value
118 * set in this slot (default: empty). This can be used
119 * to make such documents sort after all others by
120 * passing <code>get_value_upper_bound(slot) + "x"</code>
121 * - this is guaranteed to be greater than any value in
122 * this slot.
124 void add_value(Xapian::valueno slot, bool reverse = false,
125 const std::string & defvalue = std::string()) {
126 slots.push_back(KeySpec(slot, reverse, defvalue));
132 #endif // XAPIAN_INCLUDED_KEYMAKER_H