1 /******************************************************************************
2 * Copyright (C) 2008 by Niko Valimaki <nvalimak@cs.helsinki.fi> *
3 * Text collection interface for an in-memory XQuery/XPath engine *
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. *
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. *
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 ******************************************************************************/
21 #ifndef _SXSI_TextCollection_h_
22 #define _SXSI_TextCollection_h_
24 #include "Tools.h" // Defines ulong and uchar.
26 #include <utility> // Defines std::pair.
28 // Default samplerate for suffix array samples
29 #define TEXTCOLLECTION_DEFAULT_SAMPLERATE 64
34 * General interface for a text collection
36 * Class is virtual, make objects by calling
37 * the static method InitTextCollection().
42 // Type of document identifier
44 // Type for text position (FIXME ulong or long?)
45 typedef ulong TextPosition;
48 * Init an instance of a text collection object
50 * Returns a pointer to an object implementing this interface.
52 static TextCollection * InitTextCollection(unsigned samplerate = TEXTCOLLECTION_DEFAULT_SAMPLERATE);
56 * New samplerate can be given, otherwise will use the one specified in the save file!
57 * Note: This is not a static method; call InitTextCollection() first to get the object handle.
59 * Throws an exception if std::fread() fails.
62 virtual void Load(FILE *, unsigned samplerate = 0) = 0;
64 * Save data structure into a file
66 * Throws an exception if std::fwrite() fails.
68 virtual void Save(FILE *) const = 0;
72 virtual ~TextCollection() { };
76 * Must be a zero-terminated string from alphabet [1,255].
77 * Can not be called after makeStatic().
78 * The i'th text insertion gets an identifier value i-1.
79 * In other words, document identifiers start from 0.
81 virtual void InsertText(uchar const *) = 0;
85 * Convert to a static collection; reduces space and time complexities.
86 * New texts can not be inserted after this operation.
88 virtual void MakeStatic() = 0;
91 tests if the string pointed to by DocId is empty
94 virtual bool EmptyText(DocId) const = 0;
99 * Returns the i'th text in the collection.
100 * The numbering starts from 0.
102 virtual uchar* GetText(DocId) const = 0;
104 * Returns substring [i, j] of k'th text
106 * Note: Parameters i and j are text positions inside the k'th text.
108 virtual uchar* GetText(DocId, TextPosition, TextPosition) const = 0;
110 * Returns backwards (reverse) iterator to the end of i'th text
112 * Note: Do we need this?
113 * Forward iterator would be really in-efficient compared to
114 * getText(k) and getText(k, i, j).
116 * TODO Define and implement const_reverse_iterator.
118 //const_reverse_iterator rend(DocId) const;
121 * Existential queries
123 // Is there a text prefixed by given string?
124 virtual bool IsPrefix(uchar const *) const = 0;
125 // Is there a text having given string as a suffix?
126 virtual bool IsSuffix(uchar const *) const = 0;
127 // Is there a text that equals given string?
128 virtual bool IsEqual(uchar const *) const = 0;
129 // Does a text contain given string?
130 virtual bool IsContains(uchar const *) const = 0;
131 // Is there a text that is lexicographically less than given string?
132 virtual bool IsLessThan(uchar const *) const = 0;
137 * Result is the number of documents.
139 virtual unsigned CountPrefix(uchar const *) const = 0;
140 virtual unsigned CountSuffix(uchar const *) const = 0;
141 virtual unsigned CountEqual(uchar const *) const = 0;
142 virtual unsigned CountContains(uchar const *) const = 0;
143 virtual unsigned CountLessThan(uchar const *) const = 0;
146 * Document reporting queries
148 * Result is a vector of document id's in some undefined order.
150 // Data type for results
151 typedef std::vector<DocId> document_result;
152 virtual document_result Prefix(uchar const *) const = 0;
153 virtual document_result Suffix(uchar const *) const = 0;
154 virtual document_result Equal(uchar const *) const = 0;
155 virtual document_result Contains(uchar const *) const = 0;
156 virtual document_result LessThan(uchar const *) const = 0;
159 * Full reporting queries
161 * Result is a vector of pairs <doc id, offset> in some undefined order.
163 // Data type for results
164 typedef std::vector<std::pair<DocId, TextPosition> > full_result;
165 virtual full_result FullContains(uchar const *) const = 0;
168 // Protected constructor; call the static function InitTextCollection().
169 TextCollection() { };
171 // No copy constructor or assignment
172 TextCollection(TextCollection const&);
173 TextCollection& operator = (TextCollection const&);