Jouni's Incremental BWT integrated into TextCollection
[SXSI/TextCollection.git] / TCImplementation.h
1 /******************************************************************************
2  *   Copyright (C) 2006-2008 by Veli Mäkinen and Niko Välimäki                *
3  *                                                                            *
4  *                                                                            *
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.                                      *
9  *                                                                            *
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.                      *
14  *                                                                            *
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  *****************************************************************************/
20
21 #ifndef _TCImplementation_H_
22 #define _TCImplementation_H_
23 #include "BitRank.h"
24 #include "TextCollection.h"
25 #include "BlockArray.h"
26 #include "BSGAP.h"
27
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>
33
34 // Re-define word size to ulong:
35 #undef W
36 #if __WORDSIZE == 64
37 #   define W 64
38 #else
39 #   define W 32
40 #endif
41 #undef bitset
42
43 namespace SXSI 
44 {
45
46 // Un-comment to compare BWT against a BWT generated from class dynFMI:
47 //#define CSA_TEST_BWT
48
49 /**
50  * Implementation of the TextCollection interface
51  *
52  */
53 class TCImplementation : public SXSI::TextCollection {
54 public:
55     TCImplementation(uchar *, ulong, unsigned, unsigned, ulong);
56     ~TCImplementation();
57
58     bool EmptyText(DocId) const;
59     uchar* GetText(DocId) const;
60     /**
61      * Next method is not supported:
62      * Supporting GetText for some substring [i,j]
63      * would require more space.
64      */
65 //    uchar* GetText(DocId, TextPosition, TextPosition) const;
66
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;
72
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;
78
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;
85
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;
91     
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;
98
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;
104
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;
108
109     // Index from/to disk
110     TCImplementation(FILE *, unsigned);
111     void Save(FILE *) const;
112
113 private:
114     static const uchar versionFlag;
115     TextPosition n;
116     unsigned samplerate;
117     unsigned C[256];
118     TextPosition bwtEndPos;
119     static_sequence * alphabetrank;
120
121     // Sample structures for texts longer than samplerate
122     BSGAP *sampled;  // FIXME Replace with RRR02
123     BlockArray *suffixes;
124     BlockArray *suffixDocId;
125
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
131     ulong maxTextLength;
132
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;
136
137     // FIXME Replace with a more succinct data structure
138     // Note: Empty texts are already handled inside XMLTree class.
139     BSGAP *emptyTextRank; // FIXME Remove
140
141     // Following are not part of the public API
142     uchar * BWT(uchar *);
143     void makewavelet(uchar *);
144     void maketables();
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;
149
150     /**
151      * Count end-markers in given interval
152      */
153     inline unsigned CountEndmarkers(TextPosition sp, TextPosition ep) const
154     {
155         if (sp > ep)
156             return 0;
157
158         ulong ranksp = 0;
159         if (sp != 0)
160             ranksp = alphabetrank->rank(0, sp - 1);
161     
162         return alphabetrank->rank(0, ep) - ranksp;
163     }
164     unsigned CountEndmarkers(TextPosition, TextPosition, DocId, DocId) const;
165 }; // class TCImplementation
166
167 } // namespace SXSI
168
169 #endif