add cmdline tools

This commit is contained in:
Shunsuke Kanda 2021-06-28 02:54:58 +09:00
parent eebe7ac0db
commit 4ea523bc4f
7 changed files with 298 additions and 0 deletions

View file

@ -1 +1,7 @@
add_executable(xcdat_build xcdat_build.cpp) add_executable(xcdat_build xcdat_build.cpp)
add_executable(xcdat_lookup xcdat_lookup.cpp)
add_executable(xcdat_decode xcdat_decode.cpp)
add_executable(xcdat_prefix_search xcdat_prefix_search.cpp)
add_executable(xcdat_predictive_search xcdat_predictive_search.cpp)
add_executable(xcdat_enumerate xcdat_enumerate.cpp)

View file

@ -43,6 +43,8 @@ int build(const cmd_line_parser::parser& p) {
tfm::printfln("alphabet_size: %d", trie.alphabet_size()); tfm::printfln("alphabet_size: %d", trie.alphabet_size());
tfm::printfln("max_length: %d", trie.max_length()); tfm::printfln("max_length: %d", trie.max_length());
trie.save(output_idx);
return 0; return 0;
} }
@ -50,6 +52,7 @@ int main(int argc, char** argv) {
#ifndef NDEBUG #ifndef NDEBUG
tfm::warnfln("The code is running in debug mode."); tfm::warnfln("The code is running in debug mode.");
#endif #endif
std::ios::sync_with_stdio(false);
auto p = make_parser(argc, argv); auto p = make_parser(argc, argv);
if (!p.parse()) { if (!p.parse()) {

50
tools/xcdat_decode.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
p.add("trie_type", "Type of trie impl. from [7|8], (default=7)", "-t", false);
return p;
}
template <class Trie>
int decode(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
const auto trie = Trie::load(input_idx);
for (std::uint64_t id; std::cin >> id;) {
const auto dec = trie.decode(id);
tfm::printfln("%d\t%s", id, dec);
}
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;
}
const auto trie_type = p.get<int>("trie_type", 7);
switch (trie_type) {
case 7:
return decode<xcdat::trie_7_type>(p);
case 8:
return decode<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}

47
tools/xcdat_enumerate.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
p.add("trie_type", "Type of trie impl. from [7|8], (default=7)", "-t", false);
return p;
}
template <class Trie>
int enumerate(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
const auto trie = Trie::load(input_idx);
trie.enumerate([&](std::uint64_t id, std::string_view str) { tfm::printfln("%d\t%s", id, 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;
}
const auto trie_type = p.get<int>("trie_type", 7);
switch (trie_type) {
case 7:
return enumerate<xcdat::trie_7_type>(p);
case 8:
return enumerate<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}

54
tools/xcdat_lookup.cpp Normal file
View file

@ -0,0 +1,54 @@
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
p.add("trie_type", "Type of trie impl. from [7|8], (default=7)", "-t", false);
return p;
}
template <class Trie>
int lookup(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
const auto trie = Trie::load(input_idx);
for (std::string str; std::getline(std::cin, str);) {
const auto id = trie.lookup(str);
if (id.has_value()) {
tfm::printfln("%d\t%s", id.value(), str);
} else {
tfm::printfln("-1\t%s", 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;
}
const auto trie_type = p.get<int>("trie_type", 7);
switch (trie_type) {
case 7:
return lookup<xcdat::trie_7_type>(p);
case 8:
return lookup<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}

View file

@ -0,0 +1,71 @@
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
p.add("max_num_results", "The max number of results (default=10)", "-n", false);
p.add("trie_type", "Type of trie impl. from [7|8], (default=7)", "-t", false);
return p;
}
template <class Trie>
int predictive_search(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
const auto max_num_results = p.get<std::uint64_t>("max_num_results", 10);
const auto trie = Trie::load(input_idx);
struct result_type {
std::uint64_t id;
std::string str;
};
std::vector<result_type> results;
results.reserve(1ULL << 10);
for (std::string str; std::getline(std::cin, str);) {
results.clear();
auto itr = trie.make_predictive_iterator(str);
while (itr.next()) {
results.push_back({itr.id(), itr.decoded()});
}
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;
}
const auto trie_type = p.get<int>("trie_type", 7);
switch (trie_type) {
case 7:
return predictive_search<xcdat::trie_7_type>(p);
case 8:
return predictive_search<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}

View file

@ -0,0 +1,67 @@
#include <xcdat.hpp>
#include "cmd_line_parser/parser.hpp"
#include "tinyformat/tinyformat.h"
cmd_line_parser::parser make_parser(int argc, char** argv) {
cmd_line_parser::parser p(argc, argv);
p.add("input_idx", "Input filepath of trie index");
p.add("trie_type", "Type of trie impl. from [7|8], (default=7)", "-t", false);
return p;
}
template <class Trie>
int prefix_search(const cmd_line_parser::parser& p) {
const auto input_idx = p.get<std::string>("input_idx");
const auto trie = Trie::load(input_idx);
struct result_type {
std::uint64_t id;
std::string_view str;
};
std::vector<result_type> results;
results.reserve(trie.max_length());
for (std::string str; std::getline(std::cin, str);) {
results.clear();
auto itr = trie.make_prefix_iterator(str);
while (itr.next()) {
results.push_back({itr.id(), itr.decoded_view()});
}
tfm::printfln("%d found", results.size());
for (const auto& r : results) {
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;
}
const auto trie_type = p.get<int>("trie_type", 7);
switch (trie_type) {
case 7:
return prefix_search<xcdat::trie_7_type>(p);
case 8:
return prefix_search<xcdat::trie_8_type>(p);
default:
break;
}
p.help();
return 1;
}