Buffer size can be defined
[SXSI/TextCollection.git] / TextCollectionBuilder.cpp
1 #include "incbwt/rlcsa_builder.h"
2 #include "TextCollectionBuilder.h"
3
4 // Un-comment next line to run a comparison of resulting BWT
5 //#define TCB_TEST_BWT
6
7 #ifdef TCB_TEST_BWT
8 #include "dynFMI.h"
9 #endif
10
11 #include "TCImplementation.h"
12
13 namespace SXSI
14 {
15
16 struct TCBuilderRep
17 {
18     unsigned samplerate;
19     CSA::RLCSABuilder * sa;
20
21     ulong n;
22     // Total number of texts in the collection
23     unsigned numberOfTexts;
24     // Length of the longest text
25     ulong maxTextLength;
26     ulong numberOfSamples;
27
28 #ifdef TCB_TEST_BWT
29     DynFMI *dynFMI;
30 #endif
31 };
32
33 /**
34  * Init text collection
35  *
36  */
37 TextCollectionBuilder::TextCollectionBuilder(unsigned samplerate, ulong estimatedInputLength)
38     : p_(new struct TCBuilderRep())
39 {
40     p_->n = 0;
41     p_->samplerate = samplerate;
42     p_->numberOfTexts = 0;
43     p_->numberOfSamples = 0;
44     
45     // Current params: 8 bytes, no samples, buffer size n/10 bytes.
46     p_->sa = new CSA::RLCSABuilder(8, 0, estimatedInputLength/10);
47     assert(p_->sa->isOk());
48
49 #ifdef TCB_TEST_BWT
50     uchar temp[256];
51     for (unsigned i = 0; i < 255; ++i)
52         temp[i] = i+1;
53     temp[255] = 0;
54     p_->dynFMI = new DynFMI(temp, 1, 255, false);
55 #endif
56 }
57
58 TextCollectionBuilder::~TextCollectionBuilder()
59 {
60 #ifdef TCB_TEST_BWT
61     delete p_->dynFMI;
62 #endif
63
64     delete p_->sa;
65     delete p_;
66 }
67
68 void TextCollectionBuilder::InsertText(uchar const * text)
69 {
70     TextCollection::TextPosition m = std::strlen((char *)text) + 1;
71     if (m > p_->maxTextLength)
72         p_->maxTextLength = m; // Store length of the longest text seen so far.
73
74     if (m > 1)
75     {
76         p_->n += m;
77         p_->numberOfTexts ++;
78         p_->numberOfSamples += (m-1)/p_->samplerate;
79
80 #ifdef TCB_TEST_BWT
81         p_->dynFMI->addText(text, m);
82 #endif
83         p_->sa->insertSequence((char*)text, m-1, 0);
84         assert(p_->sa->isOk());
85     }
86     else
87     {
88         // FIXME indexing empty texts
89         std::cerr << "TextCollectionBuilder::InsertText() error: can not index empty texts!" << std::endl;
90         exit(1);
91     }
92 }
93
94
95 TextCollection * TextCollectionBuilder::InitTextCollection()
96 {
97     uchar * bwt = 0;
98     CSA::usint length = 0;
99     if (p_->numberOfTexts == 0)
100     {
101         p_->numberOfTexts ++; // Add one empty text
102         bwt = new uchar[2];
103         bwt[0] = '\0';
104         bwt[1] = '\0';
105         length = 1;
106         p_->maxTextLength = 1;
107     }
108     else
109     {
110         bwt = (uchar *)p_->sa->getBWT(length);
111         delete p_->sa;
112         p_->sa = 0;
113
114         assert(length == p_->n);
115
116 #ifdef TCB_TEST_BWT
117         { 
118             uchar *bwtTest = p_->dynFMI->getBWT();
119             printf("123456789012345678901234567890123456789\n");
120             for (ulong i = 0; i < p_->n && i < 100; i ++)
121                 if (bwt[i] < 50)
122                     printf("%d", (int)bwt[i]);
123                 else
124                     printf("%c", bwt[i]);
125             printf("\n");
126             for (ulong i = 0; i < p_->n && i < 100; i ++)
127                 if (bwtTest[i] < 50)
128                     printf("%d", (int)bwtTest[i]);
129                 else
130                     printf("%c", bwtTest[i]);
131             printf("\n");
132             
133             // Sanity check
134             assert(p_->numberOfTexts == p_->dynFMI->getCollectionSize());    
135             
136             delete p_->dynFMI;
137             p_->dynFMI = 0;
138             for (ulong i = 0; i < p_->n; ++i)
139                 if (bwt[i] != bwtTest[i])
140                 {
141                     std::cout << "i = " << i << ", bwt = " << (unsigned)bwt[i] << ", " 
142                               << (unsigned)bwtTest[i] << std::endl;
143                     assert(0);
144                 }
145             delete [] bwtTest;
146         }
147 #endif // TCB_TEST_BWT
148     }
149
150     TextCollection *result = new TCImplementation(bwt, (ulong)length, 
151                       p_->samplerate, p_->numberOfTexts, p_->maxTextLength, p_->numberOfSamples);
152     return result;
153 }
154
155
156 } // namespace SXSI