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:
48 * Implementation of the TextCollection interface
51 class TCImplementation : public SXSI::TextCollection {
53 TCImplementation(uchar *, ulong, unsigned, unsigned, ulong);
56 bool EmptyText(DocId) const;
57 uchar* GetText(DocId) const;
59 * Next method is not supported:
60 * Supporting GetText for some substring [i,j]
61 * would require more space.
63 // uchar* GetText(DocId, TextPosition, TextPosition) const;
65 bool IsPrefix(uchar const *) const;
66 bool IsSuffix(uchar const *) const;
67 bool IsEqual(uchar const *) const;
68 bool IsContains(uchar const *) const;
69 bool IsLessThan(uchar const *) const;
71 bool IsPrefix(uchar const *, DocId, DocId) const;
72 bool IsSuffix(uchar const *, DocId, DocId) const;
73 bool IsEqual(uchar const *, DocId, DocId) const;
74 bool IsContains(uchar const *, DocId, DocId) const;
75 bool IsLessThan(uchar const *, DocId, DocId) const;
77 ulong Count(uchar const *) const;
78 unsigned CountPrefix(uchar const *) const;
79 unsigned CountSuffix(uchar const *) const;
80 unsigned CountEqual(uchar const *) const;
81 unsigned CountContains(uchar const *) const;
82 unsigned CountLessThan(const unsigned char*) const;
84 unsigned CountPrefix(uchar const *, DocId, DocId) const;
85 unsigned CountSuffix(uchar const *, DocId, DocId) const;
86 unsigned CountEqual(uchar const *, DocId, DocId) const;
87 unsigned CountContains(uchar const *, DocId, DocId) const;
88 unsigned CountLessThan(uchar const *, DocId, DocId) const;
90 // Definition of document_result is inherited from SXSI::TextCollection.
91 document_result Prefix(uchar const *) const;
92 document_result Suffix(uchar const *) const;
93 document_result Equal(uchar const *) const;
94 document_result Contains(uchar const *) const;
95 document_result LessThan(uchar const *) const;
97 document_result Prefix(uchar const *, DocId, DocId) const;
98 document_result Suffix(uchar const *, DocId, DocId) const;
99 document_result Equal(uchar const *, DocId, DocId) const;
100 document_result Contains(uchar const *, DocId, DocId) const;
101 document_result LessThan(uchar const *, DocId, DocId) const;
103 // Definition of full_result is inherited from SXSI::TextCollection.
104 full_result FullContains(uchar const *) const;
105 full_result FullContains(uchar const *, DocId, DocId) const;
107 // Index from/to disk
108 TCImplementation(FILE *, unsigned);
109 void Save(FILE *) const;
112 static const uchar versionFlag;
116 TextPosition bwtEndPos;
117 static_sequence * alphabetrank;
119 // Sample structures for texts longer than samplerate
120 static_bitsequence * sampled; // FIXME Replace with RRR02
121 BlockArray *suffixes;
122 BlockArray *suffixDocId;
124 // Total number of texts in the collection
125 unsigned numberOfTexts;
126 // Length of the longest text
129 // Array of document id's in the order of end-markers in BWT
130 static_sequence *Doc;
132 // Following are not part of the public API
133 uchar * BWT(uchar *);
134 void makewavelet(uchar *);
136 DocId DocIdAtTextPos(BlockArray*, TextPosition) const;
137 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
138 ulong Search(uchar const *, TextPosition, TextPosition *, TextPosition *, DocId, DocId) const;
139 ulong SearchLessThan(uchar const *, TextPosition, TextPosition *, TextPosition *) const;
142 * Count end-markers in given interval
144 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
151 ranksp = alphabetrank->rank(0, sp - 1);
153 return alphabetrank->rank(0, ep) - ranksp;
157 * Count end-markers in given interval and
158 * within docIds [min,max]
160 inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
163 sp = alphabetrank->rank(0, sp - 1);
164 ep = alphabetrank->rank(0, ep);
168 return Doc->count(sp, ep-1, min, max);
172 * Enumerate all end-markers in given interval
174 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep) const
177 sp = alphabetrank->rank(0, sp - 1);
178 ep = alphabetrank->rank(0, ep);
180 return document_result();
182 return Doc->accessAll(sp, ep-1);
186 * Enumerate end-markers in given interval and
187 * within docIds [min,max]
189 inline document_result EnumerateEndmarkers(TextPosition sp, TextPosition ep, DocId min, DocId max) const
192 sp = alphabetrank->rank(0, sp - 1);
193 ep = alphabetrank->rank(0, ep);
195 return document_result();
197 return Doc->access(sp, ep-1, min, max);
199 }; // class TCImplementation