From 49419bf6fce2074152c0c1b7229c0b5f1fe43b1c Mon Sep 17 00:00:00 2001 From: Shunsuke Kanda Date: Tue, 29 Jun 2021 12:02:51 +0900 Subject: [PATCH] add flags --- include/xcdat.hpp | 14 ++++++++++++-- tools/xcdat_decode.cpp | 14 +++++++++++--- tools/xcdat_enumerate.cpp | 14 +++++++++++--- tools/xcdat_lookup.cpp | 14 +++++++++++--- tools/xcdat_predictive_search.cpp | 14 +++++++++++--- tools/xcdat_prefix_search.cpp | 14 +++++++++++--- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/include/xcdat.hpp b/include/xcdat.hpp index f12e450..7189d43 100644 --- a/include/xcdat.hpp +++ b/include/xcdat.hpp @@ -13,18 +13,26 @@ namespace xcdat { using trie_7_type = trie; using trie_8_type = trie; +using flag_type = std::remove_const_t; + template [[maybe_unused]] Trie mmap(const char* address) { - Trie idx; mmap_visitor visitor(address); + flag_type flag; + visitor.visit(flag); + XCDAT_THROW_IF(flag != Trie::l1_bits, "The input index type is different."); + Trie idx; visitor.visit(idx); return idx; } template [[maybe_unused]] Trie load(std::string_view filepath) { - Trie idx; load_visitor visitor(filepath); + flag_type flag; + visitor.visit(flag); + XCDAT_THROW_IF(flag != Trie::l1_bits, "The input index type is different."); + Trie idx; visitor.visit(idx); return idx; } @@ -32,6 +40,7 @@ template template [[maybe_unused]] std::uint64_t save(const Trie& idx, std::string_view filepath) { save_visitor visitor(filepath); + visitor.visit(Trie::l1_bits); // flag visitor.visit(const_cast(idx)); return visitor.bytes(); } @@ -39,6 +48,7 @@ template template [[maybe_unused]] std::uint64_t memory_in_bytes(const Trie& idx) { size_visitor visitor; + visitor.visit(Trie::l1_bits); // flag visitor.visit(const_cast(idx)); return visitor.bytes(); } diff --git a/tools/xcdat_decode.cpp b/tools/xcdat_decode.cpp index 6429d2e..d6d30c3 100644 --- a/tools/xcdat_decode.cpp +++ b/tools/xcdat_decode.cpp @@ -7,10 +7,17 @@ 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", "Trie type: [7|8] (default=7)", "-t", false); return p; } +xcdat::flag_type get_flag(std::string_view filepath) { + std::ifstream ifs(filepath); + XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file"); + xcdat::flag_type flag; + ifs.read(reinterpret_cast(&flag), sizeof(flag)); + return flag; +} + template int decode(const cmd_line_parser::parser& p) { const auto input_idx = p.get("input_idx"); @@ -37,9 +44,10 @@ int main(int argc, char** argv) { return 1; } - const auto trie_type = p.get("trie_type", 7); + const auto input_idx = p.get("input_idx"); + const auto flag = get_flag(input_idx); - switch (trie_type) { + switch (flag) { case 7: return decode(p); case 8: diff --git a/tools/xcdat_enumerate.cpp b/tools/xcdat_enumerate.cpp index e87eff1..cd4b99e 100644 --- a/tools/xcdat_enumerate.cpp +++ b/tools/xcdat_enumerate.cpp @@ -7,10 +7,17 @@ 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", "Trie type: [7|8] (default=7)", "-t", false); return p; } +xcdat::flag_type get_flag(std::string_view filepath) { + std::ifstream ifs(filepath); + XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file"); + xcdat::flag_type flag; + ifs.read(reinterpret_cast(&flag), sizeof(flag)); + return flag; +} + template int enumerate(const cmd_line_parser::parser& p) { const auto input_idx = p.get("input_idx"); @@ -34,9 +41,10 @@ int main(int argc, char** argv) { return 1; } - const auto trie_type = p.get("trie_type", 7); + const auto input_idx = p.get("input_idx"); + const auto flag = get_flag(input_idx); - switch (trie_type) { + switch (flag) { case 7: return enumerate(p); case 8: diff --git a/tools/xcdat_lookup.cpp b/tools/xcdat_lookup.cpp index e36e13b..d06d738 100644 --- a/tools/xcdat_lookup.cpp +++ b/tools/xcdat_lookup.cpp @@ -7,10 +7,17 @@ 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", "Trie type: [7|8] (default=7)", "-t", false); return p; } +xcdat::flag_type get_flag(std::string_view filepath) { + std::ifstream ifs(filepath); + XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file"); + xcdat::flag_type flag; + ifs.read(reinterpret_cast(&flag), sizeof(flag)); + return flag; +} + template int lookup(const cmd_line_parser::parser& p) { const auto input_idx = p.get("input_idx"); @@ -41,9 +48,10 @@ int main(int argc, char** argv) { return 1; } - const auto trie_type = p.get("trie_type", 7); + const auto input_idx = p.get("input_idx"); + const auto flag = get_flag(input_idx); - switch (trie_type) { + switch (flag) { case 7: return lookup(p); case 8: diff --git a/tools/xcdat_predictive_search.cpp b/tools/xcdat_predictive_search.cpp index b25dbaf..00f1130 100644 --- a/tools/xcdat_predictive_search.cpp +++ b/tools/xcdat_predictive_search.cpp @@ -8,10 +8,17 @@ 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", "Trie type: [7|8] (default=7)", "-t", false); return p; } +xcdat::flag_type get_flag(std::string_view filepath) { + std::ifstream ifs(filepath); + XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file"); + xcdat::flag_type flag; + ifs.read(reinterpret_cast(&flag), sizeof(flag)); + return flag; +} + template int predictive_search(const cmd_line_parser::parser& p) { const auto input_idx = p.get("input_idx"); @@ -56,9 +63,10 @@ int main(int argc, char** argv) { return 1; } - const auto trie_type = p.get("trie_type", 7); + const auto input_idx = p.get("input_idx"); + const auto flag = get_flag(input_idx); - switch (trie_type) { + switch (flag) { case 7: return predictive_search(p); case 8: diff --git a/tools/xcdat_prefix_search.cpp b/tools/xcdat_prefix_search.cpp index d52f26f..ac2f81b 100644 --- a/tools/xcdat_prefix_search.cpp +++ b/tools/xcdat_prefix_search.cpp @@ -7,10 +7,17 @@ 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", "Trie type: [7|8] (default=7)", "-t", false); return p; } +xcdat::flag_type get_flag(std::string_view filepath) { + std::ifstream ifs(filepath); + XCDAT_THROW_IF(!ifs.good(), "Cannot open the input file"); + xcdat::flag_type flag; + ifs.read(reinterpret_cast(&flag), sizeof(flag)); + return flag; +} + template int prefix_search(const cmd_line_parser::parser& p) { const auto input_idx = p.get("input_idx"); @@ -54,9 +61,10 @@ int main(int argc, char** argv) { return 1; } - const auto trie_type = p.get("trie_type", 7); + const auto input_idx = p.get("input_idx"); + const auto flag = get_flag(input_idx); - switch (trie_type) { + switch (flag) { case 7: return prefix_search(p); case 8: