Commit f050c88f authored by Asal Asgari's avatar Asal Asgari
Browse files

Update naive_search.h

added std::distance instead of for :))
also added a break somewhere in the code :D
parent 6a575fb8
......@@ -18,60 +18,52 @@ namespace dte3603::string_match::algorithms
S_Iterator_T s_begin, S_Sentinel_T s_end,
BinaryPredicate_T pred = BinaryPredicate_T())
{
// std::ofstream fout;
// fout.open("D:/UIT/Naive.txt");
using Type = typename std::iterator_traits<Iterator_T>::value_type;
int size_str =0;
int size_sub =0;
int countS = 0;
for(auto it=begin; it!= end;++it){
// fout<<(*it);
size_str++;
}
// fout<<"\n";
for(auto it=s_begin; it!= s_end;++it){
size_sub++;
// fout<<(*it);
}
// fout<<"\n";
int i=0;
std::ofstream fout;
fout.open("D:/UIT/Naive.txt");
using Type = typename std::iterator_traits<Iterator_T>::value_type;
auto size_str =std::distance(begin,end);
auto size_sub =std::distance(s_begin,s_end);
int countS = 0;
for (; i <= size_str - size_sub; i++)
{
int j;
// fout<<"we are in for \n";
int i=0;
for (j=0; j < size_sub; j++)
{
if(next(begin,i+j)!=end){
if ((std::equal_to<Type>{}((*(next(begin,i+j))),(*(next(s_begin,j)))))){
// fout<<"we are equal"<<(*(next(begin,i+j)))<<" and "<<(*(next(s_begin,j)))<<" \n";
countS++;
}
else{
countS=0;
}
}
for (; i <= size_str - size_sub; i++)
{
int j;
for (j=0; j < size_sub; j++)
{
if(next(begin,i+j)!=end){
if ((std::equal_to<Type>{}((*(next(begin,i+j))),(*(next(s_begin,j)))))){
fout<<"we are equal"<<(*(next(begin,i+j)))<<" and "<<(*(next(s_begin,j)))<<" \n";
countS++;
}
else{
countS=0;
break;
}
}
}
if (countS == size_sub)
{
// fout<<"we are in this if \n";
if(next(begin,i)!=end){
begin = next(begin, i);
}
for(auto itr=begin;itr!=end;itr++){
fout<<(*itr);
}
// fout<<"\n that was out string match \n";
break;
}
}
}
if (countS == size_sub)
{
fout<<"we are in this if \n";
if(next(begin,i)!=end){
begin = next(begin, i);
}
for(auto itr=begin;itr!=end;itr++){
fout<<(*itr);
}
fout<<"\n that was out string match \n";
break;
}
}
return begin;
// fout<<"begin is now :"<<(*begin);
return begin;
}
} // namespace dte3603::string_match::algorithms
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment