Skip to content

Commit

Permalink
y-scope#648 get_entry_matching_value may return more than one value
Browse files Browse the repository at this point in the history
  • Loading branch information
aestriplex committed Jan 15, 2025
1 parent 8f00463 commit 5e5487f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 36 deletions.
15 changes: 9 additions & 6 deletions components/core/src/clp/DictionaryReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class DictionaryReader {
* Gets the entry exactly matching the given search string
* @param search_string
* @param ignore_case
* @return nullptr if an exact match is not found, the entry otherwise
* @return a (possibly empty) list of entries
*/
EntryType const*
std::vector<EntryType const*>
get_entry_matching_value(std::string const& search_string, bool ignore_case) const;
/**
* Gets the entries that match a given wildcard string
Expand Down Expand Up @@ -233,26 +233,29 @@ std::string const& DictionaryReader<DictionaryIdType, EntryType>::get_value(Dict
}

template <typename DictionaryIdType, typename EntryType>
EntryType const* DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::vector<EntryType const*>
DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::string const& search_string,
bool ignore_case
) const {
std::vector<EntryType const*> entries;
if (false == ignore_case) {
for (auto const& entry : m_entries) {
if (entry.get_value() == search_string) {
return &entry;
entries.push_back(&entry);
return entries; /* early exit for case sensitive branch */
}
}
} else {
auto const& search_string_uppercase = boost::algorithm::to_upper_copy(search_string);
for (auto const& entry : m_entries) {
if (boost::algorithm::to_upper_copy(entry.get_value()) == search_string_uppercase) {
return &entry;
entries.push_back(&entry);
}
}
}

return nullptr;
return entries;
}

template <typename DictionaryIdType, typename EntryType>
Expand Down
12 changes: 7 additions & 5 deletions components/core/src/clp/EncodedVariableInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,17 @@ bool EncodedVariableInterpreter::encode_and_search_dictionary(
LogTypeDictionaryEntry::add_float_var(logtype);
sub_query.add_non_dict_var(encoded_var);
} else {
auto entry = var_dict.get_entry_matching_value(var_str, ignore_case);
if (nullptr == entry) {
auto entries = var_dict.get_entry_matching_value(var_str, ignore_case);
if (entries.empty()) {
// Not in dictionary
return false;
}
encoded_var = encode_var_dict_id(entry->get_id());
for (auto i = 0; i < entries.size(); i++) {
encoded_var = encode_var_dict_id(entries[i]->get_id());

LogTypeDictionaryEntry::add_dict_var(logtype);
sub_query.add_dict_var(encoded_var, entry);
LogTypeDictionaryEntry::add_dict_var(logtype);
sub_query.add_dict_var(encoded_var, entries[i]);
}
}

return true;
Expand Down
15 changes: 9 additions & 6 deletions components/core/src/clp_s/DictionaryReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class DictionaryReader {
* Gets the entry exactly matching the given search string
* @param search_string
* @param ignore_case
* @return nullptr if an exact match is not found, the entry otherwise
* @return a (possibly empty) list of entries
*/
EntryType const*
std::vector<EntryType const*>
get_entry_matching_value(std::string const& search_string, bool ignore_case) const;

/**
Expand Down Expand Up @@ -156,26 +156,29 @@ std::string const& DictionaryReader<DictionaryIdType, EntryType>::get_value(Dict
}

template <typename DictionaryIdType, typename EntryType>
EntryType const* DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::vector<EntryType const*>
DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::string const& search_string,
bool ignore_case
) const {
std::vector<EntryType const*> entries;
if (false == ignore_case) {
for (auto const& entry : m_entries) {
if (entry.get_value() == search_string) {
return &entry;
entries.push_back(&entry);
return entries; /* early exit for case sensitive branch */
}
}
} else {
auto const& search_string_uppercase = boost::algorithm::to_upper_copy(search_string);
for (auto const& entry : m_entries) {
if (boost::algorithm::to_upper_copy(entry.get_value()) == search_string_uppercase) {
return &entry;
entries.push_back(&entry);
}
}
}

return nullptr;
return entries;
}

