2021-06-27 17:54:58 +00:00
|
|
|
#include <xcdat.hpp>
|
|
|
|
|
|
|
|
#include "cmd_line_parser/parser.hpp"
|
2021-07-03 00:46:04 +00:00
|
|
|
#include "mm_file/mm_file.hpp"
|
2021-06-27 17:54:58 +00:00
|
|
|
#include "tinyformat/tinyformat.h"
|
|
|
|
|
|
|
|
cmd_line_parser::parser make_parser(int argc, char** argv) {
|
|
|
|
cmd_line_parser::parser p(argc, argv);
|
2021-07-02 12:50:10 +00:00
|
|
|
p.add("input_dic", "Input filepath of trie dictionary");
|
2021-06-27 17:54:58 +00:00
|
|
|
p.add("max_num_results", "The max number of results (default=10)", "-n", false);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Trie>
|
|
|
|
int predictive_search(const cmd_line_parser::parser& p) {
|
2021-07-02 12:50:10 +00:00
|
|
|
const auto input_dic = p.get<std::string>("input_dic");
|
2021-06-27 17:54:58 +00:00
|
|
|
const auto max_num_results = p.get<std::uint64_t>("max_num_results", 10);
|
|
|
|
|
2021-07-02 12:50:10 +00:00
|
|
|
const mm::file_source<char> fin(input_dic.c_str(), mm::advice::sequential);
|
2021-06-29 01:45:02 +00:00
|
|
|
const auto trie = xcdat::mmap<Trie>(fin.data());
|
2021-06-27 17:54:58 +00:00
|
|
|
|
|
|
|
struct result_type {
|
|
|
|
std::uint64_t id;
|
|
|
|
std::string str;
|
|
|
|
};
|
|
|
|
std::vector<result_type> results;
|
|
|
|
results.reserve(1ULL << 10);
|
|
|
|
|
2021-06-29 06:39:26 +00:00
|
|
|
for (std::string key; std::getline(std::cin, key);) {
|
2021-06-27 17:54:58 +00:00
|
|
|
results.clear();
|
2021-06-29 06:39:26 +00:00
|
|
|
trie.predictive_search(key, [&](std::uint64_t id, std::string_view str) {
|
|
|
|
results.push_back({id, std::string(str)});
|
|
|
|
});
|
2021-06-27 17:54:58 +00:00
|
|
|
|
|
|
|
tfm::printfln("%d found", results.size());
|
|
|
|
for (std::uint64_t i = 0; i < std::min<std::uint64_t>(results.size(), max_num_results); i++) {
|
|
|
|
const auto& r = results[i];
|
|
|
|
tfm::printfln("%d\t%s", r.id, r.str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
tfm::warnfln("The code is running in debug mode.");
|
|
|
|
#endif
|
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
|
|
|
|
auto p = make_parser(argc, argv);
|
|
|
|
if (!p.parse()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:50:10 +00:00
|
|
|
const auto input_dic = p.get<std::string>("input_dic");
|
2021-07-08 13:47:59 +00:00
|
|
|
const auto type_id = xcdat::get_type_id(input_dic);
|
2021-06-27 17:54:58 +00:00
|
|
|
|
2021-07-08 13:47:59 +00:00
|
|
|
switch (type_id) {
|
2021-06-27 17:54:58 +00:00
|
|
|
case 7:
|
|
|
|
return predictive_search<xcdat::trie_7_type>(p);
|
|
|
|
case 8:
|
|
|
|
return predictive_search<xcdat::trie_8_type>(p);
|
2021-07-03 00:46:04 +00:00
|
|
|
case 15:
|
|
|
|
return predictive_search<xcdat::trie_15_type>(p);
|
|
|
|
case 16:
|
|
|
|
return predictive_search<xcdat::trie_16_type>(p);
|
2021-06-27 17:54:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.help();
|
|
|
|
return 1;
|
|
|
|
}
|