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"
28 // Include from XMLTree/libcds
29 #include <basics.h> // Defines W == 32
30 #include <static_bitsequence.h>
31 #include <alphabet_mapper.h>
32 #include <static_sequence.h>
34 // 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 TCImplementation : public SXSI::TextCollection {
55 TCImplementation(uchar *, ulong, unsigned, unsigned, ulong);
58 bool EmptyText(DocId) const;
59 uchar* GetText(DocId) const;
61 * Next method is not supported:
62 * Supporting GetText for some substring [i,j]
63 * would require more space.
65 // uchar* GetText(DocId, TextPosition, TextPosition) const;
67 bool IsPrefix(uchar const *) const;
68 bool IsSuffix(uchar const *) const;
69 bool IsEqual(uchar const *) const;
70 bool IsContains(uchar const *) const;
71 bool IsLessThan(uchar const *) const;
73 bool IsPrefix(uchar const *, DocId, DocId) const;
74 bool IsSuffix(uchar const *, DocId, DocId) const;
75 bool IsEqual(uchar const *, DocId, DocId) const;
76 bool IsContains(uchar const *, DocId, DocId) const;
77 bool IsLessThan(uchar const *, DocId, DocId) const;
79 ulong Count(uchar const *) const;
80 unsigned CountPrefix(uchar const *) const;
81 unsigned CountSuffix(uchar const *) const;
82 unsigned CountEqual(uchar const *) const;
83 unsigned CountContains(uchar const *) const;
84 unsigned CountLessThan(const unsigned char*) const;
86 unsigned CountPrefix(uchar const *, DocId, DocId) const;
87 unsigned CountSuffix(uchar const *, DocId, DocId) const;
88 unsigned CountEqual(uchar const *, DocId, DocId) const;
89 unsigned CountContains(uchar const *, DocId, DocId) const;
90 unsigned CountLessThan(uchar const *, DocId, DocId) const;
92 // Definition of 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 document_result Prefix(uchar const *, DocId, DocId) const;
100 document_result Suffix(uchar const *, DocId, DocId) const;
101 document_result Equal(uchar const *, DocId, DocId) const;
102 document_result Contains(uchar const *, DocId, DocId) const;
103 document_result LessThan(uchar const *, DocId, DocId) const;
105 // Definition of full_result is inherited from SXSI::TextCollection.
106 full_result FullContains(uchar const *) const;
107 full_result FullContains(uchar const *, DocId, DocId) const;
109 // Index from/to disk
110 TCImplementation(FILE *, unsigned);
111 void Save(FILE *) const;
114 static const uchar versionFlag;
118 TextPosition bwtEndPos;
119 static_sequence * alphabetrank;
121 // Sample structures for texts longer than samplerate
122 BSGAP *sampled; // FIXME Replace with RRR02
123 BlockArray *suffixes;
124 BlockArray *suffixDocId;
126 // Total number of texts in the collection
127 unsigned numberOfTexts;
128 // Total number of texts including empty texts
129 unsigned numberOfAllTexts;
130 // Length of the longest text
133 // Array of document id's in the order of end-markers in BWT
134 // Access by endmarkerDocId[rank_$(L, p) - 1].
135 BlockArray *endmarkerDocId;
137 // FIXME Replace with a more succinct data structure
138 // Note: Empty texts are already handled inside XMLTree class.
139 BSGAP *emptyTextRank; // FIXME Remove
141 // Following are not part of the public API
142 uchar * BWT(uchar *);
143 void makewavelet(uchar *);
145 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
146 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
147 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *, DocId, DocId) const;
148 ulong SearchLessThan(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
151 * Count end-markers in given interval
153 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
160 ranksp = alphabetrank->rank(0, sp - 1);
162 return alphabetrank->rank(0, ep) - ranksp;
164 unsigned CountEndmarkers(TextPosition, TextPosition, DocId, DocId) const;
165 }; // class TCImplementation