2 * @brief Class for iterating over a list of terms
4 /* Copyright (C) 2007-2024 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
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.
31 #include <string_view>
33 #include <xapian/attributes.h>
34 #include <xapian/derefwrapper.h>
35 #include <xapian/positioniterator.h>
36 #include <xapian/types.h>
37 #include <xapian/visibility.h>
41 /// Class for iterating over a list of terms.
42 class XAPIAN_VISIBILITY_DEFAULT TermIterator
{
44 /// Class representing the TermIterator internals.
46 /// @private @internal Reference counted internals.
49 /// @private @internal Wrap an existing Internal.
50 XAPIAN_VISIBILITY_INTERNAL
51 explicit TermIterator(Internal
*internal_
);
54 TermIterator(const TermIterator
& o
);
57 TermIterator
& operator=(const TermIterator
& o
);
60 TermIterator(TermIterator
&& o
)
61 : internal(o
.internal
) {
65 /// Move assignment operator.
66 TermIterator
& operator=(TermIterator
&& o
) {
68 if (internal
) decref();
69 internal
= o
.internal
;
75 /** Default constructor.
77 * Creates an uninitialised iterator, which can't be used before being
78 * assigned to, but is sometimes syntactically convenient.
80 TermIterator() noexcept
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.
96 * Note that for a TermIterator returned by calling termlist_begin()
97 * on a Document object obtained from a sharded database, this method
98 * will return the term frequency from the shard that the document is in
99 * rather than for the combined database.
101 Xapian::doccount
get_termfreq() const;
103 /// Return the length of the position list for the current position.
104 Xapian::termcount
positionlist_count() const;
106 /// Return a PositionIterator for the current term.
107 PositionIterator
positionlist_begin() const;
109 /// Return an end PositionIterator for the current term.
110 PositionIterator
positionlist_end() const noexcept
{
111 return PositionIterator();
114 /// Advance the iterator to the next position.
115 TermIterator
& operator++();
117 /// Advance the iterator to the next position (postfix version).
118 DerefWrapper_
<std::string
> operator++(int) {
119 const std::string
& term(**this);
121 return DerefWrapper_
<std::string
>(term
);
124 /** Advance the iterator to term @a term.
126 * If the iteration is over an unsorted list of terms, then this method
127 * will throw Xapian::InvalidOperationError.
129 * @param term The term to advance to. If this term isn't in
130 * the stream being iterated, then the iterator is moved
131 * to the next term after it which is.
133 void skip_to(std::string_view term
);
135 /// Return a string describing this object.
136 std::string
get_description() const;
138 /** @private @internal TermIterator is what the C++ STL calls an
141 * The following typedefs allow std::iterator_traits<> to work so that
142 * this iterator can be used with the STL.
144 * These are deliberately hidden from the Doxygen-generated docs, as the
145 * machinery here isn't interesting to API users. They just need to know
146 * that Xapian iterator classes are compatible with the STL.
150 typedef std::input_iterator_tag iterator_category
;
152 typedef std::string value_type
;
154 typedef Xapian::termcount_diff difference_type
;
156 typedef std::string
* pointer
;
158 typedef std::string
& reference
;
164 XAPIAN_VISIBILITY_INTERNAL
165 void post_advance(Internal
* res
);
168 /// Equality test for TermIterator objects.
170 operator==(const TermIterator
& a
, const TermIterator
& b
) noexcept
172 // Use a pointer comparison - this ensures both that (a == a) and correct
173 // handling of end iterators (which we ensure have NULL internals).
174 return a
.internal
== b
.internal
;
177 /// Inequality test for TermIterator objects.
179 operator!=(const TermIterator
& a
, const TermIterator
& b
) noexcept
186 #endif // XAPIAN_INCLUDED_TERMITERATOR_H