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:
44 #include "TextStorage.h"
53 * Implementation of the TextCollection interface
56 class TCImplementation : public SXSI::TextCollection {
58 TCImplementation(uchar *, ulong, unsigned, unsigned, ulong, ulong, char);
61 bool EmptyText(DocId) const;
64 * Extracting one text.
66 * Call DeleteText() for each pointer returned by GetText()
67 * to avoid possible memory leaks.
69 uchar * GetText(DocId) const;
70 void DeleteText(uchar *text) const
71 { textStorage->DeleteText(text); }
74 * Returns a pointer to the beginning of texts i, i+1, ..., j.
75 * Texts are separated by a '\0' byte.
77 * Call DeleteText() for each pointer returned by GetText()
78 * to avoid possible memory leaks.
80 uchar * GetText(DocId i, DocId j) const
81 { return textStorage->GetText(i, j); }
84 * Returns a substring of given text ID.
86 * FIXME This may be reimplemented via TextStorage.
88 // uchar* GetText(DocId, TextPosition, TextPosition) const;
90 bool IsPrefix(uchar const *) const;
91 bool IsSuffix(uchar const *) const;
92 bool IsEqual(uchar const *) const;
93 bool IsContains(uchar const *) const;
94 bool IsLessThan(uchar const *) const;
96 bool IsPrefix(uchar const *, DocId, DocId) const;
97 bool IsSuffix(uchar const *, DocId, DocId) const;
98 bool IsEqual(uchar const *, DocId, DocId) const;
99 bool IsContains(uchar const *, DocId, DocId) const;
100 bool IsLessThan(uchar const *, DocId, DocId) const;
102 ulong Count(uchar const *) const;
103 unsigned CountPrefix(uchar const *) const;
104 unsigned CountSuffix(uchar const *) const;
105 unsigned CountEqual(uchar const *) const;
106 unsigned CountContains(uchar const *) const;
107 unsigned CountLessThan(const unsigned char*) const;
109 unsigned CountPrefix(uchar const *, DocId, DocId) const;
110 unsigned CountSuffix(uchar const *, DocId, DocId) const;
111 unsigned CountEqual(uchar const *, DocId, DocId) const;
112 unsigned CountContains(uchar const *, DocId, DocId) const;
113 unsigned CountLessThan(uchar const *, DocId, DocId) const;
115 // Definition of document_result is inherited from SXSI::TextCollection.
116 document_result Prefix(uchar const *) const;
117 document_result Suffix(uchar const *) const;
118 document_result Equal(uchar const *) const;
119 document_result Contains(uchar const *) const;
120 document_result LessThan(uchar const *) const;
121 document_result KMismaches(uchar const *, unsigned) const;
122 document_result KErrors(uchar const *, unsigned) const;
124 document_result Prefix(uchar const *, DocId, DocId) const;
125 document_result Suffix(uchar const *, DocId, DocId) const;
126 document_result Equal(uchar const *, DocId, DocId) const;
127 document_result Contains(uchar const *, DocId, DocId) const;
128 document_result LessThan(uchar const *, DocId, DocId) const;
130 // Definition of full_result is inherited from SXSI::TextCollection.
131 full_result FullContains(uchar const *) const;
132 full_result FullContains(uchar const *, DocId, DocId) const;
133 full_result FullKMismatches(uchar const *, unsigned) const;
134 full_result FullKErrors(uchar const *, unsigned) const;
136 // Index from/to disk
137 TCImplementation(FILE *, unsigned);
138 void Save(FILE *) const;
141 typedef std::vector<std::pair<ulong, ulong> > suffix_range_vector;
143 static const uchar versionFlag;
147 TextPosition bwtEndPos;
148 static_sequence * alphabetrank;
150 // Sample structures for texts longer than samplerate
151 static_bitsequence * sampled;
152 BlockArray *suffixes;
153 BlockArray *suffixDocId;
155 // Total number of texts in the collection
156 unsigned numberOfTexts;
157 // Length of the longest text
160 // Array of document id's in the order of end-markers in BWT
161 // static_sequence *Doc;
164 // Text storage for fast extraction
165 TextStorage * textStorage;
167 // Following methods are not part of the public API
168 uchar * BWT(uchar *);
169 void makewavelet(uchar *);
170 void maketables(ulong, char);
171 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
172 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
173 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *, DocId, DocId) const;
174 ulong SearchLessThan(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
175 ulong searchPrefix(uchar const *pattern, ulong i, ulong *sp, ulong *ep) const;
176 ulong kmismatches(suffix_range_vector &, uchar const *, ulong, ulong, ulong, unsigned) const;
177 ulong kerrors(suffix_range_vector &, uchar const *, ulong, ulong, ulong, unsigned, ulong const *, ulong) const;
179 * Count end-markers in given interval
181 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
188 ranksp = alphabetrank->rank(0, sp - 1);
190 return alphabetrank->rank(0, ep) - ranksp;
194 * Count end-markers in given interval and
195 * within docIds [min,max]
197 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
200 sp = alphabetrank->rank(0, sp - 1);
201 ep = alphabetrank->rank(0, ep);
205 return Doc->count(sp, ep-1, min, max);
209 * Enumerate all end-markers in given interval
211 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep) const
214 sp = alphabetrank->rank(0, sp - 1);
215 ep = alphabetrank->rank(0, ep);
217 return document_result();
219 return Doc->accessAll(sp, ep-1);
223 * Enumerate end-markers in given interval and
224 * within docIds [min,max]
226 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
229 sp = alphabetrank->rank(0, sp - 1);
230 ep = alphabetrank->rank(0, ep);
232 return document_result();
234 return Doc->access(sp, ep-1, min, max);
238 * Enumerate documents in given interval [sp, ep]
240 inline void EnumerateDocuments(std::set<DocId> &resultSet, TextPosition sp, TextPosition ep) const
242 // We want unique document indentifiers, using std::set to collect them
243 // FIXME use unordered_set?
244 uint tmp_rank_c = 0; // Cache rank value of c.
245 for (; sp <= ep; ++sp)
248 uchar c = alphabetrank->access(i, tmp_rank_c);
249 while (c != '\0' && !sampled->access(i))
251 i = C[c]+tmp_rank_c-1; //alphabetrank->rank(c,i)-1;
252 c = alphabetrank->access(i, tmp_rank_c);
256 // Rank among the end-markers in BWT
257 unsigned endmarkerRank = tmp_rank_c-1; //alphabetrank->rank(0, i) - 1;
258 resultSet.insert(Doc->access(endmarkerRank));
262 DocId di = (*suffixDocId)[sampled->rank1(i)-1];
263 assert((unsigned)di < numberOfTexts);
264 resultSet.insert(di);
270 * Enumerate documents in given interval [sp, ep]
271 * and within [begin, end]
273 inline void EnumerateDocuments(std::set<DocId> &resultSet, TextPosition sp, TextPosition ep, DocId begin, DocId end) const
275 // We want unique document indentifiers, using std::set to collect them
276 uint tmp_rank_c = 0; // Cache rank value of c.
277 for (; sp <= ep; ++sp)
280 uchar c = alphabetrank->access(i, tmp_rank_c);
281 while (c != '\0' && !sampled->access(i))
283 i = C[c]+tmp_rank_c-1; //alphabetrank->rank(c,i)-1;
284 c = alphabetrank->access(i, tmp_rank_c);
288 // Rank among the end-markers in BWT
289 unsigned endmarkerRank = tmp_rank_c-1; //alphabetrank->rank(0, i) - 1;
290 DocId docId = Doc->access(endmarkerRank);
291 if (docId >= begin && docId <= end)
292 resultSet.insert(docId);
296 DocId docId = (*suffixDocId)[sampled->rank1(i)-1];
297 assert((unsigned)docId < numberOfTexts);
298 if (docId >= begin && docId <= end)
299 resultSet.insert(docId);
305 * Enumerate document+position pairs (full_result) of
306 * each suffix in given interval.
308 inline void EnumeratePositions(full_result &result, TextPosition sp, TextPosition ep) const
310 uint tmp_rank_c = 0; // Cache rank value of c.
311 for (; sp <= ep; ++sp)
314 TextPosition dist = 0;
315 uchar c = alphabetrank->access(i, tmp_rank_c);
316 while (c != '\0' && !sampled->access(i))
318 i = C[c]+tmp_rank_c-1; //alphabetrank->rank(c,i)-1;
319 c = alphabetrank->access(i, tmp_rank_c);
324 // Rank among the end-markers in BWT
325 unsigned endmarkerRank = tmp_rank_c-1; //alphabetrank->rank(0, i) - 1;
326 DocId docId = Doc->access(endmarkerRank);
327 result.push_back(make_pair(docId, dist));
331 TextPosition textPos = (*suffixes)[sampled->rank1(i)-1] + dist;
332 DocId docId = (*suffixDocId)[sampled->rank1(i)-1];
334 result.push_back(make_pair(docId, textPos));
340 * Enumerate document+position pairs (full_result) of
341 * each suffix in given interval and within [begin, end].
343 inline void EnumeratePositions(full_result &result, TextPosition sp, TextPosition ep, DocId begin, DocId end) const
345 uint tmp_rank_c = 0; // Cache rank value of c.
346 for (; sp <= ep; ++sp)
349 TextPosition dist = 0;
350 uchar c = alphabetrank->access(i, tmp_rank_c);
351 while (c != '\0' && !sampled->access(i))
353 i = C[c]+tmp_rank_c-1; //alphabetrank->rank(c,i)-1;
354 c = alphabetrank->access(i, tmp_rank_c);
359 // Rank among the end-markers in BWT
360 unsigned endmarkerRank = tmp_rank_c-1; //alphabetrank->rank(0, i) - 1;
361 DocId docId = Doc->access(endmarkerRank);
362 if (docId >= begin && docId <= end)
363 result.push_back(make_pair(docId, dist));
367 TextPosition textPos = (*suffixes)[sampled->rank1(i)-1] + dist;
368 DocId docId = (*suffixDocId)[sampled->rank1(i)-1];
370 if (docId >= begin && docId <= end)
371 result.push_back(make_pair(docId, textPos));
376 }; // class TCImplementation