Skip to content
Snippets Groups Projects
Commit 7704720e authored by Jan Trávníček's avatar Jan Trávníček
Browse files

algo: make ReversedBMH use size_t instead of ints

ints unnecessarily reduce range of values for indexes
parent c7b3ed53
No related branches found
No related tags found
1 merge request!179C casts to C++ casts redesign
...@@ -31,18 +31,29 @@ ext::set < unsigned > ReversedBoyerMooreHorspool::match ( const string::LinearSt ...@@ -31,18 +31,29 @@ ext::set < unsigned > ReversedBoyerMooreHorspool::match ( const string::LinearSt
ext::set < unsigned > occ; ext::set < unsigned > occ;
ext::map < SymbolType, size_t > bcs = string::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern ext::map < SymbolType, size_t > bcs = string::properties::ReversedBadCharacterShiftTable::bcs ( pattern ); // NOTE: the subjects alphabet must be a subset or equal to the pattern
   
int haystack_offset = string.getContent ( ).size ( ) - pattern.getContent ( ).size ( ); if ( string.getContent ( ).size ( ) < pattern.getContent ( ).size ( ) )
return occ;
   
while ( haystack_offset >= 0 ) { // index to the subject
size_t i = 0; size_t i = string.getContent ( ).size ( ) - pattern.getContent ( ).size ( );
   
while ( i < pattern.getContent ( ).size ( ) && string.getContent ( )[haystack_offset + i] == pattern.getContent ( )[i] ) // main loop of the algorithm over all possible indexes where the pattern can start
i++; while ( true ) {
size_t j = 0;
while ( j < pattern.getContent ( ).size ( ) && string.getContent ( ) [ i + j ] == pattern.getContent ( ) [ j ] )
j++;
   
// Yay, there is match!!! // Yay, there is match!!!
if ( i == pattern.getContent ( ).size ( ) ) occ.insert ( haystack_offset ); if ( j == pattern.getContent ( ).size ( ) ) occ.insert ( i );
// shift heristics
size_t shift = bcs [ string.getContent ( ) [ i ] ];
if ( shift > i )
break;
   
haystack_offset -= bcs[string.getContent ( )[haystack_offset]]; i -= shift;
   
// common::Streams::out << haystack_offset << std::endl; // common::Streams::out << haystack_offset << std::endl;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment