X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=TCImplementation.cpp;h=130b1a2a2c74508dbff30dff32065bae22cb66a6;hb=9042e7b1b181a50b15e8ec1b79d5a5c2f913bfff;hp=c512306c6aea5175c9b1cbd253c46f9e696fa8e0;hpb=7cdaf25b1e5f1890e359b3ad37ab7ec2c9e30d5a;p=SXSI%2FTextCollection.git diff --git a/TCImplementation.cpp b/TCImplementation.cpp index c512306..130b1a2 100644 --- a/TCImplementation.cpp +++ b/TCImplementation.cpp @@ -36,19 +36,20 @@ using std::vector; using std::pair; using std::make_pair; using std::map; - +using std::string; namespace SXSI { // Save file version info -const uchar TCImplementation::versionFlag = 6; +const uchar TCImplementation::versionFlag = 8; /** * Constructor inits an empty dynamic FM-index. * Samplerate defaults to TEXTCOLLECTION_DEFAULT_SAMPLERATE. */ TCImplementation::TCImplementation(uchar * bwt, ulong length, unsigned samplerate_, - unsigned numberOfTexts_, ulong maxTextLength_, ulong numberOfSamples_, char tsType) + unsigned numberOfTexts_, ulong maxTextLength_, ulong numberOfSamples_, + CSA::DeltaVector & notIndexed, const string & niText, char tsType) : n(length), samplerate(samplerate_), alphabetrank(0), sampled(0), suffixes(0), suffixDocId(0), numberOfTexts(numberOfTexts_), maxTextLength(maxTextLength_), Doc(0) { @@ -56,7 +57,7 @@ TCImplementation::TCImplementation(uchar * bwt, ulong length, unsigned samplerat bwt = 0; // Make sampling tables - maketables(numberOfSamples_, tsType); + maketables(numberOfSamples_, tsType, notIndexed, niText); } bool TCImplementation::EmptyText(DocId k) const @@ -93,8 +94,9 @@ uchar * TCImplementation::GetText(DocId k) const res[i-j-1] = result[j]; return res;*/ } + /* - * Not supported + * Substring queries are supported via the pointer returned by TextStorage::GetText uchar* TCImplementation::GetText(DocId k, TextPosition i, TextPosition j) const { assert(k < (DocId)numberOfTexts); @@ -109,6 +111,8 @@ uchar* TCImplementation::GetText(DocId k, TextPosition i, TextPosition j) const return Substring(i + start, j-i+1); }*/ + + /****************************************************************** * Existential queries */ @@ -554,6 +558,11 @@ TextCollection::document_result TCImplementation::Contains(uchar const * pattern return result; } + +/** + *** +* * FIXME Lessthan or equal + */ TextCollection::document_result TCImplementation::LessThan(uchar const * pattern) const { TextPosition m = strlen((char *)pattern); @@ -763,6 +772,9 @@ TCImplementation::TCImplementation(FILE *file, unsigned samplerate_) : n(0), samplerate(samplerate_), alphabetrank(0), sampled(0), suffixes(0), suffixDocId(0), numberOfTexts(0), maxTextLength(0), Doc(0) { +// Tools::StartTimer(); +// std::cout << std::endl << "Loading..."<< std::endl; + uchar verFlag = 0; if (std::fread(&verFlag, 1, 1, file) != 1) throw std::runtime_error("TCImplementation::Load(): file read error (version flag)."); @@ -784,7 +796,9 @@ TCImplementation::TCImplementation(FILE *file, unsigned samplerate_) if (std::fread(&(this->bwtEndPos), sizeof(TextPosition), 1, file) != 1) throw std::runtime_error("TCImplementation::Load(): file read error (bwt end position)."); +// std::cout << "Loading alphabet rank (" << Tools::GetTime() << " s)." << std::endl; alphabetrank = static_sequence::load(file); +// std::cout << "Loading samples (" << Tools::GetTime() << " s)." << std::endl; sampled = static_bitsequence::load(file); suffixes = new BlockArray(file); suffixDocId = new BlockArray(file); @@ -794,9 +808,13 @@ TCImplementation::TCImplementation(FILE *file, unsigned samplerate_) if (std::fread(&(this->maxTextLength), sizeof(ulong), 1, file) != 1) throw std::runtime_error("TCImplementation::Load(): file read error (maxTextLength)."); - Doc = static_sequence::load(file); +// std::cout << "Loading Doc (" << Tools::GetTime() << " s)." << std::endl; + Doc = new ArrayDoc(file); //static_sequence::load(file); +// std::cout << "Loading text storage (" << Tools::GetTime() << " s)." << std::endl; textStorage = TextStorage::Load(file); +// std::cout << "Loading done(" << Tools::GetTime() << " s)." << std::endl; + // FIXME Construct data structures with new samplerate //maketables(); } @@ -1010,8 +1028,8 @@ void TCImplementation::makewavelet(uchar *bwt) #endif alphabet_mapper * am = new alphabet_mapper_none(); - static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(8); // FIXME samplerate? - wt_coder * wtc = new wt_coder_binary(bwt,n,am); + static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(8); //rrr02(8); // FIXME samplerate? + wt_coder * wtc = new wt_coder_huff(bwt,n,am);//binary(bwt,n,am); // FIXME Huffman shape alphabetrank = new static_sequence_wvtree(bwt,n,wtc,bmb,am); delete bmb; bwt = 0; // already deleted @@ -1022,7 +1040,7 @@ void TCImplementation::makewavelet(uchar *bwt) #endif } -void TCImplementation::maketables(ulong sampleLength, char tsType) +void TCImplementation::maketables(ulong sampleLength, char tsType, CSA::DeltaVector & notIndexed, const string & niText) { // Calculate BWT end-marker position (of last inserted text) { @@ -1049,7 +1067,8 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) // Mapping from end-markers to doc ID's: unsigned logNumberOfTexts = Tools::CeilLog2(numberOfTexts); - uint *endmarkerDocId = new uint[(numberOfTexts * logNumberOfTexts)/(8*sizeof(uint)) + 1]; +// uint *endmarkerDocId = new uint[(numberOfTexts * logNumberOfTexts)/(8*sizeof(uint)) + 1]; + BlockArray *endmarkerDocId = new BlockArray(numberOfTexts, logNumberOfTexts); BlockArray* positions = new BlockArray(sampleLength, Tools::CeilLog2(this->n)); uint *sampledpositions = new uint[n/(sizeof(uint)*8)+1]; @@ -1065,15 +1084,18 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) ulongmax--; uint alphabetrank_i_tmp =0; - TextStorageBuilder tsbuilder(n); - Tools::StartTimer(); + // Text length = n + number of bytes not indexed. + TextStorageBuilder tsbuilder(n + niText.length()); + ulong tsb_i = n + niText.length(); // Iterator from text length to 0. + string::const_reverse_iterator nit_i = niText.rbegin(); // Iterator through non-indexed texts for (ulong i=n-1;iGetPos(i) x=(i==n-1)?0:i+1; uchar c = alphabetrank->access(p, alphabetrank_i_tmp); - tsbuilder[i] = c; + + tsbuilder[--tsb_i] = c; // Build TextStorage if ((posOfSuccEndmarker - i) % samplerate == 0 && c != '\0') { @@ -1084,26 +1106,71 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) if (c == '\0') { - --textId; + unsigned prevTextId = textId; // Cache textId value. + --textId; + /** + * At first c == '\0' it holds that (prevTextId == numberOfTexts), thus, + * we have to search for the first text that is actually *indexed* + * to get correct prevTextId. + */ + if (prevTextId == numberOfTexts) + { + prevTextId = 0; + while (notIndexed.isSet(prevTextId)) + ++ prevTextId; + // Now prevTextId points to the first indexed Doc ID. + } + + /** + * Insert non-indexed texts + */ + while (notIndexed.isSet(textId)) + { + do { + tsbuilder[tsb_i] = *nit_i; + -- tsb_i; + ++ nit_i; + } while (nit_i != niText.rend() && *nit_i != '\0'); + + tsbuilder[tsb_i] = '\0'; + + if (textId == 0) + break; + --textId; + } // Record the order of end-markers in BWT: ulong endmarkerRank = alphabetrank_i_tmp - 1; - set_field(endmarkerDocId, logNumberOfTexts, endmarkerRank, (textId + 1) % numberOfTexts); + //set_field(endmarkerDocId, logNumberOfTexts, endmarkerRank, (textId + 1) % numberOfTexts); + (*endmarkerDocId)[endmarkerRank] = prevTextId % numberOfTexts; // Store text length and text start position: if (textId < (DocId)numberOfTexts - 1) { // (*textStartPos)[textId + 1] = x; // x-1 is text position of end-marker. + posOfSuccEndmarker = i; } - // LF-mapping from '\0' does not work with this (pseudo) BWT (see details from Wolfgang's thesis). - p = textId; // Correct LF-mapping to the last char of the previous text. + // LF-mapping from '\0' does not work with this (pseudo) BWT. + // Correct LF-mapping to the last char of the previous text: + p = textId - notIndexed.rank(textId); } else // Now c != '\0', do LF-mapping: p = C[c]+alphabetrank_i_tmp-1; } + while (textId > 0 && notIndexed.isSet(textId-1)) + { + do { + -- tsb_i; + tsbuilder[tsb_i] = *nit_i; + ++ nit_i; + } while (nit_i != niText.rend() && *nit_i != '\0'); + --textId; + } assert(textId == 0); + assert(tsb_i == 0); + assert(nit_i == niText.rend()); #ifdef DEBUG_MEMUSAGE std::cerr << "heap usage before tsbuilder init: " << HeapProfiler::GetHeapConsumption()/(1024*1024) << " / " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes, " << HeapProfiler::GetHeapConsumption() << " / " << HeapProfiler::GetMaxHeapConsumption() << std::endl; @@ -1131,16 +1198,21 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) suffixes = new BlockArray(sampleLength, Tools::CeilLog2(maxTextLength)); suffixDocId = new BlockArray(sampleLength, Tools::CeilLog2(numberOfTexts)); - x = n - 2; - posOfSuccEndmarker = n-1; - for(ulong i=0; iIsEndmarker(x)) + { posOfSuccEndmarker = x--; + -- textId; + } } assert((*positions)[i] < n); ulong j = sampled->rank1((*positions)[i]); @@ -1148,14 +1220,18 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) assert(j != 0); // if (j==0) j=sampleLength; TextPosition textPos = (x==n-1)?0:x+1; - (*suffixDocId)[j-1] = textStorage->DocIdAtTextPos(textPos); + (*suffixDocId)[j-1] = textId; // textStorage->DocIdAtTextPos(textPos); + assert(textStorage->DocIdAtTextPos(textPos) == textId); assert((*suffixDocId)[j-1] < numberOfTexts); // calculate offset from text start: (*suffixes)[j-1] = textPos - textStorage->TextStartPos((*suffixDocId)[j-1]); --x; if (x != ~0lu && textStorage->IsEndmarker(x)) + { posOfSuccEndmarker = x--; + -- textId; + } } delete positions; @@ -1165,48 +1241,19 @@ void TCImplementation::maketables(ulong sampleLength, char tsType) HeapProfiler::ResetMaxHeapConsumption(); #endif - /** - * Second pass: check tables - */ -/* p=bwtEndPos; - textId = numberOfTexts; - for (ulong i=n-1;iaccess(p)) { - ulong j = sampled->rank1(p)-1; - assert((*suffixDocId)[j] == DocIdAtTextPos(textStartPos, x)); - - // calculate offset from text start: - assert((*suffixes)[j] == x - (*textStartPos)[(*suffixDocId)[j]]); - } - - uchar c = alphabetrank->access(p, alphabetrank_i_tmp); - - if (c == '\0') - { - --textId; - // LF-mapping from '\0' does not work with this (pseudo) BWT (see details from Wolfgang's thesis). - p = textId; // Correct LF-mapping to the last char of the previous text. - } - else // Now c != '\0', do LF-mapping: - p = C[c]+alphabetrank_i_tmp-1; - } - assert(textId == 0); - delete textStartPos -*/ - #ifdef DEBUG_MEMUSAGE std::cerr << "max heap usage before Doc: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl; HeapProfiler::ResetMaxHeapConsumption(); #endif - alphabet_mapper * am = new alphabet_mapper_none(); + /*alphabet_mapper * am = new alphabet_mapper_none(); static_bitsequence_builder * bmb = new static_bitsequence_builder_rrr02(32); // FIXME samplerate? Doc = new static_sequence_wvtree_noptrs(endmarkerDocId, numberOfTexts, logNumberOfTexts, bmb, am, true); - delete bmb; + delete bmb;*/ // delete [] endmarkerDocId; // already deleted in static_sequence_wvtree_noptrs! + Doc = new ArrayDoc(endmarkerDocId); + #ifdef DEBUG_MEMUSAGE std::cerr << "max heap usage after Doc: " << HeapProfiler::GetMaxHeapConsumption()/(1024*1024) << " Mbytes" << std::endl; #endif