Added simple WCSA
[SXSI/TextCollection.git] / TextCollection.h
1 /******************************************************************************
2  *   Copyright (C) 2008 by Niko Valimaki <nvalimak@cs.helsinki.fi>            *
3  *   Text collection interface for an in-memory XQuery/XPath engine           *
4  *                                                                            *
5  *   This program is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Lesser General Public License as published *
7  *   by the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                      *
9  *                                                                            *
10  *   This program is distributed in the hope that it will be useful,          *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
13  *   GNU Lesser General Public License for more details.                      *
14  *                                                                            *
15  *   You should have received a copy of the GNU Lesser General Public License *
16  *   along with this program; if not, write to the                            *
17  *   Free Software Foundation, Inc.,                                          *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
19  ******************************************************************************/ 
20
21 #ifndef _SXSI_TextCollection_h_
22 #define _SXSI_TextCollection_h_
23
24 #include "Tools.h" // Defines ulong and uchar.
25 #include <vector>
26 #include <utility> // Defines std::pair.
27
28 namespace SXSI
29 {
30
31     /**
32      * General interface for a text collection
33      *
34      * Class is virtual, make objects by calling 
35      * the static method InitTextCollection().
36      */
37     class TextCollection
38     {
39     public:
40         // Type of document identifier
41         typedef int DocId;
42         // Type for text position (FIXME ulong or long?)
43         typedef ulong TextPosition;
44
45         // mode flag: Default includes both the index and "naive" text
46         enum index_mode_t { index_mode_default, index_mode_text_only }; // index_mode_index_only
47
48         /**
49          * Load from a file
50          *
51          * New samplerate can be given, otherwise will use the one specified in the save file!
52          *
53          * Throws an exception if std::fread() fails.
54          * 
55          */
56         static TextCollection* Load(FILE *, index_mode_t = index_mode_default, unsigned samplerate = 0);
57
58         /**
59          * Save data structure into a file
60          * 
61          * Throws an exception if std::fwrite() fails.
62          */
63         virtual void Save(FILE *) const = 0;
64
65         /**
66          * Virtual destructor
67          */
68         virtual ~TextCollection() { };
69
70         /**
71          * Tests if the string pointed to by DocId is empty
72          */
73         virtual bool EmptyText(DocId) const = 0;
74
75         /**
76          * Displaying content
77          *
78          * Returns the i'th text in the collection.
79          * The numbering starts from 0.
80          *
81          * Call DeleteText() for each pointer returned by GetText()
82          * to avoid possible memory leaks.
83          */
84         virtual uchar* GetText(DocId) const = 0;
85         virtual void DeleteText(uchar *text) const = 0;
86
87         /**
88          * Returns a pointer to the beginning of texts i, i+1, ..., j.
89          * Texts are separated by a '\0' byte.
90          *
91          * Call DeleteText() for each pointer returned by GetText()
92          * to avoid possible memory leaks.
93          */
94         virtual uchar * GetText(DocId i, DocId j) const = 0;
95
96         /**
97          * Returns substring [i, j] of k'th text
98          *
99          * Note: Parameters i and j are text positions inside the k'th text.
100          */
101 //        virtual uchar* GetText(DocId, TextPosition, TextPosition) const = 0;
102         /**
103          * Returns backwards (reverse) iterator to the end of i'th text
104          * 
105          * Note: Do we need this?
106          * Forward iterator would be really in-efficient compared to
107          * getText(k).
108          *
109          * TODO Define and implement const_reverse_iterator.
110          */
111         //const_reverse_iterator rend(DocId) const;
112         
113         /**
114          * Existential queries
115          */
116         // Is there a text prefixed by given string?
117         virtual bool IsPrefix(uchar const *) const = 0;
118         // Is there a text having given string as a suffix?
119         virtual bool IsSuffix(uchar const *) const = 0;
120         // Is there a text that equals given string?
121         virtual bool IsEqual(uchar const *) const = 0;
122         // Does a text contain given string?
123         virtual bool IsContains(uchar const *) const = 0;
124         // Is there a text that is lexicographically less than given string?
125         virtual bool IsLessThan(uchar const *) const = 0;
126
127         /**
128          * Existential queries for given DocId interval.
129          */
130         virtual bool IsPrefix(uchar const *, DocId, DocId) const = 0;
131         virtual bool IsSuffix(uchar const *, DocId, DocId) const = 0;
132         virtual bool IsEqual(uchar const *, DocId, DocId) const = 0;
133         virtual bool IsContains(uchar const *, DocId, DocId) const = 0;
134         virtual bool IsLessThan(uchar const *, DocId, DocId) const = 0;
135
136         /**
137          * Counting queries
138          * Result is the number of occurrences.
139          */
140         virtual ulong Count(uchar const *) const = 0;
141         /**
142          * More counting queries
143          * Result is the number of documents.
144          */
145         virtual unsigned CountPrefix(uchar const *) const = 0;
146         virtual unsigned CountSuffix(uchar const *) const = 0;
147         virtual unsigned CountEqual(uchar const *) const = 0;
148         virtual unsigned CountContains(uchar const *) const = 0;
149         virtual unsigned CountLessThan(uchar const *) const = 0;
150
151         /**
152          * Counting queries for given DocId interval
153          */
154         virtual unsigned CountPrefix(uchar const *, DocId, DocId) const = 0;
155         virtual unsigned CountSuffix(uchar const *, DocId, DocId) const = 0;
156         virtual unsigned CountEqual(uchar const *, DocId, DocId) const = 0;
157         virtual unsigned CountContains(uchar const *, DocId, DocId) const = 0;
158         virtual unsigned CountLessThan(uchar const *, DocId, DocId) const = 0;
159
160         /**
161          * Document reporting queries
162          *
163          * Result is a vector of document id's in some undefined order.
164          */
165         // Data type for results
166         typedef std::vector<DocId> document_result;
167         virtual document_result Prefix(uchar const *) const = 0;
168         virtual document_result Suffix(uchar const *) const = 0;
169         virtual document_result Equal(uchar const *) const = 0;
170         virtual document_result Contains(uchar const *) const = 0;
171         virtual document_result LessThan(uchar const *) const = 0;
172         virtual document_result KMismaches(uchar const *, unsigned) const = 0;
173         virtual document_result KErrors(uchar const *, unsigned) const = 0;
174     
175         /**
176          * Document reporting queries for given DocId interval.
177          */
178         virtual document_result Prefix(uchar const *, DocId, DocId) const = 0;
179         virtual document_result Suffix(uchar const *, DocId, DocId) const = 0;
180         virtual document_result Equal(uchar const *, DocId, DocId) const = 0;
181         virtual document_result Contains(uchar const *, DocId, DocId) const = 0;
182         virtual document_result LessThan(uchar const *, DocId, DocId) const = 0;
183
184         /**
185          * Full reporting queries
186          *
187          * Result is a vector of pairs <doc id, offset> in some undefined order.
188          */
189         // Data type for results
190         typedef std::vector<std::pair<DocId, TextPosition> > full_result;
191         virtual full_result FullContains(uchar const *) const = 0;
192         // Full reporting query for given DocId interval
193         virtual full_result FullContains(uchar const *, DocId, DocId) const = 0;
194
195         virtual full_result FullKMismatches(uchar const *, unsigned) const = 0;
196         virtual full_result FullKErrors(uchar const *, unsigned) const = 0;
197
198     protected:
199         // Protected constructor; use TextCollectionBuilder
200         TextCollection() { };
201
202 //        index_mode_t indexMode;
203
204         // No copy constructor or assignment
205         TextCollection(TextCollection const&);
206         TextCollection& operator = (TextCollection const&);
207     };
208 }
209 #endif