template <typename DictionaryIdType, typename EntryType>
Expand Down
6 changes: 3 additions & 3 deletions components/core/src/clp_s/search/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,13 @@ void Output::populate_string_queries(std::shared_ptr<Expression> const& expr) {
}
}

auto const* entry = m_var_dict->get_entry_matching_value(
auto entries = m_var_dict->get_entry_matching_value(
unescaped_query_string,
m_ignore_case
);

if (entry != nullptr) {
matching_vars.insert(entry->get_id());
for (auto i = 0; i < entries.size(); i++) {
matching_vars.insert(entries[i]->get_id());
}
} else if (EncodedVariableInterpreter::
wildcard_search_dictionary_and_get_encoded_matches(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ bool EncodedVariableInterpreter::encode_and_search_dictionary(
LogTypeDictionaryEntry::add_double_var(logtype);
sub_query.add_non_dict_var(encoded_var);
} else {
auto entry = var_dict.get_entry_matching_value(var_str, ignore_case);
if (nullptr == entry) {
auto entries = var_dict.get_entry_matching_value(var_str, ignore_case);
if (entries.empty()) {
// Not in dictionary
return false;
}
encoded_var = VariableEncoder::encode_var_dict_id(entry->get_id());
for (auto i = 0; i < entries.size(); i++) {
encoded_var = VariableEncoder::encode_var_dict_id(entries[i]->get_id());

LogTypeDictionaryEntry::add_non_double_var(logtype);
sub_query.add_dict_var(encoded_var, entry);
LogTypeDictionaryEntry::add_non_double_var(logtype);
sub_query.add_dict_var(encoded_var, entries[i]);
}
}

return true;
Expand Down
15 changes: 9 additions & 6 deletions components/core/src/glt/DictionaryReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class DictionaryReader {
* Gets the entry exactly matching the given search string
* @param search_string
* @param ignore_case
* @return nullptr if an exact match is not found, the entry otherwise
* @return a (possibly empty) list of entries
*/
EntryType const*
std::vector<EntryType const*>
get_entry_matching_value(std::string const& search_string, bool ignore_case) const;
/**
* Gets the entries that match a given wildcard string
Expand Down Expand Up @@ -228,26 +228,29 @@ std::string const& DictionaryReader<DictionaryIdType, EntryType>::get_value(Dict
}

template <typename DictionaryIdType, typename EntryType>
EntryType const* DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::vector<EntryType const*>
DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
std::string const& search_string,
bool ignore_case
) const {
std::vector<EntryType const*> entries;
if (false == ignore_case) {
for (auto const& entry : m_entries) {
if (entry.get_value() == search_string) {
return &entry;
entries.push_back(&entry);
return entries; /* early exit for case sensitive branch */
}
}
} else {
auto const& search_string_uppercase = boost::algorithm::to_upper_copy(search_string);
for (auto const& entry : m_entries) {
if (boost::algorithm::to_upper_copy(entry.get_value()) == search_string_uppercase) {
return &entry;
entries.push_back(&entry);
}
}
}

return nullptr;
return entries;
}

template <typename DictionaryIdType, typename EntryType>
Expand Down
13 changes: 8 additions & 5 deletions components/core/src/glt/EncodedVariableInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,18 @@ bool EncodedVariableInterpreter::encode_and_search_dictionary(
LogTypeDictionaryEntry::add_float_var(logtype);
sub_query.add_non_dict_var(encoded_var);
} else {
auto entry = var_dict.get_entry_matching_value(var_str, ignore_case);
if (nullptr == entry) {
auto entries = var_dict.get_entry_matching_value(var_str, ignore_case);
if (entries.empty()) {
// Not in dictionary
return false;
}
encoded_var = encode_var_dict_id(entry->get_id());

LogTypeDictionaryEntry::add_dict_var(logtype);
sub_query.add_dict_var(encoded_var, entry);
for (auto i = 0; i < entries.size(); i++) {
encoded_var = encode_var_dict_id(entries[i]->get_id());

LogTypeDictionaryEntry::add_dict_var(logtype);
sub_query.add_dict_var(encoded_var, entries[i]);
}
}

return true;
Expand Down

0 comments on commit 5e5487f

Please sign in to comment.