1 /* maptermlist.h
2  *
3  * Copyright 1999,2000,2001 BrightStation PLC
4  * Copyright 2002,2003,2004,2005,2006,2007,2008,2010 Olly Betts
5  *
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.
10  *
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.
15  *
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
20  */
21 
22 #ifndef OM_HGUARD_MAPTERMLIST_H
23 #define OM_HGUARD_MAPTERMLIST_H
24 
25 #include "termlist.h"
26 
27 #include "inmemory_positionlist.h"
28 #include "document.h"
29 
30 #include "omassert.h"
31 
32 using namespace std;
33 
34 class MapTermList : public TermList {
35     private:
36 	Xapian::Document::Internal::document_terms::const_iterator it;
37 	Xapian::Document::Internal::document_terms::const_iterator it_end;
38 	bool started;
39 
40     public:
MapTermList(const Xapian::Document::Internal::document_terms::const_iterator & it_,const Xapian::Document::Internal::document_terms::const_iterator & it_end_)41 	MapTermList(const Xapian::Document::Internal::document_terms::const_iterator &it_,
42 		    const Xapian::Document::Internal::document_terms::const_iterator &it_end_)
43 		: it(it_), it_end(it_end_), started(false)
44 	{ }
45 
46 	// Gets size of termlist
get_approx_size()47 	Xapian::termcount get_approx_size() const {
48 	    // This method shouldn't get called on a MapTermList.
49 	    Assert(false);
50 	    return 0;
51 	}
52 
53 	// Gets current termname
get_termname()54 	string get_termname() const {
55 	    Assert(started);
56 	    Assert(!at_end());
57 	    return it->first;
58 	}
59 
60 	// Get wdf of current term
get_wdf()61 	Xapian::termcount get_wdf() const {
62 	    Assert(started);
63 	    Assert(!at_end());
64 	    return it->second.wdf;
65 	}
66 
67 	// Get num of docs indexed by term
get_termfreq()68 	Xapian::doccount get_termfreq() const {
69 	    throw Xapian::InvalidOperationError("Can't get term frequency from a document termlist which is not associated with a database.");
70 	}
71 
positionlist_begin()72 	Xapian::PositionIterator positionlist_begin() const {
73 	    return Xapian::PositionIterator(new InMemoryPositionList(it->second.positions));
74 	}
75 
positionlist_count()76 	Xapian::termcount positionlist_count() const {
77 	    return it->second.positions.size();
78 	}
79 
next()80 	TermList * next() {
81 	    if (!started) {
82 		started = true;
83 	    } else {
84 		Assert(!at_end());
85 		++it;
86 	    }
87 	    return NULL;
88 	}
89 
skip_to(const std::string & term)90 	TermList * skip_to(const std::string & term) {
91 	    while (it != it_end && it->first < term) {
92 		++it;
93 	    }
94 	    started = true;
95 	    return NULL;
96 	}
97 
98 	// True if we're off the end of the list
at_end()99 	bool at_end() const {
100 	    Assert(started);
101 	    return it == it_end;
102 	}
103 };
104 
105 #endif /* OM_HGUARD_MAPTERMLIST_H */
106