1 /******************************************************************************
2 * Copyright (C) 2006-2008 by Veli Mäkinen and Niko Välimäki *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 *****************************************************************************/
21 #ifndef _TCImplementation_H_
22 #define _TCImplementation_H_
24 #include "TextCollection.h"
25 #include "BlockArray.h"
27 // Include from XMLTree/libcds
28 #include <basics.h> // Defines W == 32
29 #include <static_bitsequence.h>
30 #include <alphabet_mapper.h>
31 #include <static_sequence.h>
32 #include <static_sequence_wvtree_noptrs.h>
34 // Re-define word size to ulong:
43 #include "TextStorage.h"
51 * Implementation of the TextCollection interface
54 class TCImplementation : public SXSI::TextCollection {
56 TCImplementation(uchar *, ulong, unsigned, unsigned, ulong);
59 bool EmptyText(DocId) const;
62 * Returns a pointer to the original text.
64 * Do *not* try to free the array.
65 * (However, this implementation is suspect to change.)
67 * See TextStorage.h for details.
69 uchar * GetText(DocId) const;
72 * Returns a pointer to the beginning of texts i, i+1, ..., j.
73 * Texts are separated by a '\0' byte.
75 * Do *not* try to free the array.
76 * (However, this implementation is suspect to change.)
78 * See TextStorage.h for details.
80 uchar * GetText(DocId i, DocId j) const;
83 * Returns a substring of given text ID.
85 * FIXME This may be reimplemented via TextStorage.
87 // uchar* GetText(DocId, TextPosition, TextPosition) const;
89 bool IsPrefix(uchar const *) const;
90 bool IsSuffix(uchar const *) const;
91 bool IsEqual(uchar const *) const;
92 bool IsContains(uchar const *) const;
93 bool IsLessThan(uchar const *) const;
95 bool IsPrefix(uchar const *, DocId, DocId) const;
96 bool IsSuffix(uchar const *, DocId, DocId) const;
97 bool IsEqual(uchar const *, DocId, DocId) const;
98 bool IsContains(uchar const *, DocId, DocId) const;
99 bool IsLessThan(uchar const *, DocId, DocId) const;
101 ulong Count(uchar const *) const;
102 unsigned CountPrefix(uchar const *) const;
103 unsigned CountSuffix(uchar const *) const;
104 unsigned CountEqual(uchar const *) const;
105 unsigned CountContains(uchar const *) const;
106 unsigned CountLessThan(const unsigned char*) const;
108 unsigned CountPrefix(uchar const *, DocId, DocId) const;
109 unsigned CountSuffix(uchar const *, DocId, DocId) const;
110 unsigned CountEqual(uchar const *, DocId, DocId) const;
111 unsigned CountContains(uchar const *, DocId, DocId) const;
112 unsigned CountLessThan(uchar const *, DocId, DocId) const;
114 // Definition of document_result is inherited from SXSI::TextCollection.
115 document_result Prefix(uchar const *) const;
116 document_result Suffix(uchar const *) const;
117 document_result Equal(uchar const *) const;
118 document_result Contains(uchar const *) const;
119 document_result LessThan(uchar const *) const;
121 document_result Prefix(uchar const *, DocId, DocId) const;
122 document_result Suffix(uchar const *, DocId, DocId) const;
123 document_result Equal(uchar const *, DocId, DocId) const;
124 document_result Contains(uchar const *, DocId, DocId) const;
125 document_result LessThan(uchar const *, DocId, DocId) const;
127 // Definition of full_result is inherited from SXSI::TextCollection.
128 full_result FullContains(uchar const *) const;
129 full_result FullContains(uchar const *, DocId, DocId) const;
131 // Index from/to disk
132 TCImplementation(FILE *, unsigned);
133 void Save(FILE *) const;
136 static const uchar versionFlag;
140 TextPosition bwtEndPos;
141 static_sequence * alphabetrank;
143 // Sample structures for texts longer than samplerate
144 static_bitsequence * sampled;
145 BlockArray *suffixes;
146 BlockArray *suffixDocId;
148 // Total number of texts in the collection
149 unsigned numberOfTexts;
150 // Length of the longest text
153 // Array of document id's in the order of end-markers in BWT
154 static_sequence *Doc;
156 // Text storage for fast extraction
157 TextStorage * textStorage;
159 // Following methods are not part of the public API
160 uchar * BWT(uchar *);
161 void makewavelet(uchar *);
163 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
164 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
165 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *, DocId, DocId) const;
166 ulong SearchLessThan(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
169 * Count end-markers in given interval
171 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
178 ranksp = alphabetrank->rank(0, sp - 1);
180 return alphabetrank->rank(0, ep) - ranksp;
184 * Count end-markers in given interval and
185 * within docIds [min,max]
187 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
190 sp = alphabetrank->rank(0, sp - 1);
191 ep = alphabetrank->rank(0, ep);
195 return Doc->count(sp, ep-1, min, max);
199 * Enumerate all end-markers in given interval
201 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep) const
204 sp = alphabetrank->rank(0, sp - 1);
205 ep = alphabetrank->rank(0, ep);
207 return document_result();
209 return Doc->accessAll(sp, ep-1);
213 * Enumerate end-markers in given interval and
214 * within docIds [min,max]
216 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
219 sp = alphabetrank->rank(0, sp - 1);
220 ep = alphabetrank->rank(0, ep);
222 return document_result();
224 return Doc->access(sp, ep-1, min, max);
226 }; // class TCImplementation