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 "TextCollection.h"
26 #include "BlockArray.h"
27 #include "RLWaveletTree.h"
28 #include "StringIterator.h"
32 // Include from XMLTree/libcds
33 #include <basics.h> // Defines W == 32
34 #include <static_bitsequence.h>
35 #include <alphabet_mapper.h>
36 #include <static_sequence.h>
38 // Re-define word size to ulong:
50 // Un-comment to compare BWT against a BWT generated from class dynFMI:
51 //#define CSA_TEST_BWT
54 * Implementation of the TextCollection interface
57 class CSA : public SXSI::TextCollection {
60 * Constructor with (default) samplerate
62 explicit CSA(unsigned);
65 * Following functions implement the interface described in TextCollection.h.
66 * For details/documentation, see TextCollection.h.
68 void InsertText(uchar const *);
70 bool EmptyText(DocId) const;
71 uchar* GetText(DocId) const;
73 * Next method is not supported:
74 * Supporting GetText for some substring [i,j]
75 * would require more space.
77 // uchar* GetText(DocId, TextPosition, TextPosition) const;
79 bool IsPrefix(uchar const *) const;
80 bool IsSuffix(uchar const *) const;
81 bool IsEqual(uchar const *) const;
82 bool IsContains(uchar const *) const;
83 bool IsLessThan(uchar const *) const;
85 ulong Count(uchar const *) const;
86 unsigned CountPrefix(uchar const *) const;
87 unsigned CountSuffix(uchar const *) const;
88 unsigned CountEqual(uchar const *) const;
89 unsigned CountContains(uchar const *) const;
90 unsigned CountLessThan(const unsigned char*) const;
92 // document_result is inherited from SXSI::TextCollection.
93 document_result Prefix(uchar const *) const;
94 document_result Suffix(uchar const *) const;
95 document_result Equal(uchar const *) const;
96 document_result Contains(uchar const *) const;
97 document_result LessThan(uchar const *) const;
99 // full_result is inherited from SXSI::TextCollection.
100 full_result FullContains(uchar const *) const;
102 // Index from/to disk
103 void Load(FILE *, unsigned);
104 void Save(FILE *) const;
124 void deleteEndmarker()
126 delete endmarkerDocId;
137 TCodeEntry() {count=0;bits=0;code=0u;};
142 class THuffAlphabetRank {
143 // using fixed 0...255 alphabet
146 THuffAlphabetRank *left;
147 THuffAlphabetRank *right;
148 TCodeEntry *codetable;
152 THuffAlphabetRank(uchar *, TextPosition, TCodeEntry *, unsigned);
153 THuffAlphabetRank(FILE *);
154 ~THuffAlphabetRank();
157 bool Test(uchar *, TextPosition);
159 inline TextPosition rank(int c, TextPosition i) const { // returns the number of characters c before and including position i
160 THuffAlphabetRank const * temp=this;
161 if (codetable[c].count == 0) return 0;
163 unsigned code = codetable[c].code;
164 while (!temp->leaf) {
165 if ((code & (1u<<level)) == 0) {
166 i = i-temp->bitrank->rank(i);
170 i = temp->bitrank->rank(i)-1;
177 inline bool IsCharAtPos(int c, TextPosition i) const {
178 THuffAlphabetRank const * temp=this;
179 if (codetable[c].count == 0) return false;
181 unsigned code = codetable[c].code;
182 while (!temp->leaf) {
183 if ((code & (1u<<level))==0) {
184 if (temp->bitrank->IsBitSet(i)) return false;
185 i = i-temp->bitrank->rank(i);
189 if (!temp->bitrank->IsBitSet(i)) return false;
190 i = temp->bitrank->rank(i)-1;
197 inline uchar access(TextPosition i) const {
198 THuffAlphabetRank const * temp=this;
199 while (!temp->leaf) {
200 if (temp->bitrank->IsBitSet(i)) {
201 i = temp->bitrank->rank(i)-1;
205 i = i-temp->bitrank->rank(i);
212 inline uchar charAtPos(ulong i, TextPosition *rank) const {
213 THuffAlphabetRank const * temp=this;
214 while (!temp->leaf) {
215 if (temp->bitrank->IsBitSet(i)) {
216 i = temp->bitrank->rank(i)-1;
219 i = i-temp->bitrank->rank(i);
236 void maketable( unsigned code, unsigned bits, TCodeEntry *codetable ) const;
237 static void count_chars(uchar *, TextPosition , TCodeEntry *);
238 static unsigned SetBit(unsigned , unsigned , unsigned );
240 node( unsigned char c = 0, unsigned i = 0 ) {
247 node( node* c0, node *c1 ) {
249 weight = c0->weight + c1->weight;
255 bool operator>( const node &a ) const {
256 return weight > a.weight;
259 static TCodeEntry *makecodetable(uchar *, TextPosition);
262 static const uchar versionFlag;
266 TextPosition bwtEndPos;
267 // THuffAlphabetRank *alphabetrank;
268 // RLWaveletTree *alphabetrank;
269 static_sequence * alphabetrank;
274 TCodeEntry *codetable;
276 // Sample structures for texts longer than samplerate
278 BlockArray *suffixes;
279 BlockArray *suffixDocId;
281 // Total number of texts in the collection
282 unsigned numberOfTexts;
283 // Total number of texts including empty texts
284 unsigned numberOfAllTexts;
285 // Length of the longest text
288 // Array of document id's in the order of end-markers in BWT
289 // Access by endmarkerDocId[rank_$(L, p) - 1].
290 BlockArray *endmarkerDocId;
292 // FIXME Replace with a more succinct data structure
293 // Note: Empty texts are already handled inside XMLTree class.
294 std::set<unsigned> emptyTextId;
295 BSGAP *emptyTextRank;
297 // FIXME A better solution?
300 // Following are not part of the public API
301 uchar * BWT(uchar *);
302 void makewavelet(uchar *);
304 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
305 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
306 ulong SearchLessThan(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
307 // TextPosition Inverse(TextPosition) const;
308 // TextPosition LF(uchar c, TextPosition &sp, TextPosition &ep) const;
309 // TextPosition Psi(TextPosition) const;
310 // uchar * Substring(TextPosition, TextPosition) const;
311 // TextPosition Lookup(TextPosition) const;
314 * Count end-markers in given interval
316 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
323 ranksp = alphabetrank->rank(0, sp - 1);
325 return alphabetrank->rank(0, ep) - ranksp;