Factor out function to decode 2 hex digits
[xapian.git] / xapian-core / include / xapian / termiterator.h
blob95eea04029786517be31d097c52fd176549a46c7
1 /** @file
2 * @brief Class for iterating over a list of terms
3 */
4 /* Copyright (C) 2007,2008,2009,2010,2011,2012,2013,2014,2015 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
19 * USA
22 #ifndef XAPIAN_INCLUDED_TERMITERATOR_H
23 #define XAPIAN_INCLUDED_TERMITERATOR_H
25 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
26 # error Never use <xapian/termiterator.h> directly; include <xapian.h> instead.
27 #endif
29 #include <iterator>
30 #include <string>
32 #include <xapian/attributes.h>
33 #include <xapian/derefwrapper.h>
34 #include <xapian/positioniterator.h>
35 #include <xapian/types.h>
36 #include <xapian/visibility.h>
38 namespace Xapian {
40 /// Class for iterating over a list of terms.
41 class XAPIAN_VISIBILITY_DEFAULT TermIterator {
42 public:
43 /// Class representing the TermIterator internals.
44 class Internal;
45 /// @private @internal Reference counted internals.
46 Internal * internal;
48 /// @private @internal Construct given internals.
49 explicit TermIterator(Internal *internal_);
51 /// Copy constructor.
52 TermIterator(const TermIterator & o);
54 /// Assignment.
55 TermIterator & operator=(const TermIterator & o);
57 #ifdef XAPIAN_MOVE_SEMANTICS
58 /// Move constructor.
59 TermIterator(TermIterator && o)
60 : internal(o.internal) {
61 o.internal = nullptr;
64 /// Move assignment operator.
65 TermIterator & operator=(TermIterator && o) {
66 if (this != &o) {
67 if (internal) decref();
68 internal = o.internal;
69 o.internal = nullptr;
71 return *this;
73 #endif
75 /** Default constructor.
77 * Creates an uninitialised iterator, which can't be used before being
78 * assigned to, but is sometimes syntactically convenient.
80 XAPIAN_NOTHROW(TermIterator())
81 : internal(0) { }
83 /// Destructor.
84 ~TermIterator() {
85 if (internal) decref();
88 /// Return the term at the current position.
89 std::string operator*() const;
91 /// Return the wdf for the term at the current position.
92 Xapian::termcount get_wdf() const;
94 /// Return the term frequency for the term at the current position.
95 Xapian::doccount get_termfreq() const;
97 /// Return the length of the position list for the current position.
98 Xapian::termcount positionlist_count() const;
100 /// Return a PositionIterator for the current term.
101 PositionIterator positionlist_begin() const;
103 /// Return an end PositionIterator for the current term.
104 PositionIterator XAPIAN_NOTHROW(positionlist_end() const) {
105 return PositionIterator();
108 /// Advance the iterator to the next position.
109 TermIterator & operator++();
111 /// Advance the iterator to the next position (postfix version).
112 DerefWrapper_<std::string> operator++(int) {
113 const std::string & term(**this);
114 operator++();
115 return DerefWrapper_<std::string>(term);
118 /** Advance the iterator to term @a term.
120 * If the iteration is over an unsorted list of terms, then this method
121 * will throw Xapian::InvalidOperationError.
123 * @param term The term to advance to. If this term isn't in
124 * the stream being iterated, then the iterator is moved
125 * to the next term after it which is.
127 void skip_to(const std::string &term);
129 /// Return a string describing this object.
130 std::string get_description() const;
132 /** @private @internal TermIterator is what the C++ STL calls an
133 * input_iterator.
135 * The following typedefs allow std::iterator_traits<> to work so that
136 * this iterator can be used with the STL.
138 * These are deliberately hidden from the Doxygen-generated docs, as the
139 * machinery here isn't interesting to API users. They just need to know
140 * that Xapian iterator classes are compatible with the STL.
142 // @{
143 /// @private
144 typedef std::input_iterator_tag iterator_category;
145 /// @private
146 typedef std::string value_type;
147 /// @private
148 typedef Xapian::termcount_diff difference_type;
149 /// @private
150 typedef std::string * pointer;
151 /// @private
152 typedef std::string & reference;
153 // @}
155 private:
156 void decref();
158 void post_advance(Internal * res);
161 bool
162 XAPIAN_NOTHROW(operator==(const TermIterator &a, const TermIterator &b));
164 /// Equality test for TermIterator objects.
165 inline bool
166 operator==(const TermIterator &a, const TermIterator &b) XAPIAN_NOEXCEPT
168 // Use a pointer comparison - this ensures both that (a == a) and correct
169 // handling of end iterators (which we ensure have NULL internals).
170 return a.internal == b.internal;
173 bool
174 XAPIAN_NOTHROW(operator!=(const TermIterator &a, const TermIterator &b));
176 /// Inequality test for TermIterator objects.
177 inline bool
178 operator!=(const TermIterator &a, const TermIterator &b) XAPIAN_NOEXCEPT
180 return !(a == b);
185 #endif // XAPIAN_INCLUDED_TERMITERATOR_H