Skip to content

Commit a1fa32d

Browse files
authored
refactor(clp): Prepare parsing & search code for deduplication with clp-s: (#1138)
- Use templates in `EncodedVariableInterpreter` instead of CLP dictionary implementations. - Use `std::string_view` where possible in `EncodedVariableInterpreter` and dictionary classes.
1 parent 9956454 commit a1fa32d

13 files changed

+440
-404
lines changed

components/core/src/clp/DictionaryReader.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#ifndef CLP_DICTIONARYREADER_HPP
22
#define CLP_DICTIONARYREADER_HPP
33

4+
#include <iterator>
45
#include <string>
6+
#include <string_view>
7+
#include <utility>
58
#include <vector>
69

710
#include <boost/algorithm/string.hpp>
@@ -34,6 +37,9 @@ class DictionaryReader {
3437
char const* what() const noexcept override { return "DictionaryReader operation failed"; }
3538
};
3639

40+
using dictionary_id_t = DictionaryIdType;
41+
using entry_t = EntryType;
42+
3743
// Constructors
3844
DictionaryReader() : m_is_open(false), m_num_segments_read_from_index(0) {
3945
static_assert(
@@ -85,15 +91,15 @@ class DictionaryReader {
8591
* @return a vector of matching entries, or an empty vector if no entry matches.
8692
*/
8793
std::vector<EntryType const*>
88-
get_entry_matching_value(std::string const& search_string, bool ignore_case) const;
94+
get_entry_matching_value(std::string_view search_string, bool ignore_case) const;
8995
/**
9096
* Gets the entries that match a given wildcard string
9197
* @param wildcard_string
9298
* @param ignore_case
9399
* @param entries Set in which to store found entries
94100
*/
95101
void get_entries_matching_wildcard_string(
96-
std::string const& wildcard_string,
102+
std::string_view wildcard_string,
97103
bool ignore_case,
98104
std::unordered_set<EntryType const*>& entries
99105
) const;
@@ -235,7 +241,7 @@ DictionaryReader<DictionaryIdType, EntryType>::get_value(DictionaryIdType id) co
235241
template <typename DictionaryIdType, typename EntryType>
236242
std::vector<EntryType const*>
237243
DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
238-
std::string const& search_string,
244+
std::string_view search_string,
239245
bool ignore_case
240246
) const {
241247
if (false == ignore_case) {
@@ -252,7 +258,11 @@ DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
252258
}
253259

254260
std::vector<EntryType const*> entries;
255-
auto const search_string_uppercase = boost::algorithm::to_upper_copy(search_string);
261+
std::string search_string_uppercase;
262+
std::ignore = boost::algorithm::to_upper_copy(
263+
std::back_inserter(search_string_uppercase),
264+
search_string
265+
);
256266
for (auto const& entry : m_entries) {
257267
if (boost::algorithm::to_upper_copy(entry.get_value()) == search_string_uppercase) {
258268
entries.push_back(&entry);
@@ -263,7 +273,7 @@ DictionaryReader<DictionaryIdType, EntryType>::get_entry_matching_value(
263273

264274
template <typename DictionaryIdType, typename EntryType>
265275
void DictionaryReader<DictionaryIdType, EntryType>::get_entries_matching_wildcard_string(
266-
std::string const& wildcard_string,
276+
std::string_view wildcard_string,
267277
bool ignore_case,
268278
std::unordered_set<EntryType const*>& entries
269279
) const {

components/core/src/clp/DictionaryWriter.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#define CLP_DICTIONARYWRITER_HPP
33

44
#include <string>
5-
#include <unordered_map>
5+
6+
#include <absl/container/flat_hash_map.h>
67

78
#include "ArrayBackedPosIntSet.hpp"
89
#include "Defs.h"
@@ -34,6 +35,9 @@ class DictionaryWriter {
3435
char const* what() const noexcept override { return "DictionaryWriter operation failed"; }
3536
};
3637

38+
using dictionary_id_t = DictionaryIdType;
39+
using entry_t = EntryType;
40+
3741
// Constructors
3842
DictionaryWriter() : m_is_open(false) {}
3943

@@ -83,7 +87,7 @@ class DictionaryWriter {
8387

8488
protected:
8589
// Types
86-
using value_to_id_t = std::unordered_map<std::string, DictionaryIdType>;
90+
using value_to_id_t = absl::flat_hash_map<std::string, DictionaryIdType>;
8791

8892
// Variables
8993
bool m_is_open;

0 commit comments

Comments
 (0)