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"
31 // Include from XMLTree/libcds
32 #include <basics.h> // Defines W == 32
33 #include <static_bitsequence.h>
34 #include <alphabet_mapper.h>
35 #include <static_sequence.h>
37 // Re-define word size to ulong:
46 // Un-comment to compare BWT against a BWT generated from class dynFMI:
47 //#define CSA_TEST_BWT
50 * Implementation of the TextCollection interface
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;
69 * Next method is not supported:
70 * Supporting GetText for some substring [i,j]
71 * would require more space.
73 // uchar* GetText(DocId, TextPosition, TextPosition) const;
75 bool IsPrefix(uchar const *) const;
76 bool IsSuffix(uchar const *) const;
77 bool IsEqual(uchar const *) const;
78 bool IsContains(uchar const *) const;
79 bool IsLessThan(uchar const *) const;
81 ulong Count(uchar const *) const;
82 unsigned CountPrefix(uchar const *) const;
83 unsigned CountSuffix(uchar const *) const;
84 unsigned CountEqual(uchar const *) const;
85 unsigned CountContains(uchar const *) const;
86 unsigned CountLessThan(const unsigned char*) const;
88 // document_result is inherited from SXSI::TextCollection.
89 document_result Prefix(uchar const *) const;
90 document_result Suffix(uchar const *) const;
91 document_result Equal(uchar const *) const;
92 document_result Contains(uchar const *) const;
93 document_result LessThan(uchar const *) const;
95 // full_result is inherited from SXSI::TextCollection.
96 full_result FullContains(uchar const *) const;
99 void Load(FILE *, unsigned);
100 void Save(FILE *) const;
120 void deleteEndmarker()
122 delete endmarkerDocId;
133 TCodeEntry() {count=0;bits=0;code=0u;};
138 class THuffAlphabetRank {
139 // using fixed 0...255 alphabet
142 THuffAlphabetRank *left;
143 THuffAlphabetRank *right;
144 TCodeEntry *codetable;
148 THuffAlphabetRank(uchar *, TextPosition, TCodeEntry *, unsigned);
149 THuffAlphabetRank(FILE *);
150 ~THuffAlphabetRank();
153 bool Test(uchar *, TextPosition);
155 inline TextPosition rank(int c, TextPosition i) const { // returns the number of characters c before and including position i
156 THuffAlphabetRank const * temp=this;
157 if (codetable[c].count == 0) return 0;
159 unsigned code = codetable[c].code;
160 while (!temp->leaf) {
161 if ((code & (1u<<level)) == 0) {
162 i = i-temp->bitrank->rank(i);
166 i = temp->bitrank->rank(i)-1;
173 inline bool IsCharAtPos(int c, TextPosition i) const {
174 THuffAlphabetRank const * temp=this;
175 if (codetable[c].count == 0) return false;
177 unsigned code = codetable[c].code;
178 while (!temp->leaf) {
179 if ((code & (1u<<level))==0) {
180 if (temp->bitrank->IsBitSet(i)) return false;
181 i = i-temp->bitrank->rank(i);
185 if (!temp->bitrank->IsBitSet(i)) return false;
186 i = temp->bitrank->rank(i)-1;
193 inline uchar access(TextPosition i) const {
194 THuffAlphabetRank const * temp=this;
195 while (!temp->leaf) {
196 if (temp->bitrank->IsBitSet(i)) {
197 i = temp->bitrank->rank(i)-1;
201 i = i-temp->bitrank->rank(i);
208 inline uchar charAtPos(ulong i, TextPosition *rank) const {
209 THuffAlphabetRank const * temp=this;
210 while (!temp->leaf) {
211 if (temp->bitrank->IsBitSet(i)) {
212 i = temp->bitrank->rank(i)-1;
215 i = i-temp->bitrank->rank(i);
232 void maketable( unsigned code, unsigned bits, TCodeEntry *codetable ) const;
233 static void count_chars(uchar *, TextPosition , TCodeEntry *);
234 static unsigned SetBit(unsigned , unsigned , unsigned );
236 node( unsigned char c = 0, unsigned i = 0 ) {
243 node( node* c0, node *c1 ) {
245 weight = c0->weight + c1->weight;
251 bool operator>( const node &a ) const {
252 return weight > a.weight;
255 static TCodeEntry *makecodetable(uchar *, TextPosition);
258 static const uchar versionFlag;
262 TextPosition bwtEndPos;
263 // THuffAlphabetRank *alphabetrank;
264 // RLWaveletTree *alphabetrank;
265 static_sequence * alphabetrank;
270 TCodeEntry *codetable;
272 // Sample structures for texts longer than samplerate
274 BlockArray *suffixes;
275 BlockArray *suffixDocId;
277 // Total number of texts in the collection
278 unsigned numberOfTexts;
279 // Total number of texts including empty texts
280 unsigned numberOfAllTexts;
281 // Length of the longest text
284 // Array of document id's in the order of end-markers in BWT
285 // Access by endmarkerDocId[rank_$(L, p) - 1].
286 BlockArray *endmarkerDocId;
288 // FIXME Replace with a more succinct data structure
289 // Note: Empty texts are already handled inside XMLTree class.
290 std::set<unsigned> emptyTextId;
291 BSGAP *emptyTextRank;
293 // FIXME A better solution?
296 // Following are not part of the public API
297 uchar * BWT(uchar *);
298 void makewavelet(uchar *);
300 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
301 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
302 // TextPosition Inverse(TextPosition) const;
303 // TextPosition LF(uchar c, TextPosition &sp, TextPosition &ep) const;
304 // TextPosition Psi(TextPosition) const;
305 // uchar * Substring(TextPosition, TextPosition) const;
306 // TextPosition Lookup(TextPosition) const;
309 * Count end-markers in given interval
311 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
318 ranksp = alphabetrank->rank(0, sp - 1);
320 return alphabetrank->rank(0, ep) - ranksp;