Skip to content

Commit b707575

Browse files
authored
Migrate boost variant to std variant (KeyviDev#342)
Get rid of boost::variant in favor of std::variant.
1 parent 5164919 commit b707575

File tree

6 files changed

+55
-69
lines changed

6 files changed

+55
-69
lines changed

keyvi/bin/keyviinspector/keyviinspector.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
#include <iostream>
2727
#include <string>
2828

29-
#include <boost/lexical_cast.hpp>
30-
#include <boost/program_options.hpp>
29+
#include <boost/program_options.hpp> // NOLINT(misc-include-cleaner)
3130
#include <boost/program_options/options_description.hpp>
3231
#include <boost/program_options/positional_options.hpp>
3332
#include <boost/program_options/value_semantic.hpp>
@@ -59,25 +58,6 @@ void dump(const std::string& input, const std::string& output, bool keys_only =
5958
out_stream.close();
6059
}
6160

62-
void dump_with_attributes(const std::string& input, const std::string& output) {
63-
keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input));
64-
keyvi::dictionary::fsa::EntryIterator it(automata);
65-
keyvi::dictionary::fsa::EntryIterator const end_it = keyvi::dictionary::fsa::EntryIterator();
66-
67-
std::ofstream out_stream(output);
68-
69-
while (it != end_it) {
70-
it.WriteKey(out_stream);
71-
72-
out_stream << "\t";
73-
74-
out_stream << it.GetValueAsAttributeVector()->at("value");
75-
out_stream << "\n";
76-
++it;
77-
}
78-
out_stream.close();
79-
}
80-
8161
void print_statistics(const std::string& input) {
8262
keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input));
8363
std::cout << automata->GetStatistics() << '\n';
@@ -121,7 +101,6 @@ int main(int argc, char** argv) {
121101
output_file = vm["output-file"].as<std::string>();
122102

123103
dump(input_file, output_file, key_only);
124-
// dump_with_attributes (input_file, output_file);
125104
return 0;
126105
}
127106

keyvi/include/keyvi/dictionary/completion/forward_backward_completion.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <vector>
3434

3535
#include <boost/algorithm/string.hpp>
36-
#include <boost/lexical_cast.hpp>
3736

3837
#include "keyvi/dictionary/completion/prefix_completion.h"
3938
#include "keyvi/dictionary/util/bounded_priority_queue.h"
@@ -72,8 +71,8 @@ class ForwardBackwardCompletion final {
7271
util::BoundedPriorityQueue<uint32_t> best_scores(2 * number_of_results);
7372
std::vector<match_t> results;
7473

75-
for (auto match : forward_completions_.GetCompletions(query, number_of_results)) {
76-
uint32_t weight = boost::lexical_cast<uint32_t>(match->GetAttribute("weight"));
74+
for (auto& match : forward_completions_.GetCompletions(query, number_of_results)) {
75+
uint32_t weight = match->GetWeight();
7776

7877
// put the weight into the priority queue
7978
best_scores.Put(weight);
@@ -109,8 +108,8 @@ class ForwardBackwardCompletion final {
109108
best_scores.Back());
110109

111110
uint32_t last_weight = 0;
112-
for (auto match : backward_completions_.GetCompletions(phrase.c_str(), number_of_results)) {
113-
uint32_t weight = boost::lexical_cast<uint32_t>(match->GetAttribute("weight"));
111+
for (auto& match : backward_completions_.GetCompletions(phrase.c_str(), number_of_results)) {
112+
uint32_t weight = match->GetWeight();
114113

115114
if (weight < best_scores.Back()) {
116115
TRACE("Skip Backward, score to low %d", weight);
@@ -160,15 +159,15 @@ class ForwardBackwardCompletion final {
160159

161160
// reuse results vector
162161
results.clear();
163-
for (auto match : backward_completions_.GetCompletions(phrase.c_str(), number_of_results)) {
162+
for (auto& match : backward_completions_.GetCompletions(phrase.c_str(), number_of_results)) {
164163
std::string matched_string = match->GetMatchedString();
165164
std::reverse(matched_string.begin(), matched_string.end());
166165
// if the original query had a space at the end, this result should as well
167166
if (last_character_is_space) {
168167
matched_string.append(" ");
169168
}
170169

171-
uint32_t weight = boost::lexical_cast<uint32_t>(match->GetAttribute("weight"));
170+
uint32_t weight = match->GetWeight();
172171
match->SetScore(weight);
173172
match->SetMatchedString(matched_string);
174173

@@ -195,9 +194,9 @@ class ForwardBackwardCompletion final {
195194
}
196195

197196
// match forward with this
198-
for (auto match_forward :
197+
for (auto& match_forward :
199198
forward_completions_.GetCompletions(m->GetMatchedString().c_str(), number_of_results)) {
200-
uint32_t weight = boost::lexical_cast<uint32_t>(match_forward->GetAttribute("weight"));
199+
uint32_t weight = match_forward->GetWeight();
201200

202201
if (weight < best_scores.Back()) {
203202
TRACE("Skip Backward forward, score to low %d", weight);

keyvi/include/keyvi/dictionary/fsa/internal/ivalue_store.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727

2828
#include <memory>
2929
#include <string>
30+
#include <variant>
3031

3132
#include <boost/container/flat_map.hpp>
3233
#include <boost/interprocess/file_mapping.hpp>
3334
#include <boost/interprocess/mapped_region.hpp>
34-
#include <boost/variant.hpp>
3535

3636
#include "keyvi/compression/compression_selector.h"
3737
#include "keyvi/dictionary/dictionary_merger_fwd.h"
@@ -60,7 +60,7 @@ struct ValueStoreComponents {};
6060
*
6161
* The following types/constants/methods (incomplete list) are required:
6262
*
63-
* typedef {type} value_t;
63+
* using value_t = {type};
6464
* static const {type} no_value = 0;
6565
*
6666
* uint64_t GetValue(value_t value, bool* no_minimization)
@@ -85,8 +85,9 @@ struct ValueStoreComponents {};
8585
*/
8686
class IValueStoreReader {
8787
public:
88-
typedef boost::container::flat_map<std::string, boost::variant<std::string, int, double, bool>> attributes_raw_t;
89-
typedef std::shared_ptr<attributes_raw_t> attributes_t;
88+
using attribute_t = std::variant<std::string, int, double, bool>;
89+
using attributes_raw_t = boost::container::flat_map<std::string, attribute_t>;
90+
using attributes_t = std::shared_ptr<attributes_raw_t>;
9091

9192
/**
9293
* Default constructor. Override if the value store implementation requires extra data.

keyvi/include/keyvi/dictionary/match.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include <memory>
2929
#include <string>
3030
#include <utility>
31+
#include <variant>
3132

3233
#include <boost/container/flat_map.hpp>
33-
#include <boost/variant.hpp>
3434

3535
#include "keyvi/compression/compression_strategy.h"
3636
#include "keyvi/dictionary/fsa/automata.h"
@@ -56,22 +56,9 @@ keyvi::dictionary::match_t FirstFilteredMatch(const MatcherT&, const DeletedT&);
5656
} // namespace index
5757
namespace dictionary {
5858

59-
#ifdef Py_PYTHON_H
60-
class attributes_visitor : public boost::static_visitor<PyObject*> {
61-
public:
62-
PyObject* operator()(int i) const { return PyLong_FromLong(i); }
63-
64-
PyObject* operator()(double i) const { return PyFloat_FromDouble(i); }
65-
66-
PyObject* operator()(bool i) const { return i ? Py_True : Py_False; }
67-
68-
PyObject* operator()(const std::string& str) const { return PyUnicode_FromString(str.c_str()); }
69-
};
70-
#endif
71-
7259
struct Match {
73-
typedef std::shared_ptr<boost::container::flat_map<std::string, boost::variant<std::string, int, double, bool>>>
74-
attributes_t;
60+
using attribute_t = std::variant<std::string, int, double, bool>;
61+
using attributes_t = std::shared_ptr<boost::container::flat_map<std::string, attribute_t>>;
7562

7663
Match(size_t a, size_t b, const std::string& matched_item, uint32_t score = 0, uint32_t weight = 0)
7764
: start_(a), end_(b), matched_item_(matched_item), raw_value_(), score_(score) {
@@ -139,11 +126,27 @@ struct Match {
139126
#ifdef Py_PYTHON_H
140127
PyObject* GetAttributePy(const std::string& key) {
141128
auto result = GetAttribute(key);
142-
return boost::apply_visitor(attributes_visitor(), result);
129+
130+
return std::visit(
131+
[](auto&& arg) {
132+
using T = std::decay_t<decltype(arg)>;
133+
if constexpr (std::is_same_v<T, bool>) {
134+
return arg ? Py_True : Py_False;
135+
} else if constexpr (std::is_same_v<T, int>) {
136+
return PyLong_FromLong(arg);
137+
} else if constexpr (std::is_same_v<T, double>) {
138+
return PyFloat_FromDouble(arg);
139+
} else if constexpr (std::is_same_v<T, std::string>) {
140+
return PyUnicode_FromString(arg.c_str());
141+
}
142+
// not reachable
143+
throw std::runtime_error("unexpected attribute type");
144+
},
145+
result);
143146
}
144147
#endif
145148

146-
const boost::variant<std::string, int, double, bool>& GetAttribute(const std::string& key) {
149+
const attribute_t& GetAttribute(const std::string& key) {
147150
// lazy creation
148151
if (!attributes_) {
149152
if (fsa_) {

keyvi/include/keyvi/index/internal/index_settings.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#define KEYVI_INDEX_INTERNAL_INDEX_SETTINGS_H_
2626

2727
#include <string>
28+
#include <tuple>
2829
#include <unordered_map>
29-
30-
#include <boost/variant.hpp>
30+
#include <variant>
3131

3232
#include "keyvi/index/constants.h"
3333
#include "keyvi/index/internal/index_auto_config.h"
@@ -77,24 +77,24 @@ class IndexSettings final {
7777
}
7878
}
7979

80-
const std::string& GetKeyviMergerBin() const { return boost::get<std::string>(settings_.at(KEYVIMERGER_BIN)); }
80+
const std::string& GetKeyviMergerBin() const { return std::get<std::string>(settings_.at(KEYVIMERGER_BIN)); }
8181

82-
const size_t GetMaxSegments() const { return boost::get<size_t>(settings_.at(INDEX_MAX_SEGMENTS)); }
82+
const size_t GetMaxSegments() const { return std::get<size_t>(settings_.at(INDEX_MAX_SEGMENTS)); }
8383

8484
const size_t GetSegmentCompileKeyThreshold() const {
85-
return boost::get<size_t>(settings_.at(SEGMENT_COMPILE_KEY_THRESHOLD));
85+
return std::get<size_t>(settings_.at(SEGMENT_COMPILE_KEY_THRESHOLD));
8686
}
8787

88-
const size_t GetMaxConcurrentMerges() const { return boost::get<size_t>(settings_.at(MAX_CONCURRENT_MERGES)); }
88+
const size_t GetMaxConcurrentMerges() const { return std::get<size_t>(settings_.at(MAX_CONCURRENT_MERGES)); }
8989

90-
const size_t GetRefreshInterval() const { return boost::get<size_t>(settings_.at(INDEX_REFRESH_INTERVAL)); }
90+
const size_t GetRefreshInterval() const { return std::get<size_t>(settings_.at(INDEX_REFRESH_INTERVAL)); }
9191

9292
const size_t GetSegmentExternalMergeKeyThreshold() const {
93-
return boost::get<size_t>(settings_.at(SEGMENT_EXTERNAL_MERGE_KEY_THRESHOLD));
93+
return std::get<size_t>(settings_.at(SEGMENT_EXTERNAL_MERGE_KEY_THRESHOLD));
9494
}
9595

9696
private:
97-
std::unordered_map<std::string, boost::variant<std::string, size_t>> settings_;
97+
std::unordered_map<std::string, std::variant<std::string, size_t>> settings_;
9898
};
9999

100100
} /* namespace internal */

keyvi/tests/keyvi/dictionary/dictionary_test.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
* Author: hendrik
2424
*/
2525

26+
#include <string>
27+
#include <tuple>
28+
#include <vector>
29+
2630
#include <boost/filesystem.hpp>
2731
#include <boost/test/unit_test.hpp>
2832

@@ -71,7 +75,7 @@ BOOST_AUTO_TEST_CASE(DictGet) {
7175
bool matched = false;
7276
for (auto m : d->Get("test")) {
7377
BOOST_CHECK_EQUAL("test", m->GetMatchedString());
74-
BOOST_CHECK_EQUAL(std::string("22"), boost::get<std::string>(m->GetAttribute("weight")));
78+
BOOST_CHECK_EQUAL(std::string("22"), std::get<std::string>(m->GetAttribute("weight")));
7579
matched = true;
7680
}
7781
BOOST_CHECK(matched);
@@ -84,7 +88,7 @@ BOOST_AUTO_TEST_CASE(DictGet) {
8488
BOOST_CHECK(!matched);
8589

8690
auto m = (*d)["test"];
87-
BOOST_CHECK_EQUAL("22", boost::get<std::string>(m->GetAttribute("weight")));
91+
BOOST_CHECK_EQUAL("22", std::get<std::string>(m->GetAttribute("weight")));
8892
}
8993

9094
BOOST_AUTO_TEST_CASE(DictLookup) {
@@ -99,23 +103,23 @@ BOOST_AUTO_TEST_CASE(DictLookup) {
99103
bool matched = false;
100104
for (auto m : d->Lookup("nude")) {
101105
BOOST_CHECK_EQUAL("nude", m->GetMatchedString());
102-
BOOST_CHECK_EQUAL("22", boost::get<std::string>(m->GetAttribute("weight")));
106+
BOOST_CHECK_EQUAL("22", std::get<std::string>(m->GetAttribute("weight")));
103107
matched = true;
104108
}
105109
BOOST_CHECK(matched);
106110

107111
matched = false;
108112
for (auto m : d->Lookup("nude ")) {
109113
BOOST_CHECK_EQUAL("nude", m->GetMatchedString());
110-
BOOST_CHECK_EQUAL("22", boost::get<std::string>(m->GetAttribute("weight")));
114+
BOOST_CHECK_EQUAL("22", std::get<std::string>(m->GetAttribute("weight")));
111115
matched = true;
112116
}
113117
BOOST_CHECK(matched);
114118

115119
matched = false;
116120
for (auto m : d->Lookup("nude at work")) {
117121
BOOST_CHECK_EQUAL("nude", m->GetMatchedString());
118-
BOOST_CHECK_EQUAL("22", boost::get<std::string>(m->GetAttribute("weight")));
122+
BOOST_CHECK_EQUAL("22", std::get<std::string>(m->GetAttribute("weight")));
119123
matched = true;
120124
}
121125
BOOST_CHECK(matched);
@@ -207,7 +211,7 @@ BOOST_AUTO_TEST_CASE(DictGetZerobyte) {
207211
bool matched = false;
208212
for (auto m : d->Get(std::string("\0test", 5))) {
209213
BOOST_CHECK_EQUAL(std::string("\0test", 5), m->GetMatchedString());
210-
BOOST_CHECK_EQUAL(std::string("22"), boost::get<std::string>(m->GetAttribute("weight")));
214+
BOOST_CHECK_EQUAL(std::string("22"), std::get<std::string>(m->GetAttribute("weight")));
211215
matched = true;
212216
}
213217
BOOST_CHECK(matched);
@@ -220,7 +224,7 @@ BOOST_AUTO_TEST_CASE(DictGetZerobyte) {
220224
BOOST_CHECK(!matched);
221225

222226
auto m = (*d)[std::string("\0test", 5)];
223-
BOOST_CHECK_EQUAL("22", boost::get<std::string>(m->GetAttribute("weight")));
227+
BOOST_CHECK_EQUAL("22", std::get<std::string>(m->GetAttribute("weight")));
224228
}
225229

226230
BOOST_AUTO_TEST_CASE(DictGetPrefixCompletion) {

0 commit comments

Comments
 (0)