fix sample

This commit is contained in:
Shunsuke Kanda 2021-06-29 10:27:02 +09:00
parent 62129f4b29
commit 4271cc6db8

View file

@ -14,8 +14,10 @@ int main() {
std::sort(keys.begin(), keys.end());
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
const char* index_filename = "tmp.idx";
// The trie index type
using trie_type = xcdat::trie_8_type;
const std::string index_filename = "tmp.idx";
// Build and save the trie index.
{
@ -26,33 +28,53 @@ int main() {
// Load the trie index.
const auto trie = xcdat::load<trie_type>(index_filename);
std::cout << "Basic operations" << std::endl;
// Lookup
{
const auto id = trie.lookup("MacBook_Pro");
if (id.has_value()) {
std::cout << trie.decode(id.value()) << " -> " << id.has_value() << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
const auto id = trie.lookup("Mac_Pro");
std::cout << "lookup(Mac_Pro) = " << id.value_or(UINT64_MAX) << std::endl;
}
{
const auto id = trie.lookup("Google_Pixel");
std::cout << "lookup(Google_Pixel) = " << id.value_or(UINT64_MAX) << std::endl;
}
std::cout << "Common prefix search" << std::endl;
// Decoding
{
const auto dec = trie.decode(4);
std::cout << "decode(4) = " << dec << std::endl;
}
// Common prefix search
{
std::cout << "common_prefix_search(MacBook_Air) = {" << std::endl;
auto itr = trie.make_prefix_iterator("MacBook_Air");
while (itr.next()) {
std::cout << itr.decoded_view() << " -> " << itr.id() << std::endl;
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
}
std::cout << "}" << std::endl;
}
std::cout << "Predictive search" << std::endl;
// Predictive search
{
std::cout << "predictive_search(Mac) = {" << std::endl;
auto itr = trie.make_predictive_iterator("Mac");
while (itr.next()) {
std::cout << itr.decoded_view() << " -> " << itr.id() << std::endl;
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
}
std::cout << "}" << std::endl;
}
std::remove(index_filename.c_str());
// Enumerate all the keys in the trie (in lex order).
{
std::cout << "enumerate() = {" << std::endl;
auto itr = trie.make_enumerative_iterator();
while (itr.next()) {
std::cout << " (" << itr.decoded_view() << ", " << itr.id() << ")," << std::endl;
}
std::cout << "}" << std::endl;
}
std::remove(index_filename);
return 0;
}