Removed
[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         /**
46          * Load from a file
47          *
48          * New samplerate can be given, otherwise will use the one specified in the save file!
49          * Note: This is not a static method; call InitTextCollection() first to get the object handle.
50          *
51          * Throws an exception if std::fread() fails.
52          * 
53          */
54         static TextCollection* Load(FILE *, unsigned samplerate = 0);
55
56         /**
57          * Save data structure into a file
58          * 
59          * Throws an exception if std::fwrite() fails.
60          */
61         virtual void Save(FILE *) const = 0;
62
63         /**
64          * Virtual destructor
65          */
66         virtual ~TextCollection() { };
67
68         /**
69          * Tests if the string pointed to by DocId is empty
70          */
71         virtual bool EmptyText(DocId) const = 0;
72
73         /**
74          * Displaying content
75          *
76          * Returns the i'th text in the collection.
77          * The numbering starts from 0.
78          */
79         virtual uchar* GetText(DocId) const = 0;
80         /**
81          * Returns substring [i, j] of k'th text
82          *
83          * Note: Parameters i and j are text positions inside the k'th text.
84          */
85 //        virtual uchar* GetText(DocId, TextPosition, TextPosition) const = 0;
86         /**
87          * Returns backwards (reverse) iterator to the end of i'th text
88          * 
89          * Note: Do we need this?
90          * Forward iterator would be really in-efficient compared to
91          * getText(k).
92          *
93          * TODO Define and implement const_reverse_iterator.
94          */
95         //const_reverse_iterator rend(DocId) const;
96         
97         /**
98          * Existential queries
99          */
100         // Is there a text prefixed by given string?
101         virtual bool IsPrefix(uchar const *) const = 0;
102         // Is there a text having given string as a suffix?
103         virtual bool IsSuffix(uchar const *) const = 0;
104         // Is there a text that equals given string?
105         virtual bool IsEqual(uchar const *) const = 0;
106         // Does a text contain given string?
107         virtual bool IsContains(uchar const *) const = 0;
108         // Is there a text that is lexicographically less than given string?
109         virtual bool IsLessThan(uchar const *) const = 0;
110
111         /**
112          * Existential queries for given DocId interval.
113          */
114         virtual bool IsPrefix(uchar const *, DocId, DocId) const = 0;
115         virtual bool IsSuffix(uchar const *, DocId, DocId) const = 0;
116         virtual bool IsEqual(uchar const *, DocId, DocId) const = 0;
117         virtual bool IsContains(uchar const *, DocId, DocId) const = 0;
118         virtual bool IsLessThan(uchar const *, DocId, DocId) const = 0;
119
120         /**
121          * Counting queries
122          * Result is the number of occurrences.
123          */
124         virtual ulong Count(uchar const *) const = 0;
125         /**
126          * More counting queries
127          * Result is the number of documents.
128          */
129         virtual unsigned CountPrefix(uchar const *) const = 0;
130         virtual unsigned CountSuffix(uchar const *) const = 0;
131         virtual unsigned CountEqual(uchar const *) const = 0;
132         virtual unsigned CountContains(uchar const *) const = 0;
133         virtual unsigned CountLessThan(uchar const *) const = 0;
134
135         /**
136          * Counting queries for given DocId interval
137          */
138         virtual unsigned CountPrefix(uchar const *, DocId, DocId) const = 0;
139         virtual unsigned CountSuffix(uchar const *, DocId, DocId) const = 0;
140         virtual unsigned CountEqual(uchar const *, DocId, DocId) const = 0;
141         virtual unsigned CountContains(uchar const *, DocId, DocId) const = 0;
142         virtual unsigned CountLessThan(uchar const *, DocId, DocId) const = 0;
143
144         /**
145          * Document reporting queries
146          *
147          * Result is a vector of document id's in some undefined order.
148          */
149         // Data type for results
150         typedef std::vector<DocId> document_result;
151         virtual document_result Prefix(uchar const *) const = 0;
152         virtual document_result Suffix(uchar const *) const = 0;
153         virtual document_result Equal(uchar const *) const = 0;
154         virtual document_result Contains(uchar const *) const = 0;
155         virtual document_result LessThan(uchar const *) const = 0;
156
157         /**
158          * Document reporting queries for given DocId interval.
159          */
160         virtual document_result Prefix(uchar const *, DocId, DocId) const = 0;
161         virtual document_result Suffix(uchar const *, DocId, DocId) const = 0;
162         virtual document_result Equal(uchar const *, DocId, DocId) const = 0;
163         virtual document_result Contains(uchar const *, DocId, DocId) const = 0;
164         virtual document_result LessThan(uchar const *, DocId, DocId) const = 0;
165
166         /**
167          * Full reporting queries
168          *
169          * Result is a vector of pairs <doc id, offset> in some undefined order.
170          */
171         // Data type for results
172         typedef std::vector<std::pair<DocId, TextPosition> > full_result;
173         virtual full_result FullContains(uchar const *) const = 0;
174         // Full reporting query for given DocId interval
175         virtual full_result FullContains(uchar const *, DocId, DocId) const = 0;
176
177     protected:
178         // Protected constructor; use TextCollectionBuilder
179         TextCollection() { };
180
181         // No copy constructor or assignment
182         TextCollection(TextCollection const&);
183         TextCollection& operator = (TextCollection const&);
184     };
185 }
186 #endif