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 *****************************************************************************/
25 // Include from XMLTree/libcds
27 #include <static_bitsequence.h>
28 #include <alphabet_mapper.h>
29 #include <static_sequence.h>
34 #include "TextCollection.h"
35 #include "BlockArray.h"
36 #include "RLWaveletTree.h"
39 * Implementation of the TextCollection interface
41 * Implementation notes:
43 * Dynamic construction (via InsertText()) uses dynamic (balanced) wavelet tree
44 * which requires O(n log \sigma) bits of memory. If we know the distribution
45 * of characters beforehand, space can easily be made O(nH_0).
47 * The method MakeStatic() constructs a Succinct Suffix Array with a huffman
48 * shaped wavelet tree. This structure achieves only zero'th order entropy. (FIXME)
50 * Query methods Prefix, Suffix, Equal, Contains and LessThan can be used
51 * with any FM-index variant supporting LF-mapping.
53 class CSA : public SXSI::TextCollection {
56 * Constructor with (default) samplerate
58 explicit CSA(unsigned);
61 * Following functions implement the interface described in TextCollection.h.
62 * For details/documentation, see TextCollection.h.
64 void InsertText(uchar const *);
66 bool EmptyText(DocId) const;
67 uchar* GetText(DocId) const;
68 // uchar* GetText(DocId, TextPosition, TextPosition) const;
70 bool IsPrefix(uchar const *) const;
71 bool IsSuffix(uchar const *) const;
72 bool IsEqual(uchar const *) const;
73 bool IsContains(uchar const *) const;
74 bool IsLessThan(uchar const *) const;
76 unsigned CountPrefix(uchar const *) const;
77 unsigned CountSuffix(uchar const *) const;
78 unsigned CountEqual(uchar const *) const;
79 unsigned CountContains(uchar const *) const;
80 unsigned CountLessThan(const unsigned char*) const;
82 // document_result is inherited from SXSI::TextCollection.
83 document_result Prefix(uchar const *) const;
84 document_result Suffix(uchar const *) const;
85 document_result Equal(uchar const *) const;
86 document_result Contains(uchar const *) const;
87 document_result LessThan(uchar const *) const;
89 // full_result is inherited from SXSI::TextCollection.
90 full_result FullContains(uchar const *) const;
93 void Load(FILE *, unsigned);
94 void Save(FILE *) const;
116 void deleteEndmarker()
118 delete endmarkerDocId;
128 TCodeEntry() {count=0;bits=0;code=0u;};
132 class THuffAlphabetRank {
133 // using fixed 0...255 alphabet
136 THuffAlphabetRank *left;
137 THuffAlphabetRank *right;
138 TCodeEntry *codetable;
142 THuffAlphabetRank(uchar *, TextPosition, TCodeEntry *, unsigned);
143 THuffAlphabetRank(FILE *);
144 ~THuffAlphabetRank();
147 bool Test(uchar *, TextPosition);
149 inline TextPosition rank(int c, TextPosition i) const { // returns the number of characters c before and including position i
150 THuffAlphabetRank const * temp=this;
151 if (codetable[c].count == 0) return 0;
153 unsigned code = codetable[c].code;
154 while (!temp->leaf) {
155 if ((code & (1u<<level)) == 0) {
156 i = i-temp->bitrank->rank(i);
160 i = temp->bitrank->rank(i)-1;
167 inline bool IsCharAtPos(int c, TextPosition i) const {
168 THuffAlphabetRank const * temp=this;
169 if (codetable[c].count == 0) return false;
171 unsigned code = codetable[c].code;
172 while (!temp->leaf) {
173 if ((code & (1u<<level))==0) {
174 if (temp->bitrank->IsBitSet(i)) return false;
175 i = i-temp->bitrank->rank(i);
179 if (!temp->bitrank->IsBitSet(i)) return false;
180 i = temp->bitrank->rank(i)-1;
187 inline uchar access(TextPosition i) const {
188 THuffAlphabetRank const * temp=this;
189 while (!temp->leaf) {
190 if (temp->bitrank->IsBitSet(i)) {
191 i = temp->bitrank->rank(i)-1;
195 i = i-temp->bitrank->rank(i);
202 inline uchar charAtPos(ulong i, TextPosition *rank) const {
203 THuffAlphabetRank const * temp=this;
204 while (!temp->leaf) {
205 if (temp->bitrank->IsBitSet(i)) {
206 i = temp->bitrank->rank(i)-1;
209 i = i-temp->bitrank->rank(i);
225 void maketable( unsigned code, unsigned bits, TCodeEntry *codetable ) const;
226 static void count_chars(uchar *, TextPosition , TCodeEntry *);
227 static unsigned SetBit(unsigned , unsigned , unsigned );
229 node( unsigned char c = 0, unsigned i = 0 ) {
236 node( node* c0, node *c1 ) {
238 weight = c0->weight + c1->weight;
244 bool operator>( const node &a ) const {
245 return weight > a.weight;
248 static TCodeEntry *makecodetable(uchar *, TextPosition);
251 static const unsigned char print = 1;
252 static const unsigned char report = 1;
253 static const uchar versionFlag;
257 TextPosition bwtEndPos;
258 // THuffAlphabetRank *alphabetrank;
259 // RLWaveletTree *alphabetrank;
260 static_sequence * alphabetrank;
262 BlockArray *suffixes;
263 BlockArray *suffixDocId;
264 BlockArray *positions;
265 TCodeEntry *codetable;
268 // Total number of texts in the collection
269 unsigned numberOfTexts;
270 // Total number of texts including empty texts
271 unsigned numberOfAllTexts;
272 // Length of the longest text
275 // Array of document id's in the order of end-markers in BWT
276 // Access by endmarkerDocId[rank_$(L, p) - 1].
277 BlockArray *endmarkerDocId;
278 // Array of text lengths (in the inserted order)
279 BlockArray *textLength;
280 // Array of text starting positions (in the inserted order)
281 BlockArray *textStartPos;
283 // FIXME Replace with a more succinct data structure
284 std::set<unsigned> emptyTextId;
285 BSGAP *emptyTextRank;
288 uchar * BWT(uchar *);
289 uchar * LoadFromFile(const char *);
290 void SaveToFile(const char *, uchar *);
291 void makewavelet(uchar *);
294 // Following are not part of the public API
295 DocId DocIdAtTextPos(TextPosition) const;
296 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
297 TextPosition Inverse(TextPosition) const;
298 TextPosition LF(uchar c, TextPosition &sp, TextPosition &ep) const;
299 TextPosition Psi(TextPosition) const;
300 uchar * Substring(TextPosition, TextPosition) const;
301 TextPosition Lookup(TextPosition) const;
304 * Count end-markers in given interval
306 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
313 ranksp = alphabetrank->rank(0, sp - 1);
315 return alphabetrank->rank(0, ep) - ranksp;