xcdat/include/xcdat.hpp

69 lines
1.8 KiB
C++
Raw Normal View History

2021-06-25 17:18:57 +00:00
#pragma once
2021-06-26 14:48:29 +00:00
2021-06-27 05:06:08 +00:00
#include "xcdat/bc_vector_7.hpp"
2021-06-27 04:12:35 +00:00
#include "xcdat/bc_vector_8.hpp"
2021-06-29 00:06:40 +00:00
#include "xcdat/load_visitor.hpp"
#include "xcdat/mmap_visitor.hpp"
#include "xcdat/save_visitor.hpp"
#include "xcdat/size_visitor.hpp"
2021-06-29 00:27:37 +00:00
#include "xcdat/trie.hpp"
2021-06-29 00:06:40 +00:00
2021-06-26 22:40:15 +00:00
namespace xcdat {
2021-06-27 05:06:08 +00:00
using trie_7_type = trie<bc_vector_7>;
2021-06-27 04:12:35 +00:00
using trie_8_type = trie<bc_vector_8>;
2021-06-26 22:40:15 +00:00
2021-06-29 03:02:51 +00:00
using flag_type = std::remove_const_t<decltype(trie_7_type::l1_bits)>;
2021-06-29 00:06:40 +00:00
template <class Trie>
2021-06-29 00:27:37 +00:00
[[maybe_unused]] Trie mmap(const char* address) {
2021-06-29 00:06:40 +00:00
mmap_visitor visitor(address);
2021-06-29 03:02:51 +00:00
flag_type flag;
visitor.visit(flag);
XCDAT_THROW_IF(flag != Trie::l1_bits, "The input index type is different.");
Trie idx;
2021-06-29 00:06:40 +00:00
visitor.visit(idx);
return idx;
}
template <class Trie>
2021-06-29 00:27:37 +00:00
[[maybe_unused]] Trie load(std::string_view filepath) {
2021-06-29 00:06:40 +00:00
load_visitor visitor(filepath);
2021-06-29 03:02:51 +00:00
flag_type flag;
visitor.visit(flag);
XCDAT_THROW_IF(flag != Trie::l1_bits, "The input index type is different.");
Trie idx;
2021-06-29 00:06:40 +00:00
visitor.visit(idx);
return idx;
}
template <class Trie>
2021-06-29 00:27:37 +00:00
[[maybe_unused]] std::uint64_t save(const Trie& idx, std::string_view filepath) {
2021-06-29 00:06:40 +00:00
save_visitor visitor(filepath);
2021-06-29 03:02:51 +00:00
visitor.visit(Trie::l1_bits); // flag
2021-06-29 00:06:40 +00:00
visitor.visit(const_cast<Trie&>(idx));
return visitor.bytes();
}
template <class Trie>
2021-06-29 00:27:37 +00:00
[[maybe_unused]] std::uint64_t memory_in_bytes(const Trie& idx) {
2021-06-29 00:06:40 +00:00
size_visitor visitor;
2021-06-29 03:02:51 +00:00
visitor.visit(Trie::l1_bits); // flag
2021-06-29 00:06:40 +00:00
visitor.visit(const_cast<Trie&>(idx));
return visitor.bytes();
}
2021-06-29 00:27:37 +00:00
[[maybe_unused]] std::vector<std::string> load_strings(std::string_view filepath) {
std::ifstream ifs(filepath);
if (!ifs) {
return {};
}
std::vector<std::string> strs;
for (std::string str; std::getline(ifs, str);) {
strs.push_back(str);
}
return strs;
}
2021-06-27 05:06:08 +00:00
} // namespace xcdat