Skip to content

Commit

Permalink
Per issue #1950: in has() function, experimenting using the upper_bou…
Browse files Browse the repository at this point in the history
…nd() search instead of lower_bound() for sorted vectors. In progress. SL
  • Loading branch information
Seth Linden committed Mar 29, 2022
1 parent 2c58afa commit 8d5f83f
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions met/src/basic/vx_log/string_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
// cout << "From StringArray::has(), the vector of strings is Not Sorted (false)" << endl;
//}

cout << "In has(), searching for text = " << text.c_str() << endl;
//cout << "In has(), searching for text = " << text.c_str() << endl;

if (!s.empty()) {

Expand All @@ -418,7 +418,9 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
// If the vector is Sorted (true) and IgnoreCase is false
// use lower_bound() to search for the text string
if (Sorted && !IgnoreCase) {
cout << "Sorted is true and IgnoreCase is false" << endl;
//cout << "Sorted is true and IgnoreCase is false" << endl;

/*
it = lower_bound(s.begin(), s.end(), text);
if(it != s.end() && *it == text){
count = it - s.begin();
Expand All @@ -429,19 +431,33 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
cout << "Using lower_bound(), NO text = " << text.c_str() << endl << endl;
count = 0;
}
*/

it = upper_bound(s.begin(), s.end(), text);
it--;
if(*it == text){
count = it - s.begin();
//cout << "Using upper_bound(), FOUND text = " << text.c_str() << " count = " << count << endl << endl;
found = true;
}
else {
//cout << "Using upper_bound(), NO text = " << text.c_str() << endl << endl;
count = 0;
}

}
else {
std::string lower_text = text;
cout << "Before transform, lower_text = " << lower_text.c_str() << endl;
//cout << "Before transform, lower_text = " << lower_text.c_str() << endl;
if ( IgnoreCase ) transform(lower_text.begin(), lower_text.end(), lower_text.begin(), ::tolower);

cout << "After transform, lower_text = " << lower_text.c_str() << endl;
//cout << "After transform, lower_text = " << lower_text.c_str() << endl;

if (forward) {
count = 0;
for(it = s.begin(); it != s.end(); it++, count++) {
if ( IgnoreCase ) {
cout << "Sorted is false, IgnoreCase is true, forward is true" << endl;
//cout << "Sorted is false, IgnoreCase is true, forward is true" << endl;
std::string lower_s = *it;
transform(lower_s.begin(), lower_s.end(), lower_s.begin(), ::tolower);
if ( lower_s == lower_text) {
Expand All @@ -450,7 +466,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
}
}
else {
cout << "Sorted is false, IgnoreCase is false, forward is true" << endl;
//cout << "Sorted is false, IgnoreCase is false, forward is true" << endl;
if ( *it == text ) {
found = true;
break;
Expand All @@ -463,7 +479,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
it = s.end();
for(it--; it != s.begin(); it--, count--) {
if ( IgnoreCase ) {
cout << "Sorted is false, IgnoreCase is true, forward is false" << endl;
//cout << "Sorted is false, IgnoreCase is true, forward is false" << endl;
std::string lower_s = *it;
transform(lower_s.begin(), lower_s.end(), lower_s.begin(), ::tolower);
if ( lower_s == lower_text) {
Expand All @@ -472,7 +488,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
}
}
else {
cout << "Sorted is false, IgnoreCase is false, forward is false" << endl;
//cout << "Sorted is false, IgnoreCase is false, forward is false" << endl;
if ( *it == text ) {
found = true;
break;
Expand All @@ -481,7 +497,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
}
if (!found && it == s.begin()) {
if ( IgnoreCase ) {
cout << "Single string case, Sorted is false, IgnoreCase is true, forward is false" << endl;
//cout << "Single string case, Sorted is false, IgnoreCase is true, forward is false" << endl;
std::string lower_s = *it;
transform(lower_s.begin(), lower_s.end(), lower_s.begin(), ::tolower);
found = ( lower_s == lower_text );
Expand All @@ -495,7 +511,7 @@ bool StringArray::has(const std::string text, int & index, bool forward) const
if (found) index = count;
}

cout << endl;
//cout << endl;

//mlog << Debug(9) << " StringArray::has() size=" << s.size()
// << " for " << text << ", found: " << (found ? "true" : "false")
Expand Down

0 comments on commit 8d5f83f

Please sign in to comment